|
| 1 | + |
| 2 | +# Items Transferred to Consumption and Entered-in-Error Locations |
| 3 | + |
| 4 | +> Summary of stock transferred into a curated set of consumption / entered in error destination locations, with value |
| 5 | +
|
| 6 | +## Purpose |
| 7 | + |
| 8 | +Summarises supply deliveries at SSMM whose **destination** is one of a curated list of "consumption" or "entered in error" facility locations. Groups by day, origin, destination, and both delivery statuses, reporting total quantity and total purchase-price value moved. |
| 9 | + |
| 10 | +## Parameters |
| 11 | + |
| 12 | +| Parameter | Type | Description | Example | |
| 13 | +|-----------|------|-------------|---------| |
| 14 | +| `created_date` | DATE / range | Metabase date filter (typically bound to `sd.created_date`) | `'2026-07-01'` | |
| 15 | +| `location` | TEXT (multi) | Filter by one or more origin location names | `'Pharmacy', 'Main Store'` | |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +## Query |
| 20 | + |
| 21 | +```sql |
| 22 | +SELECT |
| 23 | + DATE(sd.created_date) AS created_date, |
| 24 | + fl_origin.name AS origin_location, |
| 25 | + fl_destination.name AS destination_location, |
| 26 | + sd.status, |
| 27 | + delivery_order.status, |
| 28 | + SUM(sd.supplied_item_quantity) AS total_quantity, |
| 29 | + SUM(p.purchase_price * sd.supplied_item_quantity) AS total_purchase_price |
| 30 | +FROM emr_supplydelivery sd |
| 31 | +JOIN emr_deliveryorder delivery_order |
| 32 | + ON sd.order_id = delivery_order.id |
| 33 | +JOIN emr_facilitylocation fl_destination |
| 34 | + ON delivery_order.destination_id = fl_destination.id |
| 35 | +JOIN emr_facilitylocation fl_origin |
| 36 | + ON delivery_order.origin_id = fl_origin.id |
| 37 | +JOIN emr_inventoryitem ii |
| 38 | + ON sd.supplied_inventory_item_id = ii.id |
| 39 | +JOIN emr_product p |
| 40 | + ON ii.product_id = p.id |
| 41 | +WHERE fl_destination.id IN ( |
| 42 | + 264, 32, 280, 266, 36, 638, 279, 278, 265, 481, 27, 298, |
| 43 | + 17, 273, 639, 275, 238, 270, 276, 274, 277, 297, 641 |
| 44 | +) |
| 45 | + AND fl_destination.deleted = FALSE |
| 46 | + AND fl_origin.deleted = FALSE |
| 47 | + AND sd.status IN ('completed', 'in_progress') |
| 48 | + AND delivery_order.status IN ('completed', 'pending') |
| 49 | + AND fl_destination.status = 'active' |
| 50 | + AND fl_origin.status = 'active' |
| 51 | + --[[AND {{created_date}}]] |
| 52 | + --[[AND fl_origin.name IN ({{location}})]] |
| 53 | +GROUP BY DATE(sd.created_date), fl_origin.name, fl_destination.name, sd.status, delivery_order.status |
| 54 | +ORDER BY DATE(sd.created_date) DESC, total_purchase_price DESC; |
| 55 | +``` |
| 56 | + |
| 57 | +## Notes |
| 58 | +- **Metabase filters:** |
| 59 | + - `[[AND {{created_date}}]]` is a field filter — bind it to `sd.created_date`. |
| 60 | + - `[[AND fl_origin.name IN ({{location}})]]` — multi-select filter on origin location name. |
| 61 | +- Results are ordered by most recent date first, then by highest purchase value within each day. |
| 62 | + |
| 63 | +*Last updated: 2026-07-24* |
0 commit comments