Skip to content

Commit a0b2ad3

Browse files
committed
add stock
1 parent f3d003f commit a0b2ad3

File tree

1 file changed

+51
-9
lines changed

1 file changed

+51
-9
lines changed

app/Filament/Admin/Resources/Products/ProductResource.php

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,18 @@
1515
use App\Filament\Admin\Resources\Products\Pages\CreateProduct;
1616
use App\Filament\Admin\Resources\Products\Pages\EditProduct;
1717
use App\Filament\Admin\Resources\ProductResource\Pages;
18+
use App\Models\InventoryLog;
1819
use App\Models\Product;
1920
use App\Models\ProductCategory;
2021
use App\Models\ProductTag;
22+
use Filament\Notifications\Notification;
2123
use Filament\Forms;
2224
use Filament\Resources\Resource;
2325
use Filament\Tables;
2426
use Filament\Tables\Table;
2527
use Illuminate\Database\Eloquent\Builder;
2628
use Illuminate\Database\Eloquent\SoftDeletingScope;
29+
use Illuminate\Support\Facades\DB;
2730
use Illuminate\Support\Collection;
2831
use League\Csv\Writer;
2932
use Filament\Forms\Components\Toggle;
@@ -142,24 +145,63 @@ public static function table(Table $table): Table
142145
// ])
143146
->recordActions([
144147
EditAction::make(),
148+
Action::make('addStock')
149+
->label('Add Stock')
150+
->icon('heroicon-o-plus-circle')
151+
->schema([
152+
TextInput::make('quantity')
153+
->label('Quantity to Add')
154+
->required()
155+
->integer()
156+
->minValue(1),
157+
TextInput::make('reason')
158+
->label('Reason')
159+
->required()
160+
->maxLength(255)
161+
->default('purchase'),
162+
])
163+
->action(function (Product $record, array $data): void {
164+
DB::transaction(function () use ($record, $data): void {
165+
$record->increment('inventory_count', $data['quantity']);
166+
167+
InventoryLog::create([
168+
'product_id' => $record->id,
169+
'quantity_change' => $data['quantity'],
170+
'reason' => $data['reason'],
171+
]);
172+
});
173+
}),
145174
Action::make('adjustInventory')
146175
->label('Adjust Inventory')
147176
->icon('heroicon-o-adjustments-horizontal')
148177
->action(function (Product $record, array $data): void {
149-
$record->inventory_count += $data['adjustment'];
150-
$record->save();
151-
152-
// InventoryLog::create([
153-
// 'product_id' => $record->id,
154-
// 'quantity_change' => $data['adjustment'],
155-
// 'reason' => $data['reason'],
156-
// ]);
178+
$newInventory = $record->inventory_count + $data['adjustment'];
179+
180+
if ($newInventory < 0) {
181+
Notification::make()
182+
->title('Inventory cannot go below zero.')
183+
->danger()
184+
->send();
185+
186+
return;
187+
}
188+
189+
DB::transaction(function () use ($record, $data, $newInventory): void {
190+
$record->update(['inventory_count' => $newInventory]);
191+
192+
InventoryLog::create([
193+
'product_id' => $record->id,
194+
'quantity_change' => $data['adjustment'],
195+
'reason' => $data['reason'],
196+
]);
197+
});
157198
})
158199
->schema([
159200
TextInput::make('adjustment')
160201
->label('Quantity Adjustment')
161202
->required()
162-
->integer(),
203+
->integer()
204+
->helperText('Use a positive value to increase stock and a negative value to decrease stock.'),
163205
TextInput::make('reason')
164206
->label('Reason for Adjustment')
165207
->required(),

0 commit comments

Comments
 (0)