From d0379d615c4c8e91e76ace4c7f728f3ba2d171fb Mon Sep 17 00:00:00 2001 From: colaftc Date: Wed, 4 Feb 2026 15:17:02 +0800 Subject: [PATCH] fix: stock-snapshots api and service calc wrong result --- stock/services.py | 3 +-- stock/tests.py | 2 ++ 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/stock/services.py b/stock/services.py index ca288e1..f949532 100644 --- a/stock/services.py +++ b/stock/services.py @@ -815,7 +815,6 @@ def create_stock_snapshot(stock_change_detail: models.StockChangeDetail, invento raise ValueError("无效的库存变动明细记录") delta = stock_change_detail.quantity if stock_change_detail.stock_change_record.is_incoming else -stock_change_detail.quantity - rolls_delta = 1 if stock_change_detail.stock_change_record.is_incoming else -1 # 必须在库存变动发生后创建快照 snapshot = models.StockSnapshot( @@ -827,7 +826,7 @@ def create_stock_snapshot(stock_change_detail: models.StockChangeDetail, invento quantity_after=inventory.quantity, stock_change_record_id=stock_change_detail.stock_change_record.id, unit=stock_change_detail.unit, - num_of_rolls=inventory.num_of_rolls + rolls_delta, + num_of_rolls=inventory.num_of_rolls, ) snapshot.save() logger.info( diff --git a/stock/tests.py b/stock/tests.py index f2cbfe2..6872808 100644 --- a/stock/tests.py +++ b/stock/tests.py @@ -241,6 +241,7 @@ class CreateStockSnapshotTestCase(StockServicesTestCase): self.assertEqual(snapshot.delta, 100) # 入库数量为正 self.assertEqual(snapshot.quantity_before, 50) # 150 - 100 self.assertEqual(snapshot.quantity_after, 150) + self.assertEqual(snapshot.num_of_rolls, self.inventory.num_of_rolls) self.assertEqual(snapshot.stock_change_record_id, self.stock_change_in.id) # 验证日志调用 @@ -257,6 +258,7 @@ class CreateStockSnapshotTestCase(StockServicesTestCase): self.assertEqual(snapshot.delta, -50) # 出库数量为负 self.assertEqual(snapshot.quantity_before, 200) # 150 - (-50) self.assertEqual(snapshot.quantity_after, 150) + self.assertEqual(snapshot.num_of_rolls, self.inventory.num_of_rolls) def test_create_snapshot_with_invalid_detail(self): """测试使用无效明细记录创建快照"""