Deploy / deploy (push) Successful in 15s
The recipe name input is dropped from the product form — the version number alone identifies a recipe. The product title is stored as the internal name to satisfy the NOT NULL constraint. The history table no longer shows the name column. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
96 lines
2.6 KiB
PHP
96 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\User\Resources\ProductResource\Pages;
|
|
|
|
use App\Filament\User\Resources\ProductResource;
|
|
use App\Models\Recipe;
|
|
use Filament\Actions;
|
|
use Filament\Resources\Pages\EditRecord;
|
|
|
|
class EditProduct extends EditRecord
|
|
{
|
|
protected static string $resource = ProductResource::class;
|
|
|
|
protected function getHeaderActions(): array
|
|
{
|
|
return [
|
|
Actions\Action::make('rezept-versionen')
|
|
->label('Rezept-Versionen')
|
|
->icon('heroicon-o-clock')
|
|
->color('gray')
|
|
->url(fn () => ProductResource::getUrl('recipe-history', ['record' => $this->record])),
|
|
Actions\DeleteAction::make(),
|
|
];
|
|
}
|
|
|
|
protected function afterSave(): void
|
|
{
|
|
$this->syncSizes();
|
|
$this->syncRecipe();
|
|
}
|
|
|
|
protected function syncSizes(): void
|
|
{
|
|
$product = $this->record;
|
|
|
|
$sizes = collect($this->data['sizes'] ?? [])
|
|
->mapWithKeys(function ($size) {
|
|
return [
|
|
$size['packing_id'] => [
|
|
'description' => $size['description'] ?? null,
|
|
'price' => $size['price'],
|
|
'sku' => $size['sku'] ?? null,
|
|
'gtin' => $size['gtin'] ?? null,
|
|
],
|
|
];
|
|
})
|
|
->toArray();
|
|
|
|
$product->sizes()->sync($sizes);
|
|
}
|
|
|
|
protected function syncRecipe(): void
|
|
{
|
|
$product = $this->record;
|
|
$spices = $this->data['recipe_spices'] ?? [];
|
|
|
|
if (empty($spices)) {
|
|
return;
|
|
}
|
|
|
|
$newPivot = collect($spices)->mapWithKeys(fn ($s) => [
|
|
(int) $s['spice_id'] => (float) $s['amount_in_percent'],
|
|
])->toArray();
|
|
|
|
$current = $product->recipe;
|
|
|
|
if ($current) {
|
|
$current->load('spices');
|
|
|
|
$existingPivot = $current->spices->mapWithKeys(fn ($s) => [
|
|
$s->id => (float) $s->pivot->amount_in_percent,
|
|
])->toArray();
|
|
|
|
if ($existingPivot == $newPivot) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
$nextVersion = Recipe::where('product_id', $product->id)->max('version') + 1;
|
|
|
|
$recipe = Recipe::create([
|
|
'name' => $product->title,
|
|
'product_id' => $product->id,
|
|
'version' => $nextVersion,
|
|
]);
|
|
|
|
$recipe->spices()->sync(
|
|
collect($newPivot)->mapWithKeys(fn ($pct, $id) => [
|
|
$id => ['amount_in_percent' => $pct],
|
|
])->toArray()
|
|
);
|
|
|
|
$product->update(['recipes_id' => $recipe->id]);
|
|
}
|
|
}
|