diff --git a/erpnext/patches/v12_0/move_item_tax_to_item_tax_template.py b/erpnext/patches/v12_0/move_item_tax_to_item_tax_template.py index 412f32030a..6769144aeb 100644 --- a/erpnext/patches/v12_0/move_item_tax_to_item_tax_template.py +++ b/erpnext/patches/v12_0/move_item_tax_to_item_tax_template.py @@ -1,5 +1,6 @@ import frappe import json +from frappe.model.naming import make_autoname from six import iteritems def execute(): @@ -7,7 +8,6 @@ def execute(): return old_item_taxes = {} item_tax_templates = {} - rename_template_to_untitled = [] for d in frappe.db.sql("""select parent as item_code, tax_type, tax_rate from `tabItem Tax`""", as_dict=1): old_item_taxes.setdefault(d.item_code, []) @@ -34,7 +34,7 @@ def execute(): for d in old_item_taxes[item_code]: item_tax_map[d.tax_type] = d.tax_rate - item_tax_template_name = get_item_tax_template(item_tax_templates, rename_template_to_untitled, + item_tax_template_name = get_item_tax_template(item_tax_templates, item_tax_map, item_code) # update the item tax table @@ -53,26 +53,19 @@ def execute(): for d in frappe.db.sql("""select name, parent, item_code, item_tax_rate from `tab{0} Item` where ifnull(item_tax_rate, '') not in ('', '{{}}')""".format(dt), as_dict=1): item_tax_map = json.loads(d.item_tax_rate) - item_tax_template = get_item_tax_template(item_tax_templates, rename_template_to_untitled, + item_tax_template = get_item_tax_template(item_tax_templates, item_tax_map, d.item_code, d.parent) frappe.db.set_value(dt + " Item", d.name, "item_tax_template", item_tax_template) - idx = 1 - for oldname in rename_template_to_untitled: - frappe.rename_doc("Item Tax Template", oldname, "Untitled {}".format(idx)) - idx += 1 - settings = frappe.get_single("Accounts Settings") settings.add_taxes_from_item_tax_template = 0 settings.determine_address_tax_category_from = "Billing Address" settings.save() -def get_item_tax_template(item_tax_templates, rename_template_to_untitled, item_tax_map, item_code, parent=None): +def get_item_tax_template(item_tax_templates, item_tax_map, item_code, parent=None): # search for previously created item tax template by comparing tax maps for template, item_tax_template_map in iteritems(item_tax_templates): if item_tax_map == item_tax_template_map: - if not parent: - rename_template_to_untitled.append(template) return template # if no item tax template found, create one @@ -97,5 +90,11 @@ def get_item_tax_template(item_tax_templates, rename_template_to_untitled, item_ item_tax_template.append("taxes", {"tax_type": tax_type, "tax_rate": tax_rate}) item_tax_templates.setdefault(item_tax_template.title, {}) item_tax_templates[item_tax_template.title][tax_type] = tax_rate - item_tax_template.save() + + try: + item_tax_template.save() + except frappe.DuplicateEntryError: + item_tax_template.name = make_autoname(item_tax_template.title + "/.###") + item_tax_template.save() + return item_tax_template.name