Skip to content

Commit ec8f6ec

Browse files
committed
Add expiring items query documentation for next month inventory
1 parent 4b2fde9 commit ec8f6ec

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

Care/Inventory/expiring_items.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
# Expiring Items - Next Month
3+
4+
> Inventory items expiring in the next calendar month, with batch, supplier, and supply details
5+
6+
## Purpose
7+
8+
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.
9+
10+
## Parameters
11+
12+
| Parameter | Type | Description | Example |
13+
|-----------|------|-------------|---------|
14+
| `stock_name` | TEXT | Filter by stock / product name (exact match on `emr_productknowledge.name`) | `'Paracetamol'` |
15+
| `supplier` | TEXT | Filter by supplier organization name (exact match on `emr_organization.name`) | `'MedSupplier Pvt Ltd'` |
16+
17+
---
18+
19+
## Query
20+
21+
```sql
22+
SELECT
23+
p.batch ->> 'lot_number' AS batch,
24+
pk.name AS stock_name,
25+
sd.supplied_item_quantity AS quantity,
26+
sd.created_date AS supply_date,
27+
p.expiration_date AS expiry_date,
28+
org.name AS supplier_name
29+
FROM emr_inventoryitem ii
30+
JOIN emr_product p ON ii.product_id = p.id
31+
JOIN emr_productknowledge pk ON p.product_knowledge_id = pk.id
32+
JOIN emr_supplydelivery sd ON sd.supplied_inventory_item_id = ii.id
33+
JOIN emr_deliveryorder d ON sd.order_id = d.id
34+
JOIN emr_organization org ON d.supplier_id = org.id
35+
WHERE p.status = 'active'
36+
AND p.facility_id = 11
37+
AND sd.status IN ('completed','in_progress')
38+
AND p.expiration_date >= date_trunc('month', CURRENT_DATE + INTERVAL '1 month')
39+
AND p.expiration_date < date_trunc('month', CURRENT_DATE + INTERVAL '2 months')
40+
--[[AND pk.name = {{stock_name}}]]
41+
--[[AND org.name = {{supplier}}]]
42+
ORDER BY p.expiration_date, pk.name, batch;
43+
```
44+
45+
## Notes
46+
47+
- 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.
48+
- `p.facility_id = 11` is hardcoded — change this to target a different facility.
49+
- Only products with `status = 'active'` and supply deliveries in `completed` / `in_progress` status are included.
50+
- Batch number is extracted from the `p.batch` JSONB column via `->> 'lot_number'`.
51+
- Results are ordered by earliest expiry first, then stock name and batch.
52+
53+
*Last updated: 2026-06-17*

0 commit comments

Comments
 (0)