feat(inventory): 強化調撥單功能,支援販賣機貨道欄位、開放商品重複加入及優化過帳庫存檢核
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Successful in 59s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped

This commit is contained in:
2026-02-09 16:52:35 +08:00
parent 65eb1a1b64
commit 613eb555ba
10 changed files with 745 additions and 175 deletions

View File

@@ -108,6 +108,7 @@ class TransferOrderController extends Controller
'from_warehouse_name' => $order->fromWarehouse->name,
'to_warehouse_id' => (string) $order->to_warehouse_id,
'to_warehouse_name' => $order->toWarehouse->name,
'to_warehouse_type' => $order->toWarehouse->type->value, // 用於判斷是否為販賣機
'status' => $order->status,
'remarks' => $order->remarks,
'created_at' => $order->created_at->format('Y-m-d H:i'),
@@ -128,6 +129,7 @@ class TransferOrderController extends Controller
'expiry_date' => $stock && $stock->expiry_date ? $stock->expiry_date->format('Y-m-d') : null,
'unit' => $item->product->baseUnit?->name,
'quantity' => (float) $item->quantity,
'position' => $item->position,
'max_quantity' => $item->snapshot_quantity ? (float) $item->snapshot_quantity : ($stock ? (float) $stock->quantity : 0.0),
'notes' => $item->notes,
];
@@ -145,31 +147,32 @@ class TransferOrderController extends Controller
return redirect()->back()->with('error', '只能修改草稿狀態的單據');
}
$validated = $request->validate([
'items' => 'array',
'items.*.product_id' => 'required|exists:products,id',
'items.*.quantity' => 'required|numeric|min:0.01',
'items.*.batch_number' => 'nullable|string',
'items.*.notes' => 'nullable|string',
'remarks' => 'nullable|string',
]);
// 1. 先更新資料
// 1. 先更新資料 (如果請求中包含 items則先執行儲存)
$itemsChanged = false;
if ($request->has('items')) {
$validated = $request->validate([
'items' => 'array',
'items.*.product_id' => 'required|exists:products,id',
'items.*.quantity' => 'required|numeric|min:0.01',
'items.*.batch_number' => 'nullable|string',
'items.*.position' => 'nullable|string',
'items.*.notes' => 'nullable|string',
]);
$itemsChanged = $this->transferService->updateItems($order, $validated['items']);
}
$remarksChanged = $order->remarks !== ($validated['remarks'] ?? null);
$remarksChanged = false;
if ($request->has('remarks')) {
$remarksChanged = $order->remarks !== $request->input('remarks');
$order->remarks = $request->input('remarks');
}
if ($itemsChanged || $remarksChanged) {
$order->remarks = $validated['remarks'] ?? null;
// [IMPORTANT] 使用 touch() 確保即便只有品項異動,也會因為 updated_at 變更而觸發自動日誌
$order->touch();
$message = '儲存成功';
} else {
$message = '資料未變更';
// 如果沒變更,就不執行 touch(),也不會產生 Activity Log
}
// 2. 判斷是否需要過帳
@@ -178,8 +181,10 @@ class TransferOrderController extends Controller
$this->transferService->post($order, auth()->id());
return redirect()->route('inventory.transfer.index')
->with('success', '調撥單已過帳完成');
} catch (ValidationException $e) {
return redirect()->back()->withErrors($e->errors());
} catch (\Exception $e) {
return redirect()->back()->withErrors(['items' => $e->getMessage()]);
return redirect()->back()->withErrors(['items' => $e->getMessage()]);
}
}
@@ -224,4 +229,30 @@ class TransferOrderController extends Controller
return response()->json($inventories);
}
public function importItems(Request $request, InventoryTransferOrder $order)
{
if ($order->status !== 'draft') {
return redirect()->back()->with('error', '只能在草稿狀態下匯入明細');
}
$request->validate([
'file' => 'required|file|mimes:xlsx,xls,csv',
]);
try {
\Maatwebsite\Excel\Facades\Excel::import(new \App\Modules\Inventory\Imports\InventoryTransferItemImport($order), $request->file('file'));
return redirect()->back()->with('success', '匯入成功');
} catch (\Exception $e) {
return redirect()->back()->with('error', '匯入失敗:' . $e->getMessage());
}
}
public function template()
{
return \Maatwebsite\Excel\Facades\Excel::download(
new \App\Modules\Inventory\Exports\InventoryTransferTemplateExport(),
'調撥單明細匯入範本.xlsx'
);
}
}