From 553a3ea7d2188209f0c2790cd43816b39190bb8a Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Tue, 23 Apr 2019 18:46:17 +0530 Subject: [PATCH] fix: create account if not exists in item tax patch --- .../move_item_tax_to_item_tax_template.py | 41 +++++++++++++------ 1 file changed, 28 insertions(+), 13 deletions(-) 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 575ebebadb..85bcc8fcb8 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 @@ -13,19 +13,19 @@ def execute(): old_item_taxes.setdefault(d.item_code, []) old_item_taxes[d.item_code].append(d) - frappe.reload_doc("accounts", "doctype", "item_tax_template_detail") - frappe.reload_doc("accounts", "doctype", "item_tax_template") - frappe.reload_doc("stock", "doctype", "item") - frappe.reload_doc("stock", "doctype", "item_tax") - frappe.reload_doc("selling", "doctype", "quotation_item") - frappe.reload_doc("selling", "doctype", "sales_order_item") - frappe.reload_doc("stock", "doctype", "delivery_note_item") - frappe.reload_doc("accounts", "doctype", "sales_invoice_item") - frappe.reload_doc("buying", "doctype", "supplier_quotation_item") - frappe.reload_doc("buying", "doctype", "purchase_order_item") - frappe.reload_doc("stock", "doctype", "purchase_receipt_item") - frappe.reload_doc("accounts", "doctype", "purchase_invoice_item") - frappe.reload_doc("accounts", "doctype", "accounts_settings") + frappe.reload_doc("accounts", "doctype", "item_tax_template_detail", force=1) + frappe.reload_doc("accounts", "doctype", "item_tax_template", force=1) + frappe.reload_doc("stock", "doctype", "item", force=1) + frappe.reload_doc("stock", "doctype", "item_tax", force=1) + frappe.reload_doc("selling", "doctype", "quotation_item", force=1) + frappe.reload_doc("selling", "doctype", "sales_order_item", force=1) + frappe.reload_doc("stock", "doctype", "delivery_note_item", force=1) + frappe.reload_doc("accounts", "doctype", "sales_invoice_item", force=1) + frappe.reload_doc("buying", "doctype", "supplier_quotation_item", force=1) + frappe.reload_doc("buying", "doctype", "purchase_order_item", force=1) + frappe.reload_doc("stock", "doctype", "purchase_receipt_item", force=1) + frappe.reload_doc("accounts", "doctype", "purchase_invoice_item", force=1) + frappe.reload_doc("accounts", "doctype", "accounts_settings", force=1) # for each item that have item tax rates for item_code in old_item_taxes.keys(): @@ -77,6 +77,21 @@ def get_item_tax_template(item_tax_templates, rename_template_to_untitled, item_ item_tax_template = frappe.new_doc("Item Tax Template") item_tax_template.title = "{}--{}".format(parent, item_code) if parent else "Item-{}".format(item_code) for tax_type, tax_rate in iteritems(item_tax_map): + if not frappe.db.exists("Account", tax_type): + parts = tax_type.strip().split(" - ") + account_name = " - ".join(parts[:-1]) + company = frappe.db.get_value("Company", filters={"abbr": parts[-1]}) + parent_account = frappe.db.get_value("Account", + filters={"account_type": "Tax", "root_type": "Liability"}, "parent_account") + + frappe.get_doc({ + "doctype": "Account", + "account_name": account_name, + "company": company, + "account_type": "Tax", + "parent_account": parent_account + }).insert() + 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