From e46e8741b44c6ade4d2ae0f116657486cb05c324 Mon Sep 17 00:00:00 2001 From: Rohit Waghchaure Date: Mon, 16 Feb 2026 10:38:15 +0530 Subject: [PATCH] fix: better validation for negative batch (cherry picked from commit a8636e4f59d60759e3e988c4f2817c206663886e) --- .../serial_and_batch_bundle.py | 36 +++++++++++++------ .../stock_settings/stock_settings.json | 4 +-- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py b/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py index 6376f6b9f4e..2229e934491 100644 --- a/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py +++ b/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py @@ -17,6 +17,7 @@ from frappe.utils import ( cint, cstr, flt, + format_datetime, get_datetime, get_link_to_form, getdate, @@ -1459,22 +1460,33 @@ class SerialandBatchBundle(Document): if flt(available_qty, precision) < 0: self.throw_negative_batch(d.batch_no, available_qty, precision) - def throw_negative_batch(self, batch_no, available_qty, precision): + def throw_negative_batch(self, batch_no, available_qty, precision, posting_datetime=None): from erpnext.stock.stock_ledger import NegativeStockError if frappe.db.get_single_value("Stock Settings", "allow_negative_stock_for_batch"): return + date_msg = "" + if posting_datetime: + date_msg = " " + _("as of {0}").format(format_datetime(posting_datetime)) + + msg = _( + """ + The Batch {0} of an item {1} has negative stock in the warehouse {2}{3}. + Please add a stock quantity of {4} to proceed with this entry. + If it is not possible to make an adjustment entry, please enable 'Allow Negative Stock for Batch' in Stock Settings to proceed. + However, enabling this setting may lead to negative stock in the system. + So please ensure the stock levels are adjusted as soon as possible to maintain the correct valuation rate.""" + ).format( + bold(batch_no), + bold(self.item_code), + bold(self.warehouse), + date_msg, + bold(abs(flt(available_qty, precision))), + ) + frappe.throw( - _( - """ - The Batch {0} of an item {1} has negative stock in the warehouse {2}. Please add a stock quantity of {3} to proceed with this entry.""" - ).format( - bold(batch_no), - bold(self.item_code), - bold(self.warehouse), - bold(abs(flt(available_qty, precision))), - ), + msg, title=_("Negative Stock Error"), exc=NegativeStockError, ) @@ -1497,7 +1509,9 @@ class SerialandBatchBundle(Document): available_qty[row.batch_no] = flt(row.qty) if flt(available_qty[row.batch_no], precision) < 0: - self.throw_negative_batch(row.batch_no, available_qty[row.batch_no], precision) + self.throw_negative_batch( + row.batch_no, available_qty[row.batch_no], precision, row.posting_datetime + ) return available_qty diff --git a/erpnext/stock/doctype/stock_settings/stock_settings.json b/erpnext/stock/doctype/stock_settings/stock_settings.json index e3851b5b41f..a9f6d1904a3 100644 --- a/erpnext/stock/doctype/stock_settings/stock_settings.json +++ b/erpnext/stock/doctype/stock_settings/stock_settings.json @@ -550,7 +550,7 @@ }, { "default": "0", - "description": "If enabled, the system will allow negative stock entries for the batch, but this could calculate the valuation rate incorrectly, so avoid using this option.", + "description": "If enabled, the system will allow negative stock entries for the batch. But, this may lead to incorrect valuation rates, so it is recommended to avoid using this option. The system will permit negative stock only when it is caused by backdated entries and will validate and block negative stock in all other cases.", "fieldname": "allow_negative_stock_for_batch", "fieldtype": "Check", "label": "Allow Negative Stock for Batch" @@ -562,7 +562,7 @@ "index_web_pages_for_search": 1, "issingle": 1, "links": [], - "modified": "2026-02-09 15:01:12.466175", + "modified": "2026-02-16 10:36:59.921491", "modified_by": "Administrator", "module": "Stock", "name": "Stock Settings",