From d0ef9678900c582075c898787c7bf79a4cfb32d0 Mon Sep 17 00:00:00 2001 From: Diptanil Saha Date: Wed, 12 Aug 2026 04:24:34 +0530 Subject: [PATCH] fix(coa_importer): added server-side validations for importing chart of accounts (backport #58065) (#58067) --- .../chart_of_accounts_importer.js | 12 -------- .../chart_of_accounts_importer.py | 29 +++++++++++++++++-- 2 files changed, 27 insertions(+), 14 deletions(-) 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 a50fd783d30..4949d407cf4 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) @@ -471,6 +483,19 @@ def unset_existing_data(company): frappe.qb.from_(dt).where(dt.company == company).delete().run() +def validate_user_perms(company): + # User Permission Check for Account Deletion + company_accounts = frappe.get_query("Account", filters={"company": company}).run(as_dict=1) + + for d in company_accounts: + if not frappe.get_cached_doc("Account", d.name).has_permission(): + 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): from erpnext.setup.doctype.company.company import install_country_fixtures