From 2e8abf93195a10d941fb9da97794c1b71303b399 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Mart=C3=ADnez?= Date: Thu, 17 Oct 2024 16:21:50 +0200 Subject: [PATCH] [IMP] account_financial_report: Add Amount cur. to General Ledger totals if the account has not set currency Related to https://github.com/OCA/account-financial-reporting/pull/1235#issuecomment-2419269015 --- .../report/general_ledger.py | 54 +++++++++++++++++++ .../report/general_ledger_xlsx.py | 5 +- .../report/templates/general_ledger.xml | 4 +- 3 files changed, 59 insertions(+), 4 deletions(-) diff --git a/account_financial_report/report/general_ledger.py b/account_financial_report/report/general_ledger.py index 2b79ef95..9e9f4c14 100644 --- a/account_financial_report/report/general_ledger.py +++ b/account_financial_report/report/general_ledger.py @@ -774,6 +774,7 @@ class GeneralLedgerReport(models.AbstractModel): list_centralized_ml += list(centralized_ml[jnl_id].values()) return list_centralized_ml + # flake8: noqa: C901 def _get_report_values(self, docids, data): wizard_id = data["wizard_id"] wizard = self.env["general.ledger.report.wizard"].browse(wizard_id) @@ -852,6 +853,59 @@ class GeneralLedgerReport(models.AbstractModel): account[grouped_by] = False del account["list_grouped"] general_ledger = sorted(general_ledger, key=lambda k: k["code"]) + # Set the bal_curr of the initial balance to 0 if it does not correspond + # (reducing the corresponding of the bal_curr of the initial balance). + for gl_item in general_ledger: + if ( + not gl_item["currency_id"] + or gl_item["currency_id"] != company.currency_id + ): + gl_item["fin_bal"]["bal_curr"] -= gl_item["init_bal"]["bal_curr"] + gl_item["init_bal"]["bal_curr"] = 0 + if "list_grouped" in gl_item: + for lg_item in gl_item["list_grouped"]: + lg_item["fin_bal"]["bal_curr"] -= lg_item["init_bal"][ + "bal_curr" + ] + lg_item["init_bal"]["bal_curr"] = 0 + # Set the fin_bal_currency_id value if the account does not have it set + # and there are move lines in a currency different from that of + # the company (USD for example). + for gl_item in general_ledger: + fin_bal_currency_ids = [] + fin_bal_currency_id = gl_item["currency_id"] + if gl_item["currency_id"]: + continue + gl_item["fin_bal"]["bal_curr"] = gl_item["init_bal"]["bal_curr"] + if "move_lines" in gl_item: + for ml in gl_item["move_lines"]: + ml_currency_id = ( + ml["currency_id"][0] if ml["currency_id"] else False + ) + if ml_currency_id and ml_currency_id != company.currency_id.id: + gl_item["fin_bal"]["bal_curr"] += ml["bal_curr"] + if ml_currency_id not in fin_bal_currency_ids: + fin_bal_currency_ids.append(ml_currency_id) + elif "list_grouped" in gl_item: + fin_bal_currency_ids = [] + for lg_item in gl_item["list_grouped"]: + lg_item["fin_bal"]["bal_curr"] = lg_item["init_bal"]["bal_curr"] + for ml in lg_item["move_lines"]: + ml_currency_id = ( + ml["currency_id"][0] if ml["currency_id"] else False + ) + if ml_currency_id and ml_currency_id != company.currency_id.id: + lg_item["fin_bal"]["bal_curr"] += ml["bal_curr"] + gl_item["fin_bal"]["bal_curr"] += ml["bal_curr"] + if ml_currency_id not in fin_bal_currency_ids: + fin_bal_currency_ids.append(ml_currency_id) + # If there is only 1 currency, we set that one as fin_bal_currency_id + # The use of different move lines with different currencies (EUR + GBP) + # will be excluded. We use a different field to avoid showing the initial + # balance and/or distorting data. + if not gl_item["currency_id"] and len(fin_bal_currency_ids) == 1: + fin_bal_currency_id = fin_bal_currency_ids[0] + gl_item["fin_bal_currency_id"] = fin_bal_currency_id return { "doc_ids": [wizard_id], "doc_model": "general.ledger.report.wizard", diff --git a/account_financial_report/report/general_ledger_xlsx.py b/account_financial_report/report/general_ledger_xlsx.py index 981246f3..c954071b 100644 --- a/account_financial_report/report/general_ledger_xlsx.py +++ b/account_financial_report/report/general_ledger_xlsx.py @@ -347,10 +347,11 @@ class GeneralLedgerXslx(models.AbstractModel): "final_balance": account["fin_bal"]["balance"], } ) - if foreign_currency and account["currency_id"]: + if foreign_currency and account["fin_bal_currency_id"]: account.update( { - "final_bal_curr": account["fin_bal"]["bal_curr"], + "final_bal_curr": total_bal_curr, + "currency_id": account["fin_bal_currency_id"], } ) self.write_ending_balance_from_dict(account, report_data) diff --git a/account_financial_report/report/templates/general_ledger.xml b/account_financial_report/report/templates/general_ledger.xml index a4b1fda4..eb20c343 100644 --- a/account_financial_report/report/templates/general_ledger.xml +++ b/account_financial_report/report/templates/general_ledger.xml @@ -696,10 +696,10 @@ /> - +