大更新
This commit is contained in:
@@ -14,6 +14,7 @@ class PurchaseOrderItem extends Model
|
||||
'purchase_order_id',
|
||||
'product_id',
|
||||
'quantity',
|
||||
'unit_id', // 新增單位ID欄位
|
||||
'unit_price',
|
||||
'subtotal',
|
||||
'received_quantity',
|
||||
@@ -26,25 +27,33 @@ class PurchaseOrderItem extends Model
|
||||
'received_quantity' => 'decimal:2',
|
||||
];
|
||||
|
||||
// 移除 $appends 以避免自動附加導致的錯誤
|
||||
// 這些屬性將在 Controller 中需要時手動附加
|
||||
// protected $appends = ['productName', 'unit'];
|
||||
|
||||
public function getProductNameAttribute(): string
|
||||
{
|
||||
return $this->product?->name ?? '';
|
||||
}
|
||||
|
||||
public function getUnitAttribute(): string
|
||||
|
||||
// 關聯單位
|
||||
public function unit(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
{
|
||||
// 優先使用採購單位 > 大單位 > 基本單位
|
||||
// 與 PurchaseOrderController 的邏輯保持一致
|
||||
return $this->belongsTo(Unit::class);
|
||||
}
|
||||
|
||||
public function getUnitNameAttribute(): string
|
||||
{
|
||||
// 優先使用關聯的 unit
|
||||
if ($this->unit) {
|
||||
return $this->unit->name;
|
||||
}
|
||||
|
||||
if (!$this->product) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $this->product->purchase_unit
|
||||
?: ($this->product->large_unit ?: $this->product->base_unit);
|
||||
// Fallback: 嘗試從 Product 的關聯單位獲取
|
||||
return $this->product->purchaseUnit?->name
|
||||
?? $this->product->largeUnit?->name
|
||||
?? $this->product->baseUnit?->name
|
||||
?? '';
|
||||
}
|
||||
|
||||
public function purchaseOrder(): BelongsTo
|
||||
|
||||
Reference in New Issue
Block a user