-- WorkOrderStep Archive Query
-- Source: QADTA.F3112 + QADTA.F00192 (current) + ARCDTAQA.F3112 (archived)
-- Used by: GetWorkOrderStepsArchive() - on-demand historical retrieval
-- Note: Not scheduled, used for historical lookups
-- Note: Function code lookup uses QADTA.F00192 for both current and archived data

SELECT wos.WLDOCO AS WorkOrderNumber,
       TRIM(wos.WLMMCU) AS BranchCode,
       TRIM(wos.WLMCU) AS WorkCenterCode,
       wos.WLOPSQ AS StepNumber,
       TRIM(wos.WLDSC1) AS StepDescription,
       TRIM(mes.CFDS80) AS FunctionOperationDescription,
       wos.WLOPSC AS StepTypeCode,
       CASE wos.WLSTRT WHEN 0 THEN NULL ELSE wos.WLSTRT END AS StartDT,
       CASE wos.WLSTRX WHEN 0 THEN NULL ELSE wos.WLSTRX END AS EndDT,
       TRIM(wos.WLURRF) AS FunctionCode,
       wos.WLUPMJ AS DateUpdated,
       wos.WLTDAY AS TimeUpdated
  FROM QADTA.F3112 wos LEFT OUTER JOIN
       QADTA.F00192 mes ON (wos.WLURRF = mes.CFKY)

UNION ALL

SELECT wos.WLDOCO AS WorkOrderNumber,
       TRIM(wos.WLMMCU) AS BranchCode,
       TRIM(wos.WLMCU) AS WorkCenterCode,
       wos.WLOPSQ AS StepNumber,
       TRIM(wos.WLDSC1) AS StepDescription,
       TRIM(mes.CFDS80) AS FunctionOperationDescription,
       wos.WLOPSC AS StepTypeCode,
       CASE wos.WLSTRT WHEN 0 THEN NULL ELSE wos.WLSTRT END AS StartDT,
       CASE wos.WLSTRX WHEN 0 THEN NULL ELSE wos.WLSTRX END AS EndDT,
       TRIM(wos.WLURRF) AS FunctionCode,
       wos.WLUPMJ AS DateUpdated,
       wos.WLTDAY AS TimeUpdated
  FROM ARCDTAQA.F3112 wos LEFT OUTER JOIN
       QADTA.F00192 mes ON (wos.WLURRF = mes.CFKY)
