From 30e766b228f08217b7e183ea97c76a2b75e93f89 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Wed, 5 Feb 2020 12:12:47 +0530 Subject: [PATCH] fix: SQL query in financial statements --- .../accounts/report/financial_statements.py | 53 ++++++++++--------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/erpnext/accounts/report/financial_statements.py b/erpnext/accounts/report/financial_statements.py index 96f78f9321..35915d0fc6 100644 --- a/erpnext/accounts/report/financial_statements.py +++ b/erpnext/accounts/report/financial_statements.py @@ -350,39 +350,40 @@ def set_gl_entries_by_account( accounts = frappe.db.sql_list("""select name from `tabAccount` where lft >= %s and rgt <= %s and company = %s""", (root_lft, root_rgt, company)) - additional_conditions += " and account in ({})"\ - .format(", ".join([frappe.db.escape(d) for d in accounts])) + if accounts: + additional_conditions += " and account in ({})"\ + .format(", ".join([frappe.db.escape(d) for d in accounts])) - gl_filters = { - "company": company, - "from_date": from_date, - "to_date": to_date, - "finance_book": cstr(filters.get("finance_book")) - } + gl_filters = { + "company": company, + "from_date": from_date, + "to_date": to_date, + "finance_book": cstr(filters.get("finance_book")) + } - if filters.get("include_default_book_entries"): - gl_filters["company_fb"] = frappe.db.get_value("Company", - company, 'default_finance_book') + if filters.get("include_default_book_entries"): + gl_filters["company_fb"] = frappe.db.get_value("Company", + company, 'default_finance_book') - for key, value in filters.items(): - if value: - gl_filters.update({ - key: value - }) + for key, value in filters.items(): + if value: + gl_filters.update({ + key: value + }) - gl_entries = frappe.db.sql("""select posting_date, account, debit, credit, is_opening, fiscal_year, debit_in_account_currency, credit_in_account_currency, account_currency from `tabGL Entry` - where company=%(company)s - {additional_conditions} - and posting_date <= %(to_date)s - order by account, posting_date""".format(additional_conditions=additional_conditions), gl_filters, as_dict=True) #nosec + gl_entries = frappe.db.sql("""select posting_date, account, debit, credit, is_opening, fiscal_year, debit_in_account_currency, credit_in_account_currency, account_currency from `tabGL Entry` + where company=%(company)s + {additional_conditions} + and posting_date <= %(to_date)s + order by account, posting_date""".format(additional_conditions=additional_conditions), gl_filters, as_dict=True) #nosec - if filters and filters.get('presentation_currency'): - convert_to_presentation_currency(gl_entries, get_currency(filters)) + if filters and filters.get('presentation_currency'): + convert_to_presentation_currency(gl_entries, get_currency(filters)) - for entry in gl_entries: - gl_entries_by_account.setdefault(entry.account, []).append(entry) + for entry in gl_entries: + gl_entries_by_account.setdefault(entry.account, []).append(entry) - return gl_entries_by_account + return gl_entries_by_account def get_additional_conditions(from_date, ignore_closing_entries, filters):