Skip to content

Commit a99ceb9

Browse files
Merge pull request #48 from khalidmaquilang/feature/si-46-max-value-based-on-inventory
Feature | selling quantity is based on the on hand in the inventory
2 parents d9e8d17 + 30ee0f3 commit a99ceb9

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

app/Filament/Resources/SaleResource.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\Filament\Exports\SaleExporter;
77
use App\Filament\Resources\SaleResource\Pages;
88
use App\Models\Customer;
9+
use App\Models\Inventory;
910
use App\Models\Product;
1011
use App\Models\Sale;
1112
use Awcodes\TableRepeater\Components\TableRepeater;
@@ -96,6 +97,15 @@ function ($component) {
9697
->required(),
9798
Forms\Components\TextInput::make('quantity')
9899
->lazy()
100+
->minValue(1)
101+
->maxValue(function (Forms\Get $get): float {
102+
$inventory = Inventory::where('product_id', $get('product_id'))->first();
103+
if (empty($inventory)) {
104+
return 0;
105+
}
106+
107+
return $inventory->quantity_on_hand;
108+
})
99109
->afterStateUpdated(function (Forms\Set $set, Forms\Get $get, $state) {
100110
$cost = $get('unit_cost');
101111
if (empty($cost)) {
@@ -310,7 +320,7 @@ public static function updateSubTotal(Forms\Get $get, Forms\Set $set): void
310320

311321
// Calculate subtotal based on the selected products and quantities
312322
$subtotal = $selectedProducts->reduce(function ($subtotal, $product) {
313-
return $subtotal + ($product['unit_cost'] * $product['quantity']);
323+
return $subtotal + ((float) $product['unit_cost'] * (float) $product['quantity']);
314324
}, 0);
315325

316326
// Update the state with the new values

0 commit comments

Comments
 (0)