Skip to content

Commit 9ee36df

Browse files
authored
Merge pull request #18139 from grokability/#18082-add-company-to-seat-view
Fixed #18082: Added user company to checked out licenses
2 parents 6189501 + 46d1c14 commit 9ee36df

File tree

4 files changed

+37
-12
lines changed

4 files changed

+37
-12
lines changed

app/Http/Controllers/Api/LicenseSeatsController.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ public function index(Request $request, $licenseId) : JsonResponse | array
2626
if ($license = License::find($licenseId)) {
2727
$this->authorize('view', $license);
2828

29-
$seats = LicenseSeat::with('license', 'user', 'asset', 'user.department')
29+
$seats = LicenseSeat::with('license', 'user', 'asset', 'user.department', 'user.company', 'asset.company')
3030
->where('license_seats.license_id', $licenseId);
3131

3232
if ($request->input('status') == 'available') {
33-
$seats->whereNull('license_seats.assigned_to');
33+
$seats->whereNull('license_seats.assigned_to')->whereNull('license_seats.asset_id');
3434
}
3535

3636
if ($request->input('status') == 'assigned') {
@@ -40,8 +40,10 @@ public function index(Request $request, $licenseId) : JsonResponse | array
4040

4141
$order = $request->input('order') === 'asc' ? 'asc' : 'desc';
4242

43-
if ($request->input('sort') == 'department') {
43+
if ($request->input('sort') == 'assigned_user.department') {
4444
$seats->OrderDepartments($order);
45+
} elseif ($request->input('sort') == 'assigned_user.company') {
46+
$seats->OrderCompany($order);
4547
} else {
4648
$seats->orderBy('updated_at', $order);
4749
}
@@ -83,7 +85,7 @@ public function show($licenseId, $seatId) : JsonResponse | array
8385
return response()->json(Helper::formatStandardApiResponse('error', null, 'Seat not found'));
8486
}
8587
// 2. does the seat belong to the specified license?
86-
if (! $license = $licenseSeat->license()->first() || $license->id != intval($licenseId)) {
88+
if (! $licenseSeat = $licenseSeat->license()->first() || $licenseSeat->id != intval($licenseId)) {
8789
return response()->json(Helper::formatStandardApiResponse('error', null, 'Seat does not belong to the specified license'));
8890
}
8991

app/Http/Transformers/LicenseSeatsTransformer.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ public function transformLicenseSeat(LicenseSeat $seat)
3636
'name' => e($seat->user->department->name),
3737

3838
] : null,
39+
'company'=> ($seat->user->company) ?
40+
[
41+
'id' => (int) $seat->user->company->id,
42+
'name' => e($seat->user->company->name),
43+
44+
] : null,
3945
'created_at' => Helper::getFormattedDateObject($seat->created_at, 'datetime'),
4046
] : null,
4147
'assigned_asset' => ($seat->asset) ? [

app/Models/LicenseSeat.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,17 +153,24 @@ public function scopeOrderDepartments($query, $order)
153153
}
154154

155155

156+
public function scopeOrderCompany($query, $order)
157+
{
158+
159+
160+
return $query->leftJoin('users as license_seat_users', 'license_seats.assigned_to', '=', 'license_seat_users.id')
161+
->leftJoin('companies as license_user_company', 'license_user_company.id', '=', 'license_seat_users.company_id')
162+
->whereNotNull('license_seats.assigned_to')
163+
->orderBy('license_user_company.name', $order);
164+
}
165+
166+
156167
public function scopeByAssigned($query)
157168
{
158169

159170
return $query->where(
160171
function ($query) {
161172
$query->whereNotNull('assigned_to')
162-
->orWhere(
163-
function ($query) {
164-
$query->whereNotNull('asset_id');
165-
}
166-
);
173+
->orWhereNotNull('asset_id');
167174
}
168175
);
169176

app/Presenters/LicensePresenter.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,20 @@ public static function dataTableLayoutSeats()
248248
'title' => trans('admin/users/table.email'),
249249
'visible' => true,
250250
'formatter' => 'emailFormatter',
251-
], [
252-
'field' => 'department',
251+
],
252+
[
253+
'field' => 'assigned_user.company',
253254
'searchable' => false,
254-
'sortable' => true,
255+
'sortable' => false,
256+
'switchable' => true,
257+
'title' => trans('general.company'),
258+
'visible' => true,
259+
'formatter' => 'companiesLinkObjFormatter',
260+
],
261+
[
262+
'field' => 'assigned_user.department',
263+
'searchable' => false,
264+
'sortable' => false,
255265
'switchable' => true,
256266
'title' => trans('general.department'),
257267
'visible' => false,

0 commit comments

Comments
 (0)