mirror of
https://github.com/frappe/erpnext.git
synced 2026-02-13 17:53:49 +00:00
fix: correctly calculate running balances for financial report
(cherry picked from commit ee2f8d8ebc)
This commit is contained in:
@@ -636,12 +636,15 @@ class FinancialQueryBuilder:
|
|||||||
return self._execute_with_permissions(query, "GL Entry")
|
return self._execute_with_permissions(query, "GL Entry")
|
||||||
|
|
||||||
def _calculate_running_balances(self, balances_data: dict, gl_data: list[dict]) -> dict:
|
def _calculate_running_balances(self, balances_data: dict, gl_data: list[dict]) -> dict:
|
||||||
for row in gl_data:
|
gl_dict = {row["account"]: row for row in gl_data}
|
||||||
account = row["account"]
|
accounts = set(balances_data.keys()) | set(gl_dict.keys())
|
||||||
|
|
||||||
|
for account in accounts:
|
||||||
if account not in balances_data:
|
if account not in balances_data:
|
||||||
balances_data[account] = AccountData(account=account, **self._get_account_meta(account))
|
balances_data[account] = AccountData(account=account, **self._get_account_meta(account))
|
||||||
|
|
||||||
account_data: AccountData = balances_data[account]
|
account_data: AccountData = balances_data[account]
|
||||||
|
gl_movement = gl_dict.get(account, {})
|
||||||
|
|
||||||
if account_data.has_periods():
|
if account_data.has_periods():
|
||||||
first_period = account_data.get_period(self.periods[0]["key"])
|
first_period = account_data.get_period(self.periods[0]["key"])
|
||||||
@@ -651,20 +654,13 @@ class FinancialQueryBuilder:
|
|||||||
|
|
||||||
for period in self.periods:
|
for period in self.periods:
|
||||||
period_key = period["key"]
|
period_key = period["key"]
|
||||||
movement = row.get(period_key, 0.0)
|
movement = gl_movement.get(period_key, 0.0)
|
||||||
closing_balance = current_balance + movement
|
closing_balance = current_balance + movement
|
||||||
|
|
||||||
account_data.add_period(PeriodValue(period_key, current_balance, closing_balance, movement))
|
account_data.add_period(PeriodValue(period_key, current_balance, closing_balance, movement))
|
||||||
|
|
||||||
current_balance = closing_balance
|
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):
|
def _handle_balance_accumulation(self, balances_data):
|
||||||
for account_data in balances_data.values():
|
for account_data in balances_data.values():
|
||||||
account_data: AccountData
|
account_data: AccountData
|
||||||
|
|||||||
Reference in New Issue
Block a user