codex/aram-analyst-review-workbook-20261031
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
py 207 lines 8.39 KB
Raw
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
14
15 def _unsafe_display_character(char: str) -> bool:
16 codepoint = ord(char)
17 category = unicodedata.category(char)
18 return (
19 category in {"Cc", "Cs"}
20 or (category == "Cf" and codepoint not in {0x200C, 0x200D})
21 )
22
23
24 @dataclass(frozen=True)
25 class PreviewDetail:
26 statement: str
27 item: str
28 reference: str
29 source_detail: str
30 opening_or_source: str
31 debit_or_inflow: str
32 credit_or_outflow: str
33 movement: str
34 closing_or_presentation: str
35 trace: str
36 state: str
37
38 def values(self) -> tuple[str, ...]:
39 return (
40 self.statement, self.item, self.reference, self.source_detail,
41 self.opening_or_source, self.debit_or_inflow, self.credit_or_outflow,
42 self.movement, self.closing_or_presentation, self.trace, self.state,
43 )
44
45
46 def _text(value: object, label: str, *, blank_ok: bool = False) -> str:
47 if (not isinstance(value, str) or len(value) > 1024
48 or value != value.strip() or (not value and not blank_ok)
49 or any(_unsafe_display_character(char) for char in value)):
50 raise CoverageError("DESKTOP_DETAIL_" + label + "_INVALID")
51 return value
52
53
54 def _note_reference(section: object, note: object, subnote: object) -> str:
55 section_text = _text(section, "SECTION")
56 note_text = _text(note, "NOTE")
57 subnote_text = _text(subnote, "SUBNOTE", blank_ok=True)
58 parts = [section_text, "یادداشت " + note_text]
59 if subnote_text:
60 parts.append("زیرعنوان " + subnote_text)
61 return " / ".join(parts)
62
63
64 def _amount(value: object, label: str) -> str:
65 try:
66 return format(_money(value), ",f")
67 except CoverageError as exc:
68 raise CoverageError("DESKTOP_DETAIL_" + label + "_INVALID") from exc
69
70
71 def _optional_amount(value: object, label: str) -> str:
72 if value is None:
73 return ""
74 return _amount(value, label)
75
76
77 def _row(
78 statement: str, item: object, reference: object, source_detail: object,
79 opening: object = None, debit: object = None, credit: object = None,
80 movement: object = None, closing: object = None, trace: object = "",
81 state: str = "", source_amount: bool = False,
82 ) -> PreviewDetail:
83 source_value = (
84 _amount(source_detail, "SOURCE") if source_amount
85 else _text(source_detail, "SOURCE", blank_ok=True)
86 )
87 return PreviewDetail(
88 statement=_text(statement, "STATEMENT"),
89 item=_text(item, "ITEM"),
90 reference=_text(reference, "REFERENCE", blank_ok=True),
91 source_detail=source_value,
92 opening_or_source=_optional_amount(opening, "OPENING"),
93 debit_or_inflow=_optional_amount(debit, "DEBIT"),
94 credit_or_outflow=_optional_amount(credit, "CREDIT"),
95 movement=_optional_amount(movement, "MOVEMENT"),
96 closing_or_presentation=_optional_amount(closing, "CLOSING"),
97 trace=_text(trace, "TRACE", blank_ok=True),
98 state=state,
99 )
100
101
102 def build_preview_details(
103 result: FiveStatementPipelineResult,
104 ) -> tuple[PreviewDetail, ...]:
105 """Expose source/presentation detail without changing or exporting inputs."""
106 validate_desktop_pipeline_result(result)
107 state = (
108 "قابل بررسی؛ غیررسمی" if result.eligible_for_analyst_review
109 else "مسدود؛ صرفاً تشخیصی"
110 )
111 rows: list[PreviewDetail] = []
112
113 for line in result.balance_sheet.lines:
114 rows.append(_row(
115 "ترازنامه", line.line_label,
116 _note_reference(line.statement_section, line.note_number, line.subnote_code),
117 line.source_closing_exposure, closing=line.presentation_amount,
118 source_amount=True,
119 trace=(
120 f"دسته: {line.category} | ضریب: {line.sign_multiplier} | "
121 f"مرجع: {line.note_reference_evidence} | سیاست: {line.policy_evidence_ref}"
122 ),
123 state=state,
124 ))
125 for line in result.income_statement.lines:
126 rows.append(_row(
127 "سود و زیان", line.line_label,
128 _note_reference(line.statement_section, line.note_number, line.subnote_code),
129 line.source_closing_exposure, closing=line.presentation_amount,
130 source_amount=True,
131 trace=(
132 f"دسته: {line.category} | ضریب: {line.sign_multiplier} | "
133 f"مرجع: {line.note_reference_evidence} | سیاست: {line.policy_evidence_ref}"
134 ),
135 state=state,
136 ))
137 for line in result.equity_changes.note_movements:
138 rows.append(_row(
139 "تغییرات حقوق مالکانه", line.line_label,
140 _note_reference(line.statement_section, line.note_number, line.subnote_code),
141 line.source_closing_exposure, opening=line.opening_balance,
142 source_amount=True,
143 movement=line.movement_amount, closing=line.closing_balance,
144 trace=(
145 f"جزء: {line.equity_component} | نوع تغییر: {line.movement_category} | "
146 f"مرجع: {line.note_reference_evidence} | سیاست: {line.policy_evidence_ref}"
147 ),
148 state=state,
149 ))
150 for line in result.equity_changes.account_movements:
151 rows.append(_row(
152 "تغییرات حقوق مالکانه", line.account_name,
153 f"حساب {line.account_code} / " + _note_reference(
154 line.statement_section, line.note_number, line.subnote_code,
155 ),
156 f"ردیف اکسل {line.source_excel_row} / ردیف منبع {line.source_row}",
157 opening=line.opening_balance, debit=line.debit_turnover,
158 credit=line.credit_turnover, movement=line.movement_amount,
159 closing=line.closing_balance,
160 trace=(
161 f"جزء: {line.equity_component} | نوع تغییر: {line.movement_category} | "
162 f"سیاست: {line.policy_evidence_ref}"
163 ),
164 state=state,
165 ))
166 for line in result.cash_flow.cash_flow_lines:
167 rows.append(_row(
168 "جریان وجوه نقد", line.activity,
169 f"شناسه ردیف {line.line_id} / سند {line.transaction_id}",
170 f"تاریخ {line.posting_date} / حساب نقد {line.cash_account_code}",
171 debit=line.cash_inflow, credit=line.cash_outflow,
172 trace=(
173 f"طرف مقابل: {line.counterpart_account_code} | "
174 f"سند: {line.source_evidence_ref} | "
175 f"طبقه‌بندی: {line.classification_evidence_ref or 'ندارد'}"
176 ),
177 state=state,
178 ))
179 for account in result.cash_flow.account_reconciliations:
180 rows.append(_row(
181 "جریان وجوه نقد", account.account_name,
182 f"حساب نقد {account.account_code}",
183 f"ردیف منبع {account.source_row}",
184 opening=account.opening_balance,
185 debit=account.source_debit_turnover,
186 credit=account.source_credit_turnover,
187 movement=account.rollforward_difference,
188 closing=account.closing_balance,
189 trace=(
190 f"گردش بدهکار اسناد: {account.transaction_debit_turnover} | "
191 f"گردش بستانکار اسناد: {account.transaction_credit_turnover} | "
192 f"سیاست: {account.policy_evidence_ref}"
193 ),
194 state=state,
195 ))
196 for account in result.trial_balance.accounts:
197 rows.append(_row(
198 "تراز آزمایشی", account.account_name,
199 f"کد حساب {account.account_code}",
200 f"ردیف اکسل {account.excel_row} / ردیف منبع {account.source_row}",
201 opening=account.opening, debit=account.debit, credit=account.credit,
202 movement=account.debit_net_movement, closing=account.closing,
203 state=state,
204 ))
205 if len(rows) > MAX_DETAIL_ROWS:
206 raise CoverageError("DESKTOP_DETAIL_ROW_LIMIT_EXCEEDED")
207 return tuple(rows)