CREATE OR REPLACE PROCEDURE CONSULTA_INCIDENCIA(
    P_ENTRSAIDIN    IN INCIDENCIA.IDENTRSAIDIN%TYPE,
    P_EMPRESA       IN INCIDENCIA.CDEMPRESA%TYPE,
    P_PRODUTO       IN PRODUTO.CDARVPROD%TYPE,
    P_INSCESTA      IN INCIDENCIA.CDINSCESTA%TYPE,
    P_TIPOOPER      IN INCIDENCIA.CDTIPOOPER%TYPE,
    P_FILIAL        IN VARCHAR2,
    P_LANCTONF      IN NOTAFISCAL.NRLANCTONF%TYPE,
    P_CDGRUPOFISC   IN INCIDENCIA.CDGRUPOFISC%TYPE,
    P_CDGRPFISCSV   IN INCIDENCIA.CDGRUPOFISC%TYPE DEFAULT NULL,
    P_IDINCINTEREST IN INCIDENCIA.IDINCINTEREST%TYPE DEFAULT 'N',
    P_IDSISTEMA     IN VARCHAR2 DEFAULT NULL,

    P_CDFILIRECE    IN VARCHAR2 DEFAULT NULL,    --Exclusivo Unificacao
    P_CDFORNECED    IN VARCHAR2 DEFAULT NULL,    --Exclusivo Unificacao
    P_CDCLIENTE     IN VARCHAR2 DEFAULT NULL,    --Exclusivo Unificacao
    P_INIVALID      IN DATE     DEFAULT NULL,    --Exclusivo Unificacao
    P_CDFILIEMIT    IN VARCHAR2 DEFAULT NULL,    --Exclusivo Unificacao
    P_VRPRODUTO     IN NUMBER   DEFAULT NULL,    --Exclusivo Unificacao
    P_LANCPRODUTO   IN VARCHAR2 DEFAULT NULL,    --Exclusivo Unificacao
    P_IDORIGEMPROD IN VARCHAR2 DEFAULT 'N',
    P_SGESTADO     IN VARCHAR2 DEFAULT NULL,     --Utiliza Local da prestacao do servico
    P_CDMUNICIPIO  IN VARCHAR2 DEFAULT NULL)     --Utiliza Local da prestacao do servico
IS
    V_SEQUENCIA     VARCHAR2(02);
    V_EXISTE        INTEGER;
    V_COUNTMENS     INTEGER;
    V_DSMENSINCID   VARCHAR2(120);
    V_COUNT         INTEGER;

    V_NRINCITEMP    VARCHAR2(18);

    V_INIVALID      DATE;
    V_PAISORIG       VARCHAR2(04); -- Pais de origem (utiliza UF de Origem)
    V_ESTAORIG       VARCHAR2(02); -- Estado de origem (utiliza UF de Origem)
    V_MUNIORIG       VARCHAR2(04); -- Municipio de origem (utiliza municipio)
    V_PAISDEST       VARCHAR2(04);
    V_ESTADEST       VARCHAR2(02);
    V_MUNIDEST       VARCHAR2(04);
    V_IDENDCFOP      VARCHAR2(01);
    V_IDORIGDESTNF   VARCHAR2(01);
    V_ORIGDESTOP     VARCHAR2(01);
    V_CDCLIENTE      CLIENTE.CDCLIENTE%TYPE;
    V_CDORIGDESTNF VARCHAR2(21);
    V_IDENDEMIS      VARCHAR2(01);
    V_CDEMPRESA      VARCHAR2(02);
    V_CDINSCESTA     VARCHAR2(16);
    V_CDFILIRECE     VARCHAR2(04);
    V_CDFORNECED     FORNECEDOR.CDFORNECED%TYPE;
    V_CDFILIEMIT     VARCHAR2(04);
    V_IDTRATIMPO     VARCHAR2(01);
    V_VRITEM         FLOAT;
    V_IDNFPRESTSERV  VARCHAR2(01);
    V_IDUTLOCPRESTSERV VARCHAR2(01);    

    V_CDPAISFCT      VARCHAR2(04);
    V_SGESTADOFCT    VARCHAR2(02);
    V_CDMUNICIPIOFCT VARCHAR2(04);
    V_QTDREGFCT      FLOAT;

    V_IDPRESENCIAL   VARCHAR2(01);

    V_IDENTEMIS      VARCHAR2(01);

    /* Variaveis prestacao de servico */
    V_CDPAISSERV      VARCHAR2(04);
    V_SGESTADOSERV    VARCHAR2(02);
    V_CDMUNICIPIOSERV VARCHAR2(04);
    V_QTDREGSERV      NUMBER;
    V_IDUTILNFSE      VARCHAR2(01);

    V_IDOPPRESENDDIF  VARCHAR2(01);

    V_IDEMISFILNFSERV  VARCHAR2(01);
BEGIN
   V_SEQUENCIA := '01';

   /*grupo fiscal por servico*/
   IF (P_CDGRPFISCSV IS NOT NULL) THEN

      /* Operacao Interestadual */
      IF P_IDINCINTEREST = 'S' THEN

         FOR C_IMPOSTO IN (SELECT MAX(P.CDARVPROD) AS CDARVPROD, I.CDIMPOSTO
                           FROM INCIDENCIA I, PRODUTO P, IMPOSTO IP
                           WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                             AND (I.CDEMPRESA    = P_EMPRESA)
                             AND (I.CDTIPOOPER   = P_TIPOOPER)
                             AND (I.CDINSCESTA   = RTRIM(P_INSCESTA))
                             AND ((I.CDGRUPOFISC = P_CDGRPFISCSV) AND (IP.IDGRUPFISSRV = 'S'))
                             AND (I.CDPRODUTO    = P.CDPRODUTO(+))
                             AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1)
                             AND (I.CDIMPOSTO = IP.CDIMPOSTO)
                             AND I.IDINCINTEREST = 'S'
                           GROUP BY I.CDIMPOSTO) LOOP

            SELECT COUNT(*)
              INTO V_EXISTE
              FROM INCITEMP
             WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
               AND CDIMPOTEMP = C_IMPOSTO.CDIMPOSTO;

            IF V_EXISTE > 0 THEN
               DELETE FROM INCITEMP
                WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
                  AND CDIMPOTEMP = C_IMPOSTO.CDIMPOSTO;
            END IF;

            IF NOT (C_IMPOSTO.CDARVPROD IS NULL) THEN
               FOR C_INCIDENCIA IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                           CDMENSAGEM, CDDETALHEPROD, CDENQLEGALIPI, I.CDCEST
                                      FROM INCIDENCIA I, MENSINCID M, PRODUTO P
                                     WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                       AND (I.CDEMPRESA    = P_EMPRESA)
                                       AND (I.CDTIPOOPER   = P_TIPOOPER)
                                       AND (I.CDGRUPOFISC  = P_CDGRPFISCSV)
                                       AND (I.CDIMPOSTO    = C_IMPOSTO.CDIMPOSTO)
                                       AND (I.CDINSCESTA   = RTRIM(P_INSCESTA))
                                       AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                       AND (I.CDPRODUTO    = P.CDPRODUTO(+))
                                       AND I.IDINCINTEREST = 'S'
                                       AND (P.CDARVPROD    = C_IMPOSTO.CDARVPROD)) LOOP

                  SELECT COUNT(*)
                    INTO V_COUNTMENS
                    FROM MENSAGEM
                   WHERE CDMENSAGEM = C_INCIDENCIA.CDMENSAGEM
                     AND IDSITUACAOMENS = 'I';

                  IF V_COUNTMENS > 0 THEN
                     V_DSMENSINCID := NULL;
                  ELSE
                     V_DSMENSINCID := C_INCIDENCIA.DSMENSINCID;
                  END IF;

                  INSERT INTO INCITEMP
                     (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                      CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                      CDENQLEGALIPI, CDCEST)
                  VALUES
                     (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDENCIA.CDIMPOSTO, C_INCIDENCIA.IDTRATIMPOIN,
                      C_INCIDENCIA.CDFISCAL, V_DSMENSINCID, P_INSCESTA, C_INCIDENCIA.CDDETALHEPROD,
                      C_INCIDENCIA.CDENQLEGALIPI, C_INCIDENCIA.CDCEST);

                  V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);

               END LOOP;
            ELSIF (C_IMPOSTO.CDARVPROD IS NULL) THEN
               FOR C_INCIDNULL IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                          CDMENSAGEM, CDDETALHEPROD, CDENQLEGALIPI, CDCEST
                                     FROM INCIDENCIA I, MENSINCID M
                                    WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                      AND (I.CDEMPRESA    = P_EMPRESA)
                                      AND (I.CDTIPOOPER   = P_TIPOOPER)
                                      AND (I.CDGRUPOFISC  = P_CDGRPFISCSV)
                                      AND (I.CDIMPOSTO    = C_IMPOSTO.CDIMPOSTO)
                                      AND (I.CDINSCESTA   = RTRIM(P_INSCESTA))
                                      AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                      AND I.IDINCINTEREST = 'S'
                                      AND (CDPRODUTO IS NULL)) LOOP

                  SELECT COUNT(*)
                    INTO V_COUNTMENS
                    FROM MENSAGEM
                   WHERE CDMENSAGEM = C_INCIDNULL.CDMENSAGEM
                     AND IDSITUACAOMENS = 'I';

                  IF V_COUNTMENS > 0 THEN
                     V_DSMENSINCID := NULL;
                  ELSE
                     V_DSMENSINCID := C_INCIDNULL.DSMENSINCID;
                  END IF;

                  INSERT INTO INCITEMP
                     (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                      CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                      CDENQLEGALIPI, CDCEST)
                  VALUES
                     (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDNULL.CDIMPOSTO, C_INCIDNULL.IDTRATIMPOIN,
                      C_INCIDNULL.CDFISCAL, V_DSMENSINCID, P_INSCESTA, C_INCIDNULL.CDDETALHEPROD,
                      C_INCIDNULL.CDENQLEGALIPI, C_INCIDNULL.CDCEST);

                  V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
               END LOOP;
            END IF;
         END LOOP;
      END IF;

      /* Verifica para operacoes nao interestaduais */
      FOR C_IMPOSTO IN (SELECT MAX(P.CDARVPROD) AS CDARVPROD, I.CDIMPOSTO
                        FROM INCIDENCIA I, PRODUTO P, IMPOSTO IP
                        WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                          AND (I.CDEMPRESA    = P_EMPRESA)
                          AND (I.CDTIPOOPER   = P_TIPOOPER)
                          AND (I.CDINSCESTA   = RTRIM(P_INSCESTA))
                          AND ((I.CDGRUPOFISC = P_CDGRPFISCSV) AND (IP.IDGRUPFISSRV = 'S'))
                          AND (I.CDPRODUTO    = P.CDPRODUTO(+))
                          AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1)
                          AND (I.CDIMPOSTO = IP.CDIMPOSTO)
                          AND I.IDINCINTEREST = 'N'
                          AND NOT EXISTS (SELECT NRINCITEMP
                                            FROM INCITEMP
                                           WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
                                             AND CDIMPOTEMP = I.CDIMPOSTO)
                        GROUP BY I.CDIMPOSTO) LOOP

         SELECT COUNT(*)
           INTO V_EXISTE
           FROM INCITEMP
          WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
            AND CDIMPOTEMP = C_IMPOSTO.CDIMPOSTO;

         IF V_EXISTE > 0 THEN
            DELETE FROM INCITEMP
             WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
               AND CDIMPOTEMP = C_IMPOSTO.CDIMPOSTO;
         END IF;

         IF NOT (C_IMPOSTO.CDARVPROD IS NULL) THEN
            FOR C_INCIDENCIA IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                        CDMENSAGEM, CDDETALHEPROD, CDENQLEGALIPI, I.CDCEST
                                   FROM INCIDENCIA I, MENSINCID M, PRODUTO P
                                  WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                    AND (I.CDEMPRESA    = P_EMPRESA)
                                    AND (I.CDTIPOOPER   = P_TIPOOPER)
                                    AND (I.CDGRUPOFISC  = P_CDGRPFISCSV)
                                    AND (I.CDIMPOSTO    = C_IMPOSTO.CDIMPOSTO)
                                    AND (I.CDINSCESTA   = RTRIM(P_INSCESTA))
                                    AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                    AND (I.CDPRODUTO    = P.CDPRODUTO(+))
                                    AND I.IDINCINTEREST = 'N'
                                    AND (P.CDARVPROD    = C_IMPOSTO.CDARVPROD)) LOOP

               SELECT COUNT(*)
                 INTO V_COUNTMENS
                 FROM MENSAGEM
                WHERE CDMENSAGEM = C_INCIDENCIA.CDMENSAGEM
                  AND IDSITUACAOMENS = 'I';

               IF V_COUNTMENS > 0 THEN
                  V_DSMENSINCID := NULL;
               ELSE
                  V_DSMENSINCID := C_INCIDENCIA.DSMENSINCID;
               END IF;

               INSERT INTO INCITEMP
                  (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                   CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                   CDENQLEGALIPI, CDCEST)
               VALUES
                  (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDENCIA.CDIMPOSTO, C_INCIDENCIA.IDTRATIMPOIN,
                   C_INCIDENCIA.CDFISCAL, V_DSMENSINCID, P_INSCESTA, C_INCIDENCIA.CDDETALHEPROD,
                   C_INCIDENCIA.CDENQLEGALIPI, C_INCIDENCIA.CDCEST);

               V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);

            END LOOP;
         ELSIF (C_IMPOSTO.CDARVPROD IS NULL) THEN
            FOR C_INCIDNULL IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                       CDMENSAGEM, CDDETALHEPROD, CDENQLEGALIPI, CDCEST
                                  FROM INCIDENCIA I, MENSINCID M
                                 WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                   AND (I.CDEMPRESA    = P_EMPRESA)
                                   AND (I.CDTIPOOPER   = P_TIPOOPER)
                                   AND (I.CDGRUPOFISC  = P_CDGRPFISCSV)
                                   AND (I.CDIMPOSTO    = C_IMPOSTO.CDIMPOSTO)
                                   AND (I.CDINSCESTA   = RTRIM(P_INSCESTA))
                                   AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                   AND I.IDINCINTEREST = 'N'
                                   AND (CDPRODUTO IS NULL)) LOOP

               SELECT COUNT(*)
                 INTO V_COUNTMENS
                 FROM MENSAGEM
                WHERE CDMENSAGEM = C_INCIDNULL.CDMENSAGEM
                  AND IDSITUACAOMENS = 'I';

               IF V_COUNTMENS > 0 THEN
                  V_DSMENSINCID := NULL;
               ELSE
                  V_DSMENSINCID := C_INCIDNULL.DSMENSINCID;
               END IF;

               INSERT INTO INCITEMP
                  (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                   CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                   CDENQLEGALIPI, CDCEST)
               VALUES
                  (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDNULL.CDIMPOSTO, C_INCIDNULL.IDTRATIMPOIN,
                   C_INCIDNULL.CDFISCAL, V_DSMENSINCID, P_INSCESTA, C_INCIDNULL.CDDETALHEPROD,
                   C_INCIDNULL.CDENQLEGALIPI, C_INCIDNULL.CDCEST);

               V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
            END LOOP;
         END IF;
      END LOOP;
   END IF;

   /* Se possui grupo fiscal */
   IF (P_CDGRUPOFISC IS NOT NULL)  THEN

      /* Operacao Interestadual */
      IF P_IDINCINTEREST = 'S' THEN

         FOR C_IMPOSTO IN (SELECT MAX(P.CDARVPROD) AS CDARVPROD, I.CDIMPOSTO
                           FROM INCIDENCIA I, PRODUTO P, IMPOSTO IP
                           WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                             AND (I.CDEMPRESA    = P_EMPRESA)
                             AND (I.CDTIPOOPER   = P_TIPOOPER)
                             AND (I.CDINSCESTA   = RTRIM(P_INSCESTA))
                             AND ((I.CDGRUPOFISC  = P_CDGRUPOFISC) AND (IP.IDGRUPFISSRV <> 'S'))
                             AND (I.CDPRODUTO    = P.CDPRODUTO(+))
                             AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1)
                             AND (I.CDIMPOSTO = IP.CDIMPOSTO)
                             AND I.IDINCINTEREST = 'S'
                           GROUP BY I.CDIMPOSTO) LOOP

            SELECT COUNT(*)
              INTO V_EXISTE
              FROM INCITEMP
             WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
               AND CDIMPOTEMP = C_IMPOSTO.CDIMPOSTO;

            IF V_EXISTE > 0 THEN
               DELETE FROM INCITEMP
                WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
                  AND CDIMPOTEMP = C_IMPOSTO.CDIMPOSTO;
            END IF;

            IF NOT (C_IMPOSTO.CDARVPROD IS NULL) THEN
               FOR C_INCIDENCIA IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                           CDMENSAGEM, CDDETALHEPROD, CDENQLEGALIPI, I.CDCEST
                                      FROM INCIDENCIA I, MENSINCID M, PRODUTO P
                                     WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                       AND (I.CDEMPRESA    = P_EMPRESA)
                                       AND (I.CDTIPOOPER   = P_TIPOOPER)
                                       AND (I.CDGRUPOFISC  = P_CDGRUPOFISC)
                                       AND (I.CDIMPOSTO    = C_IMPOSTO.CDIMPOSTO)
                                       AND (I.CDINSCESTA   = RTRIM(P_INSCESTA))
                                       AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                       AND (I.CDPRODUTO    = P.CDPRODUTO(+))
                                       AND I.IDINCINTEREST = 'S'
                                       AND (P.CDARVPROD    = C_IMPOSTO.CDARVPROD)) LOOP

                  SELECT COUNT(*)
                    INTO V_COUNTMENS
                    FROM MENSAGEM
                   WHERE CDMENSAGEM = C_INCIDENCIA.CDMENSAGEM
                     AND IDSITUACAOMENS = 'I';

                  IF V_COUNTMENS > 0 THEN
                     V_DSMENSINCID := NULL;
                  ELSE
                     V_DSMENSINCID := C_INCIDENCIA.DSMENSINCID;
                  END IF;

                  INSERT INTO INCITEMP
                     (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                      CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                      CDENQLEGALIPI, CDCEST)
                  VALUES
                     (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDENCIA.CDIMPOSTO, C_INCIDENCIA.IDTRATIMPOIN,
                      C_INCIDENCIA.CDFISCAL, V_DSMENSINCID, P_INSCESTA, C_INCIDENCIA.CDDETALHEPROD,
                      C_INCIDENCIA.CDENQLEGALIPI, C_INCIDENCIA.CDCEST);

                  V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);

               END LOOP;
            ELSIF (C_IMPOSTO.CDARVPROD IS NULL) THEN
               FOR C_INCIDNULL IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                          CDMENSAGEM, CDDETALHEPROD, CDENQLEGALIPI, CDCEST
                                     FROM INCIDENCIA I, MENSINCID M
                                    WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                      AND (I.CDEMPRESA    = P_EMPRESA)
                                      AND (I.CDTIPOOPER   = P_TIPOOPER)
                                      AND (I.CDGRUPOFISC  = P_CDGRUPOFISC)
                                      AND (I.CDIMPOSTO    = C_IMPOSTO.CDIMPOSTO)
                                      AND (I.CDINSCESTA   = RTRIM(P_INSCESTA))
                                      AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                      AND I.IDINCINTEREST = 'S'
                                      AND (CDPRODUTO IS NULL)) LOOP

                  SELECT COUNT(*)
                    INTO V_COUNTMENS
                    FROM MENSAGEM
                   WHERE CDMENSAGEM = C_INCIDNULL.CDMENSAGEM
                     AND IDSITUACAOMENS = 'I';

                  IF V_COUNTMENS > 0 THEN
                     V_DSMENSINCID := NULL;
                  ELSE
                     V_DSMENSINCID := C_INCIDNULL.DSMENSINCID;
                  END IF;

                  INSERT INTO INCITEMP
                     (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                      CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                      CDENQLEGALIPI, CDCEST)
                  VALUES
                     (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDNULL.CDIMPOSTO, C_INCIDNULL.IDTRATIMPOIN,
                      C_INCIDNULL.CDFISCAL, V_DSMENSINCID, P_INSCESTA, C_INCIDNULL.CDDETALHEPROD,
                      C_INCIDNULL.CDENQLEGALIPI, C_INCIDNULL.CDCEST);

                  V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
               END LOOP;
            END IF;
         END LOOP;
      END IF;

      FOR C_IMPOSTO IN (SELECT MAX(P.CDARVPROD) AS CDARVPROD, I.CDIMPOSTO
                        FROM INCIDENCIA I, PRODUTO P, IMPOSTO IP
                        WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                          AND (I.CDEMPRESA    = P_EMPRESA)
                          AND (I.CDTIPOOPER   = P_TIPOOPER)
                          AND (I.CDINSCESTA   = RTRIM(P_INSCESTA))
                          AND ((I.CDGRUPOFISC  = P_CDGRUPOFISC) AND (IP.IDGRUPFISSRV <> 'S'))
                          AND (I.CDPRODUTO    = P.CDPRODUTO(+))
                          AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1)
                          AND (I.CDIMPOSTO = IP.CDIMPOSTO)
                          AND I.IDINCINTEREST = 'N'
                          AND NOT EXISTS (SELECT NRINCITEMP
                                            FROM INCITEMP
                                           WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
                                             AND CDIMPOTEMP = I.CDIMPOSTO)
                        GROUP BY I.CDIMPOSTO) LOOP

         SELECT COUNT(*)
           INTO V_EXISTE
           FROM INCITEMP
          WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
            AND CDIMPOTEMP = C_IMPOSTO.CDIMPOSTO;

         IF V_EXISTE > 0 THEN
            DELETE FROM INCITEMP
             WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
               AND CDIMPOTEMP = C_IMPOSTO.CDIMPOSTO;
         END IF;

         IF NOT (C_IMPOSTO.CDARVPROD IS NULL) THEN
            FOR C_INCIDENCIA IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                        NVL(I.IDINCINTEREST,'N') IDINCINTEREST, CDMENSAGEM, CDDETALHEPROD, 
                                        CDENQLEGALIPI, I.CDCEST
                                   FROM INCIDENCIA I, MENSINCID M, PRODUTO P
                                  WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                    AND (I.CDEMPRESA    = P_EMPRESA)
                                    AND (I.CDTIPOOPER   = P_TIPOOPER)
                                    AND (I.CDGRUPOFISC  = P_CDGRUPOFISC)
                                    AND (I.CDIMPOSTO    = C_IMPOSTO.CDIMPOSTO)
                                    AND (I.CDINSCESTA   = RTRIM(P_INSCESTA))
                                    AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                    AND (I.CDPRODUTO    = P.CDPRODUTO(+))
                                    AND I.IDINCINTEREST = 'N'
                                    AND (P.CDARVPROD    = C_IMPOSTO.CDARVPROD)) LOOP

               SELECT COUNT(*)
                 INTO V_COUNTMENS
                 FROM MENSAGEM
                WHERE CDMENSAGEM = C_INCIDENCIA.CDMENSAGEM
                  AND IDSITUACAOMENS = 'I';

               IF V_COUNTMENS > 0 THEN
                  V_DSMENSINCID := NULL;
               ELSE
                  V_DSMENSINCID := C_INCIDENCIA.DSMENSINCID;
               END IF;

               INSERT INTO INCITEMP
                  (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                   CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                   CDENQLEGALIPI, CDCEST)
               VALUES
                  (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDENCIA.CDIMPOSTO, C_INCIDENCIA.IDTRATIMPOIN,
                   C_INCIDENCIA.CDFISCAL, V_DSMENSINCID, P_INSCESTA, C_INCIDENCIA.CDDETALHEPROD,
                   C_INCIDENCIA.CDENQLEGALIPI, C_INCIDENCIA.CDCEST);

               V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);

            END LOOP;
         ELSIF (C_IMPOSTO.CDARVPROD IS NULL) THEN
            FOR C_INCIDNULL IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                       NVL(I.IDINCINTEREST,'N') IDINCINTEREST, CDMENSAGEM, CDDETALHEPROD, 
                                       CDENQLEGALIPI, CDCEST
                                  FROM INCIDENCIA I, MENSINCID M
                                 WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                   AND (I.CDEMPRESA    = P_EMPRESA)
                                   AND (I.CDTIPOOPER   = P_TIPOOPER)
                                   AND (I.CDGRUPOFISC  = P_CDGRUPOFISC)
                                   AND (I.CDIMPOSTO    = C_IMPOSTO.CDIMPOSTO)
                                   AND (I.CDINSCESTA   = RTRIM(P_INSCESTA))
                                   AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                   AND I.IDINCINTEREST = 'N'
                                   AND (CDPRODUTO IS NULL)) LOOP

               SELECT COUNT(*)
                 INTO V_COUNTMENS
                 FROM MENSAGEM
                WHERE CDMENSAGEM = C_INCIDNULL.CDMENSAGEM
                  AND IDSITUACAOMENS = 'I';

               IF V_COUNTMENS > 0 THEN
                  V_DSMENSINCID := NULL;
               ELSE
                  V_DSMENSINCID := C_INCIDNULL.DSMENSINCID;
               END IF;

               INSERT INTO INCITEMP
                  (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                   CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                   CDENQLEGALIPI, CDCEST)
               VALUES
                  (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDNULL.CDIMPOSTO, C_INCIDNULL.IDTRATIMPOIN,
                   C_INCIDNULL.CDFISCAL, V_DSMENSINCID, P_INSCESTA, C_INCIDNULL.CDDETALHEPROD,
                   C_INCIDNULL.CDENQLEGALIPI, C_INCIDNULL.CDCEST);

               V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
            END LOOP;
         END IF;
      END LOOP;
   ELSE
      /* Operacao Interestadual */
      IF P_IDINCINTEREST = 'S' THEN

         FOR C_IMPOSTO IN (SELECT MAX(P.CDARVPROD) AS CDARVPROD, CDIMPOSTO
                           FROM INCIDENCIA I, PRODUTO P
                           WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                             AND (I.CDEMPRESA    = P_EMPRESA)
                             AND (I.CDTIPOOPER   = P_TIPOOPER)
                             AND (I.CDINSCESTA   = RTRIM(P_INSCESTA))
                             AND (I.CDGRUPOFISC IS NULL)
                             AND (I.CDPRODUTO    = P.CDPRODUTO(+))
                             AND I.IDINCINTEREST = 'S'
                             AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1)
                           GROUP BY CDIMPOSTO) LOOP

            SELECT COUNT(*)
              INTO V_EXISTE
              FROM INCITEMP
             WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
               AND CDIMPOTEMP = C_IMPOSTO.CDIMPOSTO;

            IF V_EXISTE > 0 THEN
               DELETE FROM INCITEMP
                WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
                  AND CDIMPOTEMP = C_IMPOSTO.CDIMPOSTO;
            END IF;

            IF NOT (C_IMPOSTO.CDARVPROD IS NULL) THEN
               FOR C_INCIDENCIA IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                           NVL(I.IDINCINTEREST, 'N') IDINCINTEREST, CDMENSAGEM, CDDETALHEPROD, 
                                           CDENQLEGALIPI, I.CDCEST
                                      FROM INCIDENCIA I, MENSINCID M, PRODUTO P
                                     WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                       AND (I.CDEMPRESA    = P_EMPRESA)
                                       AND (I.CDTIPOOPER   = P_TIPOOPER)
                                       AND (I.CDIMPOSTO    = C_IMPOSTO.CDIMPOSTO)
                                       AND (I.CDINSCESTA   = RTRIM(P_INSCESTA))
                                       AND (I.CDGRUPOFISC IS NULL)
                                       AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                       AND (I.CDPRODUTO    = P.CDPRODUTO(+))
                                       AND I.IDINCINTEREST = 'S'
                                       AND (P.CDARVPROD    = C_IMPOSTO.CDARVPROD)) LOOP

                  SELECT COUNT(*)
                    INTO V_COUNTMENS
                    FROM MENSAGEM
                   WHERE CDMENSAGEM = C_INCIDENCIA.CDMENSAGEM
                     AND IDSITUACAOMENS = 'I';

                  IF V_COUNTMENS > 0 THEN
                     V_DSMENSINCID := NULL;
                  ELSE
                     V_DSMENSINCID := C_INCIDENCIA.DSMENSINCID;
                  END IF;

                  INSERT INTO INCITEMP
                     (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                      CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                      CDENQLEGALIPI, CDCEST)
                  VALUES
                     (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDENCIA.CDIMPOSTO, C_INCIDENCIA.IDTRATIMPOIN,
                      C_INCIDENCIA.CDFISCAL, V_DSMENSINCID, P_INSCESTA, C_INCIDENCIA.CDDETALHEPROD,
                      C_INCIDENCIA.CDENQLEGALIPI, C_INCIDENCIA.CDCEST);

                  V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
               END LOOP;

            ELSIF (C_IMPOSTO.CDARVPROD IS NULL) THEN
               FOR C_INCIDNULL IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                          NVL(I.IDINCINTEREST, 'N') IDINCINTEREST, CDMENSAGEM, CDDETALHEPROD, 
                                          CDENQLEGALIPI, CDCEST
                                     FROM INCIDENCIA I, MENSINCID M
                                    WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                      AND (I.CDEMPRESA    = P_EMPRESA)
                                      AND (I.CDTIPOOPER   = P_TIPOOPER)
                                      AND (I.CDIMPOSTO    = C_IMPOSTO.CDIMPOSTO)
                                      AND (I.CDINSCESTA   = RTRIM(P_INSCESTA))
                                      AND (I.CDGRUPOFISC  IS NULL)
                                      AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                      AND I.IDINCINTEREST = 'S'
                                      AND (CDPRODUTO IS NULL)) LOOP

                  SELECT COUNT(*)
                    INTO V_COUNTMENS
                    FROM MENSAGEM
                   WHERE CDMENSAGEM = C_INCIDNULL.CDMENSAGEM
                     AND IDSITUACAOMENS = 'I';

                  IF V_COUNTMENS > 0 THEN
                     V_DSMENSINCID := NULL;
                  ELSE
                     V_DSMENSINCID := C_INCIDNULL.DSMENSINCID;
                  END IF;

                  INSERT INTO INCITEMP
                     (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                      CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                      CDENQLEGALIPI, CDCEST)
                  VALUES
                     (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDNULL.CDIMPOSTO, C_INCIDNULL.IDTRATIMPOIN,
                      C_INCIDNULL.CDFISCAL, V_DSMENSINCID, P_INSCESTA, C_INCIDNULL.CDDETALHEPROD,
                      C_INCIDNULL.CDENQLEGALIPI, C_INCIDNULL.CDCEST);

                  V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
               END LOOP;
            END IF;
         END LOOP;
      END IF;

      FOR C_IMPOSTO IN (SELECT MAX(P.CDARVPROD) AS CDARVPROD, CDIMPOSTO
                        FROM INCIDENCIA I, PRODUTO P
                        WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                          AND (I.CDEMPRESA    = P_EMPRESA)
                          AND (I.CDTIPOOPER   = P_TIPOOPER)
                          AND (I.CDINSCESTA   = RTRIM(P_INSCESTA))
                          AND (I.CDGRUPOFISC IS NULL)
                          AND (I.CDPRODUTO    = P.CDPRODUTO(+))
                          AND I.IDINCINTEREST = 'N'
                          AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1)
                          AND NOT EXISTS (SELECT NRINCITEMP
                                            FROM INCITEMP
                                           WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
                                             AND CDIMPOTEMP = I.CDIMPOSTO)
                        GROUP BY CDIMPOSTO) LOOP

         SELECT COUNT(*)
           INTO V_EXISTE
           FROM INCITEMP
          WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
            AND CDIMPOTEMP = C_IMPOSTO.CDIMPOSTO;

         IF V_EXISTE > 0 THEN
            DELETE FROM INCITEMP
             WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
               AND CDIMPOTEMP = C_IMPOSTO.CDIMPOSTO;
         END IF;

         IF NOT (C_IMPOSTO.CDARVPROD IS NULL) THEN
            FOR C_INCIDENCIA IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                        NVL(I.IDINCINTEREST, 'N') IDINCINTEREST, CDMENSAGEM, CDDETALHEPROD, 
                                        CDENQLEGALIPI, I.CDCEST
                                   FROM INCIDENCIA I, MENSINCID M, PRODUTO P
                                  WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                    AND (I.CDEMPRESA    = P_EMPRESA)
                                    AND (I.CDTIPOOPER   = P_TIPOOPER)
                                    AND (I.CDIMPOSTO    = C_IMPOSTO.CDIMPOSTO)
                                    AND (I.CDINSCESTA   = RTRIM(P_INSCESTA))
                                    AND (I.CDGRUPOFISC IS NULL)
                                    AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                    AND (I.CDPRODUTO    = P.CDPRODUTO(+))
                                    AND I.IDINCINTEREST = 'N'
                                    AND (P.CDARVPROD    = C_IMPOSTO.CDARVPROD)) LOOP

               SELECT COUNT(*)
                 INTO V_COUNTMENS
                 FROM MENSAGEM
                WHERE CDMENSAGEM = C_INCIDENCIA.CDMENSAGEM
                  AND IDSITUACAOMENS = 'I';

               IF V_COUNTMENS > 0 THEN
                  V_DSMENSINCID := NULL;
               ELSE
                  V_DSMENSINCID := C_INCIDENCIA.DSMENSINCID;
               END IF;

               INSERT INTO INCITEMP
                  (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                   CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                   CDENQLEGALIPI, CDCEST)
               VALUES
                  (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDENCIA.CDIMPOSTO, C_INCIDENCIA.IDTRATIMPOIN,
                   C_INCIDENCIA.CDFISCAL, V_DSMENSINCID, P_INSCESTA, C_INCIDENCIA.CDDETALHEPROD,
                   C_INCIDENCIA.CDENQLEGALIPI, C_INCIDENCIA.CDCEST);

               V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
            END LOOP;

         ELSIF (C_IMPOSTO.CDARVPROD IS NULL) THEN
            FOR C_INCIDNULL IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                       NVL(I.IDINCINTEREST, 'N') IDINCINTEREST, CDMENSAGEM, CDDETALHEPROD, 
                                       CDENQLEGALIPI, CDCEST
                                  FROM INCIDENCIA I, MENSINCID M
                                 WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                   AND (I.CDEMPRESA    = P_EMPRESA)
                                   AND (I.CDTIPOOPER   = P_TIPOOPER)
                                   AND (I.CDIMPOSTO    = C_IMPOSTO.CDIMPOSTO)
                                   AND (I.CDINSCESTA   = RTRIM(P_INSCESTA))
                                   AND (I.CDGRUPOFISC  IS NULL)
                                   AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                   AND I.IDINCINTEREST = 'N'
                                   AND (CDPRODUTO IS NULL)) LOOP

               SELECT COUNT(*)
                 INTO V_COUNTMENS
                 FROM MENSAGEM
                WHERE CDMENSAGEM = C_INCIDNULL.CDMENSAGEM
                  AND IDSITUACAOMENS = 'I';

               IF V_COUNTMENS > 0 THEN
                  V_DSMENSINCID := NULL;
               ELSE
                  V_DSMENSINCID := C_INCIDNULL.DSMENSINCID;
               END IF;

               INSERT INTO INCITEMP
                  (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                   CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                   CDENQLEGALIPI, CDCEST)
               VALUES
                  (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDNULL.CDIMPOSTO, C_INCIDNULL.IDTRATIMPOIN,
                   C_INCIDNULL.CDFISCAL, V_DSMENSINCID, P_INSCESTA, C_INCIDNULL.CDDETALHEPROD,
                   C_INCIDNULL.CDENQLEGALIPI, C_INCIDNULL.CDCEST);

               V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
            END LOOP;
         END IF;
      END LOOP;
   END IF;

   /* Verificacao das incidencias referente as filiais parametrizadas na tela Parametros de Grupo Fiscal (Imposto/Filial) */
   FOR C_IMPFILI IN (SELECT G.CDIMPOSTO, G.CDGRUPOFISC
                       FROM GRUPOFISCIMPFIL G,
                            (SELECT DECODE(P_ENTRSAIDIN, 'E', CDFILIRECE, CDFILIEMIT) AS CDFILIAL
                               FROM NOTAFISCAL
                              WHERE CDFILIAL = P_FILIAL
                                AND NRLANCTONF = P_LANCTONF
                                AND P_IDSISTEMA IS NULL
                                AND P_CDFILIRECE IS NULL
                             UNION ALL
                             SELECT CDFILIAL
                               FROM PEDIDO
                              WHERE CDFILIAL = SUBSTR(P_FILIAL,1,4)
                                AND NRPEDIDO = SUBSTR(P_FILIAL,5,10)
                                AND P_IDSISTEMA = 'PED'
                             UNION ALL
                             SELECT CDFILIAL
                               FROM FILIAL
                              WHERE CDFILIAL = P_CDFILIRECE) F
                      WHERE G.CDFILIAL = F.CDFILIAL
                        AND NOT G.CDGRUPOFISC IS NULL)
   LOOP

      V_NRINCITEMP := '';

      SELECT MAX(NRINCITEMP)
        INTO V_NRINCITEMP
        FROM INCITEMP
       WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
         AND CDIMPOTEMP = C_IMPFILI.CDIMPOSTO;

      IF NOT V_NRINCITEMP IS NULL THEN
         DELETE FROM INCITEMP
          WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
            AND CDIMPOTEMP = C_IMPFILI.CDIMPOSTO;
      END IF;

      /* Operacao Interestadual */
      IF P_IDINCINTEREST = 'S' THEN

         FOR C_IMPOSTO IN (SELECT MAX(P.CDARVPROD) AS CDARVPROD
                             FROM INCIDENCIA I, PRODUTO P, IMPOSTO IP
                            WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                              AND (I.CDEMPRESA = P_EMPRESA)
                              AND (I.CDTIPOOPER = P_TIPOOPER)
                              AND (I.CDINSCESTA = RTRIM(P_INSCESTA))
                              AND ((I.CDGRUPOFISC = C_IMPFILI.CDGRUPOFISC) AND (IP.IDGRUPFISSRV <> 'S'))
                              AND (I.CDIMPOSTO = C_IMPFILI.CDIMPOSTO)
                              AND (I.CDPRODUTO = P.CDPRODUTO(+))
                              AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1)
                              AND I.IDINCINTEREST = 'S'
                              AND (I.CDIMPOSTO = IP.CDIMPOSTO))
         LOOP
            IF NOT (C_IMPOSTO.CDARVPROD IS NULL) THEN
               FOR C_INCIDENCIA IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                           CDMENSAGEM, CDDETALHEPROD, CDENQLEGALIPI, I.CDCEST
                                      FROM INCIDENCIA I, MENSINCID M, PRODUTO P
                                     WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                       AND (I.CDEMPRESA = P_EMPRESA)
                                       AND (I.CDTIPOOPER = P_TIPOOPER)
                                       AND (I.CDGRUPOFISC = C_IMPFILI.CDGRUPOFISC)
                                       AND (I.CDIMPOSTO = C_IMPFILI.CDIMPOSTO)
                                       AND (I.CDINSCESTA = RTRIM(P_INSCESTA))
                                       AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                       AND (I.CDPRODUTO = P.CDPRODUTO(+))
                                       AND I.IDINCINTEREST = 'S'
                                       AND (P.CDARVPROD = C_IMPOSTO.CDARVPROD))
               LOOP
                  SELECT COUNT(*)
                    INTO V_COUNTMENS
                    FROM MENSAGEM
                   WHERE CDMENSAGEM = C_INCIDENCIA.CDMENSAGEM
                     AND IDSITUACAOMENS = 'I';

                  IF V_COUNTMENS > 0 THEN
                     V_DSMENSINCID := NULL;
                  ELSE
                     V_DSMENSINCID := C_INCIDENCIA.DSMENSINCID;
                  END IF;

                  INSERT INTO INCITEMP
                     (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                      CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                      CDENQLEGALIPI, CDCEST)
                  VALUES
                     (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDENCIA.CDIMPOSTO, C_INCIDENCIA.IDTRATIMPOIN,
                      C_INCIDENCIA.CDFISCAL, V_DSMENSINCID, P_INSCESTA, C_INCIDENCIA.CDDETALHEPROD,
                      C_INCIDENCIA.CDENQLEGALIPI, C_INCIDENCIA.CDCEST);

                  V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
               END LOOP;
            ELSIF (C_IMPOSTO.CDARVPROD IS NULL) THEN
               FOR C_INCIDNULL IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                          CDMENSAGEM, CDDETALHEPROD, CDENQLEGALIPI, CDCEST
                                     FROM INCIDENCIA I, MENSINCID M
                                    WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                      AND (I.CDEMPRESA = P_EMPRESA)
                                      AND (I.CDTIPOOPER = P_TIPOOPER)
                                      AND (I.CDGRUPOFISC = C_IMPFILI.CDGRUPOFISC)
                                      AND (I.CDIMPOSTO = C_IMPFILI.CDIMPOSTO)
                                      AND (I.CDINSCESTA = RTRIM(P_INSCESTA))
                                      AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                      AND I.IDINCINTEREST = 'S'
                                      AND (CDPRODUTO IS NULL))
               LOOP
                  SELECT COUNT(*)
                    INTO V_COUNTMENS
                    FROM MENSAGEM
                   WHERE CDMENSAGEM = C_INCIDNULL.CDMENSAGEM
                     AND IDSITUACAOMENS = 'I';

                  IF V_COUNTMENS > 0 THEN
                     V_DSMENSINCID := NULL;
                  ELSE
                     V_DSMENSINCID := C_INCIDNULL.DSMENSINCID;
                  END IF;

                  INSERT INTO INCITEMP
                     (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                      CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                      CDENQLEGALIPI, CDCEST)
                  VALUES
                     (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDNULL.CDIMPOSTO, C_INCIDNULL.IDTRATIMPOIN,
                      C_INCIDNULL.CDFISCAL, V_DSMENSINCID, P_INSCESTA, C_INCIDNULL.CDDETALHEPROD,
                      C_INCIDNULL.CDENQLEGALIPI, C_INCIDNULL.CDCEST);

                  V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
               END LOOP;
            END IF;
         END LOOP;
      END IF;

      FOR C_IMPOSTO IN (SELECT MAX(P.CDARVPROD) AS CDARVPROD
                          FROM INCIDENCIA I, PRODUTO P, IMPOSTO IP
                         WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                           AND (I.CDEMPRESA = P_EMPRESA)
                           AND (I.CDTIPOOPER = P_TIPOOPER)
                           AND (I.CDINSCESTA = RTRIM(P_INSCESTA))
                           AND ((I.CDGRUPOFISC = C_IMPFILI.CDGRUPOFISC) AND (IP.IDGRUPFISSRV <> 'S'))
                           AND (I.CDIMPOSTO = C_IMPFILI.CDIMPOSTO)
                           AND (I.CDPRODUTO = P.CDPRODUTO(+))
                           AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1)
                           AND I.IDINCINTEREST = 'N'
                           AND NOT EXISTS (SELECT NRINCITEMP
                                            FROM INCITEMP
                                           WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
                                             AND CDIMPOTEMP = I.CDIMPOSTO)
                           AND (I.CDIMPOSTO = IP.CDIMPOSTO))
      LOOP

         IF NOT (C_IMPOSTO.CDARVPROD IS NULL) THEN
            FOR C_INCIDENCIA IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                        CDMENSAGEM, CDDETALHEPROD, CDENQLEGALIPI, I.CDCEST
                                   FROM INCIDENCIA I, MENSINCID M, PRODUTO P
                                  WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                    AND (I.CDEMPRESA = P_EMPRESA)
                                    AND (I.CDTIPOOPER = P_TIPOOPER)
                                    AND (I.CDGRUPOFISC = C_IMPFILI.CDGRUPOFISC)
                                    AND (I.CDIMPOSTO = C_IMPFILI.CDIMPOSTO)
                                    AND (I.CDINSCESTA = RTRIM(P_INSCESTA))
                                    AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                    AND (I.CDPRODUTO = P.CDPRODUTO(+))
                                    AND I.IDINCINTEREST = 'N'
                                    AND (P.CDARVPROD = C_IMPOSTO.CDARVPROD))
            LOOP
               SELECT COUNT(*)
                 INTO V_COUNTMENS
                 FROM MENSAGEM
                WHERE CDMENSAGEM = C_INCIDENCIA.CDMENSAGEM
                  AND IDSITUACAOMENS = 'I';

               IF V_COUNTMENS > 0 THEN
                  V_DSMENSINCID := NULL;
               ELSE
                  V_DSMENSINCID := C_INCIDENCIA.DSMENSINCID;
               END IF;

               INSERT INTO INCITEMP
                  (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                   CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                   CDENQLEGALIPI, CDCEST)
               VALUES
                  (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDENCIA.CDIMPOSTO, C_INCIDENCIA.IDTRATIMPOIN,
                   C_INCIDENCIA.CDFISCAL, V_DSMENSINCID, P_INSCESTA, C_INCIDENCIA.CDDETALHEPROD,
                   C_INCIDENCIA.CDENQLEGALIPI, C_INCIDENCIA.CDCEST);

               V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
            END LOOP;
         ELSIF (C_IMPOSTO.CDARVPROD IS NULL) THEN
            FOR C_INCIDNULL IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                       CDMENSAGEM, CDDETALHEPROD, CDENQLEGALIPI, CDCEST
                                  FROM INCIDENCIA I, MENSINCID M
                                 WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                   AND (I.CDEMPRESA = P_EMPRESA)
                                   AND (I.CDTIPOOPER = P_TIPOOPER)
                                   AND (I.CDGRUPOFISC = C_IMPFILI.CDGRUPOFISC)
                                   AND (I.CDIMPOSTO = C_IMPFILI.CDIMPOSTO)
                                   AND (I.CDINSCESTA = RTRIM(P_INSCESTA))
                                   AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                   AND I.IDINCINTEREST = 'N'
                                   AND (CDPRODUTO IS NULL))
            LOOP
               SELECT COUNT(*)
                 INTO V_COUNTMENS
                 FROM MENSAGEM
                WHERE CDMENSAGEM = C_INCIDNULL.CDMENSAGEM
                  AND IDSITUACAOMENS = 'I';

               IF V_COUNTMENS > 0 THEN
                  V_DSMENSINCID := NULL;
               ELSE
                  V_DSMENSINCID := C_INCIDNULL.DSMENSINCID;
               END IF;

               INSERT INTO INCITEMP
                  (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                   CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                   CDENQLEGALIPI, CDCEST)
               VALUES
                  (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDNULL.CDIMPOSTO, C_INCIDNULL.IDTRATIMPOIN,
                   C_INCIDNULL.CDFISCAL, V_DSMENSINCID, P_INSCESTA, C_INCIDNULL.CDDETALHEPROD,
                   C_INCIDNULL.CDENQLEGALIPI, C_INCIDNULL.CDCEST);

               V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
            END LOOP;
         END IF;
      END LOOP;
   END LOOP;

   /* Verificacao das incidencias referente as filiais parametrizadas na tela Parametros de Grupo Fiscal (Imposto/Inscricao Estadual) */
   /* Por Fornecedor */
   FOR C_IMPINSCESTAFO IN (SELECT G.CDIMPOSTO, G.CDGRUPOFISC
                             FROM GRFISIMPIEFORN G,
                                  (SELECT CDFORNECED
                                     FROM NOTAFISCAL
                                    WHERE CDFILIAL = P_FILIAL
                                      AND NRLANCTONF = P_LANCTONF
                                      AND P_CDFORNECED IS NULL
                                   UNION ALL
                                   SELECT CDFORNECED
                                     FROM FORNECEDOR
                                    WHERE CDFORNECED = P_CDFORNECED) N
                            WHERE G.CDFORNECED = N.CDFORNECED
                              AND G.CDEMPRESA = P_EMPRESA
                          AND G.CDINSCESTA = P_INSCESTA
                          AND NOT G.CDGRUPOFISC IS NULL) LOOP

      V_NRINCITEMP := '';

      SELECT MAX(NRINCITEMP)
        INTO V_NRINCITEMP
        FROM INCITEMP
       WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
         AND CDIMPOTEMP = C_IMPINSCESTAFO.CDIMPOSTO;

      IF NOT V_NRINCITEMP IS NULL THEN
         DELETE FROM INCITEMP
          WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
            AND CDIMPOTEMP = C_IMPINSCESTAFO.CDIMPOSTO;
      END IF;

      /* Operacao Interestadual */
      IF P_IDINCINTEREST = 'S' THEN

         FOR C_IMPOSTO IN (SELECT MAX(P.CDARVPROD) AS CDARVPROD
                             FROM INCIDENCIA I, PRODUTO P, IMPOSTO IP
                            WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                              AND (I.CDEMPRESA = P_EMPRESA)
                              AND (I.CDTIPOOPER = P_TIPOOPER)
                              AND (I.CDINSCESTA = RTRIM(P_INSCESTA))
                              AND ((I.CDGRUPOFISC = C_IMPINSCESTAFO.CDGRUPOFISC) AND (IP.IDGRUPFISSRV <> 'S'))
                              AND (I.CDIMPOSTO = C_IMPINSCESTAFO.CDIMPOSTO)
                              AND (I.CDPRODUTO = P.CDPRODUTO(+))
                              AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1)
                              AND I.IDINCINTEREST = 'S'
                              AND (I.CDIMPOSTO = IP.CDIMPOSTO))
         LOOP
            IF NOT (C_IMPOSTO.CDARVPROD IS NULL) THEN
               FOR C_INCIDENCIA IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                           CDMENSAGEM, CDDETALHEPROD, CDENQLEGALIPI, I.CDCEST
                                      FROM INCIDENCIA I, MENSINCID M, PRODUTO P
                                     WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                       AND (I.CDEMPRESA = P_EMPRESA)
                                       AND (I.CDTIPOOPER = P_TIPOOPER)
                                       AND (I.CDGRUPOFISC = C_IMPINSCESTAFO.CDGRUPOFISC)
                                       AND (I.CDIMPOSTO = C_IMPINSCESTAFO.CDIMPOSTO)
                                       AND (I.CDINSCESTA = RTRIM(P_INSCESTA))
                                       AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                       AND (I.CDPRODUTO = P.CDPRODUTO(+))
                                       AND I.IDINCINTEREST = 'S'
                                       AND (P.CDARVPROD = C_IMPOSTO.CDARVPROD))
               LOOP
                  SELECT COUNT(*)
                    INTO V_COUNTMENS
                    FROM MENSAGEM
                   WHERE CDMENSAGEM = C_INCIDENCIA.CDMENSAGEM
                     AND IDSITUACAOMENS = 'I';

                  IF V_COUNTMENS > 0 THEN
                     V_DSMENSINCID := NULL;
                  ELSE
                     V_DSMENSINCID := C_INCIDENCIA.DSMENSINCID;
                  END IF;

                  INSERT INTO INCITEMP
                     (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                      CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                      CDENQLEGALIPI, CDCEST)
                  VALUES
                     (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDENCIA.CDIMPOSTO, C_INCIDENCIA.IDTRATIMPOIN,
                      C_INCIDENCIA.CDFISCAL, V_DSMENSINCID, P_INSCESTA, C_INCIDENCIA.CDDETALHEPROD,
                      C_INCIDENCIA.CDENQLEGALIPI, C_INCIDENCIA.CDCEST);

                  V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
               END LOOP;
            ELSIF (C_IMPOSTO.CDARVPROD IS NULL) THEN
               FOR C_INCIDNULL IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                          CDMENSAGEM, CDDETALHEPROD, CDENQLEGALIPI, CDCEST
                                     FROM INCIDENCIA I, MENSINCID M
                                    WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                      AND (I.CDEMPRESA = P_EMPRESA)
                                      AND (I.CDTIPOOPER = P_TIPOOPER)
                                      AND (I.CDGRUPOFISC = C_IMPINSCESTAFO.CDGRUPOFISC)
                                      AND (I.CDIMPOSTO = C_IMPINSCESTAFO.CDIMPOSTO)
                                      AND (I.CDINSCESTA = RTRIM(P_INSCESTA))
                                      AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                      AND I.IDINCINTEREST = 'S'
                                      AND (CDPRODUTO IS NULL))
               LOOP
                  SELECT COUNT(*)
                    INTO V_COUNTMENS
                    FROM MENSAGEM
                   WHERE CDMENSAGEM = C_INCIDNULL.CDMENSAGEM
                     AND IDSITUACAOMENS = 'I';

                  IF V_COUNTMENS > 0 THEN
                     V_DSMENSINCID := NULL;
                  ELSE
                     V_DSMENSINCID := C_INCIDNULL.DSMENSINCID;
                  END IF;

                  INSERT INTO INCITEMP
                     (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                      CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                      CDENQLEGALIPI, CDCEST)
                  VALUES
                     (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDNULL.CDIMPOSTO, C_INCIDNULL.IDTRATIMPOIN,
                      C_INCIDNULL.CDFISCAL, V_DSMENSINCID, P_INSCESTA, C_INCIDNULL.CDDETALHEPROD,
                      C_INCIDNULL.CDENQLEGALIPI, C_INCIDNULL.CDCEST);

                  V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
               END LOOP;
            END IF;
         END LOOP;
      END IF;

      FOR C_IMPOSTO IN (SELECT MAX(P.CDARVPROD) AS CDARVPROD
                          FROM INCIDENCIA I, PRODUTO P, IMPOSTO IP
                         WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                           AND (I.CDEMPRESA = P_EMPRESA)
                           AND (I.CDTIPOOPER = P_TIPOOPER)
                           AND (I.CDINSCESTA = RTRIM(P_INSCESTA))
                           AND ((I.CDGRUPOFISC = C_IMPINSCESTAFO.CDGRUPOFISC) AND (IP.IDGRUPFISSRV <> 'S'))
                           AND (I.CDIMPOSTO = C_IMPINSCESTAFO.CDIMPOSTO)
                           AND (I.CDPRODUTO = P.CDPRODUTO(+))
                           AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1)
                           AND I.IDINCINTEREST = 'N'
                           AND NOT EXISTS (SELECT NRINCITEMP
                                            FROM INCITEMP
                                           WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
                                             AND CDIMPOTEMP = I.CDIMPOSTO)
                           AND (I.CDIMPOSTO = IP.CDIMPOSTO))
      LOOP

         IF NOT (C_IMPOSTO.CDARVPROD IS NULL) THEN
            FOR C_INCIDENCIA IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                        CDMENSAGEM, CDDETALHEPROD, CDENQLEGALIPI, I.CDCEST
                                   FROM INCIDENCIA I, MENSINCID M, PRODUTO P
                                  WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                    AND (I.CDEMPRESA = P_EMPRESA)
                                    AND (I.CDTIPOOPER = P_TIPOOPER)
                                    AND (I.CDGRUPOFISC = C_IMPINSCESTAFO.CDGRUPOFISC)
                                    AND (I.CDIMPOSTO = C_IMPINSCESTAFO.CDIMPOSTO)
                                    AND (I.CDINSCESTA = RTRIM(P_INSCESTA))
                                    AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                    AND (I.CDPRODUTO = P.CDPRODUTO(+))
                                    AND I.IDINCINTEREST = 'N'
                                    AND (P.CDARVPROD = C_IMPOSTO.CDARVPROD))
            LOOP
               SELECT COUNT(*)
                 INTO V_COUNTMENS
                 FROM MENSAGEM
                WHERE CDMENSAGEM = C_INCIDENCIA.CDMENSAGEM
                  AND IDSITUACAOMENS = 'I';

               IF V_COUNTMENS > 0 THEN
                  V_DSMENSINCID := NULL;
               ELSE
                  V_DSMENSINCID := C_INCIDENCIA.DSMENSINCID;
               END IF;

               INSERT INTO INCITEMP
                  (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                   CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                   CDENQLEGALIPI, CDCEST)
               VALUES
                  (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDENCIA.CDIMPOSTO, C_INCIDENCIA.IDTRATIMPOIN,
                   C_INCIDENCIA.CDFISCAL, V_DSMENSINCID, P_INSCESTA, C_INCIDENCIA.CDDETALHEPROD,
                   C_INCIDENCIA.CDENQLEGALIPI, C_INCIDENCIA.CDCEST);

               V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
            END LOOP;
         ELSIF (C_IMPOSTO.CDARVPROD IS NULL) THEN
            FOR C_INCIDNULL IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                       CDMENSAGEM, CDDETALHEPROD, CDENQLEGALIPI, CDCEST
                                  FROM INCIDENCIA I, MENSINCID M
                                 WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                   AND (I.CDEMPRESA = P_EMPRESA)
                                   AND (I.CDTIPOOPER = P_TIPOOPER)
                                   AND (I.CDGRUPOFISC = C_IMPINSCESTAFO.CDGRUPOFISC)
                                   AND (I.CDIMPOSTO = C_IMPINSCESTAFO.CDIMPOSTO)
                                   AND (I.CDINSCESTA = RTRIM(P_INSCESTA))
                                   AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                   AND I.IDINCINTEREST = 'N'
                                   AND (CDPRODUTO IS NULL))
            LOOP
               SELECT COUNT(*)
                 INTO V_COUNTMENS
                 FROM MENSAGEM
                WHERE CDMENSAGEM = C_INCIDNULL.CDMENSAGEM
                  AND IDSITUACAOMENS = 'I';

               IF V_COUNTMENS > 0 THEN
                  V_DSMENSINCID := NULL;
               ELSE
                  V_DSMENSINCID := C_INCIDNULL.DSMENSINCID;
               END IF;

               INSERT INTO INCITEMP
                  (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                   CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                   CDENQLEGALIPI, CDCEST)
               VALUES
                  (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDNULL.CDIMPOSTO, C_INCIDNULL.IDTRATIMPOIN,
                   C_INCIDNULL.CDFISCAL, V_DSMENSINCID, P_INSCESTA, C_INCIDNULL.CDDETALHEPROD,
                   C_INCIDNULL.CDENQLEGALIPI, C_INCIDNULL.CDCEST);

               V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
            END LOOP;
         END IF;
      END LOOP;
   END LOOP;

   /* Verificacao das incidencias referente as filiais parametrizadas na tela Parametros de Grupo Fiscal (Imposto/Inscricao Estadual) */
   /* Por Cliente */
   FOR C_IMPINSCESTACL IN (SELECT G.CDIMPOSTO, G.CDGRUPOFISC
                           FROM GRFISIMPIECLIE G,
                                (SELECT CDCLIENTE
                                   FROM NOTAFISCAL
                                  WHERE CDFILIAL = P_FILIAL
                                    AND NRLANCTONF = P_LANCTONF
                                    AND P_CDCLIENTE IS NULL
                                  UNION ALL
                                 SELECT CDCLIENTE
                                   FROM CLIENTE
                                  WHERE CDCLIENTE = P_CDCLIENTE) N
                          WHERE G.CDCLIENTE = N.CDCLIENTE
                            AND G.CDEMPRESA = P_EMPRESA
                        AND G.CDINSCESTA = P_INSCESTA
                        AND NOT G.CDGRUPOFISC IS NULL)
   LOOP

      V_NRINCITEMP := '';

      SELECT MAX(NRINCITEMP)
        INTO V_NRINCITEMP
        FROM INCITEMP
       WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
         AND CDIMPOTEMP = C_IMPINSCESTACL.CDIMPOSTO;

      IF NOT V_NRINCITEMP IS NULL THEN
         DELETE FROM INCITEMP
          WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
            AND CDIMPOTEMP = C_IMPINSCESTACL.CDIMPOSTO;
      END IF;

      /* Operacao Interestadual */
      IF P_IDINCINTEREST = 'S' THEN

         FOR C_IMPOSTO IN (SELECT MAX(P.CDARVPROD) AS CDARVPROD
                             FROM INCIDENCIA I, PRODUTO P, IMPOSTO IP
                            WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                              AND (I.CDEMPRESA = P_EMPRESA)
                              AND (I.CDTIPOOPER = P_TIPOOPER)
                              AND (I.CDINSCESTA = RTRIM(P_INSCESTA))
                              AND ((I.CDGRUPOFISC = C_IMPINSCESTACL.CDGRUPOFISC) AND (IP.IDGRUPFISSRV <> 'S'))
                              AND (I.CDIMPOSTO = C_IMPINSCESTACL.CDIMPOSTO)
                              AND (I.CDPRODUTO = P.CDPRODUTO(+))
                              AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1)
                              AND I.IDINCINTEREST = 'S'
                              AND (I.CDIMPOSTO = IP.CDIMPOSTO))
         LOOP
            IF NOT (C_IMPOSTO.CDARVPROD IS NULL) THEN
               FOR C_INCIDENCIA IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                           CDMENSAGEM, CDDETALHEPROD, CDENQLEGALIPI, I.CDCEST
                                      FROM INCIDENCIA I, MENSINCID M, PRODUTO P
                                     WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                       AND (I.CDEMPRESA = P_EMPRESA)
                                       AND (I.CDTIPOOPER = P_TIPOOPER)
                                       AND (I.CDGRUPOFISC = C_IMPINSCESTACL.CDGRUPOFISC)
                                       AND (I.CDIMPOSTO = C_IMPINSCESTACL.CDIMPOSTO)
                                       AND (I.CDINSCESTA = RTRIM(P_INSCESTA))
                                       AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                       AND (I.CDPRODUTO = P.CDPRODUTO(+))
                                       AND I.IDINCINTEREST = 'S'
                                       AND (P.CDARVPROD = C_IMPOSTO.CDARVPROD))
               LOOP
                  SELECT COUNT(*)
                    INTO V_COUNTMENS
                    FROM MENSAGEM
                   WHERE CDMENSAGEM = C_INCIDENCIA.CDMENSAGEM
                     AND IDSITUACAOMENS = 'I';

                  IF V_COUNTMENS > 0 THEN
                     V_DSMENSINCID := NULL;
                  ELSE
                     V_DSMENSINCID := C_INCIDENCIA.DSMENSINCID;
                  END IF;

                  INSERT INTO INCITEMP
                     (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                      CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                      CDENQLEGALIPI, CDCEST)
                  VALUES
                     (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDENCIA.CDIMPOSTO, C_INCIDENCIA.IDTRATIMPOIN,
                      C_INCIDENCIA.CDFISCAL, V_DSMENSINCID, P_INSCESTA, C_INCIDENCIA.CDDETALHEPROD,
                      C_INCIDENCIA.CDENQLEGALIPI, C_INCIDENCIA.CDCEST);

                  V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
               END LOOP;
            ELSIF (C_IMPOSTO.CDARVPROD IS NULL) THEN
               FOR C_INCIDNULL IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                          CDMENSAGEM, CDDETALHEPROD, CDENQLEGALIPI, CDCEST
                                     FROM INCIDENCIA I, MENSINCID M
                                    WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                      AND (I.CDEMPRESA = P_EMPRESA)
                                      AND (I.CDTIPOOPER = P_TIPOOPER)
                                      AND (I.CDGRUPOFISC = C_IMPINSCESTACL.CDGRUPOFISC)
                                      AND (I.CDIMPOSTO = C_IMPINSCESTACL.CDIMPOSTO)
                                      AND (I.CDINSCESTA = RTRIM(P_INSCESTA))
                                      AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                      AND I.IDINCINTEREST = 'S'
                                      AND (CDPRODUTO IS NULL))
               LOOP
                  SELECT COUNT(*)
                    INTO V_COUNTMENS
                    FROM MENSAGEM
                   WHERE CDMENSAGEM = C_INCIDNULL.CDMENSAGEM
                     AND IDSITUACAOMENS = 'I';

                  IF V_COUNTMENS > 0 THEN
                     V_DSMENSINCID := NULL;
                  ELSE
                     V_DSMENSINCID := C_INCIDNULL.DSMENSINCID;
                  END IF;

                  INSERT INTO INCITEMP
                     (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                      CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                      CDENQLEGALIPI, CDCEST)
                  VALUES
                     (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDNULL.CDIMPOSTO, C_INCIDNULL.IDTRATIMPOIN,
                      C_INCIDNULL.CDFISCAL, V_DSMENSINCID, P_INSCESTA, C_INCIDNULL.CDDETALHEPROD,
                      C_INCIDNULL.CDENQLEGALIPI, C_INCIDNULL.CDCEST);

                  V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
               END LOOP;
            END IF;
         END LOOP;
      END IF;

      FOR C_IMPOSTO IN (SELECT MAX(P.CDARVPROD) AS CDARVPROD
                          FROM INCIDENCIA I, PRODUTO P, IMPOSTO IP
                         WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                           AND (I.CDEMPRESA = P_EMPRESA)
                           AND (I.CDTIPOOPER = P_TIPOOPER)
                           AND (I.CDINSCESTA = RTRIM(P_INSCESTA))
                           AND ((I.CDGRUPOFISC = C_IMPINSCESTACL.CDGRUPOFISC) AND (IP.IDGRUPFISSRV <> 'S'))
                           AND (I.CDIMPOSTO = C_IMPINSCESTACL.CDIMPOSTO)
                           AND (I.CDPRODUTO = P.CDPRODUTO(+))
                           AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1)
                           AND I.IDINCINTEREST = 'N'
                           AND NOT EXISTS (SELECT NRINCITEMP
                                            FROM INCITEMP
                                           WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
                                             AND CDIMPOTEMP = I.CDIMPOSTO)
                           AND (I.CDIMPOSTO = IP.CDIMPOSTO))
      LOOP
         IF NOT (C_IMPOSTO.CDARVPROD IS NULL) THEN
            FOR C_INCIDENCIA IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                        CDMENSAGEM, CDDETALHEPROD, CDENQLEGALIPI, I.CDCEST
                                   FROM INCIDENCIA I, MENSINCID M, PRODUTO P
                                  WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                    AND (I.CDEMPRESA = P_EMPRESA)
                                    AND (I.CDTIPOOPER = P_TIPOOPER)
                                    AND (I.CDGRUPOFISC = C_IMPINSCESTACL.CDGRUPOFISC)
                                    AND (I.CDIMPOSTO = C_IMPINSCESTACL.CDIMPOSTO)
                                    AND (I.CDINSCESTA = RTRIM(P_INSCESTA))
                                    AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                    AND (I.CDPRODUTO = P.CDPRODUTO(+))
                                    AND I.IDINCINTEREST = 'N'
                                    AND (P.CDARVPROD = C_IMPOSTO.CDARVPROD))
            LOOP
               SELECT COUNT(*)
                 INTO V_COUNTMENS
                 FROM MENSAGEM
                WHERE CDMENSAGEM = C_INCIDENCIA.CDMENSAGEM
                  AND IDSITUACAOMENS = 'I';

               IF V_COUNTMENS > 0 THEN
                  V_DSMENSINCID := NULL;
               ELSE
                  V_DSMENSINCID := C_INCIDENCIA.DSMENSINCID;
               END IF;

               INSERT INTO INCITEMP
                  (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                   CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                   CDENQLEGALIPI, CDCEST)
               VALUES
                  (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDENCIA.CDIMPOSTO, C_INCIDENCIA.IDTRATIMPOIN,
                   C_INCIDENCIA.CDFISCAL, V_DSMENSINCID, P_INSCESTA, C_INCIDENCIA.CDDETALHEPROD,
                   C_INCIDENCIA.CDENQLEGALIPI, C_INCIDENCIA.CDCEST);

               V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
            END LOOP;
         ELSIF (C_IMPOSTO.CDARVPROD IS NULL) THEN
            FOR C_INCIDNULL IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                       CDMENSAGEM, CDDETALHEPROD, CDENQLEGALIPI, CDCEST
                                  FROM INCIDENCIA I, MENSINCID M
                                 WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                   AND (I.CDEMPRESA = P_EMPRESA)
                                   AND (I.CDTIPOOPER = P_TIPOOPER)
                                   AND (I.CDGRUPOFISC = C_IMPINSCESTACL.CDGRUPOFISC)
                                   AND (I.CDIMPOSTO = C_IMPINSCESTACL.CDIMPOSTO)
                                   AND (I.CDINSCESTA = RTRIM(P_INSCESTA))
                                   AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                   AND I.IDINCINTEREST = 'N'
                                   AND (CDPRODUTO IS NULL))
            LOOP
               SELECT COUNT(*)
                 INTO V_COUNTMENS
                 FROM MENSAGEM
                WHERE CDMENSAGEM = C_INCIDNULL.CDMENSAGEM
                  AND IDSITUACAOMENS = 'I';

               IF V_COUNTMENS > 0 THEN
                  V_DSMENSINCID := NULL;
               ELSE
                  V_DSMENSINCID := C_INCIDNULL.DSMENSINCID;
               END IF;

               INSERT INTO INCITEMP
                  (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                   CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                   CDENQLEGALIPI, CDCEST)
               VALUES
                  (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDNULL.CDIMPOSTO, C_INCIDNULL.IDTRATIMPOIN,
                   C_INCIDNULL.CDFISCAL, V_DSMENSINCID, P_INSCESTA, C_INCIDNULL.CDDETALHEPROD,
                   C_INCIDNULL.CDENQLEGALIPI, C_INCIDNULL.CDCEST);

               V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
            END LOOP;
         END IF;
      END LOOP;
   END LOOP;

   /*------------------------IMPOSTOS FEDERAIS--------------------------------------*/
   IF (P_CDGRUPOFISC IS NOT NULL)  THEN

      /* Operacao Interestadual */
      IF P_IDINCINTEREST = 'S' THEN

         FOR C_IMPOSTO IN (SELECT MAX(P.CDARVPROD) AS CDARVPROD, I.CDIMPOSTO
                             FROM INCIDENCIA I, PRODUTO P, IMPOSTO IP
                            WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                              AND (I.CDEMPRESA    = P_EMPRESA)
                              AND (I.CDTIPOOPER   = P_TIPOOPER)
                              AND (I.CDINSCESTA   IS NULL)
                              AND ((I.CDGRUPOFISC  = P_CDGRUPOFISC) AND (IP.IDGRUPFISSRV <> 'S'))
                              AND (I.CDPRODUTO    = P.CDPRODUTO(+))
                              AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1)
                              AND (I.CDIMPOSTO = IP.CDIMPOSTO)
                              AND I.IDINCINTEREST = 'S'
                           GROUP BY I.CDIMPOSTO) LOOP

            IF NOT (C_IMPOSTO.CDARVPROD IS NULL) THEN
               FOR C_INCIDENCIA IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                           NVL(I.IDINCINTEREST,'N') IDINCINTEREST, CDMENSAGEM, CDDETALHEPROD, 
                                           CDENQLEGALIPI, I.CDCEST
                                      FROM INCIDENCIA I, MENSINCID M, PRODUTO P
                                     WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                       AND (I.CDEMPRESA    = P_EMPRESA)
                                       AND (I.CDTIPOOPER   = P_TIPOOPER)
                                       AND (I.CDGRUPOFISC  = P_CDGRUPOFISC)
                                       AND (I.CDIMPOSTO    = C_IMPOSTO.CDIMPOSTO)
                                       AND (I.CDINSCESTA   IS NULL)
                                       AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                       AND (I.CDPRODUTO    = P.CDPRODUTO(+))
                                       AND I.IDINCINTEREST = 'S'
                                       AND (P.CDARVPROD    = C_IMPOSTO.CDARVPROD)) LOOP

                  SELECT COUNT(*)
                    INTO V_COUNT
                    FROM INCITEMP
                   WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
                     AND CDIMPOTEMP = C_INCIDENCIA.CDIMPOSTO;

                  IF V_COUNT = 0 THEN
                     SELECT COUNT(*)
                       INTO V_COUNTMENS
                       FROM MENSAGEM
                      WHERE CDMENSAGEM = C_INCIDENCIA.CDMENSAGEM
                        AND IDSITUACAOMENS = 'I';

                     IF V_COUNTMENS > 0 THEN
                        V_DSMENSINCID := NULL;
                     ELSE
                        V_DSMENSINCID := C_INCIDENCIA.DSMENSINCID;
                     END IF;

                     INSERT INTO INCITEMP
                        (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                         CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                         CDENQLEGALIPI, CDCEST)
                     VALUES
                        (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDENCIA.CDIMPOSTO, C_INCIDENCIA.IDTRATIMPOIN,
                         C_INCIDENCIA.CDFISCAL, V_DSMENSINCID , NULL, C_INCIDENCIA.CDDETALHEPROD,
                         C_INCIDENCIA.CDENQLEGALIPI, C_INCIDENCIA.CDCEST);

                     V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
                  END IF;
               END LOOP;
            ELSIF (C_IMPOSTO.CDARVPROD IS NULL) THEN
               FOR C_INCIDNULL IN ( SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                           NVL(I.IDINCINTEREST,'N') IDINCINTEREST, CDMENSAGEM, CDDETALHEPROD, 
                                           CDENQLEGALIPI, CDCEST
                                      FROM INCIDENCIA I, MENSINCID M
                                     WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                       AND (I.CDEMPRESA    = P_EMPRESA)
                                       AND (I.CDTIPOOPER   = P_TIPOOPER)
                                       AND (I.CDGRUPOFISC  = P_CDGRUPOFISC)
                                       AND (I.CDIMPOSTO    = C_IMPOSTO.CDIMPOSTO)
                                       AND (I.CDINSCESTA   IS NULL)
                                       AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                       AND I.IDINCINTEREST = 'S'
                                       AND (CDPRODUTO IS NULL)) LOOP
                  SELECT COUNT(*)
                    INTO V_COUNT
                    FROM INCITEMP
                   WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
                     AND CDIMPOTEMP = C_INCIDNULL.CDIMPOSTO;

                  IF V_COUNT = 0 THEN

                     SELECT COUNT(*)
                       INTO V_COUNTMENS
                       FROM MENSAGEM
                      WHERE CDMENSAGEM = C_INCIDNULL.CDMENSAGEM
                        AND IDSITUACAOMENS = 'I';

                      IF V_COUNTMENS > 0 THEN
                         V_DSMENSINCID := NULL;
                      ELSE
                         V_DSMENSINCID := C_INCIDNULL.DSMENSINCID;
                      END IF;

                      INSERT INTO INCITEMP
                         (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                          CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                          CDENQLEGALIPI, CDCEST)
                      VALUES
                         (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDNULL.CDIMPOSTO, C_INCIDNULL.IDTRATIMPOIN,
                          C_INCIDNULL.CDFISCAL, V_DSMENSINCID, NULL, C_INCIDNULL.CDDETALHEPROD,
                          C_INCIDNULL.CDENQLEGALIPI, C_INCIDNULL.CDCEST);

                      V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
                  END IF;
               END LOOP;
            END IF;
         END LOOP;
      END IF;

      FOR C_IMPOSTO IN (SELECT MAX(P.CDARVPROD) AS CDARVPROD, I.CDIMPOSTO
                          FROM INCIDENCIA I, PRODUTO P, IMPOSTO IP
                         WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                           AND (I.CDEMPRESA    = P_EMPRESA)
                           AND (I.CDTIPOOPER   = P_TIPOOPER)
                           AND (I.CDINSCESTA   IS NULL)
                           AND ((I.CDGRUPOFISC  = P_CDGRUPOFISC) AND (IP.IDGRUPFISSRV <> 'S'))
                           AND (I.CDPRODUTO    = P.CDPRODUTO(+))
                           AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1)
                           AND (I.CDIMPOSTO = IP.CDIMPOSTO)
                           AND I.IDINCINTEREST = 'N'
                         GROUP BY I.CDIMPOSTO) LOOP

         IF NOT (C_IMPOSTO.CDARVPROD IS NULL) THEN
            FOR C_INCIDENCIA IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                        NVL(I.IDINCINTEREST,'N') IDINCINTEREST, CDMENSAGEM, CDDETALHEPROD, 
                                        CDENQLEGALIPI, I.CDCEST
                                   FROM INCIDENCIA I, MENSINCID M, PRODUTO P
                                  WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                    AND (I.CDEMPRESA    = P_EMPRESA)
                                    AND (I.CDTIPOOPER   = P_TIPOOPER)
                                    AND (I.CDGRUPOFISC  = P_CDGRUPOFISC)
                                    AND (I.CDIMPOSTO    = C_IMPOSTO.CDIMPOSTO)
                                    AND (I.CDINSCESTA   IS NULL)
                                    AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                    AND (I.CDPRODUTO    = P.CDPRODUTO(+))
                                    AND I.IDINCINTEREST = 'N'
                                    AND (P.CDARVPROD    = C_IMPOSTO.CDARVPROD)) LOOP

               SELECT COUNT(*)
                 INTO V_COUNT
                 FROM INCITEMP
                WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
                  AND CDIMPOTEMP = C_INCIDENCIA.CDIMPOSTO;

               IF V_COUNT = 0 THEN
                  SELECT COUNT(*)
                    INTO V_COUNTMENS
                    FROM MENSAGEM
                   WHERE CDMENSAGEM = C_INCIDENCIA.CDMENSAGEM
                     AND IDSITUACAOMENS = 'I';

                  IF V_COUNTMENS > 0 THEN
                     V_DSMENSINCID := NULL;
                  ELSE
                     V_DSMENSINCID := C_INCIDENCIA.DSMENSINCID;
                  END IF;


                  INSERT INTO INCITEMP
                     (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                      CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                      CDENQLEGALIPI, CDCEST)
                  VALUES
                     (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDENCIA.CDIMPOSTO, C_INCIDENCIA.IDTRATIMPOIN,
                      C_INCIDENCIA.CDFISCAL, V_DSMENSINCID, NULL, C_INCIDENCIA.CDDETALHEPROD,
                      C_INCIDENCIA.CDENQLEGALIPI, C_INCIDENCIA.CDCEST);

                  V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
               END IF;
            END LOOP;
         ELSIF (C_IMPOSTO.CDARVPROD IS NULL) THEN
            FOR C_INCIDNULL IN ( SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                        NVL(I.IDINCINTEREST,'N') IDINCINTEREST, CDMENSAGEM, CDDETALHEPROD, 
                                        CDENQLEGALIPI, CDCEST
                                   FROM INCIDENCIA I, MENSINCID M
                                  WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                    AND (I.CDEMPRESA    = P_EMPRESA)
                                    AND (I.CDTIPOOPER   = P_TIPOOPER)
                                    AND (I.CDGRUPOFISC  = P_CDGRUPOFISC)
                                    AND (I.CDIMPOSTO    = C_IMPOSTO.CDIMPOSTO)
                                    AND (I.CDINSCESTA   IS NULL)
                                    AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                    AND I.IDINCINTEREST = 'N'
                                    AND (CDPRODUTO IS NULL)) LOOP
               SELECT COUNT(*)
                 INTO V_COUNT
                 FROM INCITEMP
                WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
                  AND CDIMPOTEMP = C_INCIDNULL.CDIMPOSTO;

               IF V_COUNT = 0 THEN

                  SELECT COUNT(*)
                    INTO V_COUNTMENS
                    FROM MENSAGEM
                   WHERE CDMENSAGEM = C_INCIDNULL.CDMENSAGEM
                     AND IDSITUACAOMENS = 'I';

                   IF V_COUNTMENS > 0 THEN
                      V_DSMENSINCID := NULL;
                   ELSE
                      V_DSMENSINCID := C_INCIDNULL.DSMENSINCID;
                   END IF;

                   INSERT INTO INCITEMP
                      (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                       CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                       CDENQLEGALIPI, CDCEST)
                   VALUES
                      (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDNULL.CDIMPOSTO, C_INCIDNULL.IDTRATIMPOIN,
                       C_INCIDNULL.CDFISCAL, V_DSMENSINCID, NULL, C_INCIDNULL.CDDETALHEPROD,
                       C_INCIDNULL.CDENQLEGALIPI, C_INCIDNULL.CDCEST);

                   V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
               END IF;
            END LOOP;
         END IF;
      END LOOP;
   ELSE
      /* Operacao Interestadual */
      IF P_IDINCINTEREST = 'S' THEN

         FOR C_IMPOSTO IN (SELECT MAX(P.CDARVPROD) AS CDARVPROD, CDIMPOSTO
                             FROM INCIDENCIA I, PRODUTO P
                            WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                              AND (I.CDEMPRESA    = P_EMPRESA)
                              AND (I.CDTIPOOPER   = P_TIPOOPER)
                              AND (I.CDINSCESTA   IS NULL)
                              AND (I.CDGRUPOFISC IS NULL)
                              AND (I.CDPRODUTO    = P.CDPRODUTO(+))
                              AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1)
                              AND I.IDINCINTEREST = 'S'
                           GROUP BY CDIMPOSTO) LOOP
            IF NOT (C_IMPOSTO.CDARVPROD IS NULL) THEN
               FOR C_INCIDENCIA IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                           NVL(I.IDINCINTEREST, 'N') IDINCINTEREST, CDMENSAGEM, CDDETALHEPROD, 
                                           CDENQLEGALIPI, I.CDCEST
                                      FROM INCIDENCIA I, MENSINCID M, PRODUTO P
                                     WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                       AND (I.CDEMPRESA    = P_EMPRESA)
                                       AND (I.CDTIPOOPER   = P_TIPOOPER)
                                       AND (I.CDIMPOSTO    = C_IMPOSTO.CDIMPOSTO)
                                       AND (I.CDINSCESTA   IS NULL)
                                       AND (I.CDGRUPOFISC IS NULL)
                                       AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                       AND (I.CDPRODUTO    = P.CDPRODUTO(+))
                                       AND I.IDINCINTEREST = 'S'
                                       AND (P.CDARVPROD    = C_IMPOSTO.CDARVPROD)) LOOP

                  SELECT COUNT(*)
                    INTO V_COUNT
                    FROM INCITEMP
                   WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
                     AND CDIMPOTEMP = C_INCIDENCIA.CDIMPOSTO;

                  IF V_COUNT = 0 THEN
                     SELECT COUNT(*)
                       INTO V_COUNTMENS
                       FROM MENSAGEM
                      WHERE CDMENSAGEM = C_INCIDENCIA.CDMENSAGEM
                        AND IDSITUACAOMENS = 'I';

                     IF V_COUNTMENS > 0 THEN
                        V_DSMENSINCID := NULL;
                     ELSE
                        V_DSMENSINCID := C_INCIDENCIA.DSMENSINCID;
                     END IF;

                     INSERT INTO INCITEMP
                        (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                         CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                         CDENQLEGALIPI, CDCEST)
                     VALUES
                         (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDENCIA.CDIMPOSTO, C_INCIDENCIA.IDTRATIMPOIN,
                          C_INCIDENCIA.CDFISCAL, V_DSMENSINCID, NULL, C_INCIDENCIA.CDDETALHEPROD,
                          C_INCIDENCIA.CDENQLEGALIPI, C_INCIDENCIA.CDCEST);

                     V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
                  END IF;

               END LOOP;
            ELSIF (C_IMPOSTO.CDARVPROD IS NULL) THEN
               FOR C_INCIDNULL IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                          NVL(I.IDINCINTEREST, 'N') IDINCINTEREST, CDMENSAGEM, CDDETALHEPROD, 
                                          CDENQLEGALIPI, CDCEST
                                     FROM INCIDENCIA I, MENSINCID M
                                    WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                      AND (I.CDEMPRESA    = P_EMPRESA)
                                      AND (I.CDTIPOOPER   = P_TIPOOPER)
                                      AND (I.CDIMPOSTO    = C_IMPOSTO.CDIMPOSTO)
                                      AND (I.CDINSCESTA   IS NULL)
                                      AND (I.CDGRUPOFISC  IS NULL)
                                      AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                      AND I.IDINCINTEREST = 'S'
                                      AND (CDPRODUTO IS NULL)) LOOP
                  SELECT COUNT(*)
                    INTO V_COUNT
                    FROM INCITEMP
                   WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
                     AND CDIMPOTEMP = C_INCIDNULL.CDIMPOSTO;

                  IF V_COUNT = 0 THEN

                     SELECT COUNT(*)
                       INTO V_COUNTMENS
                       FROM MENSAGEM
                      WHERE CDMENSAGEM = C_INCIDNULL.CDMENSAGEM
                        AND IDSITUACAOMENS = 'I';

                     IF V_COUNTMENS > 0 THEN
                        V_DSMENSINCID := NULL;
                     ELSE
                        V_DSMENSINCID := C_INCIDNULL.DSMENSINCID;
                     END IF;

                     INSERT INTO INCITEMP
                        (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                         CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                         CDENQLEGALIPI, CDCEST)
                     VALUES
                        (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDNULL.CDIMPOSTO, C_INCIDNULL.IDTRATIMPOIN,
                         C_INCIDNULL.CDFISCAL, V_DSMENSINCID, NULL, C_INCIDNULL.CDDETALHEPROD,
                         C_INCIDNULL.CDENQLEGALIPI, C_INCIDNULL.CDCEST);

                     V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
                  END IF;
               END LOOP;
            END IF;
         END LOOP;
      END IF;

      FOR C_IMPOSTO IN (SELECT MAX(P.CDARVPROD) AS CDARVPROD, CDIMPOSTO
                          FROM INCIDENCIA I, PRODUTO P
                         WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                           AND (I.CDEMPRESA    = P_EMPRESA)
                           AND (I.CDTIPOOPER   = P_TIPOOPER)
                           AND (I.CDINSCESTA   IS NULL)
                           AND (I.CDGRUPOFISC IS NULL)
                           AND (I.CDPRODUTO    = P.CDPRODUTO(+))
                           AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1)
                           AND I.IDINCINTEREST = 'N'
                         GROUP BY CDIMPOSTO) LOOP

         IF NOT (C_IMPOSTO.CDARVPROD IS NULL) THEN
            FOR C_INCIDENCIA IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                        NVL(I.IDINCINTEREST, 'N') IDINCINTEREST, CDMENSAGEM, CDDETALHEPROD, 
                                        CDENQLEGALIPI, I.CDCEST
                                   FROM INCIDENCIA I, MENSINCID M, PRODUTO P
                                  WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                    AND (I.CDEMPRESA    = P_EMPRESA)
                                    AND (I.CDTIPOOPER   = P_TIPOOPER)
                                    AND (I.CDIMPOSTO    = C_IMPOSTO.CDIMPOSTO)
                                    AND (I.CDINSCESTA   IS NULL)
                                    AND (I.CDGRUPOFISC IS NULL)
                                    AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                    AND (I.CDPRODUTO    = P.CDPRODUTO(+))
                                    AND I.IDINCINTEREST = 'N'
                                    AND (P.CDARVPROD    = C_IMPOSTO.CDARVPROD)) LOOP

               SELECT COUNT(*)
                 INTO V_COUNT
                 FROM INCITEMP
                WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
                  AND CDIMPOTEMP = C_INCIDENCIA.CDIMPOSTO;

               IF V_COUNT = 0 THEN
                  SELECT COUNT(*)
                    INTO V_COUNTMENS
                    FROM MENSAGEM
                   WHERE CDMENSAGEM = C_INCIDENCIA.CDMENSAGEM
                     AND IDSITUACAOMENS = 'I';

                  IF V_COUNTMENS > 0 THEN
                     V_DSMENSINCID := NULL;
                  ELSE
                     V_DSMENSINCID := C_INCIDENCIA.DSMENSINCID;
                  END IF;

                  INSERT INTO INCITEMP
                     (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                      CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                      CDENQLEGALIPI, CDCEST)
                  VALUES
                      (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDENCIA.CDIMPOSTO, C_INCIDENCIA.IDTRATIMPOIN,
                       C_INCIDENCIA.CDFISCAL, V_DSMENSINCID, NULL, C_INCIDENCIA.CDDETALHEPROD,
                       C_INCIDENCIA.CDENQLEGALIPI, C_INCIDENCIA.CDCEST);

                  V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
               END IF;
            END LOOP;
         ELSIF (C_IMPOSTO.CDARVPROD IS NULL) THEN
            FOR C_INCIDNULL IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                       NVL(I.IDINCINTEREST, 'N') IDINCINTEREST, CDMENSAGEM, CDDETALHEPROD, 
                                       CDENQLEGALIPI, CDCEST
                                  FROM INCIDENCIA I, MENSINCID M
                                 WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                   AND (I.CDEMPRESA    = P_EMPRESA)
                                   AND (I.CDTIPOOPER   = P_TIPOOPER)
                                   AND (I.CDIMPOSTO    = C_IMPOSTO.CDIMPOSTO)
                                   AND (I.CDINSCESTA   IS NULL)
                                   AND (I.CDGRUPOFISC  IS NULL)
                                   AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                   AND I.IDINCINTEREST = 'N'
                                   AND (CDPRODUTO IS NULL)) LOOP
               SELECT COUNT(*)
                 INTO V_COUNT
                 FROM INCITEMP
                WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
                  AND CDIMPOTEMP = C_INCIDNULL.CDIMPOSTO;

               IF V_COUNT = 0 THEN

                  SELECT COUNT(*)
                    INTO V_COUNTMENS
                    FROM MENSAGEM
                   WHERE CDMENSAGEM = C_INCIDNULL.CDMENSAGEM
                     AND IDSITUACAOMENS = 'I';

                  IF V_COUNTMENS > 0 THEN
                     V_DSMENSINCID := NULL;
                  ELSE
                     V_DSMENSINCID := C_INCIDNULL.DSMENSINCID;
                  END IF;

                  INSERT INTO INCITEMP
                     (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                      CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                      CDENQLEGALIPI, CDCEST)
                  VALUES
                     (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDNULL.CDIMPOSTO, C_INCIDNULL.IDTRATIMPOIN,
                      C_INCIDNULL.CDFISCAL, V_DSMENSINCID, NULL, C_INCIDNULL.CDDETALHEPROD,
                      C_INCIDNULL.CDENQLEGALIPI, C_INCIDNULL.CDCEST);

                  V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
               END IF;
            END LOOP;
         END IF;
      END LOOP;
   END IF;

   /* Verificacao das incidencias referente as filiais parametrizadas na tela Parametros de Grupo Fiscal (Imposto/Filial) */
   FOR C_IMPFILI IN (SELECT G.CDIMPOSTO, G.CDGRUPOFISC
                       FROM GRUPOFISCIMPFIL G,
                            (SELECT DECODE(P_ENTRSAIDIN, 'E', CDFILIRECE, CDFILIEMIT) AS CDFILIAL
                               FROM NOTAFISCAL
                              WHERE CDFILIAL = P_FILIAL
                                AND NRLANCTONF = P_LANCTONF
                                AND P_IDSISTEMA IS NULL
                                AND P_CDFILIRECE IS NULL
                             UNION ALL
                             SELECT CDFILIAL
                               FROM PEDIDO
                              WHERE CDFILIAL = SUBSTR(P_FILIAL,1,4)
                                AND NRPEDIDO = SUBSTR(P_FILIAL,5,10)
                                AND P_IDSISTEMA = 'PED'
                             UNION ALL
                             SELECT CDFILIAL
                               FROM FILIAL
                              WHERE CDFILIAL = P_CDFILIRECE) F
                     WHERE G.CDFILIAL = F.CDFILIAL
                       AND NOT G.CDGRUPOFISC IS NULL)
   LOOP
      /* Operacao Interestadual */
      IF P_IDINCINTEREST = 'S' THEN

         FOR C_IMPOSTO IN (SELECT MAX(P.CDARVPROD) AS CDARVPROD
                             FROM INCIDENCIA I, PRODUTO P, IMPOSTO IP
                            WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                              AND (I.CDEMPRESA = P_EMPRESA)
                              AND (I.CDTIPOOPER = P_TIPOOPER)
                              AND (I.CDINSCESTA IS NULL)
                              AND ((I.CDGRUPOFISC = C_IMPFILI.CDGRUPOFISC) AND (IP.IDGRUPFISSRV <> 'S'))
                              AND (I.CDIMPOSTO = C_IMPFILI.CDIMPOSTO)
                              AND (I.CDPRODUTO = P.CDPRODUTO(+))
                              AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1)
                              AND I.IDINCINTEREST = 'S'
                              AND (I.CDIMPOSTO = IP.CDIMPOSTO))
         LOOP
            SELECT COUNT(*)
              INTO V_EXISTE
              FROM INCITEMP
             WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
               AND CDIMPOTEMP = C_IMPFILI.CDIMPOSTO
               AND NOT CDINSCESTAITMP IS NULL;

            IF V_EXISTE = 0 THEN
               DELETE FROM INCITEMP
                WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
                  AND CDIMPOTEMP = C_IMPFILI.CDIMPOSTO;

               IF NOT (C_IMPOSTO.CDARVPROD IS NULL) THEN
                  FOR C_INCIDENCIA IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                              CDMENSAGEM, CDDETALHEPROD, CDENQLEGALIPI, I.CDCEST
                                         FROM INCIDENCIA I, MENSINCID M, PRODUTO P
                                        WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                          AND (I.CDEMPRESA = P_EMPRESA)
                                          AND (I.CDTIPOOPER = P_TIPOOPER)
                                          AND (I.CDGRUPOFISC = C_IMPFILI.CDGRUPOFISC)
                                          AND (I.CDIMPOSTO = C_IMPFILI.CDIMPOSTO)
                                          AND (I.CDINSCESTA IS NULL)
                                          AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                          AND (I.CDPRODUTO = P.CDPRODUTO(+))
                                          AND I.IDINCINTEREST = 'S'
                                          AND (P.CDARVPROD = C_IMPOSTO.CDARVPROD))
                  LOOP
                     SELECT COUNT(*)
                       INTO V_COUNTMENS
                       FROM MENSAGEM
                      WHERE CDMENSAGEM = C_INCIDENCIA.CDMENSAGEM
                        AND IDSITUACAOMENS = 'I';

                     IF V_COUNTMENS > 0 THEN
                        V_DSMENSINCID := NULL;
                     ELSE
                        V_DSMENSINCID := C_INCIDENCIA.DSMENSINCID;
                     END IF;

                     INSERT INTO INCITEMP
                        (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                         CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                         CDENQLEGALIPI, CDCEST)
                     VALUES
                        (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDENCIA.CDIMPOSTO, C_INCIDENCIA.IDTRATIMPOIN,
                         C_INCIDENCIA.CDFISCAL, V_DSMENSINCID, NULL, C_INCIDENCIA.CDDETALHEPROD,
                         C_INCIDENCIA.CDENQLEGALIPI, C_INCIDENCIA.CDCEST);

                     V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
                  END LOOP;
               ELSIF (C_IMPOSTO.CDARVPROD IS NULL) THEN
                  FOR C_INCIDNULL IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                             CDMENSAGEM, CDDETALHEPROD, CDENQLEGALIPI, CDCEST
                                        FROM INCIDENCIA I, MENSINCID M
                                       WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                         AND (I.CDEMPRESA = P_EMPRESA)
                                         AND (I.CDTIPOOPER = P_TIPOOPER)
                                         AND (I.CDGRUPOFISC = C_IMPFILI.CDGRUPOFISC)
                                         AND (I.CDIMPOSTO = C_IMPFILI.CDIMPOSTO)
                                         AND (I.CDINSCESTA IS NULL)
                                         AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                         AND I.IDINCINTEREST = 'S'
                                         AND (CDPRODUTO IS NULL))
                  LOOP
                     SELECT COUNT(*)
                       INTO V_COUNTMENS
                       FROM MENSAGEM
                      WHERE CDMENSAGEM = C_INCIDNULL.CDMENSAGEM
                        AND IDSITUACAOMENS = 'I';

                     IF V_COUNTMENS > 0 THEN
                        V_DSMENSINCID := NULL;
                     ELSE
                        V_DSMENSINCID := C_INCIDNULL.DSMENSINCID;
                     END IF;

                     INSERT INTO INCITEMP
                        (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                         CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                         CDENQLEGALIPI, CDCEST)
                     VALUES
                        (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDNULL.CDIMPOSTO, C_INCIDNULL.IDTRATIMPOIN,
                         C_INCIDNULL.CDFISCAL, V_DSMENSINCID, NULL, C_INCIDNULL.CDDETALHEPROD,
                         C_INCIDNULL.CDENQLEGALIPI, C_INCIDNULL.CDCEST);

                     V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
                  END LOOP;
               END IF;
            END IF;
         END LOOP;
      END IF;

      FOR C_IMPOSTO IN (SELECT MAX(P.CDARVPROD) AS CDARVPROD
                          FROM INCIDENCIA I, PRODUTO P, IMPOSTO IP
                         WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                           AND (I.CDEMPRESA = P_EMPRESA)
                           AND (I.CDTIPOOPER = P_TIPOOPER)
                           AND (I.CDINSCESTA IS NULL)
                           AND ((I.CDGRUPOFISC = C_IMPFILI.CDGRUPOFISC) AND (IP.IDGRUPFISSRV <> 'S'))
                           AND (I.CDIMPOSTO = C_IMPFILI.CDIMPOSTO)
                           AND (I.CDPRODUTO = P.CDPRODUTO(+))
                           AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1)
                           AND I.IDINCINTEREST = 'N'
                           AND (I.CDIMPOSTO = IP.CDIMPOSTO))
      LOOP
         SELECT COUNT(*)
           INTO V_EXISTE
           FROM INCITEMP
          WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
            AND CDIMPOTEMP = C_IMPFILI.CDIMPOSTO
            AND NOT CDINSCESTAITMP IS NULL;

         IF V_EXISTE = 0 THEN
            DELETE FROM INCITEMP
             WHERE NRINCITEMP = P_FILIAL||P_LANCTONF
               AND CDIMPOTEMP = C_IMPFILI.CDIMPOSTO;

            IF NOT (C_IMPOSTO.CDARVPROD IS NULL) THEN
               FOR C_INCIDENCIA IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                           CDMENSAGEM, CDDETALHEPROD, CDENQLEGALIPI, I.CDCEST
                                      FROM INCIDENCIA I, MENSINCID M, PRODUTO P
                                     WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                       AND (I.CDEMPRESA = P_EMPRESA)
                                       AND (I.CDTIPOOPER = P_TIPOOPER)
                                       AND (I.CDGRUPOFISC = C_IMPFILI.CDGRUPOFISC)
                                       AND (I.CDIMPOSTO = C_IMPFILI.CDIMPOSTO)
                                       AND (I.CDINSCESTA IS NULL)
                                       AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                       AND (I.CDPRODUTO = P.CDPRODUTO(+))
                                       AND I.IDINCINTEREST = 'N'
                                       AND (P.CDARVPROD = C_IMPOSTO.CDARVPROD))
               LOOP
                  SELECT COUNT(*)
                    INTO V_COUNTMENS
                    FROM MENSAGEM
                   WHERE CDMENSAGEM = C_INCIDENCIA.CDMENSAGEM
                     AND IDSITUACAOMENS = 'I';

                  IF V_COUNTMENS > 0 THEN
                     V_DSMENSINCID := NULL;
                  ELSE
                     V_DSMENSINCID := C_INCIDENCIA.DSMENSINCID;
                  END IF;

                  INSERT INTO INCITEMP
                     (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                      CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                      CDENQLEGALIPI, CDCEST)
                  VALUES
                     (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDENCIA.CDIMPOSTO, C_INCIDENCIA.IDTRATIMPOIN,
                      C_INCIDENCIA.CDFISCAL, V_DSMENSINCID, NULL, C_INCIDENCIA.CDDETALHEPROD,
                      C_INCIDENCIA.CDENQLEGALIPI, C_INCIDENCIA.CDCEST);

                  V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
               END LOOP;
            ELSIF (C_IMPOSTO.CDARVPROD IS NULL) THEN
               FOR C_INCIDNULL IN (SELECT IDTRATIMPOIN, I.CDIMPOSTO, CDFISCAL, DSMENSINCID,
                                          CDMENSAGEM, CDDETALHEPROD, CDENQLEGALIPI, CDCEST
                                     FROM INCIDENCIA I, MENSINCID M
                                    WHERE (I.IDENTRSAIDIN = P_ENTRSAIDIN)
                                      AND (I.CDEMPRESA = P_EMPRESA)
                                      AND (I.CDTIPOOPER = P_TIPOOPER)
                                      AND (I.CDGRUPOFISC = C_IMPFILI.CDGRUPOFISC)
                                      AND (I.CDIMPOSTO = C_IMPFILI.CDIMPOSTO)
                                      AND (I.CDINSCESTA IS NULL)
                                      AND (I.NRINCIDENCIA = M.NRINCIDENCIA(+))
                                      AND I.IDINCINTEREST = 'N'
                                      AND (CDPRODUTO IS NULL))
               LOOP
                  SELECT COUNT(*)
                    INTO V_COUNTMENS
                    FROM MENSAGEM
                   WHERE CDMENSAGEM = C_INCIDNULL.CDMENSAGEM
                     AND IDSITUACAOMENS = 'I';

                  IF V_COUNTMENS > 0 THEN
                     V_DSMENSINCID := NULL;
                  ELSE
                     V_DSMENSINCID := C_INCIDNULL.DSMENSINCID;
                  END IF;

                  INSERT INTO INCITEMP
                     (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                      CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD,
                      CDENQLEGALIPI, CDCEST)
                  VALUES
                     (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_INCIDNULL.CDIMPOSTO, C_INCIDNULL.IDTRATIMPOIN,
                      C_INCIDNULL.CDFISCAL, V_DSMENSINCID, NULL, C_INCIDNULL.CDDETALHEPROD,
                      C_INCIDNULL.CDENQLEGALIPI, C_INCIDNULL.CDCEST);

                  V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
               END LOOP;
            END IF;
         END IF;
      END LOOP;
   END LOOP;

   V_IDPRESENCIAL := '9';
   IF (P_LANCPRODUTO IS NULL) THEN
      Begin
         SELECT N.DTENTRSAID,   N.CDFILIEMIT,   N.IDORIGDESTNF,   N.CDCLIENTE,
                N.CDFILIRECE,   N.CDFORNECED, (IT.VRTOTAITEM - IT.VRDESCITEM), NVL(N.IDPRESENCIAL, '9'), NVL(R.IDOPPRESENDDIF, 'N')
           INTO V_INIVALID,   V_CDFILIEMIT, V_IDORIGDESTNF, V_CDCLIENTE,
                V_CDFILIRECE, V_CDFORNECED, V_VRITEM, V_IDPRESENCIAL, V_IDOPPRESENDDIF
           FROM NOTAFISCAL N, ITEMPRODUTO IT, PRODUTO P, PARAMGERAL R
          WHERE N.CDFILIAL = P_FILIAL
            AND N.NRLANCTONF = P_LANCTONF
            AND N.CDFILIAL = IT.CDFILIAL
            AND N.NRLANCTONF = IT.NRLANCTONF
            AND IT.CDPRODUTO = P.CDPRODUTO
            AND P.CDARVPROD  = P_PRODUTO
            AND ROWNUM = '1'
            AND R.CDPARGERAL = '0001';
      Exception
         When No_Data_Found Then
            V_INIVALID  := Null; V_CDFILIEMIT  := Null; V_IDORIGDESTNF:= Null; V_CDCLIENTE:= Null;
            V_CDFILIRECE := Null; V_CDFORNECED:= Null; V_VRITEM := 0; V_IDPRESENCIAL := '9';
      End;
   END IF;

   V_CDFILIRECE := NVL(V_CDFILIRECE, P_CDFILIRECE);
   V_CDFORNECED := NVL(V_CDFORNECED, P_CDFORNECED);
   V_CDCLIENTE  := NVL(V_CDCLIENTE,  P_CDCLIENTE);
   V_CDFILIEMIT := NVL(V_CDFILIEMIT, P_CDFILIEMIT);

   IF (P_LANCPRODUTO = 'S') THEN
      V_VRITEM := P_VRPRODUTO;
   END IF;

   IF (P_ENTRSAIDIN = 'S') THEN

      Begin
         SELECT NVL(MAX(IDENDCFOPFIL),'I'), MAX(CDEMPRESA),   MAX(CDINSCESTA)
         INTO   V_IDENDCFOP,           V_CDEMPRESA, V_CDINSCESTA
         FROM FILIAL
         WHERE (CDFILIAL = V_CDFILIEMIT);
      Exception
         When No_Data_Found Then
            V_IDENDCFOP:= 'I'; V_CDEMPRESA:= Null; V_CDINSCESTA:= Null;
      End;

      IF V_IDENDCFOP = 'I' THEN
         Begin
            SELECT MAX(CDPAIS)    , MAX(SGESTADO)  , MAX(CDMUNICIPIO)
            INTO   V_PAISORIG, V_ESTAORIG, V_MUNIORIG
            FROM INSCRESTAD
            WHERE CDEMPRESA  = P_EMPRESA AND
                  CDINSCESTA = P_INSCESTA;
         Exception
            When No_Data_Found Then
               V_PAISORIG := Null; V_ESTAORIG := Null; V_MUNIORIG := Null;
         End;
      ELSE
         Begin
            SELECT MAX(CDPAIS)    , MAX(SGESTADO)  , MAX(CDMUNICIPIO)
            INTO   V_PAISORIG, V_ESTAORIG, V_MUNIORIG
            FROM ENDEFILI
            WHERE (CDFILIAL     = V_CDFILIEMIT) AND
                  (IDTPENDEFILI = V_IDENDCFOP);
         Exception
            When No_Data_Found Then
               V_PAISORIG := Null; V_ESTAORIG := Null; V_MUNIORIG := Null;
         End;
      END IF;

      /*DESTINATARIO-REMETENTE DA NOTA*/
      IF V_IDORIGDESTNF IS NULL THEN
         Begin
            SELECT MAX(IDORIGDESTOP)
            INTO   V_ORIGDESTOP
            FROM TIPOOPERAC
            WHERE IDENTRSAIDOP = 'S' AND
                  CDTIPOOPER   = P_TIPOOPER;
         Exception
            When No_Data_Found Then
               V_ORIGDESTOP := Null;
         End;
      ELSE
         V_ORIGDESTOP := V_IDORIGDESTNF;
      END IF;

      IF V_ORIGDESTOP = 'L' THEN /*DESTINATARIO -> FILIAL */
         V_CDORIGDESTNF := V_CDFILIRECE;

         Begin
            SELECT NVL(MAX(IDENDCFOPFIL),'I'), NVL(MAX(IDENDEMISFIL),'I'), MAX(CDEMPRESA)  , MAX(CDINSCESTA)
            INTO   V_IDENDCFOP          , V_IDENDEMIS          , V_CDEMPRESA, V_CDINSCESTA
            FROM FILIAL
            WHERE (CDFILIAL = V_CDFILIRECE);
         Exception
            When No_Data_Found Then
               V_IDENDCFOP := 'I'; V_IDENDEMIS := 'I'; V_CDEMPRESA := Null; V_CDINSCESTA := Null;
         End;

         IF V_IDENDEMIS = 'I' THEN
            Begin
               SELECT MAX(CDPAIS)    , MAX(SGESTADO)  , MAX(CDMUNICIPIO)
               INTO   V_PAISDEST, V_ESTADEST, V_MUNIDEST
               FROM INSCRESTAD
               WHERE CDEMPRESA  = V_CDEMPRESA AND
                     CDINSCESTA = V_CDINSCESTA;
            Exception
               When No_Data_Found Then
                  V_PAISDEST := Null; V_ESTADEST := Null; V_MUNIDEST := Null;
            End;
         ELSE
            Begin
               SELECT MAX(CDPAIS)    , MAX(SGESTADO)  , MAX(CDMUNICIPIO)
               INTO   V_PAISDEST, V_ESTADEST, V_MUNIDEST
               FROM ENDEFILI
               WHERE CDFILIAL     = V_CDFILIRECE AND
                     IDTPENDEFILI = V_IDENDEMIS;
            Exception
               When No_Data_Found Then
                  V_PAISDEST := Null; V_ESTADEST := Null; V_MUNIDEST := Null;
            End;
         END IF;
      ELSIF V_ORIGDESTOP = 'F' THEN /*DESTINATARIO -> FORNECEDOR */
         V_CDORIGDESTNF := V_CDFORNECED;
         Begin
            SELECT NVL(MAX(IDENDCFOPFOR),'P'), NVL(MAX(IDENDEMISFOR),'P')
            INTO   V_IDENDCFOP          , V_IDENDEMIS
            FROM FORNECEDOR
            WHERE (CDFORNECED = V_CDORIGDESTNF);
         Exception
            When No_Data_Found Then
               V_IDENDCFOP := 'P'; V_IDENDEMIS := 'P';
         End;

         Begin
            SELECT MAX(CDPAIS),     MAX(SGESTADO),   MAX(CDMUNICIPIO)
            INTO   V_PAISDEST, V_ESTADEST, V_MUNIDEST
            FROM ENDEFORN
            WHERE (CDFORNECED   = NVL(V_CDORIGDESTNF, P_CDFORNECED)) AND
                  (IDTPENDEFORN = V_IDENDEMIS);
         Exception
            When No_Data_Found Then
               V_PAISDEST := Null; V_ESTADEST := Null; V_MUNIDEST := Null;
         End;
      ELSE /*DESTINATARIO -> CLIENTE(DEFAULT) */
         V_CDORIGDESTNF := V_CDCLIENTE;
         Begin
            SELECT NVL(MAX(IDENDCFOPCLI),'P'), NVL(MAX(IDENDEMISCLI),'P')
            INTO   V_IDENDCFOP          , V_IDENDEMIS
            FROM CLIENTE
            WHERE (CDCLIENTE = NVL(V_CDORIGDESTNF, P_CDCLIENTE));
         Exception
            When No_Data_Found Then
               V_IDENDCFOP := 'P'; V_IDENDEMIS := 'P';
         End;

         IF V_IDENDCFOP = 'I' THEN
            Begin
               SELECT MAX(CDPAIS)    , MAX(SGESTADO), MAX(CDMUNICIPIO)
               INTO   V_PAISDEST, V_ESTADEST, V_MUNIDEST
               FROM INSCRESTAD
               WHERE CDEMPRESA  = P_EMPRESA AND
                     CDINSCESTA = P_INSCESTA;
            Exception
               When No_Data_Found Then
                  V_PAISDEST := Null; V_ESTADEST := Null; V_MUNIDEST := Null;
            End;
         ELSE

            Begin
               SELECT MAX(CDPAIS)    , MAX(SGESTADO)  , MAX(CDMUNICIPIO)
               INTO   V_PAISDEST, V_ESTADEST, V_MUNIDEST
               FROM ENDECLIE
               WHERE (CDCLIENTE    = NVL(V_CDORIGDESTNF, P_CDCLIENTE)) AND
                     (IDTPENDECLIE = V_IDENDEMIS);
            Exception
               When No_Data_Found Then
                  V_PAISDEST := Null; V_ESTADEST := Null; V_MUNIDEST := Null;
            End;
         END IF;
      END IF;

      IF (P_ENTRSAIDIN = 'S') THEN
         Begin
            SELECT IDUTILNFSE
              INTO V_IDUTILNFSE
              FROM TIPOOPERAC
             WHERE IDENTRSAIDOP = 'S' AND
                   CDTIPOOPER   = P_TIPOOPER;
         Exception
            When No_Data_Found Then
                V_IDUTILNFSE := 'N';
         End;

         /*SE UTILIZA NOTA FISCAL DE SERVICO ELETRONICA E INFORMOU PAIS, ESTADO E MUNICIPIO DE PRESTACAO DO SERVICO NO
           LANCAMENTO DA NOTA FISCAL, O SISTEMA NAO VERIFICARA O PAIS, O ESTADO E NEM O MUNICIPIO DO DESTINATARIO DA NOTA FISCAL
           E SIM DO LOCAL DE ATENDIMENTO DAQUELA PRESTACAO DE SERVICO
         */
         IF (V_IDUTILNFSE = 'S') THEN

            V_QTDREGSERV := 0;
            BEGIN
               SELECT CDPAISPRESSERV,
                      SGESTADOPRESSERV,
                      CDMUNICIPRESSERV,
                      COUNT(*)
                 INTO V_CDPAISSERV,
                      V_SGESTADOSERV,
                      V_CDMUNICIPIOSERV,
                      V_QTDREGSERV
                 FROM DADOSERVPRES
                WHERE CDFILIAL   = P_FILIAL
                  AND NRLANCTONF = P_LANCTONF
                GROUP BY CDPAISPRESSERV,
                         SGESTADOPRESSERV,
                         CDMUNICIPRESSERV;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_CDPAISSERV      := NULL;
                  V_SGESTADOSERV    := NULL;
                  V_CDMUNICIPIOSERV := NULL;
                  V_QTDREGSERV      := 0;
            END;

             IF (V_QTDREGSERV > 0) THEN
               V_PAISDEST := V_CDPAISSERV;
               V_ESTADEST := V_SGESTADOSERV;
               V_MUNIDEST := V_CDMUNICIPIOSERV;
            END IF;
         END IF;
      END IF;

      /*
      ROTINA PARA VERIFICAR AS NOTAS DO SISTEMA FACILITIES. NESTAS NOTAS O SISTEMA
      NAO VERIFICARA O PAIS, O ESTADO E NEM O MUNICIPIO DO DESTINATARIO DA NOTA FISCAL 
      E SIM DO LOCAL DE ATENDIMENTO DAQUELA PRESTACAO DE SERVICO
      */
      V_QTDREGFCT := 0;
      BEGIN
         SELECT CDPAIS, 
                SGESTADO, 
                CDMUNICIPIO,
                COUNT(*)
           INTO V_CDPAISFCT,
                V_SGESTADOFCT,
                V_CDMUNICIPIOFCT,
                V_QTDREGFCT
           FROM FCT_DADOSNF
          WHERE CDFILIAL = P_FILIAL
            AND NRLANCTONF = P_LANCTONF
          GROUP BY CDPAIS, 
                SGESTADO, 
                CDMUNICIPIO;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_CDPAISFCT := NULL;
            V_SGESTADOFCT := NULL;
            V_CDMUNICIPIOFCT := NULL;
            V_QTDREGFCT := 0;
      END;
      IF (V_QTDREGFCT > 0) THEN
         V_PAISDEST := V_CDPAISFCT;
         V_ESTADEST := V_SGESTADOFCT;
         V_MUNIDEST := V_CDMUNICIPIOFCT;
      END IF;

      /*
      QUANDO A OPERACAO DA NOTA FISCAL FOR PRESENCIAL O SISTEMA USARA COMO ENDERECO
      DE DESTINO O MESMO ENDERECO DA ORIGEM. ESTA CONDICAO FOI NECESSARIA PARA ATENDER
      A NOTA TECNICA 2015/003 e Se considera endereco de destino igual ao endereco de origem
      */
      IF (V_IDPRESENCIAL = '1') AND (V_IDOPPRESENDDIF = 'N') THEN
         V_PAISDEST := V_PAISORIG;
         V_ESTADEST := V_ESTAORIG;
         V_MUNIDEST := V_MUNIORIG;
      END IF;
   END IF;

   IF (P_ENTRSAIDIN = 'E') THEN

      Begin
         SELECT MAX(CDPAIS)    , MAX(SGESTADO)  , MAX(CDMUNICIPIO)
         INTO   V_PAISDEST, V_ESTADEST, V_MUNIDEST
         FROM INSCRESTAD
         WHERE CDEMPRESA  = P_EMPRESA AND
               CDINSCESTA = P_INSCESTA;
      Exception
         When No_Data_Found Then
            V_PAISDEST := Null; V_ESTADEST := Null; V_MUNIDEST := Null;
      End;

      Begin
         SELECT NVL(MAX(IDNFPRESTSERV), 'N'), NVL(MAX(IDUTLOCPRESTSERV), 'N')
           INTO   V_IDNFPRESTSERV, V_IDUTLOCPRESTSERV
           FROM TIPOOPERAC
          WHERE IDENTRSAIDOP = 'E'
            AND CDTIPOOPER   = P_TIPOOPER;
      Exception
         When No_Data_Found Then
            V_IDNFPRESTSERV := 'N'; V_IDUTLOCPRESTSERV := 'N';
      End;

      IF (V_IDNFPRESTSERV = 'S') THEN

         BEGIN
            SELECT NVL(MAX(IDEMISFILNFSERV),'P')
              INTO V_IDEMISFILNFSERV
              FROM FILIAL
             WHERE (CDFILIAL = V_CDFILIRECE);
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_IDEMISFILNFSERV := 'P';
         END;

         IF (V_IDEMISFILNFSERV <> 'I') THEN
            BEGIN
               SELECT MAX(CDPAIS), MAX(SGESTADO), MAX(CDMUNICIPIO)
                 INTO V_PAISDEST, V_ESTADEST, V_MUNIDEST
                 FROM ENDEFILI
                WHERE (CDFILIAL     = V_CDFILIRECE) AND
                      (IDTPENDEFILI = V_IDEMISFILNFSERV);
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_PAISDEST := NULL; V_ESTADEST := NULL; V_MUNIDEST := NULL;
            END;
         END IF;
      END IF;

      IF (V_IDNFPRESTSERV = 'S') AND (V_IDUTLOCPRESTSERV = 'S') THEN
         V_ESTADEST := NVL(P_SGESTADO, V_ESTADEST);
         V_MUNIDEST := NVL(P_CDMUNICIPIO, V_MUNIDEST);
      END IF;
    
      /*DESTINATARIO-REMETENTE DA NOTA*/
      IF V_IDORIGDESTNF IS NULL THEN
         Begin
            SELECT NVL(MAX(IDORIGDESTOP), 'F')
            INTO   V_ORIGDESTOP
            FROM TIPOOPERAC
            WHERE IDENTRSAIDOP = 'E' AND
                 CDTIPOOPER   = P_TIPOOPER;
         Exception
            When No_Data_Found Then
               V_ORIGDESTOP := Null;
         End;
      ELSE
        V_ORIGDESTOP := V_IDORIGDESTNF;
      END IF;

      IF V_ORIGDESTOP = 'L' THEN /*Remetente -> FILIAL */
         V_CDORIGDESTNF := V_CDFILIEMIT;

         Begin
            SELECT NVL(MAX(IDENDCFOPFIL),'I'), NVL(MAX(IDENDEMISFIL),'I'), MAX(CDEMPRESA)  , MAX(CDINSCESTA)
            INTO   V_IDENDCFOP          , V_IDENDEMIS          , V_CDEMPRESA, V_CDINSCESTA
            FROM FILIAL
            WHERE (CDFILIAL = V_CDFILIEMIT);
         Exception
            When No_Data_Found Then
               V_IDENDCFOP := 'I'; V_IDENDEMIS := 'I'; V_CDEMPRESA := Null; V_CDINSCESTA := Null;
         End;

         IF V_IDENDEMIS = 'I' THEN
            Begin
               SELECT MAX(CDPAIS)    , MAX(SGESTADO)  , MAX(CDMUNICIPIO)
               INTO   V_PAISORIG, V_ESTAORIG, V_MUNIORIG
               FROM INSCRESTAD
               WHERE CDEMPRESA  = V_CDEMPRESA AND
                     CDINSCESTA = V_CDINSCESTA;
            Exception
               When No_Data_Found Then
                  V_PAISORIG := Null; V_ESTAORIG := Null; V_MUNIORIG := Null;
            End;
         ELSE
            Begin
               SELECT MAX(CDPAIS)    , MAX(SGESTADO)  , MAX(CDMUNICIPIO)
               INTO   V_PAISORIG, V_ESTAORIG, V_MUNIORIG
               FROM ENDEFILI
               WHERE CDFILIAL     = V_CDFILIEMIT AND
                     IDTPENDEFILI = V_IDENDEMIS;
            Exception
               When No_Data_Found Then
                  V_PAISORIG := Null; V_ESTAORIG := Null; V_MUNIORIG := Null;
            End;
         END IF;
      ELSIF V_ORIGDESTOP = 'F' THEN /*DESTINATARIO -> FORNECEDOR */
         V_CDORIGDESTNF := V_CDFORNECED;
         Begin
            SELECT NVL(MAX(IDENDCFOPFOR),'P'), NVL(MAX(IDENDEMISFOR),'P')
            INTO   V_IDENDCFOP          , V_IDENDEMIS
            FROM FORNECEDOR
            WHERE (CDFORNECED = NVL(V_CDORIGDESTNF, P_CDFORNECED));
         Exception
            When No_Data_Found Then
               V_IDENDCFOP := 'P'; V_IDENDEMIS := 'P';
         End;

         Begin
            SELECT MAX(CDPAIS)    , MAX(SGESTADO)  , MAX(CDMUNICIPIO)
            INTO   V_PAISORIG, V_ESTAORIG, V_MUNIORIG
            FROM ENDEFORN
            WHERE (CDFORNECED   = NVL(V_CDORIGDESTNF, P_CDFORNECED)) AND
                  (IDTPENDEFORN = V_IDENDCFOP);
         Exception
            When No_Data_Found Then
               V_PAISORIG := Null; V_ESTAORIG := Null; V_MUNIORIG := Null;
         End;
      ELSE /*DESTINATARIO -> CLIENTE */
         V_CDORIGDESTNF := V_CDCLIENTE;
         Begin
            SELECT NVL(MAX(IDENDCFOPCLI),'P'), NVL(MAX(IDENDEMISCLI),'P')
            INTO   V_IDENDCFOP          , V_IDENDEMIS
            FROM CLIENTE
            WHERE (CDCLIENTE = NVL(V_CDORIGDESTNF, P_CDCLIENTE));
         Exception
            When No_Data_Found Then
               V_IDENDCFOP := 'P'; V_IDENDEMIS := 'P';
         End;

         IF V_IDENDCFOP = 'I' THEN
            Begin
               SELECT MAX(CDPAIS)    , MAX(SGESTADO), MAX(CDMUNICIPIO)
               INTO   V_PAISORIG, V_ESTAORIG, V_MUNIORIG
               FROM INSCRESTAD
               WHERE CDEMPRESA  = P_EMPRESA AND
                     CDINSCESTA = P_INSCESTA;
            Exception
               When No_Data_Found Then
                  V_PAISORIG := Null; V_ESTAORIG := Null; V_MUNIORIG := NuLL;
            End;
         ELSE

            Begin
               SELECT MAX(CDPAIS)    , MAX(SGESTADO)  , MAX(CDMUNICIPIO)
               INTO   V_PAISORIG, V_ESTAORIG, V_MUNIORIG
               FROM ENDECLIE
               WHERE (CDCLIENTE    = NVL(V_CDORIGDESTNF, P_CDCLIENTE)) AND
                     (IDTPENDECLIE = V_IDENDEMIS);
            Exception
               When No_Data_Found Then
                  V_PAISORIG := Null; V_ESTAORIG := Null; V_MUNIORIG := Null;
            End;
         END IF;
      END IF;
   END IF;
   FOR C_REGRA IN (SELECT *
                     FROM TABLE(CONSULTA_REGRA_FISCAL(null, P_EMPRESA, P_CDGRUPOFISC, P_ENTRSAIDIN, P_TIPOOPER, P_INSCESTA, P_PRODUTO, NVL(V_INIVALID, P_INIVALID),
                                                       V_PAISORIG, V_ESTAORIG, V_MUNIORIG, V_PAISDEST, V_ESTADEST, V_MUNIDEST, V_VRITEM, 'N', P_IDORIGEMPROD))
                    WHERE VRALIQUOTA IS NOT NULL) LOOP

       SELECT COUNT(*)
         INTO V_EXISTE
         FROM INCITEMP
        WHERE NRINCITEMP  = P_FILIAL||P_LANCTONF
          AND CDIMPOTEMP = C_REGRA.CDIMPOSTO
          AND CDREGRATMP IS NULL;

       IF V_EXISTE > 0 THEN
          DELETE FROM INCITEMP
          WHERE NRINCITEMP  = P_FILIAL||P_LANCTONF
            AND CDIMPOTEMP = C_REGRA.CDIMPOSTO
            AND CDREGRATMP IS NULL;
       END IF;

       SELECT IDTRATIMPO
         INTO V_IDTRATIMPO
         FROM IMPOSTO
        WHERE CDIMPOSTO = C_REGRA.CDIMPOSTO;

       IF C_REGRA.IDMENSAGEM = 'S' THEN
          IF C_REGRA.IDTIPOREGISTRO = 'PAD' THEN

             FOR C_MSGREGRA IN (SELECT *
                                  FROM MSGFISCREGRA
                                 WHERE CDREGRAFISC = C_REGRA.CDREGRAFISC
                                   AND CDIMPOSTO = C_REGRA.CDIMPOSTO
                                   AND NRSEQDADOSFISC = C_REGRA.NRSEQDADOSFISC) LOOP

                INSERT INTO INCITEMP
                  (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                   CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD, 
                   CDREGRATMP, CDENQLEGALIPI, CDCEST, VRADICALIQFECP)
                VALUES
                  (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_REGRA.CDIMPOSTO, V_IDTRATIMPO,
                   C_REGRA.CDFISCAL, C_MSGREGRA.DSMENSAGEM, NULL, C_REGRA.CDDETALHEFISC, 
                   C_REGRA.CDREGRAFISC, C_REGRA.CDENQLEGALIPI, C_REGRA.CDCEST, C_REGRA.VRADICALIQFECP);

                V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
             END LOOP;
          ELSE
             IF C_REGRA.IDTIPOREGISTRO = 'NCM' THEN

                FOR C_MSGREGRA IN (SELECT *
                                     FROM MSGFISCNCM
                                    WHERE CDREGRAFISC = C_REGRA.CDREGRAFISC
                                      AND CDIMPOSTO = C_REGRA.CDIMPOSTO
                                      AND CDCLASFISC = C_REGRA.CDCLASFISC
                                      AND NRSEQDADOSFISC = C_REGRA.NRSEQDADOSFISC) LOOP

                   INSERT INTO INCITEMP
                     (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                      CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD, 
                      CDREGRATMP, CDENQLEGALIPI, CDCEST, VRADICALIQFECP)
                   VALUES
                     (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_REGRA.CDIMPOSTO, V_IDTRATIMPO,
                      C_REGRA.CDFISCAL, C_MSGREGRA.DSMENSAGEM, NULL, C_REGRA.CDDETALHEFISC, 
                      C_REGRA.CDREGRAFISC, C_REGRA.CDENQLEGALIPI, C_REGRA.CDCEST, C_REGRA.VRADICALIQFECP);

                   V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
                END LOOP;
             ELSE
                IF C_REGRA.IDTIPOREGISTRO = 'PROD' THEN
                   FOR C_MSGREGRA IN (SELECT *
                                        FROM MSGFISCPROD
                                       WHERE CDREGRAFISC = C_REGRA.CDREGRAFISC
                                         AND CDIMPOSTO = C_REGRA.CDIMPOSTO
                                         AND NRSEQDADOSFISC = C_REGRA.NRSEQDADOSFISC
                                         AND CDPRODUTO = C_REGRA.CDPRODUTO) LOOP

                      INSERT INTO INCITEMP
                        (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                         CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD, 
                         CDREGRATMP, CDENQLEGALIPI, CDCEST, VRADICALIQFECP)
                      VALUES
                        (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_REGRA.CDIMPOSTO, V_IDTRATIMPO,
                         C_REGRA.CDFISCAL, C_MSGREGRA.DSMENSAGEM, NULL, C_REGRA.CDDETALHEFISC, 
                         C_REGRA.CDREGRAFISC, C_REGRA.CDENQLEGALIPI, C_REGRA.CDCEST, C_REGRA.VRADICALIQFECP);

                      V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
                   END LOOP;
                END IF;
             END IF;
          END IF;
       ELSE
          INSERT INTO INCITEMP
            (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
             CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD, 
             CDREGRATMP, CDENQLEGALIPI, CDCEST, VRADICALIQFECP)
          VALUES
            (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_REGRA.CDIMPOSTO, V_IDTRATIMPO,
             C_REGRA.CDFISCAL, NULL, NULL, C_REGRA.CDDETALHEFISC, 
             C_REGRA.CDREGRAFISC, C_REGRA.CDENQLEGALIPI, C_REGRA.CDCEST, C_REGRA.VRADICALIQFECP);
          
          V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
       END IF;
   END LOOP;

   /* Verificacao das incidencias referente as filiais parametrizadas na tela Parametros de Grupo Fiscal (Imposto/Filial) */
   FOR C_IMPFILI IN (SELECT G.CDIMPOSTO, G.CDGRUPOFISC
                       FROM GRUPOFISCIMPFIL G,
                            (SELECT DECODE(P_ENTRSAIDIN, 'E', CDFILIRECE, CDFILIEMIT) AS CDFILIAL
                               FROM NOTAFISCAL
                              WHERE CDFILIAL = P_FILIAL
                                AND NRLANCTONF = P_LANCTONF
                                AND P_IDSISTEMA IS NULL
                                AND P_CDFILIRECE IS NULL
                             UNION ALL
                             SELECT CDFILIAL
                               FROM PEDIDO
                              WHERE CDFILIAL = SUBSTR(P_FILIAL,1,4)
                                AND NRPEDIDO = SUBSTR(P_FILIAL,5,10)
                                AND P_IDSISTEMA = 'PED'
                              UNION ALL
                             SELECT CDFILIAL
                               FROM FILIAL
                              WHERE CDFILIAL = P_CDFILIRECE) F
                      WHERE G.CDFILIAL = F.CDFILIAL
                        AND NOT G.CDGRUPOFISC IS NULL)
   LOOP
      FOR C_REGRA IN (SELECT *
                        FROM TABLE(CONSULTA_REGRA_FISCAL(C_IMPFILI.CDIMPOSTO, P_EMPRESA, C_IMPFILI.CDGRUPOFISC, P_ENTRSAIDIN, P_TIPOOPER, P_INSCESTA, P_PRODUTO, NVL(V_INIVALID, P_INIVALID),
                                                         V_PAISORIG, V_ESTAORIG, V_MUNIORIG, V_PAISDEST, V_ESTADEST, V_MUNIDEST, V_VRITEM, 'N', P_IDORIGEMPROD))
                       WHERE VRALIQUOTA IS NOT NULL) LOOP

         SELECT COUNT(*)
           INTO V_EXISTE
           FROM INCITEMP
          WHERE NRINCITEMP  = P_FILIAL||P_LANCTONF
            AND CDIMPOTEMP = C_REGRA.CDIMPOSTO;

         IF V_EXISTE > 0 THEN
            DELETE FROM INCITEMP
            WHERE NRINCITEMP  = P_FILIAL||P_LANCTONF
              AND CDIMPOTEMP = C_REGRA.CDIMPOSTO;
         END IF;

         SELECT IDTRATIMPO
           INTO V_IDTRATIMPO
           FROM IMPOSTO
          WHERE CDIMPOSTO = C_REGRA.CDIMPOSTO;

         IF C_REGRA.IDMENSAGEM = 'S' THEN
            IF C_REGRA.IDTIPOREGISTRO = 'PAD' THEN

               FOR C_MSGREGRA IN (SELECT *
                                  FROM MSGFISCREGRA
                                 WHERE CDREGRAFISC = C_REGRA.CDREGRAFISC
                                   AND CDIMPOSTO = C_REGRA.CDIMPOSTO
                                   AND NRSEQDADOSFISC = C_REGRA.NRSEQDADOSFISC) LOOP

                  INSERT INTO INCITEMP
                    (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                     CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD, 
                     CDREGRATMP, CDENQLEGALIPI, CDCEST, VRADICALIQFECP)
                  VALUES
                    (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_REGRA.CDIMPOSTO, V_IDTRATIMPO,
                     C_REGRA.CDFISCAL, C_MSGREGRA.DSMENSAGEM, NULL, C_REGRA.CDDETALHEFISC, 
                     C_REGRA.CDREGRAFISC, C_REGRA.CDENQLEGALIPI, C_REGRA.CDCEST, C_REGRA.VRADICALIQFECP);

                  V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
               END LOOP;
            ELSE
               IF C_REGRA.IDTIPOREGISTRO = 'NCM' THEN

                  FOR C_MSGREGRA IN (SELECT *
                                     FROM MSGFISCNCM
                                    WHERE CDREGRAFISC = C_REGRA.CDREGRAFISC
                                      AND CDIMPOSTO = C_REGRA.CDIMPOSTO
                                      AND CDCLASFISC = C_REGRA.CDCLASFISC
                                      AND NRSEQDADOSFISC = C_REGRA.NRSEQDADOSFISC) LOOP

                     INSERT INTO INCITEMP
                       (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                        CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD, 
                        CDREGRATMP, CDENQLEGALIPI, CDCEST, VRADICALIQFECP)
                     VALUES
                       (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_REGRA.CDIMPOSTO, V_IDTRATIMPO,
                        C_REGRA.CDFISCAL, C_MSGREGRA.DSMENSAGEM, NULL, C_REGRA.CDDETALHEFISC, 
                        C_REGRA.CDREGRAFISC, C_REGRA.CDENQLEGALIPI, C_REGRA.CDCEST, C_REGRA.VRADICALIQFECP);

                     V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);

                  END LOOP;
               ELSE
                  IF C_REGRA.IDTIPOREGISTRO = 'PROD' THEN
                     FOR C_MSGREGRA IN (SELECT *
                                          FROM MSGFISCPROD
                                         WHERE CDREGRAFISC = C_REGRA.CDREGRAFISC
                                           AND CDIMPOSTO = C_REGRA.CDIMPOSTO
                                           AND NRSEQDADOSFISC = C_REGRA.NRSEQDADOSFISC
                                           AND CDPRODUTO = C_REGRA.CDPRODUTO) LOOP
                        INSERT INTO INCITEMP
                          (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                           CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD, 
                           CDREGRATMP, CDENQLEGALIPI, CDCEST, VRADICALIQFECP)
                        VALUES
                          (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_REGRA.CDIMPOSTO, V_IDTRATIMPO,
                           C_REGRA.CDFISCAL, C_MSGREGRA.DSMENSAGEM, NULL, C_REGRA.CDDETALHEFISC, 
                           C_REGRA.CDREGRAFISC, C_REGRA.CDENQLEGALIPI, C_REGRA.CDCEST, C_REGRA.VRADICALIQFECP);

                        V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
                     END LOOP;
                  END IF;
               END IF;
            END IF;
         ELSE

            INSERT INTO INCITEMP
              (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
               CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD, 
               CDREGRATMP, CDENQLEGALIPI, CDCEST, VRADICALIQFECP)
            VALUES
              (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_REGRA.CDIMPOSTO, V_IDTRATIMPO,
               C_REGRA.CDFISCAL, NULL, NULL, C_REGRA.CDDETALHEFISC, 
               C_REGRA.CDREGRAFISC, C_REGRA.CDENQLEGALIPI, C_REGRA.CDCEST, C_REGRA.VRADICALIQFECP);

            V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
         END IF;
      END LOOP;
    END LOOP;

    /* Verificacao das incidencias referente as filiais parametrizadas na tela Parametros de Grupo Fiscal (Imposto/Inscricao Estadual) */
    /* Por Fornecedor */
    FOR C_IMPINSCESTAFO IN (SELECT G.CDIMPOSTO, G.CDGRUPOFISC
                              FROM GRFISIMPIEFORN G,
                                   (SELECT CDFORNECED
                                      FROM NOTAFISCAL
                                     WHERE CDFILIAL = P_FILIAL
                                       AND NRLANCTONF = P_LANCTONF
                                       AND P_CDFORNECED IS NULL
                                     UNION ALL
                                    SELECT CDFORNECED
                                      FROM FORNECEDOR
                                     WHERE CDFORNECED = P_CDFORNECED) N
                             WHERE G.CDFORNECED = N.CDFORNECED
                               AND G.CDEMPRESA = P_EMPRESA
                           AND G.CDINSCESTA = P_INSCESTA
                           AND NOT G.CDGRUPOFISC IS NULL) LOOP
       FOR C_REGRA IN (SELECT *
                         FROM TABLE(CONSULTA_REGRA_FISCAL(C_IMPINSCESTAFO.CDIMPOSTO, P_EMPRESA, C_IMPINSCESTAFO.CDGRUPOFISC, P_ENTRSAIDIN, P_TIPOOPER, P_INSCESTA, P_PRODUTO, NVL(V_INIVALID, P_INIVALID),
                                                         V_PAISORIG, V_ESTAORIG, V_MUNIORIG, V_PAISDEST, V_ESTADEST, V_MUNIDEST, V_VRITEM, 'N', P_IDORIGEMPROD))
                        WHERE VRALIQUOTA IS NOT NULL) LOOP

         SELECT COUNT(*)
           INTO V_EXISTE
           FROM INCITEMP
          WHERE NRINCITEMP  = P_FILIAL||P_LANCTONF
            AND CDIMPOTEMP = C_REGRA.CDIMPOSTO;

         IF V_EXISTE > 0 THEN
            DELETE FROM INCITEMP
            WHERE NRINCITEMP  = P_FILIAL||P_LANCTONF
              AND CDIMPOTEMP = C_REGRA.CDIMPOSTO;
         END IF;

         SELECT IDTRATIMPO
           INTO V_IDTRATIMPO
           FROM IMPOSTO
          WHERE CDIMPOSTO = C_REGRA.CDIMPOSTO;

         IF C_REGRA.IDMENSAGEM = 'S' THEN
            IF C_REGRA.IDTIPOREGISTRO = 'PAD' THEN

               FOR C_MSGREGRA IN (SELECT *
                                  FROM MSGFISCREGRA
                                 WHERE CDREGRAFISC = C_REGRA.CDREGRAFISC
                                   AND CDIMPOSTO = C_REGRA.CDIMPOSTO
                                   AND NRSEQDADOSFISC = C_REGRA.NRSEQDADOSFISC) LOOP

                  INSERT INTO INCITEMP
                    (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                     CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD, 
                     CDREGRATMP, CDENQLEGALIPI, CDCEST, VRADICALIQFECP)
                  VALUES
                    (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_REGRA.CDIMPOSTO, V_IDTRATIMPO,
                     C_REGRA.CDFISCAL, C_MSGREGRA.DSMENSAGEM, NULL, C_REGRA.CDDETALHEFISC, 
                     C_REGRA.CDREGRAFISC, C_REGRA.CDENQLEGALIPI, C_REGRA.CDCEST, C_REGRA.VRADICALIQFECP);

                  V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
               END LOOP;
            ELSE
               IF C_REGRA.IDTIPOREGISTRO = 'NCM' THEN

                  FOR C_MSGREGRA IN (SELECT *
                                       FROM MSGFISCNCM
                                      WHERE CDREGRAFISC = C_REGRA.CDREGRAFISC
                                        AND CDIMPOSTO = C_REGRA.CDIMPOSTO
                                        AND CDCLASFISC = C_REGRA.CDCLASFISC
                                        AND NRSEQDADOSFISC = C_REGRA.NRSEQDADOSFISC) LOOP

                     INSERT INTO INCITEMP
                       (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                        CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD, 
                        CDREGRATMP, CDENQLEGALIPI, CDCEST, VRADICALIQFECP)
                     VALUES
                       (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_REGRA.CDIMPOSTO, V_IDTRATIMPO,
                        C_REGRA.CDFISCAL, C_MSGREGRA.DSMENSAGEM, NULL, C_REGRA.CDDETALHEFISC, 
                        C_REGRA.CDREGRAFISC, C_REGRA.CDENQLEGALIPI, C_REGRA.CDCEST, C_REGRA.VRADICALIQFECP);

                     V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
                  END LOOP;
               ELSE
                  IF C_REGRA.IDTIPOREGISTRO = 'PROD' THEN
                     FOR C_MSGREGRA IN (SELECT *
                                          FROM MSGFISCPROD
                                         WHERE CDREGRAFISC = C_REGRA.CDREGRAFISC
                                           AND CDIMPOSTO = C_REGRA.CDIMPOSTO
                                           AND NRSEQDADOSFISC = C_REGRA.NRSEQDADOSFISC
                                           AND CDPRODUTO = C_REGRA.CDPRODUTO) LOOP
                         INSERT INTO INCITEMP
                            (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                             CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD, 
                             CDREGRATMP, CDENQLEGALIPI, CDCEST, VRADICALIQFECP)
                         VALUES
                            (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_REGRA.CDIMPOSTO, V_IDTRATIMPO,
                             C_REGRA.CDFISCAL, C_MSGREGRA.DSMENSAGEM, NULL, C_REGRA.CDDETALHEFISC, 
                             C_REGRA.CDREGRAFISC, C_REGRA.CDENQLEGALIPI, C_REGRA.CDCEST, C_REGRA.VRADICALIQFECP);

                         V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
                     END LOOP;
                  END IF;
               END IF;
            END IF;
         ELSE
            INSERT INTO INCITEMP
              (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
               CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD, 
               CDREGRATMP, CDENQLEGALIPI, CDCEST, VRADICALIQFECP)
            VALUES
              (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_REGRA.CDIMPOSTO, V_IDTRATIMPO,
               C_REGRA.CDFISCAL, NULL, NULL, C_REGRA.CDDETALHEFISC, 
               C_REGRA.CDREGRAFISC, C_REGRA.CDENQLEGALIPI, C_REGRA.CDCEST, C_REGRA.VRADICALIQFECP);

            V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
         END IF;
      END LOOP;
   END LOOP;
   /* Verificacao das incidencias referente as filiais parametrizadas na tela Parametros de Grupo Fiscal (Imposto/Inscricao Estadual) */
   /* Por Cliente */
   FOR C_IMPINSCESTACL IN (SELECT G.CDIMPOSTO, G.CDGRUPOFISC
                           FROM GRFISIMPIECLIE G,
                                (SELECT CDCLIENTE
                                   FROM NOTAFISCAL
                                  WHERE CDFILIAL = P_FILIAL
                                    AND NRLANCTONF = P_LANCTONF
                                    AND P_CDCLIENTE IS NULL
                                  UNION ALL
                                 SELECT CDCLIENTE
                                   FROM CLIENTE
                                  WHERE CDCLIENTE = P_CDCLIENTE) N
                          WHERE G.CDCLIENTE = N.CDCLIENTE
                            AND G.CDEMPRESA = P_EMPRESA
                        AND G.CDINSCESTA = P_INSCESTA
                        AND NOT G.CDGRUPOFISC IS NULL)
   LOOP
      FOR C_REGRA IN (SELECT *
                        FROM TABLE(CONSULTA_REGRA_FISCAL(C_IMPINSCESTACL.CDIMPOSTO, P_EMPRESA, C_IMPINSCESTACL.CDGRUPOFISC, P_ENTRSAIDIN, P_TIPOOPER, P_INSCESTA, P_PRODUTO, NVL(V_INIVALID, P_INIVALID),
                                                         V_PAISORIG, V_ESTAORIG, V_MUNIORIG, V_PAISDEST, V_ESTADEST, V_MUNIDEST, V_VRITEM, 'N', P_IDORIGEMPROD))
                       WHERE VRALIQUOTA IS NOT NULL) LOOP

         SELECT COUNT(*)
           INTO V_EXISTE
           FROM INCITEMP
          WHERE NRINCITEMP  = P_FILIAL||P_LANCTONF
            AND CDIMPOTEMP = C_REGRA.CDIMPOSTO;

         IF V_EXISTE > 0 THEN
            DELETE FROM INCITEMP
            WHERE NRINCITEMP  = P_FILIAL||P_LANCTONF
              AND CDIMPOTEMP = C_REGRA.CDIMPOSTO;
         END IF;

         SELECT MAX(IDTRATIMPO)
           INTO V_IDTRATIMPO
           FROM IMPOSTO
          WHERE CDIMPOSTO = C_REGRA.CDIMPOSTO;

         IF C_REGRA.IDMENSAGEM = 'S' THEN
            IF C_REGRA.IDTIPOREGISTRO = 'PAD' THEN

               FOR C_MSGREGRA IN (SELECT *
                                  FROM MSGFISCREGRA
                                 WHERE CDREGRAFISC = C_REGRA.CDREGRAFISC
                                   AND CDIMPOSTO = C_REGRA.CDIMPOSTO
                                   AND NRSEQDADOSFISC = C_REGRA.NRSEQDADOSFISC) LOOP

                   INSERT INTO INCITEMP
                     (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                      CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD, 
                      CDREGRATMP, CDENQLEGALIPI, CDCEST, VRADICALIQFECP)
                   VALUES
                     (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_REGRA.CDIMPOSTO, V_IDTRATIMPO,
                      C_REGRA.CDFISCAL, C_MSGREGRA.DSMENSAGEM, NULL, C_REGRA.CDDETALHEFISC, 
                      C_REGRA.CDREGRAFISC, C_REGRA.CDENQLEGALIPI, C_REGRA.CDCEST, C_REGRA.VRADICALIQFECP);

                   V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
               END LOOP;
            ELSE
               IF C_REGRA.IDTIPOREGISTRO = 'NCM' THEN

                  FOR C_MSGREGRA IN (SELECT *
                                       FROM MSGFISCNCM
                                      WHERE CDREGRAFISC = C_REGRA.CDREGRAFISC
                                        AND CDIMPOSTO = C_REGRA.CDIMPOSTO
                                        AND CDCLASFISC = C_REGRA.CDCLASFISC
                                        AND NRSEQDADOSFISC = C_REGRA.NRSEQDADOSFISC) LOOP
                     INSERT INTO INCITEMP
                        (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                         CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD, 
                         CDREGRATMP, CDENQLEGALIPI, CDCEST, VRADICALIQFECP)
                      VALUES
                        (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_REGRA.CDIMPOSTO, V_IDTRATIMPO,
                         C_REGRA.CDFISCAL, C_MSGREGRA.DSMENSAGEM, NULL, C_REGRA.CDDETALHEFISC, 
                         C_REGRA.CDREGRAFISC, C_REGRA.CDENQLEGALIPI, C_REGRA.CDCEST, C_REGRA.VRADICALIQFECP);

                      V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
                  END LOOP;
               ELSE
                  IF C_REGRA.IDTIPOREGISTRO = 'PROD' THEN
                     FOR C_MSGREGRA IN (SELECT *
                                          FROM MSGFISCPROD
                                         WHERE CDREGRAFISC = C_REGRA.CDREGRAFISC
                                           AND CDIMPOSTO = C_REGRA.CDIMPOSTO
                                           AND NRSEQDADOSFISC = C_REGRA.NRSEQDADOSFISC
                                           AND CDPRODUTO = C_REGRA.CDPRODUTO) LOOP
                         INSERT INTO INCITEMP
                           (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
                            CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD, 
                            CDREGRATMP, CDENQLEGALIPI, CDCEST, VRADICALIQFECP)
                         VALUES
                           (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_REGRA.CDIMPOSTO, V_IDTRATIMPO,
                            C_REGRA.CDFISCAL, C_MSGREGRA.DSMENSAGEM, NULL, C_REGRA.CDDETALHEFISC, 
                            C_REGRA.CDREGRAFISC, C_REGRA.CDENQLEGALIPI, C_REGRA.CDCEST, C_REGRA.VRADICALIQFECP);

                         V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
                     END LOOP;
                  END IF;
               END IF;
            END IF;
         ELSE

            INSERT INTO INCITEMP
              (NRINCITEMP, NRSEQUINCI, CDIMPOTEMP, IDTRATIMPTMP,
               CDFISCTEMP, DSMENSINCTMP, CDINSCESTAITMP, CDDETALHEPROD, 
               CDREGRATMP, CDENQLEGALIPI, CDCEST, VRADICALIQFECP)
            VALUES
              (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_REGRA.CDIMPOSTO, V_IDTRATIMPO,
               C_REGRA.CDFISCAL, NULL, NULL, C_REGRA.CDDETALHEFISC, 
               C_REGRA.CDREGRAFISC, C_REGRA.CDENQLEGALIPI, C_REGRA.CDCEST, C_REGRA.VRADICALIQFECP);

            V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
         END IF;
      END LOOP;
   END LOOP;
END;
/