SET PATH *LIBL ; 

CREATE OR REPLACE FUNCTION RESTAPI.SFP_GET_PRODUCTION_STATS ( 
	IN_PARM CLOB(2147483647) ) 
	RETURNS CLOB(2147483647)   
	LANGUAGE SQL 
	SPECIFIC RESTAPI.SFP_GET_PRODUCTION_STATS 
	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_DATE DATE ; 
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_DATE = COALESCE ( 
DATE ( JSON_VALUE ( IN_PARM , '$.date' ) ) , CURRENT_DATE ) ; 
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 V_DATE AS FOR_DATE , 2 AS WEEK_START_DOW , 
ZONED ( V_DATE ) AS YYYYMMDD_FOR , 
ZONED ( LAST_DAY ( ADD_MONTHS ( V_DATE , - 1 ) ) + 1 DAY ) 
AS YYYYMMDD_MONTH_FROM , 
ZONED ( LAST_DAY ( V_DATE ) ) AS YYYYMMDD_MONTH_THRU , 
DAYOFWEEK ( V_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 
) , 
WC_DATA AS ( 
SELECT TRIM ( P . KIWKCTR ) AS WKCTR , TRIM ( P . KISTID ) AS STID , 
TRIM ( P . KIGRP1 ) AS PLANT , TRIM ( P . KIGRP2 ) AS DEPT , 
SUM ( P . KIEXPT ) AS TARGET_QTY , 
SUM ( P . KIRCVD ) AS GOOD_QTY , 
SUM ( P . KISCRP ) AS SCRAP_QTY , SUM ( P . KIUP ) AS UP_SEC , 
SUM ( P . KIUDWN ) AS UDWN_SEC , 
SUM ( P . KISDWN ) AS SDWN_SEC , SUM ( P . KIGAP ) AS GAP_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 , TARGET_QTY , 
GOOD_QTY AS UNITS_PRODUCED , 
SCRAP_QTY AS SCRAP_UNITS , 
( TARGET_QTY - GOOD_QTY ) AS UNITS_REMAINING , 
CASE 
WHEN 
TARGET_QTY > 0 
THEN 
INT ( 
( DECFLOAT ( GOOD_QTY ) / 
DECFLOAT ( TARGET_QTY ) * 100 ) + 0.5 ) 
ELSE 0 
END AS CURRENT_RATE_PCT , 
CASE 
WHEN 
UP_SEC > 0 
THEN 
INT ( 
( DECFLOAT ( UP_SEC - UDWN_SEC ) / 
DECFLOAT ( UP_SEC ) * 100 ) + 0.5 ) 
ELSE 0 
END AS UPTIME_PCT , 
CASE 
WHEN UDWN_SEC > 0 THEN 'down' 
ELSE 'up' 
END AS LINE_STATUS 
FROM WC_DATA 
) , 
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 ) , 
'targetUnits' : COALESCE ( TARGET_QTY , 0 ) , 
'unitsProduced' : COALESCE ( UNITS_PRODUCED , 0 ) , 
'scrapUnits' : COALESCE ( SCRAP_UNITS , 0 ) , 
'unitsRemaining' : COALESCE ( UNITS_REMAINING , 0 ) , 
'currentRate' : COALESCE ( CURRENT_RATE_PCT , 0 ) , 
'targetRate' : 100 , 'uptimePercentage' : COALESCE ( 
UPTIME_PCT , 0 ) , 'lineStatus' : COALESCE ( 
LINE_STATUS , 'up' ) 
) AS WC_OBJ 
FROM WC_CALCS 
ORDER BY WKCTR , STID ) , 
TOTALS AS ( 
SELECT SUM ( TARGET_QTY ) AS TOTAL_TARGET , 
SUM ( UNITS_PRODUCED ) AS TOTAL_PRODUCED , 
SUM ( SCRAP_UNITS ) AS TOTAL_SCRAP , 
SUM ( UNITS_REMAINING ) AS TOTAL_REMAINING 
FROM WC_CALCS 
) SELECT JSON_OBJECT ( 
'workcenters' : ( SELECT JSON_ARRAYAGG ( 
WC_OBJ FORMAT JSON 
) 
FROM JSON_WORKCENTERS ) FORMAT JSON , 'totals' : ( 
SELECT JSON_OBJECT ( 
'targetUnits' : COALESCE ( TOTAL_TARGET , 0 ) , 
'unitsProduced' : COALESCE ( TOTAL_PRODUCED , 0 ) , 
'scrapUnits' : COALESCE ( TOTAL_SCRAP , 0 ) , 
'unitsRemaining' : COALESCE ( TOTAL_REMAINING , 0 ) , 
'currentRate' : 
CASE 
WHEN 
TOTAL_TARGET > 0 
THEN 
INT ( 
( DECFLOAT ( TOTAL_PRODUCED ) / 
DECFLOAT ( TOTAL_TARGET ) * 100 ) + 0.5 ) 
ELSE 0 
END , 'targetRate' : 100 
) 
FROM TOTALS ) , 'period' : V_PERIOD , 'periodLabel' : 
CASE V_PERIOD 
WHEN 'M' THEN 'Monthly' 
WHEN 'W' THEN 'Weekly' 
ELSE 'Daily' 
END , 'forDate' : TO_CHAR ( V_DATE , 'YYYY-MM-DD' ) , 
'generatedAt' : TO_CHAR ( 
CURRENT_TIMESTAMP , 'YYYY-MM-DD HH24:MI:SS' ) 
) 
FROM SYSIBM . SYSDUMMY1 ; 
END  ; 

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

GRANT EXECUTE   
ON SPECIFIC FUNCTION RESTAPI.SFP_GET_PRODUCTION_STATS 
TO PUBLIC ; 

