fix: incorrect stock after merging the items (#22526)
* fix: incorrect stock after merging items
* fix: Readability fix
Co-authored-by: Marica <maricadsouza221197@gmail.com>
diff --git a/erpnext/stock/doctype/item/item.py b/erpnext/stock/doctype/item/item.py
index a75ee67..d5f479f 100644
--- a/erpnext/stock/doctype/item/item.py
+++ b/erpnext/stock/doctype/item/item.py
@@ -13,7 +13,7 @@
from erpnext.setup.doctype.item_group.item_group import (get_parent_item_groups, invalidate_cache_for)
from frappe import _, msgprint
from frappe.utils import (cint, cstr, flt, formatdate, get_timestamp, getdate,
- now_datetime, random_string, strip)
+ now_datetime, random_string, strip, get_link_to_form)
from frappe.utils.html_utils import clean_html
from frappe.website.doctype.website_slideshow.website_slideshow import \
get_slideshow
@@ -634,6 +634,9 @@
+ ": \n" + ", ".join([self.meta.get_label(fld) for fld in field_list]))
def after_rename(self, old_name, new_name, merge):
+ if merge:
+ self.validate_duplicate_item_in_stock_reconciliation(old_name, new_name)
+
if self.route:
invalidate_cache_for_item(self)
clear_cache(self.route)
@@ -656,6 +659,27 @@
frappe.db.set_value(dt, d.name, "item_wise_tax_detail",
json.dumps(item_wise_tax_detail), update_modified=False)
+ def validate_duplicate_item_in_stock_reconciliation(self, old_name, new_name):
+ records = frappe.db.sql(""" SELECT parent, COUNT(*) as records
+ FROM `tabStock Reconciliation Item`
+ WHERE item_code = %s and docstatus = 1
+ GROUP By item_code, warehouse, parent
+ HAVING records > 1
+ """, new_name, as_dict=1)
+
+ if not records: return
+ document = _("Stock Reconciliation") if len(records) == 1 else _("Stock Reconciliations")
+
+ msg = _("The items {0} and {1} are present in the following {2} : <br>"
+ .format(frappe.bold(old_name), frappe.bold(new_name), document))
+
+ msg += ', '.join([get_link_to_form("Stock Reconciliation", d.parent) for d in records]) + "<br><br>"
+
+ msg += _("Note: To merge the items, create a separate Stock Reconciliation for the old item {0}"
+ .format(frappe.bold(old_name)))
+
+ frappe.throw(_(msg), title=_("Merge not allowed"))
+
def set_last_purchase_rate(self, new_name):
last_purchase_rate = get_last_purchase_details(new_name).get("base_net_rate", 0)
frappe.db.set_value("Item", new_name, "last_purchase_rate", last_purchase_rate)