From f3959ef8c2b974b5b04173abd57363d7d2eee8e0 Mon Sep 17 00:00:00 2001 From: sonzsara Date: Fri, 22 May 2026 14:40:33 +0530 Subject: [PATCH 1/3] Update bed occupancy query to exclude OT and casualty locations --- Care/Encounter/bed_occupancy_ssmm.md | 32 ++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/Care/Encounter/bed_occupancy_ssmm.md b/Care/Encounter/bed_occupancy_ssmm.md index 49b8c25..507447d 100644 --- a/Care/Encounter/bed_occupancy_ssmm.md +++ b/Care/Encounter/bed_occupancy_ssmm.md @@ -19,31 +19,42 @@ Provides a real-time bed occupancy summary at SSMM, broken down by **floor** . F ```sql WITH all_beds AS ( + SELECT parent_loc.name AS level1_name, COALESCE(grandparent_loc.name, parent_loc.name) AS floor, fl.id AS bed_id FROM emr_facilitylocation fl - LEFT JOIN emr_facilitylocation parent_loc ON fl.parent_id = parent_loc.id + LEFT JOIN emr_facilitylocation parent_loc ON fl.parent_id = parent_loc.id LEFT JOIN emr_facilitylocation grandparent_loc ON parent_loc.parent_id = grandparent_loc.id WHERE fl.deleted = FALSE AND fl.status = 'active' AND fl.form = 'bd' AND fl.root_location_id != 300 + AND fl.parent_id NOT IN (19,44) + ), - occupied_beds AS ( + SELECT DISTINCT ON (e.patient_id) - fle.location_id AS bed_id + parent_loc.name AS level1_name, + COALESCE(grandparent_loc.name, parent_loc.name) AS floor, + fl.id AS bed_id FROM emr_facilitylocationencounter fle + INNER JOIN emr_facilitylocation fl ON fle.location_id = fl.id + LEFT JOIN emr_facilitylocation parent_loc ON fl.parent_id = parent_loc.id + LEFT JOIN emr_facilitylocation grandparent_loc ON parent_loc.parent_id = grandparent_loc.id INNER JOIN emr_encounter e ON fle.encounter_id = e.id - INNER JOIN all_beds ab ON fle.location_id = ab.bed_id - WHERE fle.deleted = FALSE + WHERE fl.deleted = FALSE + AND fl.status = 'active' + AND fl.form = 'bd' + AND fl.root_location_id != 300 + AND fl.parent_id NOT IN (19,44) + AND fle.deleted = FALSE --AND DATE(fle.start_datetime) <= {{report_date}} --AND (fle.end_datetime IS NULL OR DATE(fle.end_datetime) > {{report_date}}) ORDER BY e.patient_id, fle.start_datetime DESC ), - bed_stats AS ( SELECT ab.level1_name, @@ -51,12 +62,18 @@ bed_stats AS ( COUNT(DISTINCT ab.bed_id) AS total_bed_count, COUNT(DISTINCT ob.bed_id) AS occupied_bed_count FROM all_beds ab - LEFT JOIN occupied_beds ob ON ab.bed_id = ob.bed_id + LEFT JOIN occupied_beds ob + ON ab.bed_id = ob.bed_id + AND ab.level1_name = ob.level1_name + AND ab.floor = ob.floor GROUP BY ab.level1_name, ab.floor ) + SELECT * FROM ( SELECT * FROM bed_stats + UNION ALL + SELECT 'Total' AS level1_name, 'Total' AS floor, @@ -73,6 +90,7 @@ ORDER BY CASE WHEN floor = 'Total' THEN 1 ELSE 0 END, floor, level1_name; - **`occupied_beds` CTE** — for each patient, the latest bed assignment. - **Hardcoded values:** - `fl.root_location_id != 300` — excludes the fake beds root. Update if the fake-beds root ID changes. + - `fl.parent_id NOT IN (19, 44)` — excludes specific parent locations (In this case OT and casualty). Update these IDs if the excluded wards change. - `fl.form = 'bd'` — only bed-type facility locations. - `fl.status = 'active'` — only currently active beds count toward totals. - **`report_date` filter** — when provided, treats a bed as occupied if `start_datetime <= report_date` and (`end_datetime IS NULL` OR `end_datetime > report_date`). From 87295fa68aaad81d7e4ad93a253af19d871f7a20 Mon Sep 17 00:00:00 2001 From: sonzsara Date: Tue, 26 May 2026 15:35:29 +0530 Subject: [PATCH 2/3] Update bed occupancy query by consolidating joins and removing redundant conditions. --- Care/Encounter/bed_occupancy_ssmm.md | 38 ++++++++-------------------- 1 file changed, 10 insertions(+), 28 deletions(-) diff --git a/Care/Encounter/bed_occupancy_ssmm.md b/Care/Encounter/bed_occupancy_ssmm.md index 507447d..b82d4c7 100644 --- a/Care/Encounter/bed_occupancy_ssmm.md +++ b/Care/Encounter/bed_occupancy_ssmm.md @@ -19,38 +19,26 @@ Provides a real-time bed occupancy summary at SSMM, broken down by **floor** . F ```sql WITH all_beds AS ( - SELECT parent_loc.name AS level1_name, COALESCE(grandparent_loc.name, parent_loc.name) AS floor, fl.id AS bed_id FROM emr_facilitylocation fl - LEFT JOIN emr_facilitylocation parent_loc ON fl.parent_id = parent_loc.id + LEFT JOIN emr_facilitylocation parent_loc ON fl.parent_id = parent_loc.id LEFT JOIN emr_facilitylocation grandparent_loc ON parent_loc.parent_id = grandparent_loc.id WHERE fl.deleted = FALSE AND fl.status = 'active' AND fl.form = 'bd' AND fl.root_location_id != 300 - AND fl.parent_id NOT IN (19,44) - + AND fl.parent_id NOT IN (19, 44) ), occupied_beds AS ( - SELECT DISTINCT ON (e.patient_id) - parent_loc.name AS level1_name, - COALESCE(grandparent_loc.name, parent_loc.name) AS floor, - fl.id AS bed_id + ab.bed_id FROM emr_facilitylocationencounter fle - INNER JOIN emr_facilitylocation fl ON fle.location_id = fl.id - LEFT JOIN emr_facilitylocation parent_loc ON fl.parent_id = parent_loc.id - LEFT JOIN emr_facilitylocation grandparent_loc ON parent_loc.parent_id = grandparent_loc.id - INNER JOIN emr_encounter e ON fle.encounter_id = e.id - WHERE fl.deleted = FALSE - AND fl.status = 'active' - AND fl.form = 'bd' - AND fl.root_location_id != 300 - AND fl.parent_id NOT IN (19,44) - AND fle.deleted = FALSE + JOIN emr_encounter e ON fle.encounter_id = e.id + JOIN all_beds ab ON ab.bed_id = fle.location_id + WHERE fle.deleted = FALSE --AND DATE(fle.start_datetime) <= {{report_date}} --AND (fle.end_datetime IS NULL OR DATE(fle.end_datetime) > {{report_date}}) ORDER BY e.patient_id, fle.start_datetime DESC @@ -62,25 +50,19 @@ bed_stats AS ( COUNT(DISTINCT ab.bed_id) AS total_bed_count, COUNT(DISTINCT ob.bed_id) AS occupied_bed_count FROM all_beds ab - LEFT JOIN occupied_beds ob - ON ab.bed_id = ob.bed_id - AND ab.level1_name = ob.level1_name - AND ab.floor = ob.floor + LEFT JOIN occupied_beds ob ON ab.bed_id = ob.bed_id GROUP BY ab.level1_name, ab.floor ) - SELECT * FROM ( SELECT * FROM bed_stats - UNION ALL - SELECT 'Total' AS level1_name, 'Total' AS floor, - SUM(total_bed_count) AS total_bed_count, - SUM(occupied_bed_count) AS occupied_bed_count + SUM(total_bed_count), + SUM(occupied_bed_count) FROM bed_stats -) AS final_result +) final_result ORDER BY CASE WHEN floor = 'Total' THEN 1 ELSE 0 END, floor, level1_name; ``` From aab452311ba135a0e87d5f87bfe90853fd6a9527 Mon Sep 17 00:00:00 2001 From: sonzsara Date: Thu, 28 May 2026 15:21:49 +0530 Subject: [PATCH 3/3] Add Billed / Paid Charge Item Report documentation --- .../billed_paid_chargeitem_report_pallium.md | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Care/Accounting/billed_paid_chargeitem_report_pallium.md diff --git a/Care/Accounting/billed_paid_chargeitem_report_pallium.md b/Care/Accounting/billed_paid_chargeitem_report_pallium.md new file mode 100644 index 0000000..a373042 --- /dev/null +++ b/Care/Accounting/billed_paid_chargeitem_report_pallium.md @@ -0,0 +1,54 @@ + +# Billed / Paid Charge Item Report - Pallium + +> List of charge items currently in `billed` or `paid` status with patient, item, modifier and pricing details + +## Purpose + + Shows every charge item that has been billed or paid, along with the patient (and their Pallium ID), the charge item title and quantity, total price, when it was last modified and by whom. + +## Parameters + +| Parameter | Type | Description | Example | +|-----------|------|-------------|---------| +| `DATE` | DATE / range | Metabase date filter (typically bound to `ci.modified_date`) | `'2026-05-28'` | +| `chargeitem` | TEXT (multi) | Filter by one or more charge item titles | `'Consultation', 'X-Ray'` | + +--- + +## Query + +```sql +SELECT + p.name AS patient_name, + pi.value AS ssmm_id, + ci.modified_date, + ci.status, + ci.title, + ci.quantity, + TRIM(modified_user.first_name || ' ' || COALESCE(modified_user.last_name, '')) AS modified_by, + ci.total_price +FROM emr_chargeitem ci +JOIN emr_patient p ON ci.patient_id = p.id +LEFT JOIN emr_patientidentifier pi ON p.id = pi.patient_id AND pi.config_id = 4 +LEFT JOIN users_user modified_user ON ci.updated_by_id = modified_user.id +WHERE ci.status IN ('billed', 'paid') + --[[AND {{DATE}}]] + --[[AND ci.title IN ({{chargeitem}})]] +ORDER BY ci.modified_date DESC; +``` + + +## Notes + +- **Hardcoded values:** + - `pi.config_id = 4` — the identifier config representing the Pallium ID. Update if the config ID changes. +- **Metabase filters:** + - `[[AND {{DATE}}]]` is a field filter — bind it to `ci.modified_date` (or `ci.created_date`) in the Metabase variable settings. + - `[[AND ci.title IN ({{chargeitem}})]]` accepts a multi-select list of charge item titles. +- **Ordering** — most recently modified first. + +*Last updated: 2026-05-28* + +```` +