-- PROCEDURE: internal.pupdatereadingeggsweektotdate(date, integer)

-- DROP PROCEDURE IF EXISTS internal.pupdatereadingeggsweektotdate(date, integer);

CREATE OR REPLACE PROCEDURE internal.pupdatereadingeggsweektotdate(
	par_calcdate date,
	par_personid integer DEFAULT NULL::integer)
LANGUAGE 'plpgsql'
AS $BODY$

DECLARE
    var_ToTStartDate DATE DEFAULT dbo.fgetsystemparameter('Reading Eggs', 'Time on Task', 'Valid From', 0, '2018-01-01');
    var_PrevDate DATE DEFAULT par_CalcDate + (- 7::NUMERIC || ' DAY')::INTERVAL;
    var_rowcount int;
BEGIN
    /*
    [7810 - Severity CRITICAL - PostgreSQL doesn't support the SET NOCOUNT. If need try another way to send message back to the client application.]
    SET NOCOUNT ON;
    */
    /*
    [9996 - Severity CRITICAL - Transformer error occurreD. Please submit report to developers.]
    */
    UPDATE dbo.LearnerDataReadingEggs D  SET
    		"re week time (sec)" = CASE
    			WHEN D."re time (sec)" IS NOT NULL AND d1."re time (sec)" IS NULL                                                 THEN D."re time (sec)"
    			WHEN D."re time (sec)" IS NOT NULL AND d1."re time (sec)" IS NOT NULL AND D."re time (sec)" >= d1."re time (sec)" THEN D."re time (sec)" - d1."re time (sec)"
    			WHEN D."re time (sec)" IS NOT NULL AND d1."re time (sec)" IS NOT NULL AND D."re time (sec)" <  d1."re time (sec)" THEN D."re time (sec)"
    			WHEN D."re time (sec)" IS NULL     AND d1."re time (sec)" IS NOT NULL                                             THEN NULL
    		END,
    		"rex week time (sec)" = CASE
    			WHEN D."rex time (sec)" IS NOT NULL AND d1."rex time (sec)" IS NULL                                                   THEN D."rex time (sec)"
    			WHEN D."rex time (sec)" IS NOT NULL AND d1."rex time (sec)" IS NOT NULL AND D."rex time (sec)" >= d1."rex time (sec)" THEN D."rex time (sec)" - d1."rex time (sec)"
    			WHEN D."rex time (sec)" IS NOT NULL AND d1."rex time (sec)" IS NOT NULL AND D."rex time (sec)" <  d1."rex time (sec)" THEN D."rex time (sec)"
    			WHEN D."rex time (sec)" IS NULL     AND d1."rex time (sec)" IS NOT NULL                                               THEN NULL
    		END,
    		"fp week time (sec)" = CASE
    			WHEN D."fp time (sec)" IS NOT NULL AND d1."fp time (sec)" IS NULL                                                   THEN D."fp time (sec)"
    			WHEN D."fp time (sec)" IS NOT NULL AND d1."fp time (sec)" IS NOT NULL AND D."fp time (sec)" >= d1."fp time (sec)" THEN D."fp time (sec)" - d1."fp time (sec)"
    			WHEN D."fp time (sec)" IS NOT NULL AND d1."fp time (sec)" IS NOT NULL AND D."fp time (sec)" <  d1."fp time (sec)" THEN D."fp time (sec)"
    			WHEN D."fp time (sec)" IS NULL     AND d1."fp time (sec)" IS NOT NULL                                               THEN NULL
    		END
--     	FROM dbo.LearnerDataReadingEggs D
    	FROM dbo.LearnerDataReadingEggs D1 -- ON D.PersonID = D1.PersonID AND D1.Date = var_PrevDate
    	WHERE  D.PersonID = D1.PersonID AND D1.Date = var_PrevDate
		 	AND D.date >= var_ToTStartDate
    		AND D.date = par_calcdate
    		AND ((par_personid IS NULL) OR (par_personid IS NOT NULL AND D.PersonID = par_personid));
    GET DIAGNOSTICS var_rowcount = ROW_COUNT;
    /**/
--   RAISE 'Error %, severity %, state % was raiseD. Message: %. Argument: %', '50000', 0, 0, 'Rows set: %s', var_rowcount USING ERRCODE = '50000';
END;
$BODY$;
