CREATE OR REPLACE
FUNCTION SET_VRCIAP(
	P_CDIMPOSTO 	IN IMPOSTO.CDIMPOSTO%TYPE,
	P_CDEMPRESA		IN EMPRESA.CDEMPRESA%TYPE,
	P_NRSEQCIAP		IN CONTROLECIAP.NRSEQCIAP%TYPE,
	P_DTCOMPETENCIA IN DATE)
	RETURN BOOLEAN
AS
	V_IMPOSTONFE IMPOSTO.CDIMPOSTONFE%TYPE;
	V_VALOR FLOAT;
	V_APROPRIAR FLOAT;
	V_VRCOEFICIAP FLOAT;
BEGIN
	SELECT SUM(VRPARCELACIAP) AS VALOR, VRCOEFICIAP
		  INTO V_VALOR, V_VRCOEFICIAP
		  FROM BEMCONTROLECIAP B, CONTROLECIAP C
		 WHERE B.CDEMPRESA     = P_CDEMPRESA
		   AND B.CDIMPOSTO     = P_CDIMPOSTO
		   AND B.DTCOMPETECIAP = P_DTCOMPETENCIA
		   AND B.CDEMPRESA     = C.CDEMPRESA
		   AND B.CDIMPOSTO     = C.CDIMPOSTO
		   AND B.DTCOMPETECIAP = C.DTCOMPETECIAP
		   AND B.NRSEQCIAP     = C.NRSEQCIAP
		   AND B.NRSEQCIAP     = P_NRSEQCIAP
		GROUP BY VRCOEFICIAP;
		V_IMPOSTONFE := GET_VERIFICA_IMPOSTO(P_CDIMPOSTO);
		IF ((V_IMPOSTONFE = '60') OR (V_IMPOSTONFE = '50')) THEN
			V_APROPRIAR := V_VALOR;
		ELSE
			V_APROPRIAR := V_VALOR * V_VRCOEFICIAP;
		END IF;
		
		UPDATE CONTROLECIAP
		   SET VRCREDAPROPCIAP = V_APROPRIAR,
			   VRSALDOPARCCIAP = V_VALOR
		 WHERE CDIMPOSTO       = P_CDIMPOSTO
		   AND DTCOMPETECIAP   = P_DTCOMPETENCIA
		   AND CDEMPRESA       = P_CDEMPRESA
		   AND NRSEQCIAP       = P_NRSEQCIAP;
		 RETURN TRUE;
END;
/
