CREATE OR REPLACE PROCEDURE CARREGA_IMPOSTO_EAM(
	P_DTCOMPETENCIA	IN DATE,
	P_CDEMPRESA		IN EMPRESA.CDEMPRESA%TYPE,
	P_CDIMPOSTO		IN IMPOSTO.CDIMPOSTO%TYPE,
	P_VRSALDOACUMU	IN FLOAT,
	P_IDOPERACAO	IN VARCHAR2,
	P_CDINSCESTA	IN VARCHAR2,
	P_NRSEQCIAP		IN CONTROLECIAP.NRSEQCIAP%TYPE,
	P_RETORNO		OUT VARCHAR2)
AS
	V_TOTAL_SAIDAS FLOAT;
	V_TOTAL_TRIBUTADAS FLOAT;
	V_IMPOSTOR IMPOSTO.CDIMPOSTONFE%TYPE;
	V_ALIQ FLOAT;
	V_BOOL BOOLEAN;
BEGIN
	P_RETORNO := NULL;
	IF (P_IDOPERACAO = 'IMP') THEN
		SELECT NVL(SUM(NOTAFISCAL.VRNOTAFISC),0) TOTAL_SAIDAS,
			   NVL(SUM(IMPOSTONF.VRIMPOSTONF),0) TOTAL_TRIBUTADAS
			  INTO V_TOTAL_SAIDAS, V_TOTAL_TRIBUTADAS
			  FROM NOTAFISCAL, IMPOSTONF
			 WHERE NOTAFISCAL.CDFILIAL 		= IMPOSTONF.CDFILIAL
			   AND NOTAFISCAL.NRLANCTONF 	= IMPOSTONF.NRLANCTONF
			   AND NOTAFISCAL.IDENTRSAIDOP 	= 'S'
			   AND NOTAFISCAL.DTENTRSAID 	= P_DTCOMPETENCIA
			   AND NOTAFISCAL.CDEMPRESA 	= P_CDEMPRESA
			   AND IMPOSTONF.CDIMPOSTO 		= P_CDIMPOSTO;
		IF (V_TOTAL_SAIDAS <> 0) AND (V_TOTAL_TRIBUTADAS <> 0) THEN
			UPDATE CONTROLECIAP
			   SET VRSAIDACIAP		= V_TOTAL_SAIDAS,
				   VRSAIDATRIBCIAP	= V_TOTAL_TRIBUTADAS,
				   VRCOEFICIAP 		= V_TOTAL_TRIBUTADAS / V_TOTAL_SAIDAS,
				   VRCREDAPROPCIAP  = P_VRSALDOACUMU * V_TOTAL_TRIBUTADAS / V_TOTAL_SAIDAS
			 WHERE DTCOMPETECIAP	= P_DTCOMPETENCIA
			   AND CDEMPRESA		= P_CDEMPRESA
			   AND CDIMPOSTO 		= P_CDIMPOSTO
			   AND NRSEQCIAP 		= P_NRSEQCIAP;
			P_RETORNO := 'V';
		ELSE
			P_RETORNO := 'F';
		END IF;
	ELSE
		IF (P_IDOPERACAO = 'BEM') THEN 
			DELETE FROM BEMCONTROLECIAP
				  WHERE CDEMPRESA 	  = P_CDEMPRESA
				    AND CDIMPOSTO 	  = P_CDIMPOSTO
				    AND DTCOMPETECIAP = ADD_MONTHS(LAST_DAY(P_DTCOMPETENCIA)+1,-1)
				    AND NRSEQCIAP     = P_NRSEQCIAP;
			
			V_IMPOSTOR := GET_VERIFICA_IMPOSTO(P_CDIMPOSTO);
			FOR C_BEM_PARCELA IN (SELECT PR.CDBEMATIVO, BA.IDTPAPROPPISCOF, PR.VRPARCELACIAP
								  FROM PARCELACIAPBEM PR, BEMATIVO BA, FILIAL F
								 WHERE PR.CDEMPRESA 	 = P_CDEMPRESA
								   AND PR.CDIMPOSTO 	 = P_CDIMPOSTO
								   AND PR.DTCOMPPARCCIAP = P_DTCOMPETENCIA
								   AND PR.CDBEMATIVO 	 = BA.CDBEMATIVO
								   AND PR.CDEMPRESA 	 = BA.CDEMPRESA
								   AND BA.CDFILIAL		 = F.CDFILIAL
								   AND F.CDINSCESTA	 	 = P_CDINSCESTA
								   AND ((BA.IDTPAPROPPISCOF NOT IN ('09','11')) OR (V_IMPOSTOR NOT IN ('50','60'))))
			LOOP
				INSERT INTO BEMCONTROLECIAP(CDEMPRESA,	CDIMPOSTO,		 DTCOMPETECIAP,
											CDBEMATIVO,	IDTPAPROPPISCOF, VRPARCELACIAP,
											NRSEQCIAP,	CDINSCESTA)
									VALUES (P_CDEMPRESA,				P_CDIMPOSTO,					ADD_MONTHS(LAST_DAY(P_DTCOMPETENCIA)+1,-1),
											C_BEM_PARCELA.CDBEMATIVO,	C_BEM_PARCELA.IDTPAPROPPISCOF,	C_BEM_PARCELA.VRPARCELACIAP,
											P_NRSEQCIAP,				P_CDINSCESTA);
			END LOOP;
			IF (V_IMPOSTOR = '50') OR (V_IMPOSTOR = '60') THEN
				IF (V_IMPOSTOR = '50') THEN
					V_ALIQ := 1.65;
				ELSE
					V_ALIQ := 7.65;
				END IF;
				FOR C_BEM_DEPREC IN (SELECT BA.CDBEMATIVO, OP.VROPERABEM, OP.IDIDENTOPERABEM, BA.IDTPAPROPPISCOF
									  FROM OPERACAOBEM OP, BEMATIVO BA, FILIAL F, TIPOOPERBEM TOB, IMPOSTOBEM IMP
									 WHERE TOB.IDTIPOOPERBEM 						= '02'
									   AND BA.CDBEMATIVO 							= OP.CDBEMATIVO
									   AND BA.CDEMPRESA								= OP.CDEMPRESA
									   AND BA.CDFILIAL  							= F.CDFILIAL
									   AND BA.CDEMPRESA 							= P_CDEMPRESA
									   AND ('01/'||TO_CHAR(OP.DTOPERABEM,'MM/YYYY') = ADD_MONTHS(LAST_DAY(P_DTCOMPETENCIA)+1,-1))
									   AND (BA.IDTPAPROPPISCOF 						IN ('09','11'))
									   AND F.CDINSCESTA 							= P_CDINSCESTA
									   AND OP.CDTIPOOPERBEM 						= TOB.CDTIPOOPERBEM
									   AND IMP.CDEMPRESA 							= BA.CDEMPRESA
									   AND IMP.CDIMPOSTO 							= P_CDIMPOSTO
									   AND IMP.CDBEMATIVO 							= BA.CDBEMATIVO)
				LOOP
					INSERT INTO BEMCONTROLECIAP(CDEMPRESA,	CDIMPOSTO,		 DTCOMPETECIAP,
												CDBEMATIVO,	IDTPAPROPPISCOF, VRPARCELACIAP,
												NRSEQCIAP,	CDINSCESTA)
										VALUES (P_CDEMPRESA, 			 P_CDIMPOSTO, 					ADD_MONTHS(LAST_DAY(P_DTCOMPETENCIA)+1,-1),
												C_BEM_DEPREC.CDBEMATIVO, C_BEM_DEPREC.IDTPAPROPPISCOF, 	C_BEM_DEPREC.VROPERABEM * V_ALIQ / 100,
												P_NRSEQCIAP, 			 P_CDINSCESTA);
				END LOOP;
			END IF;
			V_BOOL := SET_VRCIAP(P_CDIMPOSTO, P_CDEMPRESA, P_NRSEQCIAP, P_DTCOMPETENCIA);
		END IF;
	END IF;
END;
/