CREATE OR REPLACE PROCEDURE CONTABILIZA_BCO_TRANSCORR(
  P_NRLISTA IN LISTA.NRLISTA%TYPE)
IS
  V_CDFILIRAT VARCHAR2(4);
  V_DSHISTPADR VARCHAR2(1000);
  V_CDLOTE VARCHAR2(20);
  V_CDCENTCUST VARCHAR2(04);
  V_NRLANCLOTE VARCHAR2(06);
  V_CDFILIAL VARCHAR2(04);
  V_CDHISTPADR VARCHAR2(04);

  V_CONT INTEGER;
  V_COUNT INTEGER;
  V_INPARTIDA INTEGER;

  V_RATEIO FLOAT;
  V_GERALOTE VARCHAR2(01);
  DOVRRAT FLOAT;
  V_DTLANCAMENTO DATE;
BEGIN
   EXCLUI_LANCCONTABIL('09',P_NRLISTA);
   FOR C_LISTA IN (
      SELECT SUBSTR(LI.CDLISTA,1,2) AS CDEMPRESA, SUBSTR(LI.CDLISTA,3,10) NRTRANSF
        FROM LISTA LI
       WHERE LI.NRLISTA = P_NRLISTA) LOOP

        V_GERALOTE := 'S';

        FOR C_BCO_TRANSCOOR IN(
           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
            WHERE (T.CDEMPRESA  = I.CDEMPRESA)
              AND (T.NRTRANBANC = I.NRTRANBANC)
              AND (I.CDREDUCONT = C.CDREDUCONT)
              AND (C.CDBANCO    = B.CDBANCO)
              AND (I.CDEMPRESA  = C_LISTA.CDEMPRESA)
              AND (I.NRTRANBANC = C_LISTA.NRTRANSF)) LOOP

              BEGIN
                SELECT CDHISTPADR
                  INTO V_CDHISTPADR
                  FROM HISTINTEGRA
                 WHERE CDOPERACAO= 'TRANSF';
              EXCEPTION
                WHEN NO_DATA_FOUND THEN
                 V_CDHISTPADR := NULL;
              END;

              IF V_GERALOTE = 'S' THEN
                 GERA_LOTECONTABIL(C_LISTA.CDEMPRESA,'09','','',C_BCO_TRANSCOOR.DTTRANBANC,'','',V_CDLOTE,V_NRLANCLOTE);
                 V_GERALOTE := 'N';
                 V_DTLANCAMENTO := C_BCO_TRANSCOOR.DTTRANBANC;
              END IF;

              MONTA_HISTORICO(C_BCO_TRANSCOOR.CDREDUCONT,C_BCO_TRANSCOOR.NRMOVBANC,V_CDHISTPADR,'','','09','',V_DSHISTPADR);

              IF C_BCO_TRANSCOOR.IDMOVITBANC = 'C' THEN
                 NOVA_PARTIDA(C_BCO_TRANSCOOR.CDEMPRESA,V_CDLOTE,C_BCO_TRANSCOOR.DTTRANBANC,V_NRLANCLOTE, C_BCO_TRANSCOOR.CDCONTCTBL, V_CDHISTPADR,
                              V_DSHISTPADR, C_BCO_TRANSCOOR.VRMOVITBANC * -1 , 'B', C_BCO_TRANSCOOR.CDREDUCONT,'09', C_BCO_TRANSCOOR.NRTRANBANC, 'N');
              ELSE
                 NOVA_PARTIDA(C_BCO_TRANSCOOR.CDEMPRESA,V_CDLOTE,C_BCO_TRANSCOOR.DTTRANBANC,V_NRLANCLOTE, C_BCO_TRANSCOOR.CDCONTCTBL, V_CDHISTPADR,
                              V_DSHISTPADR, C_BCO_TRANSCOOR.VRMOVITBANC , 'B', C_BCO_TRANSCOOR.CDREDUCONT, '09',C_BCO_TRANSCOOR.NRTRANBANC, 'N');
              END IF;
        END LOOP;

           INSERT INTO RELACTRANSFLANC
             (CDEMPRESA, CDLOTE, NRLANCLOTE, NRSEQLANLOTE, NRTRANBANC)
           VALUES
             (C_LISTA.CDEMPRESA, V_CDLOTE, V_NRLANCLOTE, '00001', C_LISTA.NRTRANSF);

           IF CONFERE_PARTIDA(C_LISTA.CDEMPRESA, V_CDLOTE, V_NRLANCLOTE, V_DTLANCAMENTO) THEN
              UPDATE TRANSFBANC SET IDSTATUSCTBL = 'C'
               WHERE CDEMPRESA = C_LISTA.CDEMPRESA
                 AND NRTRANBANC = C_LISTA.NRTRANSF;
           ELSE
              UPDATE TRANSFBANC SET IDSTATUSCTBL = 'C'
               WHERE CDEMPRESA = C_LISTA.CDEMPRESA
                 AND NRTRANBANC = C_LISTA.NRTRANSF;
           END IF;
   END LOOP;
   DELETE FROM LISTA
    WHERE NRLISTA = P_NRLISTA;
END;
/