|
15 | 15 | use App\Filament\Admin\Resources\Products\Pages\CreateProduct; |
16 | 16 | use App\Filament\Admin\Resources\Products\Pages\EditProduct; |
17 | 17 | use App\Filament\Admin\Resources\ProductResource\Pages; |
| 18 | +use App\Models\InventoryLog; |
18 | 19 | use App\Models\Product; |
19 | 20 | use App\Models\ProductCategory; |
20 | 21 | use App\Models\ProductTag; |
| 22 | +use Filament\Notifications\Notification; |
21 | 23 | use Filament\Forms; |
22 | 24 | use Filament\Resources\Resource; |
23 | 25 | use Filament\Tables; |
24 | 26 | use Filament\Tables\Table; |
25 | 27 | use Illuminate\Database\Eloquent\Builder; |
26 | 28 | use Illuminate\Database\Eloquent\SoftDeletingScope; |
| 29 | +use Illuminate\Support\Facades\DB; |
27 | 30 | use Illuminate\Support\Collection; |
28 | 31 | use League\Csv\Writer; |
29 | 32 | use Filament\Forms\Components\Toggle; |
@@ -142,24 +145,63 @@ public static function table(Table $table): Table |
142 | 145 | // ]) |
143 | 146 | ->recordActions([ |
144 | 147 | 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 | + }), |
145 | 174 | Action::make('adjustInventory') |
146 | 175 | ->label('Adjust Inventory') |
147 | 176 | ->icon('heroicon-o-adjustments-horizontal') |
148 | 177 | ->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 | + }); |
157 | 198 | }) |
158 | 199 | ->schema([ |
159 | 200 | TextInput::make('adjustment') |
160 | 201 | ->label('Quantity Adjustment') |
161 | 202 | ->required() |
162 | | - ->integer(), |
| 203 | + ->integer() |
| 204 | + ->helperText('Use a positive value to increase stock and a negative value to decrease stock.'), |
163 | 205 | TextInput::make('reason') |
164 | 206 | ->label('Reason for Adjustment') |
165 | 207 | ->required(), |
|
0 commit comments