diff --git a/erpnext/accounts/doctype/chart_of_accounts_importer/chart_of_accounts_importer.js b/erpnext/accounts/doctype/chart_of_accounts_importer/chart_of_accounts_importer.js index 1d8bb853083..b7200883124 100644 --- a/erpnext/accounts/doctype/chart_of_accounts_importer/chart_of_accounts_importer.js +++ b/erpnext/accounts/doctype/chart_of_accounts_importer/chart_of_accounts_importer.js @@ -110,18 +110,6 @@ frappe.ui.form.on("Chart of Accounts Importer", { args: { company: frm.doc.company, }, - callback: function (r) { - if (r.message === false) { - frm.set_value("company", ""); - frappe.throw( - __( - "Transactions against the Company already exist! Chart of Accounts can only be imported for a Company with no transactions." - ) - ); - } else { - frm.trigger("refresh"); - } - }, }); } }, diff --git a/erpnext/accounts/doctype/chart_of_accounts_importer/chart_of_accounts_importer.py b/erpnext/accounts/doctype/chart_of_accounts_importer/chart_of_accounts_importer.py index 7cd489edc62..08272da0542 100644 --- a/erpnext/accounts/doctype/chart_of_accounts_importer/chart_of_accounts_importer.py +++ b/erpnext/accounts/doctype/chart_of_accounts_importer/chart_of_accounts_importer.py @@ -70,7 +70,13 @@ def validate_company(company): frappe.throw(msg, title=_("Wrong Company")) if frappe.db.get_all("GL Entry", {"company": company}, "name", limit=1): - return False + frappe.throw( + _( + "Transactions against the Company already exist! Chart of Accounts can only be imported for a Company with no transactions." + ) + ) + + validate_user_perms(company) @frappe.whitelist() @@ -79,16 +85,22 @@ def import_coa(file_name, company): # delete existing data for accounts frappe.has_permission("Company", "write", company, throw=True) - unset_existing_data(company) # create accounts file_doc, extension = get_file(file_name) + validate_accounts(file_doc, extension) if extension == "csv": data = generate_data_from_csv(file_doc) else: data = generate_data_from_excel(file_doc, extension) + validate_columns(data) + + validate_company(company) + + unset_existing_data(company) + frappe.local.flags.ignore_root_company_validation = True forest = build_forest(data) create_charts(company, custom_chart=forest, from_coa_importer=True) @@ -451,7 +463,6 @@ def get_mandatory_account_types(): def unset_existing_data(company): # remove accounts data from company - fieldnames = get_linked_fields("Account").get("Company", {}).get("fieldname", []) linked = [{"fieldname": name} for name in fieldnames] update_values = {d.get("fieldname"): "" for d in linked} @@ -459,11 +470,32 @@ def unset_existing_data(company): frappe.db.set_value("Company", company, update_values, update_values) # remove accounts data from various doctypes - for doctype in ["Account", "Sales Taxes and Charges Template", "Purchase Taxes and Charges Template"]: - frappe.get_query(doctype, delete=True, filters={"company": company}, ignore_permissions=False).run() + for doctype in [ + "Account", + "Sales Taxes and Charges Template", + "Purchase Taxes and Charges Template", + "Party Account", + "Mode of Payment Account", + "Tax Withholding Account", + ]: + frappe.get_query(doctype, delete=True, filters={"company": company}).run() - for doctype in ["Party Account", "Mode of Payment Account", "Tax Withholding Account"]: - frappe.get_query(doctype, delete=True, filters={"company": company}, ignore_permissions=True).run() + +def validate_user_perms(company): + # User Permission Check for Account Deletion + company_accounts_count = frappe.get_query( + "Account", fields=[{"COUNT": "name"}], filters={"company": company} + ).run()[0][0] + company_accounts_user_has_access_to = frappe.get_query( + "Account", fields=[{"COUNT": "name"}], filters={"company": company}, ignore_permissions=False + ).run()[0][0] + + if company_accounts_count != company_accounts_user_has_access_to: + frappe.throw( + _("Accounts cannot be removed, as user doesn't have access to all the accounts of {0}").format( + frappe.bold(company) + ) + ) def set_default_accounts(company):