From 7690ba6b991dd69be8f23473376dced44115513c Mon Sep 17 00:00:00 2001 From: Ivan Nieto Vivanco Date: Wed, 8 Feb 2023 19:53:39 -0600 Subject: [PATCH 1/5] Decrement quantity for checked out consumables --- app/Http/Controllers/Api/ConsumablesController.php | 2 ++ .../Controllers/Consumables/ConsumableCheckoutController.php | 2 ++ 2 files changed, 4 insertions(+) diff --git a/app/Http/Controllers/Api/ConsumablesController.php b/app/Http/Controllers/Api/ConsumablesController.php index fc6620df48cc..7312c193c804 100644 --- a/app/Http/Controllers/Api/ConsumablesController.php +++ b/app/Http/Controllers/Api/ConsumablesController.php @@ -278,6 +278,8 @@ public function checkout(Request $request, $id) 'note' => $request->input('note'), ]); + $consumable->newQuery()->where(['id' => $consumable->id])->update(['qty' => $consumable->qty - 1]); + // Log checkout event $logaction = $consumable->logCheckout(e($request->input('note')), $user); $data['log_id'] = $logaction->id; diff --git a/app/Http/Controllers/Consumables/ConsumableCheckoutController.php b/app/Http/Controllers/Consumables/ConsumableCheckoutController.php index f7f2b6e54d78..802f3a50be27 100644 --- a/app/Http/Controllers/Consumables/ConsumableCheckoutController.php +++ b/app/Http/Controllers/Consumables/ConsumableCheckoutController.php @@ -69,6 +69,8 @@ public function store(Request $request, $consumableId) 'note' => $request->input('note'), ]); + $consumable->newQuery()->where(['id' => $consumable->id])->update(['qty' => $consumable->qty - 1]); + event(new CheckoutableCheckedOut($consumable, $user, Auth::user(), $request->input('note'))); // Redirect to the new consumable page From b9f5d3e555e45e0a41668a35f0f7944956e58257 Mon Sep 17 00:00:00 2001 From: Ivan Nieto Vivanco Date: Wed, 8 Feb 2023 19:56:13 -0600 Subject: [PATCH 2/5] Alter numRemaining() function in Consumable model to return the actual remaining total --- app/Models/Consumable.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/Models/Consumable.php b/app/Models/Consumable.php index c04c9b53d5f0..558a0563c9ef 100644 --- a/app/Models/Consumable.php +++ b/app/Models/Consumable.php @@ -317,11 +317,9 @@ public function numCheckedOut() */ public function numRemaining() { - $checkedout = $this->users->count(); $total = $this->qty; - $remaining = $total - $checkedout; - return $remaining; + return $total; } /** From c451b13ccdd95f6d9762a5948a5b083e535b29f7 Mon Sep 17 00:00:00 2001 From: Ivan Nieto Vivanco Date: Wed, 8 Feb 2023 20:58:44 -0600 Subject: [PATCH 3/5] Add migration to adjust consumables quantity --- ...9_015832_adjust_consumables_quantities.php | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 database/migrations/2023_02_09_015832_adjust_consumables_quantities.php diff --git a/database/migrations/2023_02_09_015832_adjust_consumables_quantities.php b/database/migrations/2023_02_09_015832_adjust_consumables_quantities.php new file mode 100644 index 000000000000..77b69e26a9e9 --- /dev/null +++ b/database/migrations/2023_02_09_015832_adjust_consumables_quantities.php @@ -0,0 +1,36 @@ +select('consumable_id', DB::raw('count(*) as total_checkedout'))->groupBy('consumable_id')->get(); + + foreach ($consumables_checkedout as $consumable){ + $consumable_qty = Consumable::select('qty')->where(['id' => $consumable->consumable_id])->value('qty'); + $total = $consumable_qty - $consumable->total_checkedout; + + Consumable::where('id', $consumable->consumable_id)->update(['qty' => $total]); + } + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +} From 0044ce8e19b5dd28aa316217c64182770f412084 Mon Sep 17 00:00:00 2001 From: Ivan Nieto Vivanco Date: Wed, 8 Feb 2023 21:02:59 -0600 Subject: [PATCH 4/5] Remove the remaining column from consumables index view --- app/Presenters/ConsumablePresenter.php | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/app/Presenters/ConsumablePresenter.php b/app/Presenters/ConsumablePresenter.php index e931fc1228e7..6fcae70c018e 100644 --- a/app/Presenters/ConsumablePresenter.php +++ b/app/Presenters/ConsumablePresenter.php @@ -69,13 +69,7 @@ public static function dataTableLayout() 'sortable' => false, 'title' => trans('admin/components/general.total'), 'visible' => true, - ], [ - 'field' => 'remaining', - 'searchable' => false, - 'sortable' => false, - 'title' => trans('admin/components/general.remaining'), - 'visible' => true, - ], [ + ], [ 'field' => 'min_amt', 'searchable' => false, 'sortable' => false, From 5b4f468da8c9abe09a29ff74fe19f566ae5e9732 Mon Sep 17 00:00:00 2001 From: Ivan Nieto Vivanco Date: Wed, 15 Mar 2023 17:12:33 -0600 Subject: [PATCH 5/5] Adjust the so it can't be a negative quantity --- .../2023_02_09_015832_adjust_consumables_quantities.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/database/migrations/2023_02_09_015832_adjust_consumables_quantities.php b/database/migrations/2023_02_09_015832_adjust_consumables_quantities.php index 77b69e26a9e9..d133cd9c3b91 100644 --- a/database/migrations/2023_02_09_015832_adjust_consumables_quantities.php +++ b/database/migrations/2023_02_09_015832_adjust_consumables_quantities.php @@ -18,7 +18,12 @@ public function up() foreach ($consumables_checkedout as $consumable){ $consumable_qty = Consumable::select('qty')->where(['id' => $consumable->consumable_id])->value('qty'); - $total = $consumable_qty - $consumable->total_checkedout; + $total = (int) $consumable_qty - (int) $consumable->total_checkedout; + + // Control that the $total is never negative + if ($total < 0){ + $total = 0; + } Consumable::where('id', $consumable->consumable_id)->update(['qty' => $total]); }