Add piece_weight calibration: enter Stück + Gramm per delivery, average auto-calculated
Deploy / deploy (push) Successful in 17s

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Malek TellissiandClaude Sonnet 4.6 committed 2026-09-13 01:26:17 +02:00
1 parent 38bcf60778
commit d87383672c
5 files changed
+73 -1

No files matched your search

@@ -7,6 +7,7 @@ use App\Filament\User\Resources\SpiceResource\RelationManagers;
use App\Models\Spice; use App\Models\Spice;
use Filament\Forms; use Filament\Forms;
use Filament\Forms\Form; use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Resources\Resource; use Filament\Resources\Resource;
use Filament\Tables; use Filament\Tables;
use Filament\Tables\Table; use Filament\Tables\Table;
@@ -35,12 +36,19 @@ class SpiceResource extends Resource
->label('Mengeneinheit') ->label('Mengeneinheit')
->native(false) ->native(false)
->searchable() ->searchable()
->live()
->options([ ->options([
'grams' => '(Kilo-)Gramm', 'grams' => '(Kilo-)Gramm',
'pieces' => 'Stück', 'pieces' => 'Stück',
'liter' => 'Liter', 'liter' => 'Liter',
]) ])
->required(), ->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'),
]); ]);
} }
@@ -71,6 +71,10 @@ class SpiceSupplierResource extends Resource
->required() ->required()
->rules(['min:0.001']) ->rules(['min:0.001'])
->disabled(fn($record) => !is_null($record)) ->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) { ->suffix(function (Get $get) {
$unit = \App\Models\Spice::find($get('spice_id'))?->unit; $unit = \App\Models\Spice::find($get('spice_id'))?->unit;
return match($unit) { return match($unit) {
@@ -90,6 +94,16 @@ class SpiceSupplierResource extends Resource
$unit = \App\Models\Spice::find($get('spice_id'))?->unit; $unit = \App\Models\Spice::find($get('spice_id'))?->unit;
return $unit === 'grams' ? (float) $state * 1000 : $state; 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') DatePicker::make('supplied_at')
->label('Lieferdatum') ->label('Lieferdatum')
+14 -1
View File
@@ -6,7 +6,7 @@ use Illuminate\Database\Eloquent\Model;
class Spice extends Model class Spice extends Model
{ {
protected $fillable = ['name', 'quantity', 'unit']; protected $fillable = ['name', 'quantity', 'unit', 'piece_weight'];
public function recipes() public function recipes()
{ {
@@ -24,4 +24,17 @@ class Spice extends Model
{ {
$this->update(['quantity' => $this->stockMovements()->sum('amount')]); $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)]);
}
}
} }
+7
View File
@@ -15,6 +15,7 @@ class SpiceSupplier extends Model
'spice_id', 'spice_id',
'supplier_id', 'supplier_id',
'amount', 'amount',
'weight_g',
'supplied_at', 'supplied_at',
]; ];
@@ -39,6 +40,9 @@ class SpiceSupplier extends Model
'description' => 'Lieferung' . ($ss->supplier ? ' von ' . $ss->supplier->name : ''), 'description' => 'Lieferung' . ($ss->supplier ? ' von ' . $ss->supplier->name : ''),
]); ]);
$ss->spice->recalculateQuantity(); $ss->spice->recalculateQuantity();
if ($ss->weight_g && $ss->amount) {
$ss->spice->recalculatePieceWeight();
}
}); });
static::deleted(function (SpiceSupplier $ss) { static::deleted(function (SpiceSupplier $ss) {
@@ -46,6 +50,9 @@ class SpiceSupplier extends Model
->where('source_id', $ss->id) ->where('source_id', $ss->id)
->delete(); ->delete();
$ss->spice->recalculateQuantity(); $ss->spice->recalculateQuantity();
if ($ss->weight_g) {
$ss->spice->recalculatePieceWeight();
}
}); });
} }
} }
@@ -0,0 +1,30 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::table('spices', function (Blueprint $table) {
$table->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');
});
}
};