feat(sp32): _content_keys_match prefers typed NPI columns over raw_json fallback

This commit is contained in:
Nora
2026-07-02 17:08:09 -06:00
parent c4bc118557
commit de77f19d9d
3 changed files with 67 additions and 23 deletions
+14 -10
View File
@@ -151,13 +151,16 @@ def _content_keys_match(remit, claim) -> set[str]:
- shim / dataclass test objects (planned names: total_charge_amount,
total_charge, rendering_provider_npi), and
- real SQLAlchemy ORM instances (Claim.charge_amount, Remittance.total_charge,
Claim.provider_npi).
Claim.provider_npi, Claim.rendering_provider_npi,
Remittance.rendering_provider_npi).
Production note: the 835 parser stores ``rendering_provider_npi`` and
``total_charge_amount`` on ``Remittance.raw_json`` (the ORM has no
dedicated column), so the read order below is:
- NPI: transient attribute → raw_json
- Charge: planned attribute name → real ORM column → raw_json
Production note (SP32): the typed ``rendering_provider_npi`` column is
the primary read for both sides (Claim = NM1*82, Remit = NM1*1P). Legacy
rows pre-0019 still carry the value in ``raw_json``; the read order is:
- NPI: typed column → raw_json (``service_provider_npi`` for remit,
``rendering_provider_npi`` for claim) → claim ``provider_npi``
(billing fallback for legacy 837p rows without NM1*82 extraction).
- Charge: planned attribute name → real ORM column → raw_json.
The ``raw_json`` fallback is what makes this helper safe against a
raw ``Remittance`` ORM instance from ``reconcile.run()``.
"""
@@ -190,12 +193,13 @@ def _content_keys_match(remit, claim) -> set[str]:
abs(_remit_charge - _claim_charge) < CHARGE_TOLERANCE:
matched.add("charge")
# NPI: prefer attribute, fall back to raw_json (where the 835 parser
# stores rendering_provider_npi). The Remittance ORM has no NPI column,
# so the raw_json path is the production read for raw ORM instances —
# it must NOT be removed.
# NPI: typed-column primary path (SP32), raw_json fallback (legacy rows).
# Remit reads rendering_provider_npi (single value, D4); claim reads
# rendering_provider_npi first, then falls back to provider_npi (billing)
# for legacy rows where the 837p parser hadn't yet extracted NM1*82.
remit_npi = (
(getattr(remit, "rendering_provider_npi", "") or "")
or ((getattr(remit, "raw_json", None) or {}).get("service_provider_npi", "") or "")
or ((getattr(remit, "raw_json", None) or {}).get("rendering_provider_npi", "") or "")
).strip()
claim_npi = (