Cizz22 3 months ago
parent a222a39230
commit 1a1c7a2c0b

@ -165,11 +165,18 @@ async def get_all(
base_query += " AND asset_location = :location_tag" base_query += " AND asset_location = :location_tag"
params["location_tag"] = location_tag params["location_tag"] = location_tag
# filtered_materials AS (
# SELECT wonum, itemnum, itemqty, inv_curbaltotal, inv_avgcost
# FROM public.wo_maxim_material
# WHERE wonum IN (SELECT wonum FROM filtered_wo)
# )
base_query += """ base_query += """
), ),
filtered_materials AS ( filtered_materials AS (
SELECT wonum, itemnum, itemqty, inv_curbaltotal, inv_avgcost SELECT wonum, itemnum, itemqty, inv_curbaltotal, inv_avgcost
FROM public.wo_maxim_material FROM public.maximo_workorder_materials mat
JOIN public.wo_maximo_inventory AS inv on inv.itemnum = mat.itemnum
WHERE wonum IN (SELECT wonum FROM filtered_wo) WHERE wonum IN (SELECT wonum FROM filtered_wo)
) )
SELECT SELECT

@ -154,12 +154,55 @@ ORDER BY avg_cost DESC;
async def get_oh_cost_summary(collector_db: CollectorDbSession, last_oh_date:datetime, upcoming_oh_date:datetime): async def get_oh_cost_summary(collector_db: CollectorDbSession, last_oh_date:datetime, upcoming_oh_date:datetime):
# query = text("""
# WITH part_costs AS (
# SELECT
# wm.wonum,
# SUM(wm.itemqty * COALESCE(wm.inv_avgcost, po.unit_cost, 0)) AS parts_total_cost
# FROM public.wo_maxim_material wm
# LEFT JOIN (
# SELECT item_num, AVG(unit_cost) AS unit_cost
# FROM public.maximo_sparepart_pr_po_line
# GROUP BY item_num
# ) po ON wm.itemnum = po.item_num
# WHERE wm.itemnum IS NOT NULL
# GROUP BY wm.wonum
# ),
# wo_costs AS (
# SELECT
# w.wonum,
# w.asset_location,
# -- Use mat_cost_max if parts_total_cost = 0
# CASE
# WHEN COALESCE(pc.parts_total_cost, 0) = 0 THEN COALESCE(w.mat_cost_max , 0)
# ELSE COALESCE(pc.parts_total_cost, 0)
# END AS total_wo_cost
# FROM wo_staging_maximo_2 w
# LEFT JOIN part_costs pc
# ON w.wonum = pc.wonum
# WHERE
# w.worktype = 'OH'
# AND w.reportdate IS NOT NULL
# AND w.actstart IS NOT NULL
# AND w.actfinish IS NOT NULL
# AND w.asset_unit IN ('3', '00')
# AND w.wonum NOT LIKE 'T%'
# )
# SELECT
# asset_location,
# AVG(total_wo_cost) AS avg_cost
# FROM wo_costs
# GROUP BY asset_location
# ORDER BY COUNT(wonum) DESC;
# """)
query = text(""" query = text("""
WITH part_costs AS ( WITH part_costs AS (
SELECT SELECT
wm.wonum, wm.wonum,
SUM(wm.itemqty * COALESCE(wm.inv_avgcost, po.unit_cost, 0)) AS parts_total_cost SUM(wm.itemqty * COALESCE(inv.avgcost, po.unit_cost, 0)) AS parts_total_cost
FROM public.wo_maxim_material wm FROM public.maximo_workorder_materials wm
JOIN public.maximo_inventory AS inv on inv.itemnum = wm.itemnum
LEFT JOIN ( LEFT JOIN (
SELECT item_num, AVG(unit_cost) AS unit_cost SELECT item_num, AVG(unit_cost) AS unit_cost
FROM public.maximo_sparepart_pr_po_line FROM public.maximo_sparepart_pr_po_line
@ -167,8 +210,8 @@ async def get_oh_cost_summary(collector_db: CollectorDbSession, last_oh_date:dat
) po ON wm.itemnum = po.item_num ) po ON wm.itemnum = po.item_num
WHERE wm.itemnum IS NOT NULL WHERE wm.itemnum IS NOT NULL
GROUP BY wm.wonum GROUP BY wm.wonum
), ),
wo_costs AS ( wo_costs AS (
SELECT SELECT
w.wonum, w.wonum,
w.asset_location, w.asset_location,
@ -187,13 +230,13 @@ wo_costs AS (
AND w.actfinish IS NOT NULL AND w.actfinish IS NOT NULL
AND w.asset_unit IN ('3', '00') AND w.asset_unit IN ('3', '00')
AND w.wonum NOT LIKE 'T%' AND w.wonum NOT LIKE 'T%'
) )
SELECT SELECT
asset_location, asset_location,
AVG(total_wo_cost) AS avg_cost AVG(total_wo_cost) AS avg_cost
FROM wo_costs FROM wo_costs
GROUP BY asset_location GROUP BY asset_location
ORDER BY COUNT(wonum) DESC; ORDER BY COUNT(wonum) DESC;
""") """)
result = await collector_db.execute(query) result = await collector_db.execute(query)

@ -213,6 +213,20 @@ async def get_spareparts_paginated(*, db_session, collector_db_session):
# limit = items_per_page # limit = items_per_page
# offset = (page - 1) * items_per_page # offset = (page - 1) * items_per_page
# wo_materials AS (
# SELECT
# wm.wonum,
# wm.itemnum,
# wm.itemqty,
# wm.inv_itemnum,
# wm.inv_location,
# wm.inv_curbaltotal,
# wm.inv_avgcost,
# sw.asset_location as location_tag
# FROM public.wo_maxim_material wm
# JOIN oh_workorders sw ON wm.wonum = sw.wonum
# ),
# ----------------------------- # -----------------------------
# Query #1: Fetch paginated rows # Query #1: Fetch paginated rows
# ----------------------------- # -----------------------------
@ -230,12 +244,13 @@ async def get_spareparts_paginated(*, db_session, collector_db_session):
wm.wonum, wm.wonum,
wm.itemnum, wm.itemnum,
wm.itemqty, wm.itemqty,
wm.inv_itemnum, inv.itemnum AS inv_itemnum,
wm.inv_location, inv.location AS inv_location,
wm.inv_curbaltotal, inv.curbaltotal AS inv_curbaltotal,
wm.inv_avgcost, inv.avgcost AS inv_avgcost,
sw.asset_location as location_tag sw.asset_location as location_tag
FROM public.wo_maxim_material wm FROM public.maximo_workorder_materials wm
JOIN maximo_inventory inv ON inv.itemnum = wm.itemnum
JOIN oh_workorders sw ON wm.wonum = sw.wonum JOIN oh_workorders sw ON wm.wonum = sw.wonum
), ),
location_sparepart_stats AS ( location_sparepart_stats AS (

Loading…
Cancel
Save