fix(control): tie every equity account movement to the matching trial-balance row
davoodmehraban89 committed
Oct 31, 2026 at 11:28 UTC
2652e64d142e1670179eec270ff79e6681f5eac9
1 file changed
+23
-4
backend/src/aram_backend/cross_statement_reconciliation.py
+23
-4
@@ -256,12 +256,22 @@ def _income_detail_bridge(income: IncomeStatementCandidate) -> bool:
256
)
257
258
259
-def _equity_detail_bridge(equity: EquityChangesCandidate) -> bool:
260
- if (not isinstance(equity.note_movements, tuple) or not equity.note_movements
259
+def _equity_detail_bridge(
260
+ equity: EquityChangesCandidate, trial: TrialBalanceDiagnostic,
261
+) -> bool:
262
+ if (not isinstance(trial.accounts, tuple)
263
+ or not isinstance(equity.note_movements, tuple) or not equity.note_movements
264
or not isinstance(equity.account_movements, tuple)
265
or not equity.account_movements
266
or not isinstance(equity.component_totals, tuple)):
267
return False
268
+ trial_by_code = {
269
+ account.account_code: account
270
+ for account in trial.accounts
271
+ if isinstance(account, TrialBalanceAccount)
272
+ }
273
+ if len(trial_by_code) != len(trial.accounts):
274
+ return False
275
accounts_by_note: dict[tuple[str, str, str], list[EquityAccountMovement]] = {}
276
seen_accounts: set[str] = set()
277
for account in equity.account_movements:
@@ -290,7 +300,16 @@ def _equity_detail_bridge(equity: EquityChangesCandidate) -> bool:
300
credit = _money(account.credit_turnover)
301
movement = _money(account.movement_amount)
302
closing = _money(account.closing_balance)
293
- if (debit < 0 or credit < 0
303
+ source = trial_by_code.get(code)
304
+ if (source is None
305
+ or account.account_name != source.account_name
306
+ or account.source_excel_row != source.excel_row
307
+ or account.source_row != source.source_row
308
+ or opening * account.sign_multiplier != _money(source.opening)
309
+ or debit != _money(source.debit)
310
+ or credit != _money(source.credit)
311
+ or closing * account.sign_multiplier != _money(source.closing)
312
+ or debit < 0 or credit < 0
313
or movement != _sum_exact([debit, -credit]) * account.sign_multiplier
314
or _sum_exact([opening, movement, -closing]) != 0):
315
return False
@@ -791,7 +810,7 @@ def reconcile_cross_statements(
810
-_money(equity_changes.closing_equity_total),
811
])
812
equity_rollforward_valid = (
794
- _equity_detail_bridge(equity_changes)
813
+ _equity_detail_bridge(equity_changes, trial_balance)
814
and equity_rollforward == 0
815
and _money(equity_changes.rollforward_difference) == 0
816
)