layer-chicken/app/Models/WarehouseStock.php

37 lines
783 B
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class WarehouseStock extends Model
{
use HasFactory;
protected $guarded = ['id'];
public function warehouse()
{
return $this->belongsTo(Warehouse::class);
}
public function unit()
{
return $this->belongsTo(Unit::class);
}
public static function adjustStock(int $warehouseId, int $unitId, bool $isBroken, float $quantity): void
{
$stock = self::firstOrCreate([
'warehouse_id' => $warehouseId,
'unit_id' => $unitId,
'is_broken' => $isBroken,
], [
'quantity' => 0,
]);
$stock->increment('quantity', $quantity);
}
}