SET PATH *LIBL ; 

CREATE OR REPLACE FUNCTION RESTAPI.SFP_GET_LABOR_HOURS ( 
	IN_PARM CLOB(2147483647) ) 
	RETURNS CLOB(2147483647)   
	LANGUAGE SQL 
	SPECIFIC RESTAPI.SFP_GET_LABOR_HOURS 
	NOT DETERMINISTIC 
	MODIFIES SQL DATA 
	CALLED ON NULL INPUT 
	SET OPTION  ALWBLK = *ALLREAD , 
	ALWCPYDTA = *OPTIMIZE , 
	COMMIT = *NONE , 
	DECRESULT = (31, 31, 00) , 
	DYNDFTCOL = *NO , 
	DYNUSRPRF = *USER , 
	SRTSEQ = *HEX   
	BEGIN 
DECLARE ENV VARCHAR ( 20 ) DEFAULT '' ; 
DECLARE V_PERIOD CHAR ( 1 ) DEFAULT 'D' ; 
DECLARE V_PLANT VARCHAR ( 15 ) DEFAULT '' ; 
DECLARE V_DEPT VARCHAR ( 15 ) DEFAULT '' ; 
DECLARE V_WC VARCHAR ( 5 ) DEFAULT '' ; 
DECLARE V_SITE VARCHAR ( 3 ) DEFAULT '' ; 
DECLARE LL VARCHAR ( 1024 ) DEFAULT '' ; 
DECLARE V_IN_PARM CLOB ( 2 G ) DEFAULT '' ; 
-- Error handling variables 
DECLARE V_SQLSTATE CHAR ( 5 ) DEFAULT '00000' ; 
DECLARE V_SQLCODE INTEGER DEFAULT 0 ; 
DECLARE V_ERRMSG VARCHAR ( 500 ) DEFAULT '' ; 
DECLARE V_HTTP_STATUS INTEGER DEFAULT 200 ; 
DECLARE V_HAS_ERROR INTEGER DEFAULT 0 ; 
-- Continue handlers for error capture 
DECLARE CONTINUE HANDLER FOR SQLSTATE '22007'  -- Invalid datetime 
BEGIN 
SET V_SQLSTATE = '22007' ; 
GET DIAGNOSTICS CONDITION 1 V_ERRMSG = MESSAGE_TEXT ; 
SET V_HTTP_STATUS = 400 ; 
SET V_HAS_ERROR = 1 ; 
END ; 
DECLARE CONTINUE HANDLER FOR SQLSTATE '22003'  -- Numeric out of range 
BEGIN 
SET V_SQLSTATE = '22003' ; 
GET DIAGNOSTICS CONDITION 1 V_ERRMSG = MESSAGE_TEXT ; 
SET V_HTTP_STATUS = 400 ; 
SET V_HAS_ERROR = 1 ; 
END ; 
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000'  -- No data found 
BEGIN 
SET V_SQLSTATE = '02000' ; 
SET V_ERRMSG = 'No data found for the specified criteria' ; 
SET V_HTTP_STATUS = 404 ; 
SET V_HAS_ERROR = 1 ; 
END ; 
DECLARE CONTINUE HANDLER FOR SQLSTATE '42501'  -- Not authorized 
BEGIN 
SET V_SQLSTATE = '42501' ; 
GET DIAGNOSTICS CONDITION 1 V_ERRMSG = MESSAGE_TEXT ; 
SET V_HTTP_STATUS = 403 ; 
SET V_HAS_ERROR = 1 ; 
END ; 
DECLARE CONTINUE HANDLER FOR SQLSTATE '42704'  -- Object not found 
BEGIN 
SET V_SQLSTATE = '42704' ; 
GET DIAGNOSTICS CONDITION 1 V_ERRMSG = MESSAGE_TEXT ; 
SET V_HTTP_STATUS = 404 ; 
SET V_HAS_ERROR = 1 ; 
END ; 
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION 
BEGIN 
GET DIAGNOSTICS CONDITION 1 
V_SQLSTATE = RETURNED_SQLSTATE , 
V_SQLCODE = DB2_RETURNED_SQLCODE , V_ERRMSG = MESSAGE_TEXT ; 
SET V_HTTP_STATUS = 500 ; 
SET V_HAS_ERROR = 1 ; 
END ; 
-- Parse input parameters 
SET V_PERIOD = COALESCE ( UPPER ( JSON_VALUE ( IN_PARM , '$.period' ) ) , 'D' ); 
SET V_PLANT = COALESCE ( JSON_VALUE ( IN_PARM , '$.plant' ) , '' ) ; 
SET V_DEPT = COALESCE ( JSON_VALUE ( IN_PARM , '$.department' ) , '' ) ; 
SET V_WC = COALESCE ( JSON_VALUE ( IN_PARM , '$.workcenter' ) , '' ) ; 
SET V_SITE = COALESCE ( JSON_VALUE ( IN_PARM , '$.site' ) , '' ) ; 
-- Extract env handling all input formats (object, array, string) 
SET V_IN_PARM = IN_PARM ; 
WHILE CISTOOLS . JSON_TYPE ( V_IN_PARM ) IN ( 'object' , 'array' , 'string' ) 
DO 
SET V_IN_PARM = 
CASE CISTOOLS . JSON_TYPE ( V_IN_PARM ) 
WHEN 'object' THEN JSON_VALUE ( V_IN_PARM , '$.env' ) 
WHEN 'array' THEN JSON_VALUE ( V_IN_PARM , '$[0]' ) 
WHEN 'string' THEN REPLACE ( V_IN_PARM , '"' , '' ) 
ELSE TRIM ( V_IN_PARM ) 
END ; 
END WHILE ; 
SET ENV = V_IN_PARM ; 
SET LL = CISTOOLS . SET_LIBRARY_LIST ( ENV ) ; 
-- Validate environment was set 
IF ENV = '' 
OR ENV IS NULL THEN 
RETURN 
JSON_OBJECT ( 
'error' : JSON_OBJECT ( 
'http_status' : 400 , 'sqlstate' : '22001' , 
'message' : 'Environment parameter is required' 
) 
) ; 
END IF ; 
-- Check for errors from library list setup 
IF V_HAS_ERROR = 1 THEN 
RETURN 
JSON_OBJECT ( 
'error' : JSON_OBJECT ( 
'http_status' : V_HTTP_STATUS , 'sqlstate' : V_SQLSTATE , 
'message' : V_ERRMSG 
) 
) ; 
END IF ; 
RETURN 
WITH BOUNDS AS ( 
SELECT CURRENT_DATE AS FOR_DATE , 2 AS WEEK_START_DOW , 
ZONED ( CURRENT_DATE ) AS YYYYMMDD_FOR , 
ZONED ( 
LAST_DAY ( ADD_MONTHS ( CURRENT_DATE , - 1 ) ) + 1 DAY ) 
AS YYYYMMDD_MONTH_FROM , 
ZONED ( LAST_DAY ( CURRENT_DATE ) ) 
AS YYYYMMDD_MONTH_THRU , 
DAYOFWEEK ( CURRENT_DATE ) AS DOW_FOR 
FROM SYSIBM . SYSDUMMY1 
) , 
WEEK_CALC AS ( 
SELECT B . * , 
MOD ( B . DOW_FOR - B . WEEK_START_DOW + 7 , 7 ) 
AS DELTA_DAYS 
FROM BOUNDS B 
) , 
DATE_RANGE AS ( 
SELECT 
CASE 
V_PERIOD 
WHEN 'M' THEN ( YYYYMMDD_MONTH_FROM - 19000000 ) 
WHEN 
'W' 
THEN 
( ( YEAR ( FOR_DATE - DELTA_DAYS DAYS ) * 10000 
+ 
MONTH ( FOR_DATE - DELTA_DAYS DAYS ) * 100 
+ DAY ( FOR_DATE - DELTA_DAYS DAYS ) ) - 
19000000 ) 
ELSE ( YYYYMMDD_FOR - 19000000 ) 
END AS FROM_CY , 
CASE V_PERIOD 
WHEN 'M' THEN ( YYYYMMDD_MONTH_THRU - 19000000 ) 
ELSE ( YYYYMMDD_FOR - 19000000 ) 
END AS THRU_CY 
FROM WEEK_CALC 
) , 
-- Group by workcenter and site from host file 
LABOR_BY_WC AS ( 
SELECT TRIM ( P . KIWKCTR ) AS WKCTR , TRIM ( P . KISTID ) AS STID , 
TRIM ( P . KIGRP1 ) AS PLANT , TRIM ( P . KIGRP2 ) AS DEPT , 
SUM ( P . KIARUN ) AS RUN_SEC , 
SUM ( P . KIASET ) AS SETUP_SEC , 
SUM ( P . KIASDW ) AS APPL_SDWN_SEC , 
SUM ( P . KIAUDW ) AS APPL_UDWN_SEC , 
SUM ( P . KIUP ) AS UP_SEC , SUM ( P . KISDWN ) AS SDWN_SEC , 
SUM ( P . KIUDWN ) AS UDWN_SEC 
FROM SFCPERI P , DATE_RANGE R 
WHERE P . KIDATE BETWEEN R . FROM_CY AND R . THRU_CY 
AND TRIM ( P . KIGRP1 ) = COALESCE ( 
NULLIF ( V_PLANT , '' ) , TRIM ( P . KIGRP1 ) ) 
AND TRIM ( P . KIGRP2 ) = COALESCE ( 
NULLIF ( V_DEPT , '' ) , TRIM ( P . KIGRP2 ) ) 
AND TRIM ( P . KIWKCTR ) = COALESCE ( 
NULLIF ( V_WC , '' ) , TRIM ( P . KIWKCTR ) ) 
AND TRIM ( P . KISTID ) = COALESCE ( 
NULLIF ( V_SITE , '' ) , TRIM ( P . KISTID ) ) 
GROUP BY P . KIWKCTR , P . KISTID , P . KIGRP1 , P . KIGRP2 
) , 
WC_CALCS AS ( 
SELECT WKCTR , STID , PLANT , DEPT , 
DEC ( DEC ( RUN_SEC , 31 , 10 ) / 3600 , 13 , 2 ) 
AS RUN_HOURS , 
DEC ( DEC ( SETUP_SEC , 31 , 10 ) / 3600 , 13 , 2 ) 
AS SETUP_HOURS , 
DEC ( DEC ( APPL_SDWN_SEC , 31 , 10 ) / 3600 , 13 , 2 ) 
AS SCHED_DOWN_HOURS , 
DEC ( DEC ( APPL_UDWN_SEC , 31 , 10 ) / 3600 , 13 , 2 ) 
AS UNSCHED_DOWN_HOURS , 
DEC ( 
DEC ( 
RUN_SEC + SETUP_SEC + APPL_SDWN_SEC + 
APPL_UDWN_SEC , 31 , 10 ) / 3600 , 13 , 2 ) 
AS TOTAL_APPLIED_HOURS , 
DEC ( DEC ( UP_SEC , 31 , 10 ) / 3600 , 13 , 2 ) 
AS UPTIME_HOURS , 
DEC ( DEC ( SDWN_SEC + UDWN_SEC , 31 , 10 ) / 3600 , 13 , 2 ) 
AS DOWNTIME_HOURS 
FROM LABOR_BY_WC 
) , 
JSON_WORKCENTERS AS ( 
SELECT 
JSON_OBJECT ( 
'workcenter' : CAST ( WKCTR AS VARCHAR ( 5 ) CCSID 37 ) , 
'site' : CAST ( STID AS VARCHAR ( 3 ) CCSID 37 ) , 
'plant' : CAST ( PLANT AS VARCHAR ( 15 ) CCSID 37 ) , 
'department' : CAST ( DEPT AS VARCHAR ( 15 ) CCSID 37 ) , 
'runHours' : COALESCE ( RUN_HOURS , 0 ) , 
'setupHours' : COALESCE ( SETUP_HOURS , 0 ) , 
'scheduledDownHours' : COALESCE ( 
SCHED_DOWN_HOURS , 0 ) , 
'unscheduledDownHours' : COALESCE ( 
UNSCHED_DOWN_HOURS , 0 ) , 
'totalAppliedHours' : COALESCE ( 
TOTAL_APPLIED_HOURS , 0 ) , 
'uptimeHours' : COALESCE ( UPTIME_HOURS , 0 ) , 
'downtimeHours' : COALESCE ( DOWNTIME_HOURS , 0 ) 
) AS WC_OBJ 
FROM WC_CALCS 
ORDER BY WKCTR , STID ) , 
TOTALS AS ( 
SELECT SUM ( RUN_HOURS ) AS TOTAL_RUN , 
SUM ( SETUP_HOURS ) AS TOTAL_SETUP , 
SUM ( SCHED_DOWN_HOURS ) AS TOTAL_SCHED_DOWN , 
SUM ( UNSCHED_DOWN_HOURS ) AS TOTAL_UNSCHED_DOWN , 
SUM ( TOTAL_APPLIED_HOURS ) AS OVERALL_APPLIED , 
SUM ( UPTIME_HOURS ) AS TOTAL_UPTIME , 
SUM ( DOWNTIME_HOURS ) AS TOTAL_DOWNTIME 
FROM WC_CALCS 
) SELECT JSON_OBJECT ( 
'workcenters' : ( SELECT JSON_ARRAYAGG ( 
WC_OBJ FORMAT JSON 
) 
FROM JSON_WORKCENTERS ) FORMAT JSON , 'totals' : ( 
SELECT JSON_OBJECT ( 
'runHours' : COALESCE ( TOTAL_RUN , 0 ) , 
'setupHours' : COALESCE ( TOTAL_SETUP , 0 ) , 
'scheduledDownHours' : COALESCE ( TOTAL_SCHED_DOWN , 0 ) , 
'unscheduledDownHours' : COALESCE ( 
TOTAL_UNSCHED_DOWN , 0 ) , 
'totalAppliedHours' : COALESCE ( OVERALL_APPLIED , 0 ) , 
'uptimeHours' : COALESCE ( TOTAL_UPTIME , 0 ) , 
'downtimeHours' : COALESCE ( TOTAL_DOWNTIME , 0 ) 
) 
FROM TOTALS ) FORMAT JSON , 'period' : V_PERIOD , 
'periodLabel' : 
CASE V_PERIOD 
WHEN 'M' THEN 'Monthly' 
WHEN 'W' THEN 'Weekly' 
ELSE 'Daily' 
END , 'generatedAt' : TO_CHAR ( 
CURRENT_TIMESTAMP , 'YYYY-MM-DD HH24:MI:SS' ) 
) 
FROM SYSIBM . SYSDUMMY1 ; 
END  ; 

GRANT ALTER , EXECUTE   
ON SPECIFIC FUNCTION RESTAPI.SFP_GET_LABOR_HOURS 
TO AMAPICS WITH GRANT OPTION ; 

GRANT EXECUTE   
ON SPECIFIC FUNCTION RESTAPI.SFP_GET_LABOR_HOURS 
TO PUBLIC ; 

