Skip to content

Commit

Permalink
fix: inventory dimension negative stock validation (backport #39149) (#…
Browse files Browse the repository at this point in the history
…39150)

fix: inventory dimension negative stock validation (#39149)

(cherry picked from commit bae7c64)

Co-authored-by: rohitwaghchaure <rohitw1991@gmail.com>
  • Loading branch information
mergify[bot] and rohitwaghchaure authored Jan 4, 2024
1 parent 948045e commit 2866f7c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def get_evaluated_inventory_dimension(doc, sl_dict, parent_doc=None):
dimensions = get_document_wise_inventory_dimensions(doc.doctype)
filter_dimensions = []
for row in dimensions:
if row.type_of_transaction:
if row.type_of_transaction and row.type_of_transaction != "Both":
if (
row.type_of_transaction == "Inward"
if doc.docstatus == 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,14 @@ def test_validate_negative_stock_for_inventory_dimension(self):
)

warehouse = create_warehouse("Negative Stock Warehouse")

doc = make_stock_entry(item_code=item_code, source=warehouse, qty=10, do_not_submit=True)
doc.items[0].inv_site = "Site 1"
self.assertRaises(frappe.ValidationError, doc.submit)
doc.reload()
if doc.docstatus == 1:
doc.cancel()

doc = make_stock_entry(item_code=item_code, target=warehouse, qty=10, do_not_submit=True)

doc.items[0].to_inv_site = "Site 1"
Expand Down
14 changes: 9 additions & 5 deletions erpnext/stock/doctype/stock_ledger_entry/stock_ledger_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,20 @@ def validate_inventory_dimension_negative_stock(self):
"posting_date": self.posting_date,
"posting_time": self.posting_time,
"company": self.company,
"sle": self.name,
}
)

sle = get_previous_sle(kwargs, extra_cond=extra_cond)
qty_after_transaction = 0.0
flt_precision = cint(frappe.db.get_default("float_precision")) or 2
if sle:
flt_precision = cint(frappe.db.get_default("float_precision")) or 2
diff = sle.qty_after_transaction + flt(self.actual_qty)
diff = flt(diff, flt_precision)
if diff < 0 and abs(diff) > 0.0001:
self.throw_validation_error(diff, dimensions)
qty_after_transaction = sle.qty_after_transaction

diff = qty_after_transaction + flt(self.actual_qty)
diff = flt(diff, flt_precision)
if diff < 0 and abs(diff) > 0.0001:
self.throw_validation_error(diff, dimensions)

def throw_validation_error(self, diff, dimensions):
dimension_msg = _(", with the inventory {0}: {1}").format(
Expand Down

0 comments on commit 2866f7c

Please sign in to comment.