Files
SpiceManagementSystem/app/Models/Spice.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

28 lines
610 B
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Spice extends Model
{
protected $fillable = ['name', 'quantity', 'unit'];
public function recipes()
{
return $this->belongsToMany(Recipe::class, 'recipes_spices')
->withPivot('amount_in_percent')
->withTimestamps();
}
public function stockMovements()
{
return $this->hasMany(SpiceStockMovement::class);
}
public function recalculateQuantity(): void
{
$this->update(['quantity' => $this->stockMovements()->sum('amount')]);
}
}