Repository navigation
Conversation
There was a problem hiding this comment.
Analytics SQL Review — GST sales register for SSMM (sales_report_ssmm.md)
Ticket: ENG-1150 "Build Sales Report" has an empty description and empty comments, so I can't verify metric, grain, filters or facility scope. I reviewed against the doc's own stated intent (one row per issued/balanced bill, GST slab split, Total row). Please add requirements to the ticket, or confirm that intent.
Verdict: not safe to publish to the dashboard yet. The tax and line-grain logic looks careful: lines are pre-aggregated per bill, so there's no fan-out. The doc is thorough, magic values are documented, and lint is clean. But:
- Critical: there is no facility scope, so numbers span all facilities.
- High: the date filter is non-sargable.
- Medium: charge items aren't excluded on
entered_in_error.
Also worth confirming with the ticket author: refunds are not netted off, which the Notes already call out.
Generated by Analytics SQL Reviewer for #179 · auto · 14.8 AIC · ⊞ 5.3K
| SELECT inv.id, inv.number, inv.patient_id, inv.total_gross, | ||
| DATE(CASE WHEN inv.issue_date < TIMESTAMPTZ '2026-06-18 00:00:00+05:30' | ||
| THEN inv.issue_date AT TIME ZONE 'UTC' | ||
| ELSE inv.issue_date AT TIME ZONE 'Asia/Kolkata' END) AS bill_date |
There was a problem hiding this comment.
Critical — no facility scoping. emr_invoice is read with no facility_id predicate, so this "SSMM" register aggregates invoices from every facility on the deployment. The _ssmm suffix doesn't scope anything. For a GST filing report that is both a wrong-number and a data-leak problem. Add a facility filter (e.g. AND inv.facility_id = {{facility_id}}, or a hardcoded ID documented in Notes per the bed_occupancy_ssmm.md style) and add it to the Parameters table. If one deployment really is one facility, say so in Notes.
| COALESCE(SUM((c->>'amount')::numeric) FILTER (WHERE c->'code'->>'code' = 'igst'), 0) AS igst | ||
| FROM jsonb_array_elements(ci.total_price_components::jsonb) c | ||
| WHERE c->>'monetary_component_type' = 'tax' | ||
| ) t |
There was a problem hiding this comment.
Medium — charge item status not checked. Only ci.deleted = FALSE is applied; emr_chargeitem also has a status and records are normally voided via entered_in_error, not deleted. An entered_in_error line on an issued invoice would still feed taxable value and GST. Please confirm the status values in care/emr/models/charge_item.py and exclude entered_in_error (and any other void status) here, or note in Notes why it's not needed.
| FROM emr_invoice inv | ||
| WHERE inv.status IN ('issued', 'balanced') | ||
| AND inv.is_refund = FALSE | ||
| AND inv.deleted = FALSE |
There was a problem hiding this comment.
High — non-sargable date filter. DATE(CASE ... AT TIME ZONE ...) wraps issue_date, so no index can be used and emr_invoice is scanned in full. Since the cutover is a fixed instant, express it as a range on the raw column, e.g. inv.issue_date >= ({{date_from}}::timestamp AT TIME ZONE <tz>) AND inv.issue_date < (({{date_to}} + 1)::timestamp AT TIME ZONE <tz>), using UTC for dates before the cutover and IST after (or a UNION ALL of the two eras). Keep bill_date as a computed output column only.
No description provided.