From d87383672c3d80fc877b1cf375cb271b355aefc7 Mon Sep 17 00:00:00 2001 From: Malek Tellissi Date: Sun, 13 Sep 2026 01:26:17 +0200 Subject: [PATCH] =?UTF-8?q?Add=20piece=5Fweight=20calibration:=20enter=20S?= =?UTF-8?q?t=C3=BCck=20+=20Gramm=20per=20delivery,=20average=20auto-calcul?= =?UTF-8?q?ated?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 4.6 --- app/Filament/User/Resources/SpiceResource.php | 8 +++++ .../User/Resources/SpiceSupplierResource.php | 14 +++++++++ app/Models/Spice.php | 15 +++++++++- app/Models/SpiceSupplier.php | 7 +++++ ...d_piece_weight_to_spices_and_suppliers.php | 30 +++++++++++++++++++ 5 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 database/migrations/2026_09_13_000000_add_piece_weight_to_spices_and_suppliers.php diff --git a/app/Filament/User/Resources/SpiceResource.php b/app/Filament/User/Resources/SpiceResource.php index cc8cefc..2e6e52c 100644 --- a/app/Filament/User/Resources/SpiceResource.php +++ b/app/Filament/User/Resources/SpiceResource.php @@ -7,6 +7,7 @@ use App\Filament\User\Resources\SpiceResource\RelationManagers; use App\Models\Spice; use Filament\Forms; use Filament\Forms\Form; +use Filament\Forms\Get; use Filament\Resources\Resource; use Filament\Tables; use Filament\Tables\Table; @@ -35,12 +36,19 @@ class SpiceResource extends Resource ->label('Mengeneinheit') ->native(false) ->searchable() + ->live() ->options([ 'grams' => '(Kilo-)Gramm', 'pieces' => 'Stück', 'liter' => 'Liter', ]) ->required(), + Forms\Components\TextInput::make('piece_weight') + ->label('Ø Stückgewicht (g)') + ->numeric() + ->suffix('g') + ->helperText('Wird automatisch aus Lieferungen errechnet, kann aber auch manuell gesetzt werden.') + ->visible(fn (Get $get) => $get('unit') === 'pieces'), ]); } diff --git a/app/Filament/User/Resources/SpiceSupplierResource.php b/app/Filament/User/Resources/SpiceSupplierResource.php index eefdf38..d692f70 100644 --- a/app/Filament/User/Resources/SpiceSupplierResource.php +++ b/app/Filament/User/Resources/SpiceSupplierResource.php @@ -71,6 +71,10 @@ class SpiceSupplierResource extends Resource ->required() ->rules(['min:0.001']) ->disabled(fn($record) => !is_null($record)) + ->label(function (Get $get) { + $unit = \App\Models\Spice::find($get('spice_id'))?->unit; + return $unit === 'pieces' ? 'Anzahl Stück' : 'Liefermenge'; + }) ->suffix(function (Get $get) { $unit = \App\Models\Spice::find($get('spice_id'))?->unit; return match($unit) { @@ -90,6 +94,16 @@ class SpiceSupplierResource extends Resource $unit = \App\Models\Spice::find($get('spice_id'))?->unit; return $unit === 'grams' ? (float) $state * 1000 : $state; }), + + TextInput::make('weight_g') + ->label('Gesamtgewicht dieser Lieferung (g)') + ->numeric() + ->suffix('g') + ->helperText('Optional — aktualisiert das Ø Stückgewicht automatisch') + ->disabled(fn($record) => !is_null($record)) + ->visible(function (Get $get) { + return \App\Models\Spice::find($get('spice_id'))?->unit === 'pieces'; + }), DatePicker::make('supplied_at') ->label('Lieferdatum') diff --git a/app/Models/Spice.php b/app/Models/Spice.php index 90c4783..5451232 100644 --- a/app/Models/Spice.php +++ b/app/Models/Spice.php @@ -6,7 +6,7 @@ use Illuminate\Database\Eloquent\Model; class Spice extends Model { - protected $fillable = ['name', 'quantity', 'unit']; + protected $fillable = ['name', 'quantity', 'unit', 'piece_weight']; public function recipes() { @@ -24,4 +24,17 @@ class Spice extends Model { $this->update(['quantity' => $this->stockMovements()->sum('amount')]); } + + public function recalculatePieceWeight(): void + { + $result = SpiceSupplier::where('spice_id', $this->id) + ->whereNotNull('weight_g') + ->where('weight_g', '>', 0) + ->selectRaw('SUM(weight_g) as total_grams, SUM(amount) as total_pieces') + ->first(); + + if ($result && $result->total_pieces > 0) { + $this->update(['piece_weight' => round($result->total_grams / $result->total_pieces, 3)]); + } + } } diff --git a/app/Models/SpiceSupplier.php b/app/Models/SpiceSupplier.php index bf3567a..d8f99fe 100644 --- a/app/Models/SpiceSupplier.php +++ b/app/Models/SpiceSupplier.php @@ -15,6 +15,7 @@ class SpiceSupplier extends Model 'spice_id', 'supplier_id', 'amount', + 'weight_g', 'supplied_at', ]; @@ -39,6 +40,9 @@ class SpiceSupplier extends Model 'description' => 'Lieferung' . ($ss->supplier ? ' von ' . $ss->supplier->name : ''), ]); $ss->spice->recalculateQuantity(); + if ($ss->weight_g && $ss->amount) { + $ss->spice->recalculatePieceWeight(); + } }); static::deleted(function (SpiceSupplier $ss) { @@ -46,6 +50,9 @@ class SpiceSupplier extends Model ->where('source_id', $ss->id) ->delete(); $ss->spice->recalculateQuantity(); + if ($ss->weight_g) { + $ss->spice->recalculatePieceWeight(); + } }); } } diff --git a/database/migrations/2026_09_13_000000_add_piece_weight_to_spices_and_suppliers.php b/database/migrations/2026_09_13_000000_add_piece_weight_to_spices_and_suppliers.php new file mode 100644 index 0000000..8cda2d5 --- /dev/null +++ b/database/migrations/2026_09_13_000000_add_piece_weight_to_spices_and_suppliers.php @@ -0,0 +1,30 @@ +decimal('piece_weight', 10, 3)->nullable()->after('unit'); + }); + + Schema::table('spices_suppliers', function (Blueprint $table) { + $table->decimal('weight_g', 10, 3)->nullable()->after('amount'); + }); + } + + public function down(): void + { + Schema::table('spices', function (Blueprint $table) { + $table->dropColumn('piece_weight'); + }); + + Schema::table('spices_suppliers', function (Blueprint $table) { + $table->dropColumn('weight_g'); + }); + } +};