codex/gate3-note-mapping-review-20261023
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
py 93 lines 3.9 KB
Raw
1 """Read-only note-level mapping review; no financial amounts or inferred targets."""
2 from __future__ import annotations
3
4 from dataclasses import dataclass
5
6 from sqlalchemy import select
7 from sqlalchemy.orm import Session
8
9 from aram_backend.mapping_service import MappingValidationError
10 from aram_backend.models import AccountVersion, FiscalYear, MappingRule, NoteDefinition
11
12
13 @dataclass(frozen=True)
14 class NoteMappingReview:
15 note_id: int
16 note_number: str
17 note_title: str
18 account_codes: tuple[str, ...]
19 ambiguous_account_codes: tuple[str, ...]
20
21
22 @dataclass(frozen=True)
23 class NoteMappingOverview:
24 year_id: int
25 notes: tuple[NoteMappingReview, ...]
26 unmapped_account_codes: tuple[str, ...]
27 orphan_rule_keys: tuple[str, ...]
28 stale_note_rule_keys: tuple[str, ...]
29
30
31 def review_note_mappings(session: Session, *, year_id: int) -> NoteMappingOverview:
32 """Report current note assignments; reject stale note versions rather than guessing.
33
34 Current = greatest version per account, rule key and note number. If an
35 account has multiple distinct current targets, show it as ambiguous.
36 Queries deliberately avoid flushing pending changes.
37 """
38 if type(year_id) is not int or year_id <= 0:
39 raise MappingValidationError("year_id must be a positive integer")
40 with session.no_autoflush:
41 if session.get(FiscalYear, year_id) is None:
42 raise MappingValidationError("fiscal year does not exist")
43
44 accounts = session.scalars(select(AccountVersion).where(
45 AccountVersion.year_id == year_id
46 ).order_by(AccountVersion.account_code, AccountVersion.version.desc(), AccountVersion.id.desc())).all()
47 current_accounts = {a.account_code for a in accounts}
48
49 notes = session.scalars(select(NoteDefinition).where(
50 NoteDefinition.year_id == year_id
51 ).order_by(NoteDefinition.note_number, NoteDefinition.version.desc(), NoteDefinition.id.desc())).all()
52 current_notes: dict[str, NoteDefinition] = {}
53 for note in notes:
54 current_notes.setdefault(note.note_number, note)
55 current_note_by_id = {n.id: n for n in current_notes.values()}
56
57 rules = session.scalars(select(MappingRule).where(
58 MappingRule.year_id == year_id
59 ).order_by(MappingRule.rule_key, MappingRule.version.desc(), MappingRule.id.desc())).all()
60 current_rules: dict[str, MappingRule] = {}
61 for rule in rules:
62 current_rules.setdefault(rule.rule_key, rule)
63
64 account_targets: dict[str, set[int]] = {code: set() for code in current_accounts}
65 orphan: list[str] = []
66 stale: list[str] = []
67 for key, rule in current_rules.items():
68 if rule.account_code not in current_accounts:
69 orphan.append(key)
70 elif rule.target_note_id not in current_note_by_id:
71 stale.append(key)
72 else:
73 account_targets[rule.account_code].add(rule.target_note_id)
74
75 ambiguous = {code for code, targets in account_targets.items() if len(targets) > 1}
76 reviews = tuple(NoteMappingReview(
77 note_id=note.id, note_number=note.note_number, note_title=note.title,
78 account_codes=tuple(sorted(
79 code for code, targets in account_targets.items()
80 if note.id in targets and code not in ambiguous
81 )),
82 ambiguous_account_codes=tuple(sorted(
83 code for code in ambiguous if note.id in account_targets[code]
84 )),
85 ) for note in sorted(current_notes.values(), key=lambda n: (n.note_number, n.id)))
86 return NoteMappingOverview(
87 year_id=year_id, notes=reviews,
88 unmapped_account_codes=tuple(sorted(
89 code for code, targets in account_targets.items() if not targets
90 )),
91 orphan_rule_keys=tuple(sorted(orphan)),
92 stale_note_rule_keys=tuple(sorted(stale)),
93 )