From 5c4f19ebdc10ca97337babf3c366d44d44931d33 Mon Sep 17 00:00:00 2001 From: Rohit Waghchaure Date: Thu, 18 Jun 2026 13:35:33 +0530 Subject: [PATCH] feat: add batch-level option to allow negative stock for batch --- erpnext/stock/doctype/batch/batch.json | 11 ++++++++++- erpnext/stock/doctype/batch/batch.py | 1 + .../serial_and_batch_bundle.py | 17 +++++++++++++++-- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/erpnext/stock/doctype/batch/batch.json b/erpnext/stock/doctype/batch/batch.json index e318f113713..00541250531 100644 --- a/erpnext/stock/doctype/batch/batch.json +++ b/erpnext/stock/doctype/batch/batch.json @@ -11,6 +11,7 @@ "disabled", "column_break_24", "use_batchwise_valuation", + "allow_negative_stock_for_batch", "sb_batch", "batch_id", "item", @@ -202,6 +203,14 @@ "label": "Use Batch-wise Valuation", "read_only": 1, "set_only_once": 1 + }, + { + "default": "0", + "description": "If enabled, the system will allow negative stock entries for this batch, overriding the 'Allow negative stock for Batch' setting in Stock Settings. This may lead to incorrect valuation rates, so it is recommended to avoid using this option.", + "fieldname": "allow_negative_stock_for_batch", + "fieldtype": "Check", + "label": "Allow Negative Stock for Batch", + "no_copy": 1 } ], "icon": "fa fa-archive", @@ -209,7 +218,7 @@ "image_field": "image", "links": [], "max_attachments": 5, - "modified": "2026-06-16 16:01:26.556324", + "modified": "2026-06-17 12:17:28.339975", "modified_by": "Administrator", "module": "Stock", "name": "Batch", diff --git a/erpnext/stock/doctype/batch/batch.py b/erpnext/stock/doctype/batch/batch.py index 7f31c65dfcf..b3a85b0bdd6 100644 --- a/erpnext/stock/doctype/batch/batch.py +++ b/erpnext/stock/doctype/batch/batch.py @@ -95,6 +95,7 @@ class Batch(Document): if TYPE_CHECKING: from frappe.types import DF + allow_negative_stock_for_batch: DF.Check batch_id: DF.Data batch_qty: DF.Float description: DF.SmallText | None 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 ce185e40a31..f8facea5f78 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 @@ -1508,7 +1508,7 @@ class SerialandBatchBundle(Document): 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"): + if allow_negative_stock_for_batch(batch_no): return date_msg = "" @@ -1519,7 +1519,7 @@ class SerialandBatchBundle(Document): """ 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. + If it is not possible to make an adjustment entry, please enable 'Allow Negative Stock for Batch' in the batch {0} or 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( @@ -2128,6 +2128,19 @@ def combine_datetime(date, time=None): return get_combine_datetime(date, time) +def allow_negative_stock_for_batch(batch_no): + """Return whether negative stock is allowed for the given batch. + + The batch-level setting takes priority: if `allow_negative_stock_for_batch` + is enabled on the Batch, negative stock is allowed regardless of Stock Settings. + Otherwise, fall back to the `allow_negative_stock_for_batch` Stock Setting. + """ + if batch_no and frappe.db.get_value("Batch", batch_no, "allow_negative_stock_for_batch"): + return True + + return bool(frappe.db.get_single_value("Stock Settings", "allow_negative_stock_for_batch")) + + def get_batch(item_code): from erpnext.stock.doctype.batch.batch import make_batch