Skip to content

Commit b16970a

Browse files
authored
Merge pull request #18629 from Godmartinz/update-print-invtentory-view-with-assigned2assets
Update print inventory view with indirect assignments table
2 parents 13dc7de + 8747ff3 commit b16970a

5 files changed

Lines changed: 152 additions & 63 deletions

File tree

app/Http/Controllers/ProfileController.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,19 @@ public function getMenuState(Request $request): void
211211
*/
212212
public function printInventory(): View
213213
{
214-
$show_users = User::where('id', auth()->user()->id)->get();
215-
216-
return view('users/print')
217-
->with('assets', auth()->user()->assets())
218-
->with('licenses', auth()->user()->licenses()->get())
219-
->with('accessories', auth()->user()->accessories()->get())
220-
->with('consumables', auth()->user()->consumables()->get())
221-
->with('users', $show_users)
214+
$userId = auth()->id();
215+
216+
$show_user = User::withInventoryRelations($userId)->first();
217+
218+
$indirectItemsCount =
219+
$show_user->assets->flatMap->assignedAssets->count()
220+
+ $show_user->assets->flatMap->components->count()
221+
+ $show_user->assets->flatMap->licenses->count()
222+
+ $show_user->assets->flatMap->assignedAccessories->count();
223+
224+
return view('users.print')
225+
->with('users', [$show_user])
226+
->with('indirectItemsCount', $indirectItemsCount)
222227
->with('settings', Setting::getSettings());
223228
}
224229

app/Http/Controllers/Users/UsersController.php

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -649,32 +649,16 @@ public function printInventory($id)
649649
{
650650
$this->authorize('view', User::class);
651651

652-
$user = User::where('id', $id)
653-
->with([
654-
'assets.log' => fn ($query) => $query->withTrashed()->where('target_type', User::class)->where('target_id', $id)->where('action_type', 'accepted'),
655-
'assets.assignedAssets.log' => fn ($query) => $query->withTrashed()->where('target_type', User::class)->where('target_id', $id)->where('action_type', 'accepted'),
656-
'assets.assignedAssets.defaultLoc',
657-
'assets.assignedAssets.location',
658-
'assets.assignedAssets.model.category',
659-
'assets.defaultLoc',
660-
'assets.location',
661-
'assets.model.category',
662-
'accessories.log' => fn ($query) => $query->withTrashed()->where('target_type', User::class)->where('target_id', $id)->where('action_type', 'accepted'),
663-
'accessories.category',
664-
'accessories.manufacturer',
665-
'consumables.log' => fn ($query) => $query->withTrashed()->where('target_type', User::class)->where('target_id', $id)->where('action_type', 'accepted'),
666-
'consumables.category',
667-
'consumables.manufacturer',
668-
'licenses.category',
669-
])
670-
->withTrashed()
671-
->first();
652+
$user = User::withInventoryRelations($id)->first();
653+
654+
$indirectItemsCount = $user?->assets?->flatMap->assignedAssets->count() + $user?->assets?->flatMap->components->count() + $user?->assets?->flatMap->licenses->count() + $user?->assets?->flatMap->assignedAccessories->count();
672655

673656
if ($user) {
674657
$this->authorize('view', $user);
675658

676659
return view('users.print')
677660
->with('users', [$user])
661+
->with('indirectItemsCount', $indirectItemsCount)
678662
->with('settings', Setting::getSettings());
679663
}
680664

app/Models/User.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,10 @@ public function licenses()
725725
{
726726
return $this->belongsToMany(License::class, 'license_seats', 'assigned_to', 'license_id')->withPivot('id', 'created_at', 'updated_at');
727727
}
728+
public function directLicenses()
729+
{
730+
return $this->belongsToMany(\App\Models\License::class, 'license_seats', 'assigned_to', 'license_id')->withPivot('id', 'created_at', 'updated_at')->wherePivotNull('asset_id')->withTrashed();
731+
}
728732

729733
/**
730734
* Establishes the user -> reportTemplates relationship
@@ -1389,7 +1393,47 @@ public function scopeUserLocation($query, $location, $search)
13891393
->orwhereRaw('CONCAT(users.first_name," ",users.last_name) LIKE \''.$search.'%\'');
13901394

13911395
}
1392-
1396+
public function scopeWithInventoryRelations($query, int $id)
1397+
{
1398+
return $query->where('id', $id)
1399+
->with([
1400+
'assets.log' => fn ($query) => $query->withTrashed()
1401+
->where('target_type', User::class)
1402+
->where('target_id', $id)
1403+
->where('action_type', 'accepted'),
1404+
'assets.defaultLoc',
1405+
'assets.location',
1406+
'assets.model.category',
1407+
'assets.assignedAssets.log' => fn ($query) => $query->withTrashed()
1408+
->where('target_type', User::class)
1409+
->where('target_id', $id)
1410+
->where('action_type', 'accepted'),
1411+
'assets.assignedAssets.assignedTo',
1412+
'assets.assignedAssets.defaultLoc',
1413+
'assets.assignedAssets.location',
1414+
'assets.assignedAssets.model.category',
1415+
'assets.components.category',
1416+
'assets.licenses',
1417+
'assets.licenses.category',
1418+
'assets.assignedAccessories',
1419+
'assets.assignedAccessories.accessory.category',
1420+
'accessories.log' => fn ($query) => $query->withTrashed()
1421+
->where('target_type', User::class)
1422+
->where('target_id', $id)
1423+
->where('action_type', 'accepted'),
1424+
'accessories.category',
1425+
'accessories.manufacturer',
1426+
'consumables.log' => fn ($query) => $query->withTrashed()
1427+
->where('target_type', User::class)
1428+
->where('target_id', $id)
1429+
->where('action_type', 'accepted'),
1430+
'consumables.category',
1431+
'consumables.manufacturer',
1432+
'directLicenses.category',
1433+
'licenses.category',
1434+
])
1435+
->withTrashed();
1436+
}
13931437
/**
13941438
* Get all direct and indirect subordinates for this user.
13951439
*

resources/lang/en-US/mail.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
'asset_tag' => 'Asset Tag',
4949
'assets_warrantee_alert' => 'There is :count asset with an expiring warranty or that are reaching their end of life in the next :threshold days.|There are :count assets with expiring warranties or that are reaching their end of life in the next :threshold days.',
5050
'assigned_to' => 'Assigned To',
51+
'assigned_to_assets' => 'Assignments to Assets',
5152
'eol' => 'EOL',
5253
'best_regards' => 'Best regards,',
5354
'canceled' => 'Canceled',

resources/views/users/print.blade.php

Lines changed: 89 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -168,39 +168,6 @@ class="snipe-table table table-striped inventory"
168168
@endif
169169
</td>
170170
</tr>
171-
@if ($settings->show_assigned_assets)
172-
@php
173-
$assignedCounter = 1;
174-
@endphp
175-
@foreach ($asset->assignedAssets as $asset)
176-
<tr>
177-
<td>{{ $counter }}.{{ $assignedCounter }}</td>
178-
<td>
179-
@if ($asset->getImageUrl())
180-
<img src="{{ $asset->getImageUrl() }}" class="thumbnail" style="max-height: 50px;">
181-
@endif
182-
</td>
183-
<td>{{ $asset->asset_tag }}</td>
184-
<td>{{ $asset->name }}</td>
185-
<td>{{ (($asset->model) && ($asset->model->category)) ? $asset->model->category->name : trans('general.invalid_category') }}</td>
186-
<td>{{ ($asset->model) ? $asset->model->name : trans('general.invalid_model') }}</td>
187-
<td>{{ ($asset->defaultLoc) ? $asset->defaultLoc->name : '' }}</td>
188-
<td>{{ ($asset->location) ? $asset->location->name : '' }}</td>
189-
<td>{{ $asset->serial }}</td>
190-
<td>
191-
{{ Helper::getFormattedDateObject($asset->last_checkout, 'datetime', false) }}
192-
</td>
193-
<td>
194-
@if ($asset->getLatestSignedAcceptance($show_user))
195-
<img style="width:auto;height:100px;" src="{{ asset('/') }}display-sig/{{ $asset->getLatestSignedAcceptance($show_user)->accept_signature }}">
196-
@endif
197-
</td>
198-
</tr>
199-
@php
200-
$assignedCounter++
201-
@endphp
202-
@endforeach
203-
@endif
204171
@php
205172
$counter++
206173
@endphp
@@ -240,7 +207,7 @@ class="snipe-table table table-striped inventory"
240207
$lcounter = 1;
241208
@endphp
242209

243-
@foreach ($show_user->licenses as $license)
210+
@foreach ($show_user->directLicenses as $license)
244211
@php
245212
if (($license->category) && ($license->category->getEula())) $eulas[] = $license->category->getEula()
246213
@endphp
@@ -398,7 +365,95 @@ class="snipe-table table table-striped inventory"
398365
@endforeach
399366
</table>
400367
@endif
368+
@if($indirectItemsCount > 0 && $settings->show_assigned_assets)
369+
<div id="indirect-assignments-toolbar">
370+
<h4>{{ $indirectItemsCount.' '.trans('mail.assigned_to_assets') }}</h4>
371+
</div>
372+
<table
373+
class="snipe-table table table-striped inventory"
374+
id="indirect-assignments"
375+
data-pagination="false"
376+
data-toolbar="#indirect-assignments-toolbar"
377+
data-id-table="indirect-assignments"
378+
data-search="false"
379+
data-side-pagination="client"
380+
data-sortable="true"
381+
data-sort-order="desc"
382+
data-sort-name="name"
383+
data-show-columns="true"
384+
data-cookie-id-table="indirect-assignments">
385+
<thead>
386+
@php
387+
$indirectAssignmentsCounter = 1;
388+
@endphp
389+
<tr>
390+
<th style="width: 20px;" data-sortable="false" data-switchable="false">#</th>
391+
<th style="width: 40%;" data-sortable="true" data-switchable="false">{{ trans('mail.assigned_to') }}</th>
392+
<th style="width: 50%;" data-sortable="true">{{ trans('general.category') }}</th>
393+
<th style="width: 10%;" data-sortable="true">{{ trans('mail.item') }}</th>
394+
<th style="width: 10%;" data-sortable="true">{{ trans('general.quantity') }}</th>
395+
</tr>
396+
</thead>
401397

398+
@foreach ($show_user->assets as $asset)
399+
@foreach ($asset->assignedAssets as $indirectAsset)
400+
<tr>
401+
<td>{{ $indirectAssignmentsCounter }}</td>
402+
<td>{{ $asset->display_name ?? ''}}</td>
403+
<td>{{ (($indirectAsset->model) && ($indirectAsset->model->category)) ? $indirectAsset->model->category->name : trans('general.invalid_category') }}</td>
404+
<td>{{ $indirectAsset->display_name ?? '' }}</td>
405+
<td>1</td>
406+
407+
</tr>
408+
@php
409+
$indirectAssignmentsCounter++
410+
@endphp
411+
@endforeach
412+
@foreach ($asset->licenses as $indirectLicense)
413+
@if($indirectLicense)
414+
<tr>
415+
<td>{{$indirectAssignmentsCounter}}</td>
416+
<td>{{ $asset->display_name ?? ''}}</td>
417+
<td>{{ $indirectLicense->category?->name ?? '' }}</td>
418+
<td>{{ $indirectLicense->name ?? '' }}</td>
419+
<td>1</td>
420+
</tr>
421+
@endif
422+
@php
423+
$indirectAssignmentsCounter ++
424+
@endphp
425+
@endforeach
426+
@foreach ($asset->components as $component)
427+
@if($component)
428+
<tr>
429+
<td>{{$indirectAssignmentsCounter}}</td>
430+
<td>{{ $asset->display_name ?? ''}}</td>
431+
<td>{{ $component->category?->name ?? '' }}</td>
432+
<td>{{ $component->name ?? '' }}</td>
433+
<td>{{ $component->pivot->assigned_qty }}</td>
434+
</tr>
435+
@endif
436+
@php
437+
$indirectAssignmentsCounter ++
438+
@endphp
439+
@endforeach
440+
@foreach ($asset->assignedAccessories as $indirectAccessory)
441+
@if($indirectAccessory)
442+
<tr>
443+
<td>{{$indirectAssignmentsCounter}}</td>
444+
<td>{{ $asset->display_name ?? '' }}</td>
445+
<td>{{ $indirectAccessory->accessory->category?->name ?? '' }}</td>
446+
<td>{{ $indirectAccessory->accessory->name ?? '' }}</td>
447+
<td>1</td>
448+
</tr>
449+
@endif
450+
@php
451+
$indirectAssignmentsCounter ++
452+
@endphp
453+
@endforeach
454+
@endforeach
455+
</table>
456+
@endif
402457
@php
403458
if (!empty($eulas)) $eulas = array_unique($eulas);
404459
@endphp

0 commit comments

Comments
 (0)