diff --git a/backend/src/cyclone/migrations/0005_line_reconciliation.sql b/backend/src/cyclone/migrations/0005_line_reconciliation.sql new file mode 100644 index 0000000..687f8f7 --- /dev/null +++ b/backend/src/cyclone/migrations/0005_line_reconciliation.sql @@ -0,0 +1,42 @@ +-- version: 5 +-- SP7: per-service-line adjustment audit +-- See spec: docs/superpowers/specs/2026-06-20-cyclone-line-reconciliation-design.md + +CREATE TABLE service_line_payments ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + remittance_id VARCHAR(64) NOT NULL REFERENCES remittances(id) ON DELETE CASCADE, + line_number INTEGER NOT NULL, + procedure_qualifier VARCHAR(4) NOT NULL, + procedure_code VARCHAR(16) NOT NULL, + modifiers_json TEXT NOT NULL DEFAULT '[]', + charge NUMERIC(12, 2) NOT NULL, + payment NUMERIC(12, 2) NOT NULL, + units NUMERIC(10, 2), + unit_type VARCHAR(8), + service_date DATE, + ref_benefit_plan VARCHAR(64), + superseded_by_id INTEGER REFERENCES service_line_payments(id) ON DELETE SET NULL +); + +CREATE INDEX ix_service_line_payments_remittance_id ON service_line_payments(remittance_id); +CREATE INDEX ix_service_line_payments_procedure_code_date ON service_line_payments(procedure_code, service_date); + +CREATE TABLE line_reconciliations ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + claim_id VARCHAR(64) NOT NULL REFERENCES claims(id) ON DELETE CASCADE, + claim_service_line_id INTEGER REFERENCES service_lines(id) ON DELETE SET NULL, + service_line_payment_id INTEGER REFERENCES service_line_payments(id) ON DELETE SET NULL, + status VARCHAR(16) NOT NULL, + match_score INTEGER, + reconciled_at TIMESTAMP NOT NULL +); + +CREATE INDEX ix_line_reconciliations_claim_id ON line_reconciliations(claim_id); +CREATE INDEX ix_line_reconciliations_claim_service_line_id ON line_reconciliations(claim_service_line_id); +CREATE INDEX ix_line_reconciliations_service_line_payment_id ON line_reconciliations(service_line_payment_id); + +ALTER TABLE cas_adjustments ADD COLUMN service_line_payment_id INTEGER REFERENCES service_line_payments(id) ON DELETE SET NULL; + +ALTER TABLE remittances ADD COLUMN claim_level_adjustment_amount NUMERIC(12, 2) NOT NULL DEFAULT 0; + +CREATE INDEX ix_cas_adjustments_service_line_payment_id ON cas_adjustments(service_line_payment_id);