create or replace PROCEDURE CONSULTA_ALIQIMPOSTO(
    P_ENTRSAID    IN INCIDENCIA.IDENTRSAIDIN%TYPE,
    P_EMPRESA     IN INCIDENCIA.CDEMPRESA%TYPE,
    P_CDARVPROD   IN PRODUTO.CDARVPROD%TYPE,
    P_INSCESTA    IN INCIDENCIA.CDINSCESTA%TYPE,
    P_CDTIPOOPER  IN INCIDENCIA.CDTIPOOPER%TYPE,
    P_FILIAL      IN FILIAL.CDFILIAL%TYPE,
    P_CDFILIEMIT  IN FILIAL.CDFILIAL%TYPE,
    P_CDCLIENTE   IN CLIENTE.CDCLIENTE%TYPE,
    P_CDIMPOSTO   IN IMPOSTO.CDIMPOSTO%TYPE,
    P_VRTOTAITEM  IN ITEMPRODUTO.VRTOTAITEM%TYPE,
    P_INIVALID    IN NOTAFISCAL.DTENTRSAID%TYPE,
    P_VRALIQUOTA  OUT FLOAT)
IS
    V_IDORIGDESTNF  VARCHAR2(01);
    V_CDGRUPOFISC   VARCHAR2(03);
    V_PAISORIG      VARCHAR2(04);
    V_ESTAORIG      VARCHAR2(02);
    V_MUNIORIG      VARCHAR2(04);
    V_PAISDEST      VARCHAR2(04);
    V_ESTADEST      VARCHAR2(02);
    V_MUNIDEST      VARCHAR2(04);
    V_IDENDCFOP     VARCHAR2(01);
    V_IDENDEMIS     VARCHAR2(01);
    V_IDPRODNOTA    VARCHAR2(01);
    V_IDUTMUNICIPIO VARCHAR2(01);
    V_TAXAALIQUOTA  FLOAT;
    V_ALIQUTMP      FLOAT;
    V_PREDBCTMP     FLOAT;
    V_IDINTEREST    VARCHAR2(01);
    V_CDEMPRESA     VARCHAR2(02);
    V_CDINSCESTA    VARCHAR2(16);
    V_VRADICALIQFECP FLOAT;
BEGIN
   DELETE INCITEMP
    WHERE NRINCITEMP = P_FILIAL||'0000000000';

   DELETE FROM ALIQTEMPPROD
    WHERE NRALIQTEMP = P_FILIAL||'0000000000';

   SELECT NVL(MAX(IDENDCFOPFIL),'I')
     INTO V_IDENDCFOP
     FROM FILIAL
    WHERE (CDFILIAL = P_CDFILIEMIT);

   IF V_IDENDCFOP = 'I' THEN
      SELECT MAX(CDPAIS), MAX(SGESTADO), MAX(CDMUNICIPIO)
        INTO V_PAISORIG,  V_ESTAORIG,    V_MUNIORIG
        FROM INSCRESTAD
       WHERE CDEMPRESA  = P_EMPRESA
         AND CDINSCESTA = P_INSCESTA;
   ELSE
      SELECT MAX(CDPAIS), MAX(SGESTADO), MAX(CDMUNICIPIO)
        INTO V_PAISORIG,  V_ESTAORIG,    V_MUNIORIG
        FROM ENDEFILI
       WHERE (CDFILIAL     = P_CDFILIEMIT) AND
             (IDTPENDEFILI = V_IDENDCFOP);
   END IF;

   SELECT MAX(IDORIGDESTOP)
     INTO V_IDORIGDESTNF
     FROM TIPOOPERAC
    WHERE CDTIPOOPER   = P_CDTIPOOPER
      AND IDENTRSAIDOP = P_ENTRSAID;

   IF V_IDORIGDESTNF IS NULL THEN
      V_IDORIGDESTNF := 'C';
   END IF;

   IF V_IDORIGDESTNF = 'L' THEN
      SELECT NVL(MAX(IDENDEMISFIL),'I') , MAX(CDEMPRESA) , MAX(CDINSCESTA)
       INTO V_IDENDEMIS , V_CDEMPRESA, V_CDINSCESTA
        FROM FILIAL
       WHERE (CDFILIAL = P_CDCLIENTE);

       IF V_IDENDEMIS = 'I'  THEN
          SELECT MAX(CDPAIS)    , MAX(SGESTADO)  , MAX(CDMUNICIPIO)
            INTO V_PAISDEST, V_ESTADEST, V_MUNIDEST
            FROM INSCRESTAD
           WHERE CDEMPRESA  = V_CDEMPRESA
             AND CDINSCESTA = V_CDINSCESTA;
      ELSE
         SELECT MAX(CDPAIS), MAX(SGESTADO), MAX(CDMUNICIPIO)
           INTO V_PAISDEST,  V_ESTADEST,    V_MUNIDEST
           FROM ENDEFILI
          WHERE CDFILIAL = P_CDCLIENTE
            AND IDTPENDEFILI = V_IDENDEMIS;
      END IF;

   ELSIF V_IDORIGDESTNF = 'F' THEN
      SELECT NVL(MAX(IDENDEMISFOR),'P')
        INTO V_IDENDEMIS
        FROM FORNECEDOR
       WHERE (CDFORNECED = P_CDCLIENTE);

      SELECT MAX(CDPAIS), MAX(SGESTADO), MAX(CDMUNICIPIO)
        INTO V_PAISDEST,  V_ESTADEST,    V_MUNIDEST
        FROM ENDEFORN
       WHERE (CDFORNECED   = P_CDCLIENTE)
         AND (IDTPENDEFORN = V_IDENDEMIS);
   ELSE
      SELECT NVL(MAX(IDENDEMISCLI),'P')
        INTO V_IDENDEMIS
        FROM CLIENTE
       WHERE (CDCLIENTE = P_CDCLIENTE);

      SELECT MAX(CDPAIS), MAX(SGESTADO), MAX(CDMUNICIPIO)
        INTO V_PAISDEST,  V_ESTADEST,    V_MUNIDEST
        FROM ENDECLIE
       WHERE (CDCLIENTE = P_CDCLIENTE)
         AND (IDTPENDECLIE = V_IDENDEMIS);
   END IF;

   V_IDINTEREST := 'N';

   IF (V_PAISDEST = V_PAISORIG) AND (V_ESTADEST <> V_ESTAORIG) THEN
      V_IDINTEREST := 'S';
   END IF;

   CONSULTA_GRUPOFISCAL(V_IDORIGDESTNF, P_CDCLIENTE, '', P_ENTRSAID, P_CDTIPOOPER, V_CDGRUPOFISC);

   CONSULTA_INCIDENCIA(P_ENTRSAID, P_EMPRESA, P_CDARVPROD, P_INSCESTA, P_CDTIPOOPER, P_FILIAL, '0000000000', V_CDGRUPOFISC, NULL,
                       V_IDINTEREST, NULL, P_CDCLIENTE, NULL, NULL, P_INIVALID, P_CDFILIEMIT, P_VRTOTAITEM, 'S');

   SELECT MAX(IDPRODNOTA)
     INTO V_IDPRODNOTA
     FROM IMPOSTO
    WHERE CDIMPOSTO = P_CDIMPOSTO;

   FOR C_INCIDENCIA IN (SELECT DISTINCT (CDIMPOTEMP) AS CDIMPOTEMP, CDFISCTEMP, IDTRATIMPTMP, IDUTMUNIORIG,
                               IDCONSISTENT, IDIMPORECUP
                          FROM INCITEMP IT, IMPOSTO IP
                         WHERE (IT.NRINCITEMP = P_FILIAL||'0000000000')
                           AND (IP.CDIMPOSTO  = IT.CDIMPOTEMP)
                           AND (IP.IDPRODNOTA = V_IDPRODNOTA)
                           AND (IP.CDIMPOSTO = P_CDIMPOSTO)
                        ORDER BY CDIMPOTEMP) LOOP

      SELECT NVL(MAX(IDUTMUNIORIG), 'N')
        INTO V_IDUTMUNICIPIO
        FROM IMPOSTO
       WHERE CDIMPOSTO = C_INCIDENCIA.CDIMPOTEMP;

      IF ((V_IDUTMUNICIPIO = 'O') OR (V_IDUTMUNICIPIO = 'N')) THEN
         CONSULTA_ALIQUOTA(C_INCIDENCIA.CDIMPOTEMP, P_ENTRSAID, P_EMPRESA, P_CDARVPROD, P_INSCESTA, V_PAISORIG,
                           V_ESTAORIG, V_PAISDEST, V_ESTADEST, V_MUNIORIG, P_VRTOTAITEM, P_INIVALID, P_FILIAL, '0000000000', V_CDGRUPOFISC, P_CDTIPOOPER);
      ELSE
         CONSULTA_ALIQUOTA(C_INCIDENCIA.CDIMPOTEMP, P_ENTRSAID, P_EMPRESA, P_CDARVPROD, P_INSCESTA, V_PAISORIG,
                           V_ESTAORIG, V_PAISDEST, V_ESTADEST, V_MUNIDEST, P_VRTOTAITEM, P_INIVALID, P_FILIAL, '0000000000', V_CDGRUPOFISC, P_CDTIPOOPER);
      END IF;

      SELECT NVL(MAX(VRALIQUTMP),0), NVL(MAX(VRPREDBCTMP),0), NVL(MAX(VRADICALIQFECP), 0)
        INTO V_ALIQUTMP, V_PREDBCTMP, V_VRADICALIQFECP
        FROM ALIQTEMP
       WHERE (NRALIQTEMP = P_FILIAL||'0000000000');

      /* Inclui valores de aliquota na tabela ALIQTEMPPROD  */
      DELETE
        FROM ALIQTEMPPROD
       WHERE NRALIQTEMP = P_FILIAL||'0000000000'
         AND CDIMPOTEMP = C_INCIDENCIA.CDIMPOTEMP;

      INSERT INTO ALIQTEMPPROD
         (NRALIQTEMP, CDIMPOTEMP, VRALIQUTMP, VRADICALIQFECP)
      VALUES
         (P_FILIAL||'0000000000', C_INCIDENCIA.CDIMPOTEMP, V_ALIQUTMP, V_VRADICALIQFECP);

      IF ((C_INCIDENCIA.IDIMPORECUP = 'S') AND (C_INCIDENCIA.CDFISCTEMP = '1')) OR (C_INCIDENCIA.IDIMPORECUP <> 'S') THEN
         V_TAXAALIQUOTA := ((100-V_PREDBCTMP)/100) * ((V_ALIQUTMP + V_VRADICALIQFECP)/100);
      ELSE
         V_TAXAALIQUOTA := 0;
      END IF;

      DELETE FROM ALIQTEMP
       WHERE NRALIQTEMP = P_FILIAL||'0000000000';
   END LOOP;

   DELETE INCITEMP
    WHERE NRINCITEMP = P_FILIAL||'0000000000';

   P_VRALIQUOTA := NVL(V_TAXAALIQUOTA,0);
END;
/