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>
146 lines
5.3 KiB
PHP
146 lines
5.3 KiB
PHP
<?php
|
||
|
||
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
|
||
{
|
||
protected static string $relationship = 'batches';
|
||
protected static ?string $title = 'Chargen (LOT)';
|
||
protected static ?string $modelLabel = 'Charge';
|
||
protected static ?string $pluralModelLabel = 'Chargen';
|
||
|
||
public function form(Form $form): Form
|
||
{
|
||
return $form->schema([
|
||
Forms\Components\TextInput::make('lot_number')
|
||
->label('LOT-Nummer')
|
||
->required()
|
||
->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')
|
||
->searchable()
|
||
->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'),
|
||
|
||
Forms\Components\DatePicker::make('expires_at')
|
||
->label('Mindesthaltbarkeitsdatum'),
|
||
|
||
Forms\Components\Repeater::make('origins')
|
||
->label('Herkunft der Gewürze')
|
||
->relationship()
|
||
->schema([
|
||
Forms\Components\Select::make('spice_id')
|
||
->label('Gewürz')
|
||
->relationship('spice', 'name')
|
||
->searchable()
|
||
->preload()
|
||
->required(),
|
||
|
||
Forms\Components\Select::make('supplier_id')
|
||
->label('Lieferant')
|
||
->relationship('supplier', 'name')
|
||
->searchable()
|
||
->preload()
|
||
->nullable(),
|
||
|
||
Forms\Components\TextInput::make('origin_country')
|
||
->label('Herkunftsland')
|
||
->maxLength(100),
|
||
|
||
Forms\Components\TextInput::make('farmer')
|
||
->label('Landwirt / Betrieb')
|
||
->maxLength(255),
|
||
|
||
Forms\Components\Textarea::make('notes')
|
||
->label('Infos zum Gewürz')
|
||
->rows(2)
|
||
->columnSpanFull(),
|
||
])
|
||
->columns(2)
|
||
->addActionLabel('Gewürz hinzufügen')
|
||
->columnSpanFull(),
|
||
]);
|
||
}
|
||
|
||
public function table(Table $table): Table
|
||
{
|
||
return $table
|
||
->recordTitleAttribute('lot_number')
|
||
->columns([
|
||
Tables\Columns\TextColumn::make('lot_number')
|
||
->label('LOT-Nummer')
|
||
->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'),
|
||
|
||
Tables\Columns\TextColumn::make('produced_at')
|
||
->label('Produktion')
|
||
->date('d.m.Y')
|
||
->sortable(),
|
||
|
||
Tables\Columns\TextColumn::make('expires_at')
|
||
->label('MHD')
|
||
->date('d.m.Y')
|
||
->sortable(),
|
||
])
|
||
->defaultSort('produced_at', 'desc')
|
||
->actions([
|
||
Tables\Actions\EditAction::make(),
|
||
Tables\Actions\DeleteAction::make(),
|
||
])
|
||
->headerActions([
|
||
Tables\Actions\CreateAction::make(),
|
||
]);
|
||
}
|
||
}
|