feat: MassHealth PDF import auto-pays full balance + patient name fix

- PDF import now marks payments as PAID when MassHealth patient's
  mhPaidAmount >= totalBilled (no patient balance)
- Newly created patients from MH vouchers get insuranceProvider = 'MassHealth'
- Existing patients with blank insuranceProvider get it filled on import
- Fix: update patient name from PDF if existing record has empty name
- Various frontend/selenium/route updates

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-26 00:16:31 -04:00
parent 9efe5c8469
commit b7e06adf2f
15 changed files with 611 additions and 448 deletions

View File

@@ -1,7 +1,15 @@
import Decimal from "decimal.js";
import rawCodeTable from "@/assets/data/procedureCodesMH.json";
import rawCCACodeTable from "@/assets/data/procedureCodesCCA.json";
import rawDDMACodeTable from "@/assets/data/procedureCodesDDMA.json";
import rawUnitedDHCodeTable from "@/assets/data/procedureCodesUnitedDH.json";
import rawTuftsSCOCodeTable from "@/assets/data/procedureCodesTuftsSCO.json";
import { PROCEDURE_COMBOS } from "./procedureCombos";
const CODE_TABLE = rawCodeTable;
const CCA_CODE_TABLE = rawCCACodeTable;
const DDMA_CODE_TABLE = rawDDMACodeTable;
const UNITEDDH_CODE_TABLE = rawUnitedDHCodeTable;
const TUFTSSCO_CODE_TABLE = rawTuftsSCOCodeTable;
/* ----------------------------- Helpers ----------------------------- */
export const COMBO_BUTTONS = Object.values(PROCEDURE_COMBOS).map((c) => ({
id: c.id,
@@ -18,6 +26,55 @@ const CODE_MAP = (() => {
}
return m;
})();
const CCA_CODE_MAP = (() => {
const m = new Map();
for (const r of CCA_CODE_TABLE) {
const k = normalizeCode(String(r["Procedure Code"] || ""));
if (k && !m.has(k))
m.set(k, r);
}
return m;
})();
const DDMA_CODE_MAP = (() => {
const m = new Map();
for (const r of DDMA_CODE_TABLE) {
const k = normalizeCode(String(r["Procedure Code"] || ""));
if (k && !m.has(k))
m.set(k, r);
}
return m;
})();
const UNITEDDH_CODE_MAP = (() => {
const m = new Map();
for (const r of UNITEDDH_CODE_TABLE) {
const k = normalizeCode(String(r["Procedure Code"] || ""));
if (k && !m.has(k))
m.set(k, r);
}
return m;
})();
const TUFTSSCO_CODE_MAP = (() => {
const m = new Map();
for (const r of TUFTSSCO_CODE_TABLE) {
const k = normalizeCode(String(r["Procedure Code"] || ""));
if (k && !m.has(k))
m.set(k, r);
}
return m;
})();
/** Return the correct fee-schedule map for the given insurance type. */
function getCodeMap(insuranceSiteKey) {
const k = (insuranceSiteKey ?? "").replace(/_/g, "").toLowerCase();
if (k === "cca")
return CCA_CODE_MAP;
if (k === "ddma")
return DDMA_CODE_MAP;
if (k === "unitedsco" || k === "uniteddh" || k === "dentalhub")
return UNITEDDH_CODE_MAP;
if (k === "tuftssco" || k === "tufts")
return TUFTSSCO_CODE_MAP;
return CODE_MAP; // default: MassHealth
}
// this function is solely for abbrevations feature in claim-form
export function getDescriptionForCode(code) {
if (!code)
@@ -83,44 +140,35 @@ const ageOnDate = (dob, on) => {
export function pickPriceForRowByAge(row, age, normalizedCode) {
// Special-case rules (add more codes here if needed)
if (normalizedCode) {
// D1110: only valid for age >=14 (14..21 => PriceLTEQ21, >21 => PriceGT21)
// D1110: only valid for age >=14
if (normalizedCode === "D1110") {
if (age < 14) {
// D1110 not applicable to children <14 (those belong to D1120)
return new Decimal(0);
}
if (age >= 14 && age <= 21) {
// use PriceLTEQ21 only if present
if (!isBlankPrice(row.PriceLTEQ21))
return toDecimalOrZero(row.PriceLTEQ21);
return new Decimal(0);
}
// age > 21
if (!isBlankPrice(row.PriceGT21))
if (age < 14)
return new Decimal(0); // D1110 not for children <14
// age >= 14: use age-split if present, then flat Price
if (age <= 21 && !isBlankPrice(row.PriceLTEQ21))
return toDecimalOrZero(row.PriceLTEQ21);
if (age > 21 && !isBlankPrice(row.PriceGT21))
return toDecimalOrZero(row.PriceGT21);
if (!isBlankPrice(row.Price))
return toDecimalOrZero(row.Price);
return new Decimal(0);
}
// D1120: child 0-13 => PriceLTEQ21, otherwise no price (NC)
// D1120: valid for child 0-13 only
if (normalizedCode === "D1120") {
if (age < 14) {
if (!isBlankPrice(row.PriceLTEQ21))
return toDecimalOrZero(row.PriceLTEQ21);
return new Decimal(0);
}
// age >= 14 => NC / no price
if (age >= 14)
return new Decimal(0); // NC for adults
if (!isBlankPrice(row.PriceLTEQ21))
return toDecimalOrZero(row.PriceLTEQ21);
if (!isBlankPrice(row.Price))
return toDecimalOrZero(row.Price);
return new Decimal(0);
}
}
// Generic/default behavior (unchanged)
if (age <= 21) {
if (!isBlankPrice(row.PriceLTEQ21))
return toDecimalOrZero(row.PriceLTEQ21);
}
else {
if (!isBlankPrice(row.PriceGT21))
return toDecimalOrZero(row.PriceGT21);
}
// Fallback to Price if tiered not available/blank
// Generic/default: age-split first, flat Price as fallback
if (age <= 21 && !isBlankPrice(row.PriceLTEQ21))
return toDecimalOrZero(row.PriceLTEQ21);
if (age > 21 && !isBlankPrice(row.PriceGT21))
return toDecimalOrZero(row.PriceGT21);
if (!isBlankPrice(row.Price))
return toDecimalOrZero(row.Price);
return new Decimal(0);
@@ -158,7 +206,8 @@ const ensureCapacity = (lines, min, lineDate) => {
* Returns a NEW form object (immutable).
*/
export function mapPricesForForm(params) {
const { form, patientDOB } = params;
const { form, patientDOB, insuranceSiteKey } = params;
const map = getCodeMap(insuranceSiteKey);
return {
...form,
serviceLines: form.serviceLines.map((ln) => {
@@ -166,7 +215,7 @@ export function mapPricesForForm(params) {
const code = normalizeCode(ln.procedureCode || "");
if (!code)
return { ...ln };
const price = getPriceForCodeWithAgeFromMap(CODE_MAP, code, age);
const price = getPriceForCodeWithAgeFromMap(map, code, age);
return { ...ln, procedureCode: code, totalBilled: price };
}),
};
@@ -175,7 +224,7 @@ export function mapPricesForForm(params) {
* Apply a preset combo (fills codes & prices) using patientDOB and serviceDate.
* Returns a NEW form object (immutable).
*/
export function applyComboToForm(form, comboId, patientDOB, options = {}) {
export function applyComboToForm(form, comboId, patientDOB, options = {}, insuranceSiteKey) {
const preset = PROCEDURE_COMBOS[String(comboId)];
if (!preset)
return form;
@@ -205,8 +254,8 @@ export function applyComboToForm(form, comboId, patientDOB, options = {}) {
} // if replaceAll, insertAt stays 0
// Make sure we have enough rows for the whole combo
ensureCapacity(next.serviceLines, insertAt + preset.codes.length, lineDate);
// Age on the specific line date we will set
const age = ageOnDate(patientDOB, lineDate);
const age = options.skipPrice ? 0 : ageOnDate(patientDOB, lineDate);
const map = options.skipPrice ? CODE_MAP : getCodeMap(insuranceSiteKey);
for (let j = 0; j < preset.codes.length; j++) {
const i = insertAt + j;
if (i >= next.serviceLines.length)
@@ -215,7 +264,9 @@ export function applyComboToForm(form, comboId, patientDOB, options = {}) {
if (!codeRaw)
continue;
const code = normalizeCode(codeRaw);
const price = getPriceForCodeWithAgeFromMap(CODE_MAP, code, age);
const price = options.skipPrice
? new Decimal(0)
: getPriceForCodeWithAgeFromMap(map, code, age);
const original = next.serviceLines[i];
next.serviceLines[i] = {
...original,
@@ -238,4 +289,32 @@ export function applyComboToForm(form, comboId, patientDOB, options = {}) {
}
return next;
}
export { CODE_MAP, getPriceForCodeWithAgeFromMap };
export { CODE_MAP, CCA_CODE_MAP, DDMA_CODE_MAP, UNITEDDH_CODE_MAP, TUFTSSCO_CODE_MAP, getCodeMap, getPriceForCodeWithAgeFromMap };
/** Compare each service line's totalBilled against the fee schedule.
* Returns lines where the entered price differs from the schedule price.
* Returns empty array if the siteKey has no schedule (United, Tufts, etc.). */
export function findPriceMismatches(serviceLines, insuranceSiteKey, patientDOB, serviceDate) {
const supported = ["MH", "MASSHEALTH", "CCA", "DDMA", "UNITEDDH", "UNITEDSCO", "TUFTSSCO"];
if (!insuranceSiteKey || !supported.includes(insuranceSiteKey.toUpperCase()))
return [];
const map = getCodeMap(insuranceSiteKey);
const mismatches = [];
for (const line of serviceLines) {
const code = normalizeCode(line.procedureCode || "");
if (!code)
continue;
const enteredPrice = new Decimal(Number(line.totalBilled) || 0);
if (enteredPrice.isZero())
continue;
const age = ageOnDate(patientDOB, serviceDate);
const schedulePrice = getPriceForCodeWithAgeFromMap(map, code, age);
if (!schedulePrice.isZero() && !enteredPrice.equals(schedulePrice)) {
mismatches.push({
procedureCode: code,
enteredPrice: enteredPrice.toNumber(),
schedulePrice: schedulePrice.toNumber(),
});
}
}
return mismatches;
}