Skip to content

Add units, PO details, delivery date, and supplier columns to stock notification queries#304

Merged
jmbrunskill merged 6 commits intomainfrom
copilot/include-additional-columns-sql-queries
Apr 13, 2026
Merged

Add units, PO details, delivery date, and supplier columns to stock notification queries#304
jmbrunskill merged 6 commits intomainfrom
copilot/include-additional-columns-sql-queries

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 31, 2026

👩🏻‍💻 What does this PR do?

Extends the critical_stock and stock_out_today reference SQL queries and templates with additional columns for richer stock reporting.

SQL changes (both queries):

  • Join name table via po.name_id in the pos CTE to resolve supplier names
  • Join unit table via i.unit_ID to resolve item units (units are stored in a separate unit table, not on the item table directly)
  • Add u.units, po.serial_number, po_outstanding, delivery_date_expected, and supplier name to SELECT
  • Add currentMOS aggregator join to stock_out_today (was previously missing)
  • Update GROUP BY to include u.units
  • Filter pos CTE join on ranking = 1 so all PO-related columns (serial_number, supplier, delivery_date, po_outstanding) consistently resolve from the single most recent confirmed PO per store/item

Template changes (both notifications):

  • Columns now follow requested order: Store Name / Item Code / Item Name / Units / AMC (12 months) / SOH / MOS Remaining / PO Placed / PO Number / Stock on Order / Delivery Date / Supplier
  • AMC column header hardcoded to AMC (12 months) — the lookback period is typically 12 months across deployments and is not queryable from the dashboard database
  • Container width increased to 800px to accommodate additional columns

🧪 How has/should this change been tested?

Documentation-only change. Verify SQL syntax and template variable references match by visual review. Queries should be validated against an mSupply database with aggregator data populated.

💌 Any notes for the reviewer?

  • po.serial_number and po.name_id field names are based on the mSupply purchase_order schema — worth confirming against the target deployment.
  • Units are resolved via LEFT JOIN unit u ON i.unit_ID = u.id since the item table stores a foreign key (unit_ID) rather than the unit name directly.
  • The AMC lookback period (12 months) is hardcoded in the column header since it is not available in the dashboard database — it lives in the mSupply scheduler configuration. If a deployment uses a different period, the template header can be manually adjusted.

📃 Documentation

  • Updated docs/reference_notifications.md — added columns, templates, and parameters for both critical_stock and stock_out_today notifications

Copilot AI linked an issue Mar 31, 2026 that may be closed by this pull request
1 task
…and templates

Updates both SQL queries and HTML templates in reference_notifications.md
to include: Units, AMC, SOH, MOS, PO Placed, PO Number, Stock on Order,
Delivery Date, and Supplier columns in the requested order.

Adds consumption_months parameter for dynamic AMC/MOS header labeling.

Agent-Logs-Url: https://github.com/msupply-foundation/notify/sessions/09a45cc7-cc88-4f58-a54e-136c22adf33b

Co-authored-by: jmbrunskill <8287373+jmbrunskill@users.noreply.github.com>
Copilot AI changed the title [WIP] Add additional columns to SQL for stock queries Add units, PO details, delivery date, and supplier columns to stock notification queries Mar 31, 2026
Copilot AI requested a review from jmbrunskill March 31, 2026 19:30
@jmbrunskill
Copy link
Copy Markdown
Collaborator

@copilot can you add the The pos CTE's RANK() column exists but is not filtered on (ranking = 1) in the main query — this is pre-existing behavior, unchanged here. The max() aggregation handles multiple PO matches. That would ensure the max resolves to the same PO right?

…ame purchase order

Agent-Logs-Url: https://github.com/msupply-foundation/notify/sessions/6326016e-3b1d-4161-b3cc-096d301cd074

Co-authored-by: jmbrunskill <8287373+jmbrunskill@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 31, 2026

@copilot can you add the The pos CTE's RANK() column exists but is not filtered on (ranking = 1) in the main query — this is pre-existing behavior, unchanged here. The max() aggregation handles mult...

Yes, exactly right — without the filter, max() on different columns could pick values from different POs. Added AND pos.ranking = 1 to the pos JOIN condition in both queries (9fc7f92). This ensures all PO-related columns (serial_number, supplier, delivery_date, po_outstanding) consistently come from the single most recent confirmed PO per store/item.

@louisaw123
Copy link
Copy Markdown

louisaw123 commented Mar 31, 2026

@copilot getting this error see screenshot- note units are in a different table called unit

Screenshot 2026-04-01 at 12 29 42 PM

@jmbrunskill
Copy link
Copy Markdown
Collaborator

@copilot getting this error see screenshot- note units are in a different table called unit

image

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 1, 2026

@copilot getting this error see screenshot- note units are in a different table called unit

<img width="1518" height="670" alt="image" src="https://github.com/user-attachments/assets/4f664447-69f5-...

Fixed in a729d57. The item table doesn't have a units column directly — it references a separate unit table via unit_ID. Both queries now include LEFT JOIN unit u ON i.unit_ID = u.id and select u.units instead of i.units.

@louisaw123
Copy link
Copy Markdown

louisaw123 commented Apr 1, 2026

Screenshot 2026-04-01 at 2 06 47 PM Screenshot 2026-04-01 at 2 06 58 PM

@copilot can we rearrange the columns so that it appears in this order:

Store name / Item code / Item Name / Units / AMC / SOH / MOS Remaining / PO Placed / PO Number / Stock on Order / Delivery Date / Supplier

can we also include in the headers for "AMC" and the "MOS Remaining" the amount of months this is looking back

@jmbrunskill
Copy link
Copy Markdown
Collaborator

@copilot can we rearrange the columns so that it appears in this order:

Store name / Item code / Item Name / Units / AMC / SOH / MOS Remaining / PO Placed / PO Number / Stock on Order / Delivery Date / Supplier

@louisaw123 I've had a quick look at the mSupply Docs and a copy of the dashboard database. I can't see where/how the AMC is defined from the docs it sounds like it might be all months with consumptions?. Perhaps have a discussion with @JonnaQuismundo or @ferreiratoni might be familar with this?
https://docs.msupply.org.nz/tables_fields:main_tables:aggregator?s[]=aggregator

One option it to make this configurable with another parameter. We could take the months consumption numbers for the last x months and average them in the query, or I think in a number of places we've re-implemented AMC calcs in the query itself. I think if we want to go that way best to provide a query from dashboard to base this one on.

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 6, 2026

@copilot can we rearrange the columns so that it appears in this order:

Store name / Item code / Item Name / Units / AMC / SOH / MOS Remaining / PO Placed / PO Number / Stock on Order / Delivery Da...

Rearranged both templates to the requested column order: Store Name / Item Code / Item Name / Units / AMC / SOH / MOS Remaining / PO Placed / PO Number / Stock on Order / Delivery Date / Supplier (e625506).

@JonnaQuismundo
Copy link
Copy Markdown

the AMC is defined from the docs it sounds like it might be all months with consumptions?.

@jmbrunskill for dashboard, it's the average of all months (depends on look back period) regardless of whether there is any consumption.

@jmbrunskill
Copy link
Copy Markdown
Collaborator

the AMC is defined from the docs it sounds like it might be all months with consumptions?.

@jmbrunskill for dashboard, it's the average of all months (depends on look back period) regardless of whether there is any consumption.

@JonnaQuismundo Is there anywhere lookback period is available to query in the dashboard database. @louisaw123 wants to display it.

@JonnaQuismundo
Copy link
Copy Markdown

the AMC is defined from the docs it sounds like it might be all months with consumptions?.

@jmbrunskill for dashboard, it's the average of all months (depends on look back period) regardless of whether there is any consumption.

@JonnaQuismundo Is there anywhere lookback period is available to query in the dashboard database. @louisaw123 wants to display it.

@jmbrunskill hmm, I dont think the lookback period is in dashboard. The config is in the scheduler.

Screenshot 2026-04-07 at 11 47 22 AM

Also want to correct my previous statement, only months with consumption are considered in AMC.

@louisaw123
Copy link
Copy Markdown

Would there be an alternative way to grab it and present it then? It just needs to be somehow presented so that users are aware what the basis of the lookback is when it is presenting the information. Or otherwise even if it is a default as 12 then I am happy to have it included in the body of the message at the top before the table

@jmbrunskill
Copy link
Copy Markdown
Collaborator

Would there be an alternative way to grab it and present it then? It just needs to be somehow presented so that users are aware what the basis of the lookback is when it is presenting the information. Or otherwise even if it is a default as 12 then I am happy to have it included in the body of the message at the top before the table

We could make it configurable so we can for example hardcode it to 3months? But it sounds like the information about the default one in mSupply isn't available in the dashboard database.

@jmbrunskill jmbrunskill marked this pull request as ready for review April 7, 2026 04:07
@louisaw123
Copy link
Copy Markdown

@JonnaQuismundo is the lookback period able to be on dashboards? do you know if its the same in all countries or is it something that countries have changed themselves?

@JonnaQuismundo
Copy link
Copy Markdown

@JonnaQuismundo is the lookback period able to be on dashboards?

@louisaw123 not without adding changes because we are not exporting the pref table. We need to put it somewhere to be exported. Another alternative is to count the monthly consumption. If dashboard only have 12 months saved, then its 12 lookback period.

do you know if its the same in all countries or is it something that countries have changed themselves?

It's usually 12. I don't know any country using different period. The configuration can be changed, but only support as normal user will not have access to the scheduler window.

@louisaw123
Copy link
Copy Markdown

thanks team! in that case im happy with just having the text say (12 months) without pulling it from anywhere or doing anything extra then. can consider review in future if this changes (i.e., countries are able to adjust this easily without support), but otherwise we can just make an internal note of it for ourselves that this is where its coming from and to adjust if the country is known to have a different one.

@jmbrunskill
Copy link
Copy Markdown
Collaborator

@copilot please add a note to the AMC column to say (12 months)

…ths parameter

Agent-Logs-Url: https://github.com/msupply-foundation/notify/sessions/4e8c3888-9767-49a9-adbd-341f97695155

Co-authored-by: jmbrunskill <8287373+jmbrunskill@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 9, 2026

@copilot please add a note to the AMC column to say (12 months)

Done — AMC column header is now hardcoded to AMC (12 months) in both templates. Also removed the consumption_months parameter since it's no longer referenced (c46a428).

Copy link
Copy Markdown
Collaborator

@jmbrunskill jmbrunskill left a comment

Choose a reason for hiding this comment

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

It's working for Louisa, so I think good to merge:)

@jmbrunskill jmbrunskill merged commit a3fb8a6 into main Apr 13, 2026
@jmbrunskill jmbrunskill deleted the copilot/include-additional-columns-sql-queries branch April 13, 2026 01:15
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.

Include additional columns in the SQL for queries

4 participants