Deploy / deploy (push) Successful in 16s
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>
28 lines
610 B
PHP
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')]);
|
|
}
|
|
}
|