Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #9148 Checking Out Consumables doesn't decrease starting quantity #12491

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/Http/Controllers/Api/ConsumablesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions app/Models/Consumable.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,9 @@ public function numCheckedOut()
*/
public function numRemaining()
{
$checkedout = $this->users->count();
$total = $this->qty;
$remaining = $total - $checkedout;

return $remaining;
return $total;
}

/**
Expand Down
8 changes: 1 addition & 7 deletions app/Presenters/ConsumablePresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use App\Models\Consumable;

class AdjustConsumablesQuantities extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$consumables_checkedout = DB::table('consumables_users')->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 = (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]);
}
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}