From 6b7af802af41c92a36e77605415869c9e72ec192 Mon Sep 17 00:00:00 2001 From: Brady Wetherington Date: Thu, 10 Oct 2024 13:28:23 +0100 Subject: [PATCH 1/2] Add 'bulk checkout' as one of the bulk actions in the bulk actions toolbar --- .../Assets/BulkAssetsController.php | 13 +++++++---- app/Models/Asset.php | 4 ++++ .../lang/en-US/admin/hardware/message.php | 5 ++++ .../partials/asset-bulk-actions.blade.php | 23 +++++++++++-------- 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index 1ce08e65e9af..b11c36469e5c 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -52,6 +52,10 @@ public function edit(Request $request) : View | RedirectResponse } $asset_ids = $request->input('ids'); + if ($request->input('bulk_actions') === 'checkout') { + $request->session()->flashInput(['selected_assets' => $asset_ids]); + return redirect()->route('hardware.bulkcheckout.show'); + } // Figure out where we need to send the user after the update is complete, and store that in the session $bulk_back_url = request()->headers->get('referer'); @@ -580,8 +584,9 @@ public function storeCheckout(AssetCheckoutRequest $request) : RedirectResponse if ($target->location_id != '') { $asset->location_id = $target->location_id; - $asset->unsetEventDispatcher(); - $asset->save(); + $asset::withoutEvents(function () use ($asset) { // TODO - I don't know why this is being saved without events + $asset->save(); + }); } if ($error) { @@ -592,10 +597,10 @@ public function storeCheckout(AssetCheckoutRequest $request) : RedirectResponse if (! $errors) { // Redirect to the new asset page - return redirect()->to('hardware')->with('success', trans('admin/hardware/message.checkout.success')); + return redirect()->to('hardware')->with('success', trans_choice('admin/hardware/message.multi-checkout.success', $asset_ids)); } // Redirect to the asset management page with error - return redirect()->route('hardware.bulkcheckout.show')->with('error', trans('admin/hardware/message.checkout.error'))->withErrors($errors); + return redirect()->route('hardware.bulkcheckout.show')->with('error', trans_choice('admin/hardware/message.multi-checkout.error', $asset_ids))->withErrors($errors); } catch (ModelNotFoundException $e) { return redirect()->route('hardware.bulkcheckout.show')->with('error', $e->getErrors()); } diff --git a/app/Models/Asset.php b/app/Models/Asset.php index ce8b870eb2e0..54257ed240fb 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -2,6 +2,7 @@ namespace App\Models; +use Watson\Validating\ValidationException; use App\Events\CheckoutableCheckedOut; use App\Exceptions\CheckoutNotAllowed; use App\Helpers\Helper; @@ -379,6 +380,9 @@ public function checkOut($target, $admin = null, $checkout_at = null, $expected_ return true; } + $validator = $this->makeValidator($this->getRules()); + + throw new ValidationException($validator, $this); return false; } diff --git a/resources/lang/en-US/admin/hardware/message.php b/resources/lang/en-US/admin/hardware/message.php index 041d32f56c90..27877e02f9fc 100644 --- a/resources/lang/en-US/admin/hardware/message.php +++ b/resources/lang/en-US/admin/hardware/message.php @@ -79,6 +79,11 @@ 'no_assets_selected' => 'You must select at least one asset from the list', ], + 'multi-checkout' => [ + 'error' => 'Asset was not checked out, please try again|Assets were not checked out, please try again', + 'success' => 'Asset checked out successfully.|Assets checked out successfully.', + ], + 'checkin' => [ 'error' => 'Asset was not checked in, please try again', 'success' => 'Asset checked in successfully.', diff --git a/resources/views/partials/asset-bulk-actions.blade.php b/resources/views/partials/asset-bulk-actions.blade.php index b597ad647f78..992fb52bba10 100644 --- a/resources/views/partials/asset-bulk-actions.blade.php +++ b/resources/views/partials/asset-bulk-actions.blade.php @@ -16,17 +16,20 @@ From 3cf746d7df83ef3e7cfa45c602fc182ebe8f11e3 Mon Sep 17 00:00:00 2001 From: Brady Wetherington Date: Wed, 16 Oct 2024 23:13:32 +0100 Subject: [PATCH 2/2] Rework the bulk checkout to not change how all checkouts work --- .../Controllers/Assets/BulkAssetsController.php | 14 ++++++++------ app/Models/Asset.php | 4 ---- resources/views/hardware/bulk-checkout.blade.php | 7 +++++++ 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index b11c36469e5c..c27cfe3e0c69 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -575,22 +575,24 @@ public function storeCheckout(AssetCheckoutRequest $request) : RedirectResponse } $errors = []; - DB::transaction(function () use ($target, $admin, $checkout_at, $expected_checkin, $errors, $asset_ids, $request) { + DB::transaction(function () use ($target, $admin, $checkout_at, $expected_checkin, &$errors, $asset_ids, $request) { //NOTE: $errors is passsed by reference! foreach ($asset_ids as $asset_id) { $asset = Asset::findOrFail($asset_id); $this->authorize('checkout', $asset); - $error = $asset->checkOut($target, $admin, $checkout_at, $expected_checkin, e($request->get('note')), $asset->name, null); + $checkout_success = $asset->checkOut($target, $admin, $checkout_at, $expected_checkin, e($request->get('note')), $asset->name, null); + //TODO - I think this logic is duplicated in the checkOut method? if ($target->location_id != '') { $asset->location_id = $target->location_id; - $asset::withoutEvents(function () use ($asset) { // TODO - I don't know why this is being saved without events + // TODO - I don't know why this is being saved without events + $asset::withoutEvents(function () use ($asset) { $asset->save(); }); } - if ($error) { - array_merge_recursive($errors, $asset->getErrors()->toArray()); + if (!$checkout_success) { + $errors = array_merge_recursive($errors, $asset->getErrors()->toArray()); } } }); @@ -600,7 +602,7 @@ public function storeCheckout(AssetCheckoutRequest $request) : RedirectResponse return redirect()->to('hardware')->with('success', trans_choice('admin/hardware/message.multi-checkout.success', $asset_ids)); } // Redirect to the asset management page with error - return redirect()->route('hardware.bulkcheckout.show')->with('error', trans_choice('admin/hardware/message.multi-checkout.error', $asset_ids))->withErrors($errors); + return redirect()->route('hardware.bulkcheckout.show')->withInput()->with('error', trans_choice('admin/hardware/message.multi-checkout.error', $asset_ids))->withErrors($errors); } catch (ModelNotFoundException $e) { return redirect()->route('hardware.bulkcheckout.show')->with('error', $e->getErrors()); } diff --git a/app/Models/Asset.php b/app/Models/Asset.php index 54257ed240fb..ce8b870eb2e0 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -2,7 +2,6 @@ namespace App\Models; -use Watson\Validating\ValidationException; use App\Events\CheckoutableCheckedOut; use App\Exceptions\CheckoutNotAllowed; use App\Helpers\Helper; @@ -380,9 +379,6 @@ public function checkOut($target, $admin = null, $checkout_at = null, $expected_ return true; } - $validator = $this->makeValidator($this->getRules()); - - throw new ValidationException($validator, $this); return false; } diff --git a/resources/views/hardware/bulk-checkout.blade.php b/resources/views/hardware/bulk-checkout.blade.php index 405e5e47cd26..39e2cdf10cbf 100644 --- a/resources/views/hardware/bulk-checkout.blade.php +++ b/resources/views/hardware/bulk-checkout.blade.php @@ -115,5 +115,12 @@ @section('moar_scripts') @include('partials/assets-assigned') + @stop