CREATE OR REPLACE PROCEDURE CONTABILIZA_REC_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;
  V_DBFATOR FLOAT;
BEGIN
   EXCLUI_LANCCONTABIL('07',P_NRLISTA);
   
   FOR C_REC_CONTA IN(
      SELECT DISTINCT CP.CDTPCTRECEB, NF.CDSERIE, NF.CDINSCESTA, NF.CDINSCMUNI, NF.VRNOTAFISC,
             NF.CDFILIEMIT, NF.CDTIPOOPER, NF.IDTPIJURNF AS IDTPINSJUR, NF.NRINSJURNF AS NRINSJURCLIE,
             NF.CDCLIENTE, NF.NRNOTAFISC, NF.DTEMISSAO, NF.DTENTRSAID,  NF.IDSTATUS,
             NF.NRLANCTBDOC, NF.NRLICENCAUSO, NF.CDVENDEDOR, NF.CDFILIAL, NF.NRLANCTONF, NF.CDEMPRESA
        FROM CONTARECEBER CP, NOTAFISCAL NF, 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)) LOOP
        
        BEGIN
          SELECT T.CDHISTPADR, H.DSHISTPADR
            INTO V_CDHISTPADR, V_DSHISTPADR
            FROM TIPOCTRECEBER T, HISTPADR H
           WHERE T.CDTPCTRECEB = C_REC_CONTA.CDTPCTRECEB 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 = 'CTAREC');
           EXCEPTION
             WHEN NO_DATA_FOUND THEN
              V_CDHISTPADR := NULL;
              V_DSHISTPADR := NULL;
           END;
        END IF;
        
        MONTA_HISTORICO(C_REC_CONTA.CDFILIAL,C_REC_CONTA.NRLANCTONF,V_CDHISTPADR,'','','07', '',V_DSHISTPADR);

        CONTCTBL_CLIENTE(C_REC_CONTA.CDEMPRESA, '', 'E', C_REC_CONTA.CDFILIEMIT,C_REC_CONTA.CDCLIENTE, V_STCONTCTBL, V_STCONTCTBLP, V_STCONTADIFE, V_CDHISTPADRA);

        GERA_LOTECONTABIL(C_REC_CONTA.CDEMPRESA,'07','',C_REC_CONTA.CDFILIEMIT,C_REC_CONTA.DTENTRSAID,C_REC_CONTA.CDTPCTRECEB,'',V_CDLOTE,V_NRLANCLOTE);

        /* DEBITA CLIENTE */
        NOVA_PARTIDA(C_REC_CONTA.CDEMPRESA,V_CDLOTE,C_REC_CONTA.DTENTRSAID,V_NRLANCLOTE, V_STCONTCTBL, V_CDHISTPADR,
                     V_DSHISTPADR, C_REC_CONTA.VRNOTAFISC * -1, 'C', C_REC_CONTA.CDCLIENTE, '07', C_REC_CONTA.NRNOTAFISC || '/' || C_REC_CONTA.CDSERIE, 'N');

        BEGIN
          SELECT NVL(F.CDCENTCUST, E.CDCENTCUST) AS CDCENTCUST
            INTO V_CDCENTCUST
            FROM FILIAL F, EMPRESA E
           WHERE F.CDFILIAL = C_REC_CONTA.CDFILIEMIT
             AND E.CDEMPRESA = C_REC_CONTA.CDEMPRESA;
        EXCEPTION
         WHEN NO_DATA_FOUND THEN
           V_CDCENTCUST := NULL;
        END;

        IF V_CDCENTCUST IS NOT NULL THEN
           RATEIO_PARTIDA(C_REC_CONTA.CDEMPRESA, V_CDLOTE, C_REC_CONTA.DTENTRSAID, V_NRLANCLOTE, C_REC_CONTA.CDFILIEMIT,
                          V_CDCENTCUST, C_REC_CONTA.VRNOTAFISC * -1);
        END IF;

        BEGIN
          SELECT CDCONTCTBL
            INTO V_STCONTCTBL
            FROM TIPOCTRECEBER
           WHERE CDTPCTRECEB = C_REC_CONTA.CDTPCTRECEB;
        EXCEPTION
          WHEN NO_DATA_FOUND THEN
           V_STCONTCTBL := NULL;
        END;

        BEGIN
          SELECT CDFILIAL
            INTO V_CDFILIAL
            FROM RATTPCTR
           WHERE (CDTPCTRECEB = C_REC_CONTA.CDTPCTRECEB);
        EXCEPTION
          WHEN NO_DATA_FOUND THEN
           V_CDFILIAL := NULL;
        END;

        /* CREDITA tipo de conta a receber */
        NOVA_PARTIDA(C_REC_CONTA.CDEMPRESA,V_CDLOTE,C_REC_CONTA.DTENTRSAID,V_NRLANCLOTE, V_STCONTCTBL, V_CDHISTPADR,
                     V_DSHISTPADR, C_REC_CONTA.VRNOTAFISC, 'C', C_REC_CONTA.CDCLIENTE, '07',C_REC_CONTA.NRNOTAFISC || '/' || C_REC_CONTA.CDSERIE, 'N');

        V_INPARTIDA := 1;

        V_DOVRTOTALRAT := 0;

        BEGIN
          SELECT COUNT(*)
            INTO V_COUNT
            FROM RATTPCTR
           WHERE (CDTPCTRECEB = C_REC_CONTA.CDTPCTRECEB);
        EXCEPTION
         WHEN NO_DATA_FOUND THEN
           V_COUNT := NULL;
        END;

        IF V_CDFILIAL IS NOT NULL THEN

           FOR RATTPCTR IN(
              SELECT CDCENTCUST, CDFIRATTPCTR, CDFILIAL, VRPERATTPCTR
                FROM RATTPCTR
               WHERE (CDTPCTRECEB = C_REC_CONTA.CDTPCTRECEB)) LOOP

               V_DOVRTOTALRAT := V_DOVRTOTALRAT + ROUND((C_REC_CONTA.VRNOTAFISC * RATTPCTR.VRPERATTPCTR)/100,2);

               V_VRRATEIO := (ROUND(C_REC_CONTA.VRNOTAFISC * RATTPCTR.VRPERATTPCTR/100,2)) ;

               IF V_INPARTIDA = V_COUNT THEN
                  IF C_REC_CONTA.VRNOTAFISC <> V_DOVRTOTALRAT THEN
                     V_VRRATEIO := V_VRRATEIO + (C_REC_CONTA.VRNOTAFISC - V_DOVRTOTALRAT);
                  END IF;
               END IF;

               IF RATTPCTR.CDFILIAL IS NOT NULL THEN
                  RATEIO_PARTIDA(C_REC_CONTA.CDEMPRESA,V_CDLOTE,C_REC_CONTA.DTENTRSAID,V_NRLANCLOTE,RATTPCTR.CDFILIAL,
                                 RATTPCTR.CDCENTCUST, V_VRRATEIO);
               ELSE
                  RATEIO_PARTIDA(C_REC_CONTA.CDEMPRESA,V_CDLOTE,C_REC_CONTA.DTENTRSAID,V_NRLANCLOTE,C_REC_CONTA.CDFILIEMIT,
                                 RATTPCTR.CDCENTCUST, V_VRRATEIO);
               END IF;

               V_INPARTIDA := V_INPARTIDA + 1 ;
           END LOOP;
        ELSE
          RATEIO_PARTIDA(C_REC_CONTA.CDEMPRESA,V_CDLOTE,C_REC_CONTA.DTENTRSAID,V_NRLANCLOTE,C_REC_CONTA.CDFILIEMIT,
                         V_CDCENTCUST, (ROUND(C_REC_CONTA.VRNOTAFISC)));
        END IF;


        FOR C_AJUSREC IN (
         SELECT A.CDFILIAL, A.NRLANCTONF, A.NRSEQVENREC,
                A.NRPARCELAREC, A.DTAJUSTEREC, A.CDAJUCTREC,
                T.IDAJUCTREC, T.DSAJUCTREC, A.DSAJUSTEREC,
                A.VRAJUSTEREC, A.CDECTBAJREC, A.NRLCTBAJREC,
                A.CDCLASSFINA, A.CDEMMFAJCNTR, A.CDFIMFAJCNTR,
                A.DTMOVFLUXO, A.IDMOVFLUXO
           FROM TIPOAJRECEBER T, AJCTRECEBER A, LISTA L
          WHERE (A.CDFILIAL     = SUBSTR(L.CDLISTA,1,4))
            AND (A.NRLANCTONF   = SUBSTR(L.CDLISTA,5,10))
            AND (A.CDAJUCTREC   = T.CDAJUCTREC)
          ORDER BY A.CDFILIAL, A.NRLANCTONF, A.CDAJUCTREC) LOOP

            FOR C_TPAJUSREC IN (
               SELECT T.CDAJUCTREC, T.CDHISTPADR, T.CDCONTCTBL, T.DSAJUCTREC,
                      T.IDAJUCTREC, H.DSHISTPADR
                 FROM TIPOAJRECEBER T, HISTPADR H
                WHERE (T.CDAJUCTREC = C_AJUSREC.CDAJUCTREC)
                  AND (T.CDHISTPADR = H.CDHISTPADR)
                ORDER BY T.CDAJUCTREC) LOOP

                MONTA_HISTORICO(C_AJUSREC.CDFILIAL,C_AJUSREC.NRLANCTONF,C_TPAJUSREC.CDHISTPADR,'',C_AJUSREC.DTAJUSTEREC,'07',C_AJUSREC.CDAJUCTREC,V_DSHISTPADR);

            END LOOP;

            CONTCTBL_CLIENTE(C_REC_CONTA.CDEMPRESA, '', 'E', C_REC_CONTA.CDFILIEMIT,C_REC_CONTA.CDCLIENTE, V_STCONTCTBL, V_STCONTCTBLP, V_STCONTADIFE, V_CDHISTPADRA);

            IF C_AJUSREC.IDAJUCTREC = 'D' THEN
               V_DBFATOR := -1;
            ELSE
               V_DBFATOR := 1;
            END IF;

            /* CREDITA CLIENTE*/
            NOVA_PARTIDA(C_REC_CONTA.CDEMPRESA,V_CDLOTE,C_REC_CONTA.DTENTRSAID,V_NRLANCLOTE, V_STCONTCTBL, V_CDHISTPADR,
                         V_DSHISTPADR, C_AJUSREC.VRAJUSTEREC * (V_DBFATOR * -1), 'C', C_REC_CONTA.CDCLIENTE, '07', C_REC_CONTA.NRNOTAFISC || '/' || C_REC_CONTA.CDSERIE, 'N');

            IF V_CDCENTCUST IS NOT NULL THEN
               RATEIO_PARTIDA(C_REC_CONTA.CDEMPRESA,V_CDLOTE,C_REC_CONTA.DTENTRSAID,V_NRLANCLOTE,C_REC_CONTA.CDFILIEMIT,
                              V_CDCENTCUST, C_AJUSREC.VRAJUSTEREC * (V_DBFATOR * -1) );
            END IF;

            BEGIN
              SELECT CDCONTCTBL
                INTO V_STCONTCTBL
                FROM TIPOAJRECEBER
               WHERE CDAJUCTREC = C_AJUSREC.CDAJUCTREC;
            EXCEPTION
              WHEN NO_DATA_FOUND THEN
               V_STCONTCTBL := NULL;
            END;

            /* DEBITA TIPO DE AJUSTE A RECEBER */
            NOVA_PARTIDA(C_REC_CONTA.CDEMPRESA,V_CDLOTE,C_REC_CONTA.DTENTRSAID,V_NRLANCLOTE, V_STCONTCTBL, V_CDHISTPADR,
                         V_DSHISTPADR, C_AJUSREC.VRAJUSTEREC * V_DBFATOR, 'C', C_REC_CONTA.CDCLIENTE, '07', C_REC_CONTA.NRNOTAFISC || '/' || C_REC_CONTA.CDSERIE, 'N');

            V_INPARTIDA := 1;

            BEGIN
             SELECT CDFILIAL
               INTO V_CDFILIAL
               FROM RATTPAJR
              WHERE (CDAJUCTREC = C_AJUSREC.CDAJUCTREC);
            EXCEPTION
              WHEN NO_DATA_FOUND THEN
               V_CDFILIAL := NULL;
            END;

            BEGIN
              SELECT COUNT(*)
                INTO V_COUNT
                FROM RATTPAJR
               WHERE (CDAJUCTREC = C_AJUSREC.CDAJUCTREC);
            EXCEPTION
              WHEN NO_DATA_FOUND THEN
                V_COUNT := NULL;
            END;

            V_DOVRTOTALRAT := 0;

            IF V_CDFILIAL IS NOT NULL THEN
               FOR RATTPAJR IN(
                 SELECT CDCENTCUST, CDFIRATTPAJR, CDFILIAL, VRPERATTPAJR
                   FROM RATTPAJR
                  WHERE (CDAJUCTREC = C_AJUSREC.CDAJUCTREC)) LOOP

                  V_DOVRTOTALRAT := 0;

                  V_DOVRTOTALRAT := V_DOVRTOTALRAT + ROUND((C_AJUSREC.VRAJUSTEREC * RATTPAJR.VRPERATTPAJR)/100,2);

                  V_VRRATEIO := (ROUND((C_AJUSREC.VRAJUSTEREC * RATTPAJR.VRPERATTPAJR)/100,2) * -1);

                  IF RATTPAJR.CDFILIAL IS NOT NULL THEN
                     RATEIO_PARTIDA(C_REC_CONTA.CDEMPRESA,V_CDLOTE,C_REC_CONTA.DTENTRSAID,V_NRLANCLOTE,RATTPAJR.CDFILIAL,
                                    RATTPAJR.CDCENTCUST, V_VRRATEIO * V_DBFATOR  );
                  ELSE
                     RATEIO_PARTIDA(C_REC_CONTA.CDEMPRESA,V_CDLOTE,C_REC_CONTA.DTENTRSAID,V_NRLANCLOTE,C_REC_CONTA.CDFILIEMIT,
                                    RATTPAJR.CDCENTCUST, V_VRRATEIO * V_DBFATOR);
                  END IF;

                  V_INPARTIDA := V_INPARTIDA + 1;
               END LOOP;
            ELSE
              RATEIO_PARTIDA(C_REC_CONTA.CDEMPRESA,V_CDLOTE,C_REC_CONTA.DTENTRSAID,V_NRLANCLOTE,C_REC_CONTA.CDFILIEMIT,
                             V_CDCENTCUST, (ROUND(C_AJUSREC.VRAJUSTEREC * V_DBFATOR,2)));
            END IF;
        END LOOP;

        INSERT INTO RELACNFLANCLOTE
           (CDEMPRESA, CDLOTE, NRLANCLOTE, NRSEQLANLOTE,CDFILIAL, NRLANCTONF)
        VALUES
           (C_REC_CONTA.CDEMPRESA, V_CDLOTE, V_NRLANCLOTE, '00001', C_REC_CONTA.CDFILIAL, C_REC_CONTA.NRLANCTONF);

        IF CONFERE_PARTIDA(C_REC_CONTA.CDEMPRESA, V_CDLOTE, V_NRLANCLOTE,C_REC_CONTA.DTENTRSAID) THEN
           UPDATE NOTAFISCAL SET IDSTATUSCTBNF = 'C'
            WHERE CDFILIAL = C_REC_CONTA.CDFILIAL
              AND NRLANCTONF = C_REC_CONTA.NRLANCTONF;
        ELSE
           UPDATE NOTAFISCAL SET IDSTATUSCTBNF = 'E'
            WHERE CDFILIAL = C_REC_CONTA.CDFILIAL
              AND NRLANCTONF = C_REC_CONTA.NRLANCTONF;
        END IF;
   END LOOP;
   DELETE FROM LISTA
    WHERE NRLISTA = P_NRLISTA;
END;
/