CREATE OR REPLACE PROCEDURE CTB_DFE_IMPOSTO(
   P_NRLANCTBTEMP IN LANCCONT.NRLANCCONT%TYPE,
   P_CDFILIAL IN FILIAL.CDFILIAL%TYPE,
   P_NRLANCTONF IN NOTAFISCAL.NRLANCTONF%TYPE,
   P_FATOR IN INT,
   P_VRLANCTO IN OUT LANCCONT.VRLANCAMENT%TYPE,
   P_FATORMULT IN INT)
AS
   V_COUNT INT;
   V_NRSEQLANC LANCCONT.NRSEQLANC%TYPE;
   V_DIFALIQ LANCCONT.VRLANCAMENT%TYPE;
   V_CDHISTPADR HISTPADR.CDHISTPADR%TYPE;
   V_DSHISTLANC LANCCONT.DSHISTLANC%TYPE;
   V_NUMPARTIDA INT;
   V_IDCOME VARCHAR2(01);
   V_IDCCUS VARCHAR2(01);
   V_CDCOMERCIAL FORNECEDOR.CDFORNECED%TYPE;
   V_CDCENTCUST CENTCUST.CDCENTCUST%TYPE;
   V_CDCENTCUSTFIL CENTCUST.CDCENTCUST%TYPE;
   V_CDFILIAL FILIAL.CDFILIAL%TYPE;
   V_CONTCCUS VARCHAR2(01);
   V_ACUMULA VARCHAR2(01);
   V_CONTAANTERIOR CONTCTBL.CDCONTCTBL%TYPE;
   V_CONTAATIV CONTCTBL.CDCONTCTBL%TYPE;
   V_CONTAPASS CONTCTBL.CDCONTCTBL%TYPE;
   V_CONTADESP CONTCTBL.CDCONTCTBL%TYPE;
   V_CDIMPOSTRIB IMPOSTO.CDIMPOSTO%TYPE;
   V_VRIMPOSTO LANCCONT.VRLANCAMENT%TYPE;
   V_CONTA CONTCTBL.CDCONTCTBL%TYPE;
   V_CONTATIPOOPER CONTCTBL.CDCONTCTBL%TYPE;
   V_CDARVPROD PRODUTO.CDARVPROD%TYPE;
   V_CDMODFISCAL MODDOCFISC.CDMODDOCFISC%TYPE;
   V_ENTROULOOP VARCHAR2(01);
   V_VRRATEIO LANCCONT.VRLANCAMENT%TYPE;
   V_VRPARTIDA LANCCONT.VRLANCAMENT%TYPE;
   PROCEDURE NUMEROPARTIDA(P_CDEMPRESA IN EMPRESA.CDEMPRESA%TYPE,
                           P_IDORIGDESTNF IN NOTAFISCAL.IDORIGDESTNF%TYPE,
                           P_CDFORNECED IN FORNECEDOR.CDFORNECED%TYPE,
                           P_CDCLIENTE IN CLIENTE.CDCLIENTE%TYPE)
   AS
   BEGIN
      IF V_NUMPARTIDA IS NULL THEN
         /* NUMERO DA PARTIDA */
         BEGIN
            SELECT TO_NUMBER(MAX(NRSEQCTBTEMP))
              INTO V_NUMPARTIDA
              FROM LANCCTBTEMP
             WHERE NRLANCCTBTEMP = P_NRLANCTBTEMP
               AND CDEMPRESA = P_CDEMPRESA;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_NUMPARTIDA := 0;
         END;

         /* CONTROLA SE GERA OS RATEIOS - CONTROLA SE AS PARTIDAS ACUMULAM */
         SELECT IDCONTGERE, IDACUMVRCONT
           INTO V_CONTCCUS, V_ACUMULA
           FROM CONTOPECTBL
          WHERE CDEMPRESA = P_CDEMPRESA;

         /* BUSCA O CODIGO COMERCIAL */
         BEGIN
            SELECT DECODE(P_IDORIGDESTNF, 'F', P_CDFORNECED, DECODE(P_IDORIGDESTNF, 'C', P_CDCLIENTE, NULL))
              INTO V_CDCOMERCIAL
              FROM DUAL;
          EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_CDCOMERCIAL := NULL;
         END;
      END IF;
   END;

   FUNCTION CONTAIMPOSTOFILIAL(P_CDFILIAL IN NOTAFISCAL.CDFILIAL%TYPE,
                               P_IDENTRSAIDOP IN NOTAFISCAL.IDENTRSAIDOP%TYPE,
                               P_CDTIPOOPER IN NOTAFISCAL.CDTIPOOPER%TYPE,
                               P_CDIMPOSTO IN IMPOSTO.CDIMPOSTO%TYPE,
                               P_CDCONTCTBL IN CONTCTBL.CDCONTCTBL%TYPE,
                               P_TIPOCONTA IN CONTCTBL.IDTIPOCONT%TYPE)
   RETURN VARCHAR
   AS
   BEGIN
      BEGIN
         SELECT DECODE(P_TIPOCONTA, 'A', CDCONTATIVO, DECODE(P_TIPOCONTA, 'P', CDCONTPASSIVO,
                DECODE(P_TIPOCONTA, 'D', CDCONTDESPESA, DECODE(P_TIPOCONTA, 'T', CDCONTDESPTRIB, '')))) CONTA
           INTO V_CONTA
           FROM PARAMIMPOSTOFIL
          WHERE CDFILIAL     = P_CDFILIAL
            AND IDENTRSAIDOP = P_IDENTRSAIDOP
            AND CDTIPOOPER   = P_CDTIPOOPER
            AND CDIMPOSTO    = P_CDIMPOSTO;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_CONTA := NULL;
      END;

      IF V_CONTA IS NOT NULL THEN
         RETURN V_CONTA;
      ELSE
         RETURN P_CDCONTCTBL;
      END IF;
   END;

   PROCEDURE PARTIDA(P_CDEMPRESA IN EMPRESA.CDEMPRESA%TYPE,
                     P_CDFILIAL IN FILIAL.CDFILIAL%TYPE,
                     P_CDFILIRECE IN FILIAL.CDFILIAL%TYPE,
                     P_CDFILIEMIT IN FILIAL.CDFILIAL%TYPE,
                     P_NRLANCTONF IN NOTAFISCAL.NRLANCTONF%TYPE,
                     P_NRNOTAFISC IN NOTAFISCAL.NRNOTAFISC%TYPE,
                     P_IDENTRSAID IN NOTAFISCAL.IDENTRSAIDOP%TYPE,
                     P_IDORIGDESTNF IN NOTAFISCAL.IDORIGDESTNF%TYPE,
                     P_CDFORNECED IN NOTAFISCAL.CDFORNECED%TYPE,
                     P_CDCLIENTE IN NOTAFISCAL.CDCLIENTE%TYPE,
                     P_DTENTRSAID IN NOTAFISCAL.DTENTRSAID%TYPE,
                     P_SINAL IN INT,
                     P_CDCONTCTBL IN CONTCTBL.CDCONTCTBL%TYPE,
                     P_IDFILIAL IN CONTCTBL.IDTIPOCONT%TYPE,
                     P_VRPARTIDA IN LANCCONT.VRLANCAMENT%TYPE)
   AS
   BEGIN
      BUSCA_TPCOMERCIAL(P_CDCONTCTBL, V_IDCOME, V_IDCCUS);
      BEGIN
         SELECT COUNT(*)
           INTO V_COUNT
           FROM LANCCTBTEMP
          WHERE CDEMPRESA = P_CDEMPRESA
            AND NRLANCCTBTEMP = P_NRLANCTBTEMP
            AND CDCONTCTBL = P_CDCONTCTBL;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_COUNT := 0;
      END;

      /* CASO NAO EXISTA PARTIDA PARA A CONTA CONTABIL, E FEITA UMA */
      IF V_COUNT = 0 THEN
         V_NUMPARTIDA := V_NUMPARTIDA + 1;
         IF NVL(V_IDCOME, 'N') = 'N' THEN
            V_CDCOMERCIAL := NULL;
         END IF;
         INSERT INTO LANCCTBTEMP
            (CDEMPRESA, NRLANCCTBTEMP, CDDOCUTEMP, NRSEQCTBTEMP,
             CDCONTCTBL, CDHISTPADR,    DSHISTCTB, DTLANCCTBTEMP,
             VRLANCCTBTEMP, IDLANCCTBCOME, CDLANCCTBCOME, IDSISTEMA)
         VALUES
            (P_CDEMPRESA, P_NRLANCTBTEMP, P_NRNOTAFISC, LPAD(TO_CHAR(V_NUMPARTIDA),5,'0'),
             P_CDCONTCTBL, V_CDHISTPADR, V_DSHISTLANC, P_DTENTRSAID, P_SINAL * (ROUND(P_VRPARTIDA, 2) * P_FATORMULT), V_IDCOME, V_CDCOMERCIAL, 'DFE');

         IF V_CONTCCUS <> 'N' THEN
            IF V_IDCCUS <> 'O' THEN
               INSERT INTO RATECTBTEMP
                  (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL,
                   CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
               VALUES
                  (P_CDEMPRESA, P_NRLANCTBTEMP, LPAD(TO_CHAR(V_NUMPARTIDA),5,'0'), DECODE(P_IDFILIAL, 'E', P_CDFILIRECE, P_CDFILIEMIT),
                   V_CDCENTCUST, P_SINAL * (ROUND(P_VRPARTIDA, 2) * P_FATORMULT), P_DTENTRSAID, '00001');
            END IF;
         END IF;
      ELSE

         /* CASO EXISTA ALGUMA PARTIDA, O VALOR DA MESMA E SOMENTE ATUALIZADO. */
         UPDATE LANCCTBTEMP
            SET VRLANCCTBTEMP = VRLANCCTBTEMP + (P_SINAL * (ROUND(P_VRPARTIDA, 2) * P_FATORMULT))
          WHERE CDEMPRESA = P_CDEMPRESA
            AND NRLANCCTBTEMP = P_NRLANCTBTEMP
            AND CDCONTCTBL = P_CDCONTCTBL;

         /* SE A EMPRESA USAR A CONTABILIDADE GERENCIAL E A CONTA FOR DIFERENTE DE SO OFICIAL, A ROTINA
            DE RATEIOS E FEITA */
         IF V_CONTCCUS <> 'N' THEN
            IF V_IDCCUS <> 'O' THEN
               BEGIN
                  SELECT COUNT(*), L.NRSEQCTBTEMP
                    INTO V_COUNT, V_NRSEQLANC
                    FROM LANCCTBTEMP L, RATECTBTEMP R
                   WHERE L.CDEMPRESA = P_CDEMPRESA
                     AND L.NRLANCCTBTEMP = P_NRLANCTBTEMP
                     AND L.CDEMPRESA = R.CDEMPRESA
                     AND L.NRSEQCTBTEMP = R.NRSEQCTBTEMP
                     AND L.CDCONTCTBL = P_CDCONTCTBL
                     AND R.CDFILIAL = DECODE(P_IDFILIAL, 'E', P_CDFILIRECE, P_CDFILIEMIT)
                     AND R.CDCENTCUST = V_CDCENTCUST
                   GROUP BY L.NRSEQCTBTEMP;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN
                     V_COUNT := 0;
                     V_NRSEQLANC := NULL;
               END;

               /* SE JA EXISTIR PARTIDA PARA AQUELA CONTA CONTABIL COM A FILIAL E O CENTRO DE CUSTO, O VALOR E SOMENTE
                  ATUALIZADO. */
               IF V_COUNT = 0 THEN
                  INSERT INTO RATECTBTEMP
                     (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL,
                      CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                  VALUES
                     (P_CDEMPRESA, P_NRLANCTBTEMP, LPAD(TO_CHAR(V_NUMPARTIDA),5,'0'), DECODE(P_IDFILIAL, 'E', P_CDFILIRECE, P_CDFILIEMIT),
                      V_CDCENTCUST, P_SINAL * (ROUND(P_VRPARTIDA, 2) * P_FATORMULT), P_DTENTRSAID, '00001');
               ELSE
                  UPDATE RATECTBTEMP
                     SET VRRATECTBTEMP = VRRATECTBTEMP + (P_SINAL * (ROUND(P_VRPARTIDA, 2) * P_FATORMULT))
                   WHERE NRLANCCTBTEMP = P_NRLANCTBTEMP
                     AND CDEMPRESA = P_CDEMPRESA
                     AND NRSEQCTBTEMP = V_NRSEQLANC
                     AND CDFILIAL = DECODE(P_IDFILIAL, 'E', P_CDFILIRECE, P_CDFILIEMIT)
                     AND CDCENTCUST = V_CDCENTCUST;
               END IF;
            END IF;
         END IF;
      END IF;
   END;
BEGIN
   /* BUSCA O HISTORICO PARA O LANCAMENTO */
   CTB_DFE_HISTORICO(P_CDFILIAL, P_NRLANCTONF, V_CDHISTPADR, V_DSHISTLANC);

   FOR C_NOTAFISCAL 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,  NF.IDORIGDESTNF,  NF.VRPRODUTOS,
              NF.CDFILIRECE,    NF.DTENTRSAID,    NF.CDINSCESTA,
              NF.DTEMISSAO,     C.IDPRIPUBCLIE,   NF.IDENTRSAIDOP,
              NVL(FI.CDCENTCUST, EP.CDCENTCUST) AS CDCENTCUST,
              TP.CDTPCTPAGAR AS CDTPCTPAGOP, TP.CDTPCTRECEB AS CDTPCTRECOP,
              TC.CDCLASSFINA AS CDCLAFINOP, FO.DTINIVRPDESC, NF.CDSERIEENTR,
              NF.CDSERIE, NF.IDTPIJURNF, NF.NRINSJURNF, TP.IDDTCONTAB,
              TP.IDCOAVRIMPVRPROD
         FROM NOTAFISCAL NF, FILIAL FI, EMPRESA EP,
              TIPOOPERAC TP, TIPOCTPAGAR TC, FORNECEDOR FO, CLIENTE C
        WHERE NF.CDFILIAL     = P_CDFILIAL        AND
              NF.NRLANCTONF   = P_NRLANCTONF      AND
              NF.CDEMPRESA    = EP.CDEMPRESA      AND
              NF.IDENTRSAIDOP = TP.IDENTRSAIDOP   AND
              NF.CDTIPOOPER   = TP.CDTIPOOPER     AND
              TP.CDTPCTPAGAR  = TC.CDTPCTPAGAR(+) AND
              DECODE(NF.IDENTRSAIDOP,'E', NF.CDFILIRECE, NF.CDFILIEMIT) = FI.CDFILIAL AND
              NF.CDFORNECED   = FO.CDFORNECED(+)  AND
              NF.CDCLIENTE    = C.CDCLIENTE(+))LOOP
      V_CDCENTCUST := BUSCA_CCUSTO(C_NOTAFISCAL.CDFILIRECE, C_NOTAFISCAL.CDEMPRESA);
      FOR C_IMPOSTONFE 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, A.CDFISCALNF, IDCONTAIMPADIC,
                COUNT(*) OVER (PARTITION BY A.CDFILIAL, A.NRLANCTONF) RECORDCOUNT
           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,
                        IDCONTAIMPADIC, NF.CDFILIAL, NF.NRLANCTONF
                   FROM IMPOSTONF INF, IMPOSTO I, NOTAFISCAL NF, PARAMFILIAL PF
                  WHERE NF.CDFILIAL     = C_NOTAFISCAL.CDFILIAL
                    AND NF.NRLANCTONF   = C_NOTAFISCAL.NRLANCTONF
                    AND PF.CDFILIAL(+)  = NF.CDFILIRECE
                    AND INF.CDFILIAL    = C_NOTAFISCAL.CDFILIAL
                    AND INF.NRLANCTONF  = C_NOTAFISCAL.NRLANCTONF
                    AND INF.CDIMPOSTO   = I.CDIMPOSTO
                    AND INF.VRIMPOSTONF > 0
                    AND ((IDDIFALIQCUSPRN = 'S' AND INF.IDIMPOSTONF IN ('1','4')) OR (IDDIFALIQCUSPRN = 'N' AND INF.IDIMPOSTONF IN ('1','4')))
                    AND I.IDTIPCONTAB  = 'D'
                    AND I.IDCONTAIMPADIC <> 'S') 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, A.CDFISCALNF,
                   A.IDCONTAIMPADIC, A.CDFILIAL, A.NRLANCTONF) LOOP


         /* CONDICOES REALIZADAS PARA QUE O VALOR DO IMPOSTO SEJA CALCULADO DE ACORDO COM
            O ANTIGO ONCALCFIELDS DA QUERY ADQIMPOSTONFE */
         IF ((C_NOTAFISCAL.IDOPERDEVONF = 'S') OR (C_NOTAFISCAL.CDTIPOOPER = '16')) THEN
            IF C_IMPOSTONFE.IDTRATIMPO = 'V' THEN
               V_VRIMPOSTO := ROUND(C_IMPOSTONFE.VRIMPOSTONFE, 2);
            ELSE
               V_VRIMPOSTO := - ROUND(C_IMPOSTONFE.VRIMPOSTONFE, 2);
            END IF;

            IF C_IMPOSTONFE.IDTRATIMPO = 'A' AND C_IMPOSTONFE.IDACRPRODENTRNF = 'N' THEN
               V_VRIMPOSTO := ROUND(V_VRIMPOSTO, 2);
            END IF;
         ELSE
            IF C_IMPOSTONFE.IDTRATIMPO = 'V' AND C_IMPOSTONFE.IDIMPORECUP = 'S' THEN
               V_VRIMPOSTO := - ROUND(C_IMPOSTONFE.VRIMPOSTONFE, 2);
            ELSE
               V_VRIMPOSTO := ROUND(C_IMPOSTONFE.VRIMPOSTONFE, 2);
            END IF;

            IF C_IMPOSTONFE.IDTRATIMPO = 'A' AND C_IMPOSTONFE.IDACRPRODENTRNF = 'N' THEN
               V_VRIMPOSTO := - ROUND(V_VRIMPOSTO, 2);
            ELSE
               V_VRIMPOSTO := ROUND(V_VRIMPOSTO, 2);
            END IF;
         END IF;

         V_CONTAATIV := CONTAIMPOSTOFILIAL(C_NOTAFISCAL.CDFILIRECE, C_NOTAFISCAL.IDENTRSAIDOP, C_NOTAFISCAL.CDTIPOOPER, C_IMPOSTONFE.CDIMPOSTO,
                                           C_IMPOSTONFE.CDCONTATIV, 'A');

         V_CONTAPASS := CONTAIMPOSTOFILIAL(C_NOTAFISCAL.CDFILIRECE, C_NOTAFISCAL.IDENTRSAIDOP, C_NOTAFISCAL.CDTIPOOPER, C_IMPOSTONFE.CDIMPOSTO,
                                           C_IMPOSTONFE.CDCCONTPASS, 'P');

         V_CONTADESP := CONTAIMPOSTOFILIAL(C_NOTAFISCAL.CDFILIRECE, C_NOTAFISCAL.IDENTRSAIDOP, C_NOTAFISCAL.CDTIPOOPER, C_IMPOSTONFE.CDIMPOSTO,
                                           C_IMPOSTONFE.CDCONTDESP, 'D');

         /*  NOTAS FISCAIS DE DEVOLUCAO  */
         IF ((C_NOTAFISCAL.IDOPERDEVONF = 'S') OR (C_NOTAFISCAL.CDTIPOOPER = '16')) THEN
            BEGIN
               SELECT MAX(CDIMPOSTO) AS CDIMPOSTRIB
                 INTO V_CDIMPOSTRIB
                 FROM IMPOSTO
                WHERE CDIMPOSTRIB = C_IMPOSTONFE.CDIMPOSTO;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_CDIMPOSTRIB := NULL;
            END;

            IF V_CDIMPOSTRIB IS NOT NULL THEN
               /* SUBSTITUICAO TRIBUTARIA */
               /* PARTIDA PARA A CONTA DO PASSIVO */
               NUMEROPARTIDA(C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.IDORIGDESTNF, C_NOTAFISCAL.CDFORNECED, C_NOTAFISCAL.CDCLIENTE);
               PARTIDA(C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.CDFILIAL, C_NOTAFISCAL.CDFILIRECE, C_NOTAFISCAL.CDFILIEMIT, C_NOTAFISCAL.NRLANCTONF,
                       C_NOTAFISCAL.NRNOTAFISC, C_NOTAFISCAL.IDENTRSAIDOP, C_NOTAFISCAL.IDORIGDESTNF,
                       C_NOTAFISCAL.CDFORNECED, C_NOTAFISCAL.CDCLIENTE, C_NOTAFISCAL.DTENTRSAID, -1, V_CONTAPASS, 'E', V_VRIMPOSTO);

               /* PARTIDA REFERENTE A DESPESA DA SUBSTITUICAO TRIBUTARIA. */
               NUMEROPARTIDA(C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.IDORIGDESTNF, C_NOTAFISCAL.CDFORNECED, C_NOTAFISCAL.CDCLIENTE);
               PARTIDA(C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.CDFILIAL, C_NOTAFISCAL.CDFILIRECE, C_NOTAFISCAL.CDFILIEMIT, C_NOTAFISCAL.NRLANCTONF,
                       C_NOTAFISCAL.NRNOTAFISC, C_NOTAFISCAL.IDENTRSAIDOP, C_NOTAFISCAL.IDORIGDESTNF,
                       C_NOTAFISCAL.CDFORNECED, C_NOTAFISCAL.CDCLIENTE, C_NOTAFISCAL.DTENTRSAID, 1, V_CONTADESP, 'E', V_VRIMPOSTO);
            ELSE
               /* IMPOSTO A RETER NA FONTE */
               IF C_IMPOSTONFE.IDTRATIMPO = 'R' THEN
                  NUMEROPARTIDA(C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.IDORIGDESTNF, C_NOTAFISCAL.CDFORNECED, C_NOTAFISCAL.CDCLIENTE);
                  PARTIDA(C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.CDFILIAL, C_NOTAFISCAL.CDFILIRECE, C_NOTAFISCAL.CDFILIEMIT, C_NOTAFISCAL.NRLANCTONF,
                          C_NOTAFISCAL.NRNOTAFISC, C_NOTAFISCAL.IDENTRSAIDOP, C_NOTAFISCAL.IDORIGDESTNF,
                          C_NOTAFISCAL.CDFORNECED, C_NOTAFISCAL.CDCLIENTE, C_NOTAFISCAL.DTENTRSAID, -1, V_CONTAATIV, 'E', V_VRIMPOSTO);
               /* IMPOSTO A ACRESCER */
               ELSIF C_IMPOSTONFE.IDTRATIMPO = 'A' THEN
                  NUMEROPARTIDA(C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.IDORIGDESTNF, C_NOTAFISCAL.CDFORNECED, C_NOTAFISCAL.CDCLIENTE);
                  PARTIDA(C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.CDFILIAL, C_NOTAFISCAL.CDFILIRECE, C_NOTAFISCAL.CDFILIEMIT, C_NOTAFISCAL.NRLANCTONF,
                          C_NOTAFISCAL.NRNOTAFISC, C_NOTAFISCAL.IDENTRSAIDOP, C_NOTAFISCAL.IDORIGDESTNF,
                          C_NOTAFISCAL.CDFORNECED, C_NOTAFISCAL.CDCLIENTE, C_NOTAFISCAL.DTENTRSAID,  1, V_CONTAATIV, 'E', V_VRIMPOSTO);

                  /* Verifica se o imposto e recuperavel para jogar o valor do mesmo na conta de Despesa.
                     Na rotina de saida, o valor do imposto tambem e jogado na conta do produto, porem,
                     o valor do imposto na nota de devolucao nao e abatido do VRCONTITEM. Sendo assim,
                     e necessario somente jogar o valor do imposto na conta que falta. */
                   FOR C_ITEMPROIMPOSIPI IN
                      (SELECT ROUND(VRIMPOSTOIT, 2) VRIMPOSTOIT
                         FROM ITEMPROIMPOS IMP, (SELECT DISTINCT CDIMPOSTO,
                                                                 CONTCTBL_IMPOSTO(I.CDIMPOSTO, 'A', C_NOTAFISCAL.IDENTRSAIDOP, NF.CDTIPOOPER, NF.IDTPIJURNF) AS CDCONTATIV,
                                                                 CONTCTBL_IMPOSTO(I.CDIMPOSTO, 'P', C_NOTAFISCAL.IDENTRSAIDOP, NF.CDTIPOOPER, NF.IDTPIJURNF) AS CDCONTPASS,
                                                                 CONTCTBL_IMPOSTO(I.CDIMPOSTO, 'D', C_NOTAFISCAL.IDENTRSAIDOP, NF.CDTIPOOPER, NF.IDTPIJURNF) AS CDCONTDESP
                                                   FROM ITEMPROIMPOS I, NOTAFISCAL NF
                                                  WHERE I.CDFILIAL   = NF.CDFILIAL
                                                    AND I.NRLANCTONF = NF.NRLANCTONF
                                                    AND I.CDFILIAL   = P_CDFILIAL
                                                    AND I.NRLANCTONF = P_NRLANCTONF) CT, IMPOSTO I, PARAMFILIAL PF, NOTAFISCAL NF, ITEMPRODUTO IT
                        WHERE IMP.NRLANCTONF = P_NRLANCTONF
                          AND IMP.CDFILIAL = P_CDFILIAL
                          AND IMP.VRIMPOSTOIT > 0
                          AND IMP.CDIMPOSTO = CT.CDIMPOSTO
                          AND IMP.CDIMPOSTO = I.CDIMPOSTO
                          AND ((IMP.NRITPROIMPOS IN ('1','4')) OR (IMP.NRITPROIMPOS IS NULL))
                          AND I.IDTIPCONTAB  = 'D'
                          AND CT.CDCONTPASS IS NOT NULL
                          AND IMP.CDFILIAL = NF.CDFILIAL
                          AND IMP.NRLANCTONF = NF.NRLANCTONF
                          AND IMP.CDFILIAL = IT.CDFILIAL
                          AND IMP.NRLANCTONF = IT.NRLANCTONF
                          AND I.CDIMPOSTO = C_IMPOSTONFE.CDIMPOSTO
                          AND DECODE(NF.IDENTRSAIDOP, 'E', NF.CDFILIRECE, NF.CDFILIEMIT) = PF.CDFILIAL
                          AND IT.NRSEQITPROD = IMP.NRSEQITPROD
                          AND I.IDTRATIMPO = 'A'
                          AND I.IDIMPORECUP = 'S') LOOP
                      NUMEROPARTIDA(C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.IDORIGDESTNF, C_NOTAFISCAL.CDFORNECED, C_NOTAFISCAL.CDCLIENTE);
                      PARTIDA(C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.CDFILIAL, C_NOTAFISCAL.CDFILIRECE, C_NOTAFISCAL.CDFILIEMIT, C_NOTAFISCAL.NRLANCTONF,
                              C_NOTAFISCAL.NRNOTAFISC, C_NOTAFISCAL.IDENTRSAIDOP, C_NOTAFISCAL.IDORIGDESTNF,
                              C_NOTAFISCAL.CDFORNECED, C_NOTAFISCAL.CDCLIENTE, C_NOTAFISCAL.DTEMISSAO, 1, V_CONTADESP, 'E', C_ITEMPROIMPOSIPI.VRIMPOSTOIT);

                   END LOOP;
               ELSE
                  NUMEROPARTIDA(C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.IDORIGDESTNF, C_NOTAFISCAL.CDFORNECED, C_NOTAFISCAL.CDCLIENTE);
                  PARTIDA(C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.CDFILIAL, C_NOTAFISCAL.CDFILIRECE, C_NOTAFISCAL.CDFILIEMIT, C_NOTAFISCAL.NRLANCTONF,
                          C_NOTAFISCAL.NRNOTAFISC, C_NOTAFISCAL.IDENTRSAIDOP, C_NOTAFISCAL.IDORIGDESTNF,
                          C_NOTAFISCAL.CDFORNECED, C_NOTAFISCAL.CDCLIENTE, C_NOTAFISCAL.DTENTRSAID,  1, V_CONTADESP, 'E', V_VRIMPOSTO);

                  NUMEROPARTIDA(C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.IDORIGDESTNF, C_NOTAFISCAL.CDFORNECED, C_NOTAFISCAL.CDCLIENTE);
                  PARTIDA(C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.CDFILIAL, C_NOTAFISCAL.CDFILIRECE, C_NOTAFISCAL.CDFILIEMIT, C_NOTAFISCAL.NRLANCTONF,
                          C_NOTAFISCAL.NRNOTAFISC, C_NOTAFISCAL.IDENTRSAIDOP, C_NOTAFISCAL.IDORIGDESTNF,
                          C_NOTAFISCAL.CDFORNECED, C_NOTAFISCAL.CDCLIENTE, C_NOTAFISCAL.DTENTRSAID,  -1, V_CONTAATIV, 'E', V_VRIMPOSTO);
               END IF;
            END IF;
            /* FIM DAS CONTABILIZACOES DE IMPOSTOS PARA AS NOTAS DE DEVOLUCAO OU DO TIPO DE OPERACAO 16 */
         ELSE
            /* NOTAS FISCAIS QUE NAO SAO DE DEVOLUCAO */
            BEGIN
               SELECT MAX(CDIMPOSTO) AS CDIMPOSTRIB
                 INTO V_CDIMPOSTRIB
                 FROM IMPOSTO
                WHERE CDIMPOSTRIB = C_IMPOSTONFE.CDIMPOSTO;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_CDIMPOSTRIB := NULL;
            END;

            BEGIN
               SELECT A.CDMODDOCFISC
                 INTO V_CDMODFISCAL
                 FROM NOTAFISCAL N, MODDOCFISC A
                WHERE (N.CDMODDOCFISC = A.CDMODDOCFISC)
                  AND (A.CDTPREGFISC = '70')
                  AND (N.CDFILIAL = P_CDFILIAL)
                  AND (N.NRLANCTONF = P_NRLANCTONF)
                ORDER BY A.CDMODDOCFISC;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_CDMODFISCAL := NULL;
            END;

            IF ((V_CDIMPOSTRIB IS NOT NULL) AND (V_CDMODFISCAL IS NOT NULL))THEN
               NUMEROPARTIDA(C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.IDORIGDESTNF, C_NOTAFISCAL.CDFORNECED, C_NOTAFISCAL.CDCLIENTE);
               PARTIDA(C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.CDFILIAL, C_NOTAFISCAL.CDFILIRECE, C_NOTAFISCAL.CDFILIEMIT, C_NOTAFISCAL.NRLANCTONF,
                       C_NOTAFISCAL.NRNOTAFISC, C_NOTAFISCAL.IDENTRSAIDOP, C_NOTAFISCAL.IDORIGDESTNF,
                       C_NOTAFISCAL.CDFORNECED, C_NOTAFISCAL.CDCLIENTE, C_NOTAFISCAL.DTENTRSAID, 1, V_CONTAATIV, 'E', V_VRIMPOSTO);

               NUMEROPARTIDA(C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.IDORIGDESTNF, C_NOTAFISCAL.CDFORNECED, C_NOTAFISCAL.CDCLIENTE);
               PARTIDA(C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.CDFILIAL, C_NOTAFISCAL.CDFILIRECE, C_NOTAFISCAL.CDFILIEMIT, C_NOTAFISCAL.NRLANCTONF,
                       C_NOTAFISCAL.NRNOTAFISC, C_NOTAFISCAL.IDENTRSAIDOP, C_NOTAFISCAL.IDORIGDESTNF,
                       C_NOTAFISCAL.CDFORNECED, C_NOTAFISCAL.CDCLIENTE, C_NOTAFISCAL.DTENTRSAID, -1, V_CONTAPASS, 'E', V_VRIMPOSTO);


               FOR C_FILIPERCN IN
                  (SELECT NVL(VRPECREPREST,0) AS VRPECREPREST
                     FROM FILIAL
                    WHERE CDFILIAL = P_CDFILIAL
                      AND NVL(VRPECREPREST,0) > 0) LOOP

                  NUMEROPARTIDA(C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.IDORIGDESTNF, C_NOTAFISCAL.CDFORNECED, C_NOTAFISCAL.CDCLIENTE);
                  PARTIDA(C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.CDFILIAL, C_NOTAFISCAL.CDFILIRECE, C_NOTAFISCAL.CDFILIEMIT, C_NOTAFISCAL.NRLANCTONF,
                          C_NOTAFISCAL.NRNOTAFISC, C_NOTAFISCAL.IDENTRSAIDOP, C_NOTAFISCAL.IDORIGDESTNF,
                          C_NOTAFISCAL.CDFORNECED, C_NOTAFISCAL.CDCLIENTE, C_NOTAFISCAL.DTENTRSAID, -1, V_CONTAATIV, 'E', V_VRIMPOSTO * (C_FILIPERCN.VRPECREPREST / 100));

                  NUMEROPARTIDA(C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.IDORIGDESTNF, C_NOTAFISCAL.CDFORNECED, C_NOTAFISCAL.CDCLIENTE);
                  PARTIDA(C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.CDFILIAL, C_NOTAFISCAL.CDFILIRECE, C_NOTAFISCAL.CDFILIEMIT, C_NOTAFISCAL.NRLANCTONF,
                          C_NOTAFISCAL.NRNOTAFISC, C_NOTAFISCAL.IDENTRSAIDOP, C_NOTAFISCAL.IDORIGDESTNF,
                          C_NOTAFISCAL.CDFORNECED, C_NOTAFISCAL.CDCLIENTE, C_NOTAFISCAL.DTENTRSAID, 1, V_CONTAPASS, 'E', V_VRIMPOSTO * (C_FILIPERCN.VRPECREPREST / 100));

               END LOOP;
            ELSE
               IF C_IMPOSTONFE.IDIMPORECUP = 'S' THEN
                  V_CONTA := CONTAIMPOSTOFILIAL(C_NOTAFISCAL.CDFILIRECE, C_NOTAFISCAL.IDENTRSAIDOP, C_NOTAFISCAL.CDTIPOOPER, C_IMPOSTONFE.CDIMPOSTO,
                                                C_IMPOSTONFE.CDCONTIMPO, 'A');
               ELSIF C_IMPOSTONFE.IDIMPORECUP = 'N' AND
                     C_IMPOSTONFE.IDTRATIMPO = 'V' AND
                     C_IMPOSTONFE.IDCONTAIMPADIC = 'S' THEN
                  V_CONTA := C_IMPOSTONFE.CDCONTIMPO;

               ELSIF C_IMPOSTONFE.IDTRATIMPO = 'R' THEN
                  V_CONTA := CONTAIMPOSTOFILIAL(C_NOTAFISCAL.CDFILIRECE, C_NOTAFISCAL.IDENTRSAIDOP, C_NOTAFISCAL.CDTIPOOPER, C_IMPOSTONFE.CDIMPOSTO,
                                                C_IMPOSTONFE.CDCONTIMPO, 'P');
               ELSIF C_IMPOSTONFE.IDTRATIMPO = 'A' THEN
                  V_CONTA := CONTAIMPOSTOFILIAL(C_NOTAFISCAL.CDFILIRECE, C_NOTAFISCAL.IDENTRSAIDOP, C_NOTAFISCAL.CDTIPOOPER, C_IMPOSTONFE.CDIMPOSTO,
                                                C_IMPOSTONFE.CDCONTIMPO, 'D');
               END IF;

               NUMEROPARTIDA(C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.IDORIGDESTNF, C_NOTAFISCAL.CDFORNECED, C_NOTAFISCAL.CDCLIENTE);
               PARTIDA(C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.CDFILIAL, C_NOTAFISCAL.CDFILIRECE, C_NOTAFISCAL.CDFILIEMIT, C_NOTAFISCAL.NRLANCTONF,
                       C_NOTAFISCAL.NRNOTAFISC, C_NOTAFISCAL.IDENTRSAIDOP, C_NOTAFISCAL.IDORIGDESTNF,
                       C_NOTAFISCAL.CDFORNECED, C_NOTAFISCAL.CDCLIENTE, C_NOTAFISCAL.DTENTRSAID, 1, V_CONTA, 'E', (V_VRIMPOSTO * P_FATOR) * P_FATORMULT);

            END IF;
         END IF;
      END LOOP;

      /* Contabilizara somente os impostos com o parametro "Contabiliza Imposto Adicional, este parametro foi criado especialmente
         para a contabilizacao de INSS de servicos tomados, como cooperativas. O Imposto nao deve ser recuperavel, e o codigo fiscal
         do mesmo deve ser 1. */
      FOR C_IMPOSTODESP IN
         (SELECT ROUND(VRIMPOSTONF, 2) AS VRIMPOSTONF,
                 CONTCTBL_IMPOSTO(I.CDIMPOSTO, 'P', NF.IDENTRSAIDOP, NF.CDTIPOOPER, NF.IDTPIJURNF) CDCONTPASS,
                 CONTCTBL_IMPOSTO(I.CDIMPOSTO, 'D', NF.IDENTRSAIDOP, NF.CDTIPOOPER, NF.IDTPIJURNF) CDCONTDESP, I.CDIMPOSTO
            FROM IMPOSTONF IMP, IMPOSTO I, NOTAFISCAL NF
           WHERE IMP.CDIMPOSTO  = I.CDIMPOSTO
             AND IMP.NRLANCTONF = P_NRLANCTONF
             AND IMP.CDFILIAL   = P_CDFILIAL
             AND IMP.NRLANCTONF = NF.NRLANCTONF
             AND IMP.CDFILIAL   = NF.CDFILIAL
             AND IMP.CDFISCALNF = '1'
             AND I.IDCONTAIMPADIC = 'S')LOOP
         /* As Partidas do lancamento devem ser uma partida na conta contabil do produto do imposto e outra na conta do passivo */
         NUMEROPARTIDA(C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.IDORIGDESTNF, C_NOTAFISCAL.CDFORNECED, C_NOTAFISCAL.CDCLIENTE);
         PARTIDA(C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.CDFILIAL, C_NOTAFISCAL.CDFILIRECE, C_NOTAFISCAL.CDFILIEMIT, C_NOTAFISCAL.NRLANCTONF,
                 C_NOTAFISCAL.NRNOTAFISC, C_NOTAFISCAL.IDENTRSAIDOP, C_NOTAFISCAL.IDORIGDESTNF,
                 C_NOTAFISCAL.CDFORNECED, C_NOTAFISCAL.CDCLIENTE, C_NOTAFISCAL.DTENTRSAID, 1, C_IMPOSTODESP.CDCONTPASS, 'E', (C_IMPOSTODESP.VRIMPOSTONF * P_FATOR));

         FOR C_ITEMPRODUTO IN
            (SELECT IT.NRSEQITPROD, IT.CDPRODUTO,
                    ROUND(IT.VRCONTITEM,2) AS VRCONTITEM,
                    CONTCTBL_PRODUTO(P.CDPRODUTO, 'E', IT.CDFILIAL) AS CDCTBLENTRPR,
                    CONTCTBL_PRODUTO(P.CDPRODUTO, 'S', IT.CDFILIAL) AS CDCTBLSAIDPR,
                    ROUND(IT.VRDESCITEM,2) AS VRDESCITEM
               FROM ITEMPRODUTO IT, PRODUTO P
              WHERE (IT.CDFILIAL     = P_CDFILIAL)
                AND (IT.NRLANCTONF   = P_NRLANCTONF)
                AND (IT.VRCONTITEM   > 0)
                AND (IT.CDPRODUTO = P.CDPRODUTO)
              ORDER BY IT.NRSEQITPROD)LOOP

            BEGIN
               SELECT P.CDARVPROD
                 INTO V_CDARVPROD
                 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_NOTAFISCAL.CDFILIRECE)
                  AND (F.CDFILIAL = P_CDFILIAL)
                  AND (N.CDFILIAL = P_CDFILIAL)
                  AND (N.NRLANCTONF = P_NRLANCTONF)
                  AND (N.CDEMPRESA = C_NOTAFISCAL.CDEMPRESA)
                  AND (N.CDEMPRESA = E.CDEMPRESA);
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_CDARVPROD := NULL;
            END;

            BEGIN
               SELECT C.CDCTBLPRODTO
                 INTO V_CONTATIPOOPER
                 FROM CTBPRDTPOPER C, (SELECT MAX(P.CDARVPROD) CDARVPROD
                                         FROM CTBPRDTPOPER A, PRODUTO P
                                        WHERE IDENTRSAIDOP = C_NOTAFISCAL.IDENTRSAIDOP
                                          AND CDTIPOOPER = C_NOTAFISCAL.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 = C_NOTAFISCAL.IDENTRSAIDOP
                   AND CDTIPOOPER = C_NOTAFISCAL.CDTIPOOPER
                   AND C.CDPRODUTO = P.CDPRODUTO
                   AND P.CDARVPROD = PR.CDARVPROD;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_CONTATIPOOPER := NULL;
            END;

            IF V_CONTATIPOOPER IS NULL THEN
               BEGIN
                  SELECT CDCONTPROD
                    INTO V_CONTATIPOOPER
                    FROM TIPOOPERAC T
                   WHERE CDTIPOOPER   = C_NOTAFISCAL.CDTIPOOPER
                     AND IDENTRSAIDOP = C_NOTAFISCAL.IDENTRSAIDOP;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN
                     V_CONTATIPOOPER := NULL;
               END;
            END IF;

            FOR C_ITEMPROIMPOS IN
               (SELECT CDFILIAL, NRLANCTONF, CDIMPOSTO, VRIMPOSTOIT, NRSEQITPROD
                  FROM ITEMPROIMPOS
                 WHERE NRLANCTONF  = P_NRLANCTONF
                   AND CDFILIAL    = P_CDFILIAL
                   AND CDIMPOSTO   = C_IMPOSTODESP.CDIMPOSTO
                   AND NRSEQITPROD = C_ITEMPRODUTO.NRSEQITPROD
                   AND CDFISCALIT  =  '1') LOOP

               V_ENTROULOOP := 'N';
               V_VRIMPOSTO := C_ITEMPROIMPOS.VRIMPOSTOIT;
               V_VRRATEIO := 0;
               /* PEGA A PORCENTAGEM DE CADA FILIAL PARA O PRODUTO, PARA QUE SE FOR NECESSARIO GERAR PARTIDAS PARA FILIAIS DIFERENTES. */
               FOR C_VRRATEIONF IN
                  (SELECT ROUND(((ROUND(VRRATENF,2) * 100) / I.VRCONTITEM), 2) AS PORCENTAGEM, CDCENTCUSNF AS CDCENTCUST,
                          RN.CDFILICCUST CDFILIAL
                     FROM RATEIONF RN, ITEMPRODUTO I
                    WHERE RN.CDFILIAL    = P_CDFILIAL    AND
                          RN.NRLANCTONF  = P_NRLANCTONF  AND
                          RN.NRLANCTONF  = I.NRLANCTONF   AND
                          RN.CDFILIAL    = I.CDFILIAL     AND
                          RN.NRSEQITPROD = I.NRSEQITPROD  AND
                          RN.NRSEQITPROD = C_ITEMPRODUTO.NRSEQITPROD
                    GROUP BY ROUND(((ROUND(VRRATENF,2) * 100) / I.VRCONTITEM), 2),
                             CDCENTCUSNF, RN.CDFILICCUST) LOOP
                  V_ENTROULOOP := 'S';

                  IF V_CONTATIPOOPER IS NULL THEN
                     BEGIN
                        SELECT CONTCTBL_PRODUTO(C_ITEMPRODUTO.CDPRODUTO, 'E', C_VRRATEIONF.CDFILIAL)
                          INTO V_CONTA
                          FROM DUAL;
                     EXCEPTION
                        WHEN NO_DATA_FOUND THEN
                           V_CONTA := NULL;
                     END;
                  ELSE
                     V_CONTA := V_CONTATIPOOPER;
                  END IF;

                  V_CDCENTCUST := C_VRRATEIONF.CDCENTCUST;
                  V_VRPARTIDA := ROUND((C_ITEMPROIMPOS.VRIMPOSTOIT * C_VRRATEIONF.PORCENTAGEM) / 100, 2);

                  NUMEROPARTIDA(C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.IDORIGDESTNF, C_NOTAFISCAL.CDFORNECED, C_NOTAFISCAL.CDCLIENTE);
                  PARTIDA(C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.CDFILIAL, C_VRRATEIONF.CDFILIAL, C_NOTAFISCAL.CDFILIEMIT, C_NOTAFISCAL.NRLANCTONF,
                          C_NOTAFISCAL.NRNOTAFISC, C_NOTAFISCAL.IDENTRSAIDOP, C_NOTAFISCAL.IDORIGDESTNF,
                          C_NOTAFISCAL.CDFORNECED, C_NOTAFISCAL.CDCLIENTE, C_NOTAFISCAL.DTENTRSAID, -1, V_CONTA, 'E', V_VRPARTIDA);
                  V_CONTAANTERIOR := V_CONTA;
                  V_CONTA := NULL;
                  V_VRRATEIO := V_VRRATEIO + ROUND((C_ITEMPROIMPOS.VRIMPOSTOIT * C_VRRATEIONF.PORCENTAGEM) / 100, 2);
                  V_CDFILIAL := C_VRRATEIONF.CDFILIAL;
               END LOOP;
               IF V_ENTROULOOP = 'S' THEN
                  IF V_VRIMPOSTO <> V_VRRATEIO THEN
                     IF V_VRIMPOSTO - V_VRRATEIO > 0 THEN
                        V_VRPARTIDA := -(V_VRIMPOSTO - V_VRRATEIO);
                     ELSE
                        V_VRPARTIDA := (V_VRIMPOSTO - V_VRRATEIO);
                     END IF;
                     NUMEROPARTIDA(C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.IDORIGDESTNF, C_NOTAFISCAL.CDFORNECED, C_NOTAFISCAL.CDCLIENTE);
                     PARTIDA(C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.CDFILIAL, V_CDFILIAL, C_NOTAFISCAL.CDFILIEMIT, C_NOTAFISCAL.NRLANCTONF,
                             C_NOTAFISCAL.NRNOTAFISC, C_NOTAFISCAL.IDENTRSAIDOP, C_NOTAFISCAL.IDORIGDESTNF,
                             C_NOTAFISCAL.CDFORNECED, C_NOTAFISCAL.CDCLIENTE, C_NOTAFISCAL.DTENTRSAID, 1, V_CONTAANTERIOR, 'E', V_VRPARTIDA);
                  END IF;
               ELSIF V_ENTROULOOP = 'N' THEN
                  NUMEROPARTIDA(C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.IDORIGDESTNF, C_NOTAFISCAL.CDFORNECED, C_NOTAFISCAL.CDCLIENTE);
                  PARTIDA(C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.CDFILIAL, C_NOTAFISCAL.CDFILIRECE, C_NOTAFISCAL.CDFILIEMIT, C_NOTAFISCAL.NRLANCTONF,
                          C_NOTAFISCAL.NRNOTAFISC, C_NOTAFISCAL.IDENTRSAIDOP, C_NOTAFISCAL.IDORIGDESTNF,
                          C_NOTAFISCAL.CDFORNECED, C_NOTAFISCAL.CDCLIENTE, C_NOTAFISCAL.DTENTRSAID, -1, V_CONTA, 'E', V_VRIMPOSTO);
               END IF;
            END LOOP;
         END LOOP;
      END LOOP;
   END LOOP;
END;
/