From 57f6ecb1da5d3186e939858a009fb8b3389bb088 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 26 Jul 2024 10:24:13 +0100 Subject: [PATCH] Added created_by to license view Signed-off-by: snipe --- app/Http/Controllers/Api/LicensesController.php | 8 +++++++- app/Http/Transformers/LicensesTransformer.php | 4 ++++ app/Models/License.php | 13 +++++++++++++ app/Presenters/LicensePresenter.php | 7 +++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/Api/LicensesController.php b/app/Http/Controllers/Api/LicensesController.php index 579d80eeb5..71ad01b59b 100644 --- a/app/Http/Controllers/Api/LicensesController.php +++ b/app/Http/Controllers/Api/LicensesController.php @@ -24,7 +24,7 @@ class LicensesController extends Controller { $this->authorize('view', License::class); - $licenses = License::with('company', 'manufacturer', 'supplier','category')->withCount('freeSeats as free_seats_count'); + $licenses = License::with('company', 'manufacturer', 'supplier','category', 'adminuser')->withCount('freeSeats as free_seats_count'); if ($request->filled('company_id')) { $licenses->where('company_id', '=', $request->input('company_id')); @@ -70,6 +70,9 @@ class LicensesController extends Controller $licenses->where('depreciation_id', '=', $request->input('depreciation_id')); } + if ($request->filled('user_id')) { + $licenses->where('user_id', '=', $request->input('user_id')); + } if (($request->filled('maintained')) && ($request->input('maintained')=='true')) { $licenses->where('maintained','=',1); @@ -113,6 +116,9 @@ class LicensesController extends Controller case 'company': $licenses = $licenses->leftJoin('companies', 'licenses.company_id', '=', 'companies.id')->orderBy('companies.name', $order); break; + case 'created_by': + $licenses = $licenses->OrderCreatedBy($order); + break; default: $allowed_columns = [ diff --git a/app/Http/Transformers/LicensesTransformer.php b/app/Http/Transformers/LicensesTransformer.php index fa218da4d1..4fad9b9a68 100644 --- a/app/Http/Transformers/LicensesTransformer.php +++ b/app/Http/Transformers/LicensesTransformer.php @@ -45,6 +45,10 @@ class LicensesTransformer 'maintained' => ($license->maintained == 1) ? true : false, 'supplier' => ($license->supplier) ? ['id' => (int) $license->supplier->id, 'name'=> e($license->supplier->name)] : null, 'category' => ($license->category) ? ['id' => (int) $license->category->id, 'name'=> e($license->category->name)] : null, + 'created_by' => ($license->adminuser) ? [ + 'id' => (int) $license->adminuser->id, + 'name'=> e($license->adminuser->present()->fullName()), + ] : null, 'created_at' => Helper::getFormattedDateObject($license->created_at, 'datetime'), 'updated_at' => Helper::getFormattedDateObject($license->updated_at, 'datetime'), 'deleted_at' => Helper::getFormattedDateObject($license->deleted_at, 'datetime'), diff --git a/app/Models/License.php b/app/Models/License.php index 2680424a84..554929c0ac 100755 --- a/app/Models/License.php +++ b/app/Models/License.php @@ -736,4 +736,17 @@ class License extends Depreciable return $query->leftJoin('companies as companies', 'licenses.company_id', '=', 'companies.id')->select('licenses.*') ->orderBy('companies.name', $order); } + + /** + * Query builder scope to order on the user that created it + * + * @param \Illuminate\Database\Query\Builder $query Query builder instance + * @param text $order Order + * + * @return \Illuminate\Database\Query\Builder Modified query builder + */ + public function scopeOrderCreatedBy($query, $order) + { + return $query->leftJoin('users as users_sort', 'licenses.user_id', '=', 'users_sort.id')->select('licenses.*')->orderBy('users_sort.first_name', $order)->orderBy('users_sort.last_name', $order); + } } \ No newline at end of file diff --git a/app/Presenters/LicensePresenter.php b/app/Presenters/LicensePresenter.php index 8ca8e120f2..1545cabd30 100644 --- a/app/Presenters/LicensePresenter.php +++ b/app/Presenters/LicensePresenter.php @@ -158,6 +158,13 @@ class LicensePresenter extends Presenter 'sortable' => true, 'visible' => false, 'title' => trans('general.order_number'), + ], [ + 'field' => 'created_by', + 'searchable' => false, + 'sortable' => true, + 'title' => trans('general.admin'), + 'visible' => false, + 'formatter' => 'usersLinkObjFormatter', ], [ 'field' => 'created_at', 'searchable' => false,