A free open source IT asset/license management system
use inventory scope, add quantity to print blade
Godfrey Mab04f3de931a
diff --git a/app/Http/Controllers/ProfileController.php b/app/Http/Controllers/ProfileController.phpindex b1cd8db71e..584e880824 100755--- a/app/Http/Controllers/ProfileController.php+++ b/app/Http/Controllers/ProfileController.php@@ -212,51 +212,7 @@ class ProfileController extends Controller{$userId = auth()->id();- $show_user = User::where('id', $userId)- ->with([- 'assets.log' => fn ($query) => $query->withTrashed()- ->where('target_type', User::class)- ->where('target_id', $userId)- ->where('action_type', 'accepted'),-- 'assets.assignedAssets.log' => fn ($query) => $query->withTrashed()- ->where('target_type', User::class)- ->where('target_id', $userId)- ->where('action_type', 'accepted'),-- 'assets.assignedAssets.assignedTo',- 'assets.assignedAssets.defaultLoc',- 'assets.assignedAssets.location',- 'assets.assignedAssets.model.category',- 'assets.components',- 'assets.assignedAccessories',- 'assets.defaultLoc',- 'assets.location',- 'assets.licenses',- 'assets.licenses.category',- 'assets.licenses.category',- 'assets.model.category',- 'assets.components.category',- 'assets.assignedAccessories.accessory.category',- 'accessories.log' => fn ($query) => $query->withTrashed()- ->where('target_type', User::class)- ->where('target_id', $userId)- ->where('action_type', 'accepted'),-- 'accessories.category',- 'accessories.manufacturer',-- 'consumables.log' => fn ($query) => $query->withTrashed()- ->where('target_type', User::class)- ->where('target_id', $userId)- ->where('action_type', 'accepted'),-- 'consumables.category',- 'consumables.manufacturer',- 'licenses.category',- ])- ->withTrashed()- ->firstOrFail();+ $show_user = User::withInventoryRelations($userId)->first();$indirectItemsCount =$show_user->assets->flatMap->assignedAssets->count()diff --git a/app/Http/Controllers/Users/UsersController.php b/app/Http/Controllers/Users/UsersController.phpindex fe18884b47..ae812bb2a9 100755--- a/app/Http/Controllers/Users/UsersController.php+++ b/app/Http/Controllers/Users/UsersController.php@@ -625,39 +625,10 @@ class UsersController extends Controller{$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.components',- 'assets.assignedAccessories',- 'assets.defaultLoc',- 'assets.location',- 'assets.licenses',- 'assets.licenses.category',- 'assets.model.category',- 'assets.assignedAssets.assignedTo',- 'assets.assignedAssets.defaultLoc',- 'assets.assignedAssets.location',- 'assets.assignedAssets.model.category',- 'assets.licenses.category',- 'assets.components.category',- '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',- '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();+if ($user) {$this->authorize('view', $user);diff --git a/app/Models/User.php b/app/Models/User.phpindex 83145dec62..6bc3ec0e45 100644--- a/app/Models/User.php+++ b/app/Models/User.php@@ -1253,7 +1253,46 @@ class User extends SnipeModel implements AuthenticatableContract, AuthorizableCo->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',+ 'licenses.category',+ ])+ ->withTrashed();+ }/*** Get all direct and indirect subordinates for this user.*diff --git a/resources/views/users/print.blade.php b/resources/views/users/print.blade.phpindex 65e1d798a0..57ee01569e 100644--- a/resources/views/users/print.blade.php+++ b/resources/views/users/print.blade.php@@ -402,6 +402,7 @@<td>{{ $indirectAsset->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@@ -415,6 +416,7 @@<td>{{ $asset->display_name ?? ''}}</td><td>{{ $indirectLicense->category?->name ?? '' }}</td><td>{{ $indirectLicense->name ?? '' }}</td>+ <td>1</td></tr>@endif@php@@ -442,6 +444,7 @@<td>{{ $asset->display_name ?? '' }}</td><td>{{ $indirectAccessory->accessory->category?->name ?? '' }}</td><td>{{ $indirectAccessory->accessory->name ?? '' }}</td>+ <td>1</td></tr>@endif@php