IF OBJECT_ID('P_EXP_CTB_MOV_PG_CRED_BANCARIO_JUROS') IS NOT NULL
	BEGIN
		DROP PROCEDURE P_EXP_CTB_MOV_PG_CRED_BANCARIO_JUROS
	END
 
GO 
CREATE PROCEDURE P_EXP_CTB_MOV_PG_CRED_BANCARIO_JUROS(@CD_EMP			INT,
									@CD_FILIAL		INT,
									@DT_INI			DATETIME,
									@DT_FIM			DATETIME,
									@CD_BC_LANC		INT)	
WITH ENCRYPTION AS 

DECLARE @RS_CD_EMP					INT
DECLARE @RS_CD_FILIAL				INT
DECLARE @RS_CD_FORN					INT
DECLARE @RS_HIST					VARCHAR(500)
DECLARE @RS_CD_CNT					INT
DECLARE @RS_NR_CHQ					INT
DECLARE @RS_SERIE_CHQ				VARCHAR(5)
DECLARE @RS_ANO_CHQ					INT
DECLARE @RS_CD_PG_DEB_LOTE			INT
DECLARE @RS_VLR_EMIT_DEST			MONEY
DECLARE @RS_VL_JUR					MONEY
DECLARE @RS_VL_DESC					MONEY
DECLARE @RS_VL_IMP					MONEY
DECLARE @RS_VLR_DESP_OUTR			MONEY
DECLARE @RS_VLR_DEVOLUCAO			MONEY
DECLARE @RS_RZ_FORN					VARCHAR(200)
DECLARE @RS_NR_TIT					VARCHAR(50)	
DECLARE @RS_NR_NF					INT 
DECLARE @RS_DT_PAG					DATETIME
DECLARE @RS_VLR_CONTA				MONEY
DECLARE @RS_QTDE_LANC				INT
	
DECLARE @RET_CD_CTB_CNT				VARCHAR(100)
DECLARE @RET_CD_CTB_CNT_RED			INT
DECLARE @RET_DS_CNT					VARCHAR(100)
DECLARE @RET_NR_CNT					VARCHAR(50)	
DECLARE @RET_TITULAR_CNT			VARCHAR(200)
DECLARE @RET_TP_CNT					INT
	
DECLARE @HIST						VARCHAR(200)	


	--#####################################################################
	--CURSOR JUROS
	--#####################################################################			
	DECLARE RS_RESULT_BANCARIO_JUROS
		CURSOR FOR 
			SELECT 	
				CD_EMP,
				CD_FILIAL,
				DT_PAG,
				ROUND(SUM(VL_JUR), 2) 			AS VL_JUR					
			FROM UFD_EXP_CTB_MOV_PG_CRED(@CD_EMP, @CD_FILIAL, @DT_INI, @DT_FIM) 
			WHERE 
				VL_JUR > 0
				AND CD_BC_LANC =@CD_BC_LANC 
			GROUP BY 
				CD_EMP,
				CD_FILIAL,										
				DT_PAG
				
	--Abrindo cursor
	OPEN RS_RESULT_BANCARIO_JUROS
	
	--Atribuindo valores do select nas variáveis
	FETCH NEXT FROM RS_RESULT_BANCARIO_JUROS INTO 
		@RS_CD_EMP,
		@RS_CD_FILIAL,
		@RS_DT_PAG,
		@RS_VL_JUR
	
	--Iniciando laço
	WHILE @@FETCH_STATUS = 0		    
		BEGIN				
			INSERT INTO ##CTB_EXP_MOV  
				SELECT		@RS_CD_EMP										AS CD_EMP,
							@CD_FILIAL										AS CD_FILIAL_EXPORT,
							@RS_CD_FILIAL									AS CD_FILIAL,
							16												AS CD_MOVIMENTO,
							'PAGAMENTO FORNECEDOR'							AS MOVIMENTO,
							''												AS OPERACAO,
							'JUROS S/ DUPL. PAGA EM ATRASO - DEBITO'		AS OPERACAO_DEBITO,
							0												AS CD_CNT_RED_CREDITO,
							''												AS CD_CNT_CREDITO,																
							0												AS CD_CNT_RED_DEBITO,
							''												AS CD_CNT_DEBITO,
							@RS_DT_PAG										AS DT_MOV,
							'JUROS S/ DUPL. PAGA EM ATRASO' 				AS HIST,								
							@RS_VL_JUR										AS VLR_LANC,
							'D'												AS TP_TRANSA,
							0 												AS RATEIO_CENTRO_CUSTO,
							'' 												AS DS_CENTRO_CUSTO,
							0 												AS CNT_CENTRO_CUSTO,
							0 												AS VLR_CENTRO_CUSTO,
							'FILIAIS'									    AS ORIG_CONTA,
							16 									    		AS COD_CONTA,
							OBJECT_NAME(@@PROCID)							AS NM_PROCEDURE
							 , 'V' AS TIPO_LANCTO
							 , 0 AS CD_EVENTO
							 , 0  AS NR_DP
							 , 0  AS ANO
							 , '' AS NR_DOC
							 , 0 AS CD_BC_LANC;

			--Atribuindo valores do select nas variáveis
			FETCH NEXT FROM RS_RESULT_BANCARIO_JUROS INTO 
				@RS_CD_EMP,
				@RS_CD_FILIAL,
				@RS_DT_PAG,
				@RS_VL_JUR
							
		END								
			
	CLOSE RS_RESULT_BANCARIO_JUROS
	DEALLOCATE RS_RESULT_BANCARIO_JUROS