From e204d30ff6b2d67c6ec572f171365b5638798f0d Mon Sep 17 00:00:00 2001 From: Gitead Date: Wed, 6 May 2026 21:51:15 -0400 Subject: [PATCH] fix: update per-patient collected in reports to use mhPaidAmount + copayment All per-patient SQL queries now compute: collected = mhPaidAmount + copayment balance = totalBilled - collected - adjustment Fixes patient rows showing $0 collected while report total was correct. Co-Authored-By: Claude Sonnet 4.6 --- .../src/storage/payments-reports-storage.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/apps/Backend/src/storage/payments-reports-storage.ts b/apps/Backend/src/storage/payments-reports-storage.ts index 561b7b41..cedbb35f 100755 --- a/apps/Backend/src/storage/payments-reports-storage.ts +++ b/apps/Backend/src/storage/payments-reports-storage.ts @@ -388,8 +388,8 @@ export const paymentsReportsStorage: IPaymentsReportsStorage = { SELECT pay."patientId" AS patient_id, SUM(pay."totalBilled")::numeric(12,2) AS total_charges, - SUM(pay."totalPaid")::numeric(12,2) AS total_paid, - SUM(pay."totalAdjusted")::numeric(12,2) AS total_adjusted, + SUM(COALESCE(pay."mhPaidAmount",0) + pay."copayment")::numeric(12,2) AS total_paid, + SUM(pay."adjustment")::numeric(12,2) AS total_adjusted, MAX(pay."createdAt") AS last_payment_date FROM "Payment" pay ${paymentWhereClause} @@ -509,8 +509,8 @@ export const paymentsReportsStorage: IPaymentsReportsStorage = { SELECT COUNT(*)::int AS cnt FROM ( SELECT pay."patientId" AS patient_id, SUM(pay."totalBilled")::numeric(14,2) AS total_charges, - SUM(pay."totalPaid")::numeric(14,2) AS total_paid, - SUM(pay."totalAdjusted")::numeric(14,2) AS total_adjusted + SUM(COALESCE(pay."mhPaidAmount",0) + pay."copayment")::numeric(14,2) AS total_paid, + SUM(pay."adjustment")::numeric(14,2) AS total_adjusted FROM "Payment" pay ${paymentWhereClause} GROUP BY pay."patientId" @@ -639,10 +639,10 @@ export const paymentsReportsStorage: IPaymentsReportsStorage = { payments_agg AS ( SELECT pay."patientId" AS patient_id, - SUM(pay."totalBilled")::numeric(14,2) AS total_charges, - SUM(pay."totalPaid")::numeric(14,2) AS total_paid, - SUM(pay."totalAdjusted")::numeric(14,2) AS total_adjusted, - MAX(pay."createdAt") AS last_payment_date + SUM(pay."totalBilled")::numeric(14,2) AS total_charges, + SUM(COALESCE(pay."mhPaidAmount",0) + pay."copayment")::numeric(14,2) AS total_paid, + SUM(pay."adjustment")::numeric(14,2) AS total_adjusted, + MAX(pay."createdAt") AS last_payment_date FROM "Payment" pay JOIN "Claim" c ON pay."claimId" = c.id WHERE c."staffId" = ${Number(staffId)}