diff --git a/erpnext/stock/stock_ledger.py b/erpnext/stock/stock_ledger.py
index 2864c00864b..8df785a0c1f 100644
--- a/erpnext/stock/stock_ledger.py
+++ b/erpnext/stock/stock_ledger.py
@@ -4,10 +4,10 @@
import json
import frappe
-from frappe import _
+from frappe import _, bold
from frappe.model.meta import get_field_precision
from frappe.query_builder.functions import Sum
-from frappe.utils import cint, cstr, flt, get_link_to_form, getdate, now, nowdate
+from frappe.utils import cint, cstr, flt, format_date, get_link_to_form, getdate, now, nowdate
import erpnext
from erpnext.stock.doctype.bin.bin import update_qty as update_bin_qty
@@ -542,11 +542,31 @@ class update_entries_after:
return self.distinct_item_warehouses[key].dependent_voucher_detail_nos
+ def validate_previous_sle_qty(self, sle):
+ previous_sle = self.data[sle.warehouse].previous_sle
+ if previous_sle and previous_sle.get("qty_after_transaction") < 0 and sle.get("actual_qty") > 0:
+ frappe.msgprint(
+ _(
+ "The stock for the item {0} in the {1} warehouse was negative on the {2}. You should create a positive entry {3} before the date {4} and time {5} to post the correct valuation rate. For more details, please read the documentation."
+ ).format(
+ bold(sle.item_code),
+ bold(sle.warehouse),
+ bold(format_date(previous_sle.posting_date)),
+ sle.voucher_no,
+ bold(format_date(previous_sle.posting_date)),
+ bold(previous_sle.posting_time),
+ ),
+ title=_("Warning on Negative Stock"),
+ indicator="blue",
+ )
+
def process_sle(self, sle):
from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos
# previous sle data for this warehouse
self.wh_data = self.data[sle.warehouse]
+
+ self.validate_previous_sle_qty(sle)
self.affected_transactions.add((sle.voucher_type, sle.voucher_no))
if (sle.serial_no and not self.via_landed_cost_voucher) or not cint(self.allow_negative_stock):