codex/gate3-mapping-batch-review-20261030
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 | """Governed, year-isolated mapping persistence; never infer financial targets.""" |
| 2 | from __future__ import annotations |
| 3 | |
| 4 | from sqlalchemy import select |
| 5 | from sqlalchemy.orm import Session |
| 6 | |
| 7 | from aram_backend.models import AccountVersion, FiscalYear, FiscalYearStatus, MappingRule, NoteDefinition |
| 8 | |
| 9 | |
| 10 | class MappingValidationError(ValueError): |
| 11 | """Mapping would violate a fiscal-year or explicit-review boundary.""" |
| 12 | |
| 13 | |
| 14 | def save_mapping_rule( |
| 15 | session: Session, *, year_id: int, rule_key: str, account_code: str, |
| 16 | target_note_id: int, mapping_type: str, version: int = 1, |
| 17 | ) -> MappingRule: |
| 18 | """Persist an explicit mapping for a draft year, leaving commit to the caller. |
| 19 | |
| 20 | Repeated identical requests are idempotent; conflicting versions are rejected. |
| 21 | No account, note, or mapping target is guessed from ledger data. |
| 22 | """ |
| 23 | if isinstance(year_id, bool) or not isinstance(year_id, int) or year_id <= 0: |
| 24 | raise MappingValidationError("year_id must be a positive integer") |
| 25 | if isinstance(version, bool) or not isinstance(version, int) or version <= 0: |
| 26 | raise MappingValidationError("version must be a positive integer") |
| 27 | for label, value, limit in ( |
| 28 | ("rule_key", rule_key, 128), ("account_code", account_code, 64), |
| 29 | ("mapping_type", mapping_type, 32), |
| 30 | ): |
| 31 | if not isinstance(value, str) or not value or value != value.strip() or len(value) > limit: |
| 32 | raise MappingValidationError(f"{label} must be nonempty, trimmed and at most {limit} characters") |
| 33 | if isinstance(target_note_id, bool) or not isinstance(target_note_id, int) or target_note_id <= 0: |
| 34 | raise MappingValidationError("target_note_id must be a positive integer") |
| 35 | |
| 36 | year = session.get(FiscalYear, year_id) |
| 37 | if year is None or year.status is not FiscalYearStatus.DRAFT: |
| 38 | raise MappingValidationError("fiscal year must exist and be Draft") |
| 39 | note = session.get(NoteDefinition, target_note_id) |
| 40 | if note is None or note.year_id != year_id: |
| 41 | raise MappingValidationError("target note must belong to the same fiscal year") |
| 42 | current_note_id = session.scalar( |
| 43 | select(NoteDefinition.id) |
| 44 | .where(NoteDefinition.year_id == year_id, NoteDefinition.note_number == note.note_number) |
| 45 | .order_by(NoteDefinition.version.desc(), NoteDefinition.id.desc()) |
| 46 | .limit(1) |
| 47 | ) |
| 48 | if current_note_id != target_note_id: |
| 49 | raise MappingValidationError("target note must reference the latest note version") |
| 50 | account = session.scalar(select(AccountVersion.id).where( |
| 51 | AccountVersion.year_id == year_id, AccountVersion.account_code == account_code, |
| 52 | ).limit(1)) |
| 53 | if account is None: |
| 54 | raise MappingValidationError("account code must exist in the same fiscal year") |
| 55 | |
| 56 | existing = session.scalar(select(MappingRule).where( |
| 57 | MappingRule.year_id == year_id, |
| 58 | MappingRule.rule_key == rule_key, |
| 59 | MappingRule.version == version, |
| 60 | )) |
| 61 | if existing is not None: |
| 62 | if (existing.account_code, existing.target_note_id, existing.mapping_type) != ( |
| 63 | account_code, target_note_id, mapping_type |
| 64 | ): |
| 65 | raise MappingValidationError("mapping version conflicts with an existing rule") |
| 66 | return existing |
| 67 | |
| 68 | rule = MappingRule(year_id=year_id, rule_key=rule_key, version=version, |
| 69 | account_code=account_code, target_note_id=target_note_id, |
| 70 | mapping_type=mapping_type) |
| 71 | session.add(rule) |
| 72 | session.flush() |
| 73 | return rule |