Skip to content

Commit 0b6859c

Browse files
committed
Added ability to checkout consumables in variable qty
Signed-off-by: snipe <[email protected]>
1 parent 5da3ce3 commit 0b6859c

File tree

5 files changed

+22
-5
lines changed

5 files changed

+22
-5
lines changed

app/Http/Controllers/Api/ConsumablesController.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,8 @@ public function checkout(Request $request, $id) : JsonResponse
258258

259259
$this->authorize('checkout', $consumable);
260260

261+
$consumable->checkout_qty = $request->input('checkout_qty', 1);
262+
261263
// Make sure there is at least one available to checkout
262264
if ($consumable->numRemaining() <= 0) {
263265
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/consumables/message.checkout.unavailable')));
@@ -268,6 +270,12 @@ public function checkout(Request $request, $id) : JsonResponse
268270
return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.invalid_item_category_single', ['type' => trans('general.consumable')])));
269271
}
270272

273+
// Make sure there is at least one available to checkout
274+
if ($consumable->numRemaining() <= 0 || $consumable->checkout_qty > $consumable->numRemaining()) {
275+
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/consumables/message.checkout.unavailable', ['requested' => $consumable->checkout_qty, 'remaining' => $consumable->numRemaining() ])));
276+
}
277+
278+
271279

272280
// Check if the user exists - @TODO: this should probably be handled via validation, not here??
273281
if (!$user = User::find($request->input('assigned_to'))) {
@@ -278,14 +286,17 @@ public function checkout(Request $request, $id) : JsonResponse
278286
// Update the consumable data
279287
$consumable->assigned_to = $request->input('assigned_to');
280288

281-
$consumable->users()->attach($consumable->id,
289+
for ($i = 0; $i < $consumable->checkout_qty; $i++) {
290+
$consumable->users()->attach($consumable->id,
282291
[
283292
'consumable_id' => $consumable->id,
284293
'created_by' => $user->id,
285294
'assigned_to' => $request->input('assigned_to'),
286295
'note' => $request->input('note'),
287296
]
288297
);
298+
}
299+
289300

290301
event(new CheckoutableCheckedOut($consumable, $user, auth()->user(), $request->input('note')));
291302

app/Http/Controllers/Consumables/ConsumableCheckoutController.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function store(Request $request, $consumableId)
7070
$this->authorize('checkout', $consumable);
7171

7272
// If the quantity is not present in the request or is not a positive integer, set it to 1
73-
$quantity = $request->input('qty');
73+
$quantity = $request->input('checkout_qty');
7474
if (!isset($quantity) || !ctype_digit((string)$quantity) || $quantity <= 0) {
7575
$quantity = 1;
7676
}
@@ -92,14 +92,16 @@ public function store(Request $request, $consumableId)
9292
// Update the consumable data
9393
$consumable->assigned_to = e($request->input('assigned_to'));
9494

95-
for($i = 0; $i < $quantity; $i++){
95+
for ($i = 0; $i < $quantity; $i++){
9696
$consumable->users()->attach($consumable->id, [
9797
'consumable_id' => $consumable->id,
9898
'created_by' => $admin_user->id,
9999
'assigned_to' => e($request->input('assigned_to')),
100100
'note' => $request->input('note'),
101101
]);
102102
}
103+
104+
$consumable->checkout_qty = $quantity;
103105
event(new CheckoutableCheckedOut($consumable, $user, auth()->user(), $request->input('note')));
104106

105107
$request->request->add(['checkout_to_type' => 'user']);

app/Notifications/CheckoutConsumableNotification.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function __construct(Consumable $consumable, $checkedOutTo, User $checked
3838
$this->note = $note;
3939
$this->target = $checkedOutTo;
4040
$this->acceptance = $acceptance;
41+
$this->qty = $consumable->checkout_qty;
4142

4243
$this->settings = Setting::getSettings();
4344
}
@@ -173,7 +174,6 @@ public function toGoogleChat()
173174
*/
174175
public function toMail()
175176
{
176-
Log::debug($this->item->getImageUrl());
177177
$eula = $this->item->getEula();
178178
$req_accept = $this->item->requireAcceptance();
179179

@@ -188,6 +188,7 @@ public function toMail()
188188
'eula' => $eula,
189189
'req_accept' => $req_accept,
190190
'accept_url' => $accept_url,
191+
'qty' => $this->qty,
191192
])
192193
->subject(trans('mail.Confirm_consumable_delivery'));
193194
}

resources/views/consumables/checkout.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
<label for="qty" class="col-md-3 control-label">{{ trans('general.qty') }}</label>
9292
<div class="col-md-7 col-sm-12 required">
9393
<div class="col-md-2" style="padding-left:0px">
94-
<input class="form-control" type="number" name="qty" id="qty" value="1" min="1" max="{{$consumable->numRemaining()}}" maxlength="999999" />
94+
<input class="form-control" type="number" name="checkout_qty" id="checkout_qty" value="1" min="1" max="{{$consumable->numRemaining()}}" maxlength="999999" />
9595
</div>
9696
</div>
9797
{!! $errors->first('qty', '<div class="col-md-8 col-md-offset-3"><span class="alert-msg" aria-hidden="true"><i class="fas fa-times" aria-hidden="true"></i> :message</span></div>') !!}

resources/views/notifications/markdown/checkout-consumable.blade.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
| **{{ trans('mail.checkout_date') }}** | {{ $checkout_date }} |
1212
@endif
1313
| **{{ trans('general.consumable') }}** | {{ $item->name }} |
14+
@if (isset($qty))
15+
| **{{ trans('general.qty') }}** | {{ $qty }} |
16+
@endif
1417
@if (isset($item->manufacturer))
1518
| **{{ trans('general.manufacturer') }}** | {{ $item->manufacturer->name }} |
1619
@endif

0 commit comments

Comments
 (0)