From 82d6e1510f6a8472e9cbfb70342113497a6a692d Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 19 Sep 2024 17:41:05 +0100 Subject: [PATCH] Order by created_by for depreciations Signed-off-by: snipe --- .../Api/DepreciationsController.php | 18 +++++++++++---- .../Transformers/DepreciationsTransformer.php | 4 ++++ app/Models/Depreciation.php | 23 +++++++++++++++++++ app/Models/Group.php | 7 ++++++ 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/Api/DepreciationsController.php b/app/Http/Controllers/Api/DepreciationsController.php index 6a2d105282..9d579d21cb 100644 --- a/app/Http/Controllers/Api/DepreciationsController.php +++ b/app/Http/Controllers/Api/DepreciationsController.php @@ -32,7 +32,7 @@ class DepreciationsController extends Controller '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') ->withCount('assets as assets_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 $offset = ($request->input('offset') > $depreciations->count()) ? $depreciations->count() : app('api_offset_value'); $limit = app('api_limit_value'); - $order = $request->input('order') === 'asc' ? 'asc' : 'desc'; - $sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at'; - $depreciations->orderBy($sort, $order); + $sort_override = $request->input('sort'); + $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(); + $components = $depreciations->skip($offset)->take($limit)->get(); + $depreciations = $depreciations->skip($offset)->take($limit)->get(); return (new DepreciationsTransformer)->transformDepreciations($depreciations, $total); diff --git a/app/Http/Transformers/DepreciationsTransformer.php b/app/Http/Transformers/DepreciationsTransformer.php index b3dc8c5aae..64d4c88f7e 100644 --- a/app/Http/Transformers/DepreciationsTransformer.php +++ b/app/Http/Transformers/DepreciationsTransformer.php @@ -31,6 +31,10 @@ class DepreciationsTransformer 'assets_count' => $depreciation->assets_count, 'models_count' => $depreciation->models_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'), 'updated_at' => Helper::getFormattedDateObject($depreciation->updated_at, 'datetime') ]; diff --git a/app/Models/Depreciation.php b/app/Models/Depreciation.php index 7aceddf7c4..11ee82c16a 100755 --- a/app/Models/Depreciation.php +++ b/app/Models/Depreciation.php @@ -88,4 +88,27 @@ class Depreciation extends SnipeModel 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 + * @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); + } } diff --git a/app/Models/Group.php b/app/Models/Group.php index b41670f2f5..7278152df9 100755 --- a/app/Models/Group.php +++ b/app/Models/Group.php @@ -82,6 +82,13 @@ class Group extends SnipeModel return json_decode($this->permissions, true); } + /** + * ----------------------------------------------- + * BEGIN QUERY SCOPES + * ----------------------------------------------- + **/ + + 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);