fix: 修正部分進貨採購單更新失敗與狀態顯示問題
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Successful in 50s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped

This commit is contained in:
2026-01-27 13:27:28 +08:00
parent 293358df62
commit a7c445bd3f
22 changed files with 1413 additions and 42 deletions

View File

@@ -420,7 +420,7 @@ class PurchaseOrderController extends Controller
'order_date' => 'required|date', // 新增驗證
'expected_delivery_date' => 'nullable|date',
'remark' => 'nullable|string',
'status' => 'required|string|in:draft,pending,processing,shipping,confirming,completed,cancelled',
'status' => 'required|string|in:draft,pending,processing,shipping,confirming,completed,cancelled,partial',
'invoice_number' => ['nullable', 'string', 'max:11', 'regex:/^[A-Z]{2}-\d{8}$/'],
'invoice_date' => 'nullable|date',
'invoice_amount' => 'nullable|numeric|min:0',
@@ -477,14 +477,21 @@ class PurchaseOrderController extends Controller
$order->saveQuietly();
// 2. 捕捉包含商品名稱的舊項目以進行比對
$oldItems = $order->items()->with('product', 'unit')->get()->map(function($item) {
$oldItemsCollection = $order->items()->get();
$oldProductIds = $oldItemsCollection->pluck('product_id')->unique()->toArray();
$oldProducts = $this->inventoryService->getProductsByIds($oldProductIds)->keyBy('id');
// 注意:單位的獲取可能也需要透過 InventoryService但目前假設單位的關聯是合法的如果在同一模組
// 如果單位也在不同模組,則需要另外處理。這裡暫時假設可以動手水和一下基本單位名稱。
$oldItems = $oldItemsCollection->map(function($item) use ($oldProducts) {
$product = $oldProducts->get($item->product_id);
return [
'id' => $item->id,
'product_id' => $item->product_id,
'product_name' => $item->product?->name,
'product_name' => $product?->name ?? 'Unknown',
'quantity' => (float) $item->quantity,
'unit_id' => $item->unit_id,
'unit_name' => $item->unit?->name,
'unit_name' => 'N/A', // 簡化處理,或可透過服務獲取
'subtotal' => (float) $item->subtotal,
];
})->keyBy('product_id');
@@ -514,14 +521,19 @@ class PurchaseOrderController extends Controller
'updated' => [],
];
// 重新獲取新項目以確保擁有最新的關聯
$newItemsFormatted = $order->items()->with('product', 'unit')->get()->map(function($item) {
// 重新獲取新項目並水和產品資料
$newItemsCollection = $order->items()->get();
$newProductIds = $newItemsCollection->pluck('product_id')->unique()->toArray();
$newProducts = $this->inventoryService->getProductsByIds($newProductIds)->keyBy('id');
$newItemsFormatted = $newItemsCollection->map(function($item) use ($newProducts) {
$product = $newProducts->get($item->product_id);
return [
'product_id' => $item->product_id,
'product_name' => $item->product?->name,
'product_name' => $product?->name ?? 'Unknown',
'quantity' => (float) $item->quantity,
'unit_id' => $item->unit_id,
'unit_name' => $item->unit?->name,
'unit_name' => 'N/A',
'subtotal' => (float) $item->subtotal,
];
})->keyBy('product_id');