From cdfba9f3b3c5e0d436134c92c1e68687ae7f068d Mon Sep 17 00:00:00 2001 From: sonzsara Date: Tue, 12 May 2026 12:25:11 +0530 Subject: [PATCH 1/2] Add documentation for most sold stock items query --- Care/Operations/mostsold_ssmm.md | 54 ++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Care/Operations/mostsold_ssmm.md diff --git a/Care/Operations/mostsold_ssmm.md b/Care/Operations/mostsold_ssmm.md new file mode 100644 index 0000000..c1395dc --- /dev/null +++ b/Care/Operations/mostsold_ssmm.md @@ -0,0 +1,54 @@ + +# Most Sold Stock Items + +> Total quantity dispensed, selling price, purchase price, and margin per stock item + +## Purpose + +Identifies the most-selling stock items by aggregating dispensed quantities and computing the gross margin (selling price minus purchase cost). + +## Parameters + +| Parameter | Type | Description | Example | +|-----------|------|-------------|---------| +| `stock_name` | TEXT | Filter by stock item name (exact match) | `'Paracetamol'` | +| `DATE` | DATE | Filter by date range on the charge item | `2026-05-01` | + +--- + +## Query + +```sql +SELECT + pk.name AS stock_name, + SUM(md.quantity) AS total_quantity_dispensed, + SUM(ci.total_price) AS selling_price, + SUM(p.purchase_price * md.quantity) AS purchase_price, + SUM(ci.total_price - (p.purchase_price * md.quantity)) AS total_margin +FROM emr_medicationdispense md +JOIN emr_chargeitem ci ON md.charge_item_id = ci.id +JOIN emr_inventoryitem ii ON md.item_id = ii.id +JOIN emr_product p ON ii.product_id = p.id +JOIN emr_productknowledge pk ON p.product_knowledge_id = pk.id +WHERE md.status IN ('completed', 'in_progress', 'preparation') + AND md.deleted = FALSE + AND ci.deleted = FALSE + AND ci.status IN ('billed', 'paid') + --[[AND pk.name = {{stock_name}}]] + --[[AND {{DATE}}]] +GROUP BY pk.name +ORDER BY total_quantity_dispensed DESC; +``` + + +## Notes + +- Only non-deleted medication dispenses and charge items are included. +- Dispense status is filtered to `completed`, `in_progress`, and `preparation` to capture both fulfilled and active dispenses. +- Charge item status is filtered to `billed` and `paid` to ensure only items that have generated revenue are counted. +- Metabase-specific filters (`[[...]]`) allow dynamic filtering in dashboards. +- Results are ordered by highest dispensed quantity first. + +*Last updated: 2026-05-12* + + From 852eaa7615022c2dfc01e7fcae2dc4b6c76277c6 Mon Sep 17 00:00:00 2001 From: sonzsara Date: Wed, 20 May 2026 15:50:40 +0530 Subject: [PATCH 2/2] Updated SQL query for most sold stock items by removing unnecessary joins . --- Care/Operations/mostsold_ssmm.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Care/Operations/mostsold_ssmm.md b/Care/Operations/mostsold_ssmm.md index c1395dc..e77b6c6 100644 --- a/Care/Operations/mostsold_ssmm.md +++ b/Care/Operations/mostsold_ssmm.md @@ -27,12 +27,11 @@ SELECT SUM(ci.total_price - (p.purchase_price * md.quantity)) AS total_margin FROM emr_medicationdispense md JOIN emr_chargeitem ci ON md.charge_item_id = ci.id -JOIN emr_inventoryitem ii ON md.item_id = ii.id -JOIN emr_product p ON ii.product_id = p.id +JOIN emr_chargeitemdefinition cid ON ci.charge_item_definition_id = cid.id +JOIN emr_product p ON p.charge_item_definition_id = cid.id JOIN emr_productknowledge pk ON p.product_knowledge_id = pk.id WHERE md.status IN ('completed', 'in_progress', 'preparation') AND md.deleted = FALSE - AND ci.deleted = FALSE AND ci.status IN ('billed', 'paid') --[[AND pk.name = {{stock_name}}]] --[[AND {{DATE}}]] @@ -43,7 +42,7 @@ ORDER BY total_quantity_dispensed DESC; ## Notes -- Only non-deleted medication dispenses and charge items are included. +- Only non-deleted medication dispenses are included. - Dispense status is filtered to `completed`, `in_progress`, and `preparation` to capture both fulfilled and active dispenses. - Charge item status is filtered to `billed` and `paid` to ensure only items that have generated revenue are counted. - Metabase-specific filters (`[[...]]`) allow dynamic filtering in dashboards.