Mirrored from https://github.com/davoodmehraban89/Aram.git
Synced Dec 4, 14:15 UTC
dependabot/github_actions/actions/checkout-7
add-index-html-10623938925177053894
arena/094f314c-aram
arena/2d4eda07-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
dependabot/github_actions/actions/checkout-7
dependabot/github_actions/actions/setup-node-6
dependabot/github_actions/actions/setup-python-7
dependabot/github_actions/actions/upload-artifact-7
dependabot/npm_and_yarn/frontend/eslint-plugin-react-hooks-7.1.1
dependabot/npm_and_yarn/frontend/globals-17.15.0
dependabot/npm_and_yarn/frontend/vite-8.4.0
dependabot/npm_and_yarn/frontend/vitest-5.1.2
dependabot/npm_and_yarn/frontend/vitest/coverage-v8-5.1.2
dependabot/pip/backend/mypy-gte-1.13-and-lt-3
dependabot/pip/backend/pytest-cov-gte-6.0-and-lt-8
dependabot/pip/backend/setuptools-gte-75-and-lt-85
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 | """R3: official-statement parsing and built-vs-official comparison. |
| 2 | |
| 3 | Covers the four different official layouts, the (statement, note) key, the |
| 4 | display-sign policy, the per-row note assignment, and the C-13 feed. |
| 5 | """ |
| 6 | |
| 7 | from __future__ import annotations |
| 8 | |
| 9 | from decimal import Decimal |
| 10 | from io import BytesIO |
| 11 | |
| 12 | from openpyxl import Workbook |
| 13 | from sqlalchemy import select |
| 14 | |
| 15 | from aram_backend.domain.controls import check_c13 |
| 16 | from aram_backend.models import LedgerBalance, StatementLine |
| 17 | from aram_backend.services.official_parse import ( |
| 18 | parse_equity_matrix, |
| 19 | parse_note_statement, |
| 20 | parse_official, |
| 21 | ) |
| 22 | from aram_backend.services.statement_build import build_statements |
| 23 | from aram_backend.services.statement_compare import ( |
| 24 | compare_built_statements, |
| 25 | official_comparisons, |
| 26 | ) |
| 27 | from test_statement_build import _map, _note, _setup |
| 28 | |
| 29 | D = Decimal |
| 30 | |
| 31 | |
| 32 | def _official_bytes(): |
| 33 | """A miniature official workbook with the real layouts (BS/IS/EQ/CF).""" |
| 34 | wb = Workbook() |
| 35 | bs = wb.active |
| 36 | bs.title = "وضعيت مالي" |
| 37 | bs.append(["شرکت نمونه"]) |
| 38 | bs.append(["به تاریخ 29 اسفند 1404"]) # title also contains the year |
| 39 | bs.append([None, None, "يادداشت", None, None, "سال 1404"]) |
| 40 | bs.append([None, "دارایی", "6", "6", None, 100]) |
| 41 | bs.append([None, "بدهی", "10", "10", None, 60]) |
| 42 | |
| 43 | isx = wb.create_sheet("سودوزيان") |
| 44 | isx.append(["صورت سود و زیان"]) |
| 45 | isx.append([None, None, None, "يادداشت", None, "سال 1404"]) |
| 46 | isx.append([None, "درآمد", "5", "5", None, 250]) |
| 47 | isx.append([None, "هزینه", "7", "7", None, -200]) |
| 48 | |
| 49 | eq = wb.create_sheet("حقوق مالكانه") |
| 50 | eq.append(["صورت تغییرات حقوق مالکانه"]) |
| 51 | eq.append([None, None, "یادداشت", "سرمایه", None, "اندوخته", None, "جمع کل"]) |
| 52 | eq.append(["مانده تجدید ارایه شده در 1404/01/01", None, None, 40, None, 10, None, 50]) |
| 53 | eq.append(["مانده در 1404/12/29", None, None, 40, None, 15, None, 55]) |
| 54 | |
| 55 | cf = wb.create_sheet("جريان هاي نقدي") |
| 56 | cf.append(["صورت جریانهای نقدی"]) |
| 57 | cf.append([None, None, "يادداشت", None, "سال 1404"]) |
| 58 | cf.append([None, "نقد حاصل از عملیات", "23", None, 30]) |
| 59 | cf.append([None, "تسعیر ارز", "46268", None, 12]) |
| 60 | buf = BytesIO() |
| 61 | wb.save(buf) |
| 62 | return buf.getvalue() |
| 63 | |
| 64 | |
| 65 | class TestOfficialParsing: |
| 66 | def test_year_column_is_not_the_title_row(self, tmp_path): |
| 67 | path = tmp_path / "official.xlsx" |
| 68 | path.write_bytes(_official_bytes()) |
| 69 | figures = parse_note_statement(path, "BS", 1404) |
| 70 | assert {f.key: f.amount for f in figures} == {"6": D(100), "10": D(60)} |
| 71 | |
| 72 | def test_income_statement_notes_and_signs(self, tmp_path): |
| 73 | path = tmp_path / "official.xlsx" |
| 74 | path.write_bytes(_official_bytes()) |
| 75 | figures = {f.key: f.amount for f in parse_note_statement(path, "IS", 1404)} |
| 76 | assert figures == {"5": D(250), "7": D(-200)} |
| 77 | |
| 78 | def test_equity_matrix_yields_opening_changes_closing(self, tmp_path): |
| 79 | path = tmp_path / "official.xlsx" |
| 80 | path.write_bytes(_official_bytes()) |
| 81 | figures = {f.key: f.amount for f in parse_equity_matrix(path, 1404)} |
| 82 | assert figures["سرمایه:opening"] == D(40) |
| 83 | assert figures["سرمایه:closing"] == D(40) |
| 84 | assert figures["اندوخته:changes"] == D(5) |
| 85 | assert figures["جمع کل:closing"] == D(55) |
| 86 | |
| 87 | def test_cf_account_code_is_not_treated_as_a_note(self, tmp_path): |
| 88 | path = tmp_path / "official.xlsx" |
| 89 | path.write_bytes(_official_bytes()) |
| 90 | figures = {f.key: f for f in parse_note_statement(path, "CF", 1404)} |
| 91 | assert figures["23"].kind == "note" |
| 92 | assert figures["46268"].kind == "account_code" |
| 93 | |
| 94 | def test_all_four_statements_parse_from_bytes(self): |
| 95 | figures = parse_official(_official_bytes(), 1404) |
| 96 | assert {f.statement for f in figures} == {"BS", "IS", "EQ", "CF"} |
| 97 | |
| 98 | def test_missing_sheet_yields_nothing(self, tmp_path): |
| 99 | wb = Workbook() |
| 100 | wb.active.title = "unrelated" |
| 101 | path = tmp_path / "empty.xlsx" |
| 102 | wb.save(path) |
| 103 | assert parse_note_statement(path, "BS", 1404) == [] |
| 104 | assert parse_equity_matrix(path, 1404) == [] |
| 105 | |
| 106 | |
| 107 | class TestBuiltComparison: |
| 108 | def _seed(self, session): |
| 109 | year, source = _setup(session) |
| 110 | from test_approval_gate import _carry |
| 111 | |
| 112 | _carry(session, year, source, "148348", D(0), D(100_000_000), 3) |
| 113 | _carry(session, year, source, "241001", D(0), D(-60_000_000), 4) |
| 114 | n_a = _note(session, year, "6", "نقد", "BS", "Assets") |
| 115 | n_l = _note(session, year, "10", "پرداختنی", "BS", "Liabilities") |
| 116 | _map(session, year, "148348", n_a) |
| 117 | _map(session, year, "241001", n_l) |
| 118 | session.flush() |
| 119 | build_statements(session, year.id) |
| 120 | session.flush() |
| 121 | return year |
| 122 | |
| 123 | def test_matching_built_lines_report_no_delta(self, session): |
| 124 | year = self._seed(session) |
| 125 | report = compare_built_statements(session, year.id, _official_bytes(), 1404) |
| 126 | bs = {d.note: d for d in report.deltas if d.statement == "BS"} |
| 127 | assert bs["6"].match and bs["6"].built_million == 100 |
| 128 | assert bs["10"].match and bs["10"].built_million == 60 |
| 129 | assert report.counts()["BS"] == {"matched": 2, "total": 2} |
| 130 | |
| 131 | def test_note_keys_are_scoped_by_statement(self, session): |
| 132 | year = self._seed(session) |
| 133 | report = compare_built_statements(session, year.id, _official_bytes(), 1404) |
| 134 | keys = {(d.statement, d.note) for d in report.deltas} |
| 135 | assert ("BS", "6") in keys and ("IS", "5") in keys |
| 136 | # IS 5 has no built line: the gap is reported, never silently dropped. |
| 137 | is5 = next(d for d in report.deltas if (d.statement, d.note) == ("IS", "5")) |
| 138 | assert is5.status == "missing_in_build" and not is5.match |
| 139 | |
| 140 | def test_mismatch_carries_account_level_deltas(self, session): |
| 141 | year = self._seed(session) |
| 142 | row = session.scalar( |
| 143 | select(LedgerBalance).where( |
| 144 | LedgerBalance.year_id == year.id, LedgerBalance.account_full_code == "148348" |
| 145 | ) |
| 146 | ) |
| 147 | row.closing = D(90_000_000) |
| 148 | session.flush() |
| 149 | build_statements(session, year.id) |
| 150 | session.flush() |
| 151 | report = compare_built_statements(session, year.id, _official_bytes(), 1404) |
| 152 | bs6 = next(d for d in report.deltas if (d.statement, d.note) == ("BS", "6")) |
| 153 | assert not bs6.match and bs6.delta == -10 |
| 154 | assert bs6.accounts and bs6.accounts[0].account_code == "148348" |
| 155 | assert bs6.accounts[0].million_rounded == 90 |
| 156 | assert bs6.official_cell # provenance to the exact official cell |
| 157 | |
| 158 | def test_c13_feed_from_the_report(self, session): |
| 159 | year = self._seed(session) |
| 160 | report = compare_built_statements(session, year.id, _official_bytes(), 1404) |
| 161 | feed = official_comparisons(report) |
| 162 | assert feed and all(c.ours == c.official for c in feed) |
| 163 | assert check_c13(feed, "ci").passed |
| 164 | |
| 165 | |
| 166 | class TestStatementBasisAndRowNotes: |
| 167 | def test_statement_balance_basis_excludes_rows_without_one(self, session): |
| 168 | year, source = _setup(session) |
| 169 | from test_approval_gate import _carry |
| 170 | |
| 171 | _carry(session, year, source, "148348", D(0), D(500), 3) |
| 172 | _carry(session, year, source, "241001", D(0), D(0), 4) |
| 173 | rows = session.scalars(select(LedgerBalance).where(LedgerBalance.year_id == year.id)).all() |
| 174 | for r in rows: |
| 175 | if r.account_full_code == "241001": |
| 176 | r.statement_balance = D(100) # only this row feeds the statement |
| 177 | n = _note(session, year, "6", "نقد", "BS", "Assets") |
| 178 | _map(session, year, "148348", n) |
| 179 | _map(session, year, "241001", n) |
| 180 | session.flush() |
| 181 | build_statements(session, year.id) |
| 182 | session.flush() |
| 183 | line = session.scalar(select(StatementLine).where(StatementLine.target_note_id == n.id)) |
| 184 | # The 500 closing must NOT leak in: the import supplies statement balances. |
| 185 | assert line.mapped_total == D(100) |
| 186 | |
| 187 | def test_source_note_disambiguates_a_multi_note_account(self, session): |
| 188 | year, source = _setup(session) |
| 189 | from test_approval_gate import _carry |
| 190 | |
| 191 | _carry(session, year, source, "148348", D(0), D(100), 3) |
| 192 | _carry(session, year, source, "148348", D(0), D(40), 4) |
| 193 | rows = session.scalars( |
| 194 | select(LedgerBalance).where(LedgerBalance.year_id == year.id) |
| 195 | ).all() |
| 196 | rows[0].source_note, rows[1].source_note = "6", "7" |
| 197 | n6 = _note(session, year, "6", "نقد", "BS", "Assets") |
| 198 | n7 = _note(session, year, "7", "سایر", "BS", "Assets") |
| 199 | _map(session, year, "148348", n6) |
| 200 | _map(session, year, "148348", n7) |
| 201 | session.flush() |
| 202 | build_statements(session, year.id) |
| 203 | session.flush() |
| 204 | totals = { |
| 205 | ln.target_note_id: ln.mapped_total |
| 206 | for ln in session.scalars(select(StatementLine)).all() |
| 207 | } |
| 208 | assert totals[n6.id] == D(100) and totals[n7.id] == D(40) |
| 209 | |
| 210 | def test_single_explicit_mapping_overrides_the_source_note(self, session): |
| 211 | year, source = _setup(session) |
| 212 | from test_approval_gate import _carry |
| 213 | |
| 214 | _carry(session, year, source, "148348", D(0), D(100), 3) |
| 215 | row = session.scalar(select(LedgerBalance).where(LedgerBalance.year_id == year.id)) |
| 216 | row.source_note = "6" |
| 217 | n7 = _note(session, year, "7", "بازطبقهبندی", "BS", "Assets") |
| 218 | _map(session, year, "148348", n7) |
| 219 | session.flush() |
| 220 | build_statements(session, year.id) |
| 221 | session.flush() |
| 222 | line = session.scalar(select(StatementLine).where(StatementLine.target_note_id == n7.id)) |
| 223 | assert line.mapped_total == D(100) |