Sort accounts by account number in financial statements (#13423)

This commit is contained in:
Nabin Hait 2018-04-02 10:14:32 +05:30 committed by GitHub
parent 456fdf09cb
commit af98f5d14d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 21 deletions

View File

@ -297,8 +297,7 @@ def filter_accounts(accounts, depth=10):
def add_to_list(parent, level): def add_to_list(parent, level):
if level < depth: if level < depth:
children = parent_children_map.get(parent) or [] children = parent_children_map.get(parent) or []
if parent == None: sort_accounts(children, is_root=True if parent==None else False)
sort_root_accounts(children)
for child in children: for child in children:
child.indent = level child.indent = level
@ -310,13 +309,11 @@ def filter_accounts(accounts, depth=10):
return filtered_accounts, accounts_by_name, parent_children_map 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""" """Sort root types as Asset, Liability, Equity, Income, Expense"""
def compare_roots(a, b): def compare_accounts(a, b):
if a.value and re.split('\W+', a.value)[0].isdigit(): if is_root:
# 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": if a.report_type != b.report_type and a.report_type == "Balance Sheet":
return -1 return -1
if a.root_type != b.root_type and a.root_type == "Asset": if a.root_type != b.root_type and a.root_type == "Asset":
@ -325,10 +322,13 @@ def sort_root_accounts(roots):
return -1 return -1
if a.root_type == "Income" and b.root_type == "Expense": if a.root_type == "Income" and b.root_type == "Expense":
return -1 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 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( def set_gl_entries_by_account(
company, from_date, to_date, root_lft, root_rgt, filters, gl_entries_by_account, ignore_closing_entries=False): company, from_date, to_date, root_lft, root_rgt, filters, gl_entries_by_account, ignore_closing_entries=False):

View File

@ -663,7 +663,7 @@ def get_companies():
@frappe.whitelist() @frappe.whitelist()
def get_children(doctype, parent, company, is_root=False): 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(' ','_')) fieldname = frappe.db.escape(doctype.lower().replace(' ','_'))
doctype = frappe.db.escape(doctype) doctype = frappe.db.escape(doctype)
@ -678,9 +678,6 @@ def get_children(doctype, parent, company, is_root=False):
and `company` = %s and docstatus<2 and `company` = %s and docstatus<2
order by name""".format(fields=fields, fieldname = fieldname, doctype=doctype), order by name""".format(fields=fields, fieldname = fieldname, doctype=doctype),
company, as_dict=1) company, as_dict=1)
if parent=="Accounts":
sort_root_accounts(acc)
else: else:
# other # other
fields = ", account_currency" if doctype=="Account" else "" fields = ", account_currency" if doctype=="Account" else ""
@ -693,6 +690,7 @@ def get_children(doctype, parent, company, is_root=False):
parent, as_dict=1) parent, as_dict=1)
if doctype == 'Account': if doctype == 'Account':
sort_accounts(acc, is_root, key="value")
company_currency = frappe.db.get_value("Company", company, "default_currency") company_currency = frappe.db.get_value("Company", company, "default_currency")
for each in acc: for each in acc:
each["company_currency"] = company_currency each["company_currency"] = company_currency