Skip to content

Commit

Permalink
Merge pull request #65 from OpenLMIS-Angola/OAM-293
Browse files Browse the repository at this point in the history
OAM-293: Update report angola_stock_on_hand_per_facility
  • Loading branch information
olewandowski1 authored Aug 7, 2024
2 parents b6f57ae + f00b01d commit c64bb9e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 34 deletions.
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.

0 comments on commit c64bb9e

Please sign in to comment.