create or replace
PROCEDURE BASECALC_RETIDO
   (P_IDRECALCULO  IN NOTAFISCAL.IDENTRSAIDOP%TYPE,
    P_CDFILIAL     IN NOTAFISCAL.CDFILIAL%TYPE,
    P_NRLANCTONF   IN NOTAFISCAL.NRLANCTONF%TYPE,
    P_IDTPIJURNF   IN NOTAFISCAL.IDTPIJURNF%TYPE,
    P_NRINSJURNF   IN NOTAFISCAL.NRINSJURNF%TYPE,
    P_DATALANCNF   IN NOTAFISCAL.DTENTRSAID%TYPE,
    P_CDIMPOSTO    IN IMPOSTO.CDIMPOSTO%TYPE,
    P_IDENTRSAIDOP IN NOTAFISCAL.IDENTRSAIDOP%TYPE,
    P_CDEMPRESA    IN NOTAFISCAL.CDEMPRESA%TYPE,
    P_INSCESTA     IN ALIQUOTA.CDINSCESTA%TYPE   DEFAULT NULL,
    P_PAISORIG     IN PAIS.CDPAIS%TYPE           DEFAULT NULL,
    P_ESTAORIG     IN ESTADO.SGESTADO%TYPE       DEFAULT NULL,
    P_PAISDEST     IN PAIS.CDPAIS%TYPE           DEFAULT NULL,
    P_ESTADEST     IN ESTADO.SGESTADO%TYPE       DEFAULT NULL,
    P_CDGRUPOFISC  IN ALIQUOTA.CDGRUPOFISC%TYPE  DEFAULT NULL,
    P_CDGRPFISCSV  IN ALIQUOTA.CDGRUPOFISC%TYPE  DEFAULT NULL,
    P_TIPOOPER     IN INCIDENCIA.CDTIPOOPER%TYPE DEFAULT NULL)
IS
   V_VRBCPERIODO    FLOAT;
   V_VRBASECALNF    FLOAT;
   V_VRMINBASE      FLOAT;
   V_VRIMPOSTO      FLOAT;
   V_VRALIQUOTA     NUMBER;
   V_NRDECITOTANF   NUMBER;
   V_VRDIFERENCA    NUMBER;
   V_VRTOTIMPONF    NUMBER;
   V_VRIMPOSTOIT    NUMBER;
   V_DATAINI        NOTAFISCAL.DTENTRSAID%TYPE;
   V_DATALANCNF     NOTAFISCAL.DTENTRSAID%TYPE;
   V_DTEMISSAO      NOTAFISCAL.DTENTRSAID%TYPE;
   V_DATAREFIMP     NOTAFISCAL.DTENTRSAID%TYPE;
   V_DATAREFIMPANT  NOTAFISCAL.DTENTRSAID%TYPE;
   V_IDFORMAPUR     IMPOSTO.IDFORMAPUR%TYPE;
   V_DTATUAPAG      NOTAFISCAL.DTENTRSAID%TYPE;
   V_BASECALCNF     FLOAT;
   V_IDGERAVENCNFAP VARCHAR2(01);
   V_VRBCALCIMPOIT  FLOAT;
   V_NRSEQUNOTAIT   VARCHAR2(04);
   V_NRSEQAUX       VARCHAR2(04);
   V_UTBCMENS       VARCHAR2(01);
   V_UTFXVAL        VARCHAR2(01);
   V_CALCIMPRET     VARCHAR2(01);
   V_IMPOSTODESC    FLOAT;
   V_BASECALCDESC   FLOAT;
   V_PARDEDTMP      NUMBER;
   V_CDARVPROD      VARCHAR2(20);
   V_FATOCONV       FLOAT;
   V_VRMAXIMP       FLOAT;
   V_VRMINIMP       FLOAT;
   V_IDCALCBCMENSDIA VARCHAR2(01);
