Skip to content

Add SQL documentation for various stock value calculations including external purchase, consumed, dispensed, return, and total stock values - #167

Open
sonzsara wants to merge 1 commit into
mainfrom
ENG-1015
Open

sonzsara wants to merge 1 commit into
mainfrom
ENG-1015

Conversation

@sonzsara

Copy link
Copy Markdown
Contributor

No description provided.

Copilot AI balanced review requested due to automatic review settings October 26, 2026 06:28
…external purchase, consumed, dispensed, return, and total stock values

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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)
Copilot AI balanced review requested due to automatic review settings October 26, 2026 06:32

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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; undocumented edo.id != 5724).
  • LEFT JOIN + deleted = FALSE in WHERE acts as an inner join in the dispensed query.
  • supplier_id = '20697' string vs numeric comparison.
  • Minor: no entered_in_error exclusion on supply deliveries (only completed/in_progress are 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 (

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 (

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 (

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two correctness points:

  1. total_purchases here omits the edo.destination_id NOT IN (36) filter that total_purchase_value_ssmm / total_stock_value_ssmm apply, so the "purchase" base differs between cards. Intended? If not, the dashboard cards won't reconcile. (Note NOT IN (36) also drops rows with NULL destination_id; IS DISTINCT FROM 36 is safer if that matters.)
  2. edo.supplier_id = '20697' compares against a string literal; if supplier_id is 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" for 2026-04-01; there are 4 in the CTEs plus 1 in the final CASE (5).

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Valid dispenses without orders are excluded from the stock deduction, overstating stock value.

4 open issues

Review effort: Balanced

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants