From fdcda85f8759a169f6e1fcb0e0e9d4913b61a99b Mon Sep 17 00:00:00 2001 From: marination Date: Mon, 31 Aug 2020 16:10:52 +0530 Subject: [PATCH] fix: Validate Missing Accounts in Child Companies - If parent account exists in Parent and now child, throw error --- erpnext/accounts/doctype/account/account.py | 24 ++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/account/account.py b/erpnext/accounts/doctype/account/account.py index 164f120067..b383ba4e30 100644 --- a/erpnext/accounts/doctype/account/account.py +++ b/erpnext/accounts/doctype/account/account.py @@ -117,7 +117,29 @@ class Account(NestedSet): for d in frappe.db.get_values('Account', filters=filters, fieldname=["company", "name"], as_dict=True): parent_acc_name_map[d["company"]] = d["name"] - if not parent_acc_name_map: return + + if not parent_acc_name_map: + # map can be empty if only one descendant or all descendants without parent account exist(s) + # or if no descendants exist + if descendants: + frappe.throw(_("Parent Account {0} does not exist in any Child Company").format(frappe.bold(parent_acc_name)), + title=_("Account Missing")) + else: + # no descendants and empty map, nothing to sync + return + + companies_missing_account = [] + for company in descendants: + if not company in parent_acc_name_map: + companies_missing_account.append(company) + + # If atleast any one of the descendants does not have the parent account, block transaction + if companies_missing_account: + message = _("Parent Account {0} does not exist in the following companies:").format(frappe.bold(parent_acc_name)) + message += "

" + message += _("Please make sure the account exists in the child companies as well") + frappe.throw(message, title=_("Account Missing")) + self.create_account_for_child_company(parent_acc_name_map, descendants, parent_acc_name) def validate_group_or_ledger(self):