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

  V_CONT INTEGER;
  V_COUNT INTEGER;
  V_INPARTIDA INTEGER;

  V_CDCONTCOME VARCHAR2(21);
  V_IDTIPOCOME VARCHAR2(01);

  V_RATEIO FLOAT;
  DOVRRAT FLOAT;
BEGIN
  EXCLUI_LANCCONTABIL('08',P_NRLISTA);

  FOR C_BCO_LANCCORR IN(
     SELECT M.DTMOVICRED AS DATA, C.DSTPCREDBANC AS TIPO, C.CDHISTPADR, C.CDCONTCTBL, M.CDTPCREDBANC AS CDTIPO,
            M.NRLCTBMCRED AS LANCCONT, M.DSMOVICRED, M.VRMOVICRED AS VALOR, M.NRMOVICRED AS NUMERO,
            M.CDEMPRESA, M.CDFILIAL, M.CDFILILANCRE AS CDFILILANCA, C.IDCTBLCRBANC AS IDCONTABILIZA,
            L.NMRAZSOCCLIE, L.NMFANTCLIE, O.NMRAZSOCFORN, O.NMFANTFORN, O.CDFORNECED, L.CDCLIENTE,
            -1 AS DEBCRED, M.CDREDUCONT AS CDREDUCONT, M.NRMOVBANC
       FROM TPCREDBANC C, MOVBANCARIO B, MOVCREDBANC M, FORNECEDOR O, CLIENTE L, LISTA LI
      WHERE (C.CDTPCREDBANC = M.CDTPCREDBANC)
        AND (M.NRMOVBANC    = B.NRMOVBANC)
        AND (B.CDREDUCONT   = M.CDREDUCONT)
        AND (M.CDREDUCONT   = SUBSTR(LI.CDLISTA,1,3))
        AND (M.NRMOVBANC   = SUBSTR(LI.CDLISTA,4,10))
        AND (M.CDCLIENTE    = L.CDCLIENTE(+))
        AND (M.CDFORNECED   = O.CDFORNECED(+))
        AND (LI.NRLISTA      = P_NRLISTA)
      UNION ALL
     SELECT M.DTMOVIDEBI AS DATA, C.DSTPDEBIBANC AS TIPO, C.CDHISTPADR, C.CDCONTCTBL, M.CDTPDEBIBANC AS CDTIPO,
            M.NRLCTBMDEB AS LANCCONT, M.DSMOVIDEBI, M.VRMOVIDEBI AS VALOR, M.NRMOVIDEBI AS NUMERO,
            M.CDEMPRESA, M.CDFILIAL, M.CDFILILANDEB AS CDFILILANCA, C.IDCTBLDBBANC AS IDCONTABILIZA,
            L.NMRAZSOCCLIE, L.NMFANTCLIE, O.NMRAZSOCFORN, O.NMFANTFORN, O.CDFORNECED, L.CDCLIENTE,
            1 AS DEBCRED, M.CDREDUCONT AS CDREDUCONT, M.NRMOVBANC
       FROM TPDEBIBANC C, MOVBANCARIO B, MOVDEBBANC  M, FORNECEDOR  O, CLIENTE L, LISTA LI
      WHERE (C.CDTPDEBIBANC = M.CDTPDEBIBANC)
        AND (M.NRMOVBANC    = B.NRMOVBANC)
        AND (B.CDREDUCONT   = M.CDREDUCONT(+))
        AND (M.CDREDUCONT   = SUBSTR(LI.CDLISTA,1,3))
        AND (M.NRMOVBANC   = SUBSTR(LI.CDLISTA,4,10))
        AND (M.CDCLIENTE    = L.CDCLIENTE(+))
        AND (M.CDFORNECED   = O.CDFORNECED(+))
        AND (LI.NRLISTA      = P_NRLISTA)
      ORDER BY 9) LOOP

        BEGIN
           SELECT C.CDCONTCTBL
             INTO V_STCONTCTBL
             FROM CONTCORR C
            WHERE (C.CDREDUCONT = NVL(C_BCO_LANCCORR.CDREDUCONT, C.CDREDUCONT))
            ORDER BY C.CDREDUCONT;
        EXCEPTION
           WHEN NO_DATA_FOUND THEN
            V_STCONTCTBL := NULL;
        END;

        IF C_BCO_LANCCORR.DEBCRED = 1 THEN
           GERA_LOTECONTABIL(C_BCO_LANCCORR.CDEMPRESA,'08','',C_BCO_LANCCORR.CDFILILANCA,C_BCO_LANCCORR.DATA,C_BCO_LANCCORR.CDTIPO,'D',V_CDLOTE,V_NRLANCLOTE);
        ELSE
           GERA_LOTECONTABIL(C_BCO_LANCCORR.CDEMPRESA,'08','',C_BCO_LANCCORR.CDFILILANCA,C_BCO_LANCCORR.DATA,C_BCO_LANCCORR.CDTIPO,'C',V_CDLOTE,V_NRLANCLOTE);
        END IF;

        MONTA_HISTORICO(C_BCO_LANCCORR.CDREDUCONT,C_BCO_LANCCORR.NRMOVBANC,C_BCO_LANCCORR.CDHISTPADR,'','','08','',V_DSHISTPADR);

        NOVA_PARTIDA(C_BCO_LANCCORR.CDEMPRESA,V_CDLOTE,C_BCO_LANCCORR.DATA,V_NRLANCLOTE, V_STCONTCTBL, C_BCO_LANCCORR.CDHISTPADR,
                     V_DSHISTPADR, C_BCO_LANCCORR.VALOR * C_BCO_LANCCORR.DEBCRED , 'B', C_BCO_LANCCORR.CDREDUCONT, '08', C_BCO_LANCCORR.CDREDUCONT || '/' || C_BCO_LANCCORR.NRMOVBANC, 'N');

        V_INPARTIDA := 1;

        BEGIN
           SELECT CDCENTCUST
             INTO V_CDCENTCUST
             FROM FILIAL
            WHERE CDFILIAL = C_BCO_LANCCORR.CDFILILANCA;
        EXCEPTION
           WHEN NO_DATA_FOUND THEN
            V_CDCENTCUST := NULL;
        END;

        IF V_CDCENTCUST IS NULL THEN
           BEGIN
              SELECT CDCENTCUST
                INTO V_CDCENTCUST
                FROM EMPRESA
               WHERE (CDEMPRESA = C_BCO_LANCCORR.CDEMPRESA);
           EXCEPTION
              WHEN NO_DATA_FOUND THEN
               V_CDCENTCUST := NULL;
           END;
        END IF;

        IF V_CDCENTCUST IS NULL THEN
           RATEIO_PARTIDA(C_BCO_LANCCORR.CDEMPRESA, V_CDLOTE, C_BCO_LANCCORR.DATA, V_NRLANCLOTE, C_BCO_LANCCORR.CDFILILANCA,
                          V_CDCENTCUST, C_BCO_LANCCORR.VALOR * C_BCO_LANCCORR.DEBCRED);
        END IF;

        IF C_BCO_LANCCORR.CDCLIENTE IS NOT NULL THEN
           V_CDCONTCOME := C_BCO_LANCCORR.CDCLIENTE;
           V_IDTIPOCOME := 'C';
        ELSIF C_BCO_LANCCORR.CDFORNECED IS NOT NULL THEN
           V_CDCONTCOME := C_BCO_LANCCORR.CDFORNECED;
           V_IDTIPOCOME := 'F';
        ELSE
           V_CDCONTCOME := C_BCO_LANCCORR.CDREDUCONT;
           V_IDTIPOCOME := 'B';
        END IF;


        NOVA_PARTIDA(C_BCO_LANCCORR.CDEMPRESA,V_CDLOTE,C_BCO_LANCCORR.DATA,V_NRLANCLOTE, C_BCO_LANCCORR.CDCONTCTBL, C_BCO_LANCCORR.CDHISTPADR,
                     V_DSHISTPADR, (C_BCO_LANCCORR.VALOR * C_BCO_LANCCORR.DEBCRED) * -1, V_IDTIPOCOME, V_CDCONTCOME, '08', C_BCO_LANCCORR.CDREDUCONT || '/' || C_BCO_LANCCORR.NRMOVBANC, 'N');

        BEGIN
           SELECT CDFILIAL
             INTO V_CDFILIAL
             FROM RATTPCRED
            WHERE CDTPCREDBANC = C_BCO_LANCCORR.CDTIPO;
        EXCEPTION
           WHEN NO_DATA_FOUND THEN
             V_CDFILIAL := NULL;
        END;

        BEGIN
           SELECT COUNT(*)
             INTO V_CONT
             FROM RATTPCRED
            WHERE CDTPCREDBANC = C_BCO_LANCCORR.CDTIPO;
        EXCEPTION
           WHEN NO_DATA_FOUND THEN
             V_CDFILIAL := NULL;
        END;

        DOVRRAT := 0;

        IF V_CDFILIAL IS NOT NULL THEN
           FOR C_RATTPCRED IN(
              SELECT CDTPCREDBANC, CDCENTCUST, CDFILIAL, VRPERATTPCRE
                FROM RATTPCRED
               WHERE CDTPCREDBANC = C_BCO_LANCCORR.CDTIPO) LOOP

              DOVRRAT := DOVRRAT + ROUND(C_BCO_LANCCORR.VALOR * C_RATTPCRED.VRPERATTPCRE/100,2);

              V_RATEIO := (ROUND(C_BCO_LANCCORR.VALOR * C_RATTPCRED.VRPERATTPCRE/100,2));

              IF V_INPARTIDA = V_COUNT THEN
                  IF C_BCO_LANCCORR.VALOR <> DOVRRAT THEN
                     V_RATEIO := V_RATEIO + (C_BCO_LANCCORR.VALOR - DOVRRAT);
                  END IF;
              END IF;

              IF C_RATTPCRED.CDFILIAL IS NULL THEN
                 RATEIO_PARTIDA(C_BCO_LANCCORR.CDEMPRESA, V_CDLOTE, C_BCO_LANCCORR.DATA, V_NRLANCLOTE, C_RATTPCRED.CDFILIAL,
                                C_RATTPCRED.CDCENTCUST, (DOVRRAT * C_BCO_LANCCORR.DEBCRED) * -1);
              ELSE
                 RATEIO_PARTIDA(C_BCO_LANCCORR.CDEMPRESA, V_CDLOTE, C_BCO_LANCCORR.DATA, V_NRLANCLOTE, C_BCO_LANCCORR.CDFILILANCA,
                                C_RATTPCRED.CDCENTCUST, (DOVRRAT * C_BCO_LANCCORR.DEBCRED) * -1);
              END IF;

              V_INPARTIDA := V_INPARTIDA + 1;
           END LOOP;
        ELSE
           RATEIO_PARTIDA(C_BCO_LANCCORR.CDEMPRESA, V_CDLOTE, C_BCO_LANCCORR.DATA, V_NRLANCLOTE, C_BCO_LANCCORR.CDFILILANCA,
                          V_CDCENTCUST, C_BCO_LANCCORR.VALOR * C_BCO_LANCCORR.DEBCRED);
        END IF;

        INSERT INTO RELAMBANCLANLOT
           (CDEMPRESA, CDLOTE, NRLANCLOTE, NRSEQLANLOTE, CDREDUCONT, NRMOVBANC)
        VALUES
           (C_BCO_LANCCORR.CDEMPRESA, V_CDLOTE, V_NRLANCLOTE, '00001', C_BCO_LANCCORR.CDREDUCONT, C_BCO_LANCCORR.NRMOVBANC);

        IF CONFERE_PARTIDA(C_BCO_LANCCORR.CDEMPRESA, V_CDLOTE, V_NRLANCLOTE,C_BCO_LANCCORR.DATA) THEN
           UPDATE MOVBANCARIO SET IDSTATUSCTBBANC = 'C'
            WHERE CDREDUCONT = C_BCO_LANCCORR.CDREDUCONT
              AND NRMOVBANC = C_BCO_LANCCORR.NRMOVBANC;
        ELSE
           UPDATE MOVBANCARIO SET IDSTATUSCTBBANC = 'C'
            WHERE CDREDUCONT = C_BCO_LANCCORR.CDREDUCONT
              AND NRMOVBANC = C_BCO_LANCCORR.NRMOVBANC;
        END IF;
  END LOOP;

  DELETE FROM LISTA
   WHERE NRLISTA = P_NRLISTA;  
END;
/