codex/aram-multidimensional-mapping-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 io import BytesIO |
| 6 | from typing import Any |
| 7 | |
| 8 | from openpyxl import load_workbook |
| 9 | |
| 10 | from aram_backend.workbook_inspection import inspect_xlsx |
| 11 | |
| 12 | |
| 13 | MAX_MAPPING_ROWS = 100_000 |
| 14 | |
| 15 | _REQUIRED_HEADERS = ( |
| 16 | "کد حساب", |
| 17 | "نوع", |
| 18 | "کد یادداشت", |
| 19 | "شرح یادداشت", |
| 20 | "فرمول یادداشت اصلی", |
| 21 | "فرمول یادداشت فرعی", |
| 22 | ) |
| 23 | |
| 24 | |
| 25 | @dataclass(frozen=True) |
| 26 | class MappingSourceRow: |
| 27 | """One untouched source mapping row with its original Excel row number.""" |
| 28 | |
| 29 | row_number: int |
| 30 | account_code: str |
| 31 | mapping_type: str |
| 32 | note_code: str |
| 33 | note_description: str |
| 34 | main_formula: str |
| 35 | sub_formula: str |
| 36 | source_values: tuple[tuple[str, Any], ...] |
| 37 | |
| 38 | |
| 39 | class MappingSourceParseError(ValueError): |
| 40 | """Raised when a mapping workbook row cannot be read unambiguously.""" |
| 41 | |
| 42 | |
| 43 | def parse_mapping_source(content: bytes) -> Iterator[MappingSourceRow]: |
| 44 | """Stream mapping rows without evaluating formulas or inventing precedence. |
| 45 | |
| 46 | All source columns are retained in source_values so later matching logic can |
| 47 | explicitly interpret dimensions only after their real headers are reviewed. |
| 48 | Formula cells are read as formulas (data_only=False), never executed. |
| 49 | """ |
| 50 | |
| 51 | inspection = inspect_xlsx(content, document_type="mapping") |
| 52 | workbook = load_workbook( |
| 53 | BytesIO(content), read_only=True, data_only=False, keep_links=False |
| 54 | ) |
| 55 | try: |
| 56 | worksheet = workbook[inspection.data_sheet] |
| 57 | header_values = next( |
| 58 | worksheet.iter_rows( |
| 59 | min_row=inspection.header_row, |
| 60 | max_row=inspection.header_row, |
| 61 | values_only=True, |
| 62 | ) |
| 63 | ) |
| 64 | headers = tuple( |
| 65 | str(value).strip() if value is not None else "" |
| 66 | for value in header_values |
| 67 | ) |
| 68 | indexes = {header: index for index, header in enumerate(headers) if header} |
| 69 | missing = set(_REQUIRED_HEADERS).difference(indexes) |
| 70 | if missing: |
| 71 | raise MappingSourceParseError( |
| 72 | f"required mapping headers are missing: {sorted(missing)}" |
| 73 | ) |
| 74 | |
| 75 | parsed_rows = 0 |
| 76 | for row_number, values in enumerate( |
| 77 | worksheet.iter_rows( |
| 78 | min_row=inspection.header_row + 1, |
| 79 | max_col=len(headers), |
| 80 | values_only=True, |
| 81 | ), |
| 82 | start=inspection.header_row + 1, |
| 83 | ): |
| 84 | if not any(value is not None for value in values): |
| 85 | continue |
| 86 | parsed_rows += 1 |
| 87 | if parsed_rows > MAX_MAPPING_ROWS: |
| 88 | raise MappingSourceParseError("mapping workbook exceeds row limit") |
| 89 | |
| 90 | def cell(header: str) -> Any: |
| 91 | index = indexes[header] |
| 92 | return values[index] if index < len(values) else None |
| 93 | |
| 94 | account_code = _required_code(cell("کد حساب"), row_number, "کد حساب") |
| 95 | mapping_type = _required_text(cell("نوع"), row_number, "نوع") |
| 96 | note_code = _required_code(cell("کد یادداشت"), row_number, "کد یادداشت") |
| 97 | note_description = _optional_text( |
| 98 | cell("شرح یادداشت"), row_number, "شرح یادداشت" |
| 99 | ) |
| 100 | main_formula = _optional_text( |
| 101 | cell("فرمول یادداشت اصلی"), row_number, "فرمول یادداشت اصلی" |
| 102 | ) |
| 103 | sub_formula = _optional_text( |
| 104 | cell("فرمول یادداشت فرعی"), row_number, "فرمول یادداشت فرعی" |
| 105 | ) |
| 106 | source_values = tuple( |
| 107 | (header, values[index] if index < len(values) else None) |
| 108 | for index, header in enumerate(headers) |
| 109 | if header |
| 110 | ) |
| 111 | yield MappingSourceRow( |
| 112 | row_number=row_number, |
| 113 | account_code=account_code, |
| 114 | mapping_type=mapping_type, |
| 115 | note_code=note_code, |
| 116 | note_description=note_description, |
| 117 | main_formula=main_formula, |
| 118 | sub_formula=sub_formula, |
| 119 | source_values=source_values, |
| 120 | ) |
| 121 | finally: |
| 122 | workbook.close() |
| 123 | |
| 124 | |
| 125 | def _required_text(value: object, row_number: int, field: str) -> str: |
| 126 | result = _optional_text(value, row_number, field) |
| 127 | if not result: |
| 128 | raise MappingSourceParseError(f"row {row_number}: {field} is required") |
| 129 | return result |
| 130 | |
| 131 | |
| 132 | def _optional_text(value: object, row_number: int, field: str) -> str: |
| 133 | if value is None: |
| 134 | return "" |
| 135 | if isinstance(value, bool): |
| 136 | raise MappingSourceParseError(f"row {row_number}: {field} is not valid text") |
| 137 | result = str(value).strip() |
| 138 | if len(result) > 2_000: |
| 139 | raise MappingSourceParseError(f"row {row_number}: {field} exceeds length limit") |
| 140 | return result |
| 141 | |
| 142 | |
| 143 | def _required_code(value: object, row_number: int, field: str) -> str: |
| 144 | result = _optional_text(value, row_number, field) |
| 145 | if not result: |
| 146 | raise MappingSourceParseError(f"row {row_number}: {field} is required") |
| 147 | if isinstance(value, float) and not value.is_integer(): |
| 148 | raise MappingSourceParseError(f"row {row_number}: {field} must be an integer code") |
| 149 | if isinstance(value, float): |
| 150 | return str(int(value)) |
| 151 | return result |