diff --git a/src/equipment_sparepart/service.py b/src/equipment_sparepart/service.py index 14d1783..5d0b20a 100644 --- a/src/equipment_sparepart/service.py +++ b/src/equipment_sparepart/service.py @@ -164,12 +164,19 @@ async def get_all( if location_tag: base_query += " AND asset_location = :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 += """ ), filtered_materials AS ( 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) ) SELECT diff --git a/src/maximo/service.py b/src/maximo/service.py index 33ca754..ea3b25a 100644 --- a/src/maximo/service.py +++ b/src/maximo/service.py @@ -154,48 +154,91 @@ ORDER BY avg_cost DESC; 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(""" - 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 ( + WITH part_costs AS ( + SELECT + wm.wonum, + SUM(wm.itemqty * COALESCE(inv.avgcost, po.unit_cost, 0)) AS parts_total_cost + FROM public.maximo_workorder_materials wm + JOIN public.maximo_inventory AS inv on inv.itemnum = wm.itemnum + 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 - 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; - """) - + asset_location, + AVG(total_wo_cost) AS avg_cost + FROM wo_costs + GROUP BY asset_location + ORDER BY COUNT(wonum) DESC; + """) + result = await collector_db.execute(query) data = [] diff --git a/src/sparepart/service.py b/src/sparepart/service.py index fec50af..5cafe96 100644 --- a/src/sparepart/service.py +++ b/src/sparepart/service.py @@ -212,6 +212,20 @@ async def get_spareparts_paginated(*, db_session, collector_db_session): # calculate limit/offset # limit = 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 @@ -230,12 +244,13 @@ async def get_spareparts_paginated(*, db_session, collector_db_session): wm.wonum, wm.itemnum, wm.itemqty, - wm.inv_itemnum, - wm.inv_location, - wm.inv_curbaltotal, - wm.inv_avgcost, + inv.itemnum AS inv_itemnum, + inv.location AS inv_location, + inv.curbaltotal AS inv_curbaltotal, + inv.avgcost AS inv_avgcost, 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 ), location_sparepart_stats AS (