Pre-fill batch origins from recipe in standalone BatchResource
Deploy / deploy (push) Successful in 14s
Deploy / deploy (push) Successful in 14s
Selecting a Produkt auto-sets the current Rezeptur and pre-fills one Herkunft row per Rohstoff. Changing the Rezeptur resets the rows. Rohstoff is shown read-only (Hidden + Placeholder); user fills in supplier, origin country, farmer and notes only. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
62be3761a9
commit
2dffe6f695
1 file changed
+62
-14
@@ -4,8 +4,10 @@ namespace App\Filament\User\Resources;
|
||||
|
||||
use App\Filament\User\Resources\BatchResource\Pages;
|
||||
use App\Models\Batch;
|
||||
use App\Models\Product;
|
||||
use App\Models\ProductSize;
|
||||
use App\Models\Recipe;
|
||||
use App\Models\Spice;
|
||||
use Filament\Forms;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Forms\Get;
|
||||
@@ -41,7 +43,28 @@ class BatchResource extends Resource
|
||||
->preload()
|
||||
->required()
|
||||
->live()
|
||||
->afterStateUpdated(fn ($set) => $set('size_id', null)),
|
||||
->afterStateUpdated(function ($state, $set) {
|
||||
$set('size_id', null);
|
||||
$set('recipe_id', null);
|
||||
$set('origins', []);
|
||||
if (! $state) {
|
||||
return;
|
||||
}
|
||||
$product = Product::find($state);
|
||||
if ($product?->recipes_id) {
|
||||
$set('recipe_id', $product->recipes_id);
|
||||
$recipe = Recipe::with('spices')->find($product->recipes_id);
|
||||
if ($recipe) {
|
||||
$set('origins', $recipe->spices->map(fn ($s) => [
|
||||
'spice_id' => $s->id,
|
||||
'supplier_id' => null,
|
||||
'origin_country' => null,
|
||||
'farmer' => null,
|
||||
'notes' => null,
|
||||
])->toArray());
|
||||
}
|
||||
}
|
||||
}),
|
||||
|
||||
Forms\Components\Select::make('size_id')
|
||||
->label('Variante (Verpackung)')
|
||||
@@ -55,11 +78,29 @@ class BatchResource extends Resource
|
||||
|
||||
Forms\Components\Select::make('recipe_id')
|
||||
->label('Rezeptur')
|
||||
->relationship('recipe', 'name')
|
||||
->searchable()
|
||||
->preload()
|
||||
->options(fn (Get $get) => Recipe::where('product_id', $get('product_id') ?? 0)
|
||||
->orderByDesc('version')
|
||||
->get()
|
||||
->mapWithKeys(fn ($r) => [$r->id => 'v' . $r->version . ' · ' . $r->created_at->format('d.m.Y')])
|
||||
)
|
||||
->required()
|
||||
->live(),
|
||||
->live()
|
||||
->afterStateUpdated(function ($state, $set) {
|
||||
if (! $state) {
|
||||
$set('origins', []);
|
||||
return;
|
||||
}
|
||||
$recipe = Recipe::with('spices')->find($state);
|
||||
if ($recipe) {
|
||||
$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')
|
||||
@@ -87,17 +128,24 @@ class BatchResource extends Resource
|
||||
->label('Mindesthaltbarkeitsdatum'),
|
||||
])->columns(2),
|
||||
|
||||
Forms\Components\Section::make('Herkunft der Gewürze')->schema([
|
||||
Forms\Components\Section::make('Herkunft der Rohstoffe')->schema([
|
||||
Forms\Components\Repeater::make('origins')
|
||||
->label('')
|
||||
->relationship()
|
||||
->afterStateHydrated(function ($component, $record) {
|
||||
// Pre-fill on create only when no origins exist
|
||||
if ($record?->id) {
|
||||
return;
|
||||
}
|
||||
// State is set reactively via recipe_id afterStateUpdated
|
||||
})
|
||||
->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')
|
||||
@@ -115,12 +163,12 @@ class BatchResource extends Resource
|
||||
->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')
|
||||
->reorderable(false),
|
||||
]),
|
||||
]);
|
||||
|
||||
Reference in new issue
Block a user