From 8938d1040db8602ff54ff14745804629fa656f5f Mon Sep 17 00:00:00 2001 From: marination Date: Mon, 31 Aug 2020 18:18:10 +0530 Subject: [PATCH] fix: Rename/Update Child company Accounts as well --- erpnext/accounts/doctype/account/account.py | 24 +++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/erpnext/accounts/doctype/account/account.py b/erpnext/accounts/doctype/account/account.py index b383ba4e30..0a888935ba 100644 --- a/erpnext/accounts/doctype/account/account.py +++ b/erpnext/accounts/doctype/account/account.py @@ -311,10 +311,13 @@ def validate_account_number(name, account_number, company): .format(account_number, account_with_same_number)) @frappe.whitelist() -def update_account_number(name, account_name, account_number=None): - +def update_account_number(name, account_name, account_number=None, from_descendant=False): account = frappe.db.get_value("Account", name, "company", as_dict=True) if not account: return + + old_acc_name, old_acc_number = frappe.db.get_value('Account', name, \ + ["account_name", "account_number"]) + validate_account_number(name, account_number, account.company) if account_number: frappe.db.set_value("Account", name, "account_number", account_number.strip()) @@ -322,6 +325,12 @@ def update_account_number(name, account_name, account_number=None): frappe.db.set_value("Account", name, "account_number", "") frappe.db.set_value("Account", name, "account_name", account_name.strip()) + if not from_descendant: + # Update and rename in child company accounts as well + descendants = get_descendants_of('Company', account.company) + if descendants: + sync_update_account_number_in_child(descendants, old_acc_name, account_name, account_number, old_acc_number) + new_name = get_account_autoname(account_number, account_name, account.company) if name != new_name: frappe.rename_doc("Account", name, new_name, force=1) @@ -352,3 +361,14 @@ def get_root_company(company): # return the topmost company in the hierarchy ancestors = get_ancestors_of('Company', company, "lft asc") return [ancestors[0]] if ancestors else [] + +def sync_update_account_number_in_child(descendants, old_acc_name, account_name, account_number=None, old_acc_number=None): + filters = { + "company": ["in", descendants], + "account_name": old_acc_name, + } + if old_acc_number: + filters["account_number"] = old_acc_number + + for d in frappe.db.get_values('Account', filters=filters, fieldname=["company", "name"], as_dict=True): + update_account_number(d["name"], account_name, account_number, from_descendant=True)