Files
star-erp/app/Models/PurchaseOrderItem.php
sky121113 fad74df6ac
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 52s
更新採購單跟商品資料一些bug
2026-01-06 15:45:13 +08:00

60 lines
1.5 KiB
PHP

<?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'];
public function getProductNameAttribute(): string
{
return $this->product?->name ?? '';
}
public function getUnitAttribute(): string
{
// 優先使用採購單位 > 大單位 > 基本單位
// 與 PurchaseOrderController 的邏輯保持一致
if (!$this->product) {
return '';
}
return $this->product->purchase_unit
?: ($this->product->large_unit ?: $this->product->base_unit);
}
public function purchaseOrder(): BelongsTo
{
return $this->belongsTo(PurchaseOrder::class);
}
public function product(): BelongsTo
{
return $this->belongsTo(Product::class);
}
}