Repository navigation
Conversation
…external purchase, consumed, dispensed, return, and total stock values
There was a problem hiding this comment.
🟡 Changes recommended
Dispense filtering and inconsistent treatment of location 36 receipts can produce incorrect financial totals.
2 open issues
What changed in this PR
Adds documented SQL reports for SSMM inventory valuation and cumulative facility counts to the analytics query catalog.
Changes:
- Adds six purchase-cost reports covering purchases, consumption, dispensing, patient returns, and stock on hand.
- Documents date parameters, financial-year boundaries, formulas, and deployment-specific filters.
- Adds facility counts at fixed financial-year and quarter-end dates.
| File | Description |
|---|---|
| Care/Operations/count_of_facilities_for_the_specified_dates.md | Adds cumulative facility counts at five cutoff dates. |
| Care/Inventory/total_stock_value_ssmm.md | Calculates cumulative stock value from inflows and deductions. |
| Care/Inventory/total_purchase_value_ssmm.md | Calculates supplier purchases less error-location movements. |
| Care/Inventory/total_medication_return_value_ssmm.md | Values patient medication returns at purchase cost. |
| Care/Inventory/total_dispensed_value_ssmm.md | Calculates the purchase cost of dispensed medications. |
| Care/Inventory/total_consumed_value_ssmm.md | Values net movements into consumption locations. |
| Care/Inventory/external_purchase_value_ssmm.md | Calculates external purchases with supplier and order exclusions. |
Review effort: Balanced
| LEFT JOIN emr_product ep ON esd.supplied_item_id = ep.id | ||
| WHERE esd.status = 'completed' | ||
| AND edo.origin_id IS NULL | ||
| AND edo.destination_id NOT IN (36) |
There was a problem hiding this comment.
Analytics SQL Review — Stock dashboard cards (purchase, external purchase, consumed, dispensed, returns, total stock value)
Ticket (ENG-1015): new stock dashboard with an opening stock that must be included in the existing cards (external purchase, total purchase, total stock value, ...).
Verdict: not ready to publish. The six queries parse cleanly and are documented well, but none includes an opening stock, so the core requirement is unmet and total stock value will be understated. Other points:
- Inconsistent purchase base across cards (
destination_id NOT IN (36)only in some; undocumentededo.id != 5724). LEFT JOIN+deleted = FALSEin WHERE acts as an inner join in the dispensed query.supplier_id = '20697'string vs numeric comparison.- Minor: no
entered_in_errorexclusion on supply deliveries (onlycompleted/in_progressare counted, so likely fine), and the "four places" date count in Notes is off.
The ticket has no comments or further detail, so I assumed opening stock is a value/balance at 2026-04-01; please confirm the source.
Generated by Analytics SQL Reviewer for #167 · auto · 20.4 AIC · ⊞ 5.1K
| ## Query | ||
|
|
||
| ```sql | ||
| WITH total_purchases AS ( |
There was a problem hiding this comment.
Requirement gap (ENG-1015): the ticket asks for an opening stock component that must feed total stock value, total purchase, external purchase, etc. There is no opening stock anywhere in this query (or in the other five). Here the formula is purely purchases − dispenses − moved, so an opening balance is missing from the result. Where should opening stock come from (a table, a hardcoded value as of 2026-04-01, or inventory balance at that date)? If it's a hardcoded figure, add it as a documented CTE/param and explain it in Notes.
Also: this query is all-time (no lower bound) while the others are FY-bounded from 2026-04-01 — once an opening stock is added, make sure the date semantics don't double count pre-FY purchases already in the opening balance.
| ## Query | ||
|
|
||
| ```sql | ||
| WITH total_purchases AS ( |
There was a problem hiding this comment.
Notes say the financial-year start is hardcoded "in three places"; it appears in total_purchases, total_error and the final CASE — fine — but also: the opening stock the ticket asks to be included in this card is absent. Also total_error is documented as covering all deliveries into 264/636 (including internal transfers), while external_purchase_value_ssmm additionally excludes edo.id != 5724 — that hardcoded order id is undocumented on its rationale (why 5724?) and inconsistent between the two cards.
| ## Query | ||
|
|
||
| ```sql | ||
| WITH total_purchases AS ( |
There was a problem hiding this comment.
Two correctness points:
total_purchaseshere omits theedo.destination_id NOT IN (36)filter thattotal_purchase_value_ssmm/total_stock_value_ssmmapply, so the "purchase" base differs between cards. Intended? If not, the dashboard cards won't reconcile. (NoteNOT IN (36)also drops rows with NULLdestination_id;IS DISTINCT FROM 36is safer if that matters.)edo.supplier_id = '20697'compares against a string literal; ifsupplier_idis a bigint this relies on implicit casting — use= 20697.
Also, the ticket's opening-stock requirement isn't reflected in this card.
Minor: Notes say "four places" for2026-04-01; there are 4 in the CTEs plus 1 in the finalCASE(5).
| LEFT JOIN emr_product ep ON eii.product_id = ep.id | ||
| WHERE esd.status IN ('completed', 'in_progress') | ||
| AND edo.destination_id IN (264, 636) | ||
| AND edo.id != 5724 |


No description provided.