feat: add batch-level option to allow negative stock for batch

(cherry picked from commit ca07982ee0)
This commit is contained in:
Rohit Waghchaure
2026-06-18 13:32:46 +05:30
committed by Mergify
parent 7f441864d6
commit 1f075d4bbf
3 changed files with 26 additions and 3 deletions

View File

@@ -19,6 +19,7 @@
"stock_uom",
"expiry_date",
"use_batchwise_valuation",
"allow_negative_stock_for_batch",
"disabled",
"source",
"column_break_9",
@@ -199,6 +200,14 @@
{
"fieldname": "column_break_xrll",
"fieldtype": "Column Break"
},
{
"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",
@@ -206,7 +215,7 @@
"image_field": "image",
"links": [],
"max_attachments": 5,
"modified": "2026-06-16 16:01:26.556324",
"modified": "2026-06-17 16:01:26.556324",
"modified_by": "Administrator",
"module": "Stock",
"name": "Batch",

View File

@@ -94,6 +94,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

View File

@@ -1581,7 +1581,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 = ""
@@ -1592,7 +1592,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 in the 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(
@@ -2188,6 +2188,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