From ba245553f00acb0d9492fa524a39f086c1e447ff Mon Sep 17 00:00:00 2001 From: Rohit Waghchaure Date: Tue, 17 Sep 2019 11:53:56 +0530 Subject: [PATCH 1/2] fix: not able to change the account type in parent company account head --- erpnext/accounts/doctype/account/account.py | 64 +++++++++++++++------ 1 file changed, 48 insertions(+), 16 deletions(-) diff --git a/erpnext/accounts/doctype/account/account.py b/erpnext/accounts/doctype/account/account.py index 68efe377190..69fc5ba7de0 100644 --- a/erpnext/accounts/doctype/account/account.py +++ b/erpnext/accounts/doctype/account/account.py @@ -100,7 +100,10 @@ class Account(NestedSet): if ancestors: if frappe.get_value("Company", self.company, "allow_account_creation_against_child_company"): return - frappe.throw(_("Please add the account to root level Company - %s" % ancestors[0])) + + 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])) else: descendants = get_descendants_of('Company', self.company) if not descendants: return @@ -114,21 +117,7 @@ class Account(NestedSet): if not parent_acc_name_map: return - for company in descendants: - 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)) - - doc = frappe.copy_doc(self) - doc.flags.ignore_root_company_validation = True - doc.update({ - "company": company, - "account_currency": None, - "parent_account": parent_acc_name_map[company] - }) - doc.save() - frappe.msgprint(_("Account {0} is added in the child company {1}") - .format(doc.name, company)) + self.create_account_for_child_company(parent_acc_name_map, descendants) def validate_group_or_ledger(self): if self.get("__islocal"): @@ -170,6 +159,49 @@ class Account(NestedSet): if frappe.db.get_value("GL Entry", {"account": self.name}): frappe.throw(_("Currency can not be changed after making entries using some other currency")) + def create_account_for_child_company(self, parent_acc_name_map, descendants): + for company in descendants: + 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_map)) + + filters = { + "account_name": self.account_name, + "company": company + } + + if self.account_number: + filters["account_number"] = self.account_number + + child_account = frappe.db.get_value("Account", filters, 'name') + + if not child_account: + doc = frappe.copy_doc(self) + doc.flags.ignore_root_company_validation = True + doc.update({ + "company": company, + # parent account's currency should be passed down to child account's curreny + # if it is None, it picks it up from default company currency, which might be unintended + "account_currency": self.account_currency, + "parent_account": parent_acc_name_map[company] + }) + + doc.save() + frappe.msgprint(_("Account {0} is added in the child company {1}") + .format(doc.name, company)) + elif child_account: + # update the parent company's value in child companies + doc = frappe.get_doc("Account", child_account) + parent_value_changed = False + for field in ['account_type', 'account_currency', + 'freeze_account', 'balance_must_be']: + if doc.get(field) != self.get(field): + parent_value_changed = True + doc.set(field, self.get(field)) + + if parent_value_changed: + doc.save() + def convert_group_to_ledger(self): if self.check_if_child_exists(): throw(_("Account with child nodes cannot be converted to ledger")) From d1d835347efd837efb53531e9b7449aa214bb2b8 Mon Sep 17 00:00:00 2001 From: Suraj Shetty Date: Wed, 30 Oct 2019 14:45:11 +0530 Subject: [PATCH 2/2] fix: Pass parent_acc_name --- erpnext/accounts/doctype/account/account.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/erpnext/accounts/doctype/account/account.py b/erpnext/accounts/doctype/account/account.py index 69fc5ba7de0..cccced8e0be 100644 --- a/erpnext/accounts/doctype/account/account.py +++ b/erpnext/accounts/doctype/account/account.py @@ -117,7 +117,7 @@ class Account(NestedSet): if not parent_acc_name_map: return - self.create_account_for_child_company(parent_acc_name_map, descendants) + self.create_account_for_child_company(parent_acc_name_map, descendants, parent_acc_name) def validate_group_or_ledger(self): if self.get("__islocal"): @@ -159,11 +159,11 @@ class Account(NestedSet): if frappe.db.get_value("GL Entry", {"account": self.name}): frappe.throw(_("Currency can not be changed after making entries using some other currency")) - def create_account_for_child_company(self, parent_acc_name_map, descendants): + def create_account_for_child_company(self, parent_acc_name_map, descendants, parent_acc_name): for company in descendants: 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_map)) + .format(company, parent_acc_name)) filters = { "account_name": self.account_name,