Files
xin-procurement/app/Models/StoreOrderItemModel.php
T
2026-08-12 23:20:05 +08:00

122 lines
3.1 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* 门店订单明细模型(下单时快照商品档案:品名/规格/单位/供应商/图文/成本价 + 等级单价,
* 商品调价或档案变更不影响历史单据)
*/
class StoreOrderItemModel extends Model
{
use HasFactory;
protected $table = 'store_order_item';
protected $primaryKey = 'id';
protected $fillable = [
'order_id',
'purchase_id',
'store_id',
'product_id',
'category_id',
'supplier_id',
'product_name',
'product_spec',
'unit',
'price',
'image_ids',
'content',
'shelf_life',
'quantity',
'weight',
'amount',
'cost_price',
'remark',
];
protected $casts = [
'order_id' => 'integer',
'purchase_id' => 'integer',
'store_id' => 'integer',
'product_id' => 'integer',
'category_id' => 'integer',
'supplier_id' => 'integer',
'price' => 'decimal:2',
'shelf_life' => 'integer',
'quantity' => 'integer',
'weight' => 'decimal:3',
'amount' => 'decimal:2',
'cost_price' => 'decimal:2',
];
/**
* 成本价属商业敏感数据,默认不随 toArray 输出(防止泄漏到小程序端);
* 后台管理接口需在查询结果上调用 makeVisible('cost_price') 恢复。
*/
protected $hidden = ['cost_price'];
/** 明细可编辑状态:待接单、已接单、采购中、配送中(已完成/已取消锁定) */
public const array ITEM_EDITABLE_STATUS = [
StoreOrderModel::STATUS_PENDING,
StoreOrderModel::STATUS_SUMMARIZED,
StoreOrderModel::STATUS_DELIVERING,
StoreOrderModel::STATUS_DISTRIBUTION,
];
/**
* 商品图片ID(逗号分隔字符串 ↔ 数组)
*/
public function imageIds(): Attribute
{
return Attribute::make(
get: fn ($value) => $value === '' || $value === null ? [] : explode(',', (string) $value),
set: fn ($value) => is_array($value) ? implode(',', $value) : $value,
);
}
/**
* 所属订单
*/
public function order(): BelongsTo
{
return $this->belongsTo(StoreOrderModel::class, 'order_id', 'id');
}
/**
* 下单门店
*/
public function store(): BelongsTo
{
return $this->belongsTo(StoreModel::class, 'store_id', 'id');
}
/**
* 下单商品
*/
public function product(): BelongsTo
{
return $this->belongsTo(ProductModel::class, 'product_id', 'id');
}
/**
* 供应商
*/
public function supplier(): BelongsTo
{
return $this->belongsTo(SupplierModel::class, 'supplier_id', 'id');
}
/**
* 快照商品分类
*/
public function category(): BelongsTo
{
return $this->belongsTo(ProductCategoryModel::class, 'category_id', 'id');
}
}