From e735e5a8c54075fdae9b3870af40c80eff8bc8d3 Mon Sep 17 00:00:00 2001 From: sky121113 Date: Thu, 7 May 2026 14:12:42 +0800 Subject: [PATCH] =?UTF-8?q?[REFACTOR]=20=E5=84=AA=E5=8C=96=20B009=20?= =?UTF-8?q?=E5=BA=AB=E5=AD=98=E5=90=8C=E6=AD=A5=E6=AF=94=E5=B0=8D=E9=82=8F?= =?UTF-8?q?=E8=BC=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 優化 MachineService@syncSlots,加入庫存與商品 ID 的實質變動檢查。 2. 解決在庫存與商品皆無變動時,仍會執行資料庫更新並產生冗餘流水帳的問題。 3. 強化商品變更判斷,確保更換商品時能正確記錄流水帳與對應備註。 --- app/Services/Machine/MachineService.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/app/Services/Machine/MachineService.php b/app/Services/Machine/MachineService.php index 9d960ac..722602e 100644 --- a/app/Services/Machine/MachineService.php +++ b/app/Services/Machine/MachineService.php @@ -226,20 +226,27 @@ class MachineService ]; if ($existingSlot) { - // B009 回報:若數值有變化,記錄 adjustment $oldStock = (int) $existingSlot->stock; - $existingSlot->update($updateData); + $oldProductId = $existingSlot->product_id; + + // 檢查是否有實質變動 (Check for actual changes) + $isStockChanged = ($oldStock !== $newStock); + $isProductChanged = ($oldProductId != $actualProductId); // 這裡用鬆散比對以處理 null/0 的情況 - if ($oldStock !== $newStock) { - $delta = $newStock - $oldStock; + if ($isStockChanged || $isProductChanged) { + $existingSlot->update($updateData); $existingSlot->refresh(); + + $delta = $newStock - $oldStock; + $note = $isProductChanged ? "movement.note.product_changed_and_adjusted" : "movement.note.replenishment_correction"; + $this->recordStockMovement( $existingSlot, $delta, MachineStockMovement::TYPE_ADJUSTMENT, null, - "movement.note.replenishment_correction", - ['old' => $oldStock, 'new' => $newStock] + $note, + ['old' => $oldStock, 'new' => $newStock, 'old_product_id' => $oldProductId, 'new_product_id' => $actualProductId] ); } } else {