CREATE OR REPLACE FUNCTION public.sp_setstatuses(
	planrequestid uuid)
    RETURNS void
    LANGUAGE 'plpgsql'

    COST 100
    VOLATILE 
AS $BODY$
BEGIN

		update t1 
	set Approved = case when t3.Status = 5 AND t3.TfomsStatus = 5 THEN 1 ELSE 0  END, t1.Mo = t2.Mo
		FROM FIES.T_PLAN_ITEM t1
		  LEFT JOIN FIES.T_PLAN t2
			ON t2.PlanID = t1.Plan
		  LEFT JOIN FIES.T_PLAN_REQUEST t3
			ON t3.PlanID = t1.Plan
		Where t2.PlanID = PlanRequestID;
END;
$BODY$;
