erp-backend/app/Models/Stock.php
2026-04-01 17:07:04 +08:00

59 lines
1.2 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Stock extends Model
{
protected $table = 'stocks';
protected $fillable = [
'sku_code',
'sku_name',
'warehouse_id',
'quantity',
'locked_quantity',
'defective_quantity',
'warning_threshold',
];
protected $casts = [
'quantity' => 'integer',
'locked_quantity' => 'integer',
'defective_quantity' => 'integer',
'warning_threshold' => 'integer',
];
/**
* 可用库存(总库存 - 占用库存)
*/
public function getAvailableQuantityAttribute()
{
return $this->quantity - $this->locked_quantity;
}
/**
* 关联仓库
*/
public function warehouse()
{
return $this->belongsTo(Warehouse::class);
}
/**
* 关联ERP商品
*/
public function sku()
{
return $this->belongsTo(ErpSku::class, 'sku_code', 'sku_code');
}
/**
* 获取该SKU在该仓库的库存流水
*/
public function logs()
{
return $this->hasMany(StockLog::class, 'sku_code', 'sku_code')
->where('warehouse_id', $this->warehouse_id);
}
}