From aff8402469894b16227ab2884c0a27e5f113cb3c Mon Sep 17 00:00:00 2001 From: Dampfklon Date: Fri, 29 Aug 2025 15:02:12 +0200 Subject: [PATCH] add api endpoint to request user asset print --- app/Http/Controllers/Api/UsersController.php | 44 ++++++++++++++++++++ routes/api.php | 8 ++++ 2 files changed, 52 insertions(+) diff --git a/app/Http/Controllers/Api/UsersController.php b/app/Http/Controllers/Api/UsersController.php index 1b06f5364f3c..69edf67038f9 100644 --- a/app/Http/Controllers/Api/UsersController.php +++ b/app/Http/Controllers/Api/UsersController.php @@ -18,6 +18,7 @@ use App\Models\Company; use App\Models\Consumable; use App\Models\License; +use App\Models\Setting; use App\Models\User; use App\Notifications\CurrentInventory; use App\Notifications\WelcomeNotification; @@ -662,6 +663,49 @@ public function assets(Request $request, $id) : JsonResponse | array } + /** + * Print inventory + * + * @since [v8.3.2] + * @author Aladin Alaily + */ + 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(); + + if ($user) { + $this->authorize('view', $user); + + return response()->json(Helper::formatStandardApiResponse('success',view('users.print') + ->with('users', [$user]) + ->with('settings', Setting::getSettings()) + ->render(), null)); + } + + return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/users/message.user_not_found', compact('id')))); + } + /** * Notify a specific user via email with all of their assigned assets. * diff --git a/routes/api.php b/routes/api.php index 988cedb64539..f8a2c829d549 100644 --- a/routes/api.php +++ b/routes/api.php @@ -1103,6 +1103,14 @@ ] )->name('api.users.email_assets'); + Route::get('{userId}/print', + [ + Api\UsersController::class, + 'printInventory' + ] + )->name('api.users.print'); + + Route::get('{user}/accessories', [ Api\UsersController::class,