From 39e38bf08318e7af60251dcf500560040ba2526b Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Mon, 10 Jul 2023 19:26:32 +0530 Subject: [PATCH] fix: Opening balance in presentation currency in Trial Balance report (#36036) fix: Opening balance in presentation currency in Trial Balance report (#36036) (cherry picked from commit 4d07e20b05289c297ae49d0221f3840afe4986ec) # Conflicts: # erpnext/accounts/report/trial_balance/trial_balance.py Co-authored-by: Deepesh Garg --- .../consolidated_financial_statement.py | 2 +- .../report/general_ledger/general_ledger.py | 2 +- .../report/trial_balance/trial_balance.py | 26 ++++++++++++++----- erpnext/accounts/report/utils.py | 3 +-- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/erpnext/accounts/report/consolidated_financial_statement/consolidated_financial_statement.py b/erpnext/accounts/report/consolidated_financial_statement/consolidated_financial_statement.py index 2f8c44f4c28..5934fd12e85 100644 --- a/erpnext/accounts/report/consolidated_financial_statement/consolidated_financial_statement.py +++ b/erpnext/accounts/report/consolidated_financial_statement/consolidated_financial_statement.py @@ -655,7 +655,7 @@ def set_gl_entries_by_account( if filters and filters.get("presentation_currency") != d.default_currency: currency_info["company"] = d.name currency_info["company_currency"] = d.default_currency - convert_to_presentation_currency(gl_entries, currency_info, filters.get("company")) + convert_to_presentation_currency(gl_entries, currency_info) for entry in gl_entries: if entry.account_number: diff --git a/erpnext/accounts/report/general_ledger/general_ledger.py b/erpnext/accounts/report/general_ledger/general_ledger.py index 227d9b68ce6..23403a4b15b 100644 --- a/erpnext/accounts/report/general_ledger/general_ledger.py +++ b/erpnext/accounts/report/general_ledger/general_ledger.py @@ -204,7 +204,7 @@ def get_gl_entries(filters, accounting_dimensions): ) if filters.get("presentation_currency"): - return convert_to_presentation_currency(gl_entries, currency_map, filters.get("company")) + return convert_to_presentation_currency(gl_entries, currency_map) else: return gl_entries diff --git a/erpnext/accounts/report/trial_balance/trial_balance.py b/erpnext/accounts/report/trial_balance/trial_balance.py index c9caa5b8519..d51c4c4acba 100644 --- a/erpnext/accounts/report/trial_balance/trial_balance.py +++ b/erpnext/accounts/report/trial_balance/trial_balance.py @@ -17,6 +17,7 @@ from erpnext.accounts.report.financial_statements import ( filter_out_zero_value_rows, set_gl_entries_by_account, ) +from erpnext.accounts.report.utils import convert_to_presentation_currency, get_currency value_fields = ( "opening_debit", @@ -39,7 +40,7 @@ def validate_filters(filters): if not filters.fiscal_year: frappe.throw(_("Fiscal Year {0} is required").format(filters.fiscal_year)) - fiscal_year = frappe.db.get_value( + fiscal_year = frappe.get_cached_value( "Fiscal Year", filters.fiscal_year, ["year_start_date", "year_end_date"], as_dict=True ) if not fiscal_year: @@ -178,8 +179,8 @@ def get_rootwise_opening_balances(filters, report_type): "opening_credit": 0.0, }, ) - opening[d.account]["opening_debit"] += flt(d.opening_debit) - opening[d.account]["opening_credit"] += flt(d.opening_credit) + opening[d.account]["opening_debit"] += flt(d.debit) + opening[d.account]["opening_credit"] += flt(d.credit) return opening @@ -194,8 +195,11 @@ def get_opening_balance( frappe.qb.from_(closing_balance) .select( closing_balance.account, - Sum(closing_balance.debit).as_("opening_debit"), - Sum(closing_balance.credit).as_("opening_credit"), + closing_balance.account_currency, + Sum(closing_balance.debit).as_("debit"), + Sum(closing_balance.credit).as_("credit"), + Sum(closing_balance.debit_in_account_currency).as_("debit_in_account_currency"), + Sum(closing_balance.credit_in_account_currency).as_("credit_in_account_currency"), ) .where( (closing_balance.company == filters.company) @@ -247,9 +251,14 @@ def get_opening_balance( if filters.project: opening_balance = opening_balance.where(closing_balance.project == filters.project) - company_fb = frappe.db.get_value("Company", filters.company, "default_finance_book") - if filters.get("include_default_book_entries"): + company_fb = frappe.get_cached_value("Company", filters.company, "default_finance_book") + + if filters.finance_book and company_fb and cstr(filters.finance_book) != cstr(company_fb): + frappe.throw( + _("To use a different finance book, please uncheck 'Include Default Book Entries'") + ) + opening_balance = opening_balance.where( (closing_balance.finance_book.isin([cstr(filters.finance_book), cstr(company_fb), ""])) | (closing_balance.finance_book.isnull()) @@ -277,6 +286,9 @@ def get_opening_balance( gle = opening_balance.run(as_dict=1) + if filters and filters.get("presentation_currency"): + convert_to_presentation_currency(gle, get_currency(filters)) + return gle diff --git a/erpnext/accounts/report/utils.py b/erpnext/accounts/report/utils.py index 781481bd0d2..7ea1fac1056 100644 --- a/erpnext/accounts/report/utils.py +++ b/erpnext/accounts/report/utils.py @@ -78,7 +78,7 @@ def get_rate_as_at(date, from_currency, to_currency): return rate -def convert_to_presentation_currency(gl_entries, currency_info, company): +def convert_to_presentation_currency(gl_entries, currency_info): """ Take a list of GL Entries and change the 'debit' and 'credit' values to currencies in `currency_info`. @@ -93,7 +93,6 @@ def convert_to_presentation_currency(gl_entries, currency_info, company): account_currencies = list(set(entry["account_currency"] for entry in gl_entries)) for entry in gl_entries: - account = entry["account"] debit = flt(entry["debit"]) credit = flt(entry["credit"]) debit_in_account_currency = flt(entry["debit_in_account_currency"])