diff --git a/erpnext/stock/doctype/item/item.py b/erpnext/stock/doctype/item/item.py index a707a0a4bc3..d6ec2a4f417 100644 --- a/erpnext/stock/doctype/item/item.py +++ b/erpnext/stock/doctype/item/item.py @@ -7,6 +7,7 @@ import json import frappe from frappe import _, bold from frappe.model.document import Document +from frappe.model.naming import NamingSeries from frappe.query_builder import Interval from frappe.query_builder.functions import Count, CurDate, UnixTimestamp from frappe.utils import ( @@ -409,6 +410,24 @@ class Item(Document): ) ) + if self.is_new() and series: + obj = NamingSeries(series) + prefix = obj.get_prefix() + doctype = frappe.qb.DocType("Series") + + query = frappe.qb.from_(doctype).select(doctype.name).where(doctype.name.like(f"{prefix}%")) + + prefix_exists = query.run(as_dict=True) + if prefix_exists: + frappe.msgprint( + _( + "The {0} prefix '{1}' already exists. Please change the Serial No Series, otherwise you will get a Duplicate Entry error." + ).format(bold(frappe.unscrub(field)), bold(prefix)), + title=_("Serial No Series Overlap"), + indicator="yellow", + alert=True, + ) + def check_for_active_boms(self): if self.default_bom: bom_item = frappe.db.get_value("BOM", self.default_bom, "item") diff --git a/erpnext/stock/serial_batch_bundle.py b/erpnext/stock/serial_batch_bundle.py index 208f9c048a0..298cec2fa89 100644 --- a/erpnext/stock/serial_batch_bundle.py +++ b/erpnext/stock/serial_batch_bundle.py @@ -1432,7 +1432,18 @@ class SerialBatchCreation: "batch_no", ] - frappe.db.bulk_insert("Serial No", fields=fields, values=set(serial_nos_details)) + try: + frappe.db.bulk_insert("Serial No", fields=fields, values=set(serial_nos_details)) + except Exception as e: + if e and len(e.args) > 1 and "Duplicate" in e.args[1]: + frappe.throw( + _( + "A naming series conflict occurred while creating serial numbers. Please change the naming series for the item {0}." + ).format(bold(self.item_code)), + title=_("Duplicate Serial Number Error"), + ) + else: + raise e obj.update_counter(current_value)