CREATE OR REPLACE PROCEDURE CONTABILIZA_NF_ENTRADA (
   P_NRLISTA LISTA.NRLISTA%TYPE)
IS
   V_VRCONTITEM      FLOAT;
   V_VRCONTITEMNF    FLOAT;

   V_COUNTITEMPROD   FLOAT;
   V_VRCONTITEMTOT   FLOAT;
   V_VRPARTRATE      FLOAT;
   V_VRRATENF        FLOAT;
   V_VRRATEIO        FLOAT;
   V_VRITEMENCA      FLOAT;
   V_VRIMPOSTONF     FLOAT;
   V_DBVRPARTIDAS    FLOAT;
   V_DBVRCONTITEM    FLOAT;
   V_VRPECREPREST    FLOAT;
   V_VRPEDESCFORN    FLOAT;
   V_VRDESCBONI      FLOAT;

   V_COUNTRATEIO     INT;
   V_COUNTITEMNFORIG INT;
   V_INPARTIDAS      INT;
   V_RATEIO          INT;
   V_NRSEQEXIS       INT;
   V_CONTIMPO        INT;


   V_VRFATORMULT   NUMBER;
   V_VRIMPOSRETIDO NUMBER;
   V_VRLANCCORPO   NUMBER;
   V_VRSUBSTRIB    NUMBER;
   V_VRPARTIDAS    NUMBER;
   V_INRATEIO      NUMBER;

   V_DTINIVRPDESC  DATE;


   V_CDFILICCUST   VARCHAR2(4);
   V_NRLANCCONT    VARCHAR2(12);
   V_PRXNRLANCCONT VARCHAR2(20);
   V_CDCCTBLCORPO  VARCHAR2(20);
   V_CDCCTBLPROD   VARCHAR2(20);
   V_CDHISTPADR    VARCHAR2(04);
   V_CDMODDOCFISC  VARCHAR2(02);
   V_CDFILIORIG    VARCHAR2(04);
   V_NRLANCORIG    VARCHAR2(04);
   V_IDLANCCOME    VARCHAR2(1);
   V_CDCONTCOME    VARCHAR2(21);
   V_CDFILIALITEM   VARCHAR2(4);
   V_NRLANCTONFITEM VARCHAR2(10);
   V_CDCENTCUST     VARCHAR2(4);
   V_FILIALVRNFORIG VARCHAR2(4);
   V_NRLANCTONF      VARCHAR2(10);
   V_CDCTBLENTRPF    VARCHAR2(20);
   V_CDCTBLSAIDPF    VARCHAR2(20);
   V_CDIMPOSTRIB     VARCHAR2(2);
   V_NRLANCTONFMOD   VARCHAR2(10);
   V_CDPRODUTO       VARCHAR2(10);
   V_STCONTCTBL      VARCHAR2(20);
   V_STCONTCTBLP     VARCHAR2(20);
   V_DSHISTPADR      VARCHAR2(1000);
   V_CDLOTE          VARCHAR2(20);
   V_NRLANCLOTE      VARCHAR2(6);
   V_DESCITEMCTBLENTR VARCHAR2(20);
   V_DESCITEMCTBLSAID VARCHAR2(20);
   V_CDARVPROD        VARCHAR2(10);
   V_STCONTPASS       VARCHAR2(20);
   V_DSMODDOCFISC     VARCHAR2(100);
   V_CDTPREGFISC      VARCHAR2(20);
   V_CDFILIALMOD      VARCHAR2(04);
   V_CDCTBLFORNCLI    VARCHAR2(20);
   V_STCONTADIFE      VARCHAR2(20);
   V_IDRETORNA        VARCHAR2(01);
   V_IDAJUCTPAG       VARCHAR2(01);
   V_IDTPDESCFORN     VARCHAR2(01);
   V_DSAJUCTPAG       VARCHAR2(50);
   V_IDCONDESBONFORN  VARCHAR2(01);
   V_CDHISTPADRFOR    VARCHAR2(04);
   V_CDCONTCTBL       VARCHAR2(20);
   V_CDCONTCTBLFOR    VARCHAR2(20);
   V_DSHISTPADRFOR    VARCHAR2(1000);
   V_IDCONTAJUDTCOMP  VARCHAR2(01);
BEGIN
   EXCLUI_LANCCONTABIL('00',P_NRLISTA);

   FOR C_NF IN (
      SELECT
         NF.CDFILIAL, NF.NRLANCTONF, NF.CDEMPRESA,
         NF.IDDOCFINANC, NF.NRNOTAFISC, NF.CDCLIENTE,
         NF.CDFORNECED, NF.NRLANCTBDOC, NF.VRNOTAFISC,
         NF.CDFILIEMIT, NF.CDTIPOOPER, TP.IDCONTABOPER,
         TP.IDOPERDEVONF, NVL(NF.IDORIGDESTNF,'F') AS IDORIGDESTNF, NF.VRPRODUTOS,
         NF.CDFILIRECE, NF.DTENTRSAID, NF.CDINSCESTA,
         NF.DTEMISSAO, C.IDPRIPUBCLIE, NF.IDENTRSAIDOP,
         NVL(FI.CDCENTCUST, EP.CDCENTCUST) AS CDCENTCUST,
         NF.CDSERIEENTR, NF.CDSERIE, NF.IDTPIJURNF, NF.NRINSJURNF, TP.IDDTCONTAB
       FROM NOTAFISCAL NF, FILIAL FI, EMPRESA EP,
            TIPOOPERAC TP, CLIENTE C, LISTA L
      WHERE NF.CDEMPRESA = EP.CDEMPRESA
        AND NF.IDENTRSAIDOP = TP.IDENTRSAIDOP
        AND NF.CDTIPOOPER = TP.CDTIPOOPER
        AND NVL(TP.IDCONTABOPER,'S') = 'S'
        AND DECODE(NF.IDENTRSAIDOP,'E', NF.CDFILIRECE, NF.CDFILIEMIT) = FI.CDFILIAL
        AND NF.CDCLIENTE = C.CDCLIENTE(+)
        AND NF.CDFILIAL = SUBSTR(L.CDLISTA,1,4)
        AND (NF.NRLANCTONF = SUBSTR(L.CDLISTA,5,10))
        AND (L.NRLISTA = P_NRLISTA)) LOOP

    GERA_LOTECONTABIL(C_NF.CDEMPRESA,'00','',C_NF.CDFILIRECE,C_NF.DTENTRSAID,C_NF.CDTIPOOPER,'',V_CDLOTE,V_NRLANCLOTE);

    IF C_NF.IDCONTABOPER <> 'N' THEN

      V_VRFATORMULT := 1;
      IF C_NF.CDTIPOOPER = '14' THEN
         V_VRFATORMULT := -1;
      END IF;

      BEGIN
        SELECT C.CDCTBLFORNCLI
          INTO V_CDCTBLFORNCLI
          FROM CTBPRDTPOPER C,
              (SELECT MAX(PR.CDARVPROD) CDARVPROD
                 FROM CTBPRDTPOPER A, PRODUTO P, ITEMPRODUTO I, PRODUTO PR
                WHERE IDENTRSAIDOP = 'E'   AND
                      CDTIPOOPER   = C_NF.CDTIPOOPER   AND
                      P.CDARVPROD  LIKE  PR.CDARVPROD || '%'    AND
                      P.CDPRODUTO  =  I.CDPRODUTO    AND
                      I.CDFILIAL  = C_NF.CDFILIAL      AND
                      I.NRLANCTONF = C_NF.NRLANCTONF      AND
                      A.CDCTBLFORNCLI IS NOT NULL     AND
                      A.CDPRODUTO = PR.CDPRODUTO) PR,
               PRODUTO P
         WHERE IDENTRSAIDOP = 'E'   AND
               CDTIPOOPER   = C_NF.CDTIPOOPER   AND
               C.CDPRODUTO  = P.CDPRODUTO     AND
               P.CDARVPROD = PR.CDARVPROD;
      EXCEPTION
        WHEN NO_DATA_FOUND THEN
         V_CDCTBLFORNCLI := NULL;
      END;

      IF C_NF.IDORIGDESTNF = 'F' THEN
         V_IDLANCCOME := 'F';
         V_CDCONTCOME := C_NF.CDFORNECED;
      /* guilherme */

         CONTCTBL_FORNECEDOR(C_NF.CDEMPRESA, C_NF.CDTIPOOPER, 'E', C_NF.CDFILIAL, C_NF.CDFORNECED, V_STCONTCTBL, V_STCONTCTBLP, V_STCONTADIFE, V_CDHISTPADR);

      ELSIF C_NF.IDORIGDESTNF = 'C' THEN
         V_IDLANCCOME := 'C';
         V_CDCONTCOME := C_NF.CDCLIENTE;
      /* GUILHERME */

         CONTCTBL_CLIENTE(C_NF.CDEMPRESA, C_NF.CDTIPOOPER, 'E', C_NF.CDFILIAL, C_NF.CDCLIENTE, V_STCONTCTBL, V_STCONTCTBLP, V_STCONTADIFE, V_CDHISTPADR);

      ELSIF C_NF.IDORIGDESTNF = 'L' THEN
         V_IDLANCCOME := ' ';
         V_CDCONTCOME := '';
        /* GUILHERME */

         CONTCTBL_FILIAL(C_NF.CDEMPRESA, C_NF.CDTIPOOPER, 'E', C_NF.CDFILIAL, V_STCONTCTBL, V_STCONTCTBLP, V_STCONTADIFE, V_CDHISTPADR);

      END IF;

      /*LANCAMENTOS DE CREDITO*/

      BEGIN
        SELECT NVL(ROUND(SUM(VRIMPOSTONF),2),0)
          INTO V_VRIMPOSRETIDO
          FROM IMPOSTONF INF, IMPOSTO I
         WHERE CDFILIAL = C_NF.CDFILIAL
           AND NRLANCTONF = C_NF.NRLANCTONF
           AND INF.CDIMPOSTO = I.CDIMPOSTO
           AND I.IDTRATIMPO = 'R'
           AND I.IDTIPCONTAB = 'D'
           AND (((IDIMPORECUP = 'S') AND ((CDCCTBLATIV IS NULL) OR (CDFISCALNF <> '1'))) OR (IDIMPORECUP <> 'S'));
      EXCEPTION
        WHEN NO_DATA_FOUND THEN
        V_VRIMPOSRETIDO := 0;
      END;

      V_VRLANCCORPO := (C_NF.VRNOTAFISC - V_VRIMPOSRETIDO) * (V_VRFATORMULT);

      BEGIN
        SELECT NVL(SUM(I.VRTOTIMPONF),0)
          INTO V_VRSUBSTRIB
          FROM IMPOSNFTOT I,
               (SELECT CDIMPOSTRIB
                  FROM IMPOSTO
                 WHERE CDIMPOSTRIB IS NOT NULL) S
         WHERE I.CDFILIAL   = C_NF.CDFILIAL
           AND I.NRLANCTONF = C_NF.NRLANCTONF
           AND I.CDIMPOSTO  = S.CDIMPOSTRIB;
      EXCEPTION
        WHEN NO_DATA_FOUND THEN
        V_VRSUBSTRIB := 0;
      END;

      BEGIN
        SELECT MAX(A.CDMODDOCFISC )
          INTO V_CDMODDOCFISC
          FROM NOTAFISCAL N, MODDOCFISC A
         WHERE N.CDMODDOCFISC = A.CDMODDOCFISC
           AND A.CDTPREGFISC = '70'
           AND N.CDFILIAL = C_NF.CDFILIAL
           AND N.NRLANCTONF = C_NF.NRLANCTONF;
      EXCEPTION
        WHEN NO_DATA_FOUND THEN
        V_CDMODDOCFISC := NULL;
      END;

      IF (V_VRSUBSTRIB > 0) AND (V_CDMODDOCFISC IS NOT NULL) THEN
         V_VRLANCCORPO := V_VRLANCCORPO - V_VRSUBSTRIB;
      END IF;

      IF V_CDCTBLFORNCLI IS NOT NULL THEN
         V_STCONTCTBL := V_CDCTBLFORNCLI;
      END IF;

      /*MONTA HISTORICO PADRAO*/

      MONTA_HISTORICO(C_NF.CDFILIAL,C_NF.NRLANCTONF,V_CDHISTPADR,C_NF.NRNOTAFISC,C_NF.DTEMISSAO, '00', '', V_DSHISTPADR);

      IF (V_VRLANCCORPO <> 0) THEN
         /*INSERE PARTIDA DE CREDITO*/
         NOVA_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE, V_STCONTCTBL, V_CDHISTPADR,
                      V_DSHISTPADR, ROUND(V_VRLANCCORPO,2), V_IDLANCCOME, V_CDCONTCOME, '00', C_NF.NRNOTAFISC, 'S');

         /*INSERE RATEIO*/
         RATEIO_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE,C_NF.CDFILIRECE,
                        C_NF.CDCENTCUST, V_VRLANCCORPO);


      END IF;

      /*LANCAMENTOS DE DEBITO*/

      FOR IE IN (
         SELECT NVL(VRITEMENCA,0) AS VRITEMENCA, CDCTBLENTREN, CDCTBLSAIDEN, IDENCARGO
           FROM ITEMENCARGO IE, ENCARGO E
          WHERE IE.CDFILIAL = C_NF.CDFILIAL
            AND IE.NRLANCTONF = C_NF.NRLANCTONF
            AND IE.CDENCARGO = E.CDENCARGO
            AND IE.VRITEMENCA > 0
            AND E.IDENCARGO IN ('A','D')
            AND (DECODE('E', 'E', CDCTBLENTREN, CDCTBLSAIDEN) IS  NOT NULL)) LOOP

         IF IE.IDENCARGO = 'A' THEN
            V_VRLANCCORPO := V_VRLANCCORPO - (IE.VRITEMENCA * V_VRFATORMULT);
         ELSE
            V_VRLANCCORPO := V_VRLANCCORPO + (IE.VRITEMENCA * V_VRFATORMULT);
         END IF;
      END LOOP;

      FOR IP IN (
         SELECT A.CDIMPOSTO, NVL(ROUND(SUM(A.VRIMPOSTONF),2),0) AS VRIMPOSTONFE,
                A.IDTRATIMPO, A.IDIMPORECUP, A.IDACRPRODENTRNF,
                MAX(CDCONTATIV) AS CDCONTATIV, MAX(CDCONTPASS)  AS CDCCONTPASS,
                MAX(CDCONTDESP)  AS CDCONTDESP, MAX(CDCONTIMPO) AS CDCONTIMPO
           FROM (SELECT INF.CDIMPOSTO, VRIMPOSTONF, NVL(IDACRPRODENTRNF, 'N') IDACRPRODENTRNF,
                        PF.IDCARACFIS, I.IDIMPORECUP, I.IDTRATIMPO, INF.CDFISCALNF,
                        CONTCTBL_IMPOSTO(I.CDIMPOSTO, 'A', NF.IDENTRSAIDOP, NF.CDTIPOOPER, NF.IDTPIJURNF) CDCONTATIV,
                        CONTCTBL_IMPOSTO(I.CDIMPOSTO, 'P', NF.IDENTRSAIDOP, NF.CDTIPOOPER, NF.IDTPIJURNF) CDCONTPASS,
                        CONTCTBL_IMPOSTO(I.CDIMPOSTO, 'D', NF.IDENTRSAIDOP, NF.CDTIPOOPER, NF.IDTPIJURNF) CDCONTDESP,
                        CONTCTBL_IMPOSTO(I.CDIMPOSTO, 'E', NF.IDENTRSAIDOP, NF.CDTIPOOPER, NF.IDTPIJURNF) CDCONTIMPO
                   FROM IMPOSTONF INF, IMPOSTO I, NOTAFISCAL NF, PARAMFILIAL PF
                  WHERE NF.CDFILIAL     = C_NF.CDFILIAL
                    AND NF.NRLANCTONF   = C_NF.NRLANCTONF
                    AND PF.CDFILIAL(+)  = NF.CDFILIRECE
                    AND INF.CDFILIAL    = C_NF.CDFILIAL
                    AND INF.NRLANCTONF  = C_NF.NRLANCTONF
                    AND INF.CDIMPOSTO   = I.CDIMPOSTO
                    AND INF.VRIMPOSTONF > 0
                    AND ((IDDIFALIQCUSPRN = 'S' AND INF.IDIMPOSTONF IN ('1','4','3')) OR (IDDIFALIQCUSPRN = 'N' AND INF.IDIMPOSTONF IN ('1','4')))
                    AND I.IDTIPCONTAB  = 'D') A
          WHERE ((((A.IDIMPORECUP  = 'S' AND A.CDFISCALNF = '1' AND NVL(A.IDCARACFIS,'C') = 'C') OR (A.IDCARACFIS = 'A')) AND (A.CDCONTATIV IS NOT NULL)) OR
                 ((A.IDIMPORECUP <> 'S') AND (A.CDCONTPASS IS NOT NULL)))
          GROUP BY A.CDIMPOSTO, A.IDTRATIMPO,  A.IDIMPORECUP, A.IDACRPRODENTRNF) LOOP

         IF ((C_NF.IDOPERDEVONF = 'S') OR (C_NF.CDTIPOOPER = '16')) THEN
            IF IP.IDTRATIMPO = 'V' THEN
               V_VRIMPOSTONF := ROUND(IP.VRIMPOSTONFE,2);
            ELSE
               V_VRIMPOSTONF := - ROUND(IP.VRIMPOSTONFE,2);
            END IF;

            IF IP.IDTRATIMPO = 'A' AND IP.IDACRPRODENTRNF = 'N' THEN
               V_VRIMPOSTONF := ROUND(V_VRIMPOSTONF,2);
            END IF;
         ELSE
            IF IP.IDTRATIMPO = 'V' THEN
               V_VRIMPOSTONF := - ROUND(IP.VRIMPOSTONFE,2);
            ELSE
               V_VRIMPOSTONF := ROUND(IP.VRIMPOSTONFE,2);
            END IF;

            IF IP.IDTRATIMPO = 'A' AND IP.IDACRPRODENTRNF = 'N' THEN
               V_VRIMPOSTONF := - ROUND(V_VRIMPOSTONF,2);
            END IF;
         END IF;

         IF C_NF.IDOPERDEVONF <> 'S' THEN
            V_VRLANCCORPO := V_VRLANCCORPO + (V_VRIMPOSTONF * (V_VRFATORMULT));
         END IF;
      END LOOP;

      /* DESCITEM */
     BEGIN
        SELECT NVL(ROUND(SUM(VRDESCITEM),2),0) AS VRITEMENCA, CDCTBLENTREN, CDCTBLSAIDEN
          INTO V_VRITEMENCA, V_DESCITEMCTBLENTR, V_DESCITEMCTBLSAID
          FROM ITEMPRODUTO I,
               (SELECT CDCONTDITEN AS CDCTBLENTREN,
                       CDCONTDITSA AS CDCTBLSAIDEN
                  FROM PARAMCTBL
                 WHERE DECODE('E', 'E', CDCONTDITEN, CDCONTDITSA) IS NOT NULL) E
         WHERE I.CDFILIAL   = C_NF.CDFILIAL
           AND I.NRLANCTONF = C_NF.NRLANCTONF
         GROUP BY CDCTBLENTREN, CDCTBLSAIDEN
        HAVING ROUND(SUM(VRDESCITEM),2) > 0;
     EXCEPTION
        WHEN NO_DATA_FOUND THEN
        V_VRITEMENCA:= 0;
        V_DESCITEMCTBLENTR := NULL;
        V_DESCITEMCTBLSAID := NULL;
     END;

      V_DBVRPARTIDAS := 0;

      BEGIN
        SELECT MAX(IT.NRSEQITPROD)
          INTO V_NRSEQEXIS
          FROM ITEMPRODUTO IT, PRODUTO P, PRODUTO PR
         WHERE CDFILIAL     = C_NF.CDFILIAL
           AND NRLANCTONF   = C_NF.NRLANCTONF
           AND VRCONTITEM   > 0
           AND (P.CDARVPROD = (SELECT MAX(CDARVPROD)
                                 FROM PRODUTO C
                                WHERE (INSTR(PR.CDARVPROD,RTRIM(C.CDARVPROD)) = 1)
                                  AND NOT (DECODE(C_NF.IDENTRSAIDOP, 'E', CDCTBLENTRPR, CDCTBLSAIDPR) IS NULL)) OR
               IT.CDPRODUTO = P.CDPRODUTO)
           AND IT.CDPRODUTO = PR.CDPRODUTO;
      EXCEPTION
        WHEN NO_DATA_FOUND THEN
         V_NRSEQEXIS := NULL;
      END;

      V_INPARTIDAS := 1;

      IF V_NRSEQEXIS IS NOT NULL THEN

         FOR C_ITEMPRODUTO IN (
            SELECT IT.NRSEQITPROD, IT.CDPRODUTO,
                   NVL(ROUND(VRCONTITEM,2),0) VRCONTITEM,
                   MAX(P.CDCTBLENTRPR) AS CDCTBLENTRPR,
                   MAX(P.CDCTBLSAIDPR) AS CDCTBLSAIDPR,
                   NVL(ROUND(VRDESCITEM,2),0) VRDESCITEM
              FROM ITEMPRODUTO IT, PRODUTO P, PRODUTO PR
             WHERE CDFILIAL     = C_NF.CDFILIAL
               AND NRLANCTONF   = C_NF.NRLANCTONF
               AND VRCONTITEM   > 0
               AND (P.CDARVPROD = (SELECT MAX(CDARVPROD)
                                     FROM PRODUTO C
                                    WHERE (INSTR(PR.CDARVPROD,RTRIM(C.CDARVPROD)) = 1)
                                      AND NOT (DECODE(C_NF.IDENTRSAIDOP, 'E', CDCTBLENTRPR, CDCTBLSAIDPR) IS NULL)) OR
                   IT.CDPRODUTO = P.CDPRODUTO)
               AND IT.CDPRODUTO = PR.CDPRODUTO
             GROUP BY IT.NRSEQITPROD, IT.CDPRODUTO, VRCONTITEM, VRDESCITEM) LOOP

            /* PRODUTO */
            BEGIN
              SELECT P.CDARVPROD, NVL(NVL(NVL(PR.CDCCUSTENTRF,P.CDCCUSTENTR),F.CDCENTCUST),E.CDCENTCUST) CDCENTCUST
                INTO V_CDARVPROD, V_CDCENTCUST
                FROM FILIAL F, NOTAFISCAL N, EMPRESA E,
                     PRODUTO P, PRODFILI PR
               WHERE (P.CDPRODUTO     = C_ITEMPRODUTO.CDPRODUTO)
                 AND (PR.CDPRODUTO(+) = P.CDPRODUTO)
                 AND (PR.CDFILIAL(+)  = C_NF.CDFILIRECE )
                 AND (F.CDFILIAL      = C_NF.CDFILIRECE )
                 AND (N.CDFILIAL      = C_NF.CDFILIAL)
                 AND (N.NRLANCTONF    = C_NF.NRLANCTONF)
                 AND (N.CDEMPRESA     = C_NF.CDEMPRESA)
                 AND (N.CDEMPRESA     = E.CDEMPRESA);
            EXCEPTION
              WHEN NO_DATA_FOUND THEN
               V_CDARVPROD := NULL;
               V_CDCENTCUST := NULL;
            END;

            BEGIN
              SELECT C.CDCTBLPRODTO
                INTO V_CDCCTBLPROD
                FROM CTBPRDTPOPER C,
                     (SELECT MAX(P.CDARVPROD) CDARVPROD
                        FROM CTBPRDTPOPER A, PRODUTO P
                       WHERE IDENTRSAIDOP = 'E'
                         AND CDTIPOOPER   = C_NF.CDTIPOOPER
                         AND P.CDPRODUTO  =  A.CDPRODUTO
                         AND (INSTR(V_CDARVPROD, RTRIM(P.CDARVPROD)) = 1)
                         AND A.CDCTBLPRODTO IS NOT NULL) PR,
                     PRODUTO P
               WHERE IDENTRSAIDOP = 'E'
                 AND CDTIPOOPER   = C_NF.CDTIPOOPER
                 AND C.CDPRODUTO  = P.CDPRODUTO
                 AND P.CDARVPROD = PR.CDARVPROD;
            EXCEPTION
              WHEN NO_DATA_FOUND THEN
               V_CDCCTBLPROD := NULL;
            END;

            IF V_CDCCTBLPROD IS NULL THEN
               V_CDCCTBLPROD := V_STCONTCTBLP;

               IF V_CDCCTBLPROD IS NULL THEN
                  /* PRODFILI */
                  BEGIN
                    SELECT MAX(CDCTBLENTRPF) AS CDCTBLENTRPF,
                           MAX(CDCTBLSAIDPF) AS CDCTBLSAIDPF
                      INTO V_CDCTBLENTRPF, V_CDCTBLSAIDPF
                      FROM PRODFILI PF, PRODUTO P
                     WHERE CDFILIAL      = C_NF.CDFILIRECE
                       AND PF.CDPRODUTO  = C_ITEMPRODUTO.CDPRODUTO
                       AND (P.CDPRODUTO IN (SELECT MAX(CDPRODUTO)
                                              FROM  PRODUTO C
                                             WHERE (INSTR(PF.CDPRODUTO,RTRIM(C.CDPRODUTO)) = 1)
                                               AND NOT (DECODE('E', 'E', CDCTBLENTRPF, CDCTBLSAIDPF) IS NULL))
                        OR PF.CDPRODUTO = P.CDPRODUTO);
                  EXCEPTION
                    WHEN NO_DATA_FOUND THEN
                     V_CDCTBLENTRPF := NULL;
                     V_CDCTBLSAIDPF := NULL;
                  END;

                  V_CDCCTBLPROD := V_CDCTBLENTRPF;
               END IF;
            END IF;

            IF V_CDCCTBLPROD IS NULL THEN
               V_CDCCTBLPROD := C_ITEMPRODUTO.CDCTBLENTRPR;
            END IF;

            IF C_ITEMPRODUTO.VRCONTITEM > 0 THEN
               V_DBVRCONTITEM := - (C_ITEMPRODUTO.VRCONTITEM * V_VRFATORMULT);
               V_DBVRPARTIDAS := V_DBVRPARTIDAS + (C_ITEMPRODUTO.VRCONTITEM * V_VRFATORMULT);

               BEGIN
                 SELECT COUNT(DISTINCT NRSEQITPROD)
                   INTO V_COUNTITEMPROD
                   FROM ITEMPRODUTO IT, PRODUTO P, PRODUTO PR
                  WHERE CDFILIAL     = C_NF.CDFILIAL
                    AND NRLANCTONF   = C_NF.NRLANCTONF
                    AND VRCONTITEM   > 0
                    AND (P.CDARVPROD = (SELECT MAX(CDARVPROD)
                                          FROM PRODUTO C
                                         WHERE (INSTR(PR.CDARVPROD,RTRIM(C.CDARVPROD)) = 1)
                                           AND NOT (DECODE(C_NF.IDENTRSAIDOP, 'E', CDCTBLENTRPR, CDCTBLSAIDPR) IS NULL)) OR
                        IT.CDPRODUTO = P.CDPRODUTO)
                    AND IT.CDPRODUTO = PR.CDPRODUTO;
               EXCEPTION
                 WHEN NO_DATA_FOUND THEN
                  V_COUNTITEMPROD := NULL;
               END;

               IF V_INPARTIDAS = V_COUNTITEMPROD THEN
                   IF (Abs(Round(ABS(V_DBVRPARTIDAS),2) - Round(ABS(V_VRLANCCORPO),2)) <= 0.1) AND
                      (Round(ABS(V_DBVRPARTIDAS),2) <> Round(ABS(V_VRLANCCORPO),2)) THEN
                       V_DBVRCONTITEM := V_DBVRCONTITEM - (V_VRLANCCORPO - V_DBVRPARTIDAS);
                   END IF;
               END IF;

               NOVA_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE, V_CDCCTBLPROD, V_CDHISTPADR,
                            V_DSHISTPADR, V_DBVRCONTITEM, V_IDLANCCOME, V_CDCONTCOME, '00', C_NF.NRNOTAFISC, 'S');

               BEGIN
                 SELECT MAX(CDFILICCUST)
                   INTO V_CDFILICCUST
                   FROM RATEIONF
                  WHERE CDFILIAL = C_NF.CDFILIAL
                    AND NRLANCTONF = C_NF.NRLANCTONF
                    AND NRSEQITPROD = C_ITEMPRODUTO.NRSEQITPROD;
               EXCEPTION
                 WHEN NO_DATA_FOUND THEN
                  V_CDFILICCUST := NULL;
               END;

               IF V_CDFILICCUST IS NOT NULL THEN
                  V_RATEIO := 1;

                  FOR C_RATEIONF IN (
                      SELECT CDFILICCUST, CDCENTCUSNF,
                             NVL(ROUND(VRRATENF,2),0) AS VRRATENF
                        FROM RATEIONF
                       WHERE CDFILIAL    = C_NF.CDFILIAL
                         AND NRLANCTONF  = C_NF.NRLANCTONF
                         AND NRSEQITPROD = C_ITEMPRODUTO.NRSEQITPROD) LOOP

                      V_VRRATEIO := -(C_RATEIONF.VRRATENF*(V_VRFATORMULT));
                      V_VRPARTRATE := V_VRPARTRATE + (C_RATEIONF.VRRATENF*(V_VRFATORMULT));

                      BEGIN
                        SELECT COUNT(*)
                          INTO V_COUNTRATEIO
                          FROM RATEIONF
                         WHERE CDFILIAL = C_NF.CDFILIAL
                           AND NRLANCTONF = C_NF.NRLANCTONF
                           AND NRSEQITPROD = C_ITEMPRODUTO.NRSEQITPROD;
                      EXCEPTION
                       WHEN NO_DATA_FOUND THEN
                        V_COUNTRATEIO := NULL;
                      END;

                      IF V_RATEIO = V_COUNTRATEIO THEN
                         IF (Abs(Round(ABS(V_VRPARTRATE),2) - Round(ABS(V_DBVRCONTITEM),2)) <= 0.1) AND
                            (Round(ABS(V_VRPARTRATE),2) <> Round(ABS(V_DBVRCONTITEM),2)) THEN
                            V_VRRATEIO := V_VRRATEIO + (V_VRPARTRATE + V_DBVRCONTITEM);
                         END IF;
                      END IF;

                      RATEIO_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE,C_RATEIONF.CDFILICCUST,
                                     C_RATEIONF.CDCENTCUSNF,V_VRRATEIO);
                  END LOOP;
              ELSE
                 RATEIO_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE,C_NF.CDFILIRECE,
                                V_CDCENTCUST,V_DBVRCONTITEM);
              END IF;
            END IF;

            IF (C_ITEMPRODUTO.VRDESCITEM > 0) AND (V_DESCITEMCTBLENTR IS NOT NULL) THEN

                NOVA_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE, V_CDCCTBLPROD, V_CDHISTPADR,
                             V_DSHISTPADR,- C_ITEMPRODUTO.VRDESCITEM, V_IDLANCCOME, V_CDCONTCOME, '00', C_NF.NRNOTAFISC, 'S');

                RATEIO_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE,C_NF.CDFILIRECE,
                               V_CDCENTCUST,- C_ITEMPRODUTO.VRDESCITEM);
            END IF;

            V_INPARTIDAS := V_INPARTIDAS + 1;
            V_CDCCTBLPROD := '';
         END LOOP;
      ELSE
         V_CDCCTBLPROD := V_STCONTCTBLP;

         NOVA_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE, V_CDCCTBLPROD, V_CDHISTPADR,
                      V_DSHISTPADR,- (V_VRLANCCORPO * V_VRFATORMULT) , V_IDLANCCOME, V_CDCONTCOME, '00', C_NF.NRNOTAFISC, 'S');

         RATEIO_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE,C_NF.CDFILIRECE,
                        V_CDCENTCUST,- (V_VRLANCCORPO * V_VRFATORMULT));
      END IF;

       FOR IE IN (
         SELECT NVL(VRITEMENCA,0) AS VRITEMENCA, CDCTBLENTREN, CDCTBLSAIDEN, IDENCARGO
           FROM ITEMENCARGO IE, ENCARGO E
          WHERE IE.CDFILIAL = C_NF.CDFILIAL
            AND IE.NRLANCTONF = C_NF.NRLANCTONF
            AND IE.CDENCARGO = E.CDENCARGO
            AND IE.VRITEMENCA > 0
            AND E.IDENCARGO IN ('A','D')
            AND (DECODE('E', 'E', CDCTBLENTREN, CDCTBLSAIDEN) IS  NOT NULL)) LOOP

            IF IE.IDENCARGO = 'A' THEN
               NOVA_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE, IE.CDCTBLENTREN, V_CDHISTPADR,
                            V_DSHISTPADR,- (IE.VRITEMENCA * V_VRFATORMULT) , V_IDLANCCOME, V_CDCONTCOME, '00', C_NF.NRNOTAFISC, 'S');

               RATEIO_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE,C_NF.CDFILIRECE,
                              V_CDCENTCUST,- (IE.VRITEMENCA * V_VRFATORMULT));
            ELSE
               NOVA_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE, IE.CDCTBLENTREN, V_CDHISTPADR,
                            V_DSHISTPADR, (IE.VRITEMENCA * V_VRFATORMULT) , V_IDLANCCOME, V_CDCONTCOME, '00', C_NF.NRNOTAFISC, 'S');

               RATEIO_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE,C_NF.CDFILIRECE,
                              V_CDCENTCUST, (IE.VRITEMENCA * V_VRFATORMULT));
            END IF;
       END LOOP;

       FOR C_DESCITEM IN (
          SELECT NVL(ROUND(SUM(VRDESCITEM),2),0) AS VRITEMENCA, CDCTBLENTREN, CDCTBLSAIDEN
            FROM ITEMPRODUTO I,
                 (SELECT CDCONTDITEN AS CDCTBLENTREN,
                  CDCONTDITSA AS CDCTBLSAIDEN
                  FROM PARAMCTBL
                  WHERE DECODE('E', 'E', CDCONTDITEN, CDCONTDITSA) IS NOT NULL) E
           WHERE I.CDFILIAL   = C_NF.CDFILIAL
             AND I.NRLANCTONF = C_NF.NRLANCTONF
           GROUP BY CDCTBLENTREN, CDCTBLSAIDEN
          HAVING ROUND(SUM(VRDESCITEM),2) > 0) LOOP

          NOVA_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE, C_DESCITEM.CDCTBLENTREN, V_CDHISTPADR,
                       V_DSHISTPADR, C_DESCITEM.VRITEMENCA , V_IDLANCCOME, V_CDCONTCOME, '00', C_NF.NRNOTAFISC, 'S');

          RATEIO_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE,C_NF.CDFILIRECE,
                         V_CDCENTCUST, C_DESCITEM.VRITEMENCA);
       END LOOP;

       FOR IMP IN (
         SELECT A.CDIMPOSTO, NVL(ROUND(SUM(A.VRIMPOSTONF),2),0) AS VRIMPOSTONFE,
                A.IDTRATIMPO, A.IDIMPORECUP, A.IDACRPRODENTRNF,
                MAX(CDCONTATIV) AS CDCONTATIV, MAX(CDCONTPASS)  AS CDCONTPASS,
                MAX(CDCONTDESP)  AS CDCONTDESP, MAX(CDCONTIMPO) AS CDCONTIMPO
           FROM (SELECT INF.CDIMPOSTO, VRIMPOSTONF, NVL(IDACRPRODENTRNF, 'N') IDACRPRODENTRNF,
                        PF.IDCARACFIS, I.IDIMPORECUP, I.IDTRATIMPO, INF.CDFISCALNF,
                        CONTCTBL_IMPOSTO(I.CDIMPOSTO, 'A', NF.IDENTRSAIDOP, NF.CDTIPOOPER, NF.IDTPIJURNF) CDCONTATIV,
                        CONTCTBL_IMPOSTO(I.CDIMPOSTO, 'P', NF.IDENTRSAIDOP, NF.CDTIPOOPER, NF.IDTPIJURNF) CDCONTPASS,
                        CONTCTBL_IMPOSTO(I.CDIMPOSTO, 'D', NF.IDENTRSAIDOP, NF.CDTIPOOPER, NF.IDTPIJURNF) CDCONTDESP,
                        CONTCTBL_IMPOSTO(I.CDIMPOSTO, 'E', NF.IDENTRSAIDOP, NF.CDTIPOOPER, NF.IDTPIJURNF) CDCONTIMPO
                   FROM IMPOSTONF INF, IMPOSTO I, NOTAFISCAL NF, PARAMFILIAL PF
                  WHERE NF.CDFILIAL     = C_NF.CDFILIAL
                    AND NF.NRLANCTONF   = C_NF.NRLANCTONF
                    AND PF.CDFILIAL(+)  = NF.CDFILIRECE
                    AND INF.CDFILIAL    = C_NF.CDFILIAL
                    AND INF.NRLANCTONF  = C_NF.NRLANCTONF
                    AND INF.CDIMPOSTO   = I.CDIMPOSTO
                    AND INF.VRIMPOSTONF > 0
                    AND ((IDDIFALIQCUSPRN = 'S' AND INF.IDIMPOSTONF IN ('1','4','3')) OR (IDDIFALIQCUSPRN = 'N' AND INF.IDIMPOSTONF IN ('1','4')))
                    AND I.IDTIPCONTAB  = 'D') A
          WHERE ((((A.IDIMPORECUP  = 'S' AND A.CDFISCALNF = '1' AND NVL(A.IDCARACFIS,'C') = 'C') OR (A.IDCARACFIS = 'A')) AND (A.CDCONTATIV IS NOT NULL)) OR
                 ((A.IDIMPORECUP <> 'S') AND (A.CDCONTPASS IS NOT NULL)))
          GROUP BY A.CDIMPOSTO, A.IDTRATIMPO,  A.IDIMPORECUP, A.IDACRPRODENTRNF) LOOP

           IF ((C_NF.IDOPERDEVONF = 'S') OR (C_NF.CDTIPOOPER = '16')) THEN
              IF IMP.IDTRATIMPO = 'V' THEN
                 V_VRIMPOSTONF := ROUND(IMP.VRIMPOSTONFE,2);
              ELSE
                 V_VRIMPOSTONF := - ROUND(IMP.VRIMPOSTONFE,2);
              END IF;

              IF IMP.IDTRATIMPO = 'A' AND IMP.IDACRPRODENTRNF = 'N' THEN
                 V_VRIMPOSTONF := ROUND(V_VRIMPOSTONF,2);
              END IF;
           ELSE
              IF IMP.IDTRATIMPO = 'V' THEN
                 V_VRIMPOSTONF := - ROUND(IMP.VRIMPOSTONFE,2);
              ELSE
                 V_VRIMPOSTONF := ROUND(IMP.VRIMPOSTONFE,2);
              END IF;

              IF IMP.IDTRATIMPO = 'A' AND IMP.IDACRPRODENTRNF = 'N' THEN
                 V_VRIMPOSTONF := - ROUND(V_VRIMPOSTONF,2);
              END IF;
           END IF;

           IF C_NF.IDOPERDEVONF <> 'S' THEN
              V_VRLANCCORPO := V_VRLANCCORPO + (V_VRIMPOSTONF * (V_VRFATORMULT));
           END IF;

           IF ((C_NF.IDOPERDEVONF = 'S') OR (C_NF.CDTIPOOPER = '16')) THEN
              BEGIN
                SELECT MAX(CDIMPOSTO) AS CDIMPOSTRIB
                  INTO V_CDIMPOSTRIB
                  FROM IMPOSTO
                 WHERE CDIMPOSTRIB = IMP.CDIMPOSTO;
              EXCEPTION
                WHEN NO_DATA_FOUND THEN
                 V_CDIMPOSTRIB := NULL;
              END;

              IF V_CDIMPOSTRIB IS NOT NULL THEN
                 NOVA_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE, IMP.CDCONTATIV, V_CDHISTPADR,
                              V_DSHISTPADR, - ROUND((V_VRIMPOSTONF),2) , V_IDLANCCOME, V_CDCONTCOME, '00', C_NF.NRNOTAFISC, 'S');

                 RATEIO_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE,C_NF.CDFILIEMIT,
                                V_CDCENTCUST, - ROUND((V_VRIMPOSTONF),2));

                 NOVA_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE, IMP.CDCONTATIV, V_CDHISTPADR,
                              V_DSHISTPADR,ROUND((V_VRIMPOSTONF),2) , V_IDLANCCOME, V_CDCONTCOME, '00', C_NF.NRNOTAFISC, 'S');

                 RATEIO_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE,C_NF.CDFILIEMIT,
                                V_CDCENTCUST,ROUND((V_VRIMPOSTONF),2));
              ELSE

                 IF (IMP.IDTRATIMPO = 'R') THEN
                    NOVA_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE, IMP.CDCONTATIV, V_CDHISTPADR,
                                 V_DSHISTPADR, - (V_VRIMPOSTONF) , V_IDLANCCOME, V_CDCONTCOME, '00', C_NF.NRNOTAFISC, 'S');

                    RATEIO_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE,C_NF.CDFILIEMIT,
                                   V_CDCENTCUST, - (V_VRIMPOSTONF));
                 ELSE IF (IMP.IDTRATIMPO = 'A') THEN
                          NOVA_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE, IMP.CDCONTPASS, V_CDHISTPADR,
                                       V_DSHISTPADR, V_VRIMPOSTONF , V_IDLANCCOME, V_CDCONTCOME, '00', C_NF.NRNOTAFISC, 'S');

                          RATEIO_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE,C_NF.CDFILIRECE,
                                         V_CDCENTCUST, V_VRIMPOSTONF);
                      ELSE
                          NOVA_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE, IMP.CDCONTDESP, V_CDHISTPADR,
                                       V_DSHISTPADR, V_VRIMPOSTONF , V_IDLANCCOME, V_CDCONTCOME, '00', C_NF.NRNOTAFISC, 'S');

                          RATEIO_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE,C_NF.CDFILIRECE,
                                         V_CDCENTCUST, V_VRIMPOSTONF);

                          NOVA_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE, IMP.CDCONTATIV, V_CDHISTPADR,
                                       V_DSHISTPADR,- V_VRIMPOSTONF, V_IDLANCCOME, V_CDCONTCOME, '00', C_NF.NRNOTAFISC, 'S');

                          RATEIO_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE,C_NF.CDFILIRECE,
                                         V_CDCENTCUST,- V_VRIMPOSTONF);
                      END IF;
                 END IF;
              END IF;
           ELSE
              BEGIN
                SELECT MAX(CDIMPOSTO) AS CDIMPOSTRIB
                  INTO V_CDIMPOSTRIB
                  FROM IMPOSTO
                 WHERE CDIMPOSTRIB = IMP.CDIMPOSTO;
              EXCEPTION
                WHEN NO_DATA_FOUND THEN
                 V_CDIMPOSTRIB := NULL;
              END;

              BEGIN
                 SELECT A.CDMODDOCFISC, A.DSMODDOCFISC,
                        A.CDTPREGFISC, N.NRLANCTONF,
                        N.CDFILIAL
                   INTO V_CDMODDOCFISC, V_DSMODDOCFISC,
                        V_CDTPREGFISC, V_NRLANCTONFMOD,
                        V_CDFILIALMOD
                   FROM NOTAFISCAL N, MODDOCFISC A
                  WHERE (N.CDMODDOCFISC = A.CDMODDOCFISC)
                    AND (A.CDTPREGFISC  = '70')
                    AND (N.CDFILIAL     = C_NF.CDFILIAL)
                    AND (N.NRLANCTONF   = C_NF.NRLANCTONF)
                  ORDER BY A.CDMODDOCFISC;
              EXCEPTION
                 WHEN NO_DATA_FOUND THEN
                    V_CDMODDOCFISC := NULL;
                    V_DSMODDOCFISC := NULL;
                    V_CDTPREGFISC  := NULL;
                    V_NRLANCTONFMOD := NULL;
                    V_CDFILIALMOD := NULL;
              END;

              IF (V_CDIMPOSTRIB IS NOT NULL) AND (V_NRLANCTONFMOD IS NOT NULL) THEN
                 NOVA_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE, IMP.CDCONTATIV, V_CDHISTPADR,
                              V_DSHISTPADR, V_VRIMPOSTONF , V_IDLANCCOME, V_CDCONTCOME, '00', C_NF.NRNOTAFISC, 'S');

                 RATEIO_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE,C_NF.CDFILIEMIT,
                                C_NF.CDCENTCUST, ROUND(V_VRIMPOSTONF,2));

                 NOVA_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE, IMP.CDCONTPASS, V_CDHISTPADR,
                              V_DSHISTPADR,- ROUND(V_VRIMPOSTONF,2), V_IDLANCCOME, V_CDCONTCOME, '00', C_NF.NRNOTAFISC, 'S');

                 RATEIO_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE,C_NF.CDFILIEMIT,
                                 C_NF.CDCENTCUST,-ROUND(V_VRIMPOSTONF,2));

                 BEGIN
                   SELECT NVL(VRPECREPREST,0) AS VRPECREPREST
                     INTO V_VRPECREPREST
                     FROM FILIAL
                    WHERE CDFILIAL = C_NF.CDFILIRECE;
                 EXCEPTION
                    WHEN NO_DATA_FOUND THEN
                       V_VRPECREPREST := 0;
                 END;

                 IF (V_VRPECREPREST > 0) THEN
                     NOVA_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE, IMP.CDCONTATIV, V_CDHISTPADR,
                                  V_DSHISTPADR, - ROUND(V_VRIMPOSTONF,2) * (V_VRPECREPREST/100)  , V_IDLANCCOME, V_CDCONTCOME, '00', C_NF.NRNOTAFISC, 'S');

                     RATEIO_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE,C_NF.CDFILIEMIT,
                                    C_NF.CDCENTCUST, ROUND(V_VRIMPOSTONF,2));

                     NOVA_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE, IMP.CDCONTPASS, V_CDHISTPADR,
                                  V_DSHISTPADR, ROUND(V_VRIMPOSTONF,2) * (V_VRPECREPREST/100), V_IDLANCCOME, V_CDCONTCOME, '00', C_NF.NRNOTAFISC, 'S');

                     RATEIO_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE,C_NF.CDFILIEMIT,
                                    C_NF.CDCENTCUST,-ROUND(V_VRIMPOSTONF,2));
                 END IF;
              ELSE
                NOVA_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE, IMP.CDCONTIMPO, V_CDHISTPADR,
                             V_DSHISTPADR, (V_VRIMPOSTONF * V_VRFATORMULT), V_IDLANCCOME, V_CDCONTCOME, '00', C_NF.NRNOTAFISC, 'S');

                RATEIO_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTENTRSAID,V_NRLANCLOTE,C_NF.CDFILIRECE,
                                V_CDCENTCUST, (V_VRIMPOSTONF * V_VRFATORMULT));
              END IF;
            END IF;
         END LOOP;
      END IF;

      BEGIN
         SELECT F.VRPEDESCFORN, F.IDTPDESCFORN,
                T.CDHISTPADR,  T.CDCONTCTBL, T.DSAJUCTPAG, T.IDAJUCTPAG,
                H.DSHISTPADR, DTINIVRPDESC
           INTO V_VRPEDESCFORN, V_IDTPDESCFORN, V_CDHISTPADRFOR, V_CDCONTCTBLFOR,
                V_DSAJUCTPAG, V_IDAJUCTPAG, V_DSHISTPADRFOR, V_DTINIVRPDESC
           FROM FORNECEDOR F, TIPOAJPAGAR T, HISTPADR H
          WHERE (F.CDFORNECED   = C_NF.CDFORNECED) AND
                (NOT F.VRPEDESCFORN IS NULL)     AND
                (F.IDTPDESCFORN = 'B')           AND
                (T.CDAJUCTPAG   = F.CDAJUCTPAG)  AND
                (H.CDHISTPADR   = T.CDHISTPADR);
      EXCEPTION
        WHEN NO_DATA_FOUND THEN
          V_VRPEDESCFORN := NULL;
          V_IDTPDESCFORN := NULL;
          V_CDHISTPADR   := NULL;
          V_CDCONTCTBL   := NULL;
          V_DSAJUCTPAG   := NULL;
          V_IDAJUCTPAG   := NULL;
          V_DSHISTPADR   := NULL;
          V_DTINIVRPDESC := NULL;
      END;

      BEGIN
         SELECT NVL(IDCONDESBONFORN,'S') IDCONDESBONFORN
           INTO V_IDCONDESBONFORN
           FROM PARAMFILIAL
          WHERE CDFILIAL = C_NF.CDFILIEMIT;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
           V_IDCONDESBONFORN := NULL;
      END;

      IF (NOT V_CDHISTPADRFOR IS NULL) AND (V_IDCONDESBONFORN = 'S') THEN
         IF V_CDHISTPADR IS NULL THEN
            V_CDHISTPADR := V_CDHISTPADRFOR;
         END IF;

         FOR C_AJUSTEPAG IN(
            SELECT DTORIGVENPAG, VRORIGPAG
              FROM CONTAPAGAR
             WHERE CDFILIAL   = C_NF.CDFILIAL AND
                   NRLANCTONF = C_NF.NRLANCTONF) LOOP

             IF C_AJUSTEPAG.DTORIGVENPAG >= V_DTINIVRPDESC THEN
                V_VRDESCBONI := ROUND(((C_AJUSTEPAG.VRORIGPAG * V_VRPEDESCFORN)/100) * V_VRFATORMULT,2);
             ELSE
                V_VRDESCBONI := 0;
             END IF;

             BEGIN
                SELECT NVL(IDCONTAJUDTCOMP, 'N') IDCONTAJUDTCOMP
                  INTO V_IDCONTAJUDTCOMP
                  FROM PARAMGERAL;
             EXCEPTION
              WHEN NO_DATA_FOUND THEN
                V_IDCONTAJUDTCOMP := NULL;
             END;

                MONTA_HISTORICO(C_NF.CDFILIAL,C_NF.NRLANCTONF,V_CDHISTPADR,C_NF.NRNOTAFISC,C_NF.DTEMISSAO, '00', '', V_DSHISTPADR);

                IF V_IDAJUCTPAG = 'D' THEN
                   NOVA_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE, C_NF.DTEMISSAO, V_NRLANCLOTE, V_STCONTCTBL, V_CDHISTPADR, V_DSHISTPADR, -V_VRDESCBONI ,
                            V_IDLANCCOME, V_CDCONTCOME, '00', C_NF.NRNOTAFISC, 'S');

                   RATEIO_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTEMISSAO,V_NRLANCLOTE, C_NF.CDFILIRECE, C_NF.CDCENTCUST, -V_VRDESCBONI);

                   NOVA_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE, C_NF.DTEMISSAO, V_NRLANCLOTE, V_CDCONTCTBLFOR, V_CDHISTPADR, V_DSHISTPADR, V_VRDESCBONI ,
                            V_IDLANCCOME, V_CDCONTCOME, '00', C_NF.NRNOTAFISC, 'S');

                   RATEIO_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTEMISSAO,V_NRLANCLOTE, C_NF.CDFILIRECE, C_NF.CDCENTCUST,  V_VRDESCBONI);
                END IF;
                IF V_IDAJUCTPAG = 'A' THEN
                   NOVA_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE, C_NF.DTEMISSAO, V_NRLANCLOTE, V_STCONTCTBL, V_CDHISTPADR, V_DSHISTPADR, V_VRDESCBONI ,
                            V_IDLANCCOME, V_CDCONTCOME, '00', C_NF.NRNOTAFISC, 'S');

                   RATEIO_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTEMISSAO,V_NRLANCLOTE, C_NF.CDFILIRECE, C_NF.CDCENTCUST,  V_VRDESCBONI);

                   NOVA_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE, C_NF.DTEMISSAO, V_NRLANCLOTE, V_STCONTCTBL, V_CDHISTPADR, V_DSHISTPADR, -V_VRDESCBONI ,
                            V_IDLANCCOME, V_CDCONTCOME, '00', C_NF.NRNOTAFISC, 'S');

                   RATEIO_PARTIDA(C_NF.CDEMPRESA,V_CDLOTE,C_NF.DTEMISSAO,V_NRLANCLOTE, C_NF.CDFILIRECE, C_NF.CDCENTCUST,  -V_VRDESCBONI);
                END IF;
         END LOOP;
      END IF;

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

      IF CONFERE_PARTIDA(C_NF.CDEMPRESA, V_CDLOTE, V_NRLANCLOTE,C_NF.DTENTRSAID) THEN
         UPDATE NOTAFISCAL SET IDSTATUSCTBNF = 'C'
          WHERE CDFILIAL = C_NF.CDFILIAL
            AND NRLANCTONF = C_NF.NRLANCTONF;
      ELSE
         UPDATE NOTAFISCAL SET IDSTATUSCTBNF = 'E'
          WHERE CDFILIAL = C_NF.CDFILIAL
            AND NRLANCTONF = C_NF.NRLANCTONF;
      END IF;
   END LOOP;

   DELETE FROM LISTA
      WHERE NRLISTA = P_NRLISTA;
END;
/

