Merge pull request #20521 from deepeshgarg007/fixnancial_query_fix_v12

fix: SQL query in financial statements
This commit is contained in:
Deepesh Garg
2020-02-05 12:33:06 +05:30
committed by GitHub

View File

@@ -348,40 +348,42 @@ def set_gl_entries_by_account(
additional_conditions = get_additional_conditions(from_date, ignore_closing_entries, filters) additional_conditions = get_additional_conditions(from_date, ignore_closing_entries, filters)
accounts = frappe.db.sql_list("""select name from `tabAccount` accounts = frappe.db.sql_list("""select name from `tabAccount`
where lft >= %s and rgt <= %s""", (root_lft, root_rgt)) 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]))
gl_filters = { if accounts:
"company": company, additional_conditions += " and account in ({})"\
"from_date": from_date, .format(", ".join([frappe.db.escape(d) for d in accounts]))
"to_date": to_date,
"finance_book": cstr(filters.get("finance_book"))
}
if filters.get("include_default_book_entries"): gl_filters = {
gl_filters["company_fb"] = frappe.db.get_value("Company", "company": company,
company, 'default_finance_book') "from_date": from_date,
"to_date": to_date,
"finance_book": cstr(filters.get("finance_book"))
}
for key, value in filters.items(): if filters.get("include_default_book_entries"):
if value: gl_filters["company_fb"] = frappe.db.get_value("Company",
gl_filters.update({ company, 'default_finance_book')
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` for key, value in filters.items():
where company=%(company)s if value:
{additional_conditions} gl_filters.update({
and posting_date <= %(to_date)s key: value
order by account, posting_date""".format(additional_conditions=additional_conditions), gl_filters, as_dict=True) #nosec })
if filters and filters.get('presentation_currency'): 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`
convert_to_presentation_currency(gl_entries, get_currency(filters)) 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
for entry in gl_entries: if filters and filters.get('presentation_currency'):
gl_entries_by_account.setdefault(entry.account, []).append(entry) convert_to_presentation_currency(gl_entries, get_currency(filters))
return gl_entries_by_account for entry in gl_entries:
gl_entries_by_account.setdefault(entry.account, []).append(entry)
return gl_entries_by_account
def get_additional_conditions(from_date, ignore_closing_entries, filters): def get_additional_conditions(from_date, ignore_closing_entries, filters):