feat(inventory): 強化調撥單功能,支援販賣機貨道欄位、開放商品重複加入及優化過帳庫存檢核
This commit is contained in:
@@ -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'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user