From 3f28fb19aa3f244981eda815b80e3a1fcf6974d5 Mon Sep 17 00:00:00 2001 From: Saqib Ansari Date: Thu, 8 Oct 2020 14:57:27 +0530 Subject: [PATCH 1/4] chore: validate is_group of child parent account before auto creating an account --- erpnext/accounts/doctype/account/account.py | 11 +++++++++-- erpnext/accounts/doctype/account/test_account.py | 11 +++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/erpnext/accounts/doctype/account/account.py b/erpnext/accounts/doctype/account/account.py index 164f120067..58674f4c1e 100644 --- a/erpnext/accounts/doctype/account/account.py +++ b/erpnext/accounts/doctype/account/account.py @@ -162,9 +162,16 @@ class Account(NestedSet): def create_account_for_child_company(self, parent_acc_name_map, descendants, parent_acc_name): for company in descendants: + company_bold = frappe.bold(company) + parent_acc_name_bold = frappe.bold(parent_acc_name) if not parent_acc_name_map.get(company): - frappe.throw(_("While creating account for child Company {0}, parent account {1} not found. Please create the parent account in corresponding COA") - .format(company, parent_acc_name)) + frappe.throw(_("While creating account for Child Company {0}, parent account {1} not found. Please create the parent account in corresponding COA") + .format(company_bold, parent_acc_name_bold), title=_("Account Not Found")) + if (frappe.db.get_value("Account", self.parent_account, "is_group") + and not frappe.db.get_value("Account", parent_acc_name_map[company], "is_group")): + frappe.throw(_("While creating account for Child Company {0}, parent account {1} is found \ + as a ledger account.

Please convert the parent account in corresponding child company COA to a group account.") + .format(company_bold, parent_acc_name_bold), title=_("Invalid Parent Account")) filters = { "account_name": self.account_name, diff --git a/erpnext/accounts/doctype/account/test_account.py b/erpnext/accounts/doctype/account/test_account.py index 89bb0184af..d16a8b2193 100644 --- a/erpnext/accounts/doctype/account/test_account.py +++ b/erpnext/accounts/doctype/account/test_account.py @@ -111,6 +111,17 @@ class TestAccount(unittest.TestCase): self.assertEqual(acc_tc_4, "Test Sync Account - _TC4") self.assertEqual(acc_tc_5, "Test Sync Account - _TC5") + def test_add_account_to_a_group(self): + frappe.db.set_value("Account", "Office Rent - _TC3", "is_group", 1) + + acc = frappe.new_doc("Account") + acc.account_name = "Test Group Account" + acc.parent_account = "Office Rent - _TC3" + acc.company = "_Test Company 3" + self.assertRaises(frappe.ValidationError, acc.insert) + + frappe.db.set_value("Account", "Office Rent - _TC3", "is_group", 0) + def _make_test_records(verbose): from frappe.test_runner import make_test_objects From 1dec68e8e4865403d2d646e14fbbbdb6bce5474c Mon Sep 17 00:00:00 2001 From: Saqib Ansari Date: Fri, 16 Oct 2020 10:47:29 +0530 Subject: [PATCH 2/4] fix: transalation syntax --- erpnext/accounts/doctype/account/account.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/erpnext/accounts/doctype/account/account.py b/erpnext/accounts/doctype/account/account.py index f23107b073..2605bc155f 100644 --- a/erpnext/accounts/doctype/account/account.py +++ b/erpnext/accounts/doctype/account/account.py @@ -171,9 +171,10 @@ class Account(NestedSet): .format(company_bold, parent_acc_name_bold), title=_("Account Not Found")) if (frappe.db.get_value("Account", self.parent_account, "is_group") and not frappe.db.get_value("Account", parent_acc_name_map[company], "is_group")): - frappe.throw(_("While creating account for Child Company {0}, parent account {1} is found \ - as a ledger account.

Please convert the parent account in corresponding child company COA to a group account.") - .format(company_bold, parent_acc_name_bold), title=_("Invalid Parent Account")) + msg = _("While creating account for Child Company {0}, parent account {1} found as a ledger account.").format(company_bold, parent_acc_name_bold) + msg += "

" + msg = _("Please convert the parent account in corresponding child company to a group account.") + frappe.throw(msg), title=_("Invalid Parent Account")) filters = { "account_name": self.account_name, From de505631a6fab5315b5d6a13ebd5a12a045f7ec1 Mon Sep 17 00:00:00 2001 From: Saqib Ansari Date: Fri, 16 Oct 2020 11:30:34 +0530 Subject: [PATCH 3/4] fix: codacy --- erpnext/accounts/doctype/account/account.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/erpnext/accounts/doctype/account/account.py b/erpnext/accounts/doctype/account/account.py index 2605bc155f..9de4649977 100644 --- a/erpnext/accounts/doctype/account/account.py +++ b/erpnext/accounts/doctype/account/account.py @@ -101,7 +101,7 @@ class Account(NestedSet): return if not frappe.db.get_value("Account", {'account_name': self.account_name, 'company': ancestors[0]}, 'name'): - frappe.throw(_("Please add the account to root level Company - %s" % ancestors[0])) + frappe.throw(_("Please add the account to root level Company - {}").format(ancestors[0])) elif self.parent_account: descendants = get_descendants_of('Company', self.company) if not descendants: return @@ -169,12 +169,14 @@ class Account(NestedSet): if not parent_acc_name_map.get(company): frappe.throw(_("While creating account for Child Company {0}, parent account {1} not found. Please create the parent account in corresponding COA") .format(company_bold, parent_acc_name_bold), title=_("Account Not Found")) - if (frappe.db.get_value("Account", self.parent_account, "is_group") + + # validate if parent of child company account to be added is a group + if (frappe.db.get_value("Account", self.parent_account, "is_group") and not frappe.db.get_value("Account", parent_acc_name_map[company], "is_group")): msg = _("While creating account for Child Company {0}, parent account {1} found as a ledger account.").format(company_bold, parent_acc_name_bold) msg += "

" msg = _("Please convert the parent account in corresponding child company to a group account.") - frappe.throw(msg), title=_("Invalid Parent Account")) + frappe.throw(msg, title=_("Invalid Parent Account")) filters = { "account_name": self.account_name, @@ -317,8 +319,9 @@ def update_account_number(name, account_name, account_number=None, from_descenda allow_child_account_creation = _("Allow Account Creation Against Child Company") message = _("Account {0} exists in parent company {1}.").format(frappe.bold(old_acc_name), frappe.bold(ancestor)) - message += "
" + _("Renaming it is only allowed via parent company {0}, \ - to avoid mismatch.").format(frappe.bold(ancestor)) + "

" + message += "
" + message += _("Renaming it is only allowed via parent company {0}, to avoid mismatch.").format(frappe.bold(ancestor)) + message += "

" message += _("To overrule this, enable '{0}' in company {1}").format(allow_child_account_creation, frappe.bold(account.company)) frappe.throw(message, title=_("Rename Not Allowed")) From fa5fe18b70168c7e6784f042004e2228adf511a5 Mon Sep 17 00:00:00 2001 From: Saqib Date: Mon, 26 Oct 2020 19:27:02 +0530 Subject: [PATCH 4/4] fix error message Co-authored-by: Deepesh Garg <42651287+deepeshgarg007@users.noreply.github.com> --- erpnext/accounts/doctype/account/account.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/account/account.py b/erpnext/accounts/doctype/account/account.py index 9de4649977..c801cfcbba 100644 --- a/erpnext/accounts/doctype/account/account.py +++ b/erpnext/accounts/doctype/account/account.py @@ -175,7 +175,7 @@ class Account(NestedSet): and not frappe.db.get_value("Account", parent_acc_name_map[company], "is_group")): msg = _("While creating account for Child Company {0}, parent account {1} found as a ledger account.").format(company_bold, parent_acc_name_bold) msg += "

" - msg = _("Please convert the parent account in corresponding child company to a group account.") + msg += _("Please convert the parent account in corresponding child company to a group account.") frappe.throw(msg, title=_("Invalid Parent Account")) filters = {