From af98f5d14d29fd18cad1a189e5ad97c3fd5e968c Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Mon, 2 Apr 2018 10:14:32 +0530 Subject: [PATCH] Sort accounts by account number in financial statements (#13423) --- .../accounts/report/financial_statements.py | 34 +++++++++---------- erpnext/accounts/utils.py | 6 ++-- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/erpnext/accounts/report/financial_statements.py b/erpnext/accounts/report/financial_statements.py index ffe860e8ec..dd47ca0b37 100644 --- a/erpnext/accounts/report/financial_statements.py +++ b/erpnext/accounts/report/financial_statements.py @@ -297,8 +297,7 @@ def filter_accounts(accounts, depth=10): def add_to_list(parent, level): if level < depth: children = parent_children_map.get(parent) or [] - if parent == None: - sort_root_accounts(children) + sort_accounts(children, is_root=True if parent==None else False) for child in children: child.indent = level @@ -310,25 +309,26 @@ def filter_accounts(accounts, depth=10): return filtered_accounts, accounts_by_name, parent_children_map -def sort_root_accounts(roots): +def sort_accounts(accounts, is_root=False, key="name"): """Sort root types as Asset, Liability, Equity, Income, Expense""" - def compare_roots(a, b): - if a.value and re.split('\W+', a.value)[0].isdigit(): - # if chart of accounts is numbered, then sort by number - return cmp(a.value, b.value) - if a.report_type != b.report_type and a.report_type == "Balance Sheet": - return -1 - if a.root_type != b.root_type and a.root_type == "Asset": - return -1 - if a.root_type == "Liability" and b.root_type == "Equity": - return -1 - if a.root_type == "Income" and b.root_type == "Expense": - return -1 + def compare_accounts(a, b): + if is_root: + if a.report_type != b.report_type and a.report_type == "Balance Sheet": + return -1 + if a.root_type != b.root_type and a.root_type == "Asset": + return -1 + if a.root_type == "Liability" and b.root_type == "Equity": + return -1 + if a.root_type == "Income" and b.root_type == "Expense": + return -1 + else: + if re.split('\W+', a[key])[0].isdigit(): + # if chart of accounts is numbered, then sort by number + return cmp(a[key], b[key]) return 1 - roots.sort(key = functools.cmp_to_key(compare_roots)) - + accounts.sort(key = functools.cmp_to_key(compare_accounts)) def set_gl_entries_by_account( company, from_date, to_date, root_lft, root_rgt, filters, gl_entries_by_account, ignore_closing_entries=False): diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py index 0e35768a6a..b214908f62 100644 --- a/erpnext/accounts/utils.py +++ b/erpnext/accounts/utils.py @@ -663,7 +663,7 @@ def get_companies(): @frappe.whitelist() def get_children(doctype, parent, company, is_root=False): - from erpnext.accounts.report.financial_statements import sort_root_accounts + from erpnext.accounts.report.financial_statements import sort_accounts fieldname = frappe.db.escape(doctype.lower().replace(' ','_')) doctype = frappe.db.escape(doctype) @@ -678,9 +678,6 @@ def get_children(doctype, parent, company, is_root=False): and `company` = %s and docstatus<2 order by name""".format(fields=fields, fieldname = fieldname, doctype=doctype), company, as_dict=1) - - if parent=="Accounts": - sort_root_accounts(acc) else: # other fields = ", account_currency" if doctype=="Account" else "" @@ -693,6 +690,7 @@ def get_children(doctype, parent, company, is_root=False): parent, as_dict=1) if doctype == 'Account': + sort_accounts(acc, is_root, key="value") company_currency = frappe.db.get_value("Company", company, "default_currency") for each in acc: each["company_currency"] = company_currency