Skip to content

Commit e46e874

Browse files
rohitwaghchauremergify[bot]
authored andcommitted
fix: better validation for negative batch
(cherry picked from commit a8636e4)
1 parent 4a4e995 commit e46e874

2 files changed

Lines changed: 27 additions & 13 deletions

File tree

erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
cint,
1818
cstr,
1919
flt,
20+
format_datetime,
2021
get_datetime,
2122
get_link_to_form,
2223
getdate,
@@ -1459,22 +1460,33 @@ def validate_batch_quantity(self):
14591460
if flt(available_qty, precision) < 0:
14601461
self.throw_negative_batch(d.batch_no, available_qty, precision)
14611462

1462-
def throw_negative_batch(self, batch_no, available_qty, precision):
1463+
def throw_negative_batch(self, batch_no, available_qty, precision, posting_datetime=None):
14631464
from erpnext.stock.stock_ledger import NegativeStockError
14641465

14651466
if frappe.db.get_single_value("Stock Settings", "allow_negative_stock_for_batch"):
14661467
return
14671468

1469+
date_msg = ""
1470+
if posting_datetime:
1471+
date_msg = " " + _("as of {0}").format(format_datetime(posting_datetime))
1472+
1473+
msg = _(
1474+
"""
1475+
The Batch {0} of an item {1} has negative stock in the warehouse {2}{3}.
1476+
Please add a stock quantity of {4} to proceed with this entry.
1477+
If it is not possible to make an adjustment entry, please enable 'Allow Negative Stock for Batch' in Stock Settings to proceed.
1478+
However, enabling this setting may lead to negative stock in the system.
1479+
So please ensure the stock levels are adjusted as soon as possible to maintain the correct valuation rate."""
1480+
).format(
1481+
bold(batch_no),
1482+
bold(self.item_code),
1483+
bold(self.warehouse),
1484+
date_msg,
1485+
bold(abs(flt(available_qty, precision))),
1486+
)
1487+
14681488
frappe.throw(
1469-
_(
1470-
"""
1471-
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."""
1472-
).format(
1473-
bold(batch_no),
1474-
bold(self.item_code),
1475-
bold(self.warehouse),
1476-
bold(abs(flt(available_qty, precision))),
1477-
),
1489+
msg,
14781490
title=_("Negative Stock Error"),
14791491
exc=NegativeStockError,
14801492
)
@@ -1497,7 +1509,9 @@ def get_batchwise_available_qty(self):
14971509
available_qty[row.batch_no] = flt(row.qty)
14981510

14991511
if flt(available_qty[row.batch_no], precision) < 0:
1500-
self.throw_negative_batch(row.batch_no, available_qty[row.batch_no], precision)
1512+
self.throw_negative_batch(
1513+
row.batch_no, available_qty[row.batch_no], precision, row.posting_datetime
1514+
)
15011515

15021516
return available_qty
15031517

erpnext/stock/doctype/stock_settings/stock_settings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@
550550
},
551551
{
552552
"default": "0",
553-
"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.",
553+
"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.",
554554
"fieldname": "allow_negative_stock_for_batch",
555555
"fieldtype": "Check",
556556
"label": "Allow Negative Stock for Batch"
@@ -562,7 +562,7 @@
562562
"index_web_pages_for_search": 1,
563563
"issingle": 1,
564564
"links": [],
565-
"modified": "2026-02-09 15:01:12.466175",
565+
"modified": "2026-02-16 10:36:59.921491",
566566
"modified_by": "Administrator",
567567
"module": "Stock",
568568
"name": "Stock Settings",

0 commit comments

Comments
 (0)