From 3f6b44a33b869da37d731b4f9992f44c11c962d2 Mon Sep 17 00:00:00 2001 From: kert Date: Fri, 13 Nov 2026 04:12:20 -0500 Subject: [PATCH] =?UTF-8?q?feat(lex):=20Payment=20view=20=E2=80=94=20every?= =?UTF-8?q?=20year,=20proposed=20apart=20from=20final,=20charts=20(S1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../public/assets/dossier/view-payment.js | 232 +++++++++++++++++- tests/lex/views-payment.test.mjs | 98 ++++++++ 2 files changed, 325 insertions(+), 5 deletions(-) create mode 100644 tests/lex/views-payment.test.mjs diff --git a/infra/gitea/custom/public/assets/dossier/view-payment.js b/infra/gitea/custom/public/assets/dossier/view-payment.js index 79653cb..3dfeb5f 100644 --- a/infra/gitea/custom/public/assets/dossier/view-payment.js +++ b/infra/gitea/custom/public/assets/dossier/view-payment.js @@ -1,10 +1,232 @@ -// lex · Payment view. A stub until S1 Task 8 fills it in. +// lex · Payment view: national non-facility payment = non-facility total RVUs +// x conversion factor, one column per year on file. // -// A view module exports needs (the API slices it reads), render(data, state) -// returning markup, and optionally mount(root, data, state, nav). +// Proposed values sit in their own hatched column (with a "proposed" header), +// before that year's final column, and are never blended with final values. +// Every code group shows by default; the Active/Deleted/Linked buttons only +// filter (S1-P2). The metric and group buttons are UI state, not URL state. + +import { esc, int, money, n2 } from "./fmt.js"; +import { citeHtml, factHtml } from "./cite.js"; export const needs = ["header", "payment"]; -export function render() { - return `

This view is being built.

`; +const METRICS = [ + ["pay_nf", "Payment $"], + ["work", "Work RVU"], + ["total_nf", "Total RVU (non-facility)"], +]; +const GROUPS = [ + ["active", "Active"], + ["deleted", "Deleted"], + ["linked", "Linked"], +]; + +const ui = { metric: "pay_nf", group: "all" }; + +const byId = (a, b) => parseInt(String(a.id).slice(1), 10) - parseInt(String(b.id).slice(1), 10); +const groups = (P) => P.code_groups || {}; +const allCodes = (P) => [...(groups(P).active || []), ...(groups(P).deleted || []), ...(groups(P).linked || [])]; +const shownCodes = (P, group) => (group === "all" ? allCodes(P) : groups(P)[group] || []); + +/** The columns: per year, the proposed one first when that year has one. */ +function columns(rows) { + const keys = new Map(); + for (const r of rows) keys.set(r.vintage, { vintage: r.vintage, year: r.year, proposed: !!r.proposed }); + return Array.from(keys.values()).sort((a, b) => a.year - b.year || (b.proposed ? 1 : 0) - (a.proposed ? 1 : 0)); +} + +const fmt = (metric, v) => (metric === "pay_nf" ? money(v) : n2(v)); + +function firstOf(rows, vintage) { + return rows.find((r) => r.vintage === vintage) || null; +} + +/** The table: a code row per code, a column per vintage, then CF and Source. */ +export function payTable(P, { metric = "pay_nf", group = "all" } = {}, code = "") { + const rows = P.rows || []; + const cols = columns(rows); + const cells = P.cells || {}; + const cls = (c, extra = "") => `${extra}${c.proposed ? " pcol" : ""}`.trim(); + let h = `
`; + h += cols.map((c) => ``).join(""); + h += ``; + for (const k of shownCodes(P, group)) { + h += ``; + for (const c of cols) { + const r = rows.find((x) => x.code === k && x.vintage === c.vintage); + if (!r) { + h += ``; + continue; + } + const id = cells[`${k}|${c.vintage}`]; + const fact = id ? ` data-fact="${esc(id)}" tabindex="0"` : ""; + const v = r[metric]; + const unpaid = metric === "pay_nf" && v == null; + h += ``; + } + h += ``; + } + h += `${cols.map((c) => ``).join("")}`; + h += `${cols + .map((c) => { + const r = firstOf(rows, c.vintage); + const cite = r ? { kind: "rvu_file", label: r.citation, url: r.url, locator: "" } : null; + return ``; + }) + .join("")}`; + return h + `
CodeCY${esc(c.year)}
${c.proposed ? "proposed" : "final"}
${esc(k)}—${unpaid ? esc(r.status) : esc(fmt(metric, v))}
Conv. factor${esc(n2((firstOf(rows, c.vintage) || {}).cf))}
Source${citeHtml(cite)}
`; +} + +const legend = (inner) => `
${inner}
`; + +/** National non-facility payment for a code: the final line, proposed rings. */ +export function payChart(rows, code) { + const mine = rows.filter((p) => p.code === code); + const fin = mine.filter((p) => !p.proposed && p.pay_nf != null).sort((a, b) => a.year - b.year); + const pro = mine.filter((p) => p.proposed && p.pay_nf != null).sort((a, b) => a.year - b.year); + const ys = [...fin, ...pro].map((p) => p.pay_nf); + if (!ys.length) return `

No RVU-based payment for ${esc(code)} (status ${esc((mine[0] || {}).status || "none on file")}).

`; + const years = Array.from(new Set(rows.map((p) => p.year))).sort((a, b) => a - b); + const x0 = years[0]; + const x1 = years[years.length - 1]; + const W = 640, H = 230, L = 58, R = 64, T = 18, B = 30; + const top = Math.max(...ys); + const step = top > 150 ? 50 : top > 60 ? 25 : 10; + const ymax = Math.ceil((top * 1.08) / step) * step; + const X = (y) => (x1 === x0 ? (L + W - R) / 2 : L + ((y - x0) / (x1 - x0)) * (W - L - R)); + const Y = (v) => T + (1 - v / ymax) * (H - T - B); + let s = ``; + for (let v = 0; v <= ymax + 1e-9; v += step) s += `$${v}`; + for (const y of years) if ((y - x0) % 2 === 0 || y === x1) s += `${esc(y)}`; + s += ``; + let path = ""; + let prev = null; + for (const p of fin) { + path += (prev != null && p.year === prev + 1 ? "L" : "M") + X(p.year).toFixed(1) + " " + Y(p.pay_nf).toFixed(1); + prev = p.year; + } + s += ``; + const tip = (p, what) => esc(`${code} · CY${p.year} ${what} · ${money(p.pay_nf)} · CF ${p.cf == null ? "—" : "$" + Number(p.cf).toFixed(4)}`); + for (const p of fin) s += ``; + for (const p of pro) s += ``; + const last = fin[fin.length - 1]; + if (last) s += `${esc(money(last.pay_nf))}`; + return ( + s + + `` + + legend(`Final (Addendum B × CF)Proposed (proposed-rule Addendum B × proposed CF)`) + ); +} + +/** Medicare allowed services for a code, one bar per year. */ +export function volChart(utilization, code) { + const all = utilization || []; + const mine = all.filter((u) => u.hcpcs === code && u.n_services != null); + if (!mine.length) return `

No national utilization rows for ${esc(code)}.

`; + const lo = Math.min(...all.map((u) => u.year)); + const hi = Math.max(...all.map((u) => u.year)); + const yrs = []; + for (let y = lo; y <= hi; y++) yrs.push(y); + const W = 640, H = 150, L = 58, R = 64, T = 12, B = 26; + const vmax = Math.max(...mine.map((r) => r.n_services)); + const mag = Math.pow(10, Math.floor(Math.log10(vmax))); + const step = vmax / mag > 5 ? mag * 2 : vmax / mag > 2 ? mag : mag / 2; + const ymax = Math.ceil(vmax / step) * step; + const bw = (W - L - R) / yrs.length; + const X = (y) => L + (y - lo) * bw; + const Y = (v) => T + (1 - v / ymax) * (H - T - B); + let s = ``; + for (let v = 0; v <= ymax + 1e-9; v += step) s += `${v >= 1e6 ? (v / 1e6).toFixed(1) + "M" : v >= 1000 ? Math.round(v / 1000) + "k" : v}`; + for (const y of yrs) { + const r = mine.find((x) => x.year === y); + if (r) { + const h = Y(0) - Y(r.n_services); + s += ``; + } + if ((y - lo) % 2 === 0 || y === hi) s += `${esc(y)}`; + } + return s + ``; +} + +function controls(P) { + const g = groups(P); + const n = (k) => (g[k] || []).length; + let h = `
Show`; + h += METRICS.map(([k, label]) => ``).join(""); + h += `Codes`; + h += ``; + h += GROUPS.map(([k, label]) => ``).join(""); + return h + `
`; +} + +function charts(P, code) { + return `

${esc(code)}: national non-facility payment

${payChart(P.rows || [], code)}
+

${esc(code)}: Medicare allowed services

${volChart(P.utilization, code)}
+

National totals, all places of service, from CMS's Medicare Physician & Other Practitioners by Geography and Service files.

`; +} + +function cfNotes(P) { + const seen = new Set(); + const items = []; + for (const c of columns(P.rows || [])) { + const r = firstOf(P.rows, c.vintage); + const note = r && r.cf_note; + if (!note || seen.has(c.vintage + "|" + note)) continue; + seen.add(c.vintage + "|" + note); + items.push(`
  • CY${esc(c.year)} ${c.proposed ? "proposed" : "final"} ${esc(note)}
  • `); + } + return items.length ? `

    Conversion factor notes

    ` : ""; +} + +const selectedCode = (P, state) => (state && state.code) || (groups(P).active || [])[0] || allCodes(P)[0] || ""; + +export function render(data, state) { + const P = data.payment; + const code = selectedCode(P, state); + let h = `

    Payment

    +

    National unadjusted non-facility payment = non-facility total RVUs × conversion factor, for every year on file. Final values come from each final rule's RVU file; proposed values from the proposed rule's file. They sit in separate columns and are never combined.

    `; + if (!(P.rows || []).length) return h + `

    No payment rows captured for this family.

    `; + h += controls(P); + h += legend(`proposed hatched columnsfinal plain columnsSelect a code to chart it.`); + h += `
    ${payTable(P, ui, code)}
    `; + h += `

    The table opens at the latest years; scroll it sideways for earlier ones. A status letter in place of a dollar value means the status is not paid from RVUs × CF (hover for the reason). A dash means the code is not in that year's file.

    `; + h += charts(P, code); + h += cfNotes(P); + const facts = P.facts || {}; + const un = (P.unplaced || []).map((id) => facts[id]).filter(Boolean).sort(byId); + if (un.length) h += `
    Unplaced payment facts

    Facts the table has no cell for. They are listed so none is lost.

    `; + return h; +} + +export function mount(root, data, state, nav) { + const P = data.payment; + const code = selectedCode(P, state); + const tbl = root.querySelector("#pay-tbl"); + if (!tbl) return; + const scrollEnd = () => { + const sc = root.querySelector("#pay-scroll"); + if (sc) sc.scrollLeft = sc.scrollWidth; + }; + scrollEnd(); + const bar = root.querySelector("#pay-f"); + bar.addEventListener("click", (e) => { + const b = e.target.closest("button"); + if (!b) return; + if (b.dataset.m) ui.metric = b.dataset.m; + if (b.dataset.g) ui.group = b.dataset.g; + for (const x of bar.querySelectorAll("[data-m]")) x.setAttribute("aria-pressed", String(x.dataset.m === ui.metric)); + for (const x of bar.querySelectorAll("[data-g]")) x.setAttribute("aria-pressed", String(x.dataset.g === ui.group)); + const sc = root.querySelector("#pay-scroll"); + const left = sc ? sc.scrollLeft : 0; + tbl.innerHTML = payTable(P, ui, code); + const sc2 = root.querySelector("#pay-scroll"); + if (sc2) sc2.scrollLeft = left; + }); + // a click on a row (not on a value cell, which opens its fact) charts that code + tbl.addEventListener("click", (e) => { + if (e.target.closest("a, button, [data-fact]")) return; + const tr = e.target.closest("tr[data-code]"); + if (tr) nav.go({ code: tr.dataset.code }, { push: false }); + }); } diff --git a/tests/lex/views-payment.test.mjs b/tests/lex/views-payment.test.mjs new file mode 100644 index 0000000..b6cd6b0 --- /dev/null +++ b/tests/lex/views-payment.test.mjs @@ -0,0 +1,98 @@ +// lex · Payment view. +import { test } from "node:test"; +import { equal as eq, ok } from "node:assert/strict"; +import { readFileSync } from "node:fs"; + +import { parseLocation } from "../../infra/gitea/custom/public/assets/dossier/state.js"; +import * as payment from "../../infra/gitea/custom/public/assets/dossier/view-payment.js"; + +const fx = (name) => JSON.parse(readFileSync(new URL(`./fixtures/${name}.json`, import.meta.url), "utf8")); +const H = fx("header"); +const P = fx("payment"); +const st = (url) => parseLocation(new URL(url, "https://lex.test")); +const count = (s, sub) => s.split(sub).length - 1; +const page = (url = "/payment?family=HOME-VISITS", p = P) => payment.render({ header: H, payment: p }, st(url)); + +test("payment: every cell fact, a hatched proposed column, the unplaced fact", () => { + const p = page(); + for (const [k, id] of Object.entries(P.cells)) ok(p.includes(`data-fact="${id}"`), k); + ok(p.includes('data-fact="F18"') && p.includes("proposed") && p.includes("pcol")); + eq(payment.needs.join(), "header,payment"); + ok(p.includes("Unplaced payment facts") && p.includes("Invented orphan payment.")); +}); + +test("payment: the proposed column sits before its year's final column, apart", () => { + const t = payment.payTable(P, { metric: "pay_nf", group: "all" }); + const heads = t.match(/]*>.*?<\/th>/g); + const i27 = heads.findIndex((h) => h.includes("CY2027")); + ok(heads[i27].includes("pcol") && heads[i27].includes("proposed")); + ok(heads.findIndex((h) => h.includes("CY2023")) < i27); + eq(heads.length, 1 + 3, "code, CY2022, CY2023, CY2027 proposed"); + // the proposed cell is in the hatched column, the final one is not + ok(/class="[^"]*pcol[^"]*"[^>]*data-fact="F12"|data-fact="F12"[^>]*class="[^"]*pcol/.test(t)); + ok(!/pcol[^>]*data-fact="F11"|data-fact="F11"[^>]*pcol/.test(t)); +}); + +test("payment: every group shows by default, the buttons only filter", () => { + const all = page(); + for (const c of ["99341", "99343", "G0318"]) ok(all.includes(`data-code="${c}"`), c); + ok(all.includes("Active 1") && all.includes("Deleted 1") && all.includes("Linked 1")); + const del = payment.payTable(P, { metric: "pay_nf", group: "deleted" }); + ok(del.includes('data-code="99343"') && !del.includes('data-code="99341"')); +}); + +test("payment: metric buttons, the value follows the metric", () => { + const pay = payment.payTable(P, { metric: "pay_nf", group: "all" }); + const work = payment.payTable(P, { metric: "work", group: "all" }); + const tot = payment.payTable(P, { metric: "total_nf", group: "all" }); + ok(pay.includes("$69.30") && !pay.includes(">2.10<")); + ok(work.includes(">1.00<") && !work.includes("$69.30")); + ok(tot.includes(">2.10<")); + const h = page(); + ok(h.includes("Payment $") && h.includes("Work RVU") && h.includes("Total RVU")); +}); + +test("payment: an unpaid status shows its letter with the note, a missing row a dash", () => { + const rows = P.rows.map((r) => (r.code === "99341" && r.year === 2023 ? { ...r, status: "B", pay_nf: null, status_note: "bundled — no separate PFS payment" } : r)); + const t = payment.payTable({ ...P, rows }, { metric: "pay_nf", group: "all" }); + ok(/title="bundled — no separate PFS payment"[^>]*>B—<")); +}); + +test("payment: CF row, source row, CF notes unchanged and escaped", () => { + const rows = P.rows.map((r) => (r.vintage === "CY2027 proposed" ? { ...r, cf_note: "proposed; QP CF $33.1693 ", citation: "CY2027 RVU" } : r)); + const p = page("/payment?family=HOME-VISITS", { ...P, rows }); + ok(p.includes("Conv. factor") && p.includes("Source") && p.includes("33.00")); + ok(p.includes("proposed; QP CF $33.1693 <b>") && !p.includes("\n")); + ok(p.includes("CY2027 <i>RVU</i>") && !p.includes("RVU")); + ok(p.includes('href="https://example.test/rvu"')); +}); + +test("payment: the payment chart is a final line with proposed rings", () => { + const c = payment.payChart(P.rows, "99341"); + ok(c.includes(" ({ ...r, status: "B", pay_nf: null })); + ok(payment.payChart(unpaid, "99341").includes("status B")); + ok(!payment.payChart(P.rows, '').includes("")); +}); + +test("payment: the volume chart bars one year each", () => { + const v = payment.volChart(P.utilization, "99341"); + eq(count(v, "services\""), 2); + ok(v.includes(" { + ok(page().includes("99341: national non-facility payment"), "default is the first active code"); + const p = page("/payment?family=HOME-VISITS#code=99343"); + ok(p.includes("99343: national non-facility payment") && p.includes("99343: Medicare allowed services")); + ok(/]*data-code="99343"[^>]*class="sel"|]*class="sel"[^>]*data-code="99343"/.test(p)); +}); + +test("payment: no payment rows is a plain message", () => { + const p = page("/payment?family=HOME-VISITS", { ...P, rows: [], cells: {}, unplaced: [], utilization: [], years: [], code_groups: { active: [], deleted: [], linked: [] } }); + ok(p.includes("No payment rows")); +});