feat(control): tie cash account balances and turnovers back to detailed trial balance
davoodmehraban89 committed
Oct 31, 2026 at 08:35 UTC
2b5993d21ef32815fe2abe3a1c7b1f9eb4764c17
1 file changed
+84
-6
backend/src/aram_backend/cross_statement_reconciliation.py
+84
-6
@@ -13,12 +13,16 @@ import re
13
from typing import Any, Sequence
14
15
from aram_backend.balance_sheet_candidate import BalanceSheetCandidate
16
-from aram_backend.cash_flow_direct_candidate import DirectCashFlowCandidate
16
+from aram_backend.cash_flow_direct_candidate import (
17
+ CashAccountReconciliation, DirectCashFlowCandidate,
18
+)
19
from aram_backend.coverage_preview import CoverageError
20
from aram_backend.equity_changes_candidate import EquityChangesCandidate
21
from aram_backend.income_statement_candidate import IncomeStatementCandidate
22
from aram_backend.reconciliation_review import _money, _sum_exact
21
-from aram_backend.trial_balance_diagnostic import TrialBalanceDiagnostic
23
+from aram_backend.trial_balance_diagnostic import (
24
+ TrialBalanceAccount, TrialBalanceDiagnostic,
25
+)
26
27
EXPECTED_KINDS = {
28
"balance_sheet": "UNAPPROVED_BALANCE_SHEET_PRESENTATION_CANDIDATE",
@@ -72,6 +76,8 @@ class CrossStatementReconciliation:
76
cash_flow_bridge_valid: bool
77
equity_rollforward_valid: bool
78
trial_balance_arithmetic_valid: bool
79
+ cash_accounts_match_trial_balance: bool
80
+ cash_account_totals_valid: bool
81
selected_cash_notes: tuple[SelectedCashBalanceNote, ...]
82
findings: tuple[str, ...]
83
eligible_for_analyst_review: bool
@@ -115,6 +121,12 @@ def _validate_candidate(candidate: object, label: str, expected_type: type) -> N
121
if any(getattr(candidate, flag, False) for flag in forbidden):
122
raise CoverageError("CROSS_STATEMENT_APPROVAL_PROVENANCE_INVALID")
123
_hash(candidate.source_sha256, label.upper() + "_SOURCE_HASH")
124
+ for hash_field in (
125
+ "note_references_sha256", "presentation_policies_sha256",
126
+ "transaction_ledger_sha256", "classification_policies_sha256",
127
+ ):
128
+ if hasattr(candidate, hash_field):
129
+ _hash(getattr(candidate, hash_field), label.upper() + "_" + hash_field.upper())
130
_text(candidate.company_code, label.upper() + "_COMPANY")
131
if type(candidate.fiscal_year) is not int or candidate.fiscal_year <= 0:
132
raise CoverageError("INVALID_CROSS_STATEMENT_" + label.upper() + "_YEAR")
@@ -174,6 +186,59 @@ def _trial_balance_bridge(trial: TrialBalanceDiagnostic) -> bool:
186
)
187
188
189
+def _cash_accounts_match_trial_balance(
190
+ cash: DirectCashFlowCandidate, trial: TrialBalanceDiagnostic,
191
+) -> tuple[bool, bool]:
192
+ findings_valid = True
193
+ totals_valid = True
194
+ if (type(trial.account_count) is not int
195
+ or trial.account_count != len(trial.accounts)):
196
+ findings_valid = False
197
+ trial_by_code: dict[str, TrialBalanceAccount] = {}
198
+ for account in trial.accounts:
199
+ if not isinstance(account, TrialBalanceAccount) or account.account_code in trial_by_code:
200
+ findings_valid = False
201
+ continue
202
+ trial_by_code[account.account_code] = account
203
+
204
+ if (type(cash.cash_account_count) is not int
205
+ or cash.cash_account_count != len(cash.account_reconciliations)):
206
+ findings_valid = False
207
+ seen: set[str] = set()
208
+ opening_values: list[Decimal] = []
209
+ closing_values: list[Decimal] = []
210
+ for item in cash.account_reconciliations:
211
+ if not isinstance(item, CashAccountReconciliation) or item.account_code in seen:
212
+ findings_valid = False
213
+ continue
214
+ seen.add(item.account_code)
215
+ trial_account = trial_by_code.get(item.account_code)
216
+ if trial_account is None:
217
+ findings_valid = False
218
+ continue
219
+ source_values = (
220
+ _money(item.opening_balance), _money(item.source_debit_turnover),
221
+ _money(item.source_credit_turnover), _money(item.closing_balance),
222
+ )
223
+ trial_values = (
224
+ _money(trial_account.opening), _money(trial_account.debit),
225
+ _money(trial_account.credit), _money(trial_account.closing),
226
+ )
227
+ if source_values != trial_values:
228
+ findings_valid = False
229
+ if (_money(item.debit_difference) != 0
230
+ or _money(item.credit_difference) != 0
231
+ or _money(item.rollforward_difference) != 0):
232
+ totals_valid = False
233
+ opening_values.append(source_values[0])
234
+ closing_values.append(source_values[3])
235
+
236
+ if (_sum_exact(opening_values) != _money(cash.opening_cash_and_equivalents)
237
+ or _sum_exact(closing_values) != _money(cash.closing_cash_and_equivalents)):
238
+ totals_valid = False
239
+ return findings_valid, totals_valid
240
+
241
+
242
def reconcile_cross_statements(
243
*, balance_sheet: BalanceSheetCandidate,
244
income_statement: IncomeStatementCandidate,
@@ -285,10 +350,13 @@ def reconcile_cross_statements(
350
if not equity_rollforward_valid:
351
findings.append("EQUITY_STATEMENT_ROLLFORWARD_INVALID")
352
288
- bs_lines = {
289
- (line.statement_section, line.note_number, line.subnote_code): line
290
- for line in balance_sheet.lines
291
- }
353
+ bs_lines = {}
354
+ for line in balance_sheet.lines:
355
+ key = (line.statement_section, line.note_number, line.subnote_code)
356
+ if key in bs_lines:
357
+ findings.append("DUPLICATE_BALANCE_SHEET_NOTE_LINE")
358
+ else:
359
+ bs_lines[key] = line
360
selected_lines: list[SelectedCashBalanceNote] = []
361
cash_amounts: list[Decimal] = []
362
for key in sorted(selected):
@@ -319,6 +387,14 @@ def reconcile_cross_statements(
387
if not cash_valid:
388
findings.append("CASH_FLOW_BRIDGE_INVALID")
389
390
+ cash_accounts_match_tb, cash_account_totals_valid = (
391
+ _cash_accounts_match_trial_balance(cash_flow, trial_balance)
392
+ )
393
+ if not cash_accounts_match_tb:
394
+ findings.append("CASH_ACCOUNTS_TRIAL_BALANCE_MISMATCH")
395
+ if not cash_account_totals_valid:
396
+ findings.append("CASH_ACCOUNT_TURNOVER_OR_TOTALS_INVALID")
397
+
398
trial_valid = _trial_balance_bridge(trial_balance)
399
if not trial_valid:
400
findings.append("TRIAL_BALANCE_ARITHMETIC_INVALID")
@@ -354,6 +430,8 @@ def reconcile_cross_statements(
430
cash_flow_bridge_valid=cash_valid,
431
equity_rollforward_valid=equity_rollforward_valid,
432
trial_balance_arithmetic_valid=trial_valid,
433
+ cash_accounts_match_trial_balance=cash_accounts_match_tb,
434
+ cash_account_totals_valid=cash_account_totals_valid,
435
selected_cash_notes=tuple(selected_lines),
436
findings=tuple(sorted(set(findings))),
437
eligible_for_analyst_review=not findings,