Compare commits

...

2 Commits

Author SHA1 Message Date
016366407c [FIX] 修正生產工單完成入庫時未寫入成本與總價值的問題
Some checks failed
ERP-Deploy-Demo / deploy-demo (push) Successful in 55s
ERP-Deploy-Production / deploy-production (push) Has been cancelled
2026-03-05 13:34:02 +08:00
ba50905626 [FIX] 修正公共事業費清單日期顯示少一天的問題 2026-03-05 11:58:32 +08:00
2 changed files with 17 additions and 1 deletions

View File

@@ -19,7 +19,7 @@ class UtilityFee extends Model
];
protected $casts = [
'transaction_date' => 'date',
'transaction_date' => 'date:Y-m-d',
'amount' => 'decimal:2',
];

View File

@@ -436,6 +436,21 @@ class ProductionOrderController extends Controller
throw new \Exception('必須提供成品批號');
}
// --- 新增:計算原物料投入總成本 ---
$totalCost = 0;
$items = $productionOrder->items()->with('inventory')->get();
foreach ($items as $item) {
if ($item->inventory) {
$totalCost += ($item->quantity_used * ($item->inventory->unit_cost ?? 0));
}
}
// 計算單位成本 (若產出數量為 0 則設為 0 避免除以零錯誤)
$unitCost = $productionOrder->output_quantity > 0
? $totalCost / $productionOrder->output_quantity
: 0;
// --------------------------------
// 更新單據資訊:批號、效期與自動記錄生產日期
$productionOrder->output_batch_number = $batchNumber;
$productionOrder->expiry_date = $expiryDate;
@@ -446,6 +461,7 @@ class ProductionOrderController extends Controller
'warehouse_id' => $warehouseId,
'product_id' => $productionOrder->product_id,
'quantity' => $productionOrder->output_quantity,
'unit_cost' => $unitCost, // 傳入計算後的單位成本
'batch_number' => $batchNumber,
'box_number' => $productionOrder->output_box_count,
'arrival_date' => now()->toDateString(),