Fixed error in bom autoname
diff --git a/erpnext/manufacturing/doctype/bom/bom.py b/erpnext/manufacturing/doctype/bom/bom.py
index 24a21d9..52dae15 100644
--- a/erpnext/manufacturing/doctype/bom/bom.py
+++ b/erpnext/manufacturing/doctype/bom/bom.py
@@ -15,13 +15,21 @@
class BOM(Document):
def autoname(self):
- last_name = frappe.db.sql("""select max(name) from `tabBOM`
- where name like "BOM/{0}/%%" and item=%s
- """.format(frappe.db.escape(self.item, percent=False)), self.item)
- if last_name:
- idx = cint(cstr(last_name[0][0]).split('/')[-1].split('-')[0]) + 1
+ names = frappe.db.sql_list("""select name from `tabBOM` where item=%s""", self.item)
+
+ if names:
+ # name can be BOM/ITEM/001, BOM/ITEM/001-1, BOM-ITEM-001, BOM-ITEM-001-1
+
+ # split by item
+ names = [name.split(self.item)[-1][1:] for name in names]
+
+ # split by (-) if cancelled
+ names = [cint(name.split('-')[-1]) for name in names]
+
+ idx = max(names) + 1
else:
idx = 1
+
self.name = 'BOM/' + self.item + ('/%.3i' % idx)
def validate(self):