laravel-vuexy-warehouse/Models/InventoryStockLevel.php

63 lines
1.9 KiB
PHP
Raw Permalink Normal View History

2025-03-05 20:44:45 -06:00
<?php
namespace Koneko\VuexyWarehouse\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Koneko\VuexyStoreManager\Models\Store;
class InventoryStockLevel extends Model
{
protected $fillable = [
'product_id', 'store_id', 'warehouse_id', 'quantity',
'pos_stock', 'ecommerce_stock', 'purchase_reserved_stock', 'asset_stock',
'last_cost', 'average_cost', 'total_last_cost', 'total_average_cost', 'total_identified_cost',
'costing_method'
];
protected $casts = [
'quantity' => 'decimal:6',
'pos_stock' => 'decimal:6',
'ecommerce_stock' => 'decimal:6',
'purchase_reserved_stock' => 'decimal:6',
'asset_stock' => 'decimal:6',
'last_cost' => 'decimal:2',
'average_cost' => 'decimal:2',
'total_last_cost' => 'decimal:2',
'total_average_cost' => 'decimal:2',
'total_identified_cost' => 'decimal:2',
'costing_method' => 'integer',
];
/**
* Verificar que el stock en áreas no supere el stock total
*/
public static function boot()
{
parent::boot();
static::saving(function ($inventory) {
$totalReserved = $inventory->pos_stock + $inventory->ecommerce_stock + $inventory->purchase_reserved_stock + $inventory->asset_stock;
if ($totalReserved > $inventory->quantity) {
throw new \Exception("Error: El stock reservado no puede ser mayor al stock total.");
}
});
}
public function product()
{
return $this->belongsTo(Product::class, 'product_id');
}
public function store(): BelongsTo
{
return $this->belongsTo(Store::class, 'store_id');
}
public function warehouse()
{
return $this->belongsTo(Warehouse::class, 'warehouse_id');
}
}