arena/094f314c-aram
add-index-html-10623938925177053894
arena/094f314c-aram
chatgpt-phase1b2-autopilot
codex/adr-future-connectors-pi-v1
codex/aram-account-note-source-trace-20261031
codex/aram-actions-node24-maintenance-20261031
codex/aram-analyst-review-workbook-20261031
codex/aram-bs-candidate-statement-20261031
codex/aram-cash-flow-direct-candidate-20261031
codex/aram-clean-reference-year-integrity-20261030
codex/aram-cross-statement-controls-20261031
codex/aram-equity-changes-candidate-20261031
codex/aram-five-statement-cli-20261031
codex/aram-five-statement-integration-20261031
codex/aram-five-statement-orchestrator-20261031
codex/aram-gate3-coverage-target-guard-20261030
codex/aram-gate3-hidden-xlsx-guard-20261030
codex/aram-gate3-integration-validation-20261030
codex/aram-gate3-mapping-change-preview-20261030
codex/aram-gate3-stale-note-guard-20261030
codex/aram-income-statement-candidate-20261031
codex/aram-manifest-referential-integrity-20261031
codex/aram-mapping-source-parser-20261101
codex/aram-multidimensional-mapping-20261101
codex/aram-multistatement-mapping-20261031
codex/aram-pipeline-manifest-schema-20261031
codex/aram-readonly-mapping-coverage-20261031
codex/aram-reconciliation-review-gate-20261031
codex/aram-same-flush-fiscal-lock-20261030
codex/aram-synthetic-windows-demo-20261031
codex/aram-trial-balance-diagnostic-20261031
codex/aram-windows-background-detail-search-20261031
codex/aram-windows-detail-review-20261031
codex/aram-windows-five-statement-pilot-20261031
codex/aram-windows-statement-figures-20261031
codex/gate-2a-pilot-workbook
codex/gate-2a-pilot-workbook-6864754244772568979
codex/gate3-authenticated-principal-boundary-20261030
codex/gate3-authored-draft-staging-20261030
codex/gate3-carry-forward-preview-20261023
codex/gate3-draft-staging-audit-20261030
codex/gate3-mapping-batch-freshness-20261030
codex/gate3-mapping-batch-review-20261030
codex/gate3-mapping-core
codex/gate3-mapping-coverage-20261023
codex/gate3-mapping-excel-20261023
codex/gate3-note-mapping-review-20261023
codex/gate3-signed-year-scope-20261030
codex/gate3-unapproved-proposal-storage-20261030
codex/payvast-mapping-discovery-20261023
codex/phase-2a-backend-foundation
codex/phase-2b-post-merge-record
codex/phase-2b-source-registration
codex/phase-2b-source-registration-12955190277775369594
codex/post-gate2a-control-reconciliation
codex/post-pr18-acceptance-record
codex/project-control-foundation
codex/t1-repo-hygiene-20261023
codex/t2-ci-hardening-20261023
codex/t3-explicit-migrations-20261023
codex/t6a-readonly-diagnostics-20261023
codex/v1-01-safety-gate-hardening
codex/v1-year-isolation-followup-20261023
codex/windows-native-pilot-shell-20261030
docs/aram-master-control-plan-20261111
docs/aram-roadmap-dashboard-20261030
docs/financial-statement-reconciliation-1402-1404-20261110
feat/gate3-governed-mapping-20261101
feat/gate3-package9-1-security-20261103
feat/gate3-package9-isolated-20261103
fix/password-unicode-validation-20261111
governance/aram-5-1-3-controls-20261030
governance/role-based-operating-model
jules-1168663913887856558-73b8a885
jules-gate-2b-mapping-workflow-foundation-4137724387867065330
phase-1b1-audit-report-7365389436990903239
phase-2a-backend-foundation-10237597358138521261
phase1b2-postmerge-metadata-fix
refactor/drg-parent-year-id-15088443704684163304
source-data-cleanup
| 1 | """Blocking financial controls C-01..C-04 (booklet section 7, debt D-02). |
| 2 | |
| 3 | Pure, deterministic functions over balance rows and active mappings. Each |
| 4 | returns a :class:`ControlResult` with a Persian message and the affected |
| 5 | accounts/rows. ERROR severity blocks approval (P-09); these are not ignorable |
| 6 | warnings. Sign convention is debit-positive (ADR-013, evidence: 252892/252898 |
| 7 | canonical rows satisfy C-01 under debit-positive vs 37% under credit-positive). |
| 8 | """ |
| 9 | |
| 10 | from __future__ import annotations |
| 11 | |
| 12 | from dataclasses import dataclass, field |
| 13 | from decimal import Decimal |
| 14 | |
| 15 | _ZERO = Decimal(0) |
| 16 | |
| 17 | |
| 18 | @dataclass(frozen=True) |
| 19 | class BalanceRow: |
| 20 | """One ledger balance row (all amounts exact Decimal rial).""" |
| 21 | |
| 22 | row_number: int |
| 23 | account_code: str |
| 24 | opening: Decimal |
| 25 | debit: Decimal |
| 26 | credit: Decimal |
| 27 | closing: Decimal |
| 28 | |
| 29 | |
| 30 | @dataclass(frozen=True) |
| 31 | class MappingTarget: |
| 32 | """One active mapping target for an account; weight is a percentage.""" |
| 33 | |
| 34 | target: str |
| 35 | weight: Decimal = Decimal(100) |
| 36 | |
| 37 | |
| 38 | @dataclass(frozen=True) |
| 39 | class ControlResult: |
| 40 | code: str |
| 41 | severity: str |
| 42 | passed: bool |
| 43 | message_fa: str |
| 44 | affected: tuple[str, ...] = field(default_factory=tuple) |
| 45 | |
| 46 | |
| 47 | def _aggregate(rows: list[BalanceRow]) -> dict[str, dict[str, Decimal]]: |
| 48 | totals: dict[str, dict[str, Decimal]] = {} |
| 49 | for row in rows: |
| 50 | bucket = totals.setdefault( |
| 51 | row.account_code, |
| 52 | {"opening": _ZERO, "debit": _ZERO, "credit": _ZERO, "closing": _ZERO}, |
| 53 | ) |
| 54 | bucket["opening"] += row.opening |
| 55 | bucket["debit"] += row.debit |
| 56 | bucket["credit"] += row.credit |
| 57 | bucket["closing"] += row.closing |
| 58 | return totals |
| 59 | |
| 60 | |
| 61 | def check_c01(rows: list[BalanceRow]) -> ControlResult: |
| 62 | """C-01: every row satisfies opening + debit - credit - closing = 0.""" |
| 63 | bad = tuple( |
| 64 | str(r.row_number) for r in rows if r.opening + r.debit - r.credit - r.closing != _ZERO |
| 65 | ) |
| 66 | return ControlResult( |
| 67 | code="C-01", |
| 68 | severity="ERROR", |
| 69 | passed=not bad, |
| 70 | message_fa=( |
| 71 | "همهٔ ردیفها: ابتدای دوره + بدهکار − بستانکار − پایان دوره = ۰" |
| 72 | if not bad |
| 73 | else f"{len(bad)} ردیف معادلهٔ مانده را نقض میکنند" |
| 74 | ), |
| 75 | affected=bad, |
| 76 | ) |
| 77 | |
| 78 | |
| 79 | def check_c02(rows: list[BalanceRow]) -> ControlResult: |
| 80 | """C-02: trial balance — total debit == total credit and sum(closing) == 0.""" |
| 81 | total_debit = sum((r.debit for r in rows), _ZERO) |
| 82 | total_credit = sum((r.credit for r in rows), _ZERO) |
| 83 | total_closing = sum((r.closing for r in rows), _ZERO) |
| 84 | passed = total_debit == total_credit and total_closing == _ZERO |
| 85 | return ControlResult( |
| 86 | code="C-02", |
| 87 | severity="ERROR", |
| 88 | passed=passed, |
| 89 | message_fa=( |
| 90 | "تراز کل برقرار است: جمع بدهکار = جمع بستانکار و جمع ماندهها صفر" |
| 91 | if passed |
| 92 | else "تراز کل نقض شده است: جمع بدهکار ≠ جمع بستانکار یا جمع ماندهها ≠ صفر" |
| 93 | ), |
| 94 | affected=() if passed else ("trial_balance",), |
| 95 | ) |
| 96 | |
| 97 | |
| 98 | def _is_active(bucket: dict[str, Decimal]) -> bool: |
| 99 | return any(bucket[k] != _ZERO for k in ("opening", "debit", "credit", "closing")) |
| 100 | |
| 101 | |
| 102 | def check_c03(rows: list[BalanceRow], mappings: dict[str, list[MappingTarget]]) -> ControlResult: |
| 103 | """C-03: each active account has exactly one active mapping (or 100% split).""" |
| 104 | bad: list[str] = [] |
| 105 | for code, bucket in sorted(_aggregate(rows).items()): |
| 106 | if not _is_active(bucket): |
| 107 | continue |
| 108 | targets = mappings.get(code, []) |
| 109 | if len(targets) == 0: |
| 110 | bad.append(code) |
| 111 | elif len(targets) > 1: |
| 112 | if sum((t.weight for t in targets), _ZERO) != Decimal(100): |
| 113 | bad.append(code) |
| 114 | return ControlResult( |
| 115 | code="C-03", |
| 116 | severity="ERROR", |
| 117 | passed=not bad, |
| 118 | message_fa=( |
| 119 | "هر حساب فعال دقیقاً یک نگاشت فعال دارد (یا تقسیم ۱۰۰٪)" |
| 120 | if not bad |
| 121 | else f"{len(bad)} حساب فعال نگاشت معتبر ندارند" |
| 122 | ), |
| 123 | affected=tuple(bad), |
| 124 | ) |
| 125 | |
| 126 | |
| 127 | def check_c04(rows: list[BalanceRow], mappings: dict[str, list[MappingTarget]]) -> ControlResult: |
| 128 | """C-04: unmapped active accounts enter the unknown queue and block approval.""" |
| 129 | queue = tuple( |
| 130 | code |
| 131 | for code, bucket in sorted(_aggregate(rows).items()) |
| 132 | if _is_active(bucket) and not mappings.get(code) |
| 133 | ) |
| 134 | return ControlResult( |
| 135 | code="C-04", |
| 136 | severity="ERROR", |
| 137 | passed=not queue, |
| 138 | message_fa=( |
| 139 | "صف حسابهای ناشناخته خالی است" |
| 140 | if not queue |
| 141 | else f"{len(queue)} حساب بدون نگاشت در صف ناشناخته؛ تا تعیین تکلیف مانع تأیید" |
| 142 | ), |
| 143 | affected=queue, |
| 144 | ) |
| 145 | |
| 146 | |
| 147 | def run_controls( |
| 148 | rows: list[BalanceRow], mappings: dict[str, list[MappingTarget]] |
| 149 | ) -> list[ControlResult]: |
| 150 | """Run the Phase-0 blocking control set in stable order.""" |
| 151 | return [check_c01(rows), check_c02(rows), check_c03(rows, mappings), check_c04(rows, mappings)] |
| 152 | |
| 153 | |
| 154 | __all__ = [ |
| 155 | "BalanceRow", |
| 156 | "MappingTarget", |
| 157 | "ControlResult", |
| 158 | "check_c01", |
| 159 | "check_c02", |
| 160 | "check_c03", |
| 161 | "check_c04", |
| 162 | "run_controls", |
| 163 | ] |