Skip to content

Add sales report (GST Register) for SSMM with detailed query and parameters - #179

Open
sonzsara wants to merge 1 commit into
mainfrom
ENG-1150
Open

sonzsara wants to merge 1 commit into
mainfrom
ENG-1150

Conversation

@sonzsara

Copy link
Copy Markdown
Contributor

No description provided.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. Critical: there is no facility scope, so numbers span all facilities.
  2. High: the date filter is non-sargable.
  3. 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant