From ee2f8d8ebce94dc3588e46413232ce661ffeb3c8 Mon Sep 17 00:00:00 2001 From: Smit Vora Date: Tue, 3 Feb 2026 15:21:38 +0530 Subject: [PATCH] fix: correctly calculate running balances for financial report --- .../financial_report_engine.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/erpnext/accounts/doctype/financial_report_template/financial_report_engine.py b/erpnext/accounts/doctype/financial_report_template/financial_report_engine.py index 4de556b7e46..ce8ed4adb07 100644 --- a/erpnext/accounts/doctype/financial_report_template/financial_report_engine.py +++ b/erpnext/accounts/doctype/financial_report_template/financial_report_engine.py @@ -636,12 +636,15 @@ class FinancialQueryBuilder: return self._execute_with_permissions(query, "GL Entry") def _calculate_running_balances(self, balances_data: dict, gl_data: list[dict]) -> dict: - for row in gl_data: - account = row["account"] + gl_dict = {row["account"]: row for row in gl_data} + accounts = set(balances_data.keys()) | set(gl_dict.keys()) + + for account in accounts: if account not in balances_data: balances_data[account] = AccountData(account=account, **self._get_account_meta(account)) account_data: AccountData = balances_data[account] + gl_movement = gl_dict.get(account, {}) if account_data.has_periods(): first_period = account_data.get_period(self.periods[0]["key"]) @@ -651,20 +654,13 @@ class FinancialQueryBuilder: for period in self.periods: period_key = period["key"] - movement = row.get(period_key, 0.0) + movement = gl_movement.get(period_key, 0.0) closing_balance = current_balance + movement account_data.add_period(PeriodValue(period_key, current_balance, closing_balance, movement)) current_balance = closing_balance - # Accounts with no movements - for account_data in balances_data.values(): - for period in self.periods: - period_key = period["key"] - if period_key not in account_data.period_values: - account_data.add_period(PeriodValue(period_key, 0.0, 0.0, 0.0)) - def _handle_balance_accumulation(self, balances_data): for account_data in balances_data.values(): account_data: AccountData