From 0b6859c49180c0cb76fa9a7fbdb87286d82815b1 Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 23 Oct 2024 15:05:35 +0100 Subject: [PATCH] Added ability to checkout consumables in variable qty Signed-off-by: snipe --- app/Http/Controllers/Api/ConsumablesController.php | 13 ++++++++++++- .../Consumables/ConsumableCheckoutController.php | 6 ++++-- .../CheckoutConsumableNotification.php | 3 ++- resources/views/consumables/checkout.blade.php | 2 +- .../markdown/checkout-consumable.blade.php | 3 +++ 5 files changed, 22 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/Api/ConsumablesController.php b/app/Http/Controllers/Api/ConsumablesController.php index 8e7f321720..7ff676c7be 100644 --- a/app/Http/Controllers/Api/ConsumablesController.php +++ b/app/Http/Controllers/Api/ConsumablesController.php @@ -258,6 +258,8 @@ class ConsumablesController extends Controller $this->authorize('checkout', $consumable); + $consumable->checkout_qty = $request->input('checkout_qty', 1); + // Make sure there is at least one available to checkout if ($consumable->numRemaining() <= 0) { return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/consumables/message.checkout.unavailable'))); @@ -268,6 +270,12 @@ class ConsumablesController extends Controller return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.invalid_item_category_single', ['type' => trans('general.consumable')]))); } + // Make sure there is at least one available to checkout + if ($consumable->numRemaining() <= 0 || $consumable->checkout_qty > $consumable->numRemaining()) { + return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/consumables/message.checkout.unavailable', ['requested' => $consumable->checkout_qty, 'remaining' => $consumable->numRemaining() ]))); + } + + // Check if the user exists - @TODO: this should probably be handled via validation, not here?? if (!$user = User::find($request->input('assigned_to'))) { @@ -278,7 +286,8 @@ class ConsumablesController extends Controller // Update the consumable data $consumable->assigned_to = $request->input('assigned_to'); - $consumable->users()->attach($consumable->id, + for ($i = 0; $i < $consumable->checkout_qty; $i++) { + $consumable->users()->attach($consumable->id, [ 'consumable_id' => $consumable->id, 'created_by' => $user->id, @@ -286,6 +295,8 @@ class ConsumablesController extends Controller 'note' => $request->input('note'), ] ); + } + event(new CheckoutableCheckedOut($consumable, $user, auth()->user(), $request->input('note'))); diff --git a/app/Http/Controllers/Consumables/ConsumableCheckoutController.php b/app/Http/Controllers/Consumables/ConsumableCheckoutController.php index 3bf202733a..e08da41229 100644 --- a/app/Http/Controllers/Consumables/ConsumableCheckoutController.php +++ b/app/Http/Controllers/Consumables/ConsumableCheckoutController.php @@ -70,7 +70,7 @@ class ConsumableCheckoutController extends Controller $this->authorize('checkout', $consumable); // If the quantity is not present in the request or is not a positive integer, set it to 1 - $quantity = $request->input('qty'); + $quantity = $request->input('checkout_qty'); if (!isset($quantity) || !ctype_digit((string)$quantity) || $quantity <= 0) { $quantity = 1; } @@ -92,7 +92,7 @@ class ConsumableCheckoutController extends Controller // Update the consumable data $consumable->assigned_to = e($request->input('assigned_to')); - for($i = 0; $i < $quantity; $i++){ + for ($i = 0; $i < $quantity; $i++){ $consumable->users()->attach($consumable->id, [ 'consumable_id' => $consumable->id, 'created_by' => $admin_user->id, @@ -100,6 +100,8 @@ class ConsumableCheckoutController extends Controller 'note' => $request->input('note'), ]); } + + $consumable->checkout_qty = $quantity; event(new CheckoutableCheckedOut($consumable, $user, auth()->user(), $request->input('note'))); $request->request->add(['checkout_to_type' => 'user']); diff --git a/app/Notifications/CheckoutConsumableNotification.php b/app/Notifications/CheckoutConsumableNotification.php index 6746795f2c..c974134fe6 100644 --- a/app/Notifications/CheckoutConsumableNotification.php +++ b/app/Notifications/CheckoutConsumableNotification.php @@ -38,6 +38,7 @@ class CheckoutConsumableNotification extends Notification $this->note = $note; $this->target = $checkedOutTo; $this->acceptance = $acceptance; + $this->qty = $consumable->checkout_qty; $this->settings = Setting::getSettings(); } @@ -173,7 +174,6 @@ class CheckoutConsumableNotification extends Notification */ public function toMail() { - Log::debug($this->item->getImageUrl()); $eula = $this->item->getEula(); $req_accept = $this->item->requireAcceptance(); @@ -188,6 +188,7 @@ class CheckoutConsumableNotification extends Notification 'eula' => $eula, 'req_accept' => $req_accept, 'accept_url' => $accept_url, + 'qty' => $this->qty, ]) ->subject(trans('mail.Confirm_consumable_delivery')); } diff --git a/resources/views/consumables/checkout.blade.php b/resources/views/consumables/checkout.blade.php index fd6b7ce2fc..8e73bdeedf 100644 --- a/resources/views/consumables/checkout.blade.php +++ b/resources/views/consumables/checkout.blade.php @@ -91,7 +91,7 @@
- +
{!! $errors->first('qty', '
') !!} diff --git a/resources/views/notifications/markdown/checkout-consumable.blade.php b/resources/views/notifications/markdown/checkout-consumable.blade.php index 06e73243ca..92d516fd0a 100644 --- a/resources/views/notifications/markdown/checkout-consumable.blade.php +++ b/resources/views/notifications/markdown/checkout-consumable.blade.php @@ -11,6 +11,9 @@ | **{{ trans('mail.checkout_date') }}** | {{ $checkout_date }} | @endif | **{{ trans('general.consumable') }}** | {{ $item->name }} | +@if (isset($qty)) +| **{{ trans('general.qty') }}** | {{ $qty }} | +@endif @if (isset($item->manufacturer)) | **{{ trans('general.manufacturer') }}** | {{ $item->manufacturer->name }} | @endif