mirror of
https://github.com/frappe/erpnext.git
synced 2026-09-18 19:07:55 +00:00
fix(coa_importer): added server-side validations for importing chart of accounts (backport #58065) (#58066)
Co-authored-by: Diptanil Saha <diptanil@frappe.io>
This commit is contained in:
@@ -110,18 +110,6 @@ frappe.ui.form.on("Chart of Accounts Importer", {
|
|||||||
args: {
|
args: {
|
||||||
company: frm.doc.company,
|
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");
|
|
||||||
}
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -70,7 +70,13 @@ def validate_company(company):
|
|||||||
frappe.throw(msg, title=_("Wrong Company"))
|
frappe.throw(msg, title=_("Wrong Company"))
|
||||||
|
|
||||||
if frappe.db.get_all("GL Entry", {"company": company}, "name", limit=1):
|
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()
|
@frappe.whitelist()
|
||||||
@@ -79,16 +85,22 @@ def import_coa(file_name, company):
|
|||||||
|
|
||||||
# delete existing data for accounts
|
# delete existing data for accounts
|
||||||
frappe.has_permission("Company", "write", company, throw=True)
|
frappe.has_permission("Company", "write", company, throw=True)
|
||||||
unset_existing_data(company)
|
|
||||||
|
|
||||||
# create accounts
|
# create accounts
|
||||||
file_doc, extension = get_file(file_name)
|
file_doc, extension = get_file(file_name)
|
||||||
|
validate_accounts(file_doc, extension)
|
||||||
|
|
||||||
if extension == "csv":
|
if extension == "csv":
|
||||||
data = generate_data_from_csv(file_doc)
|
data = generate_data_from_csv(file_doc)
|
||||||
else:
|
else:
|
||||||
data = generate_data_from_excel(file_doc, extension)
|
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
|
frappe.local.flags.ignore_root_company_validation = True
|
||||||
forest = build_forest(data)
|
forest = build_forest(data)
|
||||||
create_charts(company, custom_chart=forest, from_coa_importer=True)
|
create_charts(company, custom_chart=forest, from_coa_importer=True)
|
||||||
@@ -451,7 +463,6 @@ def get_mandatory_account_types():
|
|||||||
|
|
||||||
def unset_existing_data(company):
|
def unset_existing_data(company):
|
||||||
# remove accounts data from company
|
# remove accounts data from company
|
||||||
|
|
||||||
fieldnames = get_linked_fields("Account").get("Company", {}).get("fieldname", [])
|
fieldnames = get_linked_fields("Account").get("Company", {}).get("fieldname", [])
|
||||||
linked = [{"fieldname": name} for name in fieldnames]
|
linked = [{"fieldname": name} for name in fieldnames]
|
||||||
update_values = {d.get("fieldname"): "" for d in linked}
|
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)
|
frappe.db.set_value("Company", company, update_values, update_values)
|
||||||
|
|
||||||
# remove accounts data from various doctypes
|
# remove accounts data from various doctypes
|
||||||
for doctype in ["Account", "Sales Taxes and Charges Template", "Purchase Taxes and Charges Template"]:
|
for doctype in [
|
||||||
frappe.get_query(doctype, delete=True, filters={"company": company}, ignore_permissions=False).run()
|
"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):
|
def set_default_accounts(company):
|
||||||
|
|||||||
Reference in New Issue
Block a user