codex/aram-mapping-source-parser-20261101
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 | """Bounded, read-only source-detail rows for the ARAM desktop review.""" |
| 2 | from __future__ import annotations |
| 3 | |
| 4 | from dataclasses import dataclass |
| 5 | import unicodedata |
| 6 | |
| 7 | from aram_backend.coverage_preview import CoverageError |
| 8 | from aram_backend.desktop_five_statement_integrity import validate_desktop_pipeline_result |
| 9 | from aram_backend.five_statement_pipeline import FiveStatementPipelineResult |
| 10 | from aram_backend.reconciliation_review import _money |
| 11 | |
| 12 | MAX_DETAIL_ROWS = 75_000 |
| 13 | MAX_DETAIL_SEARCH_CHARS = 256 |
| 14 | DETAIL_STATEMENTS = ( |
| 15 | "همه", "ترازنامه", "سود و زیان", |
| 16 | "تغییرات حقوق مالکانه", "جریان وجوه نقد", "تراز آزمایشی", |
| 17 | ) |
| 18 | |
| 19 | |
| 20 | def _unsafe_display_character(char: str) -> bool: |
| 21 | codepoint = ord(char) |
| 22 | category = unicodedata.category(char) |
| 23 | return ( |
| 24 | category in {"Cc", "Cs"} |
| 25 | or (category == "Cf" and codepoint not in {0x200C, 0x200D}) |
| 26 | ) |
| 27 | |
| 28 | |
| 29 | @dataclass(frozen=True) |
| 30 | class PreviewDetail: |
| 31 | statement: str |
| 32 | item: str |
| 33 | reference: str |
| 34 | source_detail: str |
| 35 | opening_or_source: str |
| 36 | debit_or_inflow: str |
| 37 | credit_or_outflow: str |
| 38 | movement: str |
| 39 | closing_or_presentation: str |
| 40 | trace: str |
| 41 | state: str |
| 42 | |
| 43 | def values(self) -> tuple[str, ...]: |
| 44 | return ( |
| 45 | self.statement, self.item, self.reference, self.source_detail, |
| 46 | self.opening_or_source, self.debit_or_inflow, self.credit_or_outflow, |
| 47 | self.movement, self.closing_or_presentation, self.trace, self.state, |
| 48 | ) |
| 49 | |
| 50 | |
| 51 | def _text(value: object, label: str, *, blank_ok: bool = False) -> str: |
| 52 | if (not isinstance(value, str) or len(value) > 1024 |
| 53 | or value != value.strip() or (not value and not blank_ok) |
| 54 | or any(_unsafe_display_character(char) for char in value)): |
| 55 | raise CoverageError("DESKTOP_DETAIL_" + label + "_INVALID") |
| 56 | return value |
| 57 | |
| 58 | |
| 59 | def _note_reference(section: object, note: object, subnote: object) -> str: |
| 60 | section_text = _text(section, "SECTION") |
| 61 | note_text = _text(note, "NOTE") |
| 62 | subnote_text = _text(subnote, "SUBNOTE", blank_ok=True) |
| 63 | parts = [section_text, "یادداشت " + note_text] |
| 64 | if subnote_text: |
| 65 | parts.append("زیرعنوان " + subnote_text) |
| 66 | return " / ".join(parts) |
| 67 | |
| 68 | |
| 69 | def _amount(value: object, label: str) -> str: |
| 70 | try: |
| 71 | return format(_money(value), ",f") |
| 72 | except CoverageError as exc: |
| 73 | raise CoverageError("DESKTOP_DETAIL_" + label + "_INVALID") from exc |
| 74 | |
| 75 | |
| 76 | def _optional_amount(value: object, label: str) -> str: |
| 77 | if value is None: |
| 78 | return "" |
| 79 | return _amount(value, label) |
| 80 | |
| 81 | |
| 82 | def _row( |
| 83 | statement: str, item: object, reference: object, source_detail: object, |
| 84 | opening: object = None, debit: object = None, credit: object = None, |
| 85 | movement: object = None, closing: object = None, trace: object = "", |
| 86 | state: str = "", source_amount: bool = False, |
| 87 | ) -> PreviewDetail: |
| 88 | source_value = ( |
| 89 | _amount(source_detail, "SOURCE") if source_amount |
| 90 | else _text(source_detail, "SOURCE", blank_ok=True) |
| 91 | ) |
| 92 | return PreviewDetail( |
| 93 | statement=_text(statement, "STATEMENT"), |
| 94 | item=_text(item, "ITEM"), |
| 95 | reference=_text(reference, "REFERENCE", blank_ok=True), |
| 96 | source_detail=source_value, |
| 97 | opening_or_source=_optional_amount(opening, "OPENING"), |
| 98 | debit_or_inflow=_optional_amount(debit, "DEBIT"), |
| 99 | credit_or_outflow=_optional_amount(credit, "CREDIT"), |
| 100 | movement=_optional_amount(movement, "MOVEMENT"), |
| 101 | closing_or_presentation=_optional_amount(closing, "CLOSING"), |
| 102 | trace=_text(trace, "TRACE", blank_ok=True), |
| 103 | state=state, |
| 104 | ) |
| 105 | |
| 106 | |
| 107 | def filter_preview_details( |
| 108 | rows: tuple[PreviewDetail, ...], *, statement: str, query: str, |
| 109 | ) -> tuple[PreviewDetail, ...]: |
| 110 | """Filter bounded detail rows; call from a worker thread, never Tk. |
| 111 | |
| 112 | The helper deliberately avoids joining all 11 columns into a second |
| 113 | temporary string. It searches one already-bounded cell at a time. |
| 114 | """ |
| 115 | if type(rows) is not tuple or len(rows) > MAX_DETAIL_ROWS: |
| 116 | raise CoverageError("DESKTOP_DETAIL_SEARCH_ROWS_INVALID") |
| 117 | if not isinstance(statement, str) or statement not in DETAIL_STATEMENTS: |
| 118 | raise CoverageError("DESKTOP_DETAIL_SEARCH_STATEMENT_INVALID") |
| 119 | if (not isinstance(query, str) or len(query) > MAX_DETAIL_SEARCH_CHARS |
| 120 | or any(_unsafe_display_character(char) for char in query)): |
| 121 | raise CoverageError("DESKTOP_DETAIL_SEARCH_QUERY_INVALID") |
| 122 | needle = query.strip().casefold() |
| 123 | matches: list[PreviewDetail] = [] |
| 124 | for row in rows: |
| 125 | if type(row) is not PreviewDetail: |
| 126 | raise CoverageError("DESKTOP_DETAIL_SEARCH_ROW_INVALID") |
| 127 | values = row.values() |
| 128 | if len(values) != 11 or any( |
| 129 | not isinstance(value, str) or len(value) > 1024 |
| 130 | or value != value.strip() |
| 131 | or any(_unsafe_display_character(char) for char in value) |
| 132 | for value in values |
| 133 | ): |
| 134 | raise CoverageError("DESKTOP_DETAIL_SEARCH_ROW_INVALID") |
| 135 | if statement != "همه" and row.statement != statement: |
| 136 | continue |
| 137 | if not needle or any( |
| 138 | isinstance(value, str) and needle in value.casefold() |
| 139 | for value in row.values() |
| 140 | ): |
| 141 | matches.append(row) |
| 142 | return tuple(matches) |
| 143 | |
| 144 | |
| 145 | def build_preview_details( |
| 146 | result: FiveStatementPipelineResult, |
| 147 | ) -> tuple[PreviewDetail, ...]: |
| 148 | """Expose source/presentation detail without changing or exporting inputs.""" |
| 149 | validate_desktop_pipeline_result(result) |
| 150 | state = ( |
| 151 | "قابل بررسی؛ غیررسمی" if result.eligible_for_analyst_review |
| 152 | else "مسدود؛ صرفاً تشخیصی" |
| 153 | ) |
| 154 | rows: list[PreviewDetail] = [] |
| 155 | |
| 156 | for line in result.balance_sheet.lines: |
| 157 | rows.append(_row( |
| 158 | "ترازنامه", line.line_label, |
| 159 | _note_reference(line.statement_section, line.note_number, line.subnote_code), |
| 160 | line.source_closing_exposure, closing=line.presentation_amount, |
| 161 | source_amount=True, |
| 162 | trace=( |
| 163 | f"دسته: {line.category} | ضریب: {line.sign_multiplier} | " |
| 164 | f"مرجع: {line.note_reference_evidence} | سیاست: {line.policy_evidence_ref}" |
| 165 | ), |
| 166 | state=state, |
| 167 | )) |
| 168 | for line in result.income_statement.lines: |
| 169 | rows.append(_row( |
| 170 | "سود و زیان", line.line_label, |
| 171 | _note_reference(line.statement_section, line.note_number, line.subnote_code), |
| 172 | line.source_closing_exposure, closing=line.presentation_amount, |
| 173 | source_amount=True, |
| 174 | trace=( |
| 175 | f"دسته: {line.category} | ضریب: {line.sign_multiplier} | " |
| 176 | f"مرجع: {line.note_reference_evidence} | سیاست: {line.policy_evidence_ref}" |
| 177 | ), |
| 178 | state=state, |
| 179 | )) |
| 180 | for line in result.equity_changes.note_movements: |
| 181 | rows.append(_row( |
| 182 | "تغییرات حقوق مالکانه", line.line_label, |
| 183 | _note_reference(line.statement_section, line.note_number, line.subnote_code), |
| 184 | line.source_closing_exposure, opening=line.opening_balance, |
| 185 | source_amount=True, |
| 186 | movement=line.movement_amount, closing=line.closing_balance, |
| 187 | trace=( |
| 188 | f"جزء: {line.equity_component} | نوع تغییر: {line.movement_category} | " |
| 189 | f"مرجع: {line.note_reference_evidence} | سیاست: {line.policy_evidence_ref}" |
| 190 | ), |
| 191 | state=state, |
| 192 | )) |
| 193 | for line in result.equity_changes.account_movements: |
| 194 | rows.append(_row( |
| 195 | "تغییرات حقوق مالکانه", line.account_name, |
| 196 | f"حساب {line.account_code} / " + _note_reference( |
| 197 | line.statement_section, line.note_number, line.subnote_code, |
| 198 | ), |
| 199 | f"ردیف اکسل {line.source_excel_row} / ردیف منبع {line.source_row}", |
| 200 | opening=line.opening_balance, debit=line.debit_turnover, |
| 201 | credit=line.credit_turnover, movement=line.movement_amount, |
| 202 | closing=line.closing_balance, |
| 203 | trace=( |
| 204 | f"جزء: {line.equity_component} | نوع تغییر: {line.movement_category} | " |
| 205 | f"سیاست: {line.policy_evidence_ref}" |
| 206 | ), |
| 207 | state=state, |
| 208 | )) |
| 209 | for line in result.cash_flow.cash_flow_lines: |
| 210 | rows.append(_row( |
| 211 | "جریان وجوه نقد", line.activity, |
| 212 | f"شناسه ردیف {line.line_id} / سند {line.transaction_id}", |
| 213 | f"تاریخ {line.posting_date} / حساب نقد {line.cash_account_code}", |
| 214 | debit=line.cash_inflow, credit=line.cash_outflow, |
| 215 | trace=( |
| 216 | f"طرف مقابل: {line.counterpart_account_code} | " |
| 217 | f"سند: {line.source_evidence_ref} | " |
| 218 | f"طبقهبندی: {line.classification_evidence_ref or 'ندارد'}" |
| 219 | ), |
| 220 | state=state, |
| 221 | )) |
| 222 | for account in result.cash_flow.account_reconciliations: |
| 223 | rows.append(_row( |
| 224 | "جریان وجوه نقد", account.account_name, |
| 225 | f"حساب نقد {account.account_code}", |
| 226 | f"ردیف منبع {account.source_row}", |
| 227 | opening=account.opening_balance, |
| 228 | debit=account.source_debit_turnover, |
| 229 | credit=account.source_credit_turnover, |
| 230 | movement=account.rollforward_difference, |
| 231 | closing=account.closing_balance, |
| 232 | trace=( |
| 233 | f"گردش بدهکار اسناد: {account.transaction_debit_turnover} | " |
| 234 | f"گردش بستانکار اسناد: {account.transaction_credit_turnover} | " |
| 235 | f"سیاست: {account.policy_evidence_ref}" |
| 236 | ), |
| 237 | state=state, |
| 238 | )) |
| 239 | for account in result.trial_balance.accounts: |
| 240 | rows.append(_row( |
| 241 | "تراز آزمایشی", account.account_name, |
| 242 | f"کد حساب {account.account_code}", |
| 243 | f"ردیف اکسل {account.excel_row} / ردیف منبع {account.source_row}", |
| 244 | opening=account.opening, debit=account.debit, credit=account.credit, |
| 245 | movement=account.debit_net_movement, closing=account.closing, |
| 246 | state=state, |
| 247 | )) |
| 248 | # One explicit source-account->note mapping row, not an inferred |
| 249 | # presentation amount. The source closing exposure is shown in the |
| 250 | # "source" column only. An included workbook mapping is not a trusted |
| 251 | # accounting approval. |
| 252 | statement_names = { |
| 253 | "BS": "ترازنامه", |
| 254 | "IS": "سود و زیان", |
| 255 | "EQ": "تغییرات حقوق مالکانه", |
| 256 | "CF": "جریان وجوه نقد", |
| 257 | } |
| 258 | for mapping in result.mapping_coverage.mappings: |
| 259 | statement = statement_names.get(mapping.target_statement) |
| 260 | if statement is None: |
| 261 | raise CoverageError("DESKTOP_DETAIL_MAPPING_STATEMENT_INVALID") |
| 262 | rows.append(_row( |
| 263 | statement, |
| 264 | f"حساب {mapping.account_code} / {mapping.account_name}", |
| 265 | _note_reference( |
| 266 | mapping.target_section, mapping.target_note, |
| 267 | mapping.target_subnote, |
| 268 | ), |
| 269 | ( |
| 270 | f"اکسل {mapping.account_excel_row} / منبع {mapping.source_row}" |
| 271 | ), |
| 272 | opening=mapping.closing_exposure, |
| 273 | trace=( |
| 274 | f"نگاشت {mapping.mapping_id} / ردیف نگاشت " |
| 275 | f"{mapping.mapping_excel_row} | " |
| 276 | "وضعیت بر اساس فایل، فاقد تأیید مستقل" |
| 277 | ), |
| 278 | state=( |
| 279 | state if mapping.included |
| 280 | else "نگاشت مسدود؛ صرفاً تشخیصی" |
| 281 | ), |
| 282 | )) |
| 283 | |
| 284 | if len(rows) > MAX_DETAIL_ROWS: |
| 285 | raise CoverageError("DESKTOP_DETAIL_ROW_LIMIT_EXCEEDED") |
| 286 | return tuple(rows) |