From 62be3761a922c797f9b62db99483f6eadda45e36 Mon Sep 17 00:00:00 2001 From: Malek Tellissi Date: Sat, 12 Sep 2026 18:54:00 +0200 Subject: [PATCH] Pre-fill batch origin rows from recipe spices automatically When creating a new batch, the Herkunft-Repeater is automatically populated with one row per Rohstoff from the product's current recipe. The Rohstoff field is shown as a read-only label (Hidden spice_id + Placeholder name) so the user only fills in supplier, origin country, farmer and notes. Changing the recipe field updates the origin rows accordingly. On edit, existing origin data is preserved unchanged. Co-Authored-By: Claude Sonnet 4.6 --- .../BatchesRelationManager.php | 83 +++++++++++++++---- 1 file changed, 65 insertions(+), 18 deletions(-) diff --git a/app/Filament/User/Resources/ProductResource/RelationManagers/BatchesRelationManager.php b/app/Filament/User/Resources/ProductResource/RelationManagers/BatchesRelationManager.php index 9d8dcc5..1cf5066 100644 --- a/app/Filament/User/Resources/ProductResource/RelationManagers/BatchesRelationManager.php +++ b/app/Filament/User/Resources/ProductResource/RelationManagers/BatchesRelationManager.php @@ -3,6 +3,8 @@ namespace App\Filament\User\Resources\ProductResource\RelationManagers; use App\Models\ProductSize; +use App\Models\Recipe; +use App\Models\Spice; use Filament\Forms; use Filament\Forms\Form; use Filament\Forms\Get; @@ -39,10 +41,31 @@ class BatchesRelationManager extends RelationManager Forms\Components\Select::make('recipe_id') ->label('Rezeptur') - ->relationship('recipe', 'name') - ->searchable() - ->preload() - ->required(), + ->options(fn () => Recipe::where('product_id', $this->getOwnerRecord()->id) + ->orderByDesc('version') + ->get() + ->mapWithKeys(fn ($r) => [$r->id => 'v' . $r->version . ' · ' . $r->created_at->format('d.m.Y')]) + ) + ->default(fn () => $this->getOwnerRecord()->recipes_id) + ->required() + ->live() + ->afterStateUpdated(function ($state, $set) { + if (! $state) { + $set('origins', []); + return; + } + $recipe = Recipe::with('spices')->find($state); + if (! $recipe) { + return; + } + $set('origins', $recipe->spices->map(fn ($s) => [ + 'spice_id' => $s->id, + 'supplier_id' => null, + 'origin_country' => null, + 'farmer' => null, + 'notes' => null, + ])->toArray()); + }), Forms\Components\TextInput::make('units_produced') ->label('Anzahl Einheiten') @@ -53,12 +76,12 @@ class BatchesRelationManager extends RelationManager Forms\Components\Placeholder::make('quantity_g_preview') ->label('Gesamtgewicht (berechnet)') ->content(function (Get $get): HtmlString { - $units = (int) $get('units_produced'); + $units = (int) $get('units_produced'); $sizeId = $get('size_id'); if (! $units || ! $sizeId) { return new HtmlString('—'); } - $size = ProductSize::with('packing')->find($sizeId); + $size = ProductSize::with('packing')->find($sizeId); $totalG = $units * ($size?->packing?->size ?? 0); return new HtmlString("{$totalG} g ({$units} × {$size?->packing?->size}g)"); }), @@ -70,15 +93,36 @@ class BatchesRelationManager extends RelationManager ->label('Mindesthaltbarkeitsdatum'), Forms\Components\Repeater::make('origins') - ->label('Herkunft der Gewürze') + ->label('Herkunft der Rohstoffe') ->relationship() + ->afterStateHydrated(function ($component, $record, $livewire) { + // Pre-fill from product's current recipe only when creating a new batch + if ($record?->id) { + return; + } + $recipeId = $livewire->getOwnerRecord()->recipes_id; + if (! $recipeId) { + return; + } + $recipe = Recipe::with('spices')->find($recipeId); + if (! $recipe) { + return; + } + $component->state($recipe->spices->map(fn ($s) => [ + 'spice_id' => $s->id, + 'supplier_id' => null, + 'origin_country' => null, + 'farmer' => null, + 'notes' => null, + ])->toArray()); + }) ->schema([ - Forms\Components\Select::make('spice_id') - ->label('Gewürz') - ->relationship('spice', 'name') - ->searchable() - ->preload() - ->required(), + Forms\Components\Hidden::make('spice_id'), + Forms\Components\Placeholder::make('spice_name') + ->label('Rohstoff') + ->content(fn (Get $get): string => + Spice::find($get('spice_id'))?->name ?? '—' + ), Forms\Components\Select::make('supplier_id') ->label('Lieferant') @@ -96,12 +140,12 @@ class BatchesRelationManager extends RelationManager ->maxLength(255), Forms\Components\Textarea::make('notes') - ->label('Infos zum Gewürz') + ->label('Notizen') ->rows(2) ->columnSpanFull(), ]) ->columns(2) - ->addActionLabel('Gewürz hinzufügen') + ->addActionLabel('Rohstoff hinzufügen') ->columnSpanFull(), ]); } @@ -118,10 +162,13 @@ class BatchesRelationManager extends RelationManager Tables\Columns\TextColumn::make('units_produced') ->label('Einheiten') - ->suffix(fn ($record) => $record->quantity_g ? ' (' . number_format($record->quantity_g, 0, ',', '.') . 'g)' : ''), + ->suffix(fn ($record) => $record->quantity_g + ? ' (' . number_format($record->quantity_g, 0, ',', '.') . 'g)' + : ''), - Tables\Columns\TextColumn::make('recipe.name') - ->label('Rezeptur'), + Tables\Columns\TextColumn::make('recipe.version') + ->label('Rezeptur') + ->formatStateUsing(fn ($state) => $state ? 'v' . $state : '—'), Tables\Columns\TextColumn::make('produced_at') ->label('Produktion')