Mirrored from https://github.com/davoodmehraban89/Aram.git
Synced Dec 4, 10:30 UTC
arena/2d4eda07-aram
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
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 | """Phase 6 reviewable outputs: XLSX + JSON export with exact rial and lineage.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import json |
| 6 | from decimal import Decimal |
| 7 | from io import BytesIO |
| 8 | |
| 9 | from openpyxl import load_workbook |
| 10 | |
| 11 | from aram_backend.ledger_parser import parse_ledger |
| 12 | from aram_backend.models import ( |
| 13 | Company, |
| 14 | FiscalYear, |
| 15 | MappingRule, |
| 16 | NoteDefinition, |
| 17 | NoteRow, |
| 18 | SourceDocument, |
| 19 | ) |
| 20 | from aram_backend.services.ledger_import import import_ledger_balances |
| 21 | from aram_backend.services.statement_build import build_statements |
| 22 | from aram_backend.services.statement_export import ( |
| 23 | export_statements_json, |
| 24 | export_statements_workbook, |
| 25 | ) |
| 26 | |
| 27 | _HEADER = ( |
| 28 | "حساب,ACCOUNT_NAME,year,نوع,kol,T1_CODE,T2_CODE,T3_CODE,T4_CODE," |
| 29 | "OPENING,Debit,Credit,CLOSING,مانده صورت مالی" |
| 30 | ) |
| 31 | |
| 32 | |
| 33 | def _setup(session): |
| 34 | company = Company(code="X1", name="شرکت نمونه") |
| 35 | session.add(company) |
| 36 | session.flush() |
| 37 | year = FiscalYear(company_id=company.id, year=1404) |
| 38 | session.add(year) |
| 39 | session.flush() |
| 40 | # Ordinary closing-based row: acct() reads closing (ADR-020) and the BS |
| 41 | # Assets sign (+1) keeps it positive, so note value == statement line total. |
| 42 | content = (_HEADER + "\n1101,نقد و بانک,1404,e,11,,,,,0,0,0,673740637000000,\n").encode("utf-8") |
| 43 | records = list(parse_ledger(content, "ledger.csv", tolerant=True)) |
| 44 | src = SourceDocument( |
| 45 | year_id=year.id, filename="ledger.csv", document_type="general_ledger", file_hash="a" * 64 |
| 46 | ) |
| 47 | session.add(src) |
| 48 | session.flush() |
| 49 | import_ledger_balances( |
| 50 | session, year_id=year.id, source_document_id=src.id, records=records, import_version=1 |
| 51 | ) |
| 52 | note = NoteDefinition( |
| 53 | year_id=year.id, |
| 54 | note_number="10", |
| 55 | title="داراییهای جاری", |
| 56 | configuration_json={"statement": "BS", "section": "Assets"}, |
| 57 | ) |
| 58 | session.add(note) |
| 59 | session.flush() |
| 60 | session.add( |
| 61 | MappingRule( |
| 62 | year_id=year.id, |
| 63 | rule_key="111101->n", |
| 64 | account_code="111101", |
| 65 | target_note_id=note.id, |
| 66 | mapping_type="closing", |
| 67 | ) |
| 68 | ) |
| 69 | session.add(NoteRow(note_id=note.id, row_order=0, label="جمع", formula_logic='acct("111101")')) |
| 70 | session.flush() |
| 71 | build_statements(session, year.id, statement_types=("BS",)) |
| 72 | return year.id |
| 73 | |
| 74 | |
| 75 | def test_json_export_exact_rial_and_lineage(session): |
| 76 | year_id = _setup(session) |
| 77 | payload = export_statements_json(session, year_id) |
| 78 | assert payload["year"] == 1404 |
| 79 | bs = next(s for s in payload["statements"] if s["type"] == "BS") |
| 80 | line = bs["lines"][0] |
| 81 | assert line["mapped_total_rial"] == "673740637000000" |
| 82 | assert line["mapped_total_million"] == 673740637 |
| 83 | # Lineage traces to the exact source row. |
| 84 | assert payload["lineage"], "lineage must not be empty" |
| 85 | row = payload["lineage"][0] |
| 86 | assert row["filename"] == "ledger.csv" |
| 87 | assert row["source_row"] == 2 |
| 88 | assert row["effective_balance_rial"] == "673740637000000" |
| 89 | # Note evaluated. |
| 90 | note = payload["notes"][0] |
| 91 | assert note["rows"][0]["value"] == "673740637000000" |
| 92 | |
| 93 | |
| 94 | def test_xlsx_export_sheets_and_values(session): |
| 95 | year_id = _setup(session) |
| 96 | data = export_statements_workbook(session, year_id) |
| 97 | wb = load_workbook(BytesIO(data), data_only=True) |
| 98 | assert "Summary" in wb.sheetnames |
| 99 | assert "BS" in wb.sheetnames |
| 100 | assert "Lineage" in wb.sheetnames |
| 101 | assert "Note 10" in wb.sheetnames |
| 102 | bs = wb["BS"] |
| 103 | # header row + one line; rial column is exact integer text. |
| 104 | assert bs.cell(row=2, column=3).value == "673740637000000" |
| 105 | assert bs.cell(row=2, column=4).value == 673740637 |
| 106 | lineage = wb["Lineage"] |
| 107 | assert lineage.max_row >= 2 |
| 108 | wb.close() |
| 109 | |
| 110 | |
| 111 | def test_json_export_is_serializable(session): |
| 112 | year_id = _setup(session) |
| 113 | payload = export_statements_json(session, year_id) |
| 114 | # Must round-trip through JSON without Decimal leakage. |
| 115 | text = json.dumps(payload, ensure_ascii=False) |
| 116 | assert "673740637000000" in text |
| 117 | assert isinstance(payload["statements"][0]["lines"][0]["mapped_total_rial"], str) |
| 118 | assert Decimal(payload["statements"][0]["lines"][0]["mapped_total_rial"]) == Decimal( |
| 119 | "673740637000000" |
| 120 | ) |