codex/aram-mapping-source-parser-20261101
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 | from __future__ import annotations |
| 2 | |
| 3 | from collections.abc import Iterator |
| 4 | from dataclasses import dataclass |
| 5 | from decimal import Decimal, InvalidOperation |
| 6 | from io import BytesIO |
| 7 | |
| 8 | from openpyxl import load_workbook |
| 9 | |
| 10 | from aram_backend.workbook_inspection import inspect_xlsx |
| 11 | |
| 12 | |
| 13 | @dataclass(frozen=True) |
| 14 | class LedgerRecord: |
| 15 | row_number: int |
| 16 | account_code: str |
| 17 | account_name: str |
| 18 | source_year: int |
| 19 | entry_type: str |
| 20 | t1_code: str | None |
| 21 | t2_code: str | None |
| 22 | t3_code: str | None |
| 23 | t4_code: str | None |
| 24 | opening: Decimal |
| 25 | debit: Decimal |
| 26 | credit: Decimal |
| 27 | closing: Decimal |
| 28 | |
| 29 | |
| 30 | class LedgerParseError(ValueError): |
| 31 | """Raised when a source ledger row cannot be represented safely.""" |
| 32 | |
| 33 | |
| 34 | _FIELDS = { |
| 35 | "account_code": "حساب", |
| 36 | "account_name": "ACCOUNT_NAME", |
| 37 | "source_year": "year", |
| 38 | "entry_type": "نوع", |
| 39 | "t1_code": "T1_CODE", |
| 40 | "t2_code": "T2_CODE", |
| 41 | "t3_code": "T3_CODE", |
| 42 | "t4_code": "T4_CODE", |
| 43 | "opening": "OPENING", |
| 44 | "debit": "Debit", |
| 45 | "credit": "Credit", |
| 46 | "closing": "CLOSING", |
| 47 | } |
| 48 | |
| 49 | |
| 50 | def parse_general_ledger(content: bytes) -> Iterator[LedgerRecord]: |
| 51 | """Stream typed ledger rows without persisting or calculating values.""" |
| 52 | |
| 53 | inspection = inspect_xlsx(content, document_type="general_ledger") |
| 54 | workbook = load_workbook( |
| 55 | BytesIO(content), read_only=True, data_only=True, keep_links=False |
| 56 | ) |
| 57 | try: |
| 58 | worksheet = workbook[inspection.data_sheet] |
| 59 | header_values = next( |
| 60 | worksheet.iter_rows( |
| 61 | min_row=inspection.header_row, |
| 62 | max_row=inspection.header_row, |
| 63 | values_only=True, |
| 64 | ) |
| 65 | ) |
| 66 | indexes = { |
| 67 | str(value).strip(): index |
| 68 | for index, value in enumerate(header_values) |
| 69 | if value is not None |
| 70 | } |
| 71 | |
| 72 | for row_number, values in enumerate( |
| 73 | worksheet.iter_rows( |
| 74 | min_row=inspection.header_row + 1, values_only=True |
| 75 | ), |
| 76 | start=inspection.header_row + 1, |
| 77 | ): |
| 78 | if not any(value is not None for value in values): |
| 79 | continue |
| 80 | |
| 81 | def value(field: str): |
| 82 | return values[indexes[_FIELDS[field]]] |
| 83 | |
| 84 | yield LedgerRecord( |
| 85 | row_number=row_number, |
| 86 | account_code=_required_code(value("account_code"), row_number, "حساب"), |
| 87 | account_name=_required_text( |
| 88 | value("account_name"), row_number, "ACCOUNT_NAME" |
| 89 | ), |
| 90 | source_year=_required_year(value("source_year"), row_number), |
| 91 | entry_type=_required_text(value("entry_type"), row_number, "نوع"), |
| 92 | t1_code=_optional_code(value("t1_code"), row_number, "T1_CODE"), |
| 93 | t2_code=_optional_code(value("t2_code"), row_number, "T2_CODE"), |
| 94 | t3_code=_optional_code(value("t3_code"), row_number, "T3_CODE"), |
| 95 | t4_code=_optional_code(value("t4_code"), row_number, "T4_CODE"), |
| 96 | opening=_required_decimal(value("opening"), row_number, "OPENING"), |
| 97 | debit=_required_decimal(value("debit"), row_number, "Debit"), |
| 98 | credit=_required_decimal(value("credit"), row_number, "Credit"), |
| 99 | closing=_required_decimal(value("closing"), row_number, "CLOSING"), |
| 100 | ) |
| 101 | finally: |
| 102 | workbook.close() |
| 103 | |
| 104 | |
| 105 | def _required_text(value: object, row_number: int, field: str) -> str: |
| 106 | text = str(value).strip() if value is not None else "" |
| 107 | if not text: |
| 108 | raise LedgerParseError(f"row {row_number}: {field} is required") |
| 109 | return text |
| 110 | |
| 111 | |
| 112 | def _required_year(value: object, row_number: int) -> int: |
| 113 | parsed = _required_decimal(value, row_number, "year") |
| 114 | if parsed != parsed.to_integral_value() or parsed <= 0: |
| 115 | raise LedgerParseError(f"row {row_number}: year must be a positive integer") |
| 116 | return int(parsed) |
| 117 | |
| 118 | |
| 119 | def _required_code(value: object, row_number: int, field: str) -> str: |
| 120 | code = _optional_code(value, row_number, field) |
| 121 | if code is None: |
| 122 | raise LedgerParseError(f"row {row_number}: {field} is required") |
| 123 | return code |
| 124 | |
| 125 | |
| 126 | def _optional_code(value: object, row_number: int, field: str) -> str | None: |
| 127 | if value is None: |
| 128 | return None |
| 129 | if isinstance(value, bool): |
| 130 | raise LedgerParseError(f"row {row_number}: {field} is not a valid code") |
| 131 | if isinstance(value, str): |
| 132 | code = value.strip() |
| 133 | return code or None |
| 134 | parsed = _required_decimal(value, row_number, field) |
| 135 | if parsed != parsed.to_integral_value(): |
| 136 | raise LedgerParseError(f"row {row_number}: {field} must be an integer code") |
| 137 | return str(int(parsed)) |
| 138 | |
| 139 | |
| 140 | def _required_decimal(value: object, row_number: int, field: str) -> Decimal: |
| 141 | if value is None or isinstance(value, bool): |
| 142 | raise LedgerParseError(f"row {row_number}: {field} is required and numeric") |
| 143 | candidate = str(value).strip().replace(",", "").replace("٬", "") |
| 144 | try: |
| 145 | parsed = Decimal(candidate) |
| 146 | except (InvalidOperation, ValueError) as exc: |
| 147 | raise LedgerParseError(f"row {row_number}: {field} is not numeric") from exc |
| 148 | if not parsed.is_finite(): |
| 149 | raise LedgerParseError(f"row {row_number}: {field} must be finite") |
| 150 | return parsed |