diff --git a/app/Http/Controllers/Api/LicenseSeatsController.php b/app/Http/Controllers/Api/LicenseSeatsController.php index 247f71ff26..f887de63c3 100644 --- a/app/Http/Controllers/Api/LicenseSeatsController.php +++ b/app/Http/Controllers/Api/LicenseSeatsController.php @@ -26,11 +26,11 @@ class LicenseSeatsController extends Controller if ($license = License::find($licenseId)) { $this->authorize('view', $license); - $seats = LicenseSeat::with('license', 'user', 'asset', 'user.department') + $seats = LicenseSeat::with('license', 'user', 'asset', 'user.department', 'user.company', 'asset.company') ->where('license_seats.license_id', $licenseId); if ($request->input('status') == 'available') { - $seats->whereNull('license_seats.assigned_to'); + $seats->whereNull('license_seats.assigned_to')->whereNull('license_seats.asset_id'); } if ($request->input('status') == 'assigned') { @@ -40,8 +40,10 @@ class LicenseSeatsController extends Controller $order = $request->input('order') === 'asc' ? 'asc' : 'desc'; - if ($request->input('sort') == 'department') { + if ($request->input('sort') == 'assigned_user.department') { $seats->OrderDepartments($order); + } elseif ($request->input('sort') == 'assigned_user.company') { + $seats->OrderCompany($order); } else { $seats->orderBy('updated_at', $order); } @@ -83,7 +85,7 @@ class LicenseSeatsController extends Controller return response()->json(Helper::formatStandardApiResponse('error', null, 'Seat not found')); } // 2. does the seat belong to the specified license? - if (! $license = $licenseSeat->license()->first() || $license->id != intval($licenseId)) { + if (! $licenseSeat = $licenseSeat->license()->first() || $licenseSeat->id != intval($licenseId)) { return response()->json(Helper::formatStandardApiResponse('error', null, 'Seat does not belong to the specified license')); } diff --git a/app/Http/Transformers/LicenseSeatsTransformer.php b/app/Http/Transformers/LicenseSeatsTransformer.php index d018934e18..c518c67d22 100644 --- a/app/Http/Transformers/LicenseSeatsTransformer.php +++ b/app/Http/Transformers/LicenseSeatsTransformer.php @@ -36,6 +36,12 @@ class LicenseSeatsTransformer 'name' => e($seat->user->department->name), ] : null, + 'company'=> ($seat->user->company) ? + [ + 'id' => (int) $seat->user->company->id, + 'name' => e($seat->user->company->name), + + ] : null, 'created_at' => Helper::getFormattedDateObject($seat->created_at, 'datetime'), ] : null, 'assigned_asset' => ($seat->asset) ? [ diff --git a/app/Models/LicenseSeat.php b/app/Models/LicenseSeat.php index 7c98232863..cc13481329 100755 --- a/app/Models/LicenseSeat.php +++ b/app/Models/LicenseSeat.php @@ -153,17 +153,24 @@ class LicenseSeat extends SnipeModel implements ICompanyableChild } + public function scopeOrderCompany($query, $order) + { + + + return $query->leftJoin('users as license_seat_users', 'license_seats.assigned_to', '=', 'license_seat_users.id') + ->leftJoin('companies as license_user_company', 'license_user_company.id', '=', 'license_seat_users.company_id') + ->whereNotNull('license_seats.assigned_to') + ->orderBy('license_user_company.name', $order); + } + + public function scopeByAssigned($query) { return $query->where( function ($query) { $query->whereNotNull('assigned_to') - ->orWhere( - function ($query) { - $query->whereNotNull('asset_id'); - } - ); + ->orWhereNotNull('asset_id'); } ); diff --git a/app/Presenters/LicensePresenter.php b/app/Presenters/LicensePresenter.php index ea5c3b5d73..7fc1aa5682 100644 --- a/app/Presenters/LicensePresenter.php +++ b/app/Presenters/LicensePresenter.php @@ -248,10 +248,20 @@ class LicensePresenter extends Presenter 'title' => trans('admin/users/table.email'), 'visible' => true, 'formatter' => 'emailFormatter', - ], [ - 'field' => 'department', + ], + [ + 'field' => 'assigned_user.company', 'searchable' => false, - 'sortable' => true, + 'sortable' => false, + 'switchable' => true, + 'title' => trans('general.company'), + 'visible' => true, + 'formatter' => 'companiesLinkObjFormatter', + ], + [ + 'field' => 'assigned_user.department', + 'searchable' => false, + 'sortable' => false, 'switchable' => true, 'title' => trans('general.department'), 'visible' => false,