Mirrored from https://github.com/davoodmehraban89/Aram.git
Synced Dec 3, 22:00 UTC
feat(desktop): expose bounded statement line, transaction and account drill-down
davoodmehraban89 committed
Oct 31, 2026 at 21:36 UTC
865253a99ffbbc98ba99cb114ce51137596a2604
1 file changed
+179
backend/src/aram_backend/desktop_five_statement_details.py
new
+179
@@ -0,0 +1,179 @@
1
+"""Bounded, read-only source-detail rows for the ARAM desktop review."""
2
+from __future__ import annotations
3
+
4
+from dataclasses import dataclass
5
+
6
+from aram_backend.coverage_preview import CoverageError
7
+from aram_backend.desktop_five_statement_integrity import validate_desktop_pipeline_result
8
+from aram_backend.five_statement_pipeline import FiveStatementPipelineResult
9
+from aram_backend.reconciliation_review import _money
10
+
11
+MAX_DETAIL_ROWS = 75_000
12
+
13
+
14
+@dataclass(frozen=True)
15
+class PreviewDetail:
16
+ statement: str
17
+ item: str
18
+ reference: str
19
+ source_detail: str
20
+ opening_or_source: str
21
+ debit_or_inflow: str
22
+ credit_or_outflow: str
23
+ movement: str
24
+ closing_or_presentation: str
25
+ trace: str
26
+ state: str
27
+
28
+ def values(self) -> tuple[str, ...]:
29
+ return (
30
+ self.statement, self.item, self.reference, self.source_detail,
31
+ self.opening_or_source, self.debit_or_inflow, self.credit_or_outflow,
32
+ self.movement, self.closing_or_presentation, self.trace, self.state,
33
+ )
34
+
35
+
36
+def _text(value: object, label: str, *, blank_ok: bool = False) -> str:
37
+ if (not isinstance(value, str) or len(value) > 1024
38
+ or value != value.strip() or (not value and not blank_ok)
39
+ or any(ord(char) < 32 or ord(char) == 127 for char in value)):
40
+ raise CoverageError("DESKTOP_DETAIL_" + label + "_INVALID")
41
+ return value
42
+
43
+
44
+def _amount(value: object, label: str) -> str:
45
+ try:
46
+ return format(_money(value), ",f")
47
+ except CoverageError as exc:
48
+ raise CoverageError("DESKTOP_DETAIL_" + label + "_INVALID") from exc
49
+
50
+
51
+def _optional_amount(value: object, label: str) -> str:
52
+ if value is None:
53
+ return ""
54
+ return _amount(value, label)
55
+
56
+
57
+def _row(
58
+ statement: str, item: object, reference: object, source_detail: object,
59
+ opening: object = None, debit: object = None, credit: object = None,
60
+ movement: object = None, closing: object = None, trace: object = "",
61
+ state: str = "",
62
+) -> PreviewDetail:
63
+ return PreviewDetail(
64
+ statement=_text(statement, "STATEMENT"),
65
+ item=_text(item, "ITEM"),
66
+ reference=_text(reference, "REFERENCE", blank_ok=True),
67
+ source_detail=_text(source_detail, "SOURCE", blank_ok=True),
68
+ opening_or_source=_optional_amount(opening, "OPENING"),
69
+ debit_or_inflow=_optional_amount(debit, "DEBIT"),
70
+ credit_or_outflow=_optional_amount(credit, "CREDIT"),
71
+ movement=_optional_amount(movement, "MOVEMENT"),
72
+ closing_or_presentation=_optional_amount(closing, "CLOSING"),
73
+ trace=_text(trace, "TRACE", blank_ok=True),
74
+ state=state,
75
+ )
76
+
77
+
78
+def build_preview_details(
79
+ result: FiveStatementPipelineResult,
80
+) -> tuple[PreviewDetail, ...]:
81
+ """Expose source/presentation detail without changing or exporting inputs."""
82
+ validate_desktop_pipeline_result(result)
83
+ state = (
84
+ "قابل بررسی؛ غیررسمی" if result.eligible_for_analyst_review
85
+ else "مسدود؛ صرفاً تشخیصی"
86
+ )
87
+ rows: list[PreviewDetail] = []
88
+
89
+ for line in result.balance_sheet.lines:
90
+ rows.append(_row(
91
+ "ترازنامه", line.line_label,
92
+ f"{line.statement_section} / یادداشت {line.note_number} / {line.subnote_code}",
93
+ line.source_closing_exposure, closing=line.presentation_amount,
94
+ trace=(
95
+ f"دسته: {line.category} | ضریب: {line.sign_multiplier} | "
96
+ f"مرجع: {line.note_reference_evidence} | سیاست: {line.policy_evidence_ref}"
97
+ ),
98
+ state=state,
99
+ ))
100
+ for line in result.income_statement.lines:
101
+ rows.append(_row(
102
+ "سود و زیان", line.line_label,
103
+ f"{line.statement_section} / یادداشت {line.note_number} / {line.subnote_code}",
104
+ line.source_closing_exposure, closing=line.presentation_amount,
105
+ trace=(
106
+ f"دسته: {line.category} | ضریب: {line.sign_multiplier} | "
107
+ f"مرجع: {line.note_reference_evidence} | سیاست: {line.policy_evidence_ref}"
108
+ ),
109
+ state=state,
110
+ ))
111
+ for line in result.equity_changes.note_movements:
112
+ rows.append(_row(
113
+ "تغییرات حقوق مالکانه", line.line_label,
114
+ f"{line.statement_section} / یادداشت {line.note_number} / {line.subnote_code}",
115
+ line.source_closing_exposure, opening=line.opening_balance,
116
+ movement=line.movement_amount, closing=line.closing_balance,
117
+ trace=(
118
+ f"جزء: {line.equity_component} | نوع تغییر: {line.movement_category} | "
119
+ f"مرجع: {line.note_reference_evidence} | سیاست: {line.policy_evidence_ref}"
120
+ ),
121
+ state=state,
122
+ ))
123
+ for line in result.equity_changes.account_movements:
124
+ rows.append(_row(
125
+ "تغییرات حقوق مالکانه", line.account_name,
126
+ f"حساب {line.account_code} / {line.statement_section} / "
127
+ f"یادداشت {line.note_number} / {line.subnote_code}",
128
+ f"ردیف اکسل {line.source_excel_row} / ردیف منبع {line.source_row}",
129
+ opening=line.opening_balance, debit=line.debit_turnover,
130
+ credit=line.credit_turnover, movement=line.movement_amount,
131
+ closing=line.closing_balance,
132
+ trace=(
133
+ f"جزء: {line.equity_component} | نوع تغییر: {line.movement_category} | "
134
+ f"سیاست: {line.policy_evidence_ref}"
135
+ ),
136
+ state=state,
137
+ ))
138
+ for line in result.cash_flow.cash_flow_lines:
139
+ rows.append(_row(
140
+ "جریان وجوه نقد", line.activity,
141
+ f"شناسه ردیف {line.line_id} / سند {line.transaction_id}",
142
+ f"تاریخ {line.posting_date} / حساب نقد {line.cash_account_code}",
143
+ debit=line.cash_inflow, credit=line.cash_outflow,
144
+ trace=(
145
+ f"طرف مقابل: {line.counterpart_account_code} | "
146
+ f"سند: {line.source_evidence_ref} | "
147
+ f"طبقهبندی: {line.classification_evidence_ref or 'ندارد'}"
148
+ ),
149
+ state=state,
150
+ ))
151
+ for account in result.cash_flow.account_reconciliations:
152
+ rows.append(_row(
153
+ "جریان وجوه نقد", account.account_name,
154
+ f"حساب نقد {account.account_code}",
155
+ f"ردیف منبع {account.source_row}",
156
+ opening=account.opening_balance,
157
+ debit=account.source_debit_turnover,
158
+ credit=account.source_credit_turnover,
159
+ movement=account.rollforward_difference,
160
+ closing=account.closing_balance,
161
+ trace=(
162
+ f"گردش بدهکار اسناد: {account.transaction_debit_turnover} | "
163
+ f"گردش بستانکار اسناد: {account.transaction_credit_turnover} | "
164
+ f"سیاست: {account.policy_evidence_ref}"
165
+ ),
166
+ state=state,
167
+ ))
168
+ for account in result.trial_balance.accounts:
169
+ rows.append(_row(
170
+ "تراز آزمایشی", account.account_name,
171
+ f"کد حساب {account.account_code}",
172
+ f"ردیف اکسل {account.excel_row} / ردیف منبع {account.source_row}",
173
+ opening=account.opening, debit=account.debit, credit=account.credit,
174
+ movement=account.debit_net_movement, closing=account.closing,
175
+ state=state,
176
+ ))
177
+ if len(rows) > MAX_DETAIL_ROWS:
178
+ raise CoverageError("DESKTOP_DETAIL_ROW_LIMIT_EXCEEDED")
179
+ return tuple(rows)