CREATE OR REPLACE PROCEDURE CONTABILIZA_PAG_CONTA(
  P_NRLISTA  IN LISTA.NRLISTA%TYPE)
IS
  V_NRLANCLOTE VARCHAR2(06);
  V_CDLOTE VARCHAR2(20);
  V_GERLOTE VARCHAR2(01);
  V_IDRETORNA VARCHAR2(01);
  V_IDCONTCCUS VARCHAR2(01);
  V_DSHISTPADR VARCHAR2(1000);
  V_STCONTCTBL VARCHAR2(20);
  V_STCONTCTBLP VARCHAR2(20);
  V_STCONTADIFE VARCHAR2(20);
  V_CDHISTPADR VARCHAR2(4);
  V_CDHISTPADRA VARCHAR2(4);
  V_DOVRTOTALRAT FLOAT;
  V_CDCENTCUST VARCHAR(04);
  V_CDFILIAL VARCHAR2(4); 
  V_COUNT NUMBER;
  V_INPARTIDA INT;
  V_VRRATEIO FLOAT;
BEGIN
   EXCLUI_LANCCONTABIL('06',P_NRLISTA);
   
   FOR C_PAG_CONTA IN(
       SELECT DISTINCT CP.CDTPCTPAGAR, NF.CDSERIEENTR, NF.VRNOTAFISC,
              NF.CDFILIRECE AS CDFILIORIG, NF.CDTIPOOPER, NF.NRINSJURNF AS NRINSJURFORN,
              NF.CDFORNECED, NF.NRNOTAFISC, NF.DTEMISSAO, NF.DTENTRSAID, NF.IDSTATUS,
              NF.NRLANCTBDOC,NF.CDFILIAL, NF.CDEMPRESA, NF.NRLANCTONF
         FROM NOTAFISCAL NF,CONTAPAGAR CP ,LISTA L
        WHERE (NF.CDFILIAL     = SUBSTR(L.CDLISTA,1,4))
          AND (NF.NRLANCTONF   = SUBSTR(L.CDLISTA,5,10))
          AND (CP.CDFILIAL     = NF.CDFILIAL)
          AND (CP.NRLANCTONF   = NF.NRLANCTONF)
          AND (L.NRLISTA = P_NRLISTA)
        ORDER BY NF.NRLANCTONF) LOOP
        
        BEGIN
          SELECT T.CDHISTPADR, H.DSHISTPADR
            INTO V_CDHISTPADR, V_DSHISTPADR
            FROM TIPOCTPAGAR T, HISTPADR H
           WHERE T.CDTPCTPAGAR = C_PAG_CONTA.CDTPCTPAGAR 
             AND T.CDHISTPADR = H.CDHISTPADR;
        EXCEPTION
          WHEN NO_DATA_FOUND THEN
            V_CDHISTPADR := NULL;
            V_DSHISTPADR := NULL;
        END;
        
        IF V_CDHISTPADR IS NULL THEN
           BEGIN
             SELECT HI.CDHISTPADR, HP.DSHISTPADR
               INTO V_CDHISTPADR, V_DSHISTPADR
               FROM HISTINTEGRA HI, HISTPADR HP 
              WHERE (HI.CDHISTPADR = HP.CDHISTPADR) 
                AND (HI.CDOPERACAO = 'CTAPAG');
           EXCEPTION
             WHEN NO_DATA_FOUND THEN
              V_CDHISTPADR := NULL;
              V_DSHISTPADR := NULL;
           END;
        END IF;
        
        MONTA_HISTORICO(C_PAG_CONTA.CDFILIAL,C_PAG_CONTA.NRLANCTONF,V_CDHISTPADR,'','','06', '',V_DSHISTPADR);

        CONTCTBL_FORNECEDOR(C_PAG_CONTA.CDEMPRESA, '', 'E', C_PAG_CONTA.CDFILIORIG, C_PAG_CONTA.CDFORNECED, V_STCONTCTBL, V_STCONTCTBLP, V_STCONTADIFE, V_CDHISTPADRA);
    
        GERA_LOTECONTABIL(C_PAG_CONTA.CDEMPRESA,'06','',C_PAG_CONTA.CDFILIORIG,C_PAG_CONTA.DTENTRSAID,C_PAG_CONTA.CDTPCTPAGAR,'',V_CDLOTE,V_NRLANCLOTE);

        /* CREDITA FORNECEDOR */
        NOVA_PARTIDA(C_PAG_CONTA.CDEMPRESA,V_CDLOTE,C_PAG_CONTA.DTENTRSAID,V_NRLANCLOTE, V_STCONTCTBL, V_CDHISTPADR,
                     V_DSHISTPADR, C_PAG_CONTA.VRNOTAFISC, 'F', C_PAG_CONTA.CDFORNECED, '06', C_PAG_CONTA.NRNOTAFISC || '/' || C_PAG_CONTA.CDSERIEENTR, 'N');
                     
        BEGIN
          SELECT NVL(F.CDCENTCUST, E.CDCENTCUST) AS CDCENTCUST
            INTO V_CDCENTCUST
            FROM FILIAL F, EMPRESA E
           WHERE F.CDFILIAL = C_PAG_CONTA.CDFILIORIG
             AND E.CDEMPRESA = C_PAG_CONTA.CDEMPRESA;
        EXCEPTION
         WHEN NO_DATA_FOUND THEN
           V_CDCENTCUST := NULL;
        END;

        IF V_CDCENTCUST IS NOT NULL THEN
           RATEIO_PARTIDA(C_PAG_CONTA.CDEMPRESA,V_CDLOTE,C_PAG_CONTA.DTENTRSAID,V_NRLANCLOTE,C_PAG_CONTA.CDFILIORIG,
                          V_CDCENTCUST, C_PAG_CONTA.VRNOTAFISC);
        END IF;

        BEGIN
          SELECT CDCONTCTBL
            INTO V_STCONTCTBL
            FROM TIPOCTPAGAR
           WHERE CDTPCTPAGAR = C_PAG_CONTA.CDTPCTPAGAR;
        EXCEPTION
          WHEN NO_DATA_FOUND THEN
           V_STCONTCTBL := NULL;
        END;

        BEGIN
          SELECT MAX(CDFILIAL) AS CDFILIAL
            INTO V_CDFILIAL
            FROM RATTPCTP
           WHERE (CDTPCTPAGAR = C_PAG_CONTA.CDTPCTPAGAR);
        EXCEPTION
          WHEN NO_DATA_FOUND THEN
           V_CDFILIAL := NULL;
        END;

        NOVA_PARTIDA(C_PAG_CONTA.CDEMPRESA,V_CDLOTE,C_PAG_CONTA.DTENTRSAID,V_NRLANCLOTE, V_STCONTCTBL, V_CDHISTPADR,
                     V_DSHISTPADR, C_PAG_CONTA.VRNOTAFISC * -1, 'F', C_PAG_CONTA.CDFORNECED, '06', C_PAG_CONTA.NRNOTAFISC || '/' || C_PAG_CONTA.CDSERIEENTR, 'N');

        V_INPARTIDA := 1;

        V_DOVRTOTALRAT := 0;

        BEGIN
          SELECT COUNT(*)
            INTO V_COUNT
            FROM RATTPCTP
           WHERE (CDTPCTPAGAR = C_PAG_CONTA.CDTPCTPAGAR);
        EXCEPTION
         WHEN NO_DATA_FOUND THEN
           V_COUNT := NULL;
        END;

        IF V_CDFILIAL IS NOT NULL THEN

           FOR RATTPCTP IN(
              SELECT CDCENTCUST, CDFIRATTPCTP, CDFILIAL, VRPERATTPCTP
                FROM RATTPCTP
               WHERE (CDTPCTPAGAR = C_PAG_CONTA.CDTPCTPAGAR)) LOOP

               V_DOVRTOTALRAT := V_DOVRTOTALRAT + ROUND((C_PAG_CONTA.VRNOTAFISC * RATTPCTP.VRPERATTPCTP)/100,2);

               V_VRRATEIO := (ROUND(C_PAG_CONTA.VRNOTAFISC * RATTPCTP.VRPERATTPCTP/100,2) * -1);
               
               IF V_INPARTIDA = V_COUNT THEN
                  IF C_PAG_CONTA.VRNOTAFISC <> V_DOVRTOTALRAT THEN
                     V_VRRATEIO := V_VRRATEIO + (C_PAG_CONTA.VRNOTAFISC - V_DOVRTOTALRAT);
                  END IF;
               END IF;   
              
               IF RATTPCTP.CDFILIAL IS NOT NULL THEN
                  RATEIO_PARTIDA(C_PAG_CONTA.CDEMPRESA,V_CDLOTE,C_PAG_CONTA.DTENTRSAID,V_NRLANCLOTE,RATTPCTP.CDFILIAL,
                                 RATTPCTP.CDCENTCUST, V_VRRATEIO); 
               ELSE
                  RATEIO_PARTIDA(C_PAG_CONTA.CDEMPRESA,V_CDLOTE,C_PAG_CONTA.DTENTRSAID,V_NRLANCLOTE,C_PAG_CONTA.CDFILIORIG,
                                 RATTPCTP.CDCENTCUST, V_VRRATEIO);
               END IF;
               
               V_INPARTIDA := V_INPARTIDA + 1 ;
           END LOOP;
        ELSE
          RATEIO_PARTIDA(C_PAG_CONTA.CDEMPRESA,V_CDLOTE,C_PAG_CONTA.DTENTRSAID,V_NRLANCLOTE,C_PAG_CONTA.CDFILIORIG,
                         V_CDCENTCUST, (ROUND(C_PAG_CONTA.VRNOTAFISC * -1)));   
        END IF;
    

        FOR C_AJUSPAG IN (
           SELECT A.CDFILIAL, A.NRLANCTONF, A.NRSEQVENPAG, A.NRPARCELAPAG,
                  A.DTAJUSTEPAG, A.CDAJUCTPAG, T.IDAJUCTPAG, T.DSAJUCTPAG,
                  A.DSAJUSTEPAG, A.VRAJUSTEPAG, A.CDECTBAJPAG, A.NRLCTBAJPAG,
                  A.CDCLASSFINA, A.CDEMMFAJCNTP, A.CDFIMFAJCNTP, A.DTMOVFLUXO,
                  A.IDMOVFLUXO
             FROM TIPOAJPAGAR T, AJCTPAGAR A, LISTA L
            WHERE (A.CDFILIAL = SUBSTR(L.CDLISTA,1,4))
              AND (A.NRLANCTONF = SUBSTR(L.CDLISTA,5,10))
              AND (A.CDAJUCTPAG = T.CDAJUCTPAG)
              AND (L.NRLISTA = P_NRLISTA)
            ORDER BY A.CDFILIAL, A.NRLANCTONF, A.CDAJUCTPAG) LOOP

            FOR C_TPAJUSPAG IN (
               SELECT T.CDAJUCTPAG, T.CDHISTPADR, T.CDCONTCTBL, T.DSAJUCTPAG,
                      T.IDAJUCTPAG, H.DSHISTPADR, IDCTBAJCTPAG
                 FROM TIPOAJPAGAR T, HISTPADR H
                WHERE (T.CDAJUCTPAG = C_AJUSPAG.CDAJUCTPAG)
                  AND (T.CDHISTPADR = H.CDHISTPADR)
                ORDER BY T.CDAJUCTPAG) LOOP

                MONTA_HISTORICO(C_AJUSPAG.CDFILIAL,C_AJUSPAG.NRLANCTONF,C_TPAJUSPAG.CDHISTPADR,'','','06',C_AJUSPAG.CDAJUCTPAG,V_DSHISTPADR);

            END LOOP;

            CONTCTBL_FORNECEDOR(C_PAG_CONTA.CDEMPRESA, '', 'E', C_PAG_CONTA.CDFILIORIG, C_PAG_CONTA.CDFORNECED, V_STCONTCTBL, V_STCONTCTBLP, V_STCONTADIFE, V_CDHISTPADRA);

            /* DEBITA FORNECEDOR*/
            NOVA_PARTIDA(C_PAG_CONTA.CDEMPRESA,V_CDLOTE,C_PAG_CONTA.DTENTRSAID,V_NRLANCLOTE, V_STCONTCTBL, V_CDHISTPADR,
                         V_DSHISTPADR, C_AJUSPAG.VRAJUSTEPAG * -1, 'F', C_PAG_CONTA.CDFORNECED, '06', C_PAG_CONTA.NRNOTAFISC || '/' || C_PAG_CONTA.CDSERIEENTR, 'N');

            IF V_CDCENTCUST IS NOT NULL THEN
               RATEIO_PARTIDA(C_PAG_CONTA.CDEMPRESA,V_CDLOTE,C_PAG_CONTA.DTENTRSAID,V_NRLANCLOTE,C_PAG_CONTA.CDFILIORIG,
                              V_CDCENTCUST, C_AJUSPAG.VRAJUSTEPAG);
            END IF;

            BEGIN
              SELECT CDCONTCTBL
                INTO V_STCONTCTBL
                FROM TIPOAJPAGAR
               WHERE CDAJUCTPAG = C_AJUSPAG.CDAJUCTPAG;
            EXCEPTION
              WHEN NO_DATA_FOUND THEN
               V_STCONTCTBL := NULL;
            END;

            /* CREDITA TIPO DE AJUSTE A PAGAR */
            NOVA_PARTIDA(C_PAG_CONTA.CDEMPRESA,V_CDLOTE,C_PAG_CONTA.DTENTRSAID,V_NRLANCLOTE, V_STCONTCTBL, V_CDHISTPADR,
                         V_DSHISTPADR, C_AJUSPAG.VRAJUSTEPAG, 'F', C_PAG_CONTA.CDFORNECED, '06', C_PAG_CONTA.NRNOTAFISC || '/' || C_PAG_CONTA.CDSERIEENTR, 'N');

            V_INPARTIDA := 1;

            BEGIN
             SELECT MAX(CDFILIAL) AS CDFILIAL
               INTO V_CDFILIAL
               FROM RATTPAJP
              WHERE (CDAJUCTPAG = C_AJUSPAG.CDAJUCTPAG);
            EXCEPTION
              WHEN NO_DATA_FOUND THEN
               V_CDFILIAL := NULL;
            END;

            BEGIN
              SELECT COUNT(*)
                INTO V_COUNT
                FROM RATTPAJP
               WHERE (CDAJUCTPAG = C_AJUSPAG.CDAJUCTPAG);
            EXCEPTION
              WHEN NO_DATA_FOUND THEN
                V_COUNT := NULL;
            END;
            
            V_DOVRTOTALRAT := 0;
            
            IF V_CDFILIAL IS NOT NULL THEN
               FOR RATTPAJP IN(
                 SELECT CDCENTCUST, CDFIRATTPAJP, CDFILIAL, VRPERATTPAJP
                   FROM RATTPAJP
                  WHERE (CDAJUCTPAG = C_AJUSPAG.CDAJUCTPAG)) LOOP
             
                  V_DOVRTOTALRAT := 0;
             
                  V_DOVRTOTALRAT := V_DOVRTOTALRAT + ROUND((C_AJUSPAG.VRAJUSTEPAG * RATTPAJP.VRPERATTPAJP)/100,2);
                  
                  V_VRRATEIO := (ROUND((C_AJUSPAG.VRAJUSTEPAG * RATTPAJP.VRPERATTPAJP)/100,2) * -1);
              
                  IF RATTPAJP.CDFILIAL IS NOT NULL THEN
                     RATEIO_PARTIDA(C_PAG_CONTA.CDEMPRESA,V_CDLOTE,C_PAG_CONTA.DTENTRSAID,V_NRLANCLOTE,RATTPAJP.CDFILIAL,
                                    RATTPAJP.CDCENTCUST, V_VRRATEIO ); 
                  ELSE
                     RATEIO_PARTIDA(C_PAG_CONTA.CDEMPRESA,V_CDLOTE,C_PAG_CONTA.DTENTRSAID,V_NRLANCLOTE,C_PAG_CONTA.CDFILIORIG,
                                    RATTPAJP.CDCENTCUST, V_VRRATEIO);
                  END IF;
                  
                  V_INPARTIDA := V_INPARTIDA + 1;
               END LOOP;
            ELSE
              RATEIO_PARTIDA(C_PAG_CONTA.CDEMPRESA,V_CDLOTE,C_PAG_CONTA.DTENTRSAID,V_NRLANCLOTE,C_PAG_CONTA.CDFILIORIG,
                             V_CDCENTCUST, (ROUND(C_AJUSPAG.VRAJUSTEPAG * -1)));   
            END IF;              
        END LOOP;   
        
        INSERT INTO RELACNFLANCLOTE 
           (CDEMPRESA, CDLOTE, NRLANCLOTE, NRSEQLANLOTE,CDFILIAL, NRLANCTONF)
        VALUES
           (C_PAG_CONTA.CDEMPRESA, V_CDLOTE, V_NRLANCLOTE, '00001', C_PAG_CONTA.CDFILIAL, C_PAG_CONTA.NRLANCTONF);
        
        IF CONFERE_PARTIDA(C_PAG_CONTA.CDEMPRESA, V_CDLOTE, V_NRLANCLOTE,C_PAG_CONTA.DTENTRSAID) THEN
           UPDATE NOTAFISCAL SET IDSTATUSCTBNF = 'C'
            WHERE CDFILIAL = C_PAG_CONTA.CDFILIAL
              AND NRLANCTONF = C_PAG_CONTA.NRLANCTONF;
        ELSE
           UPDATE NOTAFISCAL SET IDSTATUSCTBNF = 'E'
            WHERE CDFILIAL = C_PAG_CONTA.CDFILIAL
              AND NRLANCTONF = C_PAG_CONTA.NRLANCTONF;
        END IF;
   END LOOP;
   
   DELETE FROM LISTA
    WHERE NRLISTA = P_NRLISTA;
END;  
/
