Order by created_by for depreciations

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2024-09-19 17:41:05 +01:00
parent 822ea96dfd
commit 82d6e1510f
4 changed files with 48 additions and 4 deletions

View file

@ -32,7 +32,7 @@ class DepreciationsController extends Controller
'licenses_count', 'licenses_count',
]; ];
$depreciations = Depreciation::select('id','name','months','depreciation_min','depreciation_type','created_at','updated_at') $depreciations = Depreciation::select('id','name','months','depreciation_min','depreciation_type','created_at','updated_at', 'created_by')
->with('adminuser') ->with('adminuser')
->withCount('assets as assets_count') ->withCount('assets as assets_count')
->withCount('models as models_count') ->withCount('models as models_count')
@ -45,12 +45,22 @@ class DepreciationsController extends Controller
// Make sure the offset and limit are actually integers and do not exceed system limits // Make sure the offset and limit are actually integers and do not exceed system limits
$offset = ($request->input('offset') > $depreciations->count()) ? $depreciations->count() : app('api_offset_value'); $offset = ($request->input('offset') > $depreciations->count()) ? $depreciations->count() : app('api_offset_value');
$limit = app('api_limit_value'); $limit = app('api_limit_value');
$order = $request->input('order') === 'asc' ? 'asc' : 'desc'; $order = $request->input('order') === 'asc' ? 'asc' : 'desc';
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at'; $sort_override = $request->input('sort');
$depreciations->orderBy($sort, $order); $column_sort = in_array($sort_override, $allowed_columns) ? $sort_override : 'created_at';
switch ($sort_override) {
case 'created_by':
$depreciations = $depreciations->OrderByCreatedBy($order);
break;
default:
$depreciations = $depreciations->orderBy($column_sort, $order);
break;
}
$total = $depreciations->count(); $total = $depreciations->count();
$components = $depreciations->skip($offset)->take($limit)->get();
$depreciations = $depreciations->skip($offset)->take($limit)->get(); $depreciations = $depreciations->skip($offset)->take($limit)->get();
return (new DepreciationsTransformer)->transformDepreciations($depreciations, $total); return (new DepreciationsTransformer)->transformDepreciations($depreciations, $total);

View file

@ -31,6 +31,10 @@ class DepreciationsTransformer
'assets_count' => $depreciation->assets_count, 'assets_count' => $depreciation->assets_count,
'models_count' => $depreciation->models_count, 'models_count' => $depreciation->models_count,
'licenses_count' => $depreciation->licenses_count, 'licenses_count' => $depreciation->licenses_count,
'created_by' => ($depreciation->adminuser) ? [
'id' => (int) $depreciation->adminuser->id,
'name'=> e($depreciation->adminuser->present()->fullName()),
] : null,
'created_at' => Helper::getFormattedDateObject($depreciation->created_at, 'datetime'), 'created_at' => Helper::getFormattedDateObject($depreciation->created_at, 'datetime'),
'updated_at' => Helper::getFormattedDateObject($depreciation->updated_at, 'datetime') 'updated_at' => Helper::getFormattedDateObject($depreciation->updated_at, 'datetime')
]; ];

View file

@ -88,4 +88,27 @@ class Depreciation extends SnipeModel
return $this->hasManyThrough(\App\Models\Asset::class, \App\Models\AssetModel::class, 'depreciation_id', 'model_id'); return $this->hasManyThrough(\App\Models\Asset::class, \App\Models\AssetModel::class, 'depreciation_id', 'model_id');
} }
/**
* Get the user that created the depreciation
*
* @author A. Gianotto <snipe@snipe.net>
* @since [v7.0.13]
* @return \Illuminate\Database\Eloquent\Relations\Relation
*/
public function adminuser()
{
return $this->belongsTo(\App\Models\User::class, 'created_by');
}
/**
* -----------------------------------------------
* BEGIN QUERY SCOPES
* -----------------------------------------------
**/
public function scopeOrderByCreatedBy($query, $order)
{
return $query->leftJoin('users as admin_sort', 'depreciations.created_by', '=', 'admin_sort.id')->select('depreciations.*')->orderBy('admin_sort.first_name', $order)->orderBy('admin_sort.last_name', $order);
}
} }

View file

@ -82,6 +82,13 @@ class Group extends SnipeModel
return json_decode($this->permissions, true); return json_decode($this->permissions, true);
} }
/**
* -----------------------------------------------
* BEGIN QUERY SCOPES
* -----------------------------------------------
**/
public function scopeOrderByCreatedBy($query, $order) public function scopeOrderByCreatedBy($query, $order)
{ {
return $query->leftJoin('users as admin_sort', 'permission_groups.created_by', '=', 'admin_sort.id')->select('permission_groups.*')->orderBy('admin_sort.first_name', $order)->orderBy('admin_sort.last_name', $order); return $query->leftJoin('users as admin_sort', 'permission_groups.created_by', '=', 'admin_sort.id')->select('permission_groups.*')->orderBy('admin_sort.first_name', $order)->orderBy('admin_sort.last_name', $order);