Skip to content
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

OAM-293: Update report angola_stock_on_hand_per_facility #65

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 50 additions & 34 deletions reports/angola_stock_on_hand_per_facility.jrxml
Original file line number Diff line number Diff line change
Expand Up @@ -104,43 +104,59 @@
<![CDATA[WITH RECURSIVE findChildZones AS (
SELECT * FROM referencedata.geographic_zones WHERE name = $P{district}::varchar
UNION ALL
SELECT referencedata.geographic_zones.* FROM referencedata.geographic_zones JOIN findChildZones ON findChildZones.id = referencedata.geographic_zones.parentId)
SELECT
soh_data.product AS product,
soh_data.zone as zone,
soh_data.facility as facility,
SUM(soh_data.stockonhand) AS quantity
SELECT referencedata.geographic_zones.*
FROM referencedata.geographic_zones
JOIN findChildZones ON findChildZones.id = referencedata.geographic_zones.parentId
),
facilityWithParentZone AS (
SELECT
facilities.id AS facility_id,
COALESCE(parent_zones.name, zones.name) AS parentZone
FROM
referencedata.facilities AS facilities
JOIN referencedata.geographic_zones AS zones ON facilities.geographicZoneId = zones.id
LEFT JOIN referencedata.geographic_zones AS parent_zones ON zones.parentId = parent_zones.id
JOIN referencedata.geographic_levels AS levels ON zones.levelid = levels.id
WHERE
levels.levelnumber = 4
)
SELECT
soh_data.product AS product,
soh_data.zone AS zone,
soh_data.facility as facility,
SUM(soh_data.stockonhand) AS quantity
FROM (
SELECT
CONCAT_WS(': ',products.code,products.fullproductname) AS product,
programs."name" as program_name,
zones."name" as zone,
facilities."name" as facility,
soh.stockonhand AS stockonhand,
RANK () OVER (
PARTITION BY products.id
ORDER BY products.versionnumber DESC) AS product_version_rank,
RANK () OVER (
PARTITION BY products.id
ORDER BY soh.occurreddate desc) AS date_rank
FROM
stockmanagement.stock_cards AS stock_cards
JOIN stockmanagement.calculated_stocks_on_hand AS soh ON soh.stockcardid = stock_cards.id
JOIN referencedata.orderables AS products ON products.id = stock_cards.orderableid
JOIN referencedata.facilities AS facilities ON facilities.id = stock_cards.facilityid
JOIN referencedata.programs AS programs ON programs.id = stock_cards.programid
JOIN referencedata.geographic_zones AS zones ON zones.id = facilities.geographiczoneid
JOIN referencedata.geographic_levels AS levels ON levels.id = zones.levelid
where
($P{program} IS NULL OR $X{IN, programs.name, programList})
AND ($P{district}::varchar IS NULL OR zones.id IN (SELECT id FROM findChildZones))
AND ($P{productId} IS NULL OR products.id = $P{productId}::uuid)
AND ($P{facility} IS NULL OR facilities.name = $P{facility}::varchar)
AND ($P{date} IS NULL OR soh.occurreddate::varchar <= $P{date}::varchar)
AND levels.levelnumber <> 4
SELECT
CONCAT_WS(': ', products.code, products.fullproductname) AS product,
programs."name" as program_name,
facilityWithParentZone.parentZone as zone,
facilities."name" as facility,
soh.stockonhand AS stockonhand,
RANK() OVER (
PARTITION BY products.id
ORDER BY products.versionnumber DESC
) AS product_version_rank,
RANK() OVER (
PARTITION BY products.id
ORDER BY soh.occurreddate DESC
) AS date_rank
FROM
stockmanagement.stock_cards AS stock_cards
JOIN stockmanagement.calculated_stocks_on_hand AS soh ON soh.stockcardid = stock_cards.id
JOIN referencedata.orderables AS products ON products.id = stock_cards.orderableid
JOIN referencedata.facilities AS facilities ON facilities.id = stock_cards.facilityid
JOIN referencedata.programs AS programs ON programs.id = stock_cards.programid
JOIN facilityWithParentZone ON facilities.id = facilityWithParentZone.facility_id
WHERE
($P{program} IS NULL OR $X{IN, programs.name, programList})
AND ($P{district}::varchar IS NULL OR facilities.geographicZoneId IN (SELECT id FROM findChildZones))
AND ($P{productId} IS NULL OR products.id = $P{productId}::uuid)
AND ($P{facility} IS NULL OR facilities.name = $P{facility}::varchar)
AND ($P{date} IS NULL OR soh.occurreddate::varchar <= $P{date}::varchar)
) AS soh_data
WHERE soh_data.date_rank = 1 AND soh_data.product_version_rank = 1
GROUP BY soh_data.product, soh_data.zone, soh_data.facility]]>
GROUP BY soh_data.product, soh_data.zone, soh_data.facility
]]>
</queryString>
<field name="product" class="java.lang.String"/>
<field name="quantity" class="java.lang.Integer"/>
Expand Down

Large diffs are not rendered by default.

Loading