CREATE OR REPLACE PROCEDURE CONSULTA_ALIQUOTA(
    P_IMPOSTO     IN ALIQUOTA.CDIMPOSTO%TYPE,
    P_ENTRSAIDAL  IN ALIQUOTA.IDENTRSAIDAL%TYPE,
    P_EMPRESA     IN ALIQUOTA.CDEMPRESA%TYPE,
    P_PRODUTO     IN PRODUTO.CDARVPROD%TYPE,
    P_INSCESTA    IN ALIQUOTA.CDINSCESTA%TYPE,
    P_PAISORIG    IN PAIS.CDPAIS%TYPE,
    P_ESTAORIG    IN ESTADO.SGESTADO%TYPE,
    P_PAISDEST    IN PAIS.CDPAIS%TYPE,
    P_ESTADEST    IN ESTADO.SGESTADO%TYPE,
    P_MUNIORIG    IN MUNICIPIO.CDMUNICIPIO%TYPE,
    P_BASECALC    IN ALIQUOTA.VRFIMFAIXA%TYPE,
    P_INIVALID    IN ALIQUOTA.DTINIVALID%TYPE,
    P_FILIAL      IN VARCHAR2,
    P_LANCTONF    IN VARCHAR2,
    P_CDGRUPOFISC IN ALIQUOTA.CDGRUPOFISC%TYPE,
    P_CDTIPOOPER  IN TIPOOPERAC.CDTIPOOPER%TYPE DEFAULT NULL,
    P_IDSISTEMA   IN VARCHAR2 DEFAULT NULL,
    P_LANCPRODUTO IN VARCHAR2 DEFAULT NULL,  --Exclusivo Unificacao
    P_VRPRODUTO   IN NUMBER   DEFAULT NULL,  --Exclusivo Unificacao
    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_IDORIGEMPROD IN VARCHAR2 DEFAULT 'N')
IS
    V_SEQUENCIA   VARCHAR2(02); /*Variavel para gerar o sequencial da aliquota*/
    V_CDARVPROD   VARCHAR2(20); /*Codigo de arvore do produto(Aliquota por nivel ou todos)*/

    V_UTUFORIG       CHAR(1);   /*Utiliza UF de origem*/
    V_UTUFDEST       CHAR(1);   /*Utiliza UF de destino*/
    V_UTMUNIORIG     CHAR(1);   /*Utiliza municipio (origem ou destino)*/
    V_UTFAIXVLR      CHAR(1);   /*Utiliza faixa de valores*/
    V_IDUTALIQTPOPER CHAR(1);   /*Utiliza aliquota por tipo de operacao*/
    V_IDPRODNOTA  VARCHAR2(01); /*Visualizacao do imposto: C-No corpo da nota, P-No produto*/

    V_VRITEMPROD  FLOAT;       /*Valor liquido do item*/

    V_CONTADOR    NUMBER;      /*Variavel para o total de aliquotas encontradas*/

    w_N           Char(1):= 'N';
    w_S           Char(1):= 'S';
    w_P           Char(1):= 'P';
    w_O           Char(1):= 'O';
    w_D           Char(1):= 'D';

    V_CDGRUPOFISC VARCHAR2(03); /*Codigo do grupo fiscal*/
    V_CDTIPOOPER  VARCHAR2(02); /*Codigo do tipo de operacao*/
    V_EXISTE      INTEGER;
    V_COUNTMENS     INTEGER;
    V_DSMENSALIQU VARCHAR2(120);
    V_COUNT        INTEGER;
    V_MUNIORIG    VARCHAR2(04);
    V_MUNIDEST    VARCHAR2(04);
    V_ALIQSIMPLES   INTEGER;
    V_VRALIQSIMPLES NUMBER;
BEGIN
   /*Parametros do Imposto */
   Begin
      SELECT NVL(MAX(IDUTUFORIG), w_N), NVL(MAX(IDUTUFDEST), w_N), NVL(MAX(IDUTMUNIORIG), w_N),
             NVL(MAX(IDUTFAIXVLR), w_N), MAX(IDPRODNOTA), NVL(MAX(IDUTALIQTPOPER), w_N)
      INTO V_UTUFORIG, V_UTUFDEST, V_UTMUNIORIG, V_UTFAIXVLR, V_IDPRODNOTA,
           V_IDUTALIQTPOPER
      FROM IMPOSTO
      WHERE (CDIMPOSTO = P_IMPOSTO);

   Exception
      When No_Data_Found Then
         V_UTUFORIG   := w_N;  --Utiliza UF de origem
         V_UTUFDEST   := w_N;  --Utiliza UF de destino
         V_UTMUNIORIG := w_N;  --Utiliza municipio (Origem ou destino)
         V_UTFAIXVLR  := w_N;  --Utiliza faixa de valores
         V_IDPRODNOTA := Null; --Visualizacao do imposto (C-Corpo da nota; P-Produto)
         V_IDUTALIQTPOPER := Null; --Utiliza aliquota por tipo de operacao
   End;

   IF V_UTMUNIORIG = W_O THEN
      V_MUNIORIG := P_MUNIORIG;
      V_MUNIDEST := NULL;
   END IF;
   IF V_UTMUNIORIG = W_D THEN
      V_MUNIORIG := NULL;
      V_MUNIDEST := P_MUNIORIG;
   END IF;
   IF V_UTMUNIORIG = W_N THEN
      V_MUNIORIG := NULL;
      V_MUNIDEST := NULL;
   END IF;

   IF (P_LANCTONF <> '0000000000') THEN /*Se a consulta_aliquota for chamada a partir de um lancamento*/
      IF (V_UTFAIXVLR = w_S) AND (V_IDPRODNOTA = w_P) AND (P_ENTRSAIDAL = w_S) THEN
         IF P_IDSISTEMA = 'PED' THEN
            SELECT SUM((VRPREUNITPED * QTITEMPED) - ((VRPREUNITPED * QTITEMPED) * (VRDESITPED/100)))
              INTO V_VRITEMPROD
              FROM ITEMPEDIDO
             WHERE CDFILIAL = SUBSTR(P_FILIAL,1,4) AND
                   NRPEDIDO = SUBSTR(P_FILIAL,5,10);
         ELSE
            SELECT SUM(VRTOTAITEM - VRDESCITEM)
            INTO V_VRITEMPROD
            FROM ITEMPRODUTO
            WHERE CDFILIAL   = P_FILIAL AND
                  NRLANCTONF = P_LANCTONF;
         END IF;
      ELSE
         IF(P_LANCPRODUTO = 'S') THEN
            V_VRITEMPROD := P_VRPRODUTO;
         ELSE
            V_VRITEMPROD := P_BASECALC;
         END IF;
      END IF;
   ELSE /*Se a Consulta_aliquota nao for chamada a partir de um lancamento*/
      IF(P_LANCPRODUTO = 'S') THEN
         V_VRITEMPROD := P_VRPRODUTO;
      ELSE
         V_VRITEMPROD := P_BASECALC;
      END IF;
   END IF;

   V_SEQUENCIA := '01';
   V_CDGRUPOFISC := NULL;

   /* Verificacao das aliquotas referente as filiais parametrizadas na tela Parametros de Grupo Fiscal (Imposto/Inscricao Estadual) */
      /*Por Fornecedor*/
   V_CDGRUPOFISC := NULL;

   BEGIN
      SELECT MAX(G.CDGRUPOFISC)
        INTO V_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) F
       WHERE G.CDIMPOSTO = P_IMPOSTO
         AND G.CDFORNECED = F.CDFORNECED
         AND G.CDEMPRESA = P_EMPRESA
         AND G.CDINSCESTA = P_INSCESTA;
   EXCEPTION
      WHEN No_Data_Found THEN
         V_CDGRUPOFISC := NULL;
   END;

   IF NOT (V_CDGRUPOFISC IS NULL) THEN /*Se ha grupo fiscal na tela Parametros de Grupo Fiscal na aba Imposto/Inscricao Estadual*/

      FOR C_REGRA IN (SELECT *
                     FROM TABLE(CONSULTA_REGRA_FISCAL(P_IMPOSTO, P_EMPRESA, V_CDGRUPOFISC, P_ENTRSAIDAL, P_CDTIPOOPER, P_INSCESTA, P_PRODUTO, P_INIVALID,
                                                      P_PAISORIG, P_ESTAORIG, V_MUNIORIG, P_PAISDEST, P_ESTADEST, V_MUNIDEST, P_BASECALC, 'N', P_IDORIGEMPROD))
                       WHERE VRALIQUOTA IS NOT NULL) LOOP

          SELECT COUNT(*)
            INTO V_EXISTE
            FROM ALIQTEMP
           WHERE NRALIQTEMP  = P_FILIAL||P_LANCTONF;

          IF V_EXISTE > 0 THEN
             DELETE FROM ALIQTEMP
             WHERE NRALIQTEMP  = P_FILIAL||P_LANCTONF;
          END IF;

          V_ALIQSIMPLES := 0;

          BEGIN
             SELECT VRALIQUOTA, COUNT(*)
               INTO V_VRALIQSIMPLES, V_ALIQSIMPLES
               FROM ALIQSIMPNAC
              WHERE CDFORNECED  = P_CDFORNECED
                AND CDREGRAFISC = C_REGRA.CDREGRAFISC
                AND CDIMPOSTO   = P_IMPOSTO
              GROUP BY VRALIQUOTA;
          EXCEPTION
             WHEN NO_DATA_FOUND THEN
                V_VRALIQSIMPLES := NULL; V_ALIQSIMPLES := 0;
          END;

          IF (V_ALIQSIMPLES > 0) THEN
             INSERT INTO ALIQTEMP
                (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP, VRALIQUTMP, DSMENSALITMP,
                 IDTRIMPSTTMP, VRPBCSTTMP, IDPMCBSCALTMP, CDINSCESTAATMP, VR1PREDBCTMP, CDREGRATMP,
                 CDTIPOOPERTMP, CDFISCOPER, IDDIFEALIQ, IDDIFALIQCUSPR, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, 
                 CDBENEFFISCAL, IDCONSMVAPMC, IDCONSCOMPBCPMC, IDCONSDIFALIQ)
             VALUES
                (P_FILIAL||P_LANCTONF, V_SEQUENCIA, 0, 0, V_VRALIQSIMPLES, NULL,
                 C_REGRA.IDTRATIMPST, C_REGRA.VRPBCSUBTRIB, C_REGRA.IDPMCBASECA,NULL, 0, C_REGRA.CDREGRAFISC,
                 P_CDTIPOOPER, C_REGRA.CDFISCOPER, 'N', 'N', C_REGRA.VRADICALIQFECP, C_REGRA.IDIMPODESONERA, C_REGRA.IDCONSDESONVRNF, 
                 C_REGRA.CDBENEFFISCAL, C_REGRA.IDCONSMVAPMC, C_REGRA.IDCONSCOMPBCPMC, C_REGRA.IDCONSDIFALIQ);
          ELSE
             INSERT INTO ALIQTEMP
                (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP, VRALIQUTMP, DSMENSALITMP,
                 IDTRIMPSTTMP, VRPBCSTTMP, IDPMCBSCALTMP, CDINSCESTAATMP, VR1PREDBCTMP, CDREGRATMP,
                 CDTIPOOPERTMP, CDFISCOPER, IDDIFEALIQ, IDDIFALIQCUSPR, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF,
                 CDBENEFFISCAL, IDCONSMVAPMC, IDCONSCOMPBCPMC, IDCONSDIFALIQ)
             VALUES
                (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_REGRA.VRPERREDBC, C_REGRA.VRPARDEDUZ, C_REGRA.VRALIQUOTA, NULL,
                 C_REGRA.IDTRATIMPST, C_REGRA.VRPBCSUBTRIB, C_REGRA.IDPMCBASECA,NULL, C_REGRA.VR1PERREDBC, C_REGRA.CDREGRAFISC,
                 P_CDTIPOOPER, C_REGRA.CDFISCOPER, C_REGRA.IDDIFEALIQ, C_REGRA.IDDIFALIQCUSPR, C_REGRA.VRADICALIQFECP, C_REGRA.IDIMPODESONERA, C_REGRA.IDCONSDESONVRNF, 
                 C_REGRA.CDBENEFFISCAL, C_REGRA.IDCONSMVAPMC, C_REGRA.IDCONSCOMPBCPMC, C_REGRA.IDCONSDIFALIQ);
          END IF;

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

       /*Por Cliente*/  

      BEGIN
        SELECT MAX(G.CDGRUPOFISC)
          INTO V_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) F
         WHERE G.CDIMPOSTO = P_IMPOSTO
           AND G.CDCLIENTE = F.CDCLIENTE
           AND G.CDEMPRESA = P_EMPRESA
           AND G.CDINSCESTA = P_INSCESTA;
      EXCEPTION
        WHEN No_Data_Found THEN
           V_CDGRUPOFISC := NULL;
      END;

      IF NOT (V_CDGRUPOFISC IS NULL) THEN /*Se ha grupo fiscal na tela Parametros de Grupo Fiscal na aba Imposto/Inscricao Estadual*/
         FOR C_REGRA IN (SELECT *
                        FROM TABLE(CONSULTA_REGRA_FISCAL(P_IMPOSTO, P_EMPRESA, V_CDGRUPOFISC, P_ENTRSAIDAL, P_CDTIPOOPER, P_INSCESTA, P_PRODUTO, P_INIVALID,
                                                         P_PAISORIG, P_ESTAORIG, V_MUNIORIG, P_PAISDEST, P_ESTADEST, V_MUNIDEST, P_BASECALC, 'N', P_IDORIGEMPROD))
                          WHERE VRALIQUOTA IS NOT NULL) LOOP
             SELECT COUNT(*)
               INTO V_EXISTE
               FROM ALIQTEMP
              WHERE NRALIQTEMP  = P_FILIAL||P_LANCTONF;

             IF V_EXISTE > 0 THEN
                DELETE FROM ALIQTEMP
                WHERE NRALIQTEMP  = P_FILIAL||P_LANCTONF;
             END IF;

             V_ALIQSIMPLES := 0;

             BEGIN
                SELECT VRALIQUOTA, COUNT(*)
                  INTO V_VRALIQSIMPLES, V_ALIQSIMPLES
                  FROM ALIQSIMPNAC
                 WHERE CDFORNECED  = P_CDFORNECED
                   AND CDREGRAFISC = C_REGRA.CDREGRAFISC
                   AND CDIMPOSTO   = P_IMPOSTO
                 GROUP BY VRALIQUOTA;
             EXCEPTION
                WHEN NO_DATA_FOUND THEN
                   V_VRALIQSIMPLES := NULL; V_ALIQSIMPLES := 0;
             END;
             
             IF (V_ALIQSIMPLES > 0) THEN
                INSERT INTO ALIQTEMP
                   (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP, VRALIQUTMP, DSMENSALITMP,
                    IDTRIMPSTTMP, VRPBCSTTMP, IDPMCBSCALTMP, CDINSCESTAATMP, VR1PREDBCTMP, CDREGRATMP,
                    CDTIPOOPERTMP, CDFISCOPER, IDDIFEALIQ, IDDIFALIQCUSPR, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF,
                    CDBENEFFISCAL, IDCONSMVAPMC, IDCONSCOMPBCPMC, IDCONSDIFALIQ)
                VALUES
                   (P_FILIAL||P_LANCTONF, V_SEQUENCIA, 0, 0, V_VRALIQSIMPLES, NULL,
                    C_REGRA.IDTRATIMPST, C_REGRA.VRPBCSUBTRIB, C_REGRA.IDPMCBASECA,NULL, 0, C_REGRA.CDREGRAFISC,
                    P_CDTIPOOPER, C_REGRA.CDFISCOPER, 'N', 'N', C_REGRA.VRADICALIQFECP, C_REGRA.IDIMPODESONERA, C_REGRA.IDCONSDESONVRNF, 
                    C_REGRA.CDBENEFFISCAL, C_REGRA.IDCONSMVAPMC, C_REGRA.IDCONSCOMPBCPMC, C_REGRA.IDCONSDIFALIQ);

             ELSE
                INSERT INTO ALIQTEMP
                   (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP, VRALIQUTMP, DSMENSALITMP,
                    IDTRIMPSTTMP, VRPBCSTTMP, IDPMCBSCALTMP, CDINSCESTAATMP, VR1PREDBCTMP, CDREGRATMP,
                    CDTIPOOPERTMP, CDFISCOPER, IDDIFEALIQ, IDDIFALIQCUSPR, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, 
                    CDBENEFFISCAL, IDCONSMVAPMC, IDCONSCOMPBCPMC, IDCONSDIFALIQ)
                VALUES
                   (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_REGRA.VRPERREDBC, C_REGRA.VRPARDEDUZ, C_REGRA.VRALIQUOTA, NULL,
                    C_REGRA.IDTRATIMPST, C_REGRA.VRPBCSUBTRIB, C_REGRA.IDPMCBASECA,NULL, C_REGRA.VR1PERREDBC, C_REGRA.CDREGRAFISC,
                    P_CDTIPOOPER, C_REGRA.CDFISCOPER, C_REGRA.IDDIFEALIQ, C_REGRA.IDDIFALIQCUSPR, C_REGRA.VRADICALIQFECP, C_REGRA.IDIMPODESONERA, C_REGRA.IDCONSDESONVRNF, 
                    C_REGRA.CDBENEFFISCAL, C_REGRA.IDCONSMVAPMC, C_REGRA.IDCONSCOMPBCPMC, C_REGRA.IDCONSDIFALIQ);
             END IF;

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

         BEGIN
            SELECT MAX(G.CDGRUPOFISC)
              INTO V_CDGRUPOFISC
              FROM GRUPOFISCIMPFIL G, (SELECT DECODE(P_ENTRSAIDAL, '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.CDIMPOSTO = P_IMPOSTO
               AND G.CDFILIAL = F.CDFILIAL;
         EXCEPTION
            WHEN No_Data_Found THEN
               V_CDGRUPOFISC := NULL;
         END;

         IF NOT (V_CDGRUPOFISC IS NULL) THEN /*Se ha grupo fiscal na tela Parametros de Grupo Fiscal na aba Imposto/Filial*/
            FOR C_REGRA IN (SELECT *
                           FROM TABLE(CONSULTA_REGRA_FISCAL(P_IMPOSTO, P_EMPRESA, V_CDGRUPOFISC, P_ENTRSAIDAL, P_CDTIPOOPER, P_INSCESTA, P_PRODUTO, P_INIVALID,
                                                            P_PAISORIG, P_ESTAORIG, V_MUNIORIG, P_PAISDEST, P_ESTADEST, V_MUNIDEST, P_BASECALC, 'N', P_IDORIGEMPROD))
                             WHERE VRALIQUOTA IS NOT NULL) LOOP
                SELECT COUNT(*)
                  INTO V_EXISTE
                  FROM ALIQTEMP
                 WHERE NRALIQTEMP  = P_FILIAL||P_LANCTONF;

                IF V_EXISTE > 0 THEN
                   DELETE FROM ALIQTEMP
                   WHERE NRALIQTEMP  = P_FILIAL||P_LANCTONF;
                END IF;

                V_ALIQSIMPLES := 0;

                BEGIN
                   SELECT VRALIQUOTA, COUNT(*)
                     INTO V_VRALIQSIMPLES, V_ALIQSIMPLES
                     FROM ALIQSIMPNAC
                    WHERE CDFORNECED  = P_CDFORNECED
                      AND CDREGRAFISC = C_REGRA.CDREGRAFISC
                      AND CDIMPOSTO   = P_IMPOSTO
                    GROUP BY VRALIQUOTA;
                EXCEPTION
                   WHEN NO_DATA_FOUND THEN
                      V_VRALIQSIMPLES := NULL; V_ALIQSIMPLES := 0;
                END;
 
                IF (V_ALIQSIMPLES > 0) THEN
                   INSERT INTO ALIQTEMP
                      (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP, VRALIQUTMP, DSMENSALITMP,
                       IDTRIMPSTTMP, VRPBCSTTMP, IDPMCBSCALTMP, CDINSCESTAATMP, VR1PREDBCTMP, CDREGRATMP,
                       CDTIPOOPERTMP, CDFISCOPER, IDDIFEALIQ, IDDIFALIQCUSPR, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, 
                       CDBENEFFISCAL, IDCONSMVAPMC, IDCONSCOMPBCPMC, IDCONSDIFALIQ)
                   VALUES
                      (P_FILIAL||P_LANCTONF, V_SEQUENCIA, 0, 0, V_VRALIQSIMPLES, NULL,
                       C_REGRA.IDTRATIMPST, C_REGRA.VRPBCSUBTRIB, C_REGRA.IDPMCBASECA,NULL, 0, C_REGRA.CDREGRAFISC,
                       P_CDTIPOOPER, C_REGRA.CDFISCOPER, 'N', 'N', C_REGRA.VRADICALIQFECP, C_REGRA.IDIMPODESONERA, C_REGRA.IDCONSDESONVRNF, 
                       C_REGRA.CDBENEFFISCAL, C_REGRA.IDCONSMVAPMC, C_REGRA.IDCONSCOMPBCPMC, C_REGRA.IDCONSDIFALIQ);
                ELSE
                   INSERT INTO ALIQTEMP
                      (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP, VRALIQUTMP, DSMENSALITMP,
                       IDTRIMPSTTMP, VRPBCSTTMP, IDPMCBSCALTMP, CDINSCESTAATMP, VR1PREDBCTMP, CDREGRATMP,
                       CDTIPOOPERTMP, CDFISCOPER, IDDIFEALIQ, IDDIFALIQCUSPR, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, 
                       CDBENEFFISCAL, IDCONSMVAPMC, IDCONSCOMPBCPMC, IDCONSDIFALIQ)
                   VALUES
                      (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_REGRA.VRPERREDBC, C_REGRA.VRPARDEDUZ, C_REGRA.VRALIQUOTA, NULL,
                       C_REGRA.IDTRATIMPST, C_REGRA.VRPBCSUBTRIB, C_REGRA.IDPMCBASECA,NULL, C_REGRA.VR1PERREDBC, C_REGRA.CDREGRAFISC,
                       P_CDTIPOOPER, C_REGRA.CDFISCOPER, C_REGRA.IDDIFEALIQ, C_REGRA.IDDIFALIQCUSPR, C_REGRA.VRADICALIQFECP, C_REGRA.IDIMPODESONERA, C_REGRA.IDCONSDESONVRNF, 
                       C_REGRA.CDBENEFFISCAL, C_REGRA.IDCONSMVAPMC, C_REGRA.IDCONSCOMPBCPMC, C_REGRA.IDCONSDIFALIQ);
                END IF;
                
                V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
            END LOOP;
         ELSE

            FOR C_REGRA IN (SELECT *
                              FROM TABLE(CONSULTA_REGRA_FISCAL(P_IMPOSTO, P_EMPRESA, P_CDGRUPOFISC, P_ENTRSAIDAL, P_CDTIPOOPER, P_INSCESTA, P_PRODUTO, P_INIVALID,
                                                               P_PAISORIG, P_ESTAORIG, V_MUNIORIG, P_PAISDEST, P_ESTADEST, V_MUNIDEST, P_BASECALC, 'N', P_IDORIGEMPROD))
                             WHERE VRALIQUOTA IS NOT NULL) LOOP


                SELECT COUNT(*)
                  INTO V_EXISTE
                  FROM ALIQTEMP
                 WHERE NRALIQTEMP  = P_FILIAL||P_LANCTONF
                   AND CDREGRATMP IS NULL;

                IF V_EXISTE > 0 THEN
                   DELETE FROM ALIQTEMP
                   WHERE NRALIQTEMP  = P_FILIAL||P_LANCTONF
                     AND CDREGRATMP IS NULL;
                END IF;

                V_ALIQSIMPLES := 0;

                BEGIN
                   SELECT VRALIQUOTA, COUNT(*)
                     INTO V_VRALIQSIMPLES, V_ALIQSIMPLES
                     FROM ALIQSIMPNAC
                    WHERE CDFORNECED  = P_CDFORNECED
                      AND CDREGRAFISC = C_REGRA.CDREGRAFISC
                      AND CDIMPOSTO   = P_IMPOSTO
                    GROUP BY VRALIQUOTA;
                EXCEPTION
                   WHEN NO_DATA_FOUND THEN
                      V_VRALIQSIMPLES := NULL; V_ALIQSIMPLES := 0;
                END;

                IF (V_ALIQSIMPLES > 0) THEN
                   INSERT INTO ALIQTEMP
                      (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP, VRALIQUTMP, DSMENSALITMP,
                       IDTRIMPSTTMP, VRPBCSTTMP, IDPMCBSCALTMP, CDINSCESTAATMP, VR1PREDBCTMP, CDREGRATMP,
                       CDTIPOOPERTMP, CDFISCOPER, IDDIFEALIQ, IDDIFALIQCUSPR, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, 
                       CDBENEFFISCAL, IDCONSMVAPMC, IDCONSCOMPBCPMC, IDCONSDIFALIQ)
                   VALUES
                      (P_FILIAL||P_LANCTONF, V_SEQUENCIA, 0, 0, V_VRALIQSIMPLES, NULL,
                       C_REGRA.IDTRATIMPST, C_REGRA.VRPBCSUBTRIB, C_REGRA.IDPMCBASECA,NULL, 0, C_REGRA.CDREGRAFISC,
                       P_CDTIPOOPER, C_REGRA.CDFISCOPER, 'N', 'N', C_REGRA.VRADICALIQFECP, C_REGRA.IDIMPODESONERA, C_REGRA.IDCONSDESONVRNF,
                       C_REGRA.CDBENEFFISCAL, C_REGRA.IDCONSMVAPMC, C_REGRA.IDCONSCOMPBCPMC, C_REGRA.IDCONSDIFALIQ);
                ELSE
                
                   INSERT INTO ALIQTEMP
                      (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP, VRALIQUTMP, DSMENSALITMP,
                       IDTRIMPSTTMP, VRPBCSTTMP, IDPMCBSCALTMP, CDINSCESTAATMP, VR1PREDBCTMP, CDREGRATMP,
                       CDTIPOOPERTMP, CDFISCOPER, IDDIFEALIQ, IDDIFALIQCUSPR, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, 
                       CDBENEFFISCAL, IDCONSMVAPMC, IDCONSCOMPBCPMC, IDCONSDIFALIQ)
                   VALUES
                      (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_REGRA.VRPERREDBC, C_REGRA.VRPARDEDUZ, C_REGRA.VRALIQUOTA, NULL,
                       C_REGRA.IDTRATIMPST, C_REGRA.VRPBCSUBTRIB, C_REGRA.IDPMCBASECA,NULL, C_REGRA.VR1PERREDBC, C_REGRA.CDREGRAFISC,
                       P_CDTIPOOPER, C_REGRA.CDFISCOPER, C_REGRA.IDDIFEALIQ, C_REGRA.IDDIFALIQCUSPR, C_REGRA.VRADICALIQFECP, C_REGRA.IDIMPODESONERA, C_REGRA.IDCONSDESONVRNF, 
                       C_REGRA.CDBENEFFISCAL, C_REGRA.IDCONSMVAPMC, C_REGRA.IDCONSCOMPBCPMC, C_REGRA.IDCONSDIFALIQ);
                END IF;

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

   V_EXISTE := 0;
   SELECT COUNT(*)
     INTO V_EXISTE
     FROM ALIQTEMP
    WHERE NRALIQTEMP  = P_FILIAL||P_LANCTONF
      AND CDREGRATMP IS NOT NULL;

   IF NOT(V_EXISTE > 0) THEN

      IF P_CDGRUPOFISC IS NOT NULL THEN /*Se ha grupo fiscal*/
         IF V_IDPRODNOTA = w_P THEN /*Se imposto do item*/
            SELECT MAX(CDTIPOOPER), COUNT(*)
              INTO V_CDTIPOOPER, V_CONTADOR
              FROM ALIQUOTA A, PRODUTO P
             WHERE (CDIMPOSTO    = P_IMPOSTO)
               AND (IDENTRSAIDAL = P_ENTRSAIDAL)
               AND (CDEMPRESA    = P_EMPRESA)
               AND (CDINSCESTA   = RTRIM(P_INSCESTA))
               AND (CDGRUPOFISC  = P_CDGRUPOFISC)
               AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
               AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
               AND ((CDMUNIORIG  = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
               AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
               AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
               AND (A.CDPRODUTO = P.CDPRODUTO(+))
               AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1);

            IF NOT (V_CDTIPOOPER IS NULL) THEN /*Se possui aliquota por tipo de operacao*/
               SELECT MAX(P.CDARVPROD)
               INTO V_CDARVPROD
               FROM ALIQUOTA A, PRODUTO P
               WHERE (CDIMPOSTO    = P_IMPOSTO)
                 AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                 AND (CDEMPRESA    = P_EMPRESA)
                 AND (CDINSCESTA   = RTRIM(P_INSCESTA))
                 AND (CDGRUPOFISC  = P_CDGRUPOFISC)
                 AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
                 AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
                 AND ((CDMUNIORIG  = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                 AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                 AND (CDTIPOOPER = V_CDTIPOOPER OR V_IDUTALIQTPOPER = w_N)
                 AND (A.CDPRODUTO = P.CDPRODUTO(+))
                 AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1);

            ELSIF (V_CDTIPOOPER IS NULL) THEN /*Se nao possui aliquota por tipo de operacao*/
               SELECT MAX(P.CDARVPROD)
                 INTO V_CDARVPROD
                 FROM ALIQUOTA A, PRODUTO P
                WHERE (CDIMPOSTO    = P_IMPOSTO)
                  AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                  AND (CDEMPRESA    = P_EMPRESA)
                  AND (CDINSCESTA   = RTRIM(P_INSCESTA))
                  AND (CDGRUPOFISC  = P_CDGRUPOFISC)
                  AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
                  AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
                  AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                  AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                  AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                  AND (A.CDPRODUTO = P.CDPRODUTO(+))
                  AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1);
            END IF;
         ELSE /*Se imposto da nota*/
            SELECT MAX(A.CDPRODUTO), COUNT(*), MAX(CDTIPOOPER)
            INTO V_CDARVPROD, V_CONTADOR, V_CDTIPOOPER
            FROM ALIQUOTA A
            WHERE (CDIMPOSTO    = P_IMPOSTO)
              AND (IDENTRSAIDAL = P_ENTRSAIDAL)
              AND (CDEMPRESA    = P_EMPRESA)
              AND (CDINSCESTA   = RTRIM(P_INSCESTA))
              AND (CDGRUPOFISC  = P_CDGRUPOFISC)
              AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
              AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
              AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
              AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
              AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
              AND (A.CDPRODUTO IS NULL);
         END IF;
         IF V_CONTADOR > 0 THEN /*Se encontrou aliquota para o grupo fiscal*/
            IF NOT (V_CDTIPOOPER IS NULL) THEN /*Se possui aliquota por tipo de operacao*/
               IF NOT (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o produto de maior nivel*/
                  FOR C_ALIQUOTA IN (
                      SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST,
                             VRPBCSUBTRIB, IDPMCBASECAL, A.CDTIPOOPER, CDMENSAGEM, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, 
                             CDBENEFFISCAL, IDCONSDIFALIQ
                        FROM ALIQUOTA A, MENSALIQU, PRODUTO P
                       WHERE (A.NRALIQUOTA = MENSALIQU.NRALIQUOTA(+))
                         AND (A.CDIMPOSTO = P_IMPOSTO)
                         AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                         AND (A.CDEMPRESA = P_EMPRESA)
                         AND (A.CDINSCESTA = RTRIM(P_INSCESTA))
                         AND (A.CDGRUPOFISC = P_CDGRUPOFISC)
                         AND (A.CDPRODUTO = P.CDPRODUTO(+))
                         AND (P.CDARVPROD = V_CDARVPROD)
                         AND ((A.CDPAISORIG = P_PAISORIG)
                         AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                         AND ((A.CDPAISDEST = P_PAISDEST) AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                         AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                         AND ((A.VRINIFAIXA < V_VRITEMPROD) AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                         AND (CDTIPOOPER = V_CDTIPOOPER)
                         AND (A.DTINIVALID = ( SELECT MAX(DTINIVALID)
                                               FROM ALIQUOTA A, PRODUTO P
                                               WHERE (CDIMPOSTO = P_IMPOSTO)
                                                 AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                                 AND (CDEMPRESA = P_EMPRESA)
                                                 AND (CDINSCESTA = RTRIM(P_INSCESTA))
                                                 AND (CDGRUPOFISC = P_CDGRUPOFISC)
                                                 AND (A.CDPRODUTO = P.CDPRODUTO(+))
                                                 AND (P.CDARVPROD = V_CDARVPROD)
                                                 AND ((CDPAISORIG = P_PAISORIG)
                                                 AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                                 AND ((CDPAISDEST = P_PAISDEST) AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                                 AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                                 AND ((VRINIFAIXA < V_VRITEMPROD) AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                                 AND (CDTIPOOPER = V_CDTIPOOPER)
                                                 AND (DTINIVALID <= P_INIVALID)))) LOOP

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

                      IF V_COUNTMENS > 0 THEN
                         V_DSMENSALIQU := NULL;
                      ELSE
                         V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                      END IF;

                      INSERT INTO ALIQTEMP
                         (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP,
                          VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP,
                          IDPMCBSCALTMP, CDTIPOOPERTMP, VR1PREDBCTMP, CDINSCESTAATMP,
                          VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ)
                      VALUES
                        (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                         C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB, C_ALIQUOTA.IDPMCBASECAL,
                         C_ALIQUOTA.CDTIPOOPER, C_ALIQUOTA.VR1PERREDBC, P_INSCESTA,
                         C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, C_ALIQUOTA.CDBENEFFISCAL,
                         C_ALIQUOTA.IDCONSDIFALIQ);

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

                  END LOOP;
               ELSIF (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o produto "todos"*/
                  FOR C_ALIQUOTA IN (
                     SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL, A.CDTIPOOPER,
                            CDMENSAGEM, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                       FROM ALIQUOTA A, MENSALIQU
                      WHERE (A.NRALIQUOTA = MENSALIQU.NRALIQUOTA(+))
                        AND (A.CDIMPOSTO = P_IMPOSTO)
                        AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                        AND (A.CDEMPRESA = P_EMPRESA)
                        AND (A.CDINSCESTA = RTRIM(P_INSCESTA))
                        AND (A.CDGRUPOFISC = P_CDGRUPOFISC)
                        AND (A.CDPRODUTO IS NULL)
                        AND ((A.CDPAISORIG = P_PAISORIG)
                        AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                        AND ((A.CDPAISDEST = P_PAISDEST) AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                        AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                        AND ((A.VRINIFAIXA < V_VRITEMPROD) AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                        AND (CDTIPOOPER = V_CDTIPOOPER)
                        AND (A.DTINIVALID = ( SELECT MAX(DTINIVALID)
                                                FROM ALIQUOTA
                                               WHERE (CDIMPOSTO = P_IMPOSTO)
                                                 AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                                 AND (CDEMPRESA = P_EMPRESA)
                                                 AND (CDINSCESTA = RTRIM(P_INSCESTA))
                                                 AND (CDGRUPOFISC = P_CDGRUPOFISC)
                                                 AND (CDPRODUTO IS NULL)
                                                 AND ((CDPAISORIG = P_PAISORIG)
                                                 AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                                 AND ((CDPAISDEST = P_PAISDEST)
                                                 AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                                 AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                                 AND ((VRINIFAIXA < V_VRITEMPROD)
                                                 AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                                 AND (CDTIPOOPER = V_CDTIPOOPER)
                                                 AND (DTINIVALID <= P_INIVALID)))) LOOP
                     SELECT COUNT(*)
                       INTO V_COUNTMENS
                       FROM MENSAGEM
                      WHERE CDMENSAGEM = C_ALIQUOTA.CDMENSAGEM
                        AND IDSITUACAOMENS = 'I';

                     IF V_COUNTMENS > 0 THEN
                        V_DSMENSALIQU := NULL;
                     ELSE
                        V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                     END IF;

                     INSERT INTO ALIQTEMP
                        (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP,
                         VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP,
                         IDPMCBSCALTMP, CDTIPOOPERTMP, VR1PREDBCTMP, CDINSCESTAATMP,
                         VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ)
                     VALUES
                        (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                         C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB,
                         C_ALIQUOTA.IDPMCBASECAL, C_ALIQUOTA.CDTIPOOPER, C_ALIQUOTA.VR1PERREDBC, P_INSCESTA,
                         C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, C_ALIQUOTA.CDBENEFFISCAL,
                         C_ALIQUOTA.IDCONSDIFALIQ);

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

                  END LOOP;
               END IF;
            ELSIF (V_CDTIPOOPER IS NULL) THEN /*Se nao possui aliquota por tipo de operacao*/
               IF NOT (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o produto de maior nivel*/
                  FOR C_ALIQUOTA IN (
                      SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL, CDMENSAGEM,
                             VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                        FROM ALIQUOTA A, MENSALIQU, PRODUTO P
                       WHERE (A.NRALIQUOTA = MENSALIQU.NRALIQUOTA(+))
                         AND (A.CDIMPOSTO = P_IMPOSTO)
                         AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                         AND (A.CDEMPRESA = P_EMPRESA)
                         AND (A.CDINSCESTA = RTRIM(P_INSCESTA))
                         AND (A.CDGRUPOFISC = P_CDGRUPOFISC)
                         AND (A.CDPRODUTO = P.CDPRODUTO(+))
                         AND (P.CDARVPROD = V_CDARVPROD)
                         AND ((A.CDPAISORIG = P_PAISORIG)
                         AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                         AND ((A.CDPAISDEST = P_PAISDEST) AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                         AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                         AND ((A.VRINIFAIXA < V_VRITEMPROD) AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                         AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                         AND (A.DTINIVALID = ( SELECT MAX(DTINIVALID)
                                               FROM ALIQUOTA A, PRODUTO P
                                               WHERE (CDIMPOSTO = P_IMPOSTO)
                                                 AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                                 AND (CDEMPRESA = P_EMPRESA)
                                                 AND (CDINSCESTA = RTRIM(P_INSCESTA))
                                                 AND (CDGRUPOFISC = P_CDGRUPOFISC)
                                                 AND (A.CDPRODUTO = P.CDPRODUTO(+))
                                                 AND (P.CDARVPROD = V_CDARVPROD)
                                                 AND ((CDPAISORIG = P_PAISORIG)
                                                 AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                                 AND ((CDPAISDEST = P_PAISDEST) AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                                 AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                                 AND ((VRINIFAIXA < V_VRITEMPROD) AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                                 AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                                                 AND (DTINIVALID <= P_INIVALID)))) LOOP

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

                      IF V_COUNTMENS > 0 THEN
                         V_DSMENSALIQU := NULL;
                      ELSE
                         V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                      END IF;

                      INSERT INTO ALIQTEMP
                         (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP,
                          VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP,
                          IDPMCBSCALTMP, VR1PREDBCTMP, CDINSCESTAATMP, 
                          VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, 
                          CDBENEFFISCAL, IDCONSDIFALIQ)
                      VALUES
                        (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                         C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB,
                         C_ALIQUOTA.IDPMCBASECAL, C_ALIQUOTA.VR1PERREDBC, P_INSCESTA,
                         C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, 
                         C_ALIQUOTA.CDBENEFFISCAL, C_ALIQUOTA.IDCONSDIFALIQ);

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

                  END LOOP;
               ELSIF (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o produto "todos"*/
                  FOR C_ALIQUOTA IN (
                     SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL, CDMENSAGEM,
                            VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                       FROM ALIQUOTA A, MENSALIQU
                      WHERE (A.NRALIQUOTA = MENSALIQU.NRALIQUOTA(+))
                        AND (A.CDIMPOSTO = P_IMPOSTO)
                        AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                        AND (A.CDEMPRESA = P_EMPRESA)
                        AND (A.CDINSCESTA = RTRIM(P_INSCESTA))
                        AND (A.CDGRUPOFISC = P_CDGRUPOFISC)
                        AND (A.CDPRODUTO IS NULL)
                        AND ((A.CDPAISORIG = P_PAISORIG)
                        AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                        AND ((A.CDPAISDEST = P_PAISDEST) AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                        AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                        AND ((A.VRINIFAIXA < V_VRITEMPROD) AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                        AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                        AND (A.DTINIVALID = ( SELECT MAX(DTINIVALID)
                                                FROM ALIQUOTA
                                               WHERE (CDIMPOSTO = P_IMPOSTO)
                                                 AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                                 AND (CDEMPRESA = P_EMPRESA)
                                                 AND (CDINSCESTA = RTRIM(P_INSCESTA))
                                                 AND (CDGRUPOFISC = P_CDGRUPOFISC)
                                                 AND (CDPRODUTO IS NULL)
                                                 AND ((CDPAISORIG = P_PAISORIG)
                                                 AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                                 AND ((CDPAISDEST = P_PAISDEST)
                                                 AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                                 AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                                 AND ((VRINIFAIXA < V_VRITEMPROD)
                                                 AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                                 AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                                                 AND (DTINIVALID <= P_INIVALID)))) LOOP

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

                     IF V_COUNTMENS > 0 THEN
                        V_DSMENSALIQU := NULL;
                     ELSE
                        V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                     END IF;

                     INSERT INTO ALIQTEMP
                        (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP,
                         VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP,
                         IDPMCBSCALTMP, VR1PREDBCTMP, CDINSCESTAATMP, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, 
                         CDBENEFFISCAL, IDCONSDIFALIQ)
                     VALUES
                        (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                         C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB,
                         C_ALIQUOTA.IDPMCBASECAL, C_ALIQUOTA.VR1PERREDBC, P_INSCESTA,
                         C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, 
                         C_ALIQUOTA.CDBENEFFISCAL, C_ALIQUOTA.IDCONSDIFALIQ);

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

                  END LOOP;
               END IF;
            END IF;
         END IF;
      ELSE /*Se nao ha grupo fiscal*/
         SELECT MAX(A.CDTIPOOPER)
         INTO V_CDTIPOOPER
         FROM ALIQUOTA A, PRODUTO P
         WHERE (CDIMPOSTO    = P_IMPOSTO)
           AND (IDENTRSAIDAL = P_ENTRSAIDAL)
           AND (CDEMPRESA    = P_EMPRESA)
           AND (CDINSCESTA   = RTRIM(P_INSCESTA))
           AND (CDGRUPOFISC IS NULL)
           AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
           AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
           AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
           AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
           AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
           AND (A.CDPRODUTO = P.CDPRODUTO(+))
           AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1);

         IF NOT (V_CDTIPOOPER IS NULL) THEN /*Se possui aliquota por tipo de operacao*/
            SELECT MAX(P.CDARVPROD)
              INTO V_CDARVPROD
              FROM ALIQUOTA A, PRODUTO P
             WHERE (CDIMPOSTO    = P_IMPOSTO)
               AND (IDENTRSAIDAL = P_ENTRSAIDAL)
               AND (CDEMPRESA    = P_EMPRESA)
               AND (CDINSCESTA   = RTRIM(P_INSCESTA))
               AND (CDGRUPOFISC IS NULL)
               AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
               AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
               AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
               AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
               AND (CDTIPOOPER = V_CDTIPOOPER OR V_IDUTALIQTPOPER = w_N)
               AND (A.CDPRODUTO = P.CDPRODUTO(+))
               AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1);

         ELSIF (V_CDTIPOOPER IS NULL) THEN /*Se nao possui aliquota por tipo de operacao*/
            SELECT MAX(P.CDARVPROD)
              INTO V_CDARVPROD
              FROM ALIQUOTA A, PRODUTO P
             WHERE (CDIMPOSTO    = P_IMPOSTO)
               AND (IDENTRSAIDAL = P_ENTRSAIDAL)
               AND (CDEMPRESA    = P_EMPRESA)
               AND (CDINSCESTA   = RTRIM(P_INSCESTA))
               AND (CDGRUPOFISC IS NULL)
               AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
               AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
               AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
               AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
               AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
               AND (A.CDPRODUTO = P.CDPRODUTO(+))
               AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1);
         END IF;

         IF NOT (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o maior nivel*/
            IF NOT (V_CDTIPOOPER IS NULL) THEN /*Se possui aliquota por tipo de operacao*/
               FOR C_ALIQUOTA IN (
                   SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL, A.CDTIPOOPER,
                          CDMENSAGEM, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                     FROM ALIQUOTA A, MENSALIQU, PRODUTO P
                    WHERE (A.NRALIQUOTA = MENSALIQU.NRALIQUOTA(+))
                      AND (A.CDIMPOSTO = P_IMPOSTO)
                      AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                      AND (A.CDEMPRESA = P_EMPRESA)
                      AND (A.CDINSCESTA = RTRIM(P_INSCESTA))
                      AND (A.CDGRUPOFISC IS NULL)
                      AND (A.CDPRODUTO = P.CDPRODUTO(+))
                      AND (P.CDARVPROD = V_CDARVPROD)
                      AND ((A.CDPAISORIG = P_PAISORIG)
                      AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                      AND ((A.CDPAISDEST = P_PAISDEST)
                      AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                      AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                      AND ((A.VRINIFAIXA < V_VRITEMPROD)
                      AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                      AND (CDTIPOOPER = V_CDTIPOOPER)
                      AND (A.DTINIVALID = ( SELECT MAX(DTINIVALID)
                                              FROM ALIQUOTA A, PRODUTO P
                                             WHERE (CDIMPOSTO = P_IMPOSTO)
                                               AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                               AND (CDEMPRESA = P_EMPRESA)
                                               AND (CDINSCESTA = RTRIM(P_INSCESTA))
                                               AND (CDGRUPOFISC IS NULL)
                                               AND (A.CDPRODUTO = P.CDPRODUTO(+))
                                               AND (P.CDARVPROD = V_CDARVPROD)
                                               AND ((CDPAISORIG = P_PAISORIG)
                                               AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                               AND ((CDPAISDEST = P_PAISDEST)
                                               AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                               AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                               AND ((VRINIFAIXA < V_VRITEMPROD)
                                               AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                               AND (CDTIPOOPER = V_CDTIPOOPER)
                                               AND (DTINIVALID <= P_INIVALID)))) LOOP

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

                  IF V_COUNTMENS > 0 THEN
                     V_DSMENSALIQU := NULL;
                  ELSE
                     V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                  END IF;

                  INSERT INTO ALIQTEMP
                    (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP,
                     VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP,
                     IDPMCBSCALTMP, CDTIPOOPERTMP, VR1PREDBCTMP, CDINSCESTAATMP,
                     VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ)
                  VALUES
                    (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                     C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB,
                     C_ALIQUOTA.IDPMCBASECAL, C_ALIQUOTA.CDTIPOOPER,C_ALIQUOTA.VR1PERREDBC, P_INSCESTA,
                     C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, C_ALIQUOTA.CDBENEFFISCAL,
                     C_ALIQUOTA.IDCONSDIFALIQ);

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

               END LOOP;
            ELSIF (V_CDTIPOOPER IS NULL) THEN /*Se nao possui aliquota por tipo de operacao*/
               FOR C_ALIQUOTA IN (
                   SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL,
                          CDMENSAGEM, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                     FROM ALIQUOTA A, MENSALIQU, PRODUTO P
                    WHERE (A.NRALIQUOTA = MENSALIQU.NRALIQUOTA(+))
                      AND (A.CDIMPOSTO = P_IMPOSTO)
                      AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                      AND (A.CDEMPRESA = P_EMPRESA)
                      AND (A.CDINSCESTA = RTRIM(P_INSCESTA))
                      AND (A.CDGRUPOFISC IS NULL)
                      AND (A.CDPRODUTO = P.CDPRODUTO(+))
                      AND (P.CDARVPROD = V_CDARVPROD)
                      AND ((A.CDPAISORIG = P_PAISORIG)
                      AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                      AND ((A.CDPAISDEST = P_PAISDEST)
                      AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                      AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                      AND ((A.VRINIFAIXA < V_VRITEMPROD)
                      AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                      AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                      AND (A.DTINIVALID = ( SELECT MAX(DTINIVALID)
                                              FROM ALIQUOTA A, PRODUTO P
                                             WHERE (CDIMPOSTO = P_IMPOSTO)
                                               AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                               AND (CDEMPRESA = P_EMPRESA)
                                               AND (CDINSCESTA = RTRIM(P_INSCESTA))
                                               AND (CDGRUPOFISC IS NULL)
                                               AND (A.CDPRODUTO = P.CDPRODUTO(+))
                                               AND (P.CDARVPROD = V_CDARVPROD)
                                               AND ((CDPAISORIG = P_PAISORIG)
                                               AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                               AND ((CDPAISDEST = P_PAISDEST)
                                               AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                               AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                               AND ((VRINIFAIXA < V_VRITEMPROD)
                                               AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                               AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                                               AND (DTINIVALID <= P_INIVALID)))) LOOP
                  SELECT COUNT(*)
                    INTO V_COUNTMENS
                    FROM MENSAGEM
                   WHERE CDMENSAGEM = C_ALIQUOTA.CDMENSAGEM
                     AND IDSITUACAOMENS = 'I';

                  IF V_COUNTMENS > 0 THEN
                     V_DSMENSALIQU := NULL;
                  ELSE
                     V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                  END IF;

                  INSERT INTO ALIQTEMP
                    (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP,
                     VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP,
                     IDPMCBSCALTMP, VR1PREDBCTMP, CDINSCESTAATMP, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, 
                     CDBENEFFISCAL, IDCONSDIFALIQ)
                  VALUES
                    (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                     C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB,
                     C_ALIQUOTA.IDPMCBASECAL,C_ALIQUOTA.VR1PERREDBC, P_INSCESTA, C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, 
                     C_ALIQUOTA.CDBENEFFISCAL, C_ALIQUOTA.IDCONSDIFALIQ);

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

               END LOOP;
            END IF;
         ELSIF (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para "todos"*/
            IF NOT (V_CDTIPOOPER IS NULL) THEN /*Se ha aliquota por tipo de operacao*/
               FOR C_ALIQUOTA IN (
                  SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL, A.CDTIPOOPER,
                         CDMENSAGEM, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                  FROM ALIQUOTA A, MENSALIQU
                  WHERE (A.NRALIQUOTA = MENSALIQU.NRALIQUOTA(+))
                    AND (A.CDIMPOSTO = P_IMPOSTO)
                    AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                    AND (A.CDEMPRESA = P_EMPRESA)
                    AND (A.CDINSCESTA = RTRIM(P_INSCESTA))
                    AND (A.CDGRUPOFISC IS NULL)
                    AND (A.CDPRODUTO IS NULL)
                    AND ((A.CDPAISORIG = P_PAISORIG)
                    AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                    AND ((A.CDPAISDEST = P_PAISDEST)
                    AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                    AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                    AND ((A.VRINIFAIXA < V_VRITEMPROD)
                    AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                    AND (CDTIPOOPER = V_CDTIPOOPER)
                    AND (A.DTINIVALID = ( SELECT MAX(DTINIVALID)
                                          FROM ALIQUOTA
                                          WHERE (CDIMPOSTO = P_IMPOSTO)
                                            AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                            AND (CDEMPRESA = P_EMPRESA)
                                            AND (CDINSCESTA = RTRIM(P_INSCESTA))
                                            AND (CDGRUPOFISC IS NULL)
                                            AND (CDPRODUTO IS NULL)
                                            AND ((CDPAISORIG = P_PAISORIG)
                                            AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                            AND ((CDPAISDEST = P_PAISDEST)
                                            AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                            AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                            AND ((VRINIFAIXA < V_VRITEMPROD)
                                            AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                            AND (CDTIPOOPER = V_CDTIPOOPER)
                                            AND (DTINIVALID <= P_INIVALID)))) LOOP
                  SELECT COUNT(*)
                    INTO V_COUNTMENS
                    FROM MENSAGEM
                   WHERE CDMENSAGEM = C_ALIQUOTA.CDMENSAGEM
                     AND IDSITUACAOMENS = 'I';

                  IF V_COUNTMENS > 0 THEN
                     V_DSMENSALIQU := NULL;
                  ELSE
                     V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                  END IF;

                  INSERT INTO ALIQTEMP
                     (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP,
                      VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP,
                      IDPMCBSCALTMP, CDTIPOOPERTMP, VR1PREDBCTMP, CDINSCESTAATMP,
                      VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ)
                  VALUES
                     (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                      C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB,
                      C_ALIQUOTA.IDPMCBASECAL, C_ALIQUOTA.CDTIPOOPER,C_ALIQUOTA.VR1PERREDBC, P_INSCESTA,
                      C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, C_ALIQUOTA.CDBENEFFISCAL,
                      C_ALIQUOTA.IDCONSDIFALIQ);

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

               END LOOP;
            ELSIF (V_CDTIPOOPER IS NULL) THEN /*Se nao possui aliquota por tipo de operacao*/
               FOR C_ALIQUOTA IN (
                  SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL,
                         CDMENSAGEM, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                  FROM ALIQUOTA A, MENSALIQU
                  WHERE (A.NRALIQUOTA = MENSALIQU.NRALIQUOTA(+))
                    AND (A.CDIMPOSTO = P_IMPOSTO)
                    AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                    AND (A.CDEMPRESA = P_EMPRESA)
                    AND (A.CDINSCESTA = RTRIM(P_INSCESTA))
                    AND (A.CDGRUPOFISC IS NULL)
                    AND (A.CDPRODUTO IS NULL)
                    AND ((A.CDPAISORIG = P_PAISORIG)
                    AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                    AND ((A.CDPAISDEST = P_PAISDEST)
                    AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                    AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                    AND ((A.VRINIFAIXA < V_VRITEMPROD)
                    AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                    AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                    AND (A.DTINIVALID = ( SELECT MAX(DTINIVALID)
                                          FROM ALIQUOTA
                                          WHERE (CDIMPOSTO = P_IMPOSTO)
                                            AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                            AND (CDEMPRESA = P_EMPRESA)
                                            AND (CDINSCESTA = RTRIM(P_INSCESTA))
                                            AND (CDGRUPOFISC IS NULL)
                                            AND (CDPRODUTO IS NULL)
                                            AND ((CDPAISORIG = P_PAISORIG)
                                            AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                            AND ((CDPAISDEST = P_PAISDEST)
                                            AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                            AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                            AND ((VRINIFAIXA < V_VRITEMPROD)
                                            AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                            AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                                            AND (DTINIVALID <= P_INIVALID)))) LOOP

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

                  IF V_COUNTMENS > 0 THEN
                     V_DSMENSALIQU := NULL;
                  ELSE
                     V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                  END IF;

                  INSERT INTO ALIQTEMP
                     (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP,
                      VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP,
                      IDPMCBSCALTMP, VR1PREDBCTMP, CDINSCESTAATMP, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF,
                      CDBENEFFISCAL, IDCONSDIFALIQ)
                  VALUES
                     (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                      C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB,
                      C_ALIQUOTA.IDPMCBASECAL, C_ALIQUOTA.VR1PERREDBC, P_INSCESTA, C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, 
                      C_ALIQUOTA.CDBENEFFISCAL, C_ALIQUOTA.IDCONSDIFALIQ);

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

               END LOOP;
            END IF;
         END IF;
      END IF;

      /* Verificacao das aliquotas referente as filiais parametrizadas na tela Parametros de Grupo Fiscal (Imposto/Filial) */

      V_CDGRUPOFISC := NULL;

      BEGIN
         SELECT MAX(G.CDGRUPOFISC)
           INTO V_CDGRUPOFISC
           FROM GRUPOFISCIMPFIL G, (SELECT DECODE(P_ENTRSAIDAL, '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.CDIMPOSTO = P_IMPOSTO
            AND G.CDFILIAL = F.CDFILIAL;
      EXCEPTION
         WHEN No_Data_Found THEN
            V_CDGRUPOFISC := NULL;
      END;

      IF NOT (V_CDGRUPOFISC IS NULL) THEN /*Se ha grupo fiscal na tela Parametros de Grupo Fiscal na aba Imposto/Filial*/
         IF V_IDPRODNOTA = w_P THEN /*Se imposto do item*/
            SELECT MAX(CDTIPOOPER), COUNT(*)
              INTO V_CDTIPOOPER, V_CONTADOR
              FROM ALIQUOTA A, PRODUTO P
             WHERE (CDIMPOSTO    = P_IMPOSTO)
               AND (IDENTRSAIDAL = P_ENTRSAIDAL)
               AND (CDEMPRESA    = P_EMPRESA)
               AND (CDINSCESTA   = RTRIM(P_INSCESTA))
               AND (CDGRUPOFISC  = V_CDGRUPOFISC)
               AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
               AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
               AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
               AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
               AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
               AND (A.CDPRODUTO = P.CDPRODUTO(+))
               AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1);

            IF NOT (V_CDTIPOOPER IS NULL) THEN /*Se possui aliquota por tipo de operacao*/
               SELECT MAX(P.CDARVPROD)
                 INTO V_CDARVPROD
                 FROM ALIQUOTA A, PRODUTO P
                WHERE (CDIMPOSTO    = P_IMPOSTO)
                  AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                  AND (CDEMPRESA    = P_EMPRESA)
                  AND (CDINSCESTA   = RTRIM(P_INSCESTA))
                  AND (CDGRUPOFISC  = V_CDGRUPOFISC)
                  AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
                  AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
                  AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                  AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                  AND (CDTIPOOPER = V_CDTIPOOPER OR V_IDUTALIQTPOPER = w_N)
                  AND (A.CDPRODUTO = P.CDPRODUTO(+))
                  AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1);

            ELSIF (V_CDTIPOOPER IS NULL) THEN /*Se nao possui aliquota por tipo de operacao*/
               SELECT MAX(P.CDARVPROD)
               INTO V_CDARVPROD
               FROM ALIQUOTA A, PRODUTO P
               WHERE (CDIMPOSTO    = P_IMPOSTO)
                 AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                 AND (CDEMPRESA    = P_EMPRESA)
                 AND (CDINSCESTA   = RTRIM(P_INSCESTA))
                 AND (CDGRUPOFISC  = V_CDGRUPOFISC)
                 AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
                 AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
                 AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                 AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                 AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                 AND (A.CDPRODUTO = P.CDPRODUTO(+))
                 AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1);
            END IF;
         ELSE /*Se imposto da nota*/
            SELECT MAX(A.CDPRODUTO), COUNT(*), MAX(CDTIPOOPER)
            INTO V_CDARVPROD, V_CONTADOR, V_CDTIPOOPER
            FROM ALIQUOTA A
            WHERE (CDIMPOSTO    = P_IMPOSTO)
              AND (IDENTRSAIDAL = P_ENTRSAIDAL)
              AND (CDEMPRESA    = P_EMPRESA)
              AND (CDINSCESTA   = RTRIM(P_INSCESTA))
              AND (CDGRUPOFISC  = V_CDGRUPOFISC)
              AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
              AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
              AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
              AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
              AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
              AND (A.CDPRODUTO IS NULL);
         END IF;
         IF NOT (V_CDTIPOOPER IS NULL) THEN /*Se possui aliquota por tipo de operacao*/
            IF NOT (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o maior nivel*/
               FOR C_ALIQUOTA IN (
                    SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL, CDTIPOOPER,
                           CDMENSAGEM, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                      FROM ALIQUOTA A, MENSALIQU M, PRODUTO P
                     WHERE (A.NRALIQUOTA = M.NRALIQUOTA(+))
                       AND (A.CDIMPOSTO = P_IMPOSTO)
                       AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                       AND (A.CDEMPRESA = P_EMPRESA)
                       AND (A.CDINSCESTA = RTRIM(P_INSCESTA))
                       AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                       AND (A.CDPRODUTO = P.CDPRODUTO(+))
                       AND (P.CDARVPROD = V_CDARVPROD)
                       AND ((A.CDPAISORIG = P_PAISORIG)
                       AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                       AND ((A.CDPAISDEST = P_PAISDEST)
                       AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                       AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                       AND ((A.VRINIFAIXA < V_VRITEMPROD)
                       AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                       AND (CDTIPOOPER = V_CDTIPOOPER)
                       AND (A.DTINIVALID = (SELECT MAX(DTINIVALID)
                                              FROM ALIQUOTA A, PRODUTO P
                                             WHERE (CDIMPOSTO = P_IMPOSTO)
                                               AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                               AND (CDEMPRESA = P_EMPRESA)
                                               AND (CDINSCESTA = RTRIM(P_INSCESTA))
                                               AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                                               AND (A.CDPRODUTO = P.CDPRODUTO(+))
                                               AND (P.CDARVPROD = V_CDARVPROD)
                                               AND ((CDPAISORIG = P_PAISORIG)
                                               AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                               AND ((CDPAISDEST = P_PAISDEST)
                                               AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                               AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                               AND ((VRINIFAIXA < V_VRITEMPROD)
                                               AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                               AND (CDTIPOOPER = V_CDTIPOOPER)
                                               AND (DTINIVALID <= P_INIVALID)))) LOOP
                  SELECT COUNT(*)
                    INTO V_COUNTMENS
                    FROM MENSAGEM
                   WHERE CDMENSAGEM = C_ALIQUOTA.CDMENSAGEM
                     AND IDSITUACAOMENS = 'I';

                  IF V_COUNTMENS > 0 THEN
                     V_DSMENSALIQU := NULL;
                  ELSE
                     V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                  END IF;

                  INSERT INTO ALIQTEMP
                     (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP,
                      VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP,
                      IDPMCBSCALTMP, CDTIPOOPERTMP, VR1PREDBCTMP, CDINSCESTAATMP,
                      VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ)
                  VALUES
                     (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                      C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB,
                      C_ALIQUOTA.IDPMCBASECAL, C_ALIQUOTA.CDTIPOOPER, C_ALIQUOTA.VR1PERREDBC, P_INSCESTA,
                      C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, C_ALIQUOTA.CDBENEFFISCAL,
                      C_ALIQUOTA.IDCONSDIFALIQ);

                  V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
               END LOOP;
            ELSIF (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o produto "todos"*/
               FOR C_ALIQUOTA IN (
                    SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL, A.CDTIPOOPER,
                           CDMENSAGEM, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                      FROM ALIQUOTA A, MENSALIQU M
                     WHERE (A.NRALIQUOTA = M.NRALIQUOTA(+))
                       AND (A.CDIMPOSTO = P_IMPOSTO)
                       AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                       AND (A.CDEMPRESA = P_EMPRESA)
                       AND (A.CDINSCESTA = RTRIM(P_INSCESTA))
                       AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                       AND (A.CDPRODUTO IS NULL)
                       AND ((A.CDPAISORIG = P_PAISORIG)
                       AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                       AND ((A.CDPAISDEST = P_PAISDEST)
                       AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                       AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                       AND ((A.VRINIFAIXA < V_VRITEMPROD)
                       AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                       AND (CDTIPOOPER = V_CDTIPOOPER)
                       AND (A.DTINIVALID = (SELECT MAX(DTINIVALID)
                                              FROM ALIQUOTA
                                             WHERE (CDIMPOSTO = P_IMPOSTO)
                                               AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                               AND (CDEMPRESA = P_EMPRESA)
                                               AND (CDINSCESTA = RTRIM(P_INSCESTA))
                                               AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                                               AND (CDPRODUTO IS NULL)
                                               AND ((CDPAISORIG = P_PAISORIG)
                                               AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                               AND ((CDPAISDEST = P_PAISDEST)
                                               AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                               AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                               AND ((VRINIFAIXA < V_VRITEMPROD)
                                               AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                               AND (CDTIPOOPER = V_CDTIPOOPER)
                                               AND (DTINIVALID <= P_INIVALID)))) LOOP
                  SELECT COUNT(*)
                    INTO V_COUNTMENS
                    FROM MENSAGEM
                   WHERE CDMENSAGEM = C_ALIQUOTA.CDMENSAGEM
                     AND IDSITUACAOMENS = 'I';

                  IF V_COUNTMENS > 0 THEN
                     V_DSMENSALIQU := NULL;
                  ELSE
                     V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                  END IF;

                  INSERT INTO ALIQTEMP
                     (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP,
                      VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP,
                       IDPMCBSCALTMP, CDTIPOOPERTMP, VR1PREDBCTMP, CDINSCESTAATMP,
                       VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ)
                  VALUES
                     (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                      C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB,
                      C_ALIQUOTA.IDPMCBASECAL, C_ALIQUOTA.CDTIPOOPER, C_ALIQUOTA.VR1PERREDBC, P_INSCESTA,
                      C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, C_ALIQUOTA.CDBENEFFISCAL,
                      C_ALIQUOTA.IDCONSDIFALIQ);

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

               END LOOP;
            END IF;
         ELSIF (V_CDTIPOOPER IS NULL) THEN /*Se nao possui aliquota por tipo de operacao*/
            IF NOT (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o maior nivel*/
               FOR C_ALIQUOTA IN (
                    SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL, CDMENSAGEM,
                           VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                      FROM ALIQUOTA A, MENSALIQU M, PRODUTO P
                     WHERE (A.NRALIQUOTA = M.NRALIQUOTA(+))
                       AND (A.CDIMPOSTO = P_IMPOSTO)
                       AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                       AND (A.CDEMPRESA = P_EMPRESA)
                       AND (A.CDINSCESTA = RTRIM(P_INSCESTA))
                       AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                       AND (A.CDPRODUTO = P.CDPRODUTO(+))
                       AND (P.CDARVPROD = V_CDARVPROD)
                       AND ((A.CDPAISORIG = P_PAISORIG)
                       AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                       AND ((A.CDPAISDEST = P_PAISDEST)
                       AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                       AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                       AND ((A.VRINIFAIXA < V_VRITEMPROD)
                       AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                       AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                       AND (A.DTINIVALID = (SELECT MAX(DTINIVALID)
                                              FROM ALIQUOTA A, PRODUTO P
                                             WHERE (CDIMPOSTO = P_IMPOSTO)
                                               AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                               AND (CDEMPRESA = P_EMPRESA)
                                               AND (CDINSCESTA = RTRIM(P_INSCESTA))
                                               AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                                               AND (A.CDPRODUTO = P.CDPRODUTO(+))
                                               AND (P.CDARVPROD = V_CDARVPROD)
                                               AND ((CDPAISORIG = P_PAISORIG)
                                               AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                               AND ((CDPAISDEST = P_PAISDEST)
                                               AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                               AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                               AND ((VRINIFAIXA < V_VRITEMPROD)
                                               AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                               AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                                               AND (DTINIVALID <= P_INIVALID)))) LOOP
                  SELECT COUNT(*)
                    INTO V_EXISTE
                    FROM ALIQTEMP
                   WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;

                  IF V_EXISTE > 0 THEN
                     DELETE FROM ALIQTEMP
                     WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;
                  END IF;

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

                  IF V_COUNTMENS > 0 THEN
                     V_DSMENSALIQU := NULL;
                  ELSE
                     V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                  END IF;

                  INSERT INTO ALIQTEMP
                     (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP,
                      VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP,
                      IDPMCBSCALTMP, VR1PREDBCTMP, CDINSCESTAATMP, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, 
                      CDBENEFFISCAL, IDCONSDIFALIQ)
                  VALUES
                     (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                      C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB,
                      C_ALIQUOTA.IDPMCBASECAL, C_ALIQUOTA.VR1PERREDBC, P_INSCESTA, C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, 
                      C_ALIQUOTA.CDBENEFFISCAL, C_ALIQUOTA.IDCONSDIFALIQ);

                  V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
               END LOOP;
            ELSIF (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o produto "todos"*/
               FOR C_ALIQUOTA IN (
                    SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL, CDMENSAGEM,
                           VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                      FROM ALIQUOTA A, MENSALIQU M
                     WHERE (A.NRALIQUOTA = M.NRALIQUOTA(+))
                       AND (A.CDIMPOSTO = P_IMPOSTO)
                       AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                       AND (A.CDEMPRESA = P_EMPRESA)
                       AND (A.CDINSCESTA = RTRIM(P_INSCESTA))
                       AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                       AND (A.CDPRODUTO IS NULL)
                       AND ((A.CDPAISORIG = P_PAISORIG)
                       AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                       AND ((A.CDPAISDEST = P_PAISDEST)
                       AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                       AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                       AND ((A.VRINIFAIXA < V_VRITEMPROD)
                       AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                       AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                       AND (A.DTINIVALID = (SELECT MAX(DTINIVALID)
                                              FROM ALIQUOTA
                                             WHERE (CDIMPOSTO = P_IMPOSTO)
                                               AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                               AND (CDEMPRESA = P_EMPRESA)
                                               AND (CDINSCESTA = RTRIM(P_INSCESTA))
                                               AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                                               AND (CDPRODUTO IS NULL)
                                               AND ((CDPAISORIG = P_PAISORIG)
                                               AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                               AND ((CDPAISDEST = P_PAISDEST)
                                               AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                               AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                               AND ((VRINIFAIXA < V_VRITEMPROD)
                                               AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                               AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                                               AND (DTINIVALID <= P_INIVALID)))) LOOP
                  SELECT COUNT(*)
                    INTO V_EXISTE
                    FROM ALIQTEMP
                   WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;

                  IF V_EXISTE > 0 THEN
                     DELETE FROM ALIQTEMP
                     WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;
                  END IF;

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

                  IF V_COUNTMENS > 0 THEN
                     V_DSMENSALIQU := NULL;
                  ELSE
                     V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                  END IF;

                  INSERT INTO ALIQTEMP
                     (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP,
                      VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP,
                      IDPMCBSCALTMP, VR1PREDBCTMP, CDINSCESTAATMP, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, 
                      CDBENEFFISCAL, IDCONSDIFALIQ)
                  VALUES
                     (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                      C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB,
                      C_ALIQUOTA.IDPMCBASECAL, C_ALIQUOTA.VR1PERREDBC, P_INSCESTA, C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, 
                      C_ALIQUOTA.CDBENEFFISCAL, C_ALIQUOTA.IDCONSDIFALIQ);

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

               END LOOP;
            END IF;
         END IF;
      END IF;

   ----------------------
   /* Verificacao das aliquotas referente as filiais parametrizadas na tela Parametros de Grupo Fiscal (Imposto/Inscricao Estadual) */
      /*Por Fornecedor*/
      V_CDGRUPOFISC := NULL;

      BEGIN
         SELECT MAX(G.CDGRUPOFISC)
           INTO V_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) F
          WHERE G.CDIMPOSTO = P_IMPOSTO
            AND G.CDFORNECED = F.CDFORNECED
            AND G.CDEMPRESA = P_EMPRESA
            AND G.CDINSCESTA = P_INSCESTA;
      EXCEPTION
         WHEN No_Data_Found THEN
            V_CDGRUPOFISC := NULL;
      END;

      IF NOT (V_CDGRUPOFISC IS NULL) THEN /*Se ha grupo fiscal na tela Parametros de Grupo Fiscal na aba Imposto/Inscricao Estadual*/
         IF V_IDPRODNOTA = w_P THEN /*Se imposto do item*/
            SELECT MAX(CDTIPOOPER), COUNT(*)
              INTO V_CDTIPOOPER, V_CONTADOR
              FROM ALIQUOTA A, PRODUTO P
             WHERE (CDIMPOSTO    = P_IMPOSTO)
               AND (IDENTRSAIDAL = P_ENTRSAIDAL)
               AND (CDEMPRESA    = P_EMPRESA)
               AND (CDINSCESTA   = RTRIM(P_INSCESTA))
               AND (CDGRUPOFISC  = V_CDGRUPOFISC)
               AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
               AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
               AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
               AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
               AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
               AND (A.CDPRODUTO = P.CDPRODUTO(+))
               AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1);

            IF NOT (V_CDTIPOOPER IS NULL) THEN /*Se possui aliquota por tipo de operacao*/
               SELECT MAX(P.CDARVPROD)
                 INTO V_CDARVPROD
                 FROM ALIQUOTA A, PRODUTO P
                WHERE (CDIMPOSTO    = P_IMPOSTO)
                  AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                  AND (CDEMPRESA    = P_EMPRESA)
                  AND (CDINSCESTA   = RTRIM(P_INSCESTA))
                  AND (CDGRUPOFISC  = V_CDGRUPOFISC)
                  AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
                  AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
                  AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                  AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                  AND (CDTIPOOPER = V_CDTIPOOPER OR V_IDUTALIQTPOPER = w_N)
                  AND (A.CDPRODUTO = P.CDPRODUTO(+))
                  AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1);

            ELSIF (V_CDTIPOOPER IS NULL) THEN /*Se nao possui aliquota por tipo de operacao*/
               SELECT MAX(P.CDARVPROD)
               INTO V_CDARVPROD
               FROM ALIQUOTA A, PRODUTO P
               WHERE (CDIMPOSTO    = P_IMPOSTO)
                 AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                 AND (CDEMPRESA    = P_EMPRESA)
                 AND (CDINSCESTA   = RTRIM(P_INSCESTA))
                 AND (CDGRUPOFISC  = V_CDGRUPOFISC)
                 AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
                 AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
                 AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                 AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                 AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                 AND (A.CDPRODUTO = P.CDPRODUTO(+))
                 AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1);
            END IF;
         ELSE /*Se imposto da nota*/
            SELECT MAX(A.CDPRODUTO), COUNT(*), MAX(CDTIPOOPER)
            INTO V_CDARVPROD, V_CONTADOR, V_CDTIPOOPER
            FROM ALIQUOTA A
            WHERE (CDIMPOSTO    = P_IMPOSTO)
              AND (IDENTRSAIDAL = P_ENTRSAIDAL)
              AND (CDEMPRESA    = P_EMPRESA)
              AND (CDINSCESTA   = RTRIM(P_INSCESTA))
              AND (CDGRUPOFISC  = V_CDGRUPOFISC)
              AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
              AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
              AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
              AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
              AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
              AND (A.CDPRODUTO IS NULL);
         END IF;

         IF NOT (V_CDTIPOOPER IS NULL) THEN /*Se possui aliquota por tipo de operacao*/
            IF NOT (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o maior nivel*/
               FOR C_ALIQUOTA IN (
                    SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL, CDTIPOOPER,
                           CDMENSAGEM, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                      FROM ALIQUOTA A, MENSALIQU M, PRODUTO P
                     WHERE (A.NRALIQUOTA = M.NRALIQUOTA(+))
                       AND (A.CDIMPOSTO = P_IMPOSTO)
                       AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                       AND (A.CDEMPRESA = P_EMPRESA)
                       AND (A.CDINSCESTA = RTRIM(P_INSCESTA))
                       AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                       AND (A.CDPRODUTO = P.CDPRODUTO(+))
                       AND (P.CDARVPROD = V_CDARVPROD)
                       AND ((A.CDPAISORIG = P_PAISORIG)
                       AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                       AND ((A.CDPAISDEST = P_PAISDEST)
                       AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                       AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                       AND ((A.VRINIFAIXA < V_VRITEMPROD)
                       AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                       AND (CDTIPOOPER = V_CDTIPOOPER)
                       AND (A.DTINIVALID = (SELECT MAX(DTINIVALID)
                                              FROM ALIQUOTA A, PRODUTO P
                                             WHERE (CDIMPOSTO = P_IMPOSTO)
                                               AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                               AND (CDEMPRESA = P_EMPRESA)
                                               AND (CDINSCESTA = RTRIM(P_INSCESTA))
                                               AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                                               AND (A.CDPRODUTO = P.CDPRODUTO(+))
                                               AND (P.CDARVPROD = V_CDARVPROD)
                                               AND ((CDPAISORIG = P_PAISORIG)
                                               AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                               AND ((CDPAISDEST = P_PAISDEST)
                                               AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                               AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                               AND ((VRINIFAIXA < V_VRITEMPROD)
                                               AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                               AND (CDTIPOOPER = V_CDTIPOOPER)
                                               AND (DTINIVALID <= P_INIVALID)))) LOOP

                  SELECT COUNT(*)
                    INTO V_EXISTE
                    FROM ALIQTEMP
                   WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;

                  IF V_EXISTE > 0 THEN
                     DELETE FROM ALIQTEMP
                     WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;
                  END IF;

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

                  IF V_COUNTMENS > 0 THEN
                     V_DSMENSALIQU := NULL;
                  ELSE
                     V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                  END IF;

                  INSERT INTO ALIQTEMP
                     (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP,
                      VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP,
                      IDPMCBSCALTMP, CDTIPOOPERTMP, VR1PREDBCTMP, CDINSCESTAATMP,
                      VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ)
                  VALUES
                     (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                      C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB,
                      C_ALIQUOTA.IDPMCBASECAL, C_ALIQUOTA.CDTIPOOPER,C_ALIQUOTA.VR1PERREDBC, P_INSCESTA,
                      C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, C_ALIQUOTA.CDBENEFFISCAL,
                      C_ALIQUOTA.IDCONSDIFALIQ);

                  V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
               END LOOP;
            ELSIF (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o produto "todos"*/
               FOR C_ALIQUOTA IN (
                    SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL, A.CDTIPOOPER, CDMENSAGEM,
                           VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                      FROM ALIQUOTA A, MENSALIQU M
                     WHERE (A.NRALIQUOTA = M.NRALIQUOTA(+))
                       AND (A.CDIMPOSTO = P_IMPOSTO)
                       AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                       AND (A.CDEMPRESA = P_EMPRESA)
                       AND (A.CDINSCESTA = RTRIM(P_INSCESTA))
                       AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                       AND (A.CDPRODUTO IS NULL)
                       AND ((A.CDPAISORIG = P_PAISORIG)
                       AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                       AND ((A.CDPAISDEST = P_PAISDEST)
                       AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                       AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                       AND ((A.VRINIFAIXA < V_VRITEMPROD)
                       AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                       AND (CDTIPOOPER = V_CDTIPOOPER)
                       AND (A.DTINIVALID = (SELECT MAX(DTINIVALID)
                                              FROM ALIQUOTA
                                             WHERE (CDIMPOSTO = P_IMPOSTO)
                                               AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                               AND (CDEMPRESA = P_EMPRESA)
                                               AND (CDINSCESTA = RTRIM(P_INSCESTA))
                                               AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                                               AND (CDPRODUTO IS NULL)
                                               AND ((CDPAISORIG = P_PAISORIG)
                                               AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                               AND ((CDPAISDEST = P_PAISDEST)
                                               AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                               AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                               AND ((VRINIFAIXA < V_VRITEMPROD)
                                               AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                               AND (CDTIPOOPER = V_CDTIPOOPER)
                                               AND (DTINIVALID <= P_INIVALID)))) LOOP
                  SELECT COUNT(*)
                    INTO V_EXISTE
                    FROM ALIQTEMP
                   WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;

                  IF V_EXISTE > 0 THEN
                     DELETE FROM ALIQTEMP
                     WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;
                  END IF;

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

                  IF V_COUNTMENS > 0 THEN
                     V_DSMENSALIQU := NULL;
                  ELSE
                     V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                  END IF;

                  INSERT INTO ALIQTEMP
                     (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP,
                      VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP,
                      IDPMCBSCALTMP, CDTIPOOPERTMP, VR1PREDBCTMP, CDINSCESTAATMP,
                      VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ)
                  VALUES
                     (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                      C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB,
                      C_ALIQUOTA.IDPMCBASECAL, C_ALIQUOTA.CDTIPOOPER,C_ALIQUOTA.VR1PERREDBC, P_INSCESTA,
                      C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, C_ALIQUOTA.CDBENEFFISCAL,
                      C_ALIQUOTA.IDCONSDIFALIQ);

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

               END LOOP;
            END IF;
         ELSIF (V_CDTIPOOPER IS NULL) THEN /*Se nao possui aliquota por tipo de operacao*/
            IF NOT (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o maior nivel*/
               FOR C_ALIQUOTA IN (
                    SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL,
                           CDMENSAGEM, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                      FROM ALIQUOTA A, MENSALIQU M, PRODUTO P
                     WHERE (A.NRALIQUOTA = M.NRALIQUOTA(+))
                       AND (A.CDIMPOSTO = P_IMPOSTO)
                       AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                       AND (A.CDEMPRESA = P_EMPRESA)
                       AND (A.CDINSCESTA = RTRIM(P_INSCESTA))
                       AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                       AND (A.CDPRODUTO = P.CDPRODUTO(+))
                       AND (P.CDARVPROD = V_CDARVPROD)
                       AND ((A.CDPAISORIG = P_PAISORIG)
                       AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                       AND ((A.CDPAISDEST = P_PAISDEST)
                       AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                       AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                       AND ((A.VRINIFAIXA < V_VRITEMPROD)
                       AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                       AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                       AND (A.DTINIVALID = (SELECT MAX(DTINIVALID)
                                              FROM ALIQUOTA A, PRODUTO P
                                             WHERE (CDIMPOSTO = P_IMPOSTO)
                                               AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                               AND (CDEMPRESA = P_EMPRESA)
                                               AND (CDINSCESTA = RTRIM(P_INSCESTA))
                                               AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                                               AND (A.CDPRODUTO = P.CDPRODUTO(+))
                                               AND (P.CDARVPROD = V_CDARVPROD)
                                               AND ((CDPAISORIG = P_PAISORIG)
                                               AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                               AND ((CDPAISDEST = P_PAISDEST)
                                               AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                               AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                               AND ((VRINIFAIXA < V_VRITEMPROD)
                                               AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                               AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                                               AND (DTINIVALID <= P_INIVALID)))) LOOP
                  SELECT COUNT(*)
                    INTO V_EXISTE
                    FROM ALIQTEMP
                   WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;

                  IF V_EXISTE > 0 THEN
                     DELETE FROM ALIQTEMP
                     WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;
                  END IF;

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

                  IF V_COUNTMENS > 0 THEN
                     V_DSMENSALIQU := NULL;
                  ELSE
                     V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                  END IF;

                  INSERT INTO ALIQTEMP
                     (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP,
                      VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP,
                      IDPMCBSCALTMP, VR1PREDBCTMP, CDINSCESTAATMP, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, 
                      CDBENEFFISCAL, IDCONSDIFALIQ)
                  VALUES
                     (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                      C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB,
                      C_ALIQUOTA.IDPMCBASECAL, C_ALIQUOTA.VR1PERREDBC, P_INSCESTA, C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, 
                      C_ALIQUOTA.CDBENEFFISCAL, C_ALIQUOTA.IDCONSDIFALIQ);

                  V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
               END LOOP;
            ELSIF (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o produto "todos"*/
               FOR C_ALIQUOTA IN (
                    SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL, CDMENSAGEM,
                           VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                      FROM ALIQUOTA A, MENSALIQU M
                     WHERE (A.NRALIQUOTA = M.NRALIQUOTA(+))
                       AND (A.CDIMPOSTO = P_IMPOSTO)
                       AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                       AND (A.CDEMPRESA = P_EMPRESA)
                       AND (A.CDINSCESTA = RTRIM(P_INSCESTA))
                       AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                       AND (A.CDPRODUTO IS NULL)
                       AND ((A.CDPAISORIG = P_PAISORIG)
                       AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                       AND ((A.CDPAISDEST = P_PAISDEST)
                       AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                       AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                       AND ((A.VRINIFAIXA < V_VRITEMPROD)
                       AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                       AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                       AND (A.DTINIVALID = (SELECT MAX(DTINIVALID)
                                              FROM ALIQUOTA
                                             WHERE (CDIMPOSTO = P_IMPOSTO)
                                               AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                               AND (CDEMPRESA = P_EMPRESA)
                                               AND (CDINSCESTA = RTRIM(P_INSCESTA))
                                               AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                                               AND (CDPRODUTO IS NULL)
                                               AND ((CDPAISORIG = P_PAISORIG)
                                               AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                               AND ((CDPAISDEST = P_PAISDEST)
                                               AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                               AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                               AND ((VRINIFAIXA < V_VRITEMPROD)
                                               AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                               AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                                               AND (DTINIVALID <= P_INIVALID)))) LOOP
                  SELECT COUNT(*)
                    INTO V_EXISTE
                    FROM ALIQTEMP
                   WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;

                  IF V_EXISTE > 0 THEN
                     DELETE FROM ALIQTEMP
                     WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;
                  END IF;

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

                  IF V_COUNTMENS > 0 THEN
                     V_DSMENSALIQU := NULL;
                  ELSE
                     V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                  END IF;

                  INSERT INTO ALIQTEMP
                     (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP,
                      VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP,
                      IDPMCBSCALTMP, VR1PREDBCTMP, CDINSCESTAATMP, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, 
                      CDBENEFFISCAL, IDCONSDIFALIQ)
                  VALUES
                     (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                      C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB,
                      C_ALIQUOTA.IDPMCBASECAL,C_ALIQUOTA.VR1PERREDBC, P_INSCESTA, C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, 
                      C_ALIQUOTA.CDBENEFFISCAL, C_ALIQUOTA.IDCONSDIFALIQ);

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

               END LOOP;
            END IF;
         END IF;
      END IF;
      /*Por Cliente*/
      V_CDGRUPOFISC := NULL;

      BEGIN
         SELECT MAX(G.CDGRUPOFISC)
           INTO V_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) F
          WHERE G.CDIMPOSTO = P_IMPOSTO
            AND G.CDCLIENTE = F.CDCLIENTE
            AND G.CDEMPRESA = P_EMPRESA
            AND G.CDINSCESTA = P_INSCESTA;
      EXCEPTION
         WHEN No_Data_Found THEN
            V_CDGRUPOFISC := NULL;
      END;

      IF NOT (V_CDGRUPOFISC IS NULL) THEN /*Se ha grupo fiscal na tela Parametros de Grupo Fiscal na aba Imposto/Inscricao Estadual*/
         IF V_IDPRODNOTA = w_P THEN /*Se imposto do item*/
            SELECT MAX(CDTIPOOPER), COUNT(*)
              INTO V_CDTIPOOPER, V_CONTADOR
              FROM ALIQUOTA A, PRODUTO P
             WHERE (CDIMPOSTO    = P_IMPOSTO)
               AND (IDENTRSAIDAL = P_ENTRSAIDAL)
               AND (CDEMPRESA    = P_EMPRESA)
               AND (CDINSCESTA   = RTRIM(P_INSCESTA))
               AND (CDGRUPOFISC  = V_CDGRUPOFISC)
               AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
               AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
               AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
               AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
               AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
               AND (A.CDPRODUTO = P.CDPRODUTO(+))
               AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1);

            IF NOT (V_CDTIPOOPER IS NULL) THEN /*Se possui aliquota por tipo de operacao*/
               SELECT MAX(P.CDARVPROD)
                 INTO V_CDARVPROD
                 FROM ALIQUOTA A, PRODUTO P
                WHERE (CDIMPOSTO    = P_IMPOSTO)
                  AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                  AND (CDEMPRESA    = P_EMPRESA)
                  AND (CDINSCESTA   = RTRIM(P_INSCESTA))
                  AND (CDGRUPOFISC  = V_CDGRUPOFISC)
                  AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
                  AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
                  AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                  AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                  AND (CDTIPOOPER = V_CDTIPOOPER OR V_IDUTALIQTPOPER = w_N)
                  AND (A.CDPRODUTO = P.CDPRODUTO(+))
                  AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1);

            ELSIF (V_CDTIPOOPER IS NULL) THEN /*Se nao possui aliquota por tipo de operacao*/
               SELECT MAX(P.CDARVPROD)
               INTO V_CDARVPROD
               FROM ALIQUOTA A, PRODUTO P
               WHERE (CDIMPOSTO    = P_IMPOSTO)
                 AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                 AND (CDEMPRESA    = P_EMPRESA)
                 AND (CDINSCESTA   = RTRIM(P_INSCESTA))
                 AND (CDGRUPOFISC  = V_CDGRUPOFISC)
                 AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
                 AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
                 AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                 AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                 AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                 AND (A.CDPRODUTO = P.CDPRODUTO(+))
                 AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1);
            END IF;
         ELSE /*Se imposto da nota*/
            SELECT MAX(A.CDPRODUTO), COUNT(*), MAX(CDTIPOOPER)
            INTO V_CDARVPROD, V_CONTADOR, V_CDTIPOOPER
            FROM ALIQUOTA A
            WHERE (CDIMPOSTO    = P_IMPOSTO)
              AND (IDENTRSAIDAL = P_ENTRSAIDAL)
              AND (CDEMPRESA    = P_EMPRESA)
              AND (CDINSCESTA   = RTRIM(P_INSCESTA))
              AND (CDGRUPOFISC  = V_CDGRUPOFISC)
              AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
              AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
              AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
              AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
              AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
              AND (A.CDPRODUTO IS NULL);
         END IF;

         IF NOT (V_CDTIPOOPER IS NULL) THEN /*Se possui aliquota por tipo de operacao*/

            IF NOT (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o maior nivel*/
               FOR C_ALIQUOTA IN (
                    SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL, CDTIPOOPER, CDMENSAGEM,
                           VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                      FROM ALIQUOTA A, MENSALIQU M, PRODUTO P
                     WHERE (A.NRALIQUOTA = M.NRALIQUOTA(+))
                       AND (A.CDIMPOSTO = P_IMPOSTO)
                       AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                       AND (A.CDEMPRESA = P_EMPRESA)
                       AND (A.CDINSCESTA = RTRIM(P_INSCESTA))
                       AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                       AND (A.CDPRODUTO = P.CDPRODUTO(+))
                       AND (P.CDARVPROD = V_CDARVPROD)
                       AND ((A.CDPAISORIG = P_PAISORIG)
                       AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                       AND ((A.CDPAISDEST = P_PAISDEST)
                       AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                       AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                       AND ((A.VRINIFAIXA < V_VRITEMPROD)
                       AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                       AND (CDTIPOOPER = V_CDTIPOOPER)
                       AND (A.DTINIVALID = (SELECT MAX(DTINIVALID)
                                              FROM ALIQUOTA A, PRODUTO P
                                             WHERE (CDIMPOSTO = P_IMPOSTO)
                                               AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                               AND (CDEMPRESA = P_EMPRESA)
                                               AND (CDINSCESTA = RTRIM(P_INSCESTA))
                                               AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                                               AND (A.CDPRODUTO = P.CDPRODUTO(+))
                                               AND (P.CDARVPROD = V_CDARVPROD)
                                               AND ((CDPAISORIG = P_PAISORIG)
                                               AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                               AND ((CDPAISDEST = P_PAISDEST)
                                               AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                               AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                               AND ((VRINIFAIXA < V_VRITEMPROD)
                                               AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                               AND (CDTIPOOPER = V_CDTIPOOPER)
                                               AND (DTINIVALID <= P_INIVALID)))) LOOP
                  SELECT COUNT(*)
                    INTO V_EXISTE
                    FROM ALIQTEMP
                   WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;

                  IF V_EXISTE > 0 THEN
                     DELETE FROM ALIQTEMP
                     WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;
                  END IF;

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

                  IF V_COUNTMENS > 0 THEN
                     V_DSMENSALIQU := NULL;
                  ELSE
                     V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                  END IF;

                  INSERT INTO ALIQTEMP
                     (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP,
                      VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP,
                      IDPMCBSCALTMP, CDTIPOOPERTMP, VR1PREDBCTMP, CDINSCESTAATMP,
                      VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ)
                  VALUES
                     (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                      C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB,
                      C_ALIQUOTA.IDPMCBASECAL, C_ALIQUOTA.CDTIPOOPER, C_ALIQUOTA.VR1PERREDBC, P_INSCESTA,
                      C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, C_ALIQUOTA.CDBENEFFISCAL,
                      C_ALIQUOTA.IDCONSDIFALIQ);
                  V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
               END LOOP;
            ELSIF (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o produto "todos"*/
               FOR C_ALIQUOTA IN (
                    SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL, A.CDTIPOOPER, CDMENSAGEM,
                           VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                      FROM ALIQUOTA A, MENSALIQU M
                     WHERE (A.NRALIQUOTA = M.NRALIQUOTA(+))
                       AND (A.CDIMPOSTO = P_IMPOSTO)
                       AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                       AND (A.CDEMPRESA = P_EMPRESA)
                       AND (A.CDINSCESTA = RTRIM(P_INSCESTA))
                       AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                       AND (A.CDPRODUTO IS NULL)
                       AND ((A.CDPAISORIG = P_PAISORIG)
                       AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                       AND ((A.CDPAISDEST = P_PAISDEST)
                       AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                       AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                       AND ((A.VRINIFAIXA < V_VRITEMPROD)
                       AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                       AND (CDTIPOOPER = V_CDTIPOOPER)
                       AND (A.DTINIVALID = (SELECT MAX(DTINIVALID)
                                              FROM ALIQUOTA
                                             WHERE (CDIMPOSTO = P_IMPOSTO)
                                               AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                               AND (CDEMPRESA = P_EMPRESA)
                                               AND (CDINSCESTA = RTRIM(P_INSCESTA))
                                               AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                                               AND (CDPRODUTO IS NULL)
                                               AND ((CDPAISORIG = P_PAISORIG)
                                               AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                               AND ((CDPAISDEST = P_PAISDEST)
                                               AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                               AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                               AND ((VRINIFAIXA < V_VRITEMPROD)
                                               AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                               AND (CDTIPOOPER = V_CDTIPOOPER)
                                               AND (DTINIVALID <= P_INIVALID)))) LOOP
                  SELECT COUNT(*)
                    INTO V_EXISTE
                    FROM ALIQTEMP
                   WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;

                  IF V_EXISTE > 0 THEN
                     DELETE FROM ALIQTEMP
                     WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;
                  END IF;

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

                  IF V_COUNTMENS > 0 THEN
                     V_DSMENSALIQU := NULL;
                  ELSE
                     V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                  END IF;

                  INSERT INTO ALIQTEMP
                     (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP,
                      VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP,
                      IDPMCBSCALTMP, CDTIPOOPERTMP, VR1PREDBCTMP, CDINSCESTAATMP,
                      VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ)
                  VALUES
                     (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                      C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB,
                      C_ALIQUOTA.IDPMCBASECAL, C_ALIQUOTA.CDTIPOOPER, C_ALIQUOTA.VR1PERREDBC, P_INSCESTA,
                      C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, C_ALIQUOTA.CDBENEFFISCAL,
                      C_ALIQUOTA.IDCONSDIFALIQ);

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

               END LOOP;
            END IF;
         ELSIF (V_CDTIPOOPER IS NULL) THEN /*Se nao possui aliquota por tipo de operacao*/
            IF NOT (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o maior nivel*/
               FOR C_ALIQUOTA IN (
                    SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL,
                           CDMENSAGEM, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                      FROM ALIQUOTA A, MENSALIQU M, PRODUTO P
                     WHERE (A.NRALIQUOTA = M.NRALIQUOTA(+))
                       AND (A.CDIMPOSTO = P_IMPOSTO)
                       AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                       AND (A.CDEMPRESA = P_EMPRESA)
                       AND (A.CDINSCESTA = RTRIM(P_INSCESTA))
                       AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                       AND (A.CDPRODUTO = P.CDPRODUTO(+))
                       AND (P.CDARVPROD = V_CDARVPROD)
                       AND ((A.CDPAISORIG = P_PAISORIG)
                       AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                       AND ((A.CDPAISDEST = P_PAISDEST)
                       AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                       AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                       AND ((A.VRINIFAIXA < V_VRITEMPROD)
                       AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                       AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                       AND (A.DTINIVALID = (SELECT MAX(DTINIVALID)
                                              FROM ALIQUOTA A, PRODUTO P
                                             WHERE (CDIMPOSTO = P_IMPOSTO)
                                               AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                               AND (CDEMPRESA = P_EMPRESA)
                                               AND (CDINSCESTA = RTRIM(P_INSCESTA))
                                               AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                                               AND (A.CDPRODUTO = P.CDPRODUTO(+))
                                               AND (P.CDARVPROD = V_CDARVPROD)
                                               AND ((CDPAISORIG = P_PAISORIG)
                                               AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                               AND ((CDPAISDEST = P_PAISDEST)
                                               AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                               AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                               AND ((VRINIFAIXA < V_VRITEMPROD)
                                               AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                               AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                                               AND (DTINIVALID <= P_INIVALID)))) LOOP
                  SELECT COUNT(*)
                    INTO V_EXISTE
                    FROM ALIQTEMP
                   WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;

                  IF V_EXISTE > 0 THEN
                     DELETE FROM ALIQTEMP
                     WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;
                  END IF;

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

                  IF V_COUNTMENS > 0 THEN
                     V_DSMENSALIQU := NULL;
                  ELSE
                     V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                  END IF;

                  INSERT INTO ALIQTEMP
                     (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP,
                      VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP,
                      IDPMCBSCALTMP, VR1PREDBCTMP, CDINSCESTAATMP, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, 
                      CDBENEFFISCAL, IDCONSDIFALIQ)
                  VALUES
                     (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                      C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB,
                      C_ALIQUOTA.IDPMCBASECAL, C_ALIQUOTA.VR1PERREDBC, P_INSCESTA, C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, 
                      C_ALIQUOTA.CDBENEFFISCAL, C_ALIQUOTA.IDCONSDIFALIQ);

                  V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
               END LOOP;
            ELSIF (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o produto "todos"*/
               FOR C_ALIQUOTA IN (
                    SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL,
                           CDMENSAGEM, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                      FROM ALIQUOTA A, MENSALIQU M
                     WHERE (A.NRALIQUOTA = M.NRALIQUOTA(+))
                       AND (A.CDIMPOSTO = P_IMPOSTO)
                       AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                       AND (A.CDEMPRESA = P_EMPRESA)
                       AND (A.CDINSCESTA = RTRIM(P_INSCESTA))
                       AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                       AND (A.CDPRODUTO IS NULL)
                       AND ((A.CDPAISORIG = P_PAISORIG)
                       AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                       AND ((A.CDPAISDEST = P_PAISDEST)
                       AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                       AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                       AND ((A.VRINIFAIXA < V_VRITEMPROD)
                       AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                       AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                       AND (A.DTINIVALID = (SELECT MAX(DTINIVALID)
                                              FROM ALIQUOTA
                                             WHERE (CDIMPOSTO = P_IMPOSTO)
                                               AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                               AND (CDEMPRESA = P_EMPRESA)
                                               AND (CDINSCESTA = RTRIM(P_INSCESTA))
                                               AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                                               AND (CDPRODUTO IS NULL)
                                               AND ((CDPAISORIG = P_PAISORIG)
                                               AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                               AND ((CDPAISDEST = P_PAISDEST)
                                               AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                               AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                               AND ((VRINIFAIXA < V_VRITEMPROD)
                                               AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                               AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                                               AND (DTINIVALID <= P_INIVALID)))) LOOP
                  SELECT COUNT(*)
                    INTO V_EXISTE
                    FROM ALIQTEMP
                   WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;

                  IF V_EXISTE > 0 THEN
                     DELETE FROM ALIQTEMP
                     WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;
                  END IF;

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

                  IF V_COUNTMENS > 0 THEN
                     V_DSMENSALIQU := NULL;
                  ELSE
                     V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                  END IF;

                  INSERT INTO ALIQTEMP
                     (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP,
                      VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP,
                      IDPMCBSCALTMP, VR1PREDBCTMP, CDINSCESTAATMP, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, 
                      CDBENEFFISCAL, IDCONSDIFALIQ)
                  VALUES
                     (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                      C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB,
                      C_ALIQUOTA.IDPMCBASECAL, C_ALIQUOTA.VR1PERREDBC, P_INSCESTA, C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, 
                      C_ALIQUOTA.CDBENEFFISCAL, C_ALIQUOTA.IDCONSDIFALIQ);

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

               END LOOP;
            END IF;
         END IF;
      END IF;
      /*------------------------IMPOSTOS FEDERAIS--------------------------------------*/
      SELECT COUNT(*)
        INTO V_COUNT
        FROM ALIQTEMP
       WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;

      IF V_COUNT = 0 THEN
         IF P_CDGRUPOFISC IS NOT NULL THEN /*Se ha grupo fiscal*/
            IF V_IDPRODNOTA = w_P THEN /*Se imposto do item*/
               SELECT MAX(CDTIPOOPER), COUNT(*)
                 INTO V_CDTIPOOPER, V_CONTADOR
                 FROM ALIQUOTA A, PRODUTO P
                WHERE (CDIMPOSTO    = P_IMPOSTO)
                  AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                  AND (CDEMPRESA    = P_EMPRESA)
                  AND (CDINSCESTA   IS NULL)
                  AND (CDGRUPOFISC  = P_CDGRUPOFISC)
                  AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
                  AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
                  AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                  AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                  AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                  AND (A.CDPRODUTO = P.CDPRODUTO(+))
                  AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1);

               IF NOT (V_CDTIPOOPER IS NULL) THEN /*Se possui aliquota por tipo de operacao*/
                  SELECT MAX(P.CDARVPROD)
                    INTO V_CDARVPROD
                    FROM ALIQUOTA A, PRODUTO P
                   WHERE (CDIMPOSTO    = P_IMPOSTO)
                     AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                     AND (CDEMPRESA    = P_EMPRESA)
                     AND (CDINSCESTA   IS NULL)
                     AND (CDGRUPOFISC  = P_CDGRUPOFISC)
                     AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
                     AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
                     AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                     AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                     AND (CDTIPOOPER = V_CDTIPOOPER OR V_IDUTALIQTPOPER = w_N)
                     AND (A.CDPRODUTO = P.CDPRODUTO(+))
                     AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1);

               ELSIF (V_CDTIPOOPER IS NULL) THEN /*Se nao possui aliquota por tipo de operacao*/
                  SELECT MAX(P.CDARVPROD)
                    INTO V_CDARVPROD
                    FROM ALIQUOTA A, PRODUTO P
                   WHERE (CDIMPOSTO    = P_IMPOSTO)
                     AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                     AND (CDEMPRESA    = P_EMPRESA)
                     AND (CDINSCESTA   IS NULL)
                     AND (CDGRUPOFISC  = P_CDGRUPOFISC)
                     AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
                     AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
                     AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                     AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                     AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                     AND (A.CDPRODUTO = P.CDPRODUTO(+))
                     AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1);
               END IF;
            ELSE /*Se imposto da nota*/
               SELECT MAX(A.CDPRODUTO), COUNT(*), MAX(CDTIPOOPER)
                 INTO V_CDARVPROD, V_CONTADOR, V_CDTIPOOPER
                 FROM ALIQUOTA A
                WHERE (CDIMPOSTO    = P_IMPOSTO)
                  AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                  AND (CDEMPRESA    = P_EMPRESA)
                  AND (CDINSCESTA   IS NULL)
                  AND (CDGRUPOFISC  = P_CDGRUPOFISC)
                  AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
                  AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
                  AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                  AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                  AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                  AND (A.CDPRODUTO IS NULL);
            END IF;
            IF V_CONTADOR > 0 THEN /*Se encontrou aliquota para o grupo fiscal*/
               IF NOT (V_CDTIPOOPER IS NULL) THEN /*Se possui aliquota por tipo de operacao*/
                  IF NOT (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o produto de maior nivel*/
                     FOR C_ALIQUOTA IN (
                        SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST,
                               VRPBCSUBTRIB, IDPMCBASECAL, A.CDTIPOOPER, CDMENSAGEM, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, 
                               CDBENEFFISCAL, IDCONSDIFALIQ
                          FROM ALIQUOTA A, MENSALIQU, PRODUTO P
                         WHERE (A.NRALIQUOTA = MENSALIQU.NRALIQUOTA(+))
                           AND (A.CDIMPOSTO = P_IMPOSTO)
                           AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                           AND (A.CDEMPRESA = P_EMPRESA)
                           AND (A.CDINSCESTA IS NULL)
                           AND (A.CDGRUPOFISC = P_CDGRUPOFISC)
                           AND (A.CDPRODUTO = P.CDPRODUTO(+))
                           AND (P.CDARVPROD = V_CDARVPROD)
                           AND ((A.CDPAISORIG = P_PAISORIG)
                           AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                           AND ((A.CDPAISDEST = P_PAISDEST) AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                           AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                           AND ((A.VRINIFAIXA < V_VRITEMPROD) AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                           AND (CDTIPOOPER = V_CDTIPOOPER)
                           AND (A.DTINIVALID = ( SELECT MAX(DTINIVALID)
                                                   FROM ALIQUOTA A, PRODUTO P
                                                  WHERE (CDIMPOSTO = P_IMPOSTO)
                                                    AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                                    AND (CDEMPRESA = P_EMPRESA)
                                                    AND (CDINSCESTA IS NULL)
                                                    AND (CDGRUPOFISC = P_CDGRUPOFISC)
                                                    AND (A.CDPRODUTO = P.CDPRODUTO(+))
                                                    AND (P.CDARVPROD = V_CDARVPROD)
                                                    AND ((CDPAISORIG = P_PAISORIG)
                                                    AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                                    AND ((CDPAISDEST = P_PAISDEST) AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                                    AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                                    AND ((VRINIFAIXA < V_VRITEMPROD) AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                                    AND (CDTIPOOPER = V_CDTIPOOPER)
                                                    AND (DTINIVALID <= P_INIVALID)))) LOOP

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

                        IF V_COUNTMENS > 0 THEN
                           V_DSMENSALIQU := NULL;
                        ELSE
                           V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                        END IF;

                        INSERT INTO ALIQTEMP
                           (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP, VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP, IDPMCBSCALTMP, CDTIPOOPERTMP, CDINSCESTAATMP,
                            VR1PREDBCTMP, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ)
                        VALUES
                           (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                            C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB, C_ALIQUOTA.IDPMCBASECAL, C_ALIQUOTA.CDTIPOOPER, NULL,
                            C_ALIQUOTA.VR1PERREDBC, C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, C_ALIQUOTA.CDBENEFFISCAL,
                            C_ALIQUOTA.IDCONSDIFALIQ);

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

                     END LOOP;
                  ELSIF (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o produto "todos"*/
                     FOR C_ALIQUOTA IN (
                        SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL, A.CDTIPOOPER,
                               CDMENSAGEM, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                          FROM ALIQUOTA A, MENSALIQU
                         WHERE (A.NRALIQUOTA = MENSALIQU.NRALIQUOTA(+))
                           AND (A.CDIMPOSTO = P_IMPOSTO)
                           AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                           AND (A.CDEMPRESA = P_EMPRESA)
                           AND (A.CDINSCESTA IS NULL)
                           AND (A.CDGRUPOFISC = P_CDGRUPOFISC)
                           AND (A.CDPRODUTO IS NULL)
                           AND ((A.CDPAISORIG = P_PAISORIG)
                           AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                           AND ((A.CDPAISDEST = P_PAISDEST) AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                           AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                           AND ((A.VRINIFAIXA < V_VRITEMPROD) AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                           AND (CDTIPOOPER = V_CDTIPOOPER)
                           AND (A.DTINIVALID = ( SELECT MAX(DTINIVALID)
                                                   FROM ALIQUOTA
                                                  WHERE (CDIMPOSTO = P_IMPOSTO)
                                                    AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                                    AND (CDEMPRESA = P_EMPRESA)
                                                    AND (CDINSCESTA IS NULL)
                                                    AND (CDGRUPOFISC = P_CDGRUPOFISC)
                                                    AND (CDPRODUTO IS NULL)
                                                    AND ((CDPAISORIG = P_PAISORIG)
                                                    AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                                    AND ((CDPAISDEST = P_PAISDEST)
                                                    AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                                    AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                                    AND ((VRINIFAIXA < V_VRITEMPROD)
                                                    AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                                    AND (CDTIPOOPER = V_CDTIPOOPER)
                                                    AND (DTINIVALID <= P_INIVALID)))) LOOP
                        SELECT COUNT(*)
                          INTO V_COUNTMENS
                          FROM MENSAGEM
                         WHERE CDMENSAGEM = C_ALIQUOTA.CDMENSAGEM
                           AND IDSITUACAOMENS = 'I';

                        IF V_COUNTMENS > 0 THEN
                           V_DSMENSALIQU := NULL;
                        ELSE
                           V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                        END IF;

                        INSERT INTO ALIQTEMP
                           (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP, VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP, IDPMCBSCALTMP, CDTIPOOPERTMP, CDINSCESTAATMP,
                            VR1PREDBCTMP, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ)
                        VALUES
                           (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                            C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB, C_ALIQUOTA.IDPMCBASECAL, C_ALIQUOTA.CDTIPOOPER, NULL,
                            C_ALIQUOTA.VR1PERREDBC, C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, C_ALIQUOTA.CDBENEFFISCAL,
                            C_ALIQUOTA.IDCONSDIFALIQ);

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

                     END LOOP;
                  END IF;
               ELSIF (V_CDTIPOOPER IS NULL) THEN /*Se nao possui aliquota por tipo de operacao*/
                  IF NOT (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o produto de maior nivel*/
                     FOR C_ALIQUOTA IN (
                         SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL, CDMENSAGEM,
                                VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                           FROM ALIQUOTA A, MENSALIQU, PRODUTO P
                          WHERE (A.NRALIQUOTA = MENSALIQU.NRALIQUOTA(+))
                            AND (A.CDIMPOSTO = P_IMPOSTO)
                            AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                            AND (A.CDEMPRESA = P_EMPRESA)
                            AND (A.CDINSCESTA IS NULL)
                            AND (A.CDGRUPOFISC = P_CDGRUPOFISC)
                            AND (A.CDPRODUTO = P.CDPRODUTO(+))
                            AND (P.CDARVPROD = V_CDARVPROD)
                            AND ((A.CDPAISORIG = P_PAISORIG)
                            AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                            AND ((A.CDPAISDEST = P_PAISDEST) AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                            AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                            AND ((A.VRINIFAIXA < V_VRITEMPROD) AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                            AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                            AND (A.DTINIVALID = ( SELECT MAX(DTINIVALID)
                                                    FROM ALIQUOTA A, PRODUTO P
                                                   WHERE (CDIMPOSTO = P_IMPOSTO)
                                                     AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                                     AND (CDEMPRESA = P_EMPRESA)
                                                     AND (CDINSCESTA IS NULL)
                                                     AND (CDGRUPOFISC = P_CDGRUPOFISC)
                                                     AND (A.CDPRODUTO = P.CDPRODUTO(+))
                                                     AND (P.CDARVPROD = V_CDARVPROD)
                                                     AND ((CDPAISORIG = P_PAISORIG)
                                                     AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                                     AND ((CDPAISDEST = P_PAISDEST) AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                                     AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                                     AND ((VRINIFAIXA < V_VRITEMPROD) AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                                     AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                                                     AND (DTINIVALID <= P_INIVALID)))) LOOP

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

                        IF V_COUNTMENS > 0 THEN
                           V_DSMENSALIQU := NULL;
                        ELSE
                           V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                        END IF;

                        INSERT INTO ALIQTEMP
                           (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP, VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP, IDPMCBSCALTMP, CDINSCESTAATMP,
                            VR1PREDBCTMP, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ)
                        VALUES
                           (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                            C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB, C_ALIQUOTA.IDPMCBASECAL, NULL,
                            C_ALIQUOTA.VR1PERREDBC, C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, C_ALIQUOTA.CDBENEFFISCAL,
                            C_ALIQUOTA.IDCONSDIFALIQ);

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

                     END LOOP;
                  ELSIF (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o produto "todos"*/
                     FOR C_ALIQUOTA IN (
                        SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL, CDMENSAGEM,
                               VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                          FROM ALIQUOTA A, MENSALIQU
                         WHERE (A.NRALIQUOTA = MENSALIQU.NRALIQUOTA(+))
                           AND (A.CDIMPOSTO = P_IMPOSTO)
                           AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                           AND (A.CDEMPRESA = P_EMPRESA)
                           AND (A.CDINSCESTA IS NULL)
                           AND (A.CDGRUPOFISC = P_CDGRUPOFISC)
                           AND (A.CDPRODUTO IS NULL)
                           AND ((A.CDPAISORIG = P_PAISORIG)
                           AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                           AND ((A.CDPAISDEST = P_PAISDEST) AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                           AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                           AND ((A.VRINIFAIXA < V_VRITEMPROD) AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                           AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                           AND (A.DTINIVALID = ( SELECT MAX(DTINIVALID)
                                                   FROM ALIQUOTA
                                                  WHERE (CDIMPOSTO = P_IMPOSTO)
                                                    AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                                    AND (CDEMPRESA = P_EMPRESA)
                                                    AND (CDINSCESTA IS NULL)
                                                    AND (CDGRUPOFISC = P_CDGRUPOFISC)
                                                    AND (CDPRODUTO IS NULL)
                                                    AND ((CDPAISORIG = P_PAISORIG)
                                                    AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                                    AND ((CDPAISDEST = P_PAISDEST)
                                                    AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                                    AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                                    AND ((VRINIFAIXA < V_VRITEMPROD)
                                                    AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                                    AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                                                    AND (DTINIVALID <= P_INIVALID)))) LOOP

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

                        IF V_COUNTMENS > 0 THEN
                           V_DSMENSALIQU := NULL;
                        ELSE
                           V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                        END IF;

                        INSERT INTO ALIQTEMP
                           (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP, VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP, IDPMCBSCALTMP, CDINSCESTAATMP,
                            VR1PREDBCTMP, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ)
                        VALUES
                           (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                            C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB, C_ALIQUOTA.IDPMCBASECAL, NULL,
                            C_ALIQUOTA.VR1PERREDBC, C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, C_ALIQUOTA.CDBENEFFISCAL,
                            C_ALIQUOTA.IDCONSDIFALIQ);

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

                     END LOOP;
                  END IF;
               END IF;
            END IF;
         ELSE /*Se nao ha grupo fiscal*/
            SELECT MAX(A.CDTIPOOPER)
              INTO V_CDTIPOOPER
              FROM ALIQUOTA A, PRODUTO P
             WHERE (CDIMPOSTO    = P_IMPOSTO)
               AND (IDENTRSAIDAL = P_ENTRSAIDAL)
               AND (CDEMPRESA    = P_EMPRESA)
               AND (CDINSCESTA   IS NULL)
               AND (CDGRUPOFISC IS NULL)
               AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
               AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
               AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
               AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
               AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
               AND (A.CDPRODUTO = P.CDPRODUTO(+))
               AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1);

            IF NOT (V_CDTIPOOPER IS NULL) THEN /*Se possui aliquota por tipo de operacao*/
               SELECT MAX(P.CDARVPROD)
                 INTO V_CDARVPROD
                 FROM ALIQUOTA A, PRODUTO P
                WHERE (CDIMPOSTO    = P_IMPOSTO)
                  AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                  AND (CDEMPRESA    = P_EMPRESA)
                  AND (CDINSCESTA   IS NULL)
                  AND (CDGRUPOFISC IS NULL)
                  AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
                  AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
                  AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                  AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                  AND (CDTIPOOPER = V_CDTIPOOPER OR V_IDUTALIQTPOPER = w_N)
                  AND (A.CDPRODUTO = P.CDPRODUTO(+))
                  AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1);

            ELSIF (V_CDTIPOOPER IS NULL) THEN /*Se nao possui aliquota por tipo de operacao*/
               SELECT MAX(P.CDARVPROD)
                 INTO V_CDARVPROD
                 FROM ALIQUOTA A, PRODUTO P
                WHERE (CDIMPOSTO    = P_IMPOSTO)
                  AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                  AND (CDEMPRESA    = P_EMPRESA)
                  AND (CDINSCESTA   IS NULL)
                  AND (CDGRUPOFISC IS NULL)
                  AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
                  AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
                  AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                  AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                  AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                  AND (A.CDPRODUTO = P.CDPRODUTO(+))
                  AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1);
            END IF;

            IF NOT (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o maior nivel*/
               IF NOT (V_CDTIPOOPER IS NULL) THEN /*Se possui aliquota por tipo de operacao*/
                  FOR C_ALIQUOTA IN (
                     SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL, A.CDTIPOOPER,
                            CDMENSAGEM, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                       FROM ALIQUOTA A, MENSALIQU, PRODUTO P
                      WHERE (A.NRALIQUOTA = MENSALIQU.NRALIQUOTA(+))
                        AND (A.CDIMPOSTO = P_IMPOSTO)
                        AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                        AND (A.CDEMPRESA = P_EMPRESA)
                        AND (A.CDINSCESTA IS NULL)
                        AND (A.CDGRUPOFISC IS NULL)
                        AND (A.CDPRODUTO = P.CDPRODUTO(+))
                        AND (P.CDARVPROD = V_CDARVPROD)
                        AND ((A.CDPAISORIG = P_PAISORIG)
                        AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                        AND ((A.CDPAISDEST = P_PAISDEST)
                        AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                        AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                        AND ((A.VRINIFAIXA < V_VRITEMPROD)
                        AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                        AND (CDTIPOOPER = V_CDTIPOOPER)
                        AND (A.DTINIVALID = ( SELECT MAX(DTINIVALID)
                                                FROM ALIQUOTA A, PRODUTO P
                                               WHERE (CDIMPOSTO = P_IMPOSTO)
                                                 AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                                 AND (CDEMPRESA = P_EMPRESA)
                                                 AND (CDINSCESTA IS NULL)
                                                 AND (CDGRUPOFISC IS NULL)
                                                 AND (A.CDPRODUTO = P.CDPRODUTO(+))
                                                 AND (P.CDARVPROD = V_CDARVPROD)
                                                 AND ((CDPAISORIG = P_PAISORIG)
                                                 AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                                 AND ((CDPAISDEST = P_PAISDEST)
                                                 AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                                 AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                                 AND ((VRINIFAIXA < V_VRITEMPROD)
                                                 AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                                 AND (CDTIPOOPER = V_CDTIPOOPER)
                                                 AND (DTINIVALID <= P_INIVALID)))) LOOP

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

                     IF V_COUNTMENS > 0 THEN
                        V_DSMENSALIQU := NULL;
                     ELSE
                        V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                     END IF;

                     INSERT INTO ALIQTEMP
                       (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP, VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP, IDPMCBSCALTMP, CDTIPOOPERTMP, CDINSCESTAATMP,
                        VR1PREDBCTMP, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ)
                     VALUES
                       (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                        C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB, C_ALIQUOTA.IDPMCBASECAL, C_ALIQUOTA.CDTIPOOPER, NULL,
                        C_ALIQUOTA.VR1PERREDBC, C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, C_ALIQUOTA.CDBENEFFISCAL,
                        C_ALIQUOTA.IDCONSDIFALIQ);

                     V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
                  END LOOP;
               ELSIF (V_CDTIPOOPER IS NULL) THEN /*Se nao possui aliquota por tipo de operacao*/
                  FOR C_ALIQUOTA IN (
                      SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL,
                             CDMENSAGEM, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                        FROM ALIQUOTA A, MENSALIQU, PRODUTO P
                       WHERE (A.NRALIQUOTA = MENSALIQU.NRALIQUOTA(+))
                         AND (A.CDIMPOSTO = P_IMPOSTO)
                         AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                         AND (A.CDEMPRESA = P_EMPRESA)
                         AND (A.CDINSCESTA IS NULL)
                         AND (A.CDGRUPOFISC IS NULL)
                         AND (A.CDPRODUTO = P.CDPRODUTO(+))
                         AND (P.CDARVPROD = V_CDARVPROD)
                         AND ((A.CDPAISORIG = P_PAISORIG)
                         AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                         AND ((A.CDPAISDEST = P_PAISDEST)
                         AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                         AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                         AND ((A.VRINIFAIXA < V_VRITEMPROD)
                         AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                         AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                         AND (A.DTINIVALID = ( SELECT MAX(DTINIVALID)
                                                 FROM ALIQUOTA A, PRODUTO P
                                                WHERE (CDIMPOSTO = P_IMPOSTO)
                                                  AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                                  AND (CDEMPRESA = P_EMPRESA)
                                                  AND (CDINSCESTA IS NULL)
                                                  AND (CDGRUPOFISC IS NULL)
                                                  AND (A.CDPRODUTO = P.CDPRODUTO(+))
                                                  AND (P.CDARVPROD = V_CDARVPROD)
                                                  AND ((CDPAISORIG = P_PAISORIG)
                                                  AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                                  AND ((CDPAISDEST = P_PAISDEST)
                                                  AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                                  AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                                  AND ((VRINIFAIXA < V_VRITEMPROD)
                                                  AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                                  AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                                                  AND (DTINIVALID <= P_INIVALID)))) LOOP
                     SELECT COUNT(*)
                       INTO V_COUNTMENS
                       FROM MENSAGEM
                      WHERE CDMENSAGEM = C_ALIQUOTA.CDMENSAGEM
                        AND IDSITUACAOMENS = 'I';

                     IF V_COUNTMENS > 0 THEN
                        V_DSMENSALIQU := NULL;
                     ELSE
                        V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                     END IF;

                     INSERT INTO ALIQTEMP
                       (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP, VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP, IDPMCBSCALTMP, CDINSCESTAATMP,
                        VR1PREDBCTMP, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ)
                     VALUES
                       (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                        C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB, C_ALIQUOTA.IDPMCBASECAL, NULL,
                        C_ALIQUOTA.VR1PERREDBC, C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, C_ALIQUOTA.CDBENEFFISCAL,
                        C_ALIQUOTA.IDCONSDIFALIQ);

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

                  END LOOP;
               END IF;
            ELSIF (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para "todos"*/
               IF NOT (V_CDTIPOOPER IS NULL) THEN /*Se ha aliquota por tipo de operacao*/
                  FOR C_ALIQUOTA IN (
                     SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL, A.CDTIPOOPER,
                            CDMENSAGEM, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                       FROM ALIQUOTA A, MENSALIQU
                      WHERE (A.NRALIQUOTA = MENSALIQU.NRALIQUOTA(+))
                        AND (A.CDIMPOSTO = P_IMPOSTO)
                        AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                        AND (A.CDEMPRESA = P_EMPRESA)
                        AND (A.CDINSCESTA IS NULL)
                        AND (A.CDGRUPOFISC IS NULL)
                        AND (A.CDPRODUTO IS NULL)
                        AND ((A.CDPAISORIG = P_PAISORIG)
                        AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                        AND ((A.CDPAISDEST = P_PAISDEST)
                        AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                        AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                        AND ((A.VRINIFAIXA < V_VRITEMPROD)
                        AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                        AND (CDTIPOOPER = V_CDTIPOOPER)
                        AND (A.DTINIVALID = ( SELECT MAX(DTINIVALID)
                                                FROM ALIQUOTA
                                               WHERE (CDIMPOSTO = P_IMPOSTO)
                                                 AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                                 AND (CDEMPRESA = P_EMPRESA)
                                                 AND (CDINSCESTA IS NULL)
                                                 AND (CDGRUPOFISC IS NULL)
                                                 AND (CDPRODUTO IS NULL)
                                                 AND ((CDPAISORIG = P_PAISORIG)
                                                 AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                                 AND ((CDPAISDEST = P_PAISDEST)
                                                 AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                                 AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                                 AND ((VRINIFAIXA < V_VRITEMPROD)
                                                 AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                                 AND (CDTIPOOPER = V_CDTIPOOPER)
                                                 AND (DTINIVALID <= P_INIVALID)))) LOOP
                     SELECT COUNT(*)
                       INTO V_COUNTMENS
                       FROM MENSAGEM
                      WHERE CDMENSAGEM = C_ALIQUOTA.CDMENSAGEM
                        AND IDSITUACAOMENS = 'I';

                     IF V_COUNTMENS > 0 THEN
                        V_DSMENSALIQU := NULL;
                     ELSE
                        V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                     END IF;

                     INSERT INTO ALIQTEMP
                        (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP, VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP, IDPMCBSCALTMP, CDTIPOOPERTMP, CDINSCESTAATMP,
                         VR1PREDBCTMP, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ)
                     VALUES
                        (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                         C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB, C_ALIQUOTA.IDPMCBASECAL, C_ALIQUOTA.CDTIPOOPER, NULL,
                         C_ALIQUOTA.VR1PERREDBC, C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, C_ALIQUOTA.CDBENEFFISCAL,
                         C_ALIQUOTA.IDCONSDIFALIQ);

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

                  END LOOP;
               ELSIF (V_CDTIPOOPER IS NULL) THEN /*Se nao possui aliquota por tipo de operacao*/
                  FOR C_ALIQUOTA IN (
                     SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL,
                            CDMENSAGEM, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                       FROM ALIQUOTA A, MENSALIQU
                      WHERE (A.NRALIQUOTA = MENSALIQU.NRALIQUOTA(+))
                        AND (A.CDIMPOSTO = P_IMPOSTO)
                        AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                        AND (A.CDEMPRESA = P_EMPRESA)
                        AND (A.CDINSCESTA IS NULL)
                        AND (A.CDGRUPOFISC IS NULL)
                        AND (A.CDPRODUTO IS NULL)
                        AND ((A.CDPAISORIG = P_PAISORIG)
                        AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                        AND ((A.CDPAISDEST = P_PAISDEST)
                        AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                        AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                        AND ((A.VRINIFAIXA < V_VRITEMPROD)
                        AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                        AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                        AND (A.DTINIVALID = ( SELECT MAX(DTINIVALID)
                                                FROM ALIQUOTA
                                               WHERE (CDIMPOSTO = P_IMPOSTO)
                                                 AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                                 AND (CDEMPRESA = P_EMPRESA)
                                                 AND (CDINSCESTA IS NULL)
                                                 AND (CDGRUPOFISC IS NULL)
                                                 AND (CDPRODUTO IS NULL)
                                                 AND ((CDPAISORIG = P_PAISORIG)
                                                 AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                                 AND ((CDPAISDEST = P_PAISDEST)
                                                 AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                                 AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                                 AND ((VRINIFAIXA < V_VRITEMPROD)
                                                 AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                                 AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                                                 AND (DTINIVALID <= P_INIVALID)))) LOOP

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

                     IF V_COUNTMENS > 0 THEN
                        V_DSMENSALIQU := NULL;
                     ELSE
                        V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                     END IF;

                     INSERT INTO ALIQTEMP
                        (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP, VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP, IDPMCBSCALTMP, CDINSCESTAATMP,
                         VR1PREDBCTMP, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ)
                     VALUES
                        (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                         C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB, C_ALIQUOTA.IDPMCBASECAL, NULL,
                         C_ALIQUOTA.VR1PERREDBC, C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, C_ALIQUOTA.CDBENEFFISCAL,
                         C_ALIQUOTA.IDCONSDIFALIQ);

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

                  END LOOP;
               END IF;
            END IF;
         END IF;

         /* Verificacao das aliquotas referente as filiais parametrizadas na tela Parametros de Grupo Fiscal (Imposto/Filial) */

         V_CDGRUPOFISC := NULL;

         BEGIN
            SELECT MAX(G.CDGRUPOFISC)
              INTO V_CDGRUPOFISC
              FROM GRUPOFISCIMPFIL G, (SELECT DECODE(P_ENTRSAIDAL, '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.CDIMPOSTO = P_IMPOSTO
               AND G.CDFILIAL = F.CDFILIAL;
         EXCEPTION
            WHEN No_Data_Found THEN
               V_CDGRUPOFISC := NULL;
         END;

         IF NOT (V_CDGRUPOFISC IS NULL) THEN /*Se ha grupo fiscal na tela Parametros de Grupo Fiscal na aba Imposto/Filial*/
            IF V_IDPRODNOTA = w_P THEN /*Se imposto do item*/
               SELECT MAX(CDTIPOOPER), COUNT(*)
                 INTO V_CDTIPOOPER, V_CONTADOR
                 FROM ALIQUOTA A, PRODUTO P
                WHERE (CDIMPOSTO    = P_IMPOSTO)
                  AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                  AND (CDEMPRESA    = P_EMPRESA)
                  AND (CDINSCESTA   IS NULL)
                  AND (CDGRUPOFISC  = V_CDGRUPOFISC)
                  AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
                  AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
                  AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                  AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                  AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                  AND (A.CDPRODUTO = P.CDPRODUTO(+))
                  AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1);

               IF NOT (V_CDTIPOOPER IS NULL) THEN /*Se possui aliquota por tipo de operacao*/
                  SELECT MAX(P.CDARVPROD)
                    INTO V_CDARVPROD
                    FROM ALIQUOTA A, PRODUTO P
                   WHERE (CDIMPOSTO    = P_IMPOSTO)
                     AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                     AND (CDEMPRESA    = P_EMPRESA)
                     AND (CDINSCESTA   IS NULL)
                     AND (CDGRUPOFISC  = V_CDGRUPOFISC)
                     AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
                     AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
                     AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                     AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                     AND (CDTIPOOPER = V_CDTIPOOPER OR V_IDUTALIQTPOPER = w_N)
                     AND (A.CDPRODUTO = P.CDPRODUTO(+))
                     AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1);

               ELSIF (V_CDTIPOOPER IS NULL) THEN /*Se nao possui aliquota por tipo de operacao*/
                  SELECT MAX(P.CDARVPROD)
                    INTO V_CDARVPROD
                    FROM ALIQUOTA A, PRODUTO P
                   WHERE (CDIMPOSTO    = P_IMPOSTO)
                     AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                      AND (CDEMPRESA    = P_EMPRESA)
                      AND (CDINSCESTA   IS NULL)
                      AND (CDGRUPOFISC  = V_CDGRUPOFISC)
                      AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
                      AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
                      AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                      AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                      AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                      AND (A.CDPRODUTO = P.CDPRODUTO(+))
                      AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1);
               END IF;
            ELSE /*Se imposto da nota*/
               SELECT MAX(A.CDPRODUTO), COUNT(*), MAX(CDTIPOOPER)
                 INTO V_CDARVPROD, V_CONTADOR, V_CDTIPOOPER
                 FROM ALIQUOTA A
                WHERE (CDIMPOSTO    = P_IMPOSTO)
                  AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                  AND (CDEMPRESA    = P_EMPRESA)
                  AND (CDINSCESTA   IS NULL)
                  AND (CDGRUPOFISC  = V_CDGRUPOFISC)
                  AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
                  AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
                  AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                  AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                  AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                  AND (A.CDPRODUTO IS NULL);
            END IF;
            IF NOT (V_CDTIPOOPER IS NULL) THEN /*Se possui aliquota por tipo de operacao*/
               IF NOT (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o maior nivel*/
                  FOR C_ALIQUOTA IN (
                       SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL, CDTIPOOPER,
                              CDMENSAGEM, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                         FROM ALIQUOTA A, MENSALIQU M, PRODUTO P
                        WHERE (A.NRALIQUOTA = M.NRALIQUOTA(+))
                          AND (A.CDIMPOSTO = P_IMPOSTO)
                          AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                          AND (A.CDEMPRESA = P_EMPRESA)
                          AND (A.CDINSCESTA IS NULL)
                          AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                          AND (A.CDPRODUTO = P.CDPRODUTO(+))
                          AND (P.CDARVPROD = V_CDARVPROD)
                          AND ((A.CDPAISORIG = P_PAISORIG)
                          AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                          AND ((A.CDPAISDEST = P_PAISDEST)
                          AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                          AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                          AND ((A.VRINIFAIXA < V_VRITEMPROD)
                          AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                          AND (CDTIPOOPER = V_CDTIPOOPER)
                          AND (A.DTINIVALID = (SELECT MAX(DTINIVALID)
                                                 FROM ALIQUOTA A, PRODUTO P
                                                WHERE (CDIMPOSTO = P_IMPOSTO)
                                                  AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                                  AND (CDEMPRESA = P_EMPRESA)
                                                  AND (CDINSCESTA IS NULL)
                                                  AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                                                  AND (A.CDPRODUTO = P.CDPRODUTO(+))
                                                  AND (P.CDARVPROD = V_CDARVPROD)
                                                  AND ((CDPAISORIG = P_PAISORIG)
                                                  AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                                  AND ((CDPAISDEST = P_PAISDEST)
                                                  AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                                  AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                                  AND ((VRINIFAIXA < V_VRITEMPROD)
                                                  AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                                  AND (CDTIPOOPER = V_CDTIPOOPER)
                                                  AND (DTINIVALID <= P_INIVALID)))) LOOP
                     SELECT COUNT(*)
                       INTO V_EXISTE
                       FROM ALIQTEMP
                      WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;

                     IF V_EXISTE > 0 THEN
                        DELETE FROM ALIQTEMP
                        WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;
                     END IF;

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

                     IF V_COUNTMENS > 0 THEN
                        V_DSMENSALIQU := NULL;
                     ELSE
                        V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                     END IF;

                     INSERT INTO ALIQTEMP
                        (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP, VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP, IDPMCBSCALTMP, CDTIPOOPERTMP, CDINSCESTAATMP,
                         VR1PREDBCTMP, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ)
                     VALUES
                        (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                         C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB, C_ALIQUOTA.IDPMCBASECAL, C_ALIQUOTA.CDTIPOOPER, NULL,
                         C_ALIQUOTA.VR1PERREDBC, C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, C_ALIQUOTA.CDBENEFFISCAL,
                         C_ALIQUOTA.IDCONSDIFALIQ);

                     V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
                  END LOOP;
               ELSIF (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o produto "todos"*/
                  FOR C_ALIQUOTA IN (
                       SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL, A.CDTIPOOPER,
                              CDMENSAGEM, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                         FROM ALIQUOTA A, MENSALIQU M
                        WHERE (A.NRALIQUOTA = M.NRALIQUOTA(+))
                          AND (A.CDIMPOSTO = P_IMPOSTO)
                          AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                          AND (A.CDEMPRESA = P_EMPRESA)
                          AND (A.CDINSCESTA IS NULL)
                          AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                          AND (A.CDPRODUTO IS NULL)
                          AND ((A.CDPAISORIG = P_PAISORIG)
                          AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                          AND ((A.CDPAISDEST = P_PAISDEST)
                          AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                          AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                          AND ((A.VRINIFAIXA < V_VRITEMPROD)
                          AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                          AND (CDTIPOOPER = V_CDTIPOOPER)
                          AND (A.DTINIVALID = (SELECT MAX(DTINIVALID)
                                                 FROM ALIQUOTA
                                                WHERE (CDIMPOSTO = P_IMPOSTO)
                                                  AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                                  AND (CDEMPRESA = P_EMPRESA)
                                                  AND (CDINSCESTA IS NULL)
                                                  AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                                                  AND (CDPRODUTO IS NULL)
                                                  AND ((CDPAISORIG = P_PAISORIG)
                                                  AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                                  AND ((CDPAISDEST = P_PAISDEST)
                                                  AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                                  AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                                  AND ((VRINIFAIXA < V_VRITEMPROD)
                                                  AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                                  AND (CDTIPOOPER = V_CDTIPOOPER)
                                                  AND (DTINIVALID <= P_INIVALID)))) LOOP
                     SELECT COUNT(*)
                       INTO V_EXISTE
                       FROM ALIQTEMP
                      WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;

                     IF V_EXISTE > 0 THEN
                        DELETE FROM ALIQTEMP
                        WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;
                     END IF;

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

                     IF V_COUNTMENS > 0 THEN
                        V_DSMENSALIQU := NULL;
                     ELSE
                        V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                     END IF;

                     INSERT INTO ALIQTEMP
                        (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP, VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP, IDPMCBSCALTMP, CDTIPOOPERTMP, CDINSCESTAATMP,
                         VR1PREDBCTMP, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ)
                     VALUES
                        (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                         C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB, C_ALIQUOTA.IDPMCBASECAL, C_ALIQUOTA.CDTIPOOPER, NULL,
                         C_ALIQUOTA.VR1PERREDBC, C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, C_ALIQUOTA.CDBENEFFISCAL,
                         C_ALIQUOTA.IDCONSDIFALIQ);

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

                  END LOOP;
               END IF;
            ELSIF (V_CDTIPOOPER IS NULL) THEN /*Se nao possui aliquota por tipo de operacao*/
               IF NOT (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o maior nivel*/
                  FOR C_ALIQUOTA IN (
                       SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL, CDMENSAGEM,
                              VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                         FROM ALIQUOTA A, MENSALIQU M, PRODUTO P
                        WHERE (A.NRALIQUOTA = M.NRALIQUOTA(+))
                          AND (A.CDIMPOSTO = P_IMPOSTO)
                          AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                          AND (A.CDEMPRESA = P_EMPRESA)
                          AND (A.CDINSCESTA IS NULL)
                          AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                          AND (A.CDPRODUTO = P.CDPRODUTO(+))
                          AND (P.CDARVPROD = V_CDARVPROD)
                          AND ((A.CDPAISORIG = P_PAISORIG)
                          AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                          AND ((A.CDPAISDEST = P_PAISDEST)
                          AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                          AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                          AND ((A.VRINIFAIXA < V_VRITEMPROD)
                          AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                          AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                          AND (A.DTINIVALID = (SELECT MAX(DTINIVALID)
                                                 FROM ALIQUOTA A, PRODUTO P
                                                WHERE (CDIMPOSTO = P_IMPOSTO)
                                                  AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                                  AND (CDEMPRESA = P_EMPRESA)
                                                  AND (CDINSCESTA IS NULL)
                                                  AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                                                  AND (A.CDPRODUTO = P.CDPRODUTO(+))
                                                  AND (P.CDARVPROD = V_CDARVPROD)
                                                  AND ((CDPAISORIG = P_PAISORIG)
                                                  AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                                  AND ((CDPAISDEST = P_PAISDEST)
                                                  AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                                  AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                                  AND ((VRINIFAIXA < V_VRITEMPROD)
                                                  AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                                  AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                                                  AND (DTINIVALID <= P_INIVALID)))) LOOP
                     SELECT COUNT(*)
                       INTO V_EXISTE
                       FROM ALIQTEMP
                      WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;

                     IF V_EXISTE > 0 THEN
                        DELETE FROM ALIQTEMP
                        WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;
                     END IF;

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

                     IF V_COUNTMENS > 0 THEN
                        V_DSMENSALIQU := NULL;
                     ELSE
                        V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                     END IF;

                     INSERT INTO ALIQTEMP
                        (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP, VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP, IDPMCBSCALTMP, CDINSCESTAATMP,
                         VR1PREDBCTMP, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ)
                     VALUES
                        (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                         C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB, C_ALIQUOTA.IDPMCBASECAL, NULL,
                         C_ALIQUOTA.VR1PERREDBC, C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, C_ALIQUOTA.CDBENEFFISCAL,
                         C_ALIQUOTA.IDCONSDIFALIQ);

                     V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
                  END LOOP;
               ELSIF (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o produto "todos"*/
                  FOR C_ALIQUOTA IN (
                       SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL, CDMENSAGEM,
                              VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                         FROM ALIQUOTA A, MENSALIQU M
                        WHERE (A.NRALIQUOTA = M.NRALIQUOTA(+))
                          AND (A.CDIMPOSTO = P_IMPOSTO)
                          AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                          AND (A.CDEMPRESA = P_EMPRESA)
                          AND (A.CDINSCESTA IS NULL)
                          AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                          AND (A.CDPRODUTO IS NULL)
                          AND ((A.CDPAISORIG = P_PAISORIG)
                          AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                          AND ((A.CDPAISDEST = P_PAISDEST)
                          AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                          AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                          AND ((A.VRINIFAIXA < V_VRITEMPROD)
                          AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                          AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                          AND (A.DTINIVALID = (SELECT MAX(DTINIVALID)
                                                 FROM ALIQUOTA
                                                WHERE (CDIMPOSTO = P_IMPOSTO)
                                                  AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                                  AND (CDEMPRESA = P_EMPRESA)
                                                  AND (CDINSCESTA IS NULL)
                                                  AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                                                  AND (CDPRODUTO IS NULL)
                                                  AND ((CDPAISORIG = P_PAISORIG)
                                                  AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                                  AND ((CDPAISDEST = P_PAISDEST)
                                                  AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                                  AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                                  AND ((VRINIFAIXA < V_VRITEMPROD)
                                                  AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                                  AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                                                  AND (DTINIVALID <= P_INIVALID)))) LOOP
                     SELECT COUNT(*)
                       INTO V_EXISTE
                       FROM ALIQTEMP
                      WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;

                     IF V_EXISTE > 0 THEN
                        DELETE FROM ALIQTEMP
                        WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;
                     END IF;

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

                     IF V_COUNTMENS > 0 THEN
                        V_DSMENSALIQU := NULL;
                     ELSE
                        V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                     END IF;

                     INSERT INTO ALIQTEMP
                        (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP, VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP, IDPMCBSCALTMP, CDINSCESTAATMP,
                         VR1PREDBCTMP, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ)
                     VALUES
                        (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                         C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB, C_ALIQUOTA.IDPMCBASECAL, NULL,
                         C_ALIQUOTA.VR1PERREDBC, C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, C_ALIQUOTA.CDBENEFFISCAL,
                         C_ALIQUOTA.IDCONSDIFALIQ);

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

                  END LOOP;
               END IF;
            END IF;
         END IF;

         ----------------------
         /* Verificacao das aliquotas referente as filiais parametrizadas na tela Parametros de Grupo Fiscal (Imposto/Inscricao Estadual) */
         /*Por Fornecedor*/
         V_CDGRUPOFISC := NULL;

         BEGIN
            SELECT MAX(G.CDGRUPOFISC)
              INTO V_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) F
             WHERE G.CDIMPOSTO = P_IMPOSTO
               AND G.CDFORNECED = F.CDFORNECED
               AND G.CDEMPRESA = P_EMPRESA
               AND G.CDINSCESTA = P_INSCESTA;
         EXCEPTION
             WHEN No_Data_Found THEN
                V_CDGRUPOFISC := NULL;
          END;

         IF NOT (V_CDGRUPOFISC IS NULL) THEN /*Se ha grupo fiscal na tela Parametros de Grupo Fiscal na aba Imposto/Inscricao Estadual*/
            IF V_IDPRODNOTA = w_P THEN /*Se imposto do item*/
               SELECT MAX(CDTIPOOPER), COUNT(*)
                 INTO V_CDTIPOOPER, V_CONTADOR
                 FROM ALIQUOTA A, PRODUTO P
                WHERE (CDIMPOSTO    = P_IMPOSTO)
                  AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                  AND (CDEMPRESA    = P_EMPRESA)
                  AND (CDINSCESTA   IS NULL)
                  AND (CDGRUPOFISC  = V_CDGRUPOFISC)
                  AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
                  AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
                  AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                  AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                  AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                  AND (A.CDPRODUTO = P.CDPRODUTO(+))
                  AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1);

               IF NOT (V_CDTIPOOPER IS NULL) THEN /*Se possui aliquota por tipo de operacao*/
                  SELECT MAX(P.CDARVPROD)
                    INTO V_CDARVPROD
                    FROM ALIQUOTA A, PRODUTO P
                   WHERE (CDIMPOSTO    = P_IMPOSTO)
                     AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                     AND (CDEMPRESA    = P_EMPRESA)
                     AND (CDINSCESTA   IS NULL)
                     AND (CDGRUPOFISC  = V_CDGRUPOFISC)
                     AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
                     AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
                     AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                     AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                     AND (CDTIPOOPER = V_CDTIPOOPER OR V_IDUTALIQTPOPER = w_N)
                     AND (A.CDPRODUTO = P.CDPRODUTO(+))
                     AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1);

               ELSIF (V_CDTIPOOPER IS NULL) THEN /*Se nao possui aliquota por tipo de operacao*/
                  SELECT MAX(P.CDARVPROD)
                    INTO V_CDARVPROD
                    FROM ALIQUOTA A, PRODUTO P
                   WHERE (CDIMPOSTO    = P_IMPOSTO)
                     AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                     AND (CDEMPRESA    = P_EMPRESA)
                     AND (CDINSCESTA   IS NULL)
                     AND (CDGRUPOFISC  = V_CDGRUPOFISC)
                     AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
                     AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
                     AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                     AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                     AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                     AND (A.CDPRODUTO = P.CDPRODUTO(+))
                     AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1);
               END IF;
            ELSE /*Se imposto da nota*/
               SELECT MAX(A.CDPRODUTO), COUNT(*), MAX(CDTIPOOPER)
                 INTO V_CDARVPROD, V_CONTADOR, V_CDTIPOOPER
                 FROM ALIQUOTA A
                WHERE (CDIMPOSTO    = P_IMPOSTO)
                  AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                  AND (CDEMPRESA    = P_EMPRESA)
                  AND (CDINSCESTA   IS NULL)
                  AND (CDGRUPOFISC  = V_CDGRUPOFISC)
                  AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
                  AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
                  AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                  AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                  AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                  AND (A.CDPRODUTO IS NULL);
            END IF;

            IF NOT (V_CDTIPOOPER IS NULL) THEN /*Se possui aliquota por tipo de operacao*/
               IF NOT (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o maior nivel*/
                  FOR C_ALIQUOTA IN (
                       SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL, CDTIPOOPER,
                              CDMENSAGEM, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                         FROM ALIQUOTA A, MENSALIQU M, PRODUTO P
                        WHERE (A.NRALIQUOTA = M.NRALIQUOTA(+))
                          AND (A.CDIMPOSTO = P_IMPOSTO)
                          AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                          AND (A.CDEMPRESA = P_EMPRESA)
                          AND (A.CDINSCESTA IS NULL)
                          AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                          AND (A.CDPRODUTO = P.CDPRODUTO(+))
                          AND (P.CDARVPROD = V_CDARVPROD)
                          AND ((A.CDPAISORIG = P_PAISORIG)
                          AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                          AND ((A.CDPAISDEST = P_PAISDEST)
                          AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                          AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                          AND ((A.VRINIFAIXA < V_VRITEMPROD)
                          AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                          AND (CDTIPOOPER = V_CDTIPOOPER)
                          AND (A.DTINIVALID = (SELECT MAX(DTINIVALID)
                                                 FROM ALIQUOTA A, PRODUTO P
                                                WHERE (CDIMPOSTO = P_IMPOSTO)
                                                  AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                                  AND (CDEMPRESA = P_EMPRESA)
                                                  AND (CDINSCESTA IS NULL)
                                                  AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                                                  AND (A.CDPRODUTO = P.CDPRODUTO(+))
                                                  AND (P.CDARVPROD = V_CDARVPROD)
                                                  AND ((CDPAISORIG = P_PAISORIG)
                                                  AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                                  AND ((CDPAISDEST = P_PAISDEST)
                                                  AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                                  AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                                  AND ((VRINIFAIXA < V_VRITEMPROD)
                                                  AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                                  AND (CDTIPOOPER = V_CDTIPOOPER)
                                                  AND (DTINIVALID <= P_INIVALID)))) LOOP

                    SELECT COUNT(*)
                      INTO V_EXISTE
                      FROM ALIQTEMP
                     WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;

                    IF V_EXISTE > 0 THEN
                       DELETE FROM ALIQTEMP
                       WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;
                    END IF;

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

                    IF V_COUNTMENS > 0 THEN
                       V_DSMENSALIQU := NULL;
                    ELSE
                       V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                    END IF;

                    INSERT INTO ALIQTEMP
                       (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP, VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP, IDPMCBSCALTMP, CDTIPOOPERTMP, CDINSCESTAATMP,
                        VR1PREDBCTMP, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ)
                    VALUES
                       (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                        C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB, C_ALIQUOTA.IDPMCBASECAL, C_ALIQUOTA.CDTIPOOPER, NULL,
                        C_ALIQUOTA.VR1PERREDBC, C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, C_ALIQUOTA.CDBENEFFISCAL,
                        C_ALIQUOTA.IDCONSDIFALIQ);

                    V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
                  END LOOP;
               ELSIF (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o produto "todos"*/
                  FOR C_ALIQUOTA IN (
                       SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL, A.CDTIPOOPER, CDMENSAGEM,
                              VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                         FROM ALIQUOTA A, MENSALIQU M
                        WHERE (A.NRALIQUOTA = M.NRALIQUOTA(+))
                          AND (A.CDIMPOSTO = P_IMPOSTO)
                          AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                          AND (A.CDEMPRESA = P_EMPRESA)
                          AND (A.CDINSCESTA IS NULL)
                          AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                          AND (A.CDPRODUTO IS NULL)
                          AND ((A.CDPAISORIG = P_PAISORIG)
                          AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                          AND ((A.CDPAISDEST = P_PAISDEST)
                          AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                          AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                          AND ((A.VRINIFAIXA < V_VRITEMPROD)
                          AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                          AND (CDTIPOOPER = V_CDTIPOOPER)
                          AND (A.DTINIVALID = (SELECT MAX(DTINIVALID)
                                                 FROM ALIQUOTA
                                                WHERE (CDIMPOSTO = P_IMPOSTO)
                                                  AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                                  AND (CDEMPRESA = P_EMPRESA)
                                                  AND (CDINSCESTA IS NULL)
                                                  AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                                                  AND (CDPRODUTO IS NULL)
                                                  AND ((CDPAISORIG = P_PAISORIG)
                                                  AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                                  AND ((CDPAISDEST = P_PAISDEST)
                                                  AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                                  AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                                  AND ((VRINIFAIXA < V_VRITEMPROD)
                                                  AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                                  AND (CDTIPOOPER = V_CDTIPOOPER)
                                                  AND (DTINIVALID <= P_INIVALID)))) LOOP
                     SELECT COUNT(*)
                       INTO V_EXISTE
                       FROM ALIQTEMP
                      WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;

                     IF V_EXISTE > 0 THEN
                        DELETE FROM ALIQTEMP
                        WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;
                     END IF;

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

                     IF V_COUNTMENS > 0 THEN
                        V_DSMENSALIQU := NULL;
                     ELSE
                        V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                     END IF;

                     INSERT INTO ALIQTEMP
                        (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP, VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP, IDPMCBSCALTMP, CDTIPOOPERTMP, CDINSCESTAATMP,
                         VR1PREDBCTMP, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ)
                     VALUES
                        (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                         C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB, C_ALIQUOTA.IDPMCBASECAL, C_ALIQUOTA.CDTIPOOPER, NULL,
                         C_ALIQUOTA.VR1PERREDBC, C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, C_ALIQUOTA.CDBENEFFISCAL,
                         C_ALIQUOTA.IDCONSDIFALIQ);

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

                  END LOOP;
               END IF;
            ELSIF (V_CDTIPOOPER IS NULL) THEN /*Se nao possui aliquota por tipo de operacao*/
               IF NOT (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o maior nivel*/
                  FOR C_ALIQUOTA IN (
                       SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL,
                              CDMENSAGEM, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                         FROM ALIQUOTA A, MENSALIQU M, PRODUTO P
                        WHERE (A.NRALIQUOTA = M.NRALIQUOTA(+))
                          AND (A.CDIMPOSTO = P_IMPOSTO)
                          AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                          AND (A.CDEMPRESA = P_EMPRESA)
                          AND (A.CDINSCESTA IS NULL)
                          AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                          AND (A.CDPRODUTO = P.CDPRODUTO(+))
                          AND (P.CDARVPROD = V_CDARVPROD)
                          AND ((A.CDPAISORIG = P_PAISORIG)
                          AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                          AND ((A.CDPAISDEST = P_PAISDEST)
                          AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                          AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                          AND ((A.VRINIFAIXA < V_VRITEMPROD)
                          AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                          AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                          AND (A.DTINIVALID = (SELECT MAX(DTINIVALID)
                                                 FROM ALIQUOTA A, PRODUTO P
                                                WHERE (CDIMPOSTO = P_IMPOSTO)
                                                  AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                                  AND (CDEMPRESA = P_EMPRESA)
                                                  AND (CDINSCESTA IS NULL)
                                                  AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                                                  AND (A.CDPRODUTO = P.CDPRODUTO(+))
                                                  AND (P.CDARVPROD = V_CDARVPROD)
                                                  AND ((CDPAISORIG = P_PAISORIG)
                                                  AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                                  AND ((CDPAISDEST = P_PAISDEST)
                                                  AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                                  AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                                  AND ((VRINIFAIXA < V_VRITEMPROD)
                                                  AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                                  AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                                                  AND (DTINIVALID <= P_INIVALID)))) LOOP
                     SELECT COUNT(*)
                       INTO V_EXISTE
                       FROM ALIQTEMP
                      WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;

                     IF V_EXISTE > 0 THEN
                        DELETE FROM ALIQTEMP
                        WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;
                     END IF;

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

                     IF V_COUNTMENS > 0 THEN
                        V_DSMENSALIQU := NULL;
                     ELSE
                        V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                     END IF;

                     INSERT INTO ALIQTEMP
                        (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP, VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP, IDPMCBSCALTMP, CDINSCESTAATMP,
                         VR1PREDBCTMP, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ)
                     VALUES
                        (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                         C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB, C_ALIQUOTA.IDPMCBASECAL, NULL,
                         C_ALIQUOTA.VR1PERREDBC, C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, C_ALIQUOTA.CDBENEFFISCAL,
                         C_ALIQUOTA.IDCONSDIFALIQ);

                     V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
                  END LOOP;
               ELSIF (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o produto "todos"*/
                  FOR C_ALIQUOTA IN (
                       SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL, CDMENSAGEM,
                              VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                         FROM ALIQUOTA A, MENSALIQU M
                        WHERE (A.NRALIQUOTA = M.NRALIQUOTA(+))
                          AND (A.CDIMPOSTO = P_IMPOSTO)
                          AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                          AND (A.CDEMPRESA = P_EMPRESA)
                          AND (A.CDINSCESTA IS NULL)
                          AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                          AND (A.CDPRODUTO IS NULL)
                          AND ((A.CDPAISORIG = P_PAISORIG)
                          AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                          AND ((A.CDPAISDEST = P_PAISDEST)
                          AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                          AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                          AND ((A.VRINIFAIXA < V_VRITEMPROD)
                          AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                          AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                          AND (A.DTINIVALID = (SELECT MAX(DTINIVALID)
                                                 FROM ALIQUOTA
                                                WHERE (CDIMPOSTO = P_IMPOSTO)
                                                  AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                                  AND (CDEMPRESA = P_EMPRESA)
                                                  AND (CDINSCESTA IS NULL)
                                                  AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                                                  AND (CDPRODUTO IS NULL)
                                                  AND ((CDPAISORIG = P_PAISORIG)
                                                  AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                                  AND ((CDPAISDEST = P_PAISDEST)
                                                  AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                                  AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                                  AND ((VRINIFAIXA < V_VRITEMPROD)
                                                  AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                                  AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                                                  AND (DTINIVALID <= P_INIVALID)))) LOOP
                     SELECT COUNT(*)
                       INTO V_EXISTE
                       FROM ALIQTEMP
                      WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;

                     IF V_EXISTE > 0 THEN
                        DELETE FROM ALIQTEMP
                        WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;
                     END IF;

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

                     IF V_COUNTMENS > 0 THEN
                        V_DSMENSALIQU := NULL;
                     ELSE
                        V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                     END IF;

                     INSERT INTO ALIQTEMP
                        (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP, VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP, IDPMCBSCALTMP, CDINSCESTAATMP,
                         VR1PREDBCTMP, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ)
                     VALUES
                        (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                         C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB, C_ALIQUOTA.IDPMCBASECAL, NULL,
                         C_ALIQUOTA.VR1PERREDBC, C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, C_ALIQUOTA.CDBENEFFISCAL,
                         C_ALIQUOTA.IDCONSDIFALIQ);

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

                  END LOOP;
               END IF;
            END IF;
         END IF;
         /*Por Cliente*/
         V_CDGRUPOFISC := NULL;

         BEGIN
            SELECT MAX(G.CDGRUPOFISC)
              INTO V_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) F
             WHERE G.CDIMPOSTO = P_IMPOSTO
               AND G.CDCLIENTE = F.CDCLIENTE
               AND G.CDEMPRESA = P_EMPRESA
               AND G.CDINSCESTA = P_INSCESTA;
         EXCEPTION
            WHEN No_Data_Found THEN
               V_CDGRUPOFISC := NULL;
         END;

         IF NOT (V_CDGRUPOFISC IS NULL) THEN /*Se ha grupo fiscal na tela Parametros de Grupo Fiscal na aba Imposto/Inscricao Estadual*/
            IF V_IDPRODNOTA = w_P THEN /*Se imposto do item*/
               SELECT MAX(CDTIPOOPER), COUNT(*)
                 INTO V_CDTIPOOPER, V_CONTADOR
                 FROM ALIQUOTA A, PRODUTO P
                WHERE (CDIMPOSTO    = P_IMPOSTO)
                  AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                  AND (CDEMPRESA    = P_EMPRESA)
                  AND (CDINSCESTA   IS NULL)
                  AND (CDGRUPOFISC  = V_CDGRUPOFISC)
                  AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
                  AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
                  AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                  AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                  AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                  AND (A.CDPRODUTO = P.CDPRODUTO(+))
                  AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1);

               IF NOT (V_CDTIPOOPER IS NULL) THEN /*Se possui aliquota por tipo de operacao*/
                  SELECT MAX(P.CDARVPROD)
                    INTO V_CDARVPROD
                    FROM ALIQUOTA A, PRODUTO P
                   WHERE (CDIMPOSTO    = P_IMPOSTO)
                     AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                     AND (CDEMPRESA    = P_EMPRESA)
                     AND (CDINSCESTA  IS NULL)
                     AND (CDGRUPOFISC  = V_CDGRUPOFISC)
                     AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
                     AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
                     AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                     AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                     AND (CDTIPOOPER = V_CDTIPOOPER OR V_IDUTALIQTPOPER = w_N)
                     AND (A.CDPRODUTO = P.CDPRODUTO(+))
                     AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1);
               ELSIF (V_CDTIPOOPER IS NULL) THEN /*Se nao possui aliquota por tipo de operacao*/
                  SELECT MAX(P.CDARVPROD)
                    INTO V_CDARVPROD
                    FROM ALIQUOTA A, PRODUTO P
                   WHERE (CDIMPOSTO    = P_IMPOSTO)
                     AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                     AND (CDEMPRESA    = P_EMPRESA)
                     AND (CDINSCESTA   IS NULL)
                     AND (CDGRUPOFISC  = V_CDGRUPOFISC)
                     AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
                     AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
                     AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                     AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                     AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                     AND (A.CDPRODUTO = P.CDPRODUTO(+))
                     AND (INSTR(NVL(P_PRODUTO,'_'),NVL(RTRIM(P.CDARVPROD),NVL(P_PRODUTO,'_'))) = 1);
               END IF;
            ELSE /*Se imposto da nota*/
               SELECT MAX(A.CDPRODUTO), COUNT(*), MAX(CDTIPOOPER)
                 INTO V_CDARVPROD, V_CONTADOR, V_CDTIPOOPER
                 FROM ALIQUOTA A
                WHERE (CDIMPOSTO    = P_IMPOSTO)
                  AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                  AND (CDEMPRESA    = P_EMPRESA)
                  AND (CDINSCESTA   IS NULL)
                  AND (CDGRUPOFISC  = V_CDGRUPOFISC)
                  AND ((CDPAISORIG  = P_PAISORIG) AND (SGESTAORIG  = P_ESTAORIG) OR V_UTUFORIG = w_N)
                  AND ((CDPAISDEST  = P_PAISDEST) AND (SGESTADEST  = P_ESTADEST) OR V_UTUFDEST = w_N)
                  AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                  AND ((VRINIFAIXA  < V_VRITEMPROD) AND (VRFIMFAIXA  >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                  AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                  AND (A.CDPRODUTO IS NULL);
            END IF;

            IF NOT (V_CDTIPOOPER IS NULL) THEN /*Se possui aliquota por tipo de operacao*/

               IF NOT (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o maior nivel*/
                  FOR C_ALIQUOTA IN (
                       SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL, CDTIPOOPER, CDMENSAGEM,
                              VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                         FROM ALIQUOTA A, MENSALIQU M, PRODUTO P
                        WHERE (A.NRALIQUOTA = M.NRALIQUOTA(+))
                          AND (A.CDIMPOSTO = P_IMPOSTO)
                          AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                          AND (A.CDEMPRESA = P_EMPRESA)
                          AND (A.CDINSCESTA IS NULL)
                          AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                          AND (A.CDPRODUTO = P.CDPRODUTO(+))
                          AND (P.CDARVPROD = V_CDARVPROD)
                          AND ((A.CDPAISORIG = P_PAISORIG)
                          AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                          AND ((A.CDPAISDEST = P_PAISDEST)
                          AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                          AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                          AND ((A.VRINIFAIXA < V_VRITEMPROD)
                          AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                          AND (CDTIPOOPER = V_CDTIPOOPER)
                          AND (A.DTINIVALID = (SELECT MAX(DTINIVALID)
                                                 FROM ALIQUOTA A, PRODUTO P
                                                WHERE (CDIMPOSTO = P_IMPOSTO)
                                                  AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                                  AND (CDEMPRESA = P_EMPRESA)
                                                  AND (CDINSCESTA IS NULL)
                                                  AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                                                  AND (A.CDPRODUTO = P.CDPRODUTO(+))
                                                  AND (P.CDARVPROD = V_CDARVPROD)
                                                  AND ((CDPAISORIG = P_PAISORIG)
                                                  AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                                  AND ((CDPAISDEST = P_PAISDEST)
                                                  AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                                  AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                                  AND ((VRINIFAIXA < V_VRITEMPROD)
                                                  AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                                  AND (CDTIPOOPER = V_CDTIPOOPER)
                                                  AND (DTINIVALID <= P_INIVALID)))) LOOP
                     SELECT COUNT(*)
                       INTO V_EXISTE
                       FROM ALIQTEMP
                      WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;

                     IF V_EXISTE > 0 THEN
                        DELETE FROM ALIQTEMP
                        WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;
                     END IF;

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

                     IF V_COUNTMENS > 0 THEN
                        V_DSMENSALIQU := NULL;
                     ELSE
                        V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                     END IF;

                     INSERT INTO ALIQTEMP
                        (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP, VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP, IDPMCBSCALTMP, CDTIPOOPERTMP, CDINSCESTAATMP,
                         VR1PREDBCTMP, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ)
                     VALUES
                        (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                         C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB, C_ALIQUOTA.IDPMCBASECAL, C_ALIQUOTA.CDTIPOOPER, NULL,
                         C_ALIQUOTA.VR1PERREDBC, C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, C_ALIQUOTA.CDBENEFFISCAL,
                         C_ALIQUOTA.IDCONSDIFALIQ);

                     V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
                  END LOOP;
               ELSIF (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o produto "todos"*/
                  FOR C_ALIQUOTA IN (
                       SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL, A.CDTIPOOPER, CDMENSAGEM,
                              VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                         FROM ALIQUOTA A, MENSALIQU M
                        WHERE (A.NRALIQUOTA = M.NRALIQUOTA(+))
                          AND (A.CDIMPOSTO = P_IMPOSTO)
                          AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                          AND (A.CDEMPRESA = P_EMPRESA)
                          AND (A.CDINSCESTA IS NULL)
                          AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                          AND (A.CDPRODUTO IS NULL)
                          AND ((A.CDPAISORIG = P_PAISORIG)
                          AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                          AND ((A.CDPAISDEST = P_PAISDEST)
                          AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                          AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                          AND ((A.VRINIFAIXA < V_VRITEMPROD)
                          AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                          AND (CDTIPOOPER = V_CDTIPOOPER)
                          AND (A.DTINIVALID = (SELECT MAX(DTINIVALID)
                                                 FROM ALIQUOTA
                                                WHERE (CDIMPOSTO = P_IMPOSTO)
                                                  AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                                  AND (CDEMPRESA = P_EMPRESA)
                                                  AND (CDINSCESTA IS NULL)
                                                  AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                                                  AND (CDPRODUTO IS NULL)
                                                  AND ((CDPAISORIG = P_PAISORIG)
                                                  AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                                  AND ((CDPAISDEST = P_PAISDEST)
                                                  AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                                  AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                                  AND ((VRINIFAIXA < V_VRITEMPROD)
                                                  AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                                  AND (CDTIPOOPER = V_CDTIPOOPER)
                                                  AND (DTINIVALID <= P_INIVALID)))) LOOP
                     SELECT COUNT(*)
                       INTO V_EXISTE
                       FROM ALIQTEMP
                      WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;

                     IF V_EXISTE > 0 THEN
                        DELETE FROM ALIQTEMP
                        WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;
                     END IF;

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

                     IF V_COUNTMENS > 0 THEN
                        V_DSMENSALIQU := NULL;
                     ELSE
                        V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                     END IF;

                     INSERT INTO ALIQTEMP
                        (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP, VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP, IDPMCBSCALTMP, CDTIPOOPERTMP, CDINSCESTAATMP,
                         VR1PREDBCTMP, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ)
                     VALUES
                        (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                         C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB, C_ALIQUOTA.IDPMCBASECAL, C_ALIQUOTA.CDTIPOOPER, NULL,
                         C_ALIQUOTA.VR1PERREDBC, C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, C_ALIQUOTA.CDBENEFFISCAL,
                         C_ALIQUOTA.IDCONSDIFALIQ);

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

                  END LOOP;
               END IF;
            ELSIF (V_CDTIPOOPER IS NULL) THEN /*Se nao possui aliquota por tipo de operacao*/
               IF NOT (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o maior nivel*/
                  FOR C_ALIQUOTA IN (
                       SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL,
                              CDMENSAGEM, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                         FROM ALIQUOTA A, MENSALIQU M, PRODUTO P
                        WHERE (A.NRALIQUOTA = M.NRALIQUOTA(+))
                          AND (A.CDIMPOSTO = P_IMPOSTO)
                          AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                          AND (A.CDEMPRESA = P_EMPRESA)
                          AND (A.CDINSCESTA IS NULL)
                          AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                          AND (A.CDPRODUTO = P.CDPRODUTO(+))
                          AND (P.CDARVPROD = V_CDARVPROD)
                          AND ((A.CDPAISORIG = P_PAISORIG)
                          AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                          AND ((A.CDPAISDEST = P_PAISDEST)
                          AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                          AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                          AND ((A.VRINIFAIXA < V_VRITEMPROD)
                          AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                          AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                          AND (A.DTINIVALID = (SELECT MAX(DTINIVALID)
                                                 FROM ALIQUOTA A, PRODUTO P
                                                WHERE (CDIMPOSTO = P_IMPOSTO)
                                                  AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                                  AND (CDEMPRESA = P_EMPRESA)
                                                  AND (CDINSCESTA IS NULL)
                                                  AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                                                  AND (A.CDPRODUTO = P.CDPRODUTO(+))
                                                  AND (P.CDARVPROD = V_CDARVPROD)
                                                  AND ((CDPAISORIG = P_PAISORIG)
                                                  AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                                  AND ((CDPAISDEST = P_PAISDEST)
                                                  AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                                  AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                                  AND ((VRINIFAIXA < V_VRITEMPROD)
                                                  AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                                  AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                                                  AND (DTINIVALID <= P_INIVALID)))) LOOP
                     SELECT COUNT(*)
                       INTO V_EXISTE
                       FROM ALIQTEMP
                      WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;

                     IF V_EXISTE > 0 THEN
                        DELETE FROM ALIQTEMP
                        WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;
                     END IF;

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

                     IF V_COUNTMENS > 0 THEN
                        V_DSMENSALIQU := NULL;
                     ELSE
                        V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                     END IF;

                     INSERT INTO ALIQTEMP
                        (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP, VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP, IDPMCBSCALTMP, CDINSCESTAATMP,
                         VR1PREDBCTMP, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ)
                     VALUES
                        (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                         C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB, C_ALIQUOTA.IDPMCBASECAL, NULL,
                         C_ALIQUOTA.VR1PERREDBC, C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, C_ALIQUOTA.CDBENEFFISCAL,
                         C_ALIQUOTA.IDCONSDIFALIQ);

                     V_SEQUENCIA := LPAD(RTRIM(V_SEQUENCIA + '01'),2,0);
                  END LOOP;
               ELSIF (V_CDARVPROD IS NULL) THEN /*Se ha aliquota para o produto "todos"*/
                  FOR C_ALIQUOTA IN (
                       SELECT A.NRALIQUOTA, VRPERREDBC, VR1PERREDBC, VRPARDEDUZ, VRALIQUOTA, DSMENSALIQU, IDTRATIMPST, VRPBCSUBTRIB, IDPMCBASECAL,
                              CDMENSAGEM, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ
                         FROM ALIQUOTA A, MENSALIQU M
                        WHERE (A.NRALIQUOTA = M.NRALIQUOTA(+))
                          AND (A.CDIMPOSTO = P_IMPOSTO)
                          AND (A.IDENTRSAIDAL = P_ENTRSAIDAL)
                          AND (A.CDEMPRESA = P_EMPRESA)
                          AND (A.CDINSCESTA IS NULL)
                          AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                          AND (A.CDPRODUTO IS NULL)
                          AND ((A.CDPAISORIG = P_PAISORIG)
                          AND (A.SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                          AND ((A.CDPAISDEST = P_PAISDEST)
                          AND (A.SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                          AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                          AND ((A.VRINIFAIXA < V_VRITEMPROD)
                          AND (A.VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                          AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                          AND (A.DTINIVALID = (SELECT MAX(DTINIVALID)
                                                 FROM ALIQUOTA
                                                WHERE (CDIMPOSTO = P_IMPOSTO)
                                                  AND (IDENTRSAIDAL = P_ENTRSAIDAL)
                                                  AND (CDEMPRESA = P_EMPRESA)
                                                  AND (CDINSCESTA IS NULL)
                                                  AND (A.CDGRUPOFISC = V_CDGRUPOFISC)
                                                  AND (CDPRODUTO IS NULL)
                                                  AND ((CDPAISORIG = P_PAISORIG)
                                                  AND (SGESTAORIG = P_ESTAORIG) OR V_UTUFORIG = w_N)
                                                  AND ((CDPAISDEST = P_PAISDEST)
                                                  AND (SGESTADEST = P_ESTADEST) OR V_UTUFDEST = w_N)
                                                  AND ((CDMUNIORIG   = P_MUNIORIG and V_UTMUNIORIG = w_O) OR (CDMUNIDEST   = P_MUNIORIG and V_UTMUNIORIG = w_D) OR (V_UTMUNIORIG = w_N))
                                                  AND ((VRINIFAIXA < V_VRITEMPROD)
                                                  AND (VRFIMFAIXA >= V_VRITEMPROD) OR V_UTFAIXVLR = w_N)
                                                  AND (INSTR(NVL(P_CDTIPOOPER,'_'), NVL(RTRIM(CDTIPOOPER), NVL(P_CDTIPOOPER,'_'))) = 1  OR V_IDUTALIQTPOPER = w_N)
                                                  AND (DTINIVALID <= P_INIVALID)))) LOOP
                     SELECT COUNT(*)
                       INTO V_EXISTE
                       FROM ALIQTEMP
                      WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;

                     IF V_EXISTE > 0 THEN
                        DELETE FROM ALIQTEMP
                        WHERE NRALIQTEMP = P_FILIAL||P_LANCTONF;
                     END IF;

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

                     IF V_COUNTMENS > 0 THEN
                        V_DSMENSALIQU := NULL;
                     ELSE
                        V_DSMENSALIQU := C_ALIQUOTA.DSMENSALIQU;
                     END IF;

                     INSERT INTO ALIQTEMP
                        (NRALIQTEMP, NRSEQUALIQ, VRPREDBCTMP, VRPARDEDTMP, VRALIQUTMP, DSMENSALITMP, IDTRIMPSTTMP, VRPBCSTTMP, IDPMCBSCALTMP, CDINSCESTAATMP,
                         VR1PREDBCTMP, VRADICALIQFECP, IDIMPODESONERA, IDCONSDESONVRNF, CDBENEFFISCAL, IDCONSDIFALIQ)
                     VALUES
                        (P_FILIAL||P_LANCTONF, V_SEQUENCIA, C_ALIQUOTA.VRPERREDBC, C_ALIQUOTA.VRPARDEDUZ,
                         C_ALIQUOTA.VRALIQUOTA, V_DSMENSALIQU, C_ALIQUOTA.IDTRATIMPST, C_ALIQUOTA.VRPBCSUBTRIB, C_ALIQUOTA.IDPMCBASECAL,NULL,
                         C_ALIQUOTA.VR1PERREDBC, C_ALIQUOTA.VRADICALIQFECP, C_ALIQUOTA.IDIMPODESONERA, C_ALIQUOTA.IDCONSDESONVRNF, C_ALIQUOTA.CDBENEFFISCAL,
                         C_ALIQUOTA.IDCONSDIFALIQ);

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

                  END LOOP;
               END IF;
            END IF;
         END IF;
      END IF;
   END IF;
END;
/