Files
star-erp/app/Models/PurchaseOrderItem.php

60 lines
1.5 KiB
PHP
Raw Normal View History

2025-12-30 15:03:19 +08:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class PurchaseOrderItem extends Model
{
use HasFactory;
protected $fillable = [
'purchase_order_id',
'product_id',
'quantity',
'unit_price',
'subtotal',
'received_quantity',
];
protected $casts = [
'quantity' => 'decimal:2',
'unit_price' => 'decimal:2',
'subtotal' => 'decimal:2',
'received_quantity' => 'decimal:2',
];
// 移除 $appends 以避免自動附加導致的錯誤
// 這些屬性將在 Controller 中需要時手動附加
// protected $appends = ['productName', 'unit'];
2025-12-30 15:03:19 +08:00
public function getProductNameAttribute(): string
{
return $this->product?->name ?? '';
2025-12-30 15:03:19 +08:00
}
public function getUnitAttribute(): string
{
// 優先使用採購單位 > 大單位 > 基本單位
// 與 PurchaseOrderController 的邏輯保持一致
if (!$this->product) {
return '';
}
return $this->product->purchase_unit
?: ($this->product->large_unit ?: $this->product->base_unit);
2025-12-30 15:03:19 +08:00
}
public function purchaseOrder(): BelongsTo
{
return $this->belongsTo(PurchaseOrder::class);
}
public function product(): BelongsTo
{
return $this->belongsTo(Product::class);
}
}