feat(mapping): validate account-level coverage separately from multi-statement mapping traces
davoodmehraban89 committed
Oct 31, 2026 at 08:47 UTC
f46c2934fbdf3f89b20dcc31e5de449ab937d8f4
1 file changed
+71
-11
backend/src/aram_backend/reconciliation_review.py
+71
-11
@@ -134,27 +134,87 @@ def _validated_preview(content: bytes, preview: CoveragePreview) -> None:
134
if (preview.account_count < 1
135
or preview.account_count != len(preview.accounts)
136
or preview.account_count != preview.candidate_mapped_accounts + preview.blocked_accounts
137
- or preview.candidate_mapped_accounts != sum(a.included for a in preview.accounts)):
137
+ or preview.candidate_mapped_accounts != sum(a.included for a in preview.accounts)
138
+ or preview.blocked_accounts != sum(not a.included for a in preview.accounts)):
139
raise CoverageError("COVERAGE_COUNT_MISMATCH")
139
- if len({account.account_code for account in preview.accounts}) != preview.account_count:
140
+ accounts = {account.account_code: account for account in preview.accounts}
141
+ if len(accounts) != preview.account_count:
142
raise CoverageError("DUPLICATE_SOURCE_ACCOUNT")
141
- source_values = [_money(a.closing_exposure) for a in preview.accounts]
143
+
144
+ mappings_by_account: dict[str, int] = {}
145
+ mapping_ids: set[str] = set()
146
+ mapping_rows: set[tuple[str, int]] = set()
147
+ included_statement_keys: set[tuple[str, str]] = set()
148
+ grouped: dict[tuple[str, str, str, str], list[tuple[str, Decimal]]] = {}
149
+ for mapping in preview.mappings:
150
+ if mapping.account_code not in accounts:
151
+ raise CoverageError("MAPPING_TRACE_UNKNOWN_ACCOUNT")
152
+ mappings_by_account[mapping.account_code] = (
153
+ mappings_by_account.get(mapping.account_code, 0) + 1
154
+ )
155
+ if mapping.mapping_id in mapping_ids or (
156
+ mapping.account_code, mapping.mapping_excel_row
157
+ ) in mapping_rows:
158
+ raise CoverageError("DUPLICATE_MAPPING_TRACE")
159
+ mapping_ids.add(mapping.mapping_id)
160
+ mapping_rows.add((mapping.account_code, mapping.mapping_excel_row))
161
+ if mapping.included:
162
+ statement_key = (mapping.account_code, mapping.target_statement)
163
+ if statement_key in included_statement_keys:
164
+ raise CoverageError("DUPLICATE_ACCOUNT_STATEMENT_MAPPING")
165
+ included_statement_keys.add(statement_key)
166
+ target = _key(
167
+ mapping.target_statement, mapping.target_section,
168
+ mapping.target_note, mapping.target_subnote,
169
+ )
170
+ grouped.setdefault(target, []).append(
171
+ (mapping.account_code, _money(mapping.closing_exposure))
172
+ )
173
+ elif accounts[mapping.account_code].included and not any(
174
+ other.included and other.account_code == mapping.account_code
175
+ for other in preview.mappings
176
+ ):
177
+ raise CoverageError("ACCOUNT_MAPPING_ELIGIBILITY_MISMATCH")
178
+
179
+ for account in preview.accounts:
180
+ if account.mapping_count != mappings_by_account.get(account.account_code, 0):
181
+ raise CoverageError("MAPPING_TRACE_COUNT_MISMATCH")
182
+ if account.included != any(
183
+ item.included for item in preview.mappings
184
+ if item.account_code == account.account_code
185
+ ):
186
+ raise CoverageError("ACCOUNT_MAPPING_ELIGIBILITY_MISMATCH")
187
+
188
+ source_values = [_money(account.closing_exposure) for account in preview.accounts]
189
candidate_values = [
143
- _money(a.closing_exposure) for a in preview.accounts if a.included
190
+ _money(account.closing_exposure) for account in preview.accounts if account.included
191
]
192
blocked_values = [
146
- _money(a.closing_exposure) for a in preview.accounts if not a.included
193
+ _money(account.closing_exposure) for account in preview.accounts if not account.included
194
]
148
- note_values = [_money(n.closing_exposure) for n in preview.notes]
149
- if (len(preview.notes) != len({
150
- _key(n.statement, n.section, n.note_number, n.subnote_code)
151
- for n in preview.notes
152
- })):
195
+ if len(preview.notes) != len({
196
+ _key(note.statement, note.section, note.note_number, note.subnote_code)
197
+ for note in preview.notes
198
+ }):
199
raise CoverageError("DUPLICATE_CANDIDATE_NOTE")
200
+ note_by_key = {
201
+ _key(note.statement, note.section, note.note_number, note.subnote_code): note
202
+ for note in preview.notes
203
+ }
204
+ if set(note_by_key) != set(grouped):
205
+ raise CoverageError("COVERAGE_NOTE_MAPPING_MISMATCH")
206
+ for target, note in note_by_key.items():
207
+ mapped = grouped[target]
208
+ if (_money(note.closing_exposure) != _sum_exact([amount for _, amount in mapped])
209
+ or note.account_codes != tuple(code for code, _ in mapped)):
210
+ raise CoverageError("COVERAGE_NOTE_MAPPING_MISMATCH")
211
if (_sum_exact(source_values) != _money(preview.source_closing_exposure)
212
or _sum_exact(candidate_values) != _money(preview.candidate_closing_exposure)
213
or _sum_exact(blocked_values) != _money(preview.blocked_closing_exposure)
157
- or _sum_exact(note_values) != _sum_exact(candidate_values)):
214
+ or _sum_exact(source_values) != _sum_exact([
215
+ _money(preview.candidate_closing_exposure),
216
+ _money(preview.blocked_closing_exposure),
217
+ ])):
218
raise CoverageError("COVERAGE_RECONCILIATION_FAILED")
219
220