Skip to content
Open
21 changes: 13 additions & 8 deletions app/Http/Controllers/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,19 @@ public function getMenuState(Request $request): void
*/
public function printInventory(): View
{
$show_users = User::where('id', auth()->user()->id)->get();

return view('users/print')
->with('assets', auth()->user()->assets())
->with('licenses', auth()->user()->licenses()->get())
->with('accessories', auth()->user()->accessories()->get())
->with('consumables', auth()->user()->consumables()->get())
->with('users', $show_users)
$userId = auth()->id();

$show_user = User::withInventoryRelations($userId)->first();

$indirectItemsCount =
$show_user->assets->flatMap->assignedAssets->count()
+ $show_user->assets->flatMap->components->count()
+ $show_user->assets->flatMap->licenses->count()
+ $show_user->assets->flatMap->assignedAccessories->count();

return view('users.print')
->with('users', [$show_user])
->with('indirectItemsCount', $indirectItemsCount)
->with('settings', Setting::getSettings());
}

Expand Down
24 changes: 4 additions & 20 deletions app/Http/Controllers/Users/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -671,32 +671,16 @@ public function printInventory($id)
{
$this->authorize('view', User::class);

$user = User::where('id', $id)
->with([
'assets.log' => fn ($query) => $query->withTrashed()->where('target_type', User::class)->where('target_id', $id)->where('action_type', 'accepted'),
'assets.assignedAssets.log' => fn ($query) => $query->withTrashed()->where('target_type', User::class)->where('target_id', $id)->where('action_type', 'accepted'),
'assets.assignedAssets.defaultLoc',
'assets.assignedAssets.location',
'assets.assignedAssets.model.category',
'assets.defaultLoc',
'assets.location',
'assets.model.category',
'accessories.log' => fn ($query) => $query->withTrashed()->where('target_type', User::class)->where('target_id', $id)->where('action_type', 'accepted'),
'accessories.category',
'accessories.manufacturer',
'consumables.log' => fn ($query) => $query->withTrashed()->where('target_type', User::class)->where('target_id', $id)->where('action_type', 'accepted'),
'consumables.category',
'consumables.manufacturer',
'licenses.category',
])
->withTrashed()
->first();
$user = User::withInventoryRelations($id)->first();

$indirectItemsCount = $user?->assets?->flatMap->assignedAssets->count() + $user?->assets?->flatMap->components->count() + $user?->assets?->flatMap->licenses->count() + $user?->assets?->flatMap->assignedAccessories->count();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This caught my eye. I wonder if there is a way to have the database do the counting. I'm not sure off the top of my head.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didnt find a good/better solution to this.. this was less lines. But I am open to suggestions.


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

return view('users.print')
->with('users', [$user])
->with('indirectItemsCount', $indirectItemsCount)
->with('settings', Setting::getSettings());
}

Expand Down
46 changes: 45 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,10 @@ public function licenses()
{
return $this->belongsToMany(License::class, 'license_seats', 'assigned_to', 'license_id')->withPivot('id', 'created_at', 'updated_at');
}
public function directLicenses()
{
return $this->belongsToMany(\App\Models\License::class, 'license_seats', 'assigned_to', 'license_id')->withPivot('id', 'created_at', 'updated_at')->wherePivotNull('asset_id')->withTrashed();
}

/**
* Establishes the user -> reportTemplates relationship
Expand Down Expand Up @@ -1290,7 +1294,47 @@ public function scopeUserLocation($query, $location, $search)
->orwhereRaw('CONCAT(users.first_name," ",users.last_name) LIKE \''.$search.'%\'');

}

public function scopeWithInventoryRelations($query, int $id)
{
return $query->where('id', $id)
->with([
'assets.log' => fn ($query) => $query->withTrashed()
->where('target_type', User::class)
->where('target_id', $id)
->where('action_type', 'accepted'),
'assets.defaultLoc',
'assets.location',
'assets.model.category',
'assets.assignedAssets.log' => fn ($query) => $query->withTrashed()
->where('target_type', User::class)
->where('target_id', $id)
->where('action_type', 'accepted'),
'assets.assignedAssets.assignedTo',
'assets.assignedAssets.defaultLoc',
'assets.assignedAssets.location',
'assets.assignedAssets.model.category',
'assets.components.category',
'assets.licenses',
'assets.licenses.category',
'assets.assignedAccessories',
'assets.assignedAccessories.accessory.category',
'accessories.log' => fn ($query) => $query->withTrashed()
->where('target_type', User::class)
->where('target_id', $id)
->where('action_type', 'accepted'),
'accessories.category',
'accessories.manufacturer',
'consumables.log' => fn ($query) => $query->withTrashed()
->where('target_type', User::class)
->where('target_id', $id)
->where('action_type', 'accepted'),
'consumables.category',
'consumables.manufacturer',
'directLicenses.category',
'licenses.category',
])
->withTrashed();
}
/**
* Get all direct and indirect subordinates for this user.
*
Expand Down
1 change: 1 addition & 0 deletions resources/lang/en-US/mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
'asset_tag' => 'Asset Tag',
'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.',
'assigned_to' => 'Assigned To',
'assigned_to_assets' => 'Assignments to Assets',
'eol' => 'EOL',
'best_regards' => 'Best regards,',
'canceled' => 'Canceled',
Expand Down
123 changes: 89 additions & 34 deletions resources/views/users/print.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,39 +168,6 @@ class="snipe-table table table-striped inventory"
@endif
</td>
</tr>
@if ($settings->show_assigned_assets)
@php
$assignedCounter = 1;
@endphp
@foreach ($asset->assignedAssets as $asset)
<tr>
<td>{{ $counter }}.{{ $assignedCounter }}</td>
<td>
@if ($asset->getImageUrl())
<img src="{{ $asset->getImageUrl() }}" class="thumbnail" style="max-height: 50px;">
@endif
</td>
<td>{{ $asset->asset_tag }}</td>
<td>{{ $asset->name }}</td>
<td>{{ (($asset->model) && ($asset->model->category)) ? $asset->model->category->name : trans('general.invalid_category') }}</td>
<td>{{ ($asset->model) ? $asset->model->name : trans('general.invalid_model') }}</td>
<td>{{ ($asset->defaultLoc) ? $asset->defaultLoc->name : '' }}</td>
<td>{{ ($asset->location) ? $asset->location->name : '' }}</td>
<td>{{ $asset->serial }}</td>
<td>
{{ Helper::getFormattedDateObject($asset->last_checkout, 'datetime', false) }}
</td>
<td>
@if ($asset->getLatestSignedAcceptance($show_user))
<img style="width:auto;height:100px;" src="{{ asset('/') }}display-sig/{{ $asset->getLatestSignedAcceptance($show_user)->accept_signature }}">
@endif
</td>
</tr>
@php
$assignedCounter++
@endphp
@endforeach
@endif
@php
$counter++
@endphp
Expand Down Expand Up @@ -240,7 +207,7 @@ class="snipe-table table table-striped inventory"
$lcounter = 1;
@endphp

@foreach ($show_user->licenses as $license)
@foreach ($show_user->directLicenses as $license)
@php
if (($license->category) && ($license->category->getEula())) $eulas[] = $license->category->getEula()
@endphp
Expand Down Expand Up @@ -398,7 +365,95 @@ class="snipe-table table table-striped inventory"
@endforeach
</table>
@endif
@if($indirectItemsCount > 0 && $settings->show_assigned_assets)
<div id="indirect-assignments-toolbar">
<h4>{{ $indirectItemsCount.' '.trans('mail.assigned_to_assets') }}</h4>
</div>
<table
class="snipe-table table table-striped inventory"
id="indirect-assignments"
data-pagination="false"
data-toolbar="#indirect-assignments-toolbar"
data-id-table="indirect-assignments"
data-search="false"
data-side-pagination="client"
data-sortable="true"
data-sort-order="desc"
data-sort-name="name"
data-show-columns="true"
data-cookie-id-table="indirect-assignments">
<thead>
@php
$indirectAssignmentsCounter = 1;
@endphp
<tr>
<th style="width: 20px;" data-sortable="false" data-switchable="false">#</th>
<th style="width: 40%;" data-sortable="true" data-switchable="false">{{ trans('mail.assigned_to') }}</th>
<th style="width: 50%;" data-sortable="true">{{ trans('general.category') }}</th>
<th style="width: 10%;" data-sortable="true">{{ trans('mail.item') }}</th>
<th style="width: 10%;" data-sortable="true">{{ trans('general.quantity') }}</th>
</tr>
</thead>

@foreach ($show_user->assets as $asset)
@foreach ($asset->assignedAssets as $indirectAsset)
<tr>
<td>{{ $indirectAssignmentsCounter }}</td>
<td>{{ $asset->display_name ?? ''}}</td>
<td>{{ (($indirectAsset->model) && ($indirectAsset->model->category)) ? $indirectAsset->model->category->name : trans('general.invalid_category') }}</td>
<td>{{ $indirectAsset->display_name ?? '' }}</td>
<td>1</td>

</tr>
@php
$indirectAssignmentsCounter++
@endphp
@endforeach
@foreach ($asset->licenses as $indirectLicense)
@if($indirectLicense)
<tr>
<td>{{$indirectAssignmentsCounter}}</td>
<td>{{ $asset->display_name ?? ''}}</td>
<td>{{ $indirectLicense->category?->name ?? '' }}</td>
<td>{{ $indirectLicense->name ?? '' }}</td>
<td>1</td>
</tr>
@endif
@php
$indirectAssignmentsCounter ++
@endphp
@endforeach
@foreach ($asset->components as $component)
@if($component)
<tr>
<td>{{$indirectAssignmentsCounter}}</td>
<td>{{ $asset->display_name ?? ''}}</td>
<td>{{ $component->category?->name ?? '' }}</td>
<td>{{ $component->name ?? '' }}</td>
<td>{{ $component->pivot->assigned_qty }}</td>
</tr>
@endif
@php
$indirectAssignmentsCounter ++
@endphp
@endforeach
@foreach ($asset->assignedAccessories as $indirectAccessory)
@if($indirectAccessory)
<tr>
<td>{{$indirectAssignmentsCounter}}</td>
<td>{{ $asset->display_name ?? '' }}</td>
<td>{{ $indirectAccessory->accessory->category?->name ?? '' }}</td>
<td>{{ $indirectAccessory->accessory->name ?? '' }}</td>
<td>1</td>
</tr>
@endif
@php
$indirectAssignmentsCounter ++
@endphp
@endforeach
@endforeach
</table>
@endif
@php
if (!empty($eulas)) $eulas = array_unique($eulas);
@endphp
Expand Down
Loading