BEGIN
   IF P_IDRECALCULO = 'S' THEN

      V_UTBCMENS   := 'N';
      V_UTFXVAL    := 'N';
      V_CALCIMPRET := 'N';

      Begin
        SELECT NVL(VRMINBASECAL, 0),   NVL(IDFORMAPUR, 'C'), NVL(VRMAXIMP, 0),
               NVL(IDUTVRMENSBC, 'N'), NVL(IDUTFAIXVLR, 'N'),
               NVL(IDCALCBCMENSDIA, 'M') AS IDCALCBCMENSDIA,
               NVL(DTINIREFSEMPER, P_DATALANCNF) AS DTINIREFSEMPER,
               NVL(NVL(DTANTREFSEMPER, DTINIREFSEMPER), P_DATALANCNF) AS DTANTREFSEMPER,
               NVL(VRMINIMP, 0) AS VRMINIMP
          INTO V_VRMINBASE, V_IDFORMAPUR, V_VRMAXIMP,
               V_UTBCMENS,  V_UTFXVAL,  V_IDCALCBCMENSDIA,
               V_DATAREFIMP, V_DATAREFIMPANT, V_VRMINIMP
          FROM IMPOSTO
         WHERE CDIMPOSTO = P_CDIMPOSTO;
      Exception
      When No_Data_Found Then
         V_VRMINBASE := 0;   V_IDFORMAPUR := 'C'; V_VRMAXIMP := 0;
         V_UTBCMENS  := 'N'; V_UTFXVAL    := 'N'; V_IDCALCBCMENSDIA := 'M';
      End;



      IF ((V_VRMINIMP > 0) AND (V_IDCALCBCMENSDIA = 'S')) THEN
         SELECT NVL(MAX(VRALIQNF),0)
           INTO V_VRALIQUOTA
           FROM IMPOSTONF
          WHERE CDFILIAL = P_CDFILIAL
            AND NRLANCTONF = P_NRLANCTONF
            AND CDIMPOSTO = P_CDIMPOSTO;
         IF (V_VRALIQUOTA > 0) THEN
            V_VRMINBASE := (V_VRMINIMP/(V_VRALIQUOTA/100)) - 0.01;
         END IF;
      END IF;

      BEGIN
         SELECT NVL(NRDECITOTANF, 2)
           INTO V_NRDECITOTANF
           FROM PARAMGERAL;
      EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_NRDECITOTANF := 2;
      END;

      V_VRBCPERIODO := 0;

      BEGIN
         SELECT NVL(DTEMISSAO, DTENTRSAID)
           INTO V_DTEMISSAO
           FROM NOTAFISCAL
          WHERE CDFILIAL   = P_CDFILIAL
            AND NRLANCTONF = P_NRLANCTONF;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_DTEMISSAO := SYSDATE;
      END;

      IF (V_IDFORMAPUR = 'E') THEN
         V_DATALANCNF := V_DTEMISSAO;
      ELSE
         V_DATALANCNF := P_DATALANCNF;
      END IF;

      V_DATAINI := TO_DATE('01/'||TO_CHAR(V_DATALANCNF, 'MM/YYYY'));

      IF (V_IDCALCBCMENSDIA = 'D') THEN     /*SE A BASE DE CALCULO FOR DIARIA, A DATA INICIAL E A MESMA DATA DA NOTA FISCAL*/
         V_DATAINI := V_DATALANCNF;
      ELSIF (V_IDCALCBCMENSDIA = 'M') THEN
         V_DATAINI := TO_DATE('01/'||TO_CHAR(V_DATALANCNF, 'MM/YYYY'));
      ELSIF (V_IDCALCBCMENSDIA = 'S') THEN
         IF (V_DATAREFIMP = V_DATAREFIMPANT) THEN
            V_DATAINI := TO_DATE('01/'||TO_CHAR(V_DATAREFIMP, 'MM/YYYY'));
         ELSE
            IF (V_DATALANCNF < V_DATAREFIMP) THEN
               V_DATAINI := TO_DATE('01/'||TO_CHAR(V_DATAREFIMPANT, 'MM/YYYY'));
            END IF;
         END IF;
      END IF;

      IF V_IDFORMAPUR = 'P' THEN

         IF P_IDENTRSAIDOP = 'E' THEN

            SELECT MIN(DTORIGVENPAG)
              INTO V_DTATUAPAG
              FROM CONTAPAGAR
             WHERE CDFILIAL = P_CDFILIAL
               AND NRLANCTONF = P_NRLANCTONF;

            IF V_DTATUAPAG IS NULL THEN
               V_DTATUAPAG := V_DATAINI;
            ELSE
               V_DTATUAPAG := TO_DATE('01/'||TO_CHAR(V_DTATUAPAG, 'MM/YYYY'));
            END IF;

            IF  (V_UTBCMENS = 'S' AND V_UTFXVAL = 'S') THEN /* Se utiliza base de calculo mensal e faixa de valores */
               SELECT SUM(VRBCALCIRRF)
                 INTO V_VRBCPERIODO
                 FROM NOTAFISCAL NF, ITEMPROIMPOS IP
                WHERE NF.CDEMPRESA    = P_CDEMPRESA                      AND
                      NF.IDTPIJURNF   = P_IDTPIJURNF                     AND
                      ((NF.IDTPIJURNF   = 'J' AND SUBSTR(NF.NRINSJURNF,1,8) = SUBSTR(P_NRINSJURNF,1,8))
                        OR NF.NRINSJURNF = P_NRINSJURNF)                 AND
                      NF.IDENTRSAIDOP = P_IDENTRSAIDOP                   AND
                      NF.CDFILIAL     = IP.CDFILIAL                      AND
                      NF.NRLANCTONF   = IP.NRLANCTONF                    AND
                      NF.CDFILIAL||NF.NRLANCTONF <> P_CDFILIAL||P_NRLANCTONF AND
                      IP.CDIMPOSTO    = P_CDIMPOSTO                      AND
                      IP.VRBCALCIRRF > 0                                 AND
                      EXISTS (SELECT C.CDFILIAL
                                FROM CONTAPAGAR C
                               WHERE C.CDFILIAL = NF.CDFILIAL
                                 AND C.NRLANCTONF = NF.NRLANCTONF
                                 AND DTORIGVENPAG BETWEEN V_DTATUAPAG AND LAST_DAY(V_DTATUAPAG));

               SELECT SUM(VRBCALCIRRF)
                 INTO V_BASECALCNF
                 FROM ITEMPROIMPOS IP
                WHERE IP.CDFILIAL   = P_CDFILIAL
                  AND IP.NRLANCTONF = P_NRLANCTONF
                  AND IP.CDIMPOSTO  = P_CDIMPOSTO
                  AND IP.VRBCALCIRRF > 0;

               V_CALCIMPRET := 'S';

            ELSE
               SELECT SUM(VRBASECALCNF)
                 INTO V_VRBCPERIODO
                 FROM NOTAFISCAL NF, IMPOSTONF IP
                WHERE NF.CDEMPRESA    = P_CDEMPRESA                      AND
                      NF.IDTPIJURNF   = P_IDTPIJURNF                     AND
                      ((NF.IDTPIJURNF   = 'J' AND SUBSTR(NF.NRINSJURNF,1,8) = SUBSTR(P_NRINSJURNF,1,8))
                        OR NF.NRINSJURNF = P_NRINSJURNF)                 AND
                      NF.IDENTRSAIDOP = P_IDENTRSAIDOP                   AND
                      NF.CDFILIAL     = IP.CDFILIAL                      AND
                      NF.NRLANCTONF   = IP.NRLANCTONF                    AND
                      IP.CDIMPOSTO    = P_CDIMPOSTO                      AND
                      NF.CDFILIAL||NF.NRLANCTONF <> P_CDFILIAL||P_NRLANCTONF AND
                      IP.VRBASECALCNF > 0                                AND
                      EXISTS (SELECT C.CDFILIAL
                                FROM CONTAPAGAR C
                               WHERE C.CDFILIAL = NF.CDFILIAL
                                 AND C.NRLANCTONF = NF.NRLANCTONF
                                 AND DTORIGVENPAG BETWEEN V_DTATUAPAG AND LAST_DAY(V_DTATUAPAG));

               SELECT SUM(VRBASECALCNF)
                 INTO V_BASECALCNF
                 FROM IMPOSTONF IP
                WHERE IP.CDFILIAL   = P_CDFILIAL
                  AND IP.NRLANCTONF = P_NRLANCTONF
                  AND IP.CDIMPOSTO  = P_CDIMPOSTO
                  AND IP.VRBASECALCNF > 0;
            END IF;
            V_VRBCPERIODO := NVL(V_VRBCPERIODO,0) + NVL(V_BASECALCNF,0);
         ELSE
            SELECT MIN(DTATUAVENREC)
              INTO V_DTATUAPAG
              FROM CONTARECEBER
             WHERE CDFILIAL = P_CDFILIAL
               AND NRLANCTONF = P_NRLANCTONF;

            IF V_DTATUAPAG IS NULL THEN
               V_DTATUAPAG := V_DATAINI;
            ELSE
               V_DTATUAPAG := TO_DATE('01/'||TO_CHAR(V_DTATUAPAG, 'MM/YYYY'));
            END IF;

            IF  (V_UTBCMENS = 'S' AND V_UTFXVAL = 'S') THEN /* Se utiliza base de calculo mensal e faixa de valores */
               SELECT SUM(VRBCALCIRRF)
                 INTO V_VRBCPERIODO
                 FROM NOTAFISCAL NF, ITEMPROIMPOS IP
                WHERE NF.CDEMPRESA    = P_CDEMPRESA                      AND
                      NF.IDTPIJURNF   = P_IDTPIJURNF                     AND
                      ((NF.IDTPIJURNF   = 'J' AND SUBSTR(NF.NRINSJURNF,1,8) = SUBSTR(P_NRINSJURNF,1,8))
                        OR NF.NRINSJURNF = P_NRINSJURNF)                 AND
                      NF.IDENTRSAIDOP = P_IDENTRSAIDOP                   AND
                      NF.CDFILIAL     = IP.CDFILIAL                      AND
                      NF.NRLANCTONF   = IP.NRLANCTONF                    AND
                      IP.CDIMPOSTO    = P_CDIMPOSTO                      AND
                      NF.CDFILIAL||NF.NRLANCTONF <> P_CDFILIAL||P_NRLANCTONF AND
                      IP.VRBCALCIRRF > 0                                 AND
                      EXISTS (SELECT C.CDFILIAL
                                FROM CONTARECEBER C
                               WHERE C.CDFILIAL = NF.CDFILIAL
                                 AND C.NRLANCTONF = NF.NRLANCTONF
                                 AND DTATUAVENREC BETWEEN V_DTATUAPAG AND LAST_DAY(V_DTATUAPAG));

               SELECT SUM(VRBCALCIRRF)
                 INTO V_BASECALCNF
                 FROM ITEMPROIMPOS IP
                WHERE IP.CDFILIAL   = P_CDFILIAL
                  AND IP.NRLANCTONF = P_NRLANCTONF
                  AND IP.CDIMPOSTO  = P_CDIMPOSTO
                  AND IP.VRBCALCIRRF > 0;

               V_CALCIMPRET := 'S';

            ELSE
               SELECT SUM(VRBASECALCNF)
                 INTO V_VRBCPERIODO
                 FROM NOTAFISCAL NF, IMPOSTONF IP
                WHERE NF.CDEMPRESA    = P_CDEMPRESA                      AND
                      NF.IDTPIJURNF   = P_IDTPIJURNF                     AND
                      ((NF.IDTPIJURNF   = 'J' AND SUBSTR(NF.NRINSJURNF,1,8) = SUBSTR(P_NRINSJURNF,1,8))
                        OR NF.NRINSJURNF = P_NRINSJURNF)                 AND
                      NF.IDENTRSAIDOP = P_IDENTRSAIDOP                   AND
                      NF.CDFILIAL     = IP.CDFILIAL                      AND
                      NF.NRLANCTONF   = IP.NRLANCTONF                    AND
                      IP.CDIMPOSTO    = P_CDIMPOSTO                      AND
                      NF.CDFILIAL||NF.NRLANCTONF <> P_CDFILIAL||P_NRLANCTONF AND
                      IP.VRBASECALCNF > 0                                AND
                      EXISTS (SELECT C.CDFILIAL
                                FROM CONTARECEBER C
                               WHERE C.CDFILIAL = NF.CDFILIAL
                                 AND C.NRLANCTONF = NF.NRLANCTONF
                                 AND DTATUAVENREC BETWEEN V_DTATUAPAG AND LAST_DAY(V_DTATUAPAG));

               SELECT SUM(VRBASECALCNF)
                 INTO V_BASECALCNF
                 FROM IMPOSTONF IP
                WHERE IP.CDFILIAL   = P_CDFILIAL
                  AND IP.NRLANCTONF = P_NRLANCTONF
                  AND IP.CDIMPOSTO  = P_CDIMPOSTO
                  AND IP.VRBASECALCNF > 0;
            END IF;
            V_VRBCPERIODO := NVL(V_VRBCPERIODO,0) + NVL(V_BASECALCNF,0);
         END IF;
      ELSE
         IF  (V_UTBCMENS = 'S' AND V_UTFXVAL = 'S') THEN /* Se utiliza base de calculo mensal e faixa de valores */
            SELECT SUM(VRBCALCIRRF)
              INTO V_VRBCPERIODO
              FROM NOTAFISCAL NF, ITEMPROIMPOS IP
             WHERE NF.CDEMPRESA    = P_CDEMPRESA                      AND
                   NF.IDTPIJURNF   = P_IDTPIJURNF                     AND
                   ((NF.IDTPIJURNF   = 'J' AND SUBSTR(NF.NRINSJURNF,1,8) = SUBSTR(P_NRINSJURNF,1,8))
                     OR NF.NRINSJURNF = P_NRINSJURNF)                 AND
                   NF.IDENTRSAIDOP = P_IDENTRSAIDOP                   AND
                   (((NF.DTENTRSAID   BETWEEN V_DATAINI AND V_DATALANCNF) AND (V_IDFORMAPUR <> 'E')) OR
                   ((NVL(NF.DTEMISSAO, NF.DTENTRSAID)   BETWEEN V_DATAINI AND V_DATALANCNF) AND (V_IDFORMAPUR = 'E'))) AND
                   NF.CDFILIAL     = IP.CDFILIAL                      AND
                   NF.NRLANCTONF   = IP.NRLANCTONF                    AND
                   IP.CDIMPOSTO    = P_CDIMPOSTO                      AND
                   IP.VRBCALCIRRF > 0;

            V_CALCIMPRET := 'S';

         ELSE
            SELECT SUM(VRBASECALCNF)
              INTO V_VRBCPERIODO
              FROM NOTAFISCAL NF, IMPOSTONF IP
             WHERE NF.CDEMPRESA    = P_CDEMPRESA                      AND
                   NF.IDTPIJURNF   = P_IDTPIJURNF                     AND
                   ((NF.IDTPIJURNF   = 'J' AND SUBSTR(NF.NRINSJURNF,1,8) = SUBSTR(P_NRINSJURNF,1,8))
                     OR NF.NRINSJURNF = P_NRINSJURNF)                 AND
                   NF.IDENTRSAIDOP = P_IDENTRSAIDOP                   AND
                   (((NF.DTENTRSAID   BETWEEN V_DATAINI AND V_DATALANCNF) AND (V_IDFORMAPUR <> 'E')) OR
                   ((NVL(NF.DTEMISSAO, NF.DTENTRSAID)   BETWEEN V_DATAINI AND V_DATALANCNF) AND (V_IDFORMAPUR = 'E'))) AND
                   NF.CDFILIAL     = IP.CDFILIAL                      AND
                   NF.NRLANCTONF   = IP.NRLANCTONF                    AND
                   IP.CDIMPOSTO    = P_CDIMPOSTO                      AND
                   IP.VRBASECALCNF > 0;
         END IF;
      END IF;

      IF V_VRBCPERIODO IS NULL THEN
         V_VRBCPERIODO := 0;
      END IF;

      IF (V_CALCIMPRET = 'S') THEN
         SELECT NVL(SUM(VRBCALCIRRF),0)
           INTO V_VRBASECALNF
           FROM ITEMPROIMPOS
          WHERE CDFILIAL = P_CDFILIAL
            AND NRLANCTONF = P_NRLANCTONF
            AND CDIMPOSTO = P_CDIMPOSTO
            AND VRBCALCIRRF > 0;

      ELSE
         SELECT NVL(SUM(VRBASECALCNF),0), NVL(MAX(VRALIQNF),0)
           INTO V_VRBASECALNF,     V_VRALIQUOTA
           FROM IMPOSTONF
          WHERE CDFILIAL = P_CDFILIAL
            AND NRLANCTONF = P_NRLANCTONF
            AND CDIMPOSTO = P_CDIMPOSTO
            AND VRBASECALCNF > 0;
      END IF;

      V_VRBCPERIODO := V_VRBCPERIODO - V_VRBASECALNF;

      IF (V_VRBCPERIODO <= V_VRMINBASE) OR ((V_CALCIMPRET = 'S') AND (V_VRBCPERIODO > V_VRMINBASE)) THEN

         IF ((V_VRBCPERIODO + V_VRBASECALNF) > V_VRMINBASE) THEN

            IF (V_CALCIMPRET = 'S') THEN /* Se utiliza base de calculo mensal e faixa de valores busca aliquota para a faixa do periodo*/

               DELETE FROM ALIQTEMP
                WHERE (NRALIQTEMP = P_CDFILIAL||P_NRLANCTONF);

               SELECT MAX(CDARVPROD)
                 INTO V_CDARVPROD
                 FROM PRODUTO P, ITEMPRODUTO IP, ITEMPROIMPOS IT
                WHERE P.CDPRODUTO    = IP.CDPRODUTO
                  AND IP.CDFILIAL    = P_CDFILIAL
                  AND IP.NRLANCTONF  = P_NRLANCTONF
                  AND IP.CDFILIAL    = IT.CDFILIAL
                  AND IP.NRLANCTONF  = IT.NRLANCTONF
                  AND IP.NRSEQITPROD = IT.NRSEQITPROD
                  AND IT.CDIMPOSTO   = P_CDIMPOSTO;

               IF (P_CDGRPFISCSV IS NOT NULL) THEN
                  CONSULTA_ALIQUOTA(P_CDIMPOSTO, P_IDENTRSAIDOP, P_CDEMPRESA, V_CDARVPROD, P_INSCESTA, P_PAISORIG,
                                    P_ESTAORIG, P_PAISDEST, P_ESTADEST, NULL, V_VRBCPERIODO + V_VRBASECALNF, P_DATALANCNF, P_CDFILIAL, P_NRLANCTONF, P_CDGRPFISCSV, P_TIPOOPER,
                                    NULL, NULL, NULL, NULL, NULL, 'N');
               ELSE
                  CONSULTA_ALIQUOTA(P_CDIMPOSTO, P_IDENTRSAIDOP, P_CDEMPRESA, V_CDARVPROD, P_INSCESTA, P_PAISORIG,
                                    P_ESTAORIG, P_PAISDEST, P_ESTADEST, NULL, V_VRBCPERIODO + V_VRBASECALNF, P_DATALANCNF, P_CDFILIAL, P_NRLANCTONF, P_CDGRUPOFISC, P_TIPOOPER,
                                    NULL, NULL, NULL, NULL, NULL, 'N');
               END IF;

               SELECT NVL(MAX(VRALIQUTMP),0), NVL(MAX(VRPARDEDTMP),0)
                 INTO V_VRALIQUOTA, V_PARDEDTMP
                 FROM ALIQTEMP
                WHERE (NRALIQTEMP = P_CDFILIAL||P_NRLANCTONF);

               DELETE FROM ALIQTEMP
                WHERE (NRALIQTEMP = P_CDFILIAL||P_NRLANCTONF);


               IF(P_IDENTRSAIDOP = 'E') THEN
                  IF V_IDFORMAPUR = 'P' THEN
                     SELECT SUM(VRIMPOSTONF), SUM(VRBASECALCNF)
                       INTO V_IMPOSTODESC, V_BASECALCDESC
                       FROM NOTAFISCAL NF, IMPOSTONF IP
                      WHERE NF.CDEMPRESA    = P_CDEMPRESA                      AND
                            NF.IDTPIJURNF   = P_IDTPIJURNF                     AND
                            ((NF.IDTPIJURNF   = 'J' AND SUBSTR(NF.NRINSJURNF,1,8) = SUBSTR(P_NRINSJURNF,1,8))
                              OR NF.NRINSJURNF = P_NRINSJURNF)                 AND
                            NF.IDENTRSAIDOP = P_IDENTRSAIDOP                   AND
                            NF.CDFILIAL     = IP.CDFILIAL                      AND
                            NF.NRLANCTONF   = IP.NRLANCTONF                    AND
                            IP.CDIMPOSTO    = P_CDIMPOSTO                      AND
                            NF.CDFILIAL||NF.NRLANCTONF <> P_CDFILIAL||P_NRLANCTONF AND
                            IP.VRIMPOSTONF > 0                                     AND
                            EXISTS (SELECT C.CDFILIAL
                                      FROM CONTAPAGAR C
                                     WHERE C.CDFILIAL = NF.CDFILIAL
                                       AND C.NRLANCTONF = NF.NRLANCTONF
                                       AND DTATUAVENPAG BETWEEN V_DTATUAPAG AND LAST_DAY(V_DTATUAPAG));
                  ELSE
                     SELECT SUM(VRIMPOSTONF), SUM(VRBASECALCNF)
                       INTO V_IMPOSTODESC, V_BASECALCDESC
                       FROM NOTAFISCAL NF, IMPOSTONF IP
                      WHERE NF.CDEMPRESA    = P_CDEMPRESA                      AND
                            NF.IDTPIJURNF   = P_IDTPIJURNF                     AND
                            ((NF.IDTPIJURNF   = 'J' AND SUBSTR(NF.NRINSJURNF,1,8) = SUBSTR(P_NRINSJURNF,1,8))
                              OR NF.NRINSJURNF = P_NRINSJURNF)                 AND
                            NF.IDENTRSAIDOP = P_IDENTRSAIDOP                   AND
                            NF.CDFILIAL     = IP.CDFILIAL                      AND
                            NF.NRLANCTONF   = IP.NRLANCTONF                    AND
                            IP.CDIMPOSTO    = P_CDIMPOSTO                      AND
                            NF.CDFILIAL||NF.NRLANCTONF <> P_CDFILIAL||P_NRLANCTONF AND
                            (((NF.DTENTRSAID   BETWEEN V_DATAINI AND V_DATALANCNF) AND (V_IDFORMAPUR <> 'E')) OR
                            ((NVL(NF.DTEMISSAO, NF.DTENTRSAID)   BETWEEN V_DATAINI AND V_DATALANCNF) AND (V_IDFORMAPUR = 'E'))) AND
                            IP.VRIMPOSTONF > 0;
                  END IF;
               ELSE
                  IF V_IDFORMAPUR = 'P' THEN
                     SELECT SUM(VRIMPOSTONF), SUM(VRBASECALCNF)
                       INTO V_IMPOSTODESC, V_BASECALCDESC
                       FROM NOTAFISCAL NF, IMPOSTONF IP
                      WHERE NF.CDEMPRESA    = P_CDEMPRESA                      AND
                            NF.IDTPIJURNF   = P_IDTPIJURNF                     AND
                            ((NF.IDTPIJURNF   = 'J' AND SUBSTR(NF.NRINSJURNF,1,8) = SUBSTR(P_NRINSJURNF,1,8))
                              OR NF.NRINSJURNF = P_NRINSJURNF)                 AND
                            NF.IDENTRSAIDOP = P_IDENTRSAIDOP                   AND
                            NF.CDFILIAL     = IP.CDFILIAL                      AND
                            NF.NRLANCTONF   = IP.NRLANCTONF                    AND
                            IP.CDIMPOSTO    = P_CDIMPOSTO                      AND
                            NF.CDFILIAL||NF.NRLANCTONF <> P_CDFILIAL||P_NRLANCTONF AND
                            IP.VRIMPOSTONF > 0                                     AND
                            EXISTS (SELECT C.CDFILIAL
                                      FROM CONTARECEBER C
                                     WHERE C.CDFILIAL = NF.CDFILIAL
                                       AND C.NRLANCTONF = NF.NRLANCTONF
                                       AND DTATUAVENREC BETWEEN V_DTATUAPAG AND LAST_DAY(V_DTATUAPAG));
                  ELSE
                     SELECT SUM(VRIMPOSTONF), SUM(VRBASECALCNF)
                       INTO V_IMPOSTODESC, V_BASECALCDESC
                       FROM NOTAFISCAL NF, IMPOSTONF IP
                      WHERE NF.CDEMPRESA    = P_CDEMPRESA                      AND
                            NF.IDTPIJURNF   = P_IDTPIJURNF                     AND
                            ((NF.IDTPIJURNF   = 'J' AND SUBSTR(NF.NRINSJURNF,1,8) = SUBSTR(P_NRINSJURNF,1,8))
                              OR NF.NRINSJURNF = P_NRINSJURNF)                 AND
                            NF.IDENTRSAIDOP = P_IDENTRSAIDOP                   AND
                            NF.CDFILIAL     = IP.CDFILIAL                      AND
                            NF.NRLANCTONF   = IP.NRLANCTONF                    AND
                            IP.CDIMPOSTO    = P_CDIMPOSTO                      AND
                            NF.CDFILIAL||NF.NRLANCTONF <> P_CDFILIAL||P_NRLANCTONF AND
                            (((NF.DTENTRSAID   BETWEEN V_DATAINI AND V_DATALANCNF) AND (V_IDFORMAPUR <> 'E')) OR
                            ((NVL(NF.DTEMISSAO, NF.DTENTRSAID)   BETWEEN V_DATAINI AND V_DATALANCNF) AND (V_IDFORMAPUR = 'E'))) AND
                            IP.VRIMPOSTONF > 0;
                  END IF;
               END IF;

               V_VRIMPOSTO := ((V_VRBCPERIODO + V_VRBASECALNF) * (V_VRALIQUOTA/100)) - NVL(V_IMPOSTODESC, 0) - NVL(V_PARDEDTMP, 0);
               V_VRBCALCIMPOIT := 0;

               /* Verifica se o existe valor maximo do imposto cadastrado. Caso exista e o valor calculado ultrapassar o valor cadastrado,
                  o imposto e recalculado novamente para ficar valor cadastrado */
               IF (V_VRMAXIMP > 0) THEN
                  IF(((V_VRBCPERIODO + V_VRBASECALNF) > ((V_VRMAXIMP * 100)/V_VRALIQUOTA))) THEN
                     V_VRIMPOSTO := (((V_VRMAXIMP * 100)/V_VRALIQUOTA) * (V_VRALIQUOTA/100)) - NVL(V_IMPOSTODESC, 0) - NVL(V_PARDEDTMP, 0);
                  END IF;
               END IF;

               /*
               IF (V_IDCALCBCMENSDIA = 'S') THEN
                  IF ((V_VRMINIMP > 0) AND (V_VRIMPOSTO < V_VRMINIMP)) THEN
                     V_VRIMPOSTO := 0;
                     IF P_IDENTRSAIDOP = 'E' THEN
                        UPDATE TRABIMPOSNFTOT
                        SET VRIMPOSTOT = 0.00
                        WHERE CDFILIMPTOT  = P_CDFILIAL   AND
                              NRLANCIMPTOT = P_NRLANCTONF AND
                              CDIMPOSTOT   = P_CDIMPOSTO;

                        UPDATE TRABITPROIMPOS
                        SET VRIMPOSIT    = 0.00,
                            VRALIQIMPOIT = 0.00
                        WHERE CDFILIMPOIT  = P_CDFILIAL   AND
                              NRLANCIMPOIT = P_NRLANCTONF AND
                              CDIMPOSTOIT  = P_CDIMPOSTO;
                     END IF;

                     UPDATE ITEMPROIMPOS
                     SET VRALIQITEM  = 0.00,
                         VRIMPOSTOIT = 0.00
                     WHERE CDFILIAL   = P_CDFILIAL   AND
                           NRLANCTONF = P_NRLANCTONF AND
                           CDIMPOSTO  = P_CDIMPOSTO;

                     UPDATE IMPOSTONF
                     SET VRIMPOSTONF = 0.00
                     WHERE CDFILIAL   = P_CDFILIAL   AND
                           NRLANCTONF = P_NRLANCTONF AND
                           CDIMPOSTO  = P_CDIMPOSTO;

                     UPDATE IMPOSNFTOT
                     SET VRTOTIMPONF = 0.00
                     WHERE CDFILIAL   = P_CDFILIAL   AND
                           NRLANCTONF = P_NRLANCTONF AND
                           CDIMPOSTO  = P_CDIMPOSTO;
                  END IF;
               END IF; */

               IF P_IDENTRSAIDOP = 'E' THEN

                  SELECT NVL(SUM(VRBCALCIMPOIT),0)
                    INTO V_VRBCALCIMPOIT
                    FROM TRABITPROIMPOS
                   WHERE CDFILIMPOIT = P_CDFILIAL
                     AND NRLANCIMPOIT = P_NRLANCTONF
                     AND CDIMPOSTOIT = P_CDIMPOSTO
                     AND NRSEQITPRO = '1';

                  V_VRBCALCIMPOIT := (V_VRBCPERIODO + V_VRBASECALNF) - NVL(V_BASECALCDESC, 0);

                  IF (V_VRMAXIMP > 0) THEN
                     IF(((V_VRBCPERIODO + V_VRBASECALNF) > ((V_VRMAXIMP * 100)/V_VRALIQUOTA))) THEN
                        V_VRBCALCIMPOIT := ROUND(((V_VRMAXIMP * 100)/V_VRALIQUOTA) - NVL(V_BASECALCDESC, 0), 2);
                     END IF;
                  END IF;

                  /*
                  IF (V_IDCALCBCMENSDIA = 'S') THEN
                     IF ((V_VRMINIMP > 0) AND (V_VRIMPOSTO < V_VRMINIMP)) THEN
                        V_VRIMPOSTO := 0;
                        IF P_IDENTRSAIDOP = 'E' THEN
                           UPDATE TRABIMPOSNFTOT
                           SET VRIMPOSTOT = 0.00
                           WHERE CDFILIMPTOT  = P_CDFILIAL   AND
                                 NRLANCIMPTOT = P_NRLANCTONF AND
                                 CDIMPOSTOT   = P_CDIMPOSTO;

                           UPDATE TRABITPROIMPOS
                           SET VRIMPOSIT    = 0.00,
                               VRALIQIMPOIT = 0.00
                           WHERE CDFILIMPOIT  = P_CDFILIAL   AND
                                 NRLANCIMPOIT = P_NRLANCTONF AND
                                 CDIMPOSTOIT  = P_CDIMPOSTO;
                        END IF;

                        UPDATE ITEMPROIMPOS
                        SET VRALIQITEM  = 0.00,
                            VRIMPOSTOIT = 0.00
                        WHERE CDFILIAL   = P_CDFILIAL   AND
                              NRLANCTONF = P_NRLANCTONF AND
                              CDIMPOSTO  = P_CDIMPOSTO;

                        UPDATE IMPOSTONF
                        SET VRIMPOSTONF = 0.00
                        WHERE CDFILIAL   = P_CDFILIAL   AND
                              NRLANCTONF = P_NRLANCTONF AND
                              CDIMPOSTO  = P_CDIMPOSTO;

                        UPDATE IMPOSNFTOT
                        SET VRTOTIMPONF = 0.00
                        WHERE CDFILIAL   = P_CDFILIAL   AND
                              NRLANCTONF = P_NRLANCTONF AND
                              CDIMPOSTO  = P_CDIMPOSTO;
                     END IF;
                  END IF;
                  */

                  IF (V_VRIMPOSTO < 0) THEN
                     V_VRIMPOSTO := 0;
                  END IF;

                  IF (V_VRBCALCIMPOIT < 0) THEN
                     V_VRBCALCIMPOIT := 0;
                  END IF;

                  UPDATE TRABIMPOSNFTOT
                     SET VRIMPOSTOT   = ROUND(V_VRIMPOSTO, V_NRDECITOTANF),
                         VRBCALIMPTOT = V_VRBCALCIMPOIT
                   WHERE CDFILIMPTOT  = P_CDFILIAL
                     AND NRLANCIMPTOT = P_NRLANCTONF
                     AND CDIMPOSTOT   = P_CDIMPOSTO;
               ELSE
                  SELECT NVL(SUM(VRBASECALCIT),0)
                    INTO V_VRBCALCIMPOIT
                    FROM ITEMPROIMPOS
                   WHERE CDFILIAL = P_CDFILIAL
                     AND NRLANCTONF = P_NRLANCTONF
                     AND CDIMPOSTO = P_CDIMPOSTO
                     AND NRITPROIMPOS = '1';

                  V_VRBCALCIMPOIT := (V_VRBCPERIODO + V_VRBASECALNF) - NVL(V_BASECALCDESC, 0);

                  IF (V_VRMAXIMP > 0) THEN
                     IF(((V_VRBCPERIODO + V_VRBASECALNF) > ((V_VRMAXIMP * 100)/V_VRALIQUOTA))) THEN
                        V_VRBCALCIMPOIT := ROUND(((V_VRMAXIMP * 100)/V_VRALIQUOTA) - NVL(V_BASECALCDESC, 0), 2);
                     END IF;
                  END IF;

                  /*
                  IF (V_IDCALCBCMENSDIA = 'S') THEN
                     IF ((V_VRMINIMP > 0) AND (V_VRIMPOSTO < V_VRMINIMP)) THEN
                        V_VRIMPOSTO := 0;
                        IF P_IDENTRSAIDOP = 'E' THEN
                           UPDATE TRABIMPOSNFTOT
                           SET VRIMPOSTOT = 0.00
                           WHERE CDFILIMPTOT  = P_CDFILIAL   AND
                                 NRLANCIMPTOT = P_NRLANCTONF AND
                                 CDIMPOSTOT   = P_CDIMPOSTO;

                           UPDATE TRABITPROIMPOS
                           SET VRIMPOSIT    = 0.00,
                               VRALIQIMPOIT = 0.00
                           WHERE CDFILIMPOIT  = P_CDFILIAL   AND
                                 NRLANCIMPOIT = P_NRLANCTONF AND
                                 CDIMPOSTOIT  = P_CDIMPOSTO;
                        END IF;

                        UPDATE ITEMPROIMPOS
                        SET VRALIQITEM  = 0.00,
                            VRIMPOSTOIT = 0.00
                        WHERE CDFILIAL   = P_CDFILIAL   AND
                              NRLANCTONF = P_NRLANCTONF AND
                              CDIMPOSTO  = P_CDIMPOSTO;

                        UPDATE IMPOSTONF
                        SET VRIMPOSTONF = 0.00
                        WHERE CDFILIAL   = P_CDFILIAL   AND
                              NRLANCTONF = P_NRLANCTONF AND
                              CDIMPOSTO  = P_CDIMPOSTO;

                        UPDATE IMPOSNFTOT
                        SET VRTOTIMPONF = 0.00
                        WHERE CDFILIAL   = P_CDFILIAL   AND
                              NRLANCTONF = P_NRLANCTONF AND
                              CDIMPOSTO  = P_CDIMPOSTO;
                     END IF;
                  END IF;
                  */

                  IF (V_VRIMPOSTO < 0) THEN
                     V_VRIMPOSTO := 0;
                  END IF;

                  IF (V_VRBCALCIMPOIT < 0) THEN
                     V_VRBCALCIMPOIT := 0;
                  END IF;

               END IF;
            ELSE
               V_VRIMPOSTO := (V_VRBCPERIODO + V_VRBASECALNF) * (V_VRALIQUOTA/100);
               V_VRBCALCIMPOIT := 0;
               /*
               IF (V_IDCALCBCMENSDIA = 'S') THEN
                  IF ((V_VRMINIMP > 0) AND (V_VRIMPOSTO < V_VRMINIMP)) THEN
                     V_VRIMPOSTO := 0;
                     IF P_IDENTRSAIDOP = 'E' THEN
                        UPDATE TRABIMPOSNFTOT
                        SET VRIMPOSTOT = 0.00
                        WHERE CDFILIMPTOT  = P_CDFILIAL   AND
                              NRLANCIMPTOT = P_NRLANCTONF AND
                              CDIMPOSTOT   = P_CDIMPOSTO;

                        UPDATE TRABITPROIMPOS
                        SET VRIMPOSIT    = 0.00,
                            VRALIQIMPOIT = 0.00
                        WHERE CDFILIMPOIT  = P_CDFILIAL   AND
                              NRLANCIMPOIT = P_NRLANCTONF AND
                              CDIMPOSTOIT  = P_CDIMPOSTO;
                     END IF;

                     UPDATE ITEMPROIMPOS
                     SET VRALIQITEM  = 0.00,
                         VRIMPOSTOIT = 0.00
                     WHERE CDFILIAL   = P_CDFILIAL   AND
                           NRLANCTONF = P_NRLANCTONF AND
                           CDIMPOSTO  = P_CDIMPOSTO;

                     UPDATE IMPOSTONF
                     SET VRIMPOSTONF = 0.00
                     WHERE CDFILIAL   = P_CDFILIAL   AND
                           NRLANCTONF = P_NRLANCTONF AND
                           CDIMPOSTO  = P_CDIMPOSTO;

                     UPDATE IMPOSNFTOT
                     SET VRTOTIMPONF = 0.00
                     WHERE CDFILIAL   = P_CDFILIAL   AND
                           NRLANCTONF = P_NRLANCTONF AND
                           CDIMPOSTO  = P_CDIMPOSTO;
                  END IF;
               END IF;
               */

               IF P_IDENTRSAIDOP = 'E' THEN
                  SELECT NVL(SUM(VRBCALCIMPOIT),0)
                    INTO V_VRBCALCIMPOIT
                    FROM TRABITPROIMPOS
                   WHERE CDFILIMPOIT = P_CDFILIAL
                     AND NRLANCIMPOIT = P_NRLANCTONF
                     AND CDIMPOSTOIT = P_CDIMPOSTO
                     AND NRSEQITPRO = '1'
                     AND VRALIQIMPOIT > 0;

                  UPDATE TRABIMPOSNFTOT
                     SET VRIMPOSTOT = ROUND(V_VRIMPOSTO, V_NRDECITOTANF),
                         VRBCALIMPTOT = V_VRBCALCIMPOIT
                   WHERE CDFILIMPTOT = P_CDFILIAL
                     AND NRLANCIMPTOT = P_NRLANCTONF
                     AND CDIMPOSTOT = P_CDIMPOSTO;
               ELSE
                  SELECT NVL(SUM(VRBASECALCIT),0)
                    INTO V_VRBCALCIMPOIT
                    FROM ITEMPROIMPOS
                   WHERE CDFILIAL = P_CDFILIAL
                     AND NRLANCTONF = P_NRLANCTONF
                     AND CDIMPOSTO = P_CDIMPOSTO
                     AND NRITPROIMPOS = '1'
                     AND VRALIQITEM > 0;
               END IF;
            END IF;

            IF (V_CALCIMPRET = 'S') THEN  /* Se utiliza base de calculo mensal e faixa de valores Continuar daqui*/
               FOR C_ITEM IN (
                  SELECT NRSEQITPROD, VRBCALCIRRF
                    FROM ITEMPROIMPOS
                   WHERE CDFILIAL = P_CDFILIAL
                     AND NRLANCTONF = P_NRLANCTONF
                     AND CDIMPOSTO = P_CDIMPOSTO
                     AND NRITPROIMPOS = '1')
               LOOP

                  V_FATOCONV := 0;
                  V_FATOCONV := (C_ITEM.VRBCALCIRRF / V_VRBASECALNF);

                  IF P_IDENTRSAIDOP = 'E' THEN   -- Fazer proporcional aqui
                     UPDATE TRABITPROIMPOS
                        SET VRIMPOSIT     = ROUND(((V_VRIMPOSTO * (V_VRBCPERIODO + V_VRBASECALNF))/ (V_VRBCPERIODO + V_VRBASECALNF)) * V_FATOCONV, V_NRDECITOTANF),
                            VRALIQIMPOIT  = V_VRALIQUOTA,
                            VRBCALCIMPOIT = ROUND((V_VRBCALCIMPOIT * V_FATOCONV), 2),
                            VRPDEDIMPOIT  = ROUND((V_PARDEDTMP * V_FATOCONV), 2)
                      WHERE CDFILIMPOIT   = P_CDFILIAL
                        AND NRLANCIMPOIT  = P_NRLANCTONF
                        AND NRSEQIMPOIT   = C_ITEM.NRSEQITPROD
                        AND CDIMPOSTOIT   = P_CDIMPOSTO
                        AND NRSEQITPRO    = '1';
                  END IF;

                  UPDATE ITEMPROIMPOS
                     SET VRBASECALCIT = ROUND((V_VRBCALCIMPOIT * V_FATOCONV), 2),
                         VRIMPOSTOIT  = ROUND(((V_VRIMPOSTO * (V_VRBCPERIODO + V_VRBASECALNF))/ (V_VRBCPERIODO + V_VRBASECALNF)) * V_FATOCONV, V_NRDECITOTANF),
                         VRALIQITEM   = V_VRALIQUOTA,
                         VRPARDEDIT   = ROUND((V_PARDEDTMP * V_FATOCONV), 2)
                   WHERE CDFILIAL     = P_CDFILIAL
                     AND NRLANCTONF   = P_NRLANCTONF
                     AND NRSEQITPROD  = C_ITEM.NRSEQITPROD
                     AND CDIMPOSTO    = P_CDIMPOSTO
                     AND NRITPROIMPOS = '1';

               END LOOP;

               UPDATE IMPOSTONF
                  SET VRIMPOSTONF  = ROUND(V_VRIMPOSTO, V_NRDECITOTANF),
                      VRBASECALCNF = V_VRBCALCIMPOIT,
                      VRPARDEDNF   = V_PARDEDTMP,
                      VRALIQNF     = V_VRALIQUOTA
                WHERE CDFILIAL     = P_CDFILIAL
                  AND NRLANCTONF   = P_NRLANCTONF
                  AND CDIMPOSTO    = P_CDIMPOSTO
                  AND IDIMPOSTONF  = '1';

               UPDATE IMPOSNFTOT
                  SET VRTOTIMPONF  = ROUND(V_VRIMPOSTO, V_NRDECITOTANF),
                      VRTOTBCALCNF = V_VRBCALCIMPOIT
                WHERE CDFILIAL     = P_CDFILIAL
                  AND NRLANCTONF   = P_NRLANCTONF
                  AND CDIMPOSTO    = P_CDIMPOSTO;

               UPDATE TRABIMPOSNFTOT
                  SET VRIMPOSTOT   = ROUND(V_VRIMPOSTO, V_NRDECITOTANF),
                      VRBCALIMPTOT = V_VRBCALCIMPOIT
                WHERE CDFILIMPTOT  = P_CDFILIAL
                  AND NRLANCIMPTOT = P_NRLANCTONF
                  AND CDIMPOSTOT   = P_CDIMPOSTO;

               --Atualiza valor da impostonf de acordo com a soma dos impostos na itemproimpos

               V_VRDIFERENCA := 0;
               V_VRTOTIMPONF := 0;
               V_VRIMPOSTOIT := 0;

               SELECT NVL(SUM(VRIMPOSTOIT),0) AS VRIMPOSTOIT
                 INTO V_VRIMPOSTOIT
                 FROM ITEMPROIMPOS
                WHERE CDFILIAL     = P_CDFILIAL
                  AND NRLANCTONF   = P_NRLANCTONF
                  AND CDIMPOSTO    = P_CDIMPOSTO
                  AND NRITPROIMPOS = '1';

               SELECT NVL(SUM(VRIMPOSTONF),0)
                 INTO V_VRTOTIMPONF
                 FROM IMPOSTONF
                WHERE CDFILIAL    = P_CDFILIAL
                  AND NRLANCTONF  = P_NRLANCTONF
                  AND CDIMPOSTO   = P_CDIMPOSTO
                  AND IDIMPOSTONF = '1';

               IF (V_VRIMPOSTOIT > 0) THEN

                  V_VRDIFERENCA := V_VRTOTIMPONF - V_VRIMPOSTOIT;

                  IF V_VRDIFERENCA <> 0 THEN

                     IF P_IDENTRSAIDOP = 'E' THEN
                        UPDATE TRABIMPOSNFTOT
                           SET VRIMPOSTOT   = VRIMPOSTOT + V_VRDIFERENCA
                         WHERE CDFILIMPTOT  = P_CDFILIAL
                           AND NRLANCIMPTOT = P_NRLANCTONF
                           AND CDIMPOSTOT   = P_CDIMPOSTO;
                     END IF;

                     UPDATE IMPOSTONF
                        SET VRIMPOSTONF = VRIMPOSTONF + V_VRDIFERENCA
                      WHERE CDFILIAL    = P_CDFILIAL
                        AND NRLANCTONF  = P_NRLANCTONF
                        AND CDIMPOSTO   = P_CDIMPOSTO
                        AND IDIMPOSTONF = '1'
                        AND VRALIQNF > 0;

                    UPDATE IMPOSNFTOT
                       SET VRTOTIMPONF = VRTOTIMPONF + V_VRDIFERENCA
                     WHERE CDFILIAL    = P_CDFILIAL
                       AND NRLANCTONF  = P_NRLANCTONF
                       AND CDIMPOSTO   = P_CDIMPOSTO;
                  END IF;
               END IF;
            ELSE
               FOR C_ITEM IN (
                  SELECT NRSEQITPROD
                    FROM ITEMPROIMPOS
                   WHERE CDFILIAL = P_CDFILIAL
                     AND NRLANCTONF = P_NRLANCTONF
                     AND CDIMPOSTO = P_CDIMPOSTO
                     AND NRITPROIMPOS = '1'
                     AND VRALIQITEM > 0)
               LOOP
                  IF P_IDENTRSAIDOP = 'E' THEN
                     UPDATE TRABITPROIMPOS
                        SET VRIMPOSIT = ROUND(((V_VRIMPOSTO * VRBCALCIMPOIT)/ V_VRBCALCIMPOIT), V_NRDECITOTANF)
                      WHERE CDFILIMPOIT = P_CDFILIAL
                        AND NRLANCIMPOIT = P_NRLANCTONF
                        AND NRSEQIMPOIT = C_ITEM.NRSEQITPROD
                        AND CDIMPOSTOIT = P_CDIMPOSTO
                        AND NRSEQITPRO = '1';
                  END IF;

                  UPDATE ITEMPROIMPOS
                     SET VRIMPOSTOIT  = ROUND(((V_VRIMPOSTO * VRBASECALCIT)/ V_VRBCALCIMPOIT),V_NRDECITOTANF)
                   WHERE CDFILIAL     = P_CDFILIAL
                     AND NRLANCTONF   = P_NRLANCTONF
                     AND NRSEQITPROD  = C_ITEM.NRSEQITPROD
                     AND CDIMPOSTO    = P_CDIMPOSTO
                     AND NRITPROIMPOS = '1';
               END LOOP;

               UPDATE IMPOSTONF
                  SET VRIMPOSTONF = ROUND(V_VRIMPOSTO, V_NRDECITOTANF)
                WHERE CDFILIAL = P_CDFILIAL
                  AND NRLANCTONF = P_NRLANCTONF
                  AND CDIMPOSTO = P_CDIMPOSTO
                  AND IDIMPOSTONF = '1'
                  AND VRALIQNF > 0;

               UPDATE IMPOSNFTOT
                  SET VRTOTIMPONF = ROUND(V_VRIMPOSTO, V_NRDECITOTANF),
                      VRTOTBCALCNF = V_VRBCALCIMPOIT
                WHERE CDFILIAL = P_CDFILIAL
                  AND NRLANCTONF = P_NRLANCTONF
                  AND CDIMPOSTO = P_CDIMPOSTO;

               --Atualiza valor da impostonf de acordo com a soma dos impostof na itemproimpos

               V_VRDIFERENCA := 0;
               V_VRTOTIMPONF := 0;
               V_VRIMPOSTOIT := 0;

               SELECT NVL(SUM(VRIMPOSTOIT),0) AS VRIMPOSTOIT
                 INTO V_VRIMPOSTOIT
                 FROM ITEMPROIMPOS
                WHERE CDFILIAL     = P_CDFILIAL
                  AND NRLANCTONF   = P_NRLANCTONF
                  AND CDIMPOSTO    = P_CDIMPOSTO
                  AND NRITPROIMPOS = '1'
                  AND VRALIQITEM > 0;

               SELECT NVL(SUM(VRIMPOSTONF),0)
                 INTO V_VRTOTIMPONF
                 FROM IMPOSTONF
                WHERE CDFILIAL    = P_CDFILIAL
                  AND NRLANCTONF  = P_NRLANCTONF
                  AND CDIMPOSTO   = P_CDIMPOSTO
                  AND IDIMPOSTONF = '1'
                  AND VRALIQNF > 0;

               IF (V_VRIMPOSTOIT > 0) THEN

                  V_VRDIFERENCA := V_VRTOTIMPONF - V_VRIMPOSTOIT;

                  IF V_VRDIFERENCA <> 0 THEN

                     IF P_IDENTRSAIDOP = 'E' THEN
                        UPDATE TRABIMPOSNFTOT
                           SET VRIMPOSTOT   = VRIMPOSTOT + V_VRDIFERENCA
                         WHERE CDFILIMPTOT  = P_CDFILIAL
                           AND NRLANCIMPTOT = P_NRLANCTONF
                           AND CDIMPOSTOT   = P_CDIMPOSTO;
                     END IF;

                     UPDATE IMPOSTONF
                        SET VRIMPOSTONF = VRIMPOSTONF + V_VRDIFERENCA
                      WHERE CDFILIAL    = P_CDFILIAL
                        AND NRLANCTONF  = P_NRLANCTONF
                        AND CDIMPOSTO   = P_CDIMPOSTO
                        AND IDIMPOSTONF = '1'
                        AND VRALIQNF > 0;

                     UPDATE IMPOSNFTOT
                        SET VRTOTIMPONF = VRTOTIMPONF + V_VRDIFERENCA
                      WHERE CDFILIAL    = P_CDFILIAL
                        AND NRLANCTONF  = P_NRLANCTONF
                        AND CDIMPOSTO   = P_CDIMPOSTO;
                  END IF;
               END IF;
            END IF;

            SELECT NVL(MAX(NRSEQUNOTAIT),0)
              INTO V_NRSEQAUX
              FROM NOTABCMENSAL
             WHERE CDFILIAL = P_CDFILIAL
               AND NRLANCTONF = P_NRLANCTONF
               AND CDIMPOSTO = P_CDIMPOSTO
               AND NOT EXISTS (SELECT N.CDFILIAL
                                 FROM NOTABCMENSAL N
                                WHERE N.CDFILIALIT = P_CDFILIAL
                                  AND N.NRLANCTONFIT = P_NRLANCTONF
                                  AND CDIMPOSTO = P_CDIMPOSTO);

            IF V_NRSEQAUX <> 0 THEN
               SELECT NVL(MAX(NRSEQUNOTAIT),0)
                 INTO V_NRSEQAUX
                 FROM NOTABCMENSAL
                WHERE CDFILIAL = P_CDFILIAL
                  AND NRLANCTONF = P_NRLANCTONF
                  AND NOT EXISTS (SELECT N.CDFILIAL
                                    FROM NOTABCMENSAL N
                                   WHERE N.CDFILIALIT = P_CDFILIAL
                                     AND N.NRLANCTONFIT = P_NRLANCTONF);

               INSERT INTO NOTABCMENSAL
                  (CDFILIAL, NRLANCTONF, NRSEQUNOTAIT, CDFILIALIT, NRLANCTONFIT, IDNFBCMENSALCALC, CDIMPOSTO)
               VALUES
                  (P_CDFILIAL, P_NRLANCTONF, LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQAUX) + 1)),4,0), P_CDFILIAL, P_NRLANCTONF, 'S', P_CDIMPOSTO);
               IF (V_IDCALCBCMENSDIA = 'S') THEN

                  IF NOT(V_DATALANCNF < V_DATAREFIMP) THEN
                     UPDATE IMPOSTO
                        SET DTANTREFSEMPER = DTINIREFSEMPER
                      WHERE CDIMPOSTO = P_CDIMPOSTO;
                  END IF;

                  UPDATE IMPOSTO
                     SET DTINIREFSEMPER = (SELECT ADD_MONTHS(TO_DATE ('01/'||(TO_CHAR(V_DATALANCNF, 'MMYYYY'))), 1) FROM DUAL)
                   WHERE CDIMPOSTO = P_CDIMPOSTO;

               END IF;

            END IF;
         ELSE
            IF P_IDENTRSAIDOP = 'E' THEN
               UPDATE TRABIMPOSNFTOT
               SET VRIMPOSTOT = 0.00
               WHERE CDFILIMPTOT  = P_CDFILIAL   AND
                     NRLANCIMPTOT = P_NRLANCTONF AND
                     CDIMPOSTOT   = P_CDIMPOSTO;

               UPDATE TRABITPROIMPOS
               SET VRIMPOSIT    = 0.00,
                   VRALIQIMPOIT = 0.00
               WHERE CDFILIMPOIT  = P_CDFILIAL   AND
                     NRLANCIMPOIT = P_NRLANCTONF AND
                     CDIMPOSTOIT  = P_CDIMPOSTO;
            END IF;

            UPDATE ITEMPROIMPOS
            SET VRALIQITEM  = 0.00,
                VRIMPOSTOIT = 0.00
            WHERE CDFILIAL   = P_CDFILIAL   AND
                  NRLANCTONF = P_NRLANCTONF AND
                  CDIMPOSTO  = P_CDIMPOSTO;

            UPDATE IMPOSTONF
            SET VRIMPOSTONF = 0.00
            WHERE CDFILIAL   = P_CDFILIAL   AND
                  NRLANCTONF = P_NRLANCTONF AND
                  CDIMPOSTO  = P_CDIMPOSTO;

            UPDATE IMPOSNFTOT
            SET VRTOTIMPONF = 0.00
            WHERE CDFILIAL   = P_CDFILIAL   AND
                  NRLANCTONF = P_NRLANCTONF AND
                  CDIMPOSTO  = P_CDIMPOSTO;
         END IF;

         IF V_IDFORMAPUR = 'P' THEN
            IF P_IDENTRSAIDOP = 'E' THEN
               IF (V_CALCIMPRET = 'S') THEN
                  FOR C_NOTABCMENSAL IN (SELECT NF.CDFILIAL, NF.NRLANCTONF
                                           FROM NOTAFISCAL NF, ITEMPROIMPOS IP
                                          WHERE NF.CDEMPRESA    = P_CDEMPRESA
                                            AND NF.IDTPIJURNF   = P_IDTPIJURNF
                                            AND ((NF.IDTPIJURNF = 'J' AND SUBSTR(NF.NRINSJURNF,1,8) = SUBSTR(P_NRINSJURNF,1,8))
                                             OR NF.NRINSJURNF   = P_NRINSJURNF)
                                            AND NF.IDENTRSAIDOP = P_IDENTRSAIDOP
                                            AND NF.CDFILIAL     = IP.CDFILIAL
                                            AND NF.NRLANCTONF   = IP.NRLANCTONF
                                            AND IP.CDIMPOSTO    = P_CDIMPOSTO
                                            AND NF.CDFILIAL||NF.NRLANCTONF <> P_CDFILIAL||P_NRLANCTONF
                                            AND IP.VRBCALCIRRF > 0
                                            AND EXISTS (SELECT C.CDFILIAL
                                                          FROM CONTAPAGAR C
                                                         WHERE C.CDFILIAL = NF.CDFILIAL
                                                           AND C.NRLANCTONF = NF.NRLANCTONF
                                                           AND DTATUAVENPAG BETWEEN V_DTATUAPAG AND LAST_DAY(V_DTATUAPAG))
                                            AND NOT EXISTS (SELECT N.CDFILIAL
                                                              FROM NOTABCMENSAL N
                                                             WHERE N.CDFILIALIT = P_CDFILIAL
                                                               AND N.NRLANCTONFIT = P_NRLANCTONF
                                                               AND CDIMPOSTO = P_CDIMPOSTO))
                  LOOP

                     SELECT MAX(NRSEQUNOTAIT)
                       INTO V_NRSEQUNOTAIT
                       FROM NOTABCMENSAL
                      WHERE CDFILIAL     = P_CDFILIAL
                        AND NRLANCTONF   = P_NRLANCTONF
                        AND CDFILIALIT   = C_NOTABCMENSAL.CDFILIAL
                        AND NRLANCTONFIT = C_NOTABCMENSAL.NRLANCTONF;
                     IF (V_NRSEQUNOTAIT IS NULL) THEN
                        SELECT NVL(MAX(NRSEQUNOTAIT),0)
                          INTO V_NRSEQAUX
                          FROM NOTABCMENSAL
                         WHERE CDFILIAL = P_CDFILIAL
                           AND NRLANCTONF = P_NRLANCTONF;

                        INSERT INTO NOTABCMENSAL
                           (CDFILIAL, NRLANCTONF, NRSEQUNOTAIT, CDFILIALIT, NRLANCTONFIT, IDNFBCMENSALCALC, CDIMPOSTO)
                        VALUES
                           (P_CDFILIAL, P_NRLANCTONF, LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQAUX) + 1)),4,0), C_NOTABCMENSAL.CDFILIAL, C_NOTABCMENSAL.NRLANCTONF, 'N', P_CDIMPOSTO);

                        IF (V_IDCALCBCMENSDIA = 'S') THEN

                           IF NOT(V_DATALANCNF < V_DATAREFIMP) THEN
                              UPDATE IMPOSTO
                                 SET DTANTREFSEMPER = DTINIREFSEMPER
                               WHERE CDIMPOSTO = P_CDIMPOSTO;
                           END IF;

                           UPDATE IMPOSTO
                              SET DTINIREFSEMPER = (SELECT ADD_MONTHS(TO_DATE ('01/'||(TO_CHAR(V_DATALANCNF, 'MMYYYY'))), 1) FROM DUAL)
                            WHERE CDIMPOSTO = P_CDIMPOSTO;

                        END IF;
                     END IF;
                  END LOOP;
               ELSE
                  FOR C_NOTABCMENSAL IN (SELECT NF.CDFILIAL, NF.NRLANCTONF
                                           FROM NOTAFISCAL NF, IMPOSTONF IP
                                          WHERE NF.CDEMPRESA    = P_CDEMPRESA
                                            AND NF.IDTPIJURNF   = P_IDTPIJURNF
                                            AND ((NF.IDTPIJURNF = 'J' AND SUBSTR(NF.NRINSJURNF,1,8) = SUBSTR(P_NRINSJURNF,1,8))
                                             OR NF.NRINSJURNF   = P_NRINSJURNF)
                                            AND NF.IDENTRSAIDOP = P_IDENTRSAIDOP
                                            AND NF.CDFILIAL     = IP.CDFILIAL
                                            AND NF.NRLANCTONF   = IP.NRLANCTONF
                                            AND IP.CDIMPOSTO    = P_CDIMPOSTO
                                            AND NF.CDFILIAL||NF.NRLANCTONF <> P_CDFILIAL||P_NRLANCTONF
                                            AND IP.VRBASECALCNF > 0
                                            AND EXISTS (SELECT C.CDFILIAL
                                                          FROM CONTAPAGAR C
                                                         WHERE C.CDFILIAL = NF.CDFILIAL
                                                           AND C.NRLANCTONF = NF.NRLANCTONF
                                                           AND DTORIGVENPAG BETWEEN V_DTATUAPAG AND LAST_DAY(V_DTATUAPAG))
                                            AND NOT EXISTS (SELECT N.CDFILIAL
                                                              FROM NOTABCMENSAL N
                                                             WHERE N.CDFILIALIT = P_CDFILIAL
                                                               AND N.NRLANCTONFIT = P_NRLANCTONF
                                                               AND CDIMPOSTO = P_CDIMPOSTO))
                  LOOP

                     SELECT MAX(NRSEQUNOTAIT)
                       INTO V_NRSEQUNOTAIT
                       FROM NOTABCMENSAL
                      WHERE CDFILIAL     = P_CDFILIAL
                        AND NRLANCTONF   = P_NRLANCTONF
                        AND CDFILIALIT   = C_NOTABCMENSAL.CDFILIAL
                        AND NRLANCTONFIT = C_NOTABCMENSAL.NRLANCTONF;
                     IF (V_NRSEQUNOTAIT IS NULL) THEN
                        SELECT NVL(MAX(NRSEQUNOTAIT),0)
                          INTO V_NRSEQAUX
                          FROM NOTABCMENSAL
                         WHERE CDFILIAL = P_CDFILIAL
                           AND NRLANCTONF = P_NRLANCTONF;

                        INSERT INTO NOTABCMENSAL
                           (CDFILIAL, NRLANCTONF, NRSEQUNOTAIT, CDFILIALIT, NRLANCTONFIT, IDNFBCMENSALCALC, CDIMPOSTO)
                        VALUES
                           (P_CDFILIAL, P_NRLANCTONF, LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQAUX) + 1)),4,0), C_NOTABCMENSAL.CDFILIAL, C_NOTABCMENSAL.NRLANCTONF, 'N', P_CDIMPOSTO);

                        IF (V_IDCALCBCMENSDIA = 'S') THEN

                           IF NOT(V_DATALANCNF < V_DATAREFIMP) THEN
                              UPDATE IMPOSTO
                                 SET DTANTREFSEMPER = DTINIREFSEMPER
                               WHERE CDIMPOSTO = P_CDIMPOSTO;
                           END IF;

                           UPDATE IMPOSTO
                              SET DTINIREFSEMPER = (SELECT ADD_MONTHS(TO_DATE ('01/'||(TO_CHAR(V_DATALANCNF, 'MMYYYY'))), 1) FROM DUAL)
                            WHERE CDIMPOSTO = P_CDIMPOSTO;
                        END IF;

                     END IF;
                  END LOOP;
               END IF;
            ELSE
               FOR C_NOTABCMENSAL IN (SELECT NF.CDFILIAL, NF.NRLANCTONF
                                        FROM NOTAFISCAL NF, IMPOSTONF IP
                                       WHERE NF.CDEMPRESA    = P_CDEMPRESA
                                         AND NF.IDTPIJURNF   = P_IDTPIJURNF
                                         AND ((NF.IDTPIJURNF = 'J' AND SUBSTR(NF.NRINSJURNF,1,8) = SUBSTR(P_NRINSJURNF,1,8))
                                          OR NF.NRINSJURNF   = P_NRINSJURNF)
                                         AND NF.IDENTRSAIDOP = P_IDENTRSAIDOP
                                         AND NF.CDFILIAL     = IP.CDFILIAL
                                         AND NF.NRLANCTONF   = IP.NRLANCTONF
                                         AND IP.CDIMPOSTO    = P_CDIMPOSTO
                                         AND NF.CDFILIAL||NF.NRLANCTONF <> P_CDFILIAL||P_NRLANCTONF
                                         AND IP.VRBASECALCNF > 0
                                         AND EXISTS (SELECT C.CDFILIAL
                                                       FROM CONTARECEBER C
                                                      WHERE C.CDFILIAL = NF.CDFILIAL
                                                        AND C.NRLANCTONF = NF.NRLANCTONF
                                                        AND DTATUAVENREC BETWEEN V_DTATUAPAG AND LAST_DAY(V_DTATUAPAG))
                                         AND NOT EXISTS (SELECT N.CDFILIAL
                                                           FROM NOTABCMENSAL N
                                                          WHERE N.CDFILIALIT = P_CDFILIAL
                                                            AND N.NRLANCTONFIT = P_NRLANCTONF
                                                            AND CDIMPOSTO = P_CDIMPOSTO))
               LOOP
                  SELECT MAX(NRSEQUNOTAIT)
                    INTO V_NRSEQUNOTAIT
                    FROM NOTABCMENSAL
                   WHERE CDFILIAL     = P_CDFILIAL
                     AND NRLANCTONF   = P_NRLANCTONF
                     AND CDFILIALIT   = C_NOTABCMENSAL.CDFILIAL
                     AND NRLANCTONFIT = C_NOTABCMENSAL.NRLANCTONF;
                  IF (V_NRSEQUNOTAIT IS NULL) THEN
                     SELECT NVL(MAX(NRSEQUNOTAIT),0)
                       INTO V_NRSEQAUX
                       FROM NOTABCMENSAL
                      WHERE CDFILIAL = P_CDFILIAL
                        AND NRLANCTONF = P_NRLANCTONF;

                     INSERT INTO NOTABCMENSAL
                        (CDFILIAL, NRLANCTONF, NRSEQUNOTAIT, CDFILIALIT, NRLANCTONFIT, IDNFBCMENSALCALC, CDIMPOSTO)
                     VALUES
                        (P_CDFILIAL, P_NRLANCTONF, LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQAUX) + 1)),4,0), C_NOTABCMENSAL.CDFILIAL, C_NOTABCMENSAL.NRLANCTONF, 'N', P_CDIMPOSTO);

                     IF (V_IDCALCBCMENSDIA = 'S') THEN
                        IF NOT(V_DATALANCNF < V_DATAREFIMP) THEN
                           UPDATE IMPOSTO
                              SET DTANTREFSEMPER = DTINIREFSEMPER
                            WHERE CDIMPOSTO = P_CDIMPOSTO;
                        END IF;

                        UPDATE IMPOSTO
                           SET DTINIREFSEMPER = (SELECT ADD_MONTHS(TO_DATE ('01/'||(TO_CHAR(V_DATALANCNF, 'MMYYYY'))), 1) FROM DUAL)
                         WHERE CDIMPOSTO = P_CDIMPOSTO;
                     END IF;

                  END IF;
               END LOOP;
            END IF;
         ELSE
            IF (V_CALCIMPRET = 'S') THEN
               FOR C_NOTABCMENSAL IN(SELECT NF.CDFILIAL, NF.NRLANCTONF
                                       FROM NOTAFISCAL NF, ITEMPROIMPOS IP
                                      WHERE NF.CDEMPRESA    = P_CDEMPRESA
                                        AND NF.IDTPIJURNF   = P_IDTPIJURNF
                                        AND ((NF.IDTPIJURNF = 'J' AND SUBSTR(NF.NRINSJURNF,1,8) = SUBSTR(P_NRINSJURNF,1,8))
                                         OR NF.NRINSJURNF   = P_NRINSJURNF)
                                        AND NF.IDENTRSAIDOP = P_IDENTRSAIDOP
                                        AND (((NF.DTENTRSAID BETWEEN V_DATAINI AND V_DATALANCNF) AND (V_IDFORMAPUR <> 'E'))
                                         OR ((NVL(NF.DTEMISSAO, NF.DTENTRSAID) BETWEEN V_DATAINI AND V_DATALANCNF) AND (V_IDFORMAPUR = 'E')))
                                        AND NF.CDFILIAL     = IP.CDFILIAL
                                        AND NF.NRLANCTONF   = IP.NRLANCTONF
                                        AND IP.CDIMPOSTO    = P_CDIMPOSTO
                                        AND NF.CDFILIAL||NF.NRLANCTONF <> P_CDFILIAL||P_NRLANCTONF
                                        AND IP.VRBCALCIRRF > 0
                                        AND NOT EXISTS (SELECT N.CDFILIAL
                                                          FROM NOTABCMENSAL N
                                                         WHERE N.CDFILIALIT   = P_CDFILIAL
                                                           AND N.NRLANCTONFIT = P_NRLANCTONF
                                                           AND CDIMPOSTO      = P_CDIMPOSTO))
               LOOP
                  SELECT MAX(NRSEQUNOTAIT)
                    INTO V_NRSEQUNOTAIT
                    FROM NOTABCMENSAL
                   WHERE CDFILIAL     = P_CDFILIAL
                     AND NRLANCTONF   = P_NRLANCTONF
                     AND CDFILIALIT   = C_NOTABCMENSAL.CDFILIAL
                     AND NRLANCTONFIT = C_NOTABCMENSAL.NRLANCTONF;
                  IF (V_NRSEQUNOTAIT IS NULL) THEN
                     SELECT NVL(MAX(NRSEQUNOTAIT),0)
                       INTO V_NRSEQAUX
                       FROM NOTABCMENSAL
                      WHERE CDFILIAL = P_CDFILIAL
                        AND NRLANCTONF = P_NRLANCTONF;

                     INSERT INTO NOTABCMENSAL
                        (CDFILIAL, NRLANCTONF, NRSEQUNOTAIT, CDFILIALIT, NRLANCTONFIT, IDNFBCMENSALCALC, CDIMPOSTO)
                     VALUES
                        (P_CDFILIAL, P_NRLANCTONF, LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQAUX) + 1)),4,0), C_NOTABCMENSAL.CDFILIAL, C_NOTABCMENSAL.NRLANCTONF, 'N', P_CDIMPOSTO);

                     IF (V_IDCALCBCMENSDIA = 'S') THEN

                        IF NOT(V_DATALANCNF < V_DATAREFIMP) THEN
                           UPDATE IMPOSTO
                              SET DTANTREFSEMPER = DTINIREFSEMPER
                            WHERE CDIMPOSTO = P_CDIMPOSTO;
                        END IF;

                        UPDATE IMPOSTO
                           SET DTINIREFSEMPER = (SELECT ADD_MONTHS(TO_DATE ('01/'||(TO_CHAR(V_DATALANCNF, 'MMYYYY'))), 1) FROM DUAL)
                         WHERE CDIMPOSTO = P_CDIMPOSTO;

                     END IF;
                  END IF;
               END LOOP;
            ELSE
               FOR C_NOTABCMENSAL IN(SELECT NF.CDFILIAL, NF.NRLANCTONF
                                       FROM NOTAFISCAL NF, IMPOSTONF IP
                                      WHERE NF.CDEMPRESA    = P_CDEMPRESA
                                        AND NF.IDTPIJURNF   = P_IDTPIJURNF
                                        AND ((NF.IDTPIJURNF = 'J' AND SUBSTR(NF.NRINSJURNF,1,8) = SUBSTR(P_NRINSJURNF,1,8))
                                         OR NF.NRINSJURNF   = P_NRINSJURNF)
                                        AND NF.IDENTRSAIDOP = P_IDENTRSAIDOP
                                        AND (((NF.DTENTRSAID BETWEEN V_DATAINI AND V_DATALANCNF) AND (V_IDFORMAPUR <> 'E'))
                                         OR ((NVL(NF.DTEMISSAO, NF.DTENTRSAID) BETWEEN V_DATAINI AND V_DATALANCNF) AND (V_IDFORMAPUR = 'E')))
                                        AND NF.CDFILIAL     = IP.CDFILIAL
                                        AND NF.NRLANCTONF   = IP.NRLANCTONF
                                        AND IP.CDIMPOSTO    = P_CDIMPOSTO
                                        AND NF.CDFILIAL||NF.NRLANCTONF <> P_CDFILIAL||P_NRLANCTONF
                                        AND IP.VRBASECALCNF > 0
                                        AND NOT EXISTS (SELECT N.CDFILIAL
                                                          FROM NOTABCMENSAL N
                                                         WHERE N.CDFILIALIT = P_CDFILIAL
                                                           AND N.NRLANCTONFIT = P_NRLANCTONF
                                                           AND CDIMPOSTO = P_CDIMPOSTO))
               LOOP
                  SELECT MAX(NRSEQUNOTAIT)
                    INTO V_NRSEQUNOTAIT
                    FROM NOTABCMENSAL
                   WHERE CDFILIAL     = P_CDFILIAL
                     AND NRLANCTONF   = P_NRLANCTONF
                     AND CDFILIALIT   = C_NOTABCMENSAL.CDFILIAL
                     AND NRLANCTONFIT = C_NOTABCMENSAL.NRLANCTONF;
                  IF (V_NRSEQUNOTAIT IS NULL) THEN
                     SELECT NVL(MAX(NRSEQUNOTAIT),0)
                       INTO V_NRSEQAUX
                       FROM NOTABCMENSAL
                      WHERE CDFILIAL = P_CDFILIAL
                        AND NRLANCTONF = P_NRLANCTONF;

                     INSERT INTO NOTABCMENSAL
                        (CDFILIAL, NRLANCTONF, NRSEQUNOTAIT, CDFILIALIT, NRLANCTONFIT, IDNFBCMENSALCALC, CDIMPOSTO)
                     VALUES
                        (P_CDFILIAL, P_NRLANCTONF, LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQAUX) + 1)),4,0), C_NOTABCMENSAL.CDFILIAL, C_NOTABCMENSAL.NRLANCTONF, 'N', P_CDIMPOSTO);

                     IF (V_IDCALCBCMENSDIA = 'S') THEN

                        IF NOT(V_DATALANCNF < V_DATAREFIMP) THEN
                           UPDATE IMPOSTO
                              SET DTANTREFSEMPER = DTINIREFSEMPER
                            WHERE CDIMPOSTO = P_CDIMPOSTO;
                        END IF;

                        UPDATE IMPOSTO
                           SET DTINIREFSEMPER = (SELECT ADD_MONTHS(TO_DATE ('01/'||(TO_CHAR(V_DATALANCNF, 'MMYYYY'))), 1) FROM DUAL)
                         WHERE CDIMPOSTO = P_CDIMPOSTO;

                     END IF;
                  END IF;
               END LOOP;
            END IF;
         END IF;
      END IF;
   ELSE
      IF P_IDENTRSAIDOP = 'E' THEN
         UPDATE TRABIMPOSNFTOT
            SET VRIMPOSTOT = 0.00
          WHERE CDFILIMPTOT = P_CDFILIAL
            AND NRLANCIMPTOT = P_NRLANCTONF
            AND CDIMPOSTOT = P_CDIMPOSTO;

         UPDATE TRABITPROIMPOS
            SET VRIMPOSIT    = 0.00,
                VRALIQIMPOIT = 0.00
          WHERE CDFILIMPOIT = P_CDFILIAL
            AND NRLANCIMPOIT = P_NRLANCTONF
            AND CDIMPOSTOIT = P_CDIMPOSTO;
      END IF;

      UPDATE ITEMPROIMPOS
         SET VRALIQITEM  = 0.00,
             VRIMPOSTOIT = 0.00
       WHERE CDFILIAL = P_CDFILIAL
         AND NRLANCTONF = P_NRLANCTONF
         AND CDIMPOSTO = P_CDIMPOSTO;

      UPDATE IMPOSTONF
         SET VRIMPOSTONF = 0.00
       WHERE CDFILIAL = P_CDFILIAL
         AND NRLANCTONF = P_NRLANCTONF
         AND CDIMPOSTO = P_CDIMPOSTO;

      UPDATE IMPOSNFTOT
         SET VRTOTIMPONF = 0.00
       WHERE CDFILIAL = P_CDFILIAL
         AND NRLANCTONF = P_NRLANCTONF
         AND CDIMPOSTO  = P_CDIMPOSTO;
   END IF;
END;
/