fix(835): drop over-constraining UNIQUE constraints, add multi-BPR warning

Three related changes for real CO Medicaid data:

1. Drop UNIQUE(batch_id, patient_control_number) on claims and
   UNIQUE(batch_id, payer_claim_control_number) on remittances. The X12
   spec allows multiple CLM segments per 2000B subscriber loop and 835
   ERAs can repeat a payer_claim_control_number for reversals. Claim/
   remittance identity is provided by the primary key (claims.id = CLM01,
   remittances.id = CLP01).

2. Add validator rule R835_MULTI_BPR warning for files with multiple BPR
   segments (CO Medicaid split-payment pattern). The parser already sums
   BPR02 paid_amounts; this surfaces the non-standard data to operators.

3. Skip R835_BAL_BPR_vs_CLP04 when BPR01='I' (Information Only 835).
   In that mode BPR02 is informational and the per-claim CLP04 totals
   are authoritative — a diff is expected, not an error.

Migration 0003 handles the drop with IF EXISTS so fresh DBs skip cleanly.
Updates affected tests to reflect new schema (no UNIQUE constraint on
batch_id + patient_control_number / payer_claim_control_number).

Fixes test_api_835::test_prodfile_round_trip_persists_separately which
was failing on real production data.
This commit is contained in:
Tyler
2026-06-20 18:10:07 -06:00
parent 66da69baa0
commit b5e27927e0
12 changed files with 371 additions and 23 deletions
+4 -3
View File
@@ -45,9 +45,10 @@ def _make_claim_837(claim_id: str = "CLM-1", charge: str = "124.00") -> ClaimOut
billing_provider=BillingProvider(
name="Test", npi="1234567890",
),
# Member_id doubles as patient_control_number in the DB; the
# ``uq_claims_batch_pcn`` unique constraint requires it to be
# distinct within a batch, so derive it from the claim id.
# Member_id doubles as patient_control_number in the DB. Derive it
# from the claim id so two claims in the same batch are easy to tell
# apart in test assertions (no longer required by a unique constraint
# — see migration 0003 — but still a useful invariant for tests).
subscriber=Subscriber(
first_name="Jane", last_name="Doe", member_id=f"M-{claim_id}",
),