diff --git a/src/pfs/valuation.py b/src/pfs/valuation.py index 48194e7..94c8864 100644 --- a/src/pfs/valuation.py +++ b/src/pfs/valuation.py @@ -232,22 +232,37 @@ def proposed_valuation( return out +def skipped_years(con: Any, codes: Sequence[str]) -> list[int]: + """``pfs.rvu`` years for *codes* with no ``RULES`` entry (no CF, so + they cannot be priced) — the dossier's gap notes.""" + wanted = sorted({c.upper() for c in codes}) + if not wanted: + return [] + return [ + int(y) + for (y,) in con.execute(_UNKNOWN_YEARS_SQL, [wanted, sorted(RULES)]).fetchall() + ] + + def valuation( - con: Any, codes: Sequence[str], *, years: int = 4 + con: Any, + codes: Sequence[str], + *, + years: int = 4, + all_proposed: bool = False, ) -> tuple[list[ValuationRow], list[str]]: """Rows for *codes* (last *years* final vintages + the newest NPRM), ordered by code then year with the proposed row last per code, and the - codes that produced no rows at all.""" + codes that produced no rows at all. ``years <= 0`` returns every final + year ``RULES`` knows; ``all_proposed`` returns one proposed row per + captured NPRM vintage instead of only the newest.""" wanted = sorted({c.upper() for c in codes}) if not wanted: return [], [] by_code: dict[str, list[ValuationRow]] = {c: [] for c in wanted} rule_years = sorted(RULES) - skipped = [ - int(y) - for (y,) in con.execute(_UNKNOWN_YEARS_SQL, [wanted, rule_years]).fetchall() - ] + skipped = skipped_years(con, wanted) if skipped: log.warning( "valuation: %s priced in pfs.rvu year(s) %s with no pfs.rules entry " @@ -257,7 +272,7 @@ def valuation( ) for hcpcs, year, desc, status, work, pe_nf, pe_f, mp, tot_nf, tot_f in con.execute( - _FINAL_SQL, [wanted, rule_years, years] + _FINAL_SQL, [wanted, rule_years, years if years > 0 else 10**6] ).fetchall(): rule = RULES[int(year)] cf = rule.conversion_factor @@ -287,8 +302,12 @@ def valuation( ) ) - nprm_year, _tag, rule_id, _pin = max(NPRM_SOURCES, key=lambda s: s[0]) - for row in _proposed_rows(con, wanted, nprm_year, rule_id): + if all_proposed: + proposed = proposed_valuation(con, wanted) + else: + nprm_year, _tag, rule_id, _pin = max(NPRM_SOURCES, key=lambda s: s[0]) + proposed = _proposed_rows(con, wanted, nprm_year, rule_id) + for row in proposed: by_code[row.code].append(row) rows = [ diff --git a/tests/pfs/test_valuation.py b/tests/pfs/test_valuation.py index 52efe5c..d09efc8 100644 --- a/tests/pfs/test_valuation.py +++ b/tests/pfs/test_valuation.py @@ -338,3 +338,86 @@ class TestProposedValuation: monkeypatch.setattr(v, "proposed_for", lambda y: None) assert proposed_valuation(pcon, ["99350"]) == [] + + +class TestEveryYear: + @pytest.fixture + def acon(self): + c = duckdb.connect(":memory:") + c.execute("CREATE SCHEMA pfs") + c.execute(f"CREATE TABLE pfs.rvu ({RVU_COLS})") + c.execute(f"CREATE TABLE pfs.rvu_proposed ({PROPOSED_COLS})") + c.executemany( + "INSERT INTO pfs.rvu VALUES (?,?,?,?,?,?,?,?,?,?,?,?)", + [ + ("99341", None, "Home visit", "A", 1.0, 1.0, 0.5, 0.1, 2.1, 1.6, 1.0, y) + for y in range(2015, 2028) + ] + + [ + ( + "99343", + None, + "Home visit 3", + "A", + 2.0, + 1.0, + 0.5, + 0.1, + 3.1, + 2.6, + 1.0, + y, + ) + for y in range(2015, 2023) + ], + ) + c.executemany( + "INSERT INTO pfs.rvu_proposed VALUES (?,?,?,?,?,?,?,?,?)", + [ + ("99341", None, "Home visit", "A", 1.1, 1.0, 0.5, 0.1, "CMS-1832-P"), + ("99341", None, "Home visit", "A", 1.2, 1.0, 0.5, 0.1, "CMS-1848-P"), + ], + ) + yield c + c.close() + + def test_valuation_all_years(self, acon): + rows, _ = valuation(acon, ["99341"], years=0) + assert [r.year for r in rows if not r.proposed] == list(range(2015, 2028)) + assert len([r for r in rows if not r.proposed]) == 13 + + def test_all_proposed_rows_per_nprm_year(self, acon): + rows, _ = valuation(acon, ["99341"], years=1, all_proposed=True) + prop = [r for r in rows if r.proposed] + assert [r.vintage for r in prop] == ["CY2026 proposed", "CY2027 proposed"] + assert [r.cf for r in prop] == [ + proposed_for(2026).conversion_factor, + proposed_for(2027).conversion_factor, + ] + assert [r.work for r in prop] == [1.1, 1.2] + + def test_default_unchanged(self, acon): + rows, _ = valuation(acon, ["99341"]) + assert [(r.year, r.proposed, r.work) for r in rows] == [ + (2024, False, 1.0), + (2025, False, 1.0), + (2026, False, 1.0), + (2027, False, 1.0), + (2027, True, 1.2), + ] + + def test_deleted_code_rows_stop_at_last_year(self, acon): + rows, unpriced = valuation(acon, ["99343"], years=0, all_proposed=True) + assert unpriced == [] + assert [r.year for r in rows] == list(range(2015, 2023)) + assert not any(r.proposed for r in rows) + + def test_skipped_years(self, acon, monkeypatch): + import pfs.valuation as v + + acon.execute( + "INSERT INTO pfs.rvu VALUES ('99341',NULL,'x','A',1,1,1,1,1,1,1.0,2014)" + ) + monkeypatch.setattr(v, "RULES", {y: r for y, r in RULES.items() if y != 2014}) + assert v.skipped_years(acon, ["99341"]) == [2014] + assert v.skipped_years(acon, []) == []