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 | """Tolerant real-ledger import: reclassified rows preserved, anomalies reported. |
| 2 | |
| 3 | Proves D3/P-01 (raw rows kept for drill-down) and the Phase-4 statement-balance |
| 4 | capture: empty money cells on reclassified rows become exact zero, the signed |
| 5 | ``مانده صورت مالی`` is stored, and a non-numeric money cell is skipped + reported |
| 6 | (fail-loud, never invented). |
| 7 | """ |
| 8 | |
| 9 | from __future__ import annotations |
| 10 | |
| 11 | from decimal import Decimal |
| 12 | |
| 13 | import pytest |
| 14 | |
| 15 | from aram_backend.ledger_parser import SkippedRow, parse_ledger |
| 16 | from aram_backend.models import LedgerBalance |
| 17 | from aram_backend.services.ledger_import import import_ledger_balances |
| 18 | from aram_backend.services.statement_build import build_statements |
| 19 | |
| 20 | # Column order matches _FIELDS plus the optional statement-balance column. |
| 21 | _COLUMNS = [ |
| 22 | "حساب", |
| 23 | "ACCOUNT_NAME", |
| 24 | "year", |
| 25 | "نوع", |
| 26 | "kol", |
| 27 | "T1_CODE", |
| 28 | "T2_CODE", |
| 29 | "T3_CODE", |
| 30 | "T4_CODE", |
| 31 | "OPENING", |
| 32 | "Debit", |
| 33 | "Credit", |
| 34 | "CLOSING", |
| 35 | "مانده صورت مالی", |
| 36 | ] |
| 37 | |
| 38 | |
| 39 | def _row(**fields: str) -> str: |
| 40 | return ",".join(fields.get(col, "") for col in _COLUMNS) |
| 41 | |
| 42 | |
| 43 | def _csv(rows: list[str]) -> bytes: |
| 44 | return ("\n".join([",".join(_COLUMNS), *rows]) + "\n").encode("utf-8") |
| 45 | |
| 46 | |
| 47 | _BASE = {"حساب": "1101", "ACCOUNT_NAME": "نقد و بانک", "year": "1404", "نوع": "e", "kol": "11"} |
| 48 | |
| 49 | |
| 50 | def test_reclassified_row_kept_with_statement_balance(): |
| 51 | content = _csv([_row(**_BASE, **{"مانده صورت مالی": "-52400000000000"})]) |
| 52 | skipped: list[SkippedRow] = [] |
| 53 | records = list(parse_ledger(content, "ledger.csv", tolerant=True, skipped=skipped)) |
| 54 | assert len(records) == 1 |
| 55 | rec = records[0] |
| 56 | assert rec.opening == Decimal(0) |
| 57 | assert rec.closing == Decimal(0) |
| 58 | assert rec.statement_balance == Decimal("-52400000000000") |
| 59 | assert skipped == [] |
| 60 | |
| 61 | |
| 62 | def test_non_numeric_money_row_skipped_and_reported(): |
| 63 | content = _csv([_row(**_BASE, Debit="abc", Credit="0", CLOSING="0", OPENING="0")]) |
| 64 | skipped: list[SkippedRow] = [] |
| 65 | records = list(parse_ledger(content, "ledger.csv", tolerant=True, skipped=skipped)) |
| 66 | assert records == [] |
| 67 | assert len(skipped) == 1 |
| 68 | assert skipped[0].row_number == 2 |
| 69 | assert "Debit" in skipped[0].reason |
| 70 | |
| 71 | |
| 72 | def test_strict_mode_still_fails_loud(): |
| 73 | content = _csv([_row(**_BASE, **{"مانده صورت مالی": "-52400000000000"})]) |
| 74 | with pytest.raises(ValueError): |
| 75 | list(parse_ledger(content, "ledger.csv")) |
| 76 | |
| 77 | |
| 78 | def test_statement_balance_drives_build_and_drilldown(session): |
| 79 | from aram_backend.models import Company, FiscalYear, MappingRule, NoteDefinition, SourceDocument |
| 80 | |
| 81 | company = Company(code="T1", name="شرکت آزمون") |
| 82 | session.add(company) |
| 83 | session.flush() |
| 84 | year = FiscalYear(company_id=company.id, year=1404) |
| 85 | session.add(year) |
| 86 | session.flush() |
| 87 | year_id = year.id |
| 88 | |
| 89 | content = _csv( |
| 90 | [ |
| 91 | _row(**_BASE, **{"مانده صورت مالی": "-673740637000000"}), |
| 92 | _row( |
| 93 | **{**_BASE, "حساب": "1102"}, OPENING="0", Debit="0", Credit="0", CLOSING="1000000" |
| 94 | ), |
| 95 | ] |
| 96 | ) |
| 97 | records = list(parse_ledger(content, "ledger.csv", tolerant=True)) |
| 98 | src = SourceDocument( |
| 99 | year_id=year_id, filename="ledger.csv", document_type="general_ledger", file_hash="h" * 64 |
| 100 | ) |
| 101 | session.add(src) |
| 102 | session.flush() |
| 103 | import_ledger_balances( |
| 104 | session, year_id=year_id, source_document_id=src.id, records=records, import_version=1 |
| 105 | ) |
| 106 | |
| 107 | note = NoteDefinition( |
| 108 | year_id=year_id, |
| 109 | note_number="10", |
| 110 | title="داراییهای جاری", |
| 111 | configuration_json={"statement": "BS", "section": "Assets"}, |
| 112 | ) |
| 113 | session.add(note) |
| 114 | session.flush() |
| 115 | session.add( |
| 116 | MappingRule( |
| 117 | year_id=year_id, |
| 118 | rule_key="111101->n", |
| 119 | account_code="111101", |
| 120 | target_note_id=note.id, |
| 121 | mapping_type="closing", |
| 122 | ) |
| 123 | ) |
| 124 | session.add( |
| 125 | MappingRule( |
| 126 | year_id=year_id, |
| 127 | rule_key="111102->n", |
| 128 | account_code="111102", |
| 129 | target_note_id=note.id, |
| 130 | mapping_type="closing", |
| 131 | ) |
| 132 | ) |
| 133 | session.flush() |
| 134 | |
| 135 | result = build_statements(session, year_id, statement_types=("BS",)) |
| 136 | line = result.statements[0].lines[0] |
| 137 | # Reclassified row contributes its signed statement balance directly; |
| 138 | # the ordinary closing row contributes sign(Assets=+1) * closing. |
| 139 | assert line.mapped_total == Decimal("-673740637000000") + Decimal("1000000") |
| 140 | |
| 141 | rows = session.query(LedgerBalance).filter_by(year_id=year_id).all() |
| 142 | assert len(rows) == 2 # raw rows preserved, not aggregated |
| 143 | reclassified = next(r for r in rows if r.subsidiary_code == "1101") |
| 144 | assert reclassified.statement_balance == Decimal("-673740637000000") |
| 145 | assert reclassified.source_row == 2 |