--622
CREATE OR REPLACE
PROCEDURE EAM_PROCESSAR_ENTRADA(
	P_CDOPERADOR		  IN OPERADOR.CDOPERADOR%TYPE,
	P_CDEMPRESA			  IN EMPRESA.CDEMPRESA%TYPE,
	P_NRSEQCAPAENTBEM	IN CAPAENTRADABEM.NRSEQCAPAENTBEM%TYPE,
	P_GERABEM			    IN VARCHAR2,
	P_NRORG				    IN NUMBER,
	P_RETORNO			    OUT VARCHAR2)
AS
	V_CONT				      INTEGER;
	V_CONT2				      INTEGER;
	V_VRINICIAL			    FLOAT;
	V_VRINICIALFIS		  FLOAT;
	V_NRSEQITEM			    VARCHAR2(20);
	V_CODPATR			      VARCHAR2(20);
	V_ITCAPA			      INTEGER;
	V_TRASFDESCART		  INTEGER;
	V_TODOSITCAPA		    INTEGER;
	V_GRUPO				      INTEGER;
	V_QTDE				      INTEGER;
	V_NUMBEM			      INTEGER;
	V_TESTANUM			    INTEGER;
	V_CDBEMATIVO		    VARCHAR2(20);
	V_CDPATRIBEMATIVO	  BEMATIVO.CDPATRIBEMATIVO%TYPE;
	V_CODPATRI			    BEMATIVO.CDPATRIBEMATIVO%TYPE;
	V_RECEBE_CODPATRI	  BEMATIVO.CDPATRIBEMATIVO%TYPE;
	V_SGUNIDADE			    BEMATIVO.SGUNIDADE%TYPE;
	V_VRINIDEPITCENT	  ITCAPAENTRABEM.VRINIDEPITCENT%TYPE;
	V_VRAQUIITCAPAENT	  ITCAPAENTRABEM.VRAQUIITCAPAENT%TYPE;
	V_QTMESDEPITCENT	  ITCAPAENTRABEM.QTMESDEPITCENT%TYPE;
	V_QTMESJADEPBEM		  BEMATIVO.QTMESJADEPBEM%TYPE;
	V_QTMESJADEPBEMFI	  BEMATIVO.QTMESJADEPBEMFI%TYPE;
	V_QTMESJADEP		    BEMATIVO.QTMESJADEPBEM%TYPE;
	V_QTMESJADEPFIS		  BEMATIVO.QTMESJADEPBEMFI%TYPE;
	V_QTMESFALTADBEM	  FLOAT;
	V_QTMESDEPBEMFISC	  FLOAT;
	V_VRTXANODEPBEMFI	  FLOAT;
	V_VRINIDEPRECFIS	  FLOAT;
	V_QTMESFALTDBEMFIS	FLOAT;
	V_VRFALTDEPBEMFIS	  FLOAT;
	V_QTMESUSOITCENT	  INTEGER;
	V_NRSEQITPROD		    ITEMPRODUTO.NRSEQITPROD%TYPE;
	V_REGRAT			      INTEGER;
	V_CONTRAT			      INTEGER;
	V_CONTRAT2			    INTEGER;
	V_SUMRAT			      FLOAT;
	V_COUNTRATEIO		    INTEGER;
	V_VRRATEIO			    FLOAT;
	V_VRAQUIBEMFISC		  FLOAT;
	V_VRFALTADEPBEM		  FLOAT;
	V_NRSEQCAPAENTBEM		CAPAENTRADABEM.NRSEQCAPAENTBEM%TYPE;
	V_NRSEQITCAPAENT		ITCAPAENTRABEM.NRSEQITCAPAENT%TYPE;
	V_NRBEMQTITCAPAEN		QTITCAPAENTRABE.NRBEMQTITCAPAEN%TYPE;
	V_QTBEMQTITCAPAEN		QTITCAPAENTRABE.QTBEMQTITCAPAEN%TYPE;
BEGIN
	V_CONT       := -1;
	P_RETORNO    := '';
	V_CODPATRI   := '0';
	V_QTMESJADEP := 0;
	V_NRSEQITEM  := '';
	V_NUMBEM     := 0;
	V_TESTANUM   := 0;
	
	--adqPendRestri
	SELECT COUNT(NRSEQITCAPAENT) AS ITCAPA
	  INTO V_ITCAPA
	  FROM ITCAPAENTRABEM
	 WHERE IDBEMITCAPAENT IN ('1','2')
	   AND P_CDEMPRESA 		 = CDEMPRESA
	   AND P_NRSEQCAPAENTBEM = NRSEQCAPAENTBEM;
		
	IF (V_ITCAPA > 0) THEN
		P_RETORNO := 'Operação não pode ser processada. Existem itens com status "Pendente" e ou "Verificado com restrição".';
	ELSE
		--adqProcessar
		V_CONT := 0;
		FOR C_PROCESSAR IN (SELECT ITCAPAENTRABEM.CDEMPRESA,  ITCAPAENTRABEM.NRSEQITCAPAENT,  ITCAPAENTRABEM.NRSEQCAPAENTBEM,
								   ITCAPAENTRABEM.NMBEMITCAPAENT,         ITCAPAENTRABEM.CDPATITCAPAENT,  ITCAPAENTRABEM.DTAQUIITCAPAENT,
								   ITCAPAENTRABEM.QTBEMITCAPAENT,         ITCAPAENTRABEM.SGUNIDADE,       ITCAPAENTRABEM.VRAQUIITCAPAENT,
								   ITCAPAENTRABEM.CDCENTCUITCENT,         ITCAPAENTRABEM.CDGRUPOITCENT,   ITCAPAENTRABEM.CDTIPODEPREC,
								   ITCAPAENTRABEM.QTMESDEPITCENT,         ITCAPAENTRABEM.VRINIDEPITCENT,  ITCAPAENTRABEM.VRTAXDEPITCENT,
								   ITCAPAENTRABEM.DTINIDEPITCENT,         ITCAPAENTRABEM.CDFILIALITCENT,  ITCAPAENTRABEM.CDOPERESPITCENT,
								   ITCAPAENTRABEM.CDFORNECEITCENT,        ITCAPAENTRABEM.CDSERIENFITCENT, ITCAPAENTRABEM.NRNOTAFISITCENT,
								   ITCAPAENTRABEM.DSLOCALITCENT,          ITCAPAENTRABEM.CDPRODUTO,       ITCAPAENTRABEM.CDREGIMEBEM,
								   ITCAPAENTRABEM.CDSETORITCENT,          ITCAPAENTRABEM.DSOBSITCAPAENT,  ITCAPAENTRABEM.IDNOVOUSAITCENT,
								   ITCAPAENTRABEM.QTMESUSOITCENT,         ITCAPAENTRABEM.VRJADEPITCENT,   ITCAPAENTRABEM.QTMESDEPITCENFI,
								   ITCAPAENTRABEM.VRTAXDEPITCENFI,        ITCAPAENTRABEM.VRINIDEPITCENFI, ITCAPAENTRABEM.VRJADEPITCENFI,
								   ITCAPAENTRABEM.VRAQUIITCENFI,          ITCAPAENTRABEM.DTINIDEPITCENFI, ITCAPAENTRABEM.CDFILIALNF,
								   ITCAPAENTRABEM.NRLANCTONF, 	          GB.IDIDENTIFBEM
							  FROM ITCAPAENTRABEM, GRUPOBEM GB
							 WHERE ITCAPAENTRABEM.CDEMPRESA       = P_CDEMPRESA
							   AND ITCAPAENTRABEM.NRSEQCAPAENTBEM = P_NRSEQCAPAENTBEM
							   AND ITCAPAENTRABEM.IDBEMITCAPAENT  = 3
							   AND ITCAPAENTRABEM.CDGRUPOITCENT   = GB.CDGRUPOBEM
							   AND ITCAPAENTRABEM.CDEMPRESA       = GB.CDEMPRESA)
		LOOP
			--adqGerarQtBem
			SELECT NRSEQCAPAENTBEM, NRSEQITCAPAENT, NRBEMQTITCAPAEN, QTBEMQTITCAPAEN
			  INTO V_NRSEQCAPAENTBEM, V_NRSEQITCAPAENT, V_NRBEMQTITCAPAEN, V_QTBEMQTITCAPAEN
			  FROM QTITCAPAENTRABE
			 WHERE P_CDEMPRESA 		 			= CDEMPRESA
			   AND P_NRSEQCAPAENTBEM 			= NRSEQCAPAENTBEM
			   AND C_PROCESSAR.NRSEQITCAPAENT	= NRSEQITCAPAENT;
			
			--adqGerarQtBem
			FOR C_GERARQTBEM IN (SELECT CDEMPRESA, NRSEQCAPAENTBEM, NRSEQITCAPAENT, NRBEMQTITCAPAEN, QTBEMQTITCAPAEN
								   FROM QTITCAPAENTRABE
								  WHERE P_CDEMPRESA 				= CDEMPRESA
									AND P_NRSEQCAPAENTBEM			= NRSEQCAPAENTBEM
									AND C_PROCESSAR.NRSEQITCAPAENT 	= NRSEQITCAPAENT
								  ORDER BY NRSEQQTITCAPAEN)
			LOOP
				IF (C_GERARQTBEM.NRSEQCAPAENTBEM = C_PROCESSAR.NRSEQCAPAENTBEM) THEN
					V_GRUPO := C_GERARQTBEM.NRBEMQTITCAPAEN;
					V_QTDE  := C_GERARQTBEM.QTBEMQTITCAPAEN;
					
					IF ((P_GERABEM = 'S') AND (V_NRSEQITEM <> C_GERARQTBEM.NRSEQITCAPAENT)) THEN
						V_NUMBEM := 1;
						V_NRSEQITEM := C_GERARQTBEM.NRSEQITCAPAENT;
					END IF;
					
					V_TESTANUM := VERIFICA_NUMERICO(C_PROCESSAR.CDPATITCAPAENT);
					V_CONT2 := 1;
					
					WHILE (V_CONT2 <= V_GRUPO) LOOP
						NOVO_CODIGO('BEMATIVO'||P_CDEMPRESA, V_CDBEMATIVO, 1, P_NRORG);
						V_CDBEMATIVO := SUBSTR(V_CDBEMATIVO, 11);
						
						IF ((C_PROCESSAR.CDPATITCAPAENT IS NULL) OR (C_PROCESSAR.CDPATITCAPAENT = '')) THEN
							V_CDPATRIBEMATIVO := V_CDBEMATIVO;
						ELSE
							IF ((V_NUMBEM = 1) AND (P_GERABEM = 'S') AND (V_TESTANUM = 1)) THEN
								V_CDPATRIBEMATIVO := C_PROCESSAR.CDPATITCAPAENT;
							ELSE
								IF ((V_NUMBEM > 1) AND (P_GERABEM = 'S') AND (V_TESTANUM = 1)) THEN
									IF (V_CODPATRI = '0') THEN
										V_RECEBE_CODPATRI := TO_CHAR(TO_NUMBER(C_PROCESSAR.CDPATITCAPAENT) + 1);
										V_CODPATRI := V_RECEBE_CODPATRI;
									ELSE
										V_RECEBE_CODPATRI := TO_CHAR(TO_NUMBER(V_CODPATRI) + 1);
										V_CODPATRI := V_RECEBE_CODPATRI;
									END IF;
									V_CDPATRIBEMATIVO := LPAD(V_RECEBE_CODPATRI, LENGTH(C_PROCESSAR.CDPATITCAPAENT));
								ELSE
									V_CDPATRIBEMATIVO := C_PROCESSAR.CDPATITCAPAENT;
								END IF;
							END IF;
						END IF;
						
						IF (C_PROCESSAR.SGUNIDADE IS NULL) THEN
							V_SGUNIDADE := 'UN';
						ELSE
							V_SGUNIDADE := C_PROCESSAR.SGUNIDADE;
						END IF;
						
						IF (C_GERARQTBEM.NRBEMQTITCAPAEN > 1) THEN
							V_VRINIDEPITCENT := ROUND((C_PROCESSAR.VRINIDEPITCENT / C_GERARQTBEM.NRBEMQTITCAPAEN), 2);
							V_VRINICIAL := ROUND((C_PROCESSAR.VRINIDEPITCENT / C_GERARQTBEM.NRBEMQTITCAPAEN), 2);
							
							V_VRINIDEPRECFIS := ROUND((C_PROCESSAR.VRINIDEPITCENFI / C_GERARQTBEM.NRBEMQTITCAPAEN), 2);
							V_VRINICIALFIS := ROUND((C_PROCESSAR.VRINIDEPITCENFI / C_GERARQTBEM.NRBEMQTITCAPAEN), 2);
							V_VRAQUIBEMFISC := ROUND((C_PROCESSAR.VRAQUIITCENFI / C_GERARQTBEM.NRBEMQTITCAPAEN), 2);
							
							V_VRAQUIITCAPAENT := ROUND((C_PROCESSAR.VRINIDEPITCENT / C_GERARQTBEM.NRBEMQTITCAPAEN), 2);
						ELSE
							V_VRINIDEPITCENT := C_PROCESSAR.VRINIDEPITCENT;
							V_VRINICIAL := C_PROCESSAR.VRINIDEPITCENT;
							
							V_VRINIDEPRECFIS := C_PROCESSAR.VRINIDEPITCENFI;
							V_VRAQUIBEMFISC := C_PROCESSAR.VRAQUIITCENFI;
							V_VRINICIALFIS := ROUND((C_PROCESSAR.VRINIDEPITCENFI / C_GERARQTBEM.NRBEMQTITCAPAEN), 2);
							
							V_VRAQUIITCAPAENT := C_PROCESSAR.VRAQUIITCAPAENT;
						END IF;
						
						IF (C_PROCESSAR.QTMESDEPITCENT <> '') THEN
							V_QTMESDEPITCENT := C_PROCESSAR.QTMESDEPITCENT;
						ELSE
							V_QTMESDEPITCENT := 0;
						END IF;
						
						IF (C_PROCESSAR.QTMESDEPITCENT <> 0) THEN
							V_QTMESJADEPBEM := ROUND(C_PROCESSAR.VRJADEPITCENT / (V_VRINICIAL / C_PROCESSAR.QTMESDEPITCENT), 2);
							V_QTMESJADEP := (C_PROCESSAR.VRJADEPITCENT / (V_VRINICIAL / C_PROCESSAR.QTMESDEPITCENT));
						ELSE
							V_QTMESJADEPBEM := '0';
						END IF;
						
						V_QTMESFALTADBEM := C_PROCESSAR.QTMESDEPITCENT - V_QTMESJADEP;
						V_VRFALTADEPBEM := (V_VRINICIAL - C_PROCESSAR.VRJADEPITCENT);
						
						V_QTMESDEPBEMFISC := ROUND(C_PROCESSAR.QTMESDEPITCENFI, 2);
						
						V_VRTXANODEPBEMFI := ROUND(C_PROCESSAR.VRTAXDEPITCENFI, 2);
						
						IF (C_PROCESSAR.QTMESDEPITCENFI <> 0) THEN
							V_QTMESJADEPBEMFI := ROUND((C_PROCESSAR.VRJADEPITCENFI / (C_PROCESSAR.VRINIDEPITCENFI / C_PROCESSAR.QTMESDEPITCENFI)), 2);
							V_QTMESJADEPFIS := V_QTMESJADEPBEMFI;
						ELSE
							V_QTMESJADEPBEMFI := 0;
						END IF;
						
						V_QTMESFALTDBEMFIS := ROUND((C_PROCESSAR.QTMESDEPITCENFI - V_QTMESJADEPFIS), 2);
						V_VRFALTDEPBEMFIS := ROUND((V_VRINICIALFIS - C_PROCESSAR.VRJADEPITCENFI), 2);
						
						IF (C_PROCESSAR.QTMESUSOITCENT IS NULL) THEN
							V_QTMESUSOITCENT := '0';
						ELSE
							V_QTMESUSOITCENT := C_PROCESSAR.QTMESUSOITCENT;
						END IF;
						
						--acmTransfAtivo
						INSERT INTO BEMATIVO(
									CDEMPRESA,       							CDBEMATIVO,      				NMBEMATIVO,
									NMABREVBEMATIVO, 							CDPATRIBEMATIVO, 				DTAQUIBEMATIVO,
									QTORIGBEMATIVO,  							QTATUABEMATIVO,  				SGUNIDADE,
									VRAQUIBEMATIVO,  							CDCENTCUST,      				CDGRUPOBEM,
									CDTIPODEPREC,    							QTMESDEPBEM,     				VRINIDEPBEM,
									IDNOVOUSABEM,    							QTMESUSOBEM,     				VRTAXANODEPBEM,
									DTINIDEPRECBEM,  							CDFILIAL,        				CDOPERADOR,
									QTMESJADEPBEM,								VRJADEPBEMATIVO, 				QTMESFALTADBEM,
									VRFALTADEPBEM,   							CDOPERATUBEM,
									DTINCBEMATIVO,   							CDOPERINCBEM,    				IDTIPOBEMATIVO,
									CDFORNECED,      							CDSERIEENTRBEM,  				NRNOTAFISCBEM,
									DSLOCALBEMATIVO, 							CDPRODUTO,       				CDREGIMEBEM,
									CDSETOR,         							DSOBSBEMATIVO,   				NRSEQCAPAENTBEM,
									NRSEQITCAPAENT,  							QTMESDEPBEMFISC, 				VRTXANODEPBEMFI,
									VRINIDEPRECFIS,  							VRJADEPBEMFISC,  				VRAQUIBEMFISC,
									DTINIDEPRECFIS,  							QTMESJADEPBEMFI, 				QTMESFALTDBEMFIS,
									VRFALTDEPBEMFIS,  							NRLANCTONF,      			CDFILIALNF,
									IDIDENTBEMPISCOF, 							DTINCLUSAO,						NRORGINCLUSAO,
									CDOPERINCLUSAO,								IDATIVO)
							 VALUES (C_PROCESSAR.CDEMPRESA,						    V_CDBEMATIVO,				          C_PROCESSAR.NMBEMITCAPAENT,
									SUBSTR(C_PROCESSAR.NMBEMITCAPAENT,1,15),  V_CDPATRIBEMATIVO,	          C_PROCESSAR.DTAQUIITCAPAENT,
									V_QTDE,										                V_QTDE,					              V_SGUNIDADE,
									V_VRAQUIITCAPAENT, 							          C_PROCESSAR.CDCENTCUITCENT,   C_PROCESSAR.CDGRUPOITCENT,
									C_PROCESSAR.CDTIPODEPREC,					        V_QTMESDEPITCENT,				      V_VRINIDEPITCENT,
									C_PROCESSAR.IDNOVOUSAITCENT,				      V_QTMESUSOITCENT,				      C_PROCESSAR.VRTAXDEPITCENT,
									C_PROCESSAR.DTINIDEPITCENT,					      C_PROCESSAR.CDFILIALITCENT,	  C_PROCESSAR.CDOPERESPITCENT,
									V_QTMESJADEPBEM,							            C_PROCESSAR.VRJADEPITCENT,		V_QTMESFALTADBEM,
									V_VRFALTADEPBEM,							            P_CDOPERADOR,
									SYSDATE,									                P_CDOPERADOR,					        'R',
									C_PROCESSAR.CDFORNECEITCENT,				      C_PROCESSAR.CDSERIENFITCENT,	C_PROCESSAR.NRNOTAFISITCENT,
									C_PROCESSAR.DSLOCALITCENT,					      C_PROCESSAR.CDPRODUTO,			  C_PROCESSAR.CDREGIMEBEM,
									C_PROCESSAR.CDSETORITCENT,					      C_PROCESSAR.DSOBSITCAPAENT,		C_PROCESSAR.NRSEQCAPAENTBEM,
									C_PROCESSAR.NRSEQITCAPAENT,					      V_QTMESDEPBEMFISC,				    V_VRTXANODEPBEMFI,
									C_PROCESSAR.VRINIDEPITCENFI,				      C_PROCESSAR.VRJADEPITCENFI,		V_VRAQUIBEMFISC,
									C_PROCESSAR.DTINIDEPITCENFI,				      V_QTMESJADEPBEMFI,				    V_QTMESFALTDBEMFIS,
									V_VRFALTDEPBEMFIS,							          C_PROCESSAR.NRLANCTONF,			  C_PROCESSAR.CDFILIALNF,
									C_PROCESSAR.IDIDENTIFBEM,					        SYSDATE,						          P_NRORG,
									P_CDOPERADOR,								'S');
						
						--adqNFRateio
						SELECT NRSEQITPROD 
						  INTO V_NRSEQITPROD
						  FROM ITEMPRODUTO
						 WHERE (CDFILIAL    = C_PROCESSAR.CDFILIALNF)
						   AND (NRLANCTONF  = C_PROCESSAR.NRLANCTONF)
						   AND (CDPRODUTO   = C_PROCESSAR.CDPRODUTO);
						
						
						--adqSumRat
					   SELECT SUM(ROUND(((R.VRRATENF * 100) / RA.VRRATENF), 2)) AS SUMRAT
						 INTO V_SUMRAT
						 FROM RATEIONF R, FILIAL F, CENTCUST C, (SELECT SUM(VRRATENF) AS VRRATENF
																   FROM RATEIONF
																  WHERE (CDFILIAL    = C_PROCESSAR.CDFILIALNF) 
																	AND (NRLANCTONF  = C_PROCESSAR.NRLANCTONF)  
																	AND(NRSEQITPROD  = V_NRSEQITPROD)) RA
						WHERE (R.CDFILIAL    = C_PROCESSAR.CDFILIALNF)    
						  AND (R.NRLANCTONF  = C_PROCESSAR.NRLANCTONF)  
						  AND (R.NRSEQITPROD = V_NRSEQITPROD) 
						  AND (R.CDFILICCUST = F.CDFILIAL)     
						  AND (R.CDCENTCUSNF = C.CDCENTCUST)
						ORDER BY R.CDFILICCUST, R.CDCENTCUSNF;
						
						
						SELECT COUNT(*) 
						  INTO V_COUNTRATEIO
						  FROM (SELECT R.CDFILIAL, R.NRLANCTONF, R.NRSEQITPROD, R.CDFILICCUST, F.NMFILIAL,
												   R.CDCENTCUSNF, C.NMCENTCUST, ROUND(((R.VRRATENF * 100) / RA.VRRATENF), 2) AS VRRATENF
											  FROM RATEIONF R, FILIAL F, CENTCUST C, (SELECT SUM(VRRATENF) AS VRRATENF 
																					   FROM RATEIONF
																					  WHERE (CDFILIAL  = C_PROCESSAR.CDFILIALNF) 
																						AND	(NRLANCTONF  = C_PROCESSAR.NRLANCTONF) 
																						AND	(NRSEQITPROD = V_NRSEQITPROD)) RA
											WHERE (R.CDFILIAL    = C_PROCESSAR.CDFILIALNF) 
											  AND (R.NRLANCTONF  = C_PROCESSAR.NRLANCTONF) 
											  AND (R.NRSEQITPROD = V_NRSEQITPROD) 
											  AND (R.CDFILICCUST = F.CDFILIAL) 
											  AND (R.CDCENTCUSNF = C.CDCENTCUST)
											ORDER BY R.CDFILICCUST, R.CDCENTCUSNF);
											
						IF (V_COUNTRATEIO > 1) THEN
							V_REGRAT := 1;
							V_CONTRAT := 100 - V_SUMRAT;
							V_CONTRAT2 := 100;
							--adqRateioBem
							FOR C_RATEIOBEM IN (SELECT R.CDFILIAL, R.NRLANCTONF, R.NRSEQITPROD, R.CDFILICCUST, F.NMFILIAL,
													   R.CDCENTCUSNF, C.NMCENTCUST, ROUND(((R.VRRATENF * 100) / RA.VRRATENF), 2) AS VRRATENF
												  FROM RATEIONF R, FILIAL F, CENTCUST C, (SELECT SUM(VRRATENF) AS VRRATENF 
																						   FROM RATEIONF
																						  WHERE (CDFILIAL  = C_PROCESSAR.CDFILIALNF) 
																							AND	(NRLANCTONF  = C_PROCESSAR.NRLANCTONF) 
																							AND	(NRSEQITPROD = V_NRSEQITPROD)) RA
												WHERE (R.CDFILIAL    = C_PROCESSAR.CDFILIALNF) 
												  AND (R.NRLANCTONF  = C_PROCESSAR.NRLANCTONF) 
												  AND (R.NRSEQITPROD = V_NRSEQITPROD) 
												  AND (R.CDFILICCUST = F.CDFILIAL) 
												  AND (R.CDCENTCUSNF = C.CDCENTCUST)
												ORDER BY R.CDFILICCUST, R.CDCENTCUSNF)
							LOOP
								IF ((V_REGRAT = V_COUNTRATEIO) AND (V_CONTRAT <> 0)) THEN
									V_VRRATEIO := V_CONTRAT2;
								ELSE
									V_VRRATEIO := C_RATEIOBEM.VRRATENF;
								END IF;
								--acmRateioBem
								INSERT INTO RATEIOBEM  (CDEMPRESA,			CDBEMATIVO, 
										CDFILICCUST,		CDCENTCUSBEM, 
										VRRATEBEM,			NRORG,
										DTINCLUSAO,			NRORGINCLUSAO,
										CDOPERINCLUSAO,	IDATIVO)
								VALUES (P_CDEMPRESA,				      V_CDBEMATIVO, 
										C_RATEIOBEM.CDFILICCUST,	C_RATEIOBEM.CDCENTCUSNF, 
										V_VRRATEIO,					      P_NRORG,
										SYSDATE,					        P_NRORG,
										P_CDOPERADOR,				      'S');
								V_CONTRAT2 := V_CONTRAT2 - C_RATEIOBEM.VRRATENF;
								V_REGRAT := V_REGRAT + 1;
							END LOOP;
						END IF;
						V_CONT2 := V_CONT2 + 1;
						V_NUMBEM := V_NUMBEM + 1;
					END LOOP;
				END IF;
			END LOOP;
			--acmUpdStatusBemGerado
			UPDATE ITCAPAENTRABEM
			   SET IDBEMITCAPAENT  = '5'
			 WHERE CDEMPRESA       = C_PROCESSAR.CDEMPRESA
			   AND NRSEQCAPAENTBEM = C_PROCESSAR.NRSEQCAPAENTBEM
			   AND NRSEQITCAPAENT  = C_PROCESSAR.NRSEQITCAPAENT;
			
			V_CONT := V_CONT + 1;
		END LOOP;
	END IF;
	
	IF (V_CONT = 0) THEN
		P_RETORNO := 'Não existe(m) item(ns) com status "Verificado OK" para ser(em) transformado(s) em ativo.';
	ELSE
		--adqTrasfDescart_1
		SELECT COUNT(NRSEQITCAPAENT) AS ITCAPA
		  INTO V_TRASFDESCART
		  FROM ITCAPAENTRABEM
		 WHERE IDBEMITCAPAENT 	 IN ('4','5')
		   AND P_CDEMPRESA 		 = CDEMPRESA
		   AND P_NRSEQCAPAENTBEM = NRSEQCAPAENTBEM;
		   
		--adqTrasfDescart_2
		SELECT COUNT(NRSEQITCAPAENT) AS ITCAPA
		  INTO V_TODOSITCAPA
		  FROM ITCAPAENTRABEM
		 WHERE P_CDEMPRESA 		 = CDEMPRESA
		   AND P_NRSEQCAPAENTBEM = NRSEQCAPAENTBEM;
		
		IF (V_TRASFDESCART = V_TODOSITCAPA) THEN
			--acmUpdCapaOperProcessado
			UPDATE CAPAENTRADABEM
			  SET IDSITCAPAENTBEM = '2'
			WHERE P_CDEMPRESA 		= CDEMPRESA
			  AND P_NRSEQCAPAENTBEM = NRSEQCAPAENTBEM;
			  
			P_RETORNO := 'SUCESSO';
		END IF;
	END IF;
END;
/
