layer-chicken/app/Models/WarehouseStock.php

44 lines
869 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'];
protected $casts = [
'quantity' => 'decimal:2',
];
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
{
if ($isBroken) {
return;
}
$stock = self::firstOrCreate([
'warehouse_id' => $warehouseId,
'unit_id' => $unitId,
], [
'quantity' => 0,
]);
$stock->increment('quantity', $quantity);
}
}