Files
SpiceManagementSystem/app/Models/SpiceSupplier.php
T
Malek TellissiandClaude Sonnet 4.6 fb19e9a579
Deploy / deploy (push) Successful in 16s
Introduce spice_stock_movements ledger for traceable stock changes
Every delivery and batch now writes a signed movement entry instead of
directly incrementing/decrementing spice.quantity. The balance is always
the sum of all movements. Existing data is backfilled in the migration.
Rohstoff edit page shows the full movement history (Lagerbewegungen tab).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-12 22:34:10 +02:00

52 lines
1.3 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class SpiceSupplier extends Model
{
use HasFactory;
protected $table = 'spices_suppliers'; // explizit, weil der Standard sonst 'spice_suppliers' wäre
protected $fillable = [
'spice_id',
'supplier_id',
'amount',
'supplied_at',
];
public function spice()
{
return $this->belongsTo(Spice::class);
}
public function supplier()
{
return $this->belongsTo(Supplier::class);
}
protected static function booted(): void
{
static::created(function (SpiceSupplier $ss) {
SpiceStockMovement::create([
'spice_id' => $ss->spice_id,
'amount' => $ss->amount,
'source_type' => 'delivery',
'source_id' => $ss->id,
'description' => 'Lieferung' . ($ss->supplier ? ' von ' . $ss->supplier->name : ''),
]);
$ss->spice->recalculateQuantity();
});
static::deleted(function (SpiceSupplier $ss) {
SpiceStockMovement::where('source_type', 'delivery')
->where('source_id', $ss->id)
->delete();
$ss->spice->recalculateQuantity();
});
}
}