Auto-deduct spice inventory when batch is created/updated/deleted
Deploy / deploy (push) Successful in 14s
Deploy / deploy (push) Successful in 14s
- Add size_id, units_produced, quantity_g to batches table - quantity_g auto-calculated from units × packing size on save - Batch model deducts spice quantities on create, reverses on delete, recalculates on update when relevant fields change - Batch form shows live total weight preview - Chargen table shows units + grams produced Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
1e93aaa7d8
commit
a34bb29620
4 files changed
+163
-1
No files matched your search
@@ -4,11 +4,13 @@ namespace App\Filament\User\Resources;
|
||||
|
||||
use App\Filament\User\Resources\BatchResource\Pages;
|
||||
use App\Models\Batch;
|
||||
use App\Models\ProductSize;
|
||||
use App\Models\Recipe;
|
||||
use Filament\Forms;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Forms\Get;
|
||||
use Filament\Resources\Resource;
|
||||
use Illuminate\Support\HtmlString;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
@@ -38,6 +40,17 @@ class BatchResource extends Resource
|
||||
->searchable()
|
||||
->preload()
|
||||
->required()
|
||||
->live()
|
||||
->afterStateUpdated(fn ($set) => $set('size_id', null)),
|
||||
|
||||
Forms\Components\Select::make('size_id')
|
||||
->label('Variante (Verpackung)')
|
||||
->options(fn (Get $get) => ProductSize::with('packing')
|
||||
->where('product_id', $get('product_id'))
|
||||
->get()
|
||||
->mapWithKeys(fn ($s) => [$s->id => $s->packing->name . ' ' . $s->packing->size . 'g'])
|
||||
)
|
||||
->required()
|
||||
->live(),
|
||||
|
||||
Forms\Components\Select::make('recipe_id')
|
||||
@@ -48,6 +61,25 @@ class BatchResource extends Resource
|
||||
->required()
|
||||
->live(),
|
||||
|
||||
Forms\Components\TextInput::make('units_produced')
|
||||
->label('Anzahl Einheiten')
|
||||
->numeric()
|
||||
->required()
|
||||
->live(debounce: 400),
|
||||
|
||||
Forms\Components\Placeholder::make('quantity_g_preview')
|
||||
->label('Gesamtgewicht (berechnet)')
|
||||
->content(function (Get $get): HtmlString {
|
||||
$units = (int) $get('units_produced');
|
||||
$sizeId = $get('size_id');
|
||||
if (! $units || ! $sizeId) {
|
||||
return new HtmlString('<span class="text-gray-400">—</span>');
|
||||
}
|
||||
$size = ProductSize::with('packing')->find($sizeId);
|
||||
$totalG = $units * ($size?->packing?->size ?? 0);
|
||||
return new HtmlString("<strong>{$totalG} g</strong> ({$units} × {$size?->packing?->size}g)");
|
||||
}),
|
||||
|
||||
Forms\Components\DatePicker::make('produced_at')
|
||||
->label('Produktionsdatum'),
|
||||
|
||||
|
||||
+36
@@ -2,11 +2,14 @@
|
||||
|
||||
namespace App\Filament\User\Resources\ProductResource\RelationManagers;
|
||||
|
||||
use App\Models\ProductSize;
|
||||
use Filament\Forms;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Forms\Get;
|
||||
use Filament\Resources\RelationManagers\RelationManager;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Support\HtmlString;
|
||||
|
||||
class BatchesRelationManager extends RelationManager
|
||||
{
|
||||
@@ -24,6 +27,16 @@ class BatchesRelationManager extends RelationManager
|
||||
->unique(ignoreRecord: true)
|
||||
->maxLength(255),
|
||||
|
||||
Forms\Components\Select::make('size_id')
|
||||
->label('Variante (Verpackung)')
|
||||
->options(fn () => ProductSize::with('packing')
|
||||
->where('product_id', $this->getOwnerRecord()->id)
|
||||
->get()
|
||||
->mapWithKeys(fn ($s) => [$s->id => $s->packing->name . ' ' . $s->packing->size . 'g'])
|
||||
)
|
||||
->required()
|
||||
->live(),
|
||||
|
||||
Forms\Components\Select::make('recipe_id')
|
||||
->label('Rezeptur')
|
||||
->relationship('recipe', 'name')
|
||||
@@ -31,6 +44,25 @@ class BatchesRelationManager extends RelationManager
|
||||
->preload()
|
||||
->required(),
|
||||
|
||||
Forms\Components\TextInput::make('units_produced')
|
||||
->label('Anzahl Einheiten')
|
||||
->numeric()
|
||||
->required()
|
||||
->live(debounce: 400),
|
||||
|
||||
Forms\Components\Placeholder::make('quantity_g_preview')
|
||||
->label('Gesamtgewicht (berechnet)')
|
||||
->content(function (Get $get): HtmlString {
|
||||
$units = (int) $get('units_produced');
|
||||
$sizeId = $get('size_id');
|
||||
if (! $units || ! $sizeId) {
|
||||
return new HtmlString('<span class="text-gray-400">—</span>');
|
||||
}
|
||||
$size = ProductSize::with('packing')->find($sizeId);
|
||||
$totalG = $units * ($size?->packing?->size ?? 0);
|
||||
return new HtmlString("<strong>{$totalG} g</strong> ({$units} × {$size?->packing?->size}g)");
|
||||
}),
|
||||
|
||||
Forms\Components\DatePicker::make('produced_at')
|
||||
->label('Produktionsdatum'),
|
||||
|
||||
@@ -84,6 +116,10 @@ class BatchesRelationManager extends RelationManager
|
||||
->searchable()
|
||||
->sortable(),
|
||||
|
||||
Tables\Columns\TextColumn::make('units_produced')
|
||||
->label('Einheiten')
|
||||
->suffix(fn ($record) => $record->quantity_g ? ' (' . number_format($record->quantity_g, 0, ',', '.') . 'g)' : ''),
|
||||
|
||||
Tables\Columns\TextColumn::make('recipe.name')
|
||||
->label('Rezeptur'),
|
||||
|
||||
|
||||
+66
-1
@@ -6,7 +6,10 @@ use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Batch extends Model
|
||||
{
|
||||
protected $fillable = ['lot_number', 'product_id', 'recipe_id', 'produced_at', 'expires_at'];
|
||||
protected $fillable = [
|
||||
'lot_number', 'product_id', 'size_id', 'recipe_id',
|
||||
'units_produced', 'quantity_g', 'produced_at', 'expires_at',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'produced_at' => 'date',
|
||||
@@ -18,6 +21,11 @@ class Batch extends Model
|
||||
return $this->belongsTo(Product::class);
|
||||
}
|
||||
|
||||
public function size()
|
||||
{
|
||||
return $this->belongsTo(ProductSize::class);
|
||||
}
|
||||
|
||||
public function recipe()
|
||||
{
|
||||
return $this->belongsTo(Recipe::class);
|
||||
@@ -27,4 +35,61 @@ class Batch extends Model
|
||||
{
|
||||
return $this->hasMany(BatchOrigin::class);
|
||||
}
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
// Auto-calculate quantity_g before saving
|
||||
static::saving(function (Batch $batch) {
|
||||
if ($batch->units_produced && $batch->size_id) {
|
||||
$gramsPerUnit = $batch->size?->packing?->size ?? 0;
|
||||
$batch->quantity_g = $batch->units_produced * $gramsPerUnit;
|
||||
}
|
||||
});
|
||||
|
||||
static::created(function (Batch $batch) {
|
||||
$batch->deductSpices();
|
||||
});
|
||||
|
||||
static::updated(function (Batch $batch) {
|
||||
if ($batch->wasChanged(['recipe_id', 'size_id', 'units_produced'])) {
|
||||
$batch->restoreSpices(
|
||||
$batch->getOriginal('recipe_id'),
|
||||
$batch->getOriginal('quantity_g'),
|
||||
);
|
||||
$batch->deductSpices();
|
||||
}
|
||||
});
|
||||
|
||||
static::deleting(function (Batch $batch) {
|
||||
$batch->restoreSpices($batch->recipe_id, $batch->quantity_g);
|
||||
});
|
||||
}
|
||||
|
||||
private function deductSpices(): void
|
||||
{
|
||||
if (! $this->quantity_g || ! $this->recipe_id) {
|
||||
return;
|
||||
}
|
||||
|
||||
$recipe = Recipe::with('spices')->find($this->recipe_id);
|
||||
|
||||
foreach ($recipe->spices as $spice) {
|
||||
$grams = $this->quantity_g * $spice->pivot->amount_in_percent / 100;
|
||||
$spice->decrement('quantity', $grams);
|
||||
}
|
||||
}
|
||||
|
||||
private function restoreSpices(int $recipeId, float $quantityG): void
|
||||
{
|
||||
if (! $quantityG || ! $recipeId) {
|
||||
return;
|
||||
}
|
||||
|
||||
$recipe = Recipe::with('spices')->find($recipeId);
|
||||
|
||||
foreach ($recipe->spices as $spice) {
|
||||
$grams = $quantityG * $spice->pivot->amount_in_percent / 100;
|
||||
$spice->increment('quantity', $grams);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('batches', function (Blueprint $table) {
|
||||
$table->foreignId('size_id')->nullable()->after('product_id')
|
||||
->constrained('products_sizes')->nullOnDelete();
|
||||
$table->unsignedInteger('units_produced')->nullable()->after('size_id');
|
||||
$table->decimal('quantity_g', 10, 2)->nullable()->after('units_produced');
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('batches', function (Blueprint $table) {
|
||||
$table->dropForeign(['size_id']);
|
||||
$table->dropColumn(['size_id', 'units_produced', 'quantity_g']);
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user