From fd97475ca54e2b9b654000dcfb5179bc877637ad Mon Sep 17 00:00:00 2001 From: Cizz22 Date: Fri, 17 Oct 2025 08:20:21 +0700 Subject: [PATCH] fix --- src/maximo/service.py | 84 ++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 45 deletions(-) diff --git a/src/maximo/service.py b/src/maximo/service.py index b046686..33ca754 100644 --- a/src/maximo/service.py +++ b/src/maximo/service.py @@ -155,51 +155,45 @@ ORDER BY avg_cost DESC; async def get_oh_cost_summary(collector_db: CollectorDbSession, last_oh_date:datetime, upcoming_oh_date:datetime): query = text(""" - 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, - (COALESCE(pc.parts_total_cost, 0)) 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.asset_system IN ( - 'HPB', 'AH', 'APC', 'SCR', 'CL', 'DM', 'CRH', 'ASH', 'BAD', 'DS', 'WTP', - 'MT', 'SUP', 'DCS', 'FF', 'EG', 'AI', 'SPS', 'EVM', 'SCW', 'KLH', 'CH', - 'TUR', 'LOT', 'HRH', 'ESP', 'CAE', 'GMC', 'BFT', 'LSH', 'CHB', 'BSS', - 'LOS', 'LPB', 'SAC', 'CP', 'EHS', 'RO', 'GG', 'MS', 'CW', 'SO', 'ATT', - 'AFG', 'EHB', 'RP', 'FO', 'PC', 'APE', 'AF', 'DMW', 'BRS', 'GEN', 'ABS', - 'CHA', 'TR', 'H2', 'BDW', 'LOM', 'ACR', 'AL', 'FW', 'COND', 'CCCW', 'IA', - 'GSS', 'BOL', 'SSB', 'CO', 'OA', 'CTH-UPD', 'AS', 'DP' - ) - 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.reportdate >= '2019-01-01' - 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; + 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; """) result = await collector_db.execute(query)