Files
SpiceManagementSystem/app/Models/SpiceSupplier.php
T
2026-09-13 01:26:17 +02:00

59 lines
1.6 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',
'weight_g',
'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();
if ($ss->weight_g && $ss->amount) {
$ss->spice->recalculatePieceWeight();
}
});
static::deleted(function (SpiceSupplier $ss) {
SpiceStockMovement::where('source_type', 'delivery')
->where('source_id', $ss->id)
->delete();
$ss->spice->recalculateQuantity();
if ($ss->weight_g) {
$ss->spice->recalculatePieceWeight();
}
});
}
}