diff --git a/inventory-backend/app/services/trans_service.py b/inventory-backend/app/services/trans_service.py index 6b5afee..1f10c57 100644 --- a/inventory-backend/app/services/trans_service.py +++ b/inventory-backend/app/services/trans_service.py @@ -381,10 +381,14 @@ class TransService: if ModelClass: stock = ModelClass.query.with_for_update().get(record.stock_id) if stock: - stock.stock_quantity = float(stock.stock_quantity or 0) - pending_qty - # 防止负库存 - if stock.stock_quantity < 0: - stock.stock_quantity = 0 + stock_qty = float(stock.stock_quantity or 0) + # 不足则报错整单回滚:直接夹到 0 会让 available_quantity > stock_quantity, + # 凭空多出可用库存 + if pending_qty > stock_qty: + raise ValueError( + f"SKU {record.sku} 实物库存不足(剩 {stock_qty}),无法报废 {pending_qty}" + ) + stock.stock_quantity = stock_qty - pending_qty # 3. 创建报废记录 scrap_record = TransScrap(