mirror of
https://github.com/frappe/erpnext.git
synced 2026-08-14 15:11:52 +00:00
fix(coa_importer): added server-side validations for importing chart of accounts (backport #58065) (#58067)
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)
|
||||||
@@ -471,6 +483,19 @@ def unset_existing_data(company):
|
|||||||
frappe.qb.from_(dt).where(dt.company == company).delete().run()
|
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):
|
def set_default_accounts(company):
|
||||||
from erpnext.setup.doctype.company.company import install_country_fixtures
|
from erpnext.setup.doctype.company.company import install_country_fixtures
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user