Merge pull request #56080 from rohitwaghchaure/allow-negative-stock-for-batch-v15

feat: allow negative stock at batch level (backport to v15)
This commit is contained in:
rohitwaghchaure
2026-06-18 14:31:21 +05:30
committed by GitHub
3 changed files with 26 additions and 3 deletions

View File

@@ -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",

View File

@@ -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

View File

@@ -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