feat: 優化庫存調撥單操作紀錄與 UI 佈局
This commit is contained in:
@@ -82,50 +82,9 @@ class TransferOrderController extends Controller
|
||||
auth()->id()
|
||||
);
|
||||
|
||||
// 記錄活動
|
||||
activity()
|
||||
->performedOn($order)
|
||||
->causedBy(auth()->user())
|
||||
->event('created')
|
||||
->withProperties([
|
||||
'attributes' => $order->toArray(),
|
||||
'snapshot' => [
|
||||
'doc_no' => $order->doc_no,
|
||||
'from_warehouse_name' => $order->fromWarehouse?->name,
|
||||
'to_warehouse_name' => $order->toWarehouse?->name,
|
||||
]
|
||||
])
|
||||
->log('created');
|
||||
|
||||
// 如果請求包含單筆商品資訊
|
||||
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());
|
||||
|
||||
// 記錄過帳活動
|
||||
activity()
|
||||
->performedOn($order)
|
||||
->causedBy(auth()->user())
|
||||
->event('posted')
|
||||
->withProperties([
|
||||
'attributes' => ['status' => 'posted'],
|
||||
'old' => ['status' => 'draft'],
|
||||
'snapshot' => [
|
||||
'doc_no' => $order->doc_no,
|
||||
'from_warehouse_name' => $order->fromWarehouse?->name,
|
||||
'to_warehouse_name' => $order->toWarehouse?->name,
|
||||
]
|
||||
])
|
||||
->log('posted');
|
||||
|
||||
return redirect()->back()->with('success', '撥補成功,庫存已更新');
|
||||
} catch (\Exception $e) {
|
||||
@@ -185,60 +144,35 @@ class TransferOrderController extends Controller
|
||||
return redirect()->back()->with('error', '只能修改草稿狀態的單據');
|
||||
}
|
||||
|
||||
if ($request->input('action') === 'post') {
|
||||
try {
|
||||
$this->transferService->post($order, auth()->id());
|
||||
|
||||
// 記錄活動
|
||||
activity()
|
||||
->performedOn($order)
|
||||
->causedBy(auth()->user())
|
||||
->event('posted')
|
||||
->withProperties([
|
||||
'attributes' => ['status' => 'posted'],
|
||||
'old' => ['status' => 'draft'],
|
||||
'snapshot' => [
|
||||
'doc_no' => $order->doc_no,
|
||||
'from_warehouse_name' => $order->fromWarehouse?->name,
|
||||
'to_warehouse_name' => $order->toWarehouse?->name,
|
||||
]
|
||||
])
|
||||
->log('posted');
|
||||
|
||||
return redirect()->route('inventory.transfer.index')
|
||||
->with('success', '調撥單已過帳完成');
|
||||
} catch (\Exception $e) {
|
||||
return redirect()->back()->withErrors(['items' => $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
$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. 先更新資料
|
||||
if ($request->has('items')) {
|
||||
$this->transferService->updateItems($order, $validated['items']);
|
||||
}
|
||||
|
||||
$order->update($request->only(['remarks']));
|
||||
$order->fill($request->only(['remarks']));
|
||||
|
||||
// [IMPORTANT] 使用 touch() 確保即便只有品項異動,也會因為 updated_at 變更而觸發自動日誌
|
||||
$order->touch();
|
||||
|
||||
// 記錄暫存活動
|
||||
activity()
|
||||
->performedOn($order)
|
||||
->causedBy(auth()->user())
|
||||
->event('updated')
|
||||
->withProperties([
|
||||
'snapshot' => [
|
||||
'doc_no' => $order->doc_no,
|
||||
'from_warehouse_name' => $order->fromWarehouse?->name,
|
||||
'to_warehouse_name' => $order->toWarehouse?->name,
|
||||
]
|
||||
])
|
||||
->log('updated_items');
|
||||
// 2. 判斷是否需要過帳
|
||||
if ($request->input('action') === 'post') {
|
||||
try {
|
||||
$this->transferService->post($order, auth()->id());
|
||||
return redirect()->route('inventory.transfer.index')
|
||||
->with('success', '調撥單已過帳完成');
|
||||
} catch (\Exception $e) {
|
||||
return redirect()->back()->withErrors(['items' => $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
return redirect()->back()->with('success', '儲存成功');
|
||||
}
|
||||
@@ -249,20 +183,6 @@ class TransferOrderController extends Controller
|
||||
return redirect()->back()->with('error', '只能刪除草稿狀態的單據');
|
||||
}
|
||||
|
||||
// 記錄活動
|
||||
activity()
|
||||
->performedOn($order)
|
||||
->causedBy(auth()->user())
|
||||
->event('deleted')
|
||||
->withProperties([
|
||||
'snapshot' => [
|
||||
'doc_no' => $order->doc_no,
|
||||
'from_warehouse_name' => $order->fromWarehouse?->name,
|
||||
'to_warehouse_name' => $order->toWarehouse?->name,
|
||||
]
|
||||
])
|
||||
->log('deleted');
|
||||
|
||||
$order->items()->delete();
|
||||
$order->delete();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user