feat(inventory): 實作撥補單建立即自動過帳邏輯並修正參數對齊問題
This commit is contained in:
@@ -55,19 +55,50 @@ class TransferOrderController extends Controller
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
// 兼容前端不同的參數命名 (from/source, to/target)
|
||||
$fromId = $request->input('from_warehouse_id') ?? $request->input('sourceWarehouseId');
|
||||
$toId = $request->input('to_warehouse_id') ?? $request->input('targetWarehouseId');
|
||||
|
||||
$validated = $request->validate([
|
||||
'from_warehouse_id' => 'required|exists:warehouses,id',
|
||||
'to_warehouse_id' => 'required|exists:warehouses,id|different:from_warehouse_id',
|
||||
'from_warehouse_id' => 'required_without:sourceWarehouseId|exists:warehouses,id',
|
||||
'to_warehouse_id' => 'required_without:targetWarehouseId|exists:warehouses,id|different:from_warehouse_id',
|
||||
'remarks' => 'nullable|string',
|
||||
'notes' => 'nullable|string',
|
||||
'instant_post' => 'boolean',
|
||||
// 支援單筆商品直接建立 (撥補單模式)
|
||||
'product_id' => 'nullable|exists:products,id',
|
||||
'quantity' => 'nullable|numeric|min:0.01',
|
||||
'batch_number' => 'nullable|string',
|
||||
]);
|
||||
|
||||
$remarks = $validated['remarks'] ?? $validated['notes'] ?? null;
|
||||
$order = $this->transferService->createOrder(
|
||||
$validated['from_warehouse_id'],
|
||||
$validated['to_warehouse_id'],
|
||||
$validated['remarks'] ?? null,
|
||||
$fromId,
|
||||
$toId,
|
||||
$remarks,
|
||||
auth()->id()
|
||||
);
|
||||
|
||||
// 如果請求包含單筆商品資訊
|
||||
if ($request->has('product_id')) {
|
||||
$this->transferService->updateItems($order, [[
|
||||
'product_id' => $validated['product_id'],
|
||||
'quantity' => $validated['quantity'],
|
||||
'batch_number' => $validated['batch_number'] ?? null,
|
||||
]]);
|
||||
}
|
||||
|
||||
// 如果是撥補單,執行直接過帳
|
||||
if ($request->input('instant_post') === true) {
|
||||
try {
|
||||
$this->transferService->post($order, auth()->id());
|
||||
return redirect()->back()->with('success', '撥補成功,庫存已更新');
|
||||
} catch (\Exception $e) {
|
||||
// 如果過帳失敗,雖然單據已建立,但應回報錯誤
|
||||
return redirect()->back()->withErrors(['items' => $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
return redirect()->route('inventory.transfer.show', [$order->id])
|
||||
->with('success', '已建立調撥單');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user