-
Notifications
You must be signed in to change notification settings - Fork 1
Add Total Stock Value Report documentation for SSMM #97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
|
|
||
| # Total Stock Value Report - SSMM | ||
|
|
||
| > Net stock quantity and total purchase value per stock item, as of a given date. | ||
|
|
||
| ## Purpose | ||
|
|
||
| Computes the current on-hand stock value for each stock item at SSMM as of a chosen date. It does this by: | ||
|
|
||
| 1. Summing everything **purchased** (received via external supply deliveries) up to that date. | ||
| 2. Subtracting everything **dispensed** to patients up to that date. | ||
| 3. Subtracting **mistake adjustments** — stock moved into a configured set of "mistake / correction" destination locations, treated as outflow. | ||
|
|
||
| The result is the **net quantity** and **net purchase value** of stock remaining per item. | ||
|
|
||
| ## Parameters | ||
|
|
||
| | Parameter | Type | Description | Example | | ||
| |-----------|------|-------------|---------| | ||
| | `selected_date` | DATE | As-of date for the report. All purchases, dispenses, and mistake entries up to and including this date are considered. | `'2026-05-31'` | | ||
|
|
||
| --- | ||
|
|
||
| ## Query | ||
|
|
||
| ```sql | ||
| WITH purchase_items AS ( | ||
| SELECT | ||
| epk.name AS stock_name, | ||
| SUM(esd.supplied_item_quantity) AS qty, | ||
| SUM(ep.purchase_price * esd.supplied_item_quantity) AS total_value | ||
|
sonzsara marked this conversation as resolved.
|
||
| FROM emr_supplydelivery esd | ||
| LEFT JOIN emr_deliveryorder edo ON esd.order_id = edo.id | ||
| LEFT JOIN emr_product ep ON esd.supplied_item_id = ep.id | ||
| LEFT JOIN emr_productknowledge epk ON ep.product_knowledge_id = epk.id | ||
| WHERE esd.status = 'completed' | ||
| AND edo.origin_id IS NULL | ||
| AND esd.deleted = FALSE | ||
| AND edo.deleted = FALSE | ||
| AND edo.status = 'completed' | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Delivery order status check here also |
||
| AND ep.purchase_price IS NOT NULL | ||
| --AND DATE(esd.created_date) <= {{selected_date}} | ||
| GROUP BY epk.name | ||
|
sonzsara marked this conversation as resolved.
|
||
| ), | ||
|
|
||
| dispense_items AS ( | ||
| SELECT | ||
| epk.name AS stock_name, | ||
| SUM(emd.quantity) AS qty, | ||
| SUM(ep.purchase_price * emd.quantity) AS total_value | ||
| FROM emr_medicationdispense emd | ||
| LEFT JOIN emr_dispenseorder edo ON edo.id = emd.order_id | ||
| LEFT JOIN emr_inventoryitem eii ON eii.id = emd.item_id | ||
| LEFT JOIN emr_product ep ON eii.product_id = ep.id | ||
| LEFT JOIN emr_productknowledge epk ON ep.product_knowledge_id = epk.id | ||
| WHERE edo.status IN ('completed', 'in_progress', 'draft') | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does Dispense order status have any effect on stiock?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it does . When that condition is removed, additional dispense rows get included, which lowers the net stock value
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is understood but how does our backend calculate stock now? Is order status checked in inventory calculation? |
||
| AND emd.status IN ('completed', 'in_progress', 'preparation') | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Double check if the status check is correct
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yesss
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about |
||
| AND emd.deleted = FALSE | ||
| AND edo.deleted = FALSE | ||
| AND ep.purchase_price IS NOT NULL | ||
| --AND DATE(emd.created_date) <= {{selected_date}} | ||
| GROUP BY epk.name | ||
| ), | ||
|
|
||
| mistake_items AS ( | ||
| SELECT | ||
| epk.name AS stock_name, | ||
| SUM(esd.supplied_item_quantity) AS qty, | ||
| SUM(ep.purchase_price * esd.supplied_item_quantity) AS total_value | ||
|
sonzsara marked this conversation as resolved.
|
||
| FROM emr_supplydelivery esd | ||
| LEFT JOIN emr_deliveryorder edo ON esd.order_id = edo.id | ||
| LEFT JOIN emr_inventoryitem eii ON esd.supplied_inventory_item_id = eii.id | ||
| LEFT JOIN emr_product ep ON eii.product_id = ep.id | ||
| LEFT JOIN emr_productknowledge epk ON ep.product_knowledge_id = epk.id | ||
| WHERE esd.status IN ('completed', 'in_progress') | ||
| AND edo.destination_id IN (264,270,280,274,273,275,276,266,279,36,265,278,297,238,298,27,481,17,32,277) | ||
| AND esd.deleted = FALSE | ||
| AND edo.deleted = FALSE | ||
| AND edo.status IN ('completed', 'in_progress') | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
| AND ep.purchase_price IS NOT NULL | ||
| --AND DATE(esd.created_date) <= {{selected_date}} | ||
| GROUP BY epk.name | ||
| ) | ||
|
|
||
| SELECT | ||
| COALESCE(p.stock_name, d.stock_name, m.stock_name) AS stock_name, | ||
| COALESCE(p.qty, 0) - COALESCE(d.qty, 0) - COALESCE(m.qty, 0) AS net_qty, | ||
| COALESCE(p.total_value, 0) - COALESCE(d.total_value, 0) - COALESCE(m.total_value, 0) AS net_value | ||
| FROM purchase_items p | ||
| FULL OUTER JOIN dispense_items d ON p.stock_name = d.stock_name | ||
| FULL OUTER JOIN mistake_items m ON COALESCE(p.stock_name, d.stock_name) = m.stock_name | ||
| ORDER BY stock_name; | ||
| ``` | ||
|
|
||
|
|
||
| ## Notes | ||
|
|
||
| - **`purchase_items` CTE** — external purchases only. Uses `edo.origin_id IS NULL` to filter to supply deliveries that originate from outside the facility | ||
| - **`dispense_items` CTE** — outflow via medication dispenses to patients. Includes dispense orders in `completed / in_progress / draft` and dispenses in `completed / in_progress / preparation` to match what reduces stock in practice. | ||
| - **`mistake_items` CTE** — supply deliveries whose destination is one of the configured "mistake / correction" locations. These represent stock moved out for adjustments and is treated as outflow. | ||
| - **Hardcoded values:** | ||
| - `edo.destination_id IN (264,270,280,274,273,275,276,266,279,36,265,278,297,238,298,27,481,17,32,277)` — the list of "mistake / correction" destination locations. Update this list whenever new mistake/correction locations are added or old ones are retired. | ||
|
|
||
| *Last updated: 2026-05-22* | ||
|
|
||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.