From ec8f6ecab16ab234c55252c913417d6b1490436b Mon Sep 17 00:00:00 2001 From: sonzsara Date: Wed, 17 Jun 2026 13:09:21 +0530 Subject: [PATCH 1/2] Add expiring items query documentation for next month inventory --- Care/Inventory/expiring_items.md | 53 ++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Care/Inventory/expiring_items.md diff --git a/Care/Inventory/expiring_items.md b/Care/Inventory/expiring_items.md new file mode 100644 index 0000000..e1e28f3 --- /dev/null +++ b/Care/Inventory/expiring_items.md @@ -0,0 +1,53 @@ + +# Expiring Items - Next Month + +> Inventory items expiring in the next calendar month, with batch, supplier, and supply details + +## Purpose + +Lists active inventory products at a specific facility whose `expiration_date` falls within the next calendar month. Each row shows the batch / lot number, stock name, quantity supplied, supply date, expiry date, and the originating supplier. + +## Parameters + +| Parameter | Type | Description | Example | +|-----------|------|-------------|---------| +| `stock_name` | TEXT | Filter by stock / product name (exact match on `emr_productknowledge.name`) | `'Paracetamol'` | +| `supplier` | TEXT | Filter by supplier organization name (exact match on `emr_organization.name`) | `'MedSupplier Pvt Ltd'` | + +--- + +## Query + +```sql +SELECT + p.batch ->> 'lot_number' AS batch, + pk.name AS stock_name, + sd.supplied_item_quantity AS quantity, + sd.created_date AS supply_date, + p.expiration_date AS expiry_date, + org.name AS supplier_name +FROM emr_inventoryitem ii +JOIN emr_product p ON ii.product_id = p.id +JOIN emr_productknowledge pk ON p.product_knowledge_id = pk.id +JOIN emr_supplydelivery sd ON sd.supplied_inventory_item_id = ii.id +JOIN emr_deliveryorder d ON sd.order_id = d.id +JOIN emr_organization org ON d.supplier_id = org.id +WHERE p.status = 'active' + AND p.facility_id = 11 + AND sd.status IN ('completed','in_progress') + AND p.expiration_date >= date_trunc('month', CURRENT_DATE + INTERVAL '1 month') + AND p.expiration_date < date_trunc('month', CURRENT_DATE + INTERVAL '2 months') + --[[AND pk.name = {{stock_name}}]] + --[[AND org.name = {{supplier}}]] +ORDER BY p.expiration_date, pk.name, batch; +``` + +## Notes + +- The date window covers **the next calendar month** (from the 1st of next month up to but not including the 1st of the month after). Adjust the `INTERVAL` values to widen or shift the window. +- `p.facility_id = 11` is hardcoded — change this to target a different facility. +- Only products with `status = 'active'` and supply deliveries in `completed` / `in_progress` status are included. +- Batch number is extracted from the `p.batch` JSONB column via `->> 'lot_number'`. +- Results are ordered by earliest expiry first, then stock name and batch. + +*Last updated: 2026-06-17* From 489cc8dbd253afaa7f6539ab13de28779f22a2b6 Mon Sep 17 00:00:00 2001 From: sonzsara Date: Wed, 17 Jun 2026 14:47:08 +0530 Subject: [PATCH 2/2] Add filter for delivery order status in expiring items query --- Care/Inventory/expiring_items.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Care/Inventory/expiring_items.md b/Care/Inventory/expiring_items.md index e1e28f3..5d01c9e 100644 --- a/Care/Inventory/expiring_items.md +++ b/Care/Inventory/expiring_items.md @@ -35,6 +35,7 @@ JOIN emr_organization org ON d.supplier_id = org.id WHERE p.status = 'active' AND p.facility_id = 11 AND sd.status IN ('completed','in_progress') + AND d.status IN ('completed','pending') AND p.expiration_date >= date_trunc('month', CURRENT_DATE + INTERVAL '1 month') AND p.expiration_date < date_trunc('month', CURRENT_DATE + INTERVAL '2 months') --[[AND pk.name = {{stock_name}}]]