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\Filament\User\Resources\BatchResource\Pages;
|
||||||
use App\Models\Batch;
|
use App\Models\Batch;
|
||||||
|
use App\Models\Product;
|
||||||
use App\Models\ProductSize;
|
use App\Models\ProductSize;
|
||||||
use App\Models\Recipe;
|
use App\Models\Recipe;
|
||||||
|
use App\Models\Spice;
|
||||||
use Filament\Forms;
|
use Filament\Forms;
|
||||||
use Filament\Forms\Form;
|
use Filament\Forms\Form;
|
||||||
use Filament\Forms\Get;
|
use Filament\Forms\Get;
|
||||||
@@ -41,7 +43,28 @@ class BatchResource extends Resource
|
|||||||
->preload()
|
->preload()
|
||||||
->required()
|
->required()
|
||||||
->live()
|
->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')
|
Forms\Components\Select::make('size_id')
|
||||||
->label('Variante (Verpackung)')
|
->label('Variante (Verpackung)')
|
||||||
@@ -55,11 +78,29 @@ class BatchResource extends Resource
|
|||||||
|
|
||||||
Forms\Components\Select::make('recipe_id')
|
Forms\Components\Select::make('recipe_id')
|
||||||
->label('Rezeptur')
|
->label('Rezeptur')
|
||||||
->relationship('recipe', 'name')
|
->options(fn (Get $get) => Recipe::where('product_id', $get('product_id') ?? 0)
|
||||||
->searchable()
|
->orderByDesc('version')
|
||||||
->preload()
|
->get()
|
||||||
|
->mapWithKeys(fn ($r) => [$r->id => 'v' . $r->version . ' · ' . $r->created_at->format('d.m.Y')])
|
||||||
|
)
|
||||||
->required()
|
->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')
|
Forms\Components\TextInput::make('units_produced')
|
||||||
->label('Anzahl Einheiten')
|
->label('Anzahl Einheiten')
|
||||||
@@ -87,17 +128,24 @@ class BatchResource extends Resource
|
|||||||
->label('Mindesthaltbarkeitsdatum'),
|
->label('Mindesthaltbarkeitsdatum'),
|
||||||
])->columns(2),
|
])->columns(2),
|
||||||
|
|
||||||
Forms\Components\Section::make('Herkunft der Gewürze')->schema([
|
Forms\Components\Section::make('Herkunft der Rohstoffe')->schema([
|
||||||
Forms\Components\Repeater::make('origins')
|
Forms\Components\Repeater::make('origins')
|
||||||
->label('')
|
->label('')
|
||||||
->relationship()
|
->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([
|
->schema([
|
||||||
Forms\Components\Select::make('spice_id')
|
Forms\Components\Hidden::make('spice_id'),
|
||||||
->label('Gewürz')
|
Forms\Components\Placeholder::make('spice_name')
|
||||||
->relationship('spice', 'name')
|
->label('Rohstoff')
|
||||||
->searchable()
|
->content(fn (Get $get): string =>
|
||||||
->preload()
|
Spice::find($get('spice_id'))?->name ?? '—'
|
||||||
->required(),
|
),
|
||||||
|
|
||||||
Forms\Components\Select::make('supplier_id')
|
Forms\Components\Select::make('supplier_id')
|
||||||
->label('Lieferant')
|
->label('Lieferant')
|
||||||
@@ -115,12 +163,12 @@ class BatchResource extends Resource
|
|||||||
->maxLength(255),
|
->maxLength(255),
|
||||||
|
|
||||||
Forms\Components\Textarea::make('notes')
|
Forms\Components\Textarea::make('notes')
|
||||||
->label('Infos zum Gewürz')
|
->label('Notizen')
|
||||||
->rows(2)
|
->rows(2)
|
||||||
->columnSpanFull(),
|
->columnSpanFull(),
|
||||||
])
|
])
|
||||||
->columns(2)
|
->columns(2)
|
||||||
->addActionLabel('Gewürz hinzufügen')
|
->addActionLabel('Rohstoff hinzufügen')
|
||||||
->reorderable(false),
|
->reorderable(false),
|
||||||
]),
|
]),
|
||||||
]);
|
]);
|
||||||
|
|||||||
Reference in new issue
Block a user