59 lines
1.4 KiB
PHP
59 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace Modules\MaterialLogistics\Models;
|
|
use Modules\MasterData\Models\Material;
|
|
use Modules\MasterData\Models\MaterialGroup;
|
|
|
|
use App\Traits\HasPublicIdentifier;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
class WarehouseStock extends Model
|
|
{
|
|
use HasPublicIdentifier;
|
|
|
|
protected $table = 'warehouse_stock';
|
|
|
|
protected $fillable = [
|
|
'warehouse_id', 'material_id',
|
|
'quantity', 'reserved_qty', 'unit_cost', 'low_stock_threshold',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'quantity' => 'decimal:2',
|
|
'reserved_qty' => 'decimal:2',
|
|
'unit_cost' => 'decimal:2',
|
|
'low_stock_threshold' => 'integer',
|
|
];
|
|
}
|
|
|
|
public function warehouse(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Warehouse::class);
|
|
}
|
|
|
|
public function material(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Material::class);
|
|
}
|
|
|
|
public function getAvailableQtyAttribute(): float
|
|
{
|
|
return (float) $this->quantity - (float) $this->reserved_qty;
|
|
}
|
|
|
|
public function getTotalValueAttribute(): float
|
|
{
|
|
return (float) $this->quantity * (float) $this->unit_cost;
|
|
}
|
|
|
|
public function getIsLowStockAttribute(): bool
|
|
{
|
|
$threshold = $this->low_stock_threshold ?? 10;
|
|
return (float) $this->quantity > 0 && (float) $this->quantity <= $threshold;
|
|
}
|
|
}
|
|
|