fix(control): recompute cash-flow totals from lines and cash-account turnover controls
davoodmehraban89 committed
Oct 31, 2026 at 08:39 UTC
5b88d2b2af0ec569e8547907f9b16443e503f39e
1 file changed
+56
-8
backend/src/aram_backend/cross_statement_reconciliation.py
+56
-8
@@ -14,7 +14,7 @@ from typing import Any, Sequence
14
15
from aram_backend.balance_sheet_candidate import BalanceSheetCandidate
16
from aram_backend.cash_flow_direct_candidate import (
17
- CashAccountReconciliation, DirectCashFlowCandidate,
17
+ CashAccountReconciliation, CashFlowLine, DirectCashFlowCandidate,
18
)
19
from aram_backend.coverage_preview import CoverageError
20
from aram_backend.equity_changes_candidate import EquityChangesCandidate
@@ -152,14 +152,42 @@ def _income_bridge(income: IncomeStatementCandidate) -> bool:
152
153
154
def _cash_flow_bridge(cash: DirectCashFlowCandidate) -> bool:
155
+ categories = {
156
+ activity: {"inflows": [], "outflows": []}
157
+ for activity in ("OPERATING", "INVESTING", "FINANCING")
158
+ }
159
+ line_ids: set[str] = set()
160
+ if (type(cash.transaction_count) is not int
161
+ or cash.transaction_count != len(cash.cash_flow_lines)):
162
+ return False
163
+ for line in cash.cash_flow_lines:
164
+ if not isinstance(line, CashFlowLine):
165
+ return False
166
+ line_id = _text(line.line_id, "CASH_FLOW_LINE_ID")
167
+ if line_id in line_ids:
168
+ return False
169
+ line_ids.add(line_id)
170
+ inflow = _money(line.cash_inflow)
171
+ outflow = _money(line.cash_outflow)
172
+ if inflow < 0 or outflow < 0 or not ((inflow > 0) ^ (outflow > 0)):
173
+ return False
174
+ if line.activity in categories:
175
+ categories[line.activity]["inflows"].append(inflow)
176
+ categories[line.activity]["outflows"].append(outflow)
177
+ elif line.activity != "CASH_TRANSFER":
178
+ return False
179
+
180
operating = _sum_exact([
156
- _money(cash.operating_inflows), -_money(cash.operating_outflows),
181
+ _sum_exact(categories["OPERATING"]["inflows"]),
182
+ -_sum_exact(categories["OPERATING"]["outflows"]),
183
])
184
investing = _sum_exact([
159
- _money(cash.investing_inflows), -_money(cash.investing_outflows),
185
+ _sum_exact(categories["INVESTING"]["inflows"]),
186
+ -_sum_exact(categories["INVESTING"]["outflows"]),
187
])
188
financing = _sum_exact([
162
- _money(cash.financing_inflows), -_money(cash.financing_outflows),
189
+ _sum_exact(categories["FINANCING"]["inflows"]),
190
+ -_sum_exact(categories["FINANCING"]["outflows"]),
191
])
192
net_change = _money(cash.net_change_in_cash)
193
rollforward = _sum_exact([
@@ -167,10 +195,18 @@ def _cash_flow_bridge(cash: DirectCashFlowCandidate) -> bool:
195
-_money(cash.closing_cash_and_equivalents),
196
])
197
return (
170
- operating == _money(cash.operating_net_cash_flow)
198
+ _sum_exact(categories["OPERATING"]["inflows"]) == _money(cash.operating_inflows)
199
+ and _sum_exact(categories["OPERATING"]["outflows"]) == _money(cash.operating_outflows)
200
+ and operating == _money(cash.operating_net_cash_flow)
201
+ and _sum_exact(categories["INVESTING"]["inflows"]) == _money(cash.investing_inflows)
202
+ and _sum_exact(categories["INVESTING"]["outflows"]) == _money(cash.investing_outflows)
203
and investing == _money(cash.investing_net_cash_flow)
204
+ and _sum_exact(categories["FINANCING"]["inflows"]) == _money(cash.financing_inflows)
205
+ and _sum_exact(categories["FINANCING"]["outflows"]) == _money(cash.financing_outflows)
206
and financing == _money(cash.financing_net_cash_flow)
207
and _sum_exact([operating, investing, financing, -net_change]) == 0
208
+ and _money(cash.internal_transfer_inflows) == _money(cash.internal_transfer_outflows)
209
+ and _money(cash.internal_transfer_inflows) >= 0
210
and rollforward == _money(cash.rollforward_difference) == 0
211
)
212
@@ -264,9 +300,21 @@ def _cash_accounts_match_trial_balance(
300
if (source_values != trial_values
301
or item.source_row != trial_account.source_row):
302
findings_valid = False
267
- if (_money(item.debit_difference) != 0
268
- or _money(item.credit_difference) != 0
269
- or _money(item.rollforward_difference) != 0):
303
+ debit_difference = _sum_exact([
304
+ _money(item.transaction_debit_turnover),
305
+ -_money(item.source_debit_turnover),
306
+ ])
307
+ credit_difference = _sum_exact([
308
+ _money(item.transaction_credit_turnover),
309
+ -_money(item.source_credit_turnover),
310
+ ])
311
+ rollforward = _sum_exact([
312
+ source_values[0], source_values[1], -source_values[2], -source_values[3],
313
+ ])
314
+ if (debit_difference != 0 or credit_difference != 0 or rollforward != 0
315
+ or _money(item.debit_difference) != debit_difference
316
+ or _money(item.credit_difference) != credit_difference
317
+ or _money(item.rollforward_difference) != rollforward):
318
totals_valid = False
319
opening_values.append(source_values[0])
320
closing_values.append(source_values[3])