優化門市叫貨流程:實作庫存預扣機制、鎖定自動產生的調撥單明細、修復自動販賣機貨道數量連動 Bug 及狀態同步問題
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 56s

This commit is contained in:
2026-02-25 17:32:28 +08:00
parent e3df090afd
commit 63e4f88a14
8 changed files with 469 additions and 161 deletions

View File

@@ -45,6 +45,17 @@ class TransferService
return [$key => $item];
});
// 釋放舊明細的預扣庫存
foreach ($order->items as $item) {
$inv = Inventory::where('warehouse_id', $order->from_warehouse_id)
->where('product_id', $item->product_id)
->where('batch_number', $item->batch_number)
->first();
if ($inv) {
$inv->releaseReservedQuantity($item->quantity);
}
}
$diff = [
'added' => [],
'removed' => [],
@@ -67,6 +78,21 @@ class TransferService
]);
$item->load('product');
// 增加新明細的預扣庫存
$inv = Inventory::firstOrCreate(
[
'warehouse_id' => $order->from_warehouse_id,
'product_id' => $item->product_id,
'batch_number' => $item->batch_number,
],
[
'quantity' => 0,
'unit_cost' => 0,
'total_value' => 0,
]
);
$inv->reserveQuantity($item->quantity);
if ($oldItemsMap->has($key)) {
$oldItem = $oldItemsMap->get($key);
if ((float)$oldItem->quantity !== (float)$data['quantity'] ||
@@ -163,6 +189,9 @@ class TransferService
$oldSourceQty = $sourceInventory->quantity;
$newSourceQty = $oldSourceQty - $item->quantity;
// 釋放草稿階段預扣的庫存
$sourceInventory->reserved_quantity = max(0, $sourceInventory->reserved_quantity - $item->quantity);
$item->update(['snapshot_quantity' => $oldSourceQty]);
$sourceInventory->quantity = $newSourceQty;
@@ -346,9 +375,22 @@ class TransferService
if ($order->status !== 'draft') {
throw new \Exception('只能作廢草稿狀態的單據');
}
$order->update([
'status' => 'voided',
'updated_by' => $userId
]);
DB::transaction(function () use ($order, $userId) {
foreach ($order->items as $item) {
$inv = Inventory::where('warehouse_id', $order->from_warehouse_id)
->where('product_id', $item->product_id)
->where('batch_number', $item->batch_number)
->first();
if ($inv) {
$inv->releaseReservedQuantity($item->quantity);
}
}
$order->update([
'status' => 'voided',
'updated_by' => $userId
]);
});
}
}