Reporting a bug (with a ready fix) in the per-dimension Validate Negative Stock check. GitHub issue: Inventory Dimension Validate Negative Stock · Issue #55871 · frappe/erpnext · GitHub
The bug
StockLedgerEntry.validate_inventory_dimension_negative_stock → get_available_qty_after_prev_transaction computes the available balance with a strict posting_datetime < self.posting_datetime filter. Every Stock Ledger Entry produced by a single voucher shares an identical posting_datetime, so the sibling rows of the same voucher are excluded from the available-qty sum — each row is validated only against the balance that existed before the whole voucher.
As a result, a single voucher (e.g. one Delivery Note) with multiple rows against the same item_code + warehouse + inventory dimension — each row within the available balance but collectively exceeding it — passes validation and drives the dimension balance negative, even with Validate Negative Stock enabled on the dimension. The warehouse-level guard does not catch this when stock of the same item exists in other dimension values of the same warehouse.
Reproduce (version-15)
- Create an Inventory Dimension and enable Validate Negative Stock on it.
- Receive 100 units of an item into a warehouse + dimension value.
- In a single Delivery Note, add two rows for that item/warehouse/dimension, e.g. 60 and 60 (each ≤ 100, sum = 120 > 100).
- Submit → succeeds, dimension balance goes to −20. Expected: blocked.
Proposed fix
Use <= instead of < and exclude the current entry by name, so already-posted sibling rows of the same voucher are counted while the current (not-yet-inserted) row is excluded. This mirrors how the warehouse-level engine in stock_ledger.py carries a running balance across sibling rows. Includes a regression test (test_negative_stock_for_multiple_rows_in_single_voucher).
Branch (based on version-15) and mergeable diff:
I am unable to open the PR directly (the repo limits PR creation to collaborators), so sharing here — would a maintainer be willing to review / pull it in? Happy to adjust.