@davoodmehraban89 / Aram / commits / e122c77
Fork 0

fix(control): tie cash-flow lines to each cash account and recompute TB row rollforwards

davoodmehraban89 committed Oct 31, 2026 at 11:22 UTC e122c77207e8b05f55d0a6b276492dbfe4d60697
1 file changed +99 -23
backend/src/aram_backend/cross_statement_reconciliation.py
+99 -23
@@ -430,23 +430,31 @@ def _cash_flow_bridge(cash: DirectCashFlowCandidate) -> bool:
430 line_ids: set[str] = set()
431 transfer_values: list[Decimal] = []
432 if (type(cash.transaction_count) is not int
433 + or not isinstance(cash.cash_flow_lines, tuple)
434 or cash.transaction_count != len(cash.cash_flow_lines)):
435 return False
436 for line in cash.cash_flow_lines:
437 if not isinstance(line, CashFlowLine):
438 return False
439 line_id = _text(line.line_id, "CASH_FLOW_LINE_ID")
439 - if line_id in line_ids:
440 + _text(line.transaction_id, "CASH_FLOW_TRANSACTION_ID")
441 + _text(line.posting_date, "CASH_FLOW_POSTING_DATE")
442 + cash_code = _text(line.cash_account_code, "CASH_FLOW_CASH_ACCOUNT")
443 + counterpart_code = _text(line.counterpart_account_code, "CASH_FLOW_COUNTERPART")
444 + activity = _text(line.activity, "CASH_FLOW_ACTIVITY")
445 + _text(line.source_evidence_ref, "CASH_FLOW_SOURCE_EVIDENCE")
446 + _text(line.classification_evidence_ref, "CASH_FLOW_CLASSIFICATION_EVIDENCE")
447 + if line_id in line_ids or cash_code == counterpart_code:
448 return False
449 line_ids.add(line_id)
450 inflow = _money(line.cash_inflow)
451 outflow = _money(line.cash_outflow)
452 if inflow < 0 or outflow < 0 or not ((inflow > 0) ^ (outflow > 0)):
453 return False
446 - if line.activity in categories:
447 - categories[line.activity]["inflows"].append(inflow)
448 - categories[line.activity]["outflows"].append(outflow)
449 - elif line.activity == "CASH_TRANSFER":
454 + if activity in categories:
455 + categories[activity]["inflows"].append(inflow)
456 + categories[activity]["outflows"].append(outflow)
457 + elif activity == "CASH_TRANSFER":
458 transfer_values.append(_sum_exact([inflow, outflow]))
459 else:
460 return False
@@ -488,13 +496,16 @@ def _cash_flow_bridge(cash: DirectCashFlowCandidate) -> bool:
496
497 def _trial_balance_bridge(trial: TrialBalanceDiagnostic) -> bool:
498 if (type(trial.account_count) is not int
491 - or trial.account_count != len(trial.accounts) or not trial.accounts):
499 + or not isinstance(trial.accounts, tuple)
500 + or trial.account_count != len(trial.accounts) or not trial.accounts
501 + or type(trial.balanced_for_analyst_review) is not bool):
502 return False
503 seen: set[str] = set()
504 openings: list[Decimal] = []
505 debits: list[Decimal] = []
506 credits: list[Decimal] = []
507 closings: list[Decimal] = []
508 + source_rows: set[int] = set()
509 for account in trial.accounts:
510 if not isinstance(account, TrialBalanceAccount):
511 return False
@@ -502,11 +513,18 @@ def _trial_balance_bridge(trial: TrialBalanceDiagnostic) -> bool:
513 if code in seen:
514 return False
515 seen.add(code)
516 + if (type(account.source_row) is not int or account.source_row <= 0
517 + or account.source_row in source_rows):
518 + return False
519 + source_rows.add(account.source_row)
520 + _text(account.account_name, "TRIAL_BALANCE_ACCOUNT_NAME")
521 opening = _money(account.opening)
522 debit = _money(account.debit)
523 credit = _money(account.credit)
524 closing = _money(account.closing)
509 - if _sum_exact([debit, -credit]) != _money(account.debit_net_movement):
525 + if (debit < 0 or credit < 0
526 + or _sum_exact([debit, -credit]) != _money(account.debit_net_movement)
527 + or _sum_exact([opening, debit, -credit, -closing]) != 0):
528 return False
529 openings.append(opening)
530 debits.append(debit)
@@ -536,34 +554,88 @@ def _cash_accounts_match_trial_balance(
554 findings_valid = True
555 totals_valid = True
556 if (type(trial.account_count) is not int
539 - or trial.account_count != len(trial.accounts)):
557 + or trial.account_count != len(trial.accounts)
558 + or type(cash.cash_account_count) is not int
559 + or not isinstance(cash.account_reconciliations, tuple)
560 + or cash.cash_account_count != len(cash.account_reconciliations)
561 + or cash.cash_account_count < 1
562 + or not isinstance(cash.cash_flow_lines, tuple)):
563 findings_valid = False
564 +
565 trial_by_code: dict[str, TrialBalanceAccount] = {}
566 for account in trial.accounts:
567 if (not isinstance(account, TrialBalanceAccount)
568 or not isinstance(account.account_code, str)
569 + or account.account_code != account.account_code.strip()
570 + or not account.account_code
571 or account.account_code in trial_by_code):
572 findings_valid = False
573 continue
574 trial_by_code[account.account_code] = account
575
550 - if (type(cash.cash_account_count) is not int
551 - or cash.cash_account_count != len(cash.account_reconciliations)):
552 - findings_valid = False
553 - seen: set[str] = set()
554 - opening_values: list[Decimal] = []
555 - closing_values: list[Decimal] = []
576 + cash_by_code: dict[str, CashAccountReconciliation] = {}
577 for item in cash.account_reconciliations:
578 if (not isinstance(item, CashAccountReconciliation)
579 or not isinstance(item.account_code, str)
559 - or item.account_code in seen):
580 + or item.account_code != item.account_code.strip()
581 + or not item.account_code or item.account_code in cash_by_code):
582 + findings_valid = False
583 + continue
584 + cash_by_code[item.account_code] = item
585 +
586 + line_debits: dict[str, list[Decimal]] = {
587 + code: [] for code in cash_by_code
588 + }
589 + line_credits: dict[str, list[Decimal]] = {
590 + code: [] for code in cash_by_code
591 + }
592 + for line in cash.cash_flow_lines:
593 + if not isinstance(line, CashFlowLine):
594 + findings_valid = False
595 + continue
596 + cash_code = line.cash_account_code
597 + counterpart_code = line.counterpart_account_code
598 + if (not isinstance(cash_code, str) or cash_code not in cash_by_code
599 + or not isinstance(counterpart_code, str)
600 + or counterpart_code not in trial_by_code):
601 findings_valid = False
602 continue
562 - seen.add(item.account_code)
563 - trial_account = trial_by_code.get(item.account_code)
603 + inflow = _money(line.cash_inflow)
604 + outflow = _money(line.cash_outflow)
605 + if inflow > 0:
606 + line_debits[cash_code].append(inflow)
607 + if outflow > 0:
608 + line_credits[cash_code].append(outflow)
609 + if line.activity == "CASH_TRANSFER":
610 + if counterpart_code not in cash_by_code:
611 + findings_valid = False
612 + continue
613 + if inflow > 0:
614 + line_credits[counterpart_code].append(inflow)
615 + elif outflow > 0:
616 + line_debits[counterpart_code].append(outflow)
617 + else:
618 + findings_valid = False
619 + elif counterpart_code in cash_by_code:
620 + findings_valid = False
621 +
622 + seen: set[str] = set()
623 + opening_values: list[Decimal] = []
624 + closing_values: list[Decimal] = []
625 + for code, item in cash_by_code.items():
626 + if code in seen:
627 + findings_valid = False
628 + continue
629 + seen.add(code)
630 + trial_account = trial_by_code.get(code)
631 if trial_account is None:
632 findings_valid = False
633 continue
634 + if (type(item.source_row) is not int or item.source_row <= 0
635 + or item.source_row != trial_account.source_row
636 + or item.account_name != trial_account.account_name):
637 + findings_valid = False
638 + _text(item.policy_evidence_ref, "CASH_ACCOUNT_POLICY_EVIDENCE")
639 source_values = (
640 _money(item.opening_balance), _money(item.source_debit_turnover),
641 _money(item.source_credit_turnover), _money(item.closing_balance),
@@ -572,16 +644,20 @@ def _cash_accounts_match_trial_balance(
644 _money(trial_account.opening), _money(trial_account.debit),
645 _money(trial_account.credit), _money(trial_account.closing),
646 )
575 - if (source_values != trial_values
576 - or item.source_row != trial_account.source_row):
647 + if source_values != trial_values:
648 findings_valid = False
649 + transaction_debit = _sum_exact(line_debits[code])
650 + transaction_credit = _sum_exact(line_credits[code])
651 + if (
652 + transaction_debit != _money(item.transaction_debit_turnover)
653 + or transaction_credit != _money(item.transaction_credit_turnover)
654 + ):
655 + totals_valid = False
656 debit_difference = _sum_exact([
579 - _money(item.transaction_debit_turnover),
580 - -_money(item.source_debit_turnover),
657 + transaction_debit, -_money(item.source_debit_turnover),
658 ])
659 credit_difference = _sum_exact([
583 - _money(item.transaction_credit_turnover),
584 - -_money(item.source_credit_turnover),
660 + transaction_credit, -_money(item.source_credit_turnover),
661 ])
662 rollforward = _sum_exact([
663 source_values[0], source_values[1], -source_values[2], -source_values[3],