IF EXISTS(SELECT name FROM sysobjects
          WHERE name = 'CONTABILIZA_BCO_TRANSCORR' AND type = 'P')
DROP PROCEDURE CONTABILIZA_BCO_TRANSCORR
GO

CREATE PROCEDURE CONTABILIZA_BCO_TRANSCORR @P_NRLISTA VARCHAR(10) 
AS
  DECLARE @V_CDFILIRAT  VARCHAR(4);
  DECLARE @V_STCONTCTBL VARCHAR(20);
  DECLARE @V_DSHISTPADR VARCHAR(1000);
  DECLARE @V_CDLOTE     VARCHAR(20);
  DECLARE @V_CDCENTCUST VARCHAR(04);
  DECLARE @V_NRLANCLOTE VARCHAR(06);
  DECLARE @V_CDFILIAL   VARCHAR(04);
  DECLARE @V_CDHISTPADR VARCHAR(04);
  
  DECLARE @V_CONT      INTEGER;
  DECLARE @V_COUNT     INTEGER;
  DECLARE @V_INPARTIDA INTEGER;
 
  DECLARE @V_RATEIO FLOAT;
  DECLARE @DOVRRAT  FLOAT;
  
  DECLARE @MULTPLI FLOAT;
  DECLARE @SOMA    FLOAT;

  --VARIAVEIS DO C_LISTA  
  DECLARE @CDEMPRESA VARCHAR(2);
  DECLARE @NRTRANSF VARCHAR(50);
  
  --VARIAVEIS DO C_BCO_TRANSCOOR
  DECLARE @CDEMPRESA_T VARCHAR(2);
  DECLARE @NRTRANBANC  VARCHAR(10);
  DECLARE @CDREDUCONT  VARCHAR(4);
  DECLARE @NRMOVBANC   VARCHAR(10);
  DECLARE @IDMOVITBANC VARCHAR(1);
  DECLARE @VRMOVITBANC FLOAT;
  DECLARE @NRCHEQUE VARCHAR(6);
  DECLARE @CDBANCO VARCHAR(3);
  DECLARE @CDAGENCIA VARCHAR(5);
  DECLARE @CDCONTCORR VARCHAR(10);
  DECLARE @DSCONTCORR VARCHAR(20);
  DECLARE @CDPICTCONT VARCHAR(20);
  DECLARE @CDCONTCTBL VARCHAR(20);
  DECLARE @NRLCTBTRAN VARCHAR(12);
  DECLARE @DSHISTTBANC VARCHAR(40);
  DECLARE @DTTRANBANC DATETIME;

BEGIN
   EXEC EXCLUI_LANCCONTABIL '09', @P_NRLISTA;
   DECLARE C_LISTA CURSOR FOR SELECT SUBSTRING(LI.CDLISTA,1,2) AS CDEMPRESA, SUBSTRING(LI.CDLISTA,3,10) AS NRTRANSF 
                                FROM LISTA LI
                               WHERE LI.NRLISTA = @P_NRLISTA; 
   OPEN C_LISTA;
   
   FETCH NEXT FROM C_LISTA INTO @CDEMPRESA, @NRTRANSF;

   WHILE (@@FETCH_STATUS = 0)
   BEGIN                                                          
      DECLARE C_BCO_TRANSCOOR CURSOR FOR SELECT I.CDEMPRESA, I.NRTRANBANC, I.CDREDUCONT, I.NRMOVBANC, I.IDMOVITBANC, I.VRMOVITBANC,
                                                I.NRCHEQUE, C.CDBANCO, C.CDAGENCIA, C.CDCONTCORR, C.DSCONTCORR, B.CDPICTCONT, C.CDCONTCTBL,
                                                T.NRLCTBTRAN, T.DSHISTTBANC, T.DTTRANBANC
                                           FROM ITTRANSFBANC I, CONTCORR C, BANCO B, TRANSFBANC T, LISTA LI
                                          WHERE (T.CDEMPRESA  = I.CDEMPRESA)
                                            AND (T.NRTRANBANC = I.NRTRANBANC)
                                            AND (I.CDREDUCONT = C.CDREDUCONT)
                                            AND (C.CDBANCO    = B.CDBANCO)
                                            AND (I.CDEMPRESA  = @CDEMPRESA)
                                            AND (I.NRTRANBANC = @NRTRANSF);
              
      OPEN C_BCO_TRANSCOOR;
      
      FETCH NEXT FROM C_BCO_TRANSCOOR INTO @CDEMPRESA_T, @NRTRANBANC, @CDREDUCONT, @NRMOVBANC, @IDMOVITBANC,
                                           @VRMOVITBANC, @NRCHEQUE, @CDBANCO, @CDAGENCIA, @CDCONTCORR, @DSCONTCORR,
                                           @CDPICTCONT, @CDCONTCTBL, @NRLCTBTRAN, @DSHISTTBANC, @DTTRANBANC;
      WHILE (@@FETCH_STATUS = 0)
      BEGIN
         SELECT V_CDHISTPADR = CDHISTPADR
           FROM HISTINTEGRA
          WHERE CDOPERACAO = 'TRANSF';
          
         EXEC MONTA_HISTORICO @CDREDUCONT, @NRMOVBANC, @V_CDHISTPADR,'','','09','', @V_DSHISTPADR;
         
         IF @IDMOVITBANC = 'C'
         BEGIN
            SET @MULTPLI = @VRMOVITBANC * -1;
            EXEC NOVA_PARTIDA @CDEMPRESA_T, @V_CDLOTE, @DTTRANBANC, @V_NRLANCLOTE, @CDCONTCTBL, @V_CDHISTPADR,
                              @V_DSHISTPADR, @MULTPLI, 'B', @CDREDUCONT, '09', 'N';             
         END
         ELSE
         BEGIN  
            EXEC  NOVA_PARTIDA @CDEMPRESA_T, @V_CDLOTE, @DTTRANBANC, @V_NRLANCLOTE, @V_STCONTCTBL, @V_CDHISTPADR,
                               @V_DSHISTPADR, @VRMOVITBANC, 'B', @CDREDUCONT, '09', 'N';  
         END;
          
         FETCH NEXT FROM C_BCO_TRANSCOOR INTO @CDEMPRESA_T, @NRTRANBANC, @CDREDUCONT, @NRMOVBANC, @IDMOVITBANC,
                                              @VRMOVITBANC, @NRCHEQUE, @CDBANCO, @CDAGENCIA, @CDCONTCORR, @DSCONTCORR,
                                              @CDPICTCONT, @CDCONTCTBL, @NRLCTBTRAN, @DSHISTTBANC, @DTTRANBANC;      
      END;
      CLOSE C_BCO_TRANSCOOR;
      DEALLOCATE C_BCO_TRANSCOOR;          

      FETCH NEXT FROM C_LISTA INTO @CDEMPRESA, @NRTRANSF;
   END;
   CLOSE C_LISTA;
   DEALLOCATE C_LISTA;      
END;         
GO