create or replace PROCEDURE IMPOSTO_ITEMPEDIDO(
   P_EMPRESA      IN ALIQUOTA.CDEMPRESA%TYPE,
   P_INSCESTA     IN ALIQUOTA.CDINSCESTA%TYPE,
   P_CLIENTE      IN CLIENTE.CDCLIENTE%TYPE,
   P_FILIDEST     IN FILIAL.CDFILIAL%TYPE,
   P_FILIAL       IN FILIAL.CDFILIAL%TYPE,
   P_NRPEDIDO     IN PEDIDO.NRPEDIDO%TYPE,
   P_NRITEMPED    IN ITEMPEDIDO.NRITEMPED%TYPE,
   P_INIVALID     IN ITEMPEDIDO.DTENTITPED%TYPE,
   P_CDTIPOOPER   IN PEDIDO.CDTIPOOPER%TYPE,
   P_IDTIPOOPER   IN TIPOOPERAC.IDORIGDESTOP%TYPE,
   P_IDMOBILE     IN VARCHAR2                     DEFAULT 'N')
IS
   V_PAISORIG     VARCHAR2(04);
   V_ESTAORIG     VARCHAR2(02);
   V_MUNIDEST     VARCHAR2(04);
   V_PAISDEST     VARCHAR2(04);
   V_ESTADEST     VARCHAR2(02);
   V_ALIQTEMP     VARCHAR2(18);
   V_INCITEMP     VARCHAR2(18);
   V_CARACFIS     VARCHAR2(01);
   V_LIQUIITEM    NUMBER;
   V_PARDEDTMP    NUMBER;
   V_PREDBCTMP    NUMBER;
   V_ALIQUTMP     NUMBER;
   V_VRPBCSTTMP   NUMBER;
   V_IMPOSTOIT    NUMBER;
   V_TAXABASECALC NUMBER;
   V_TAXABCSUBSTR NUMBER;
   V_TAXAALIQUOTA NUMBER;
   V_BASECALCIT   NUMBER;
   V_CONTABCALC   NUMBER;
   V_PERDBCALC    NUMBER;
   V_ADIMPAF      NUMBER;
   V_TOTAIMPBCALC NUMBER;
   V_TOTAPRODENCA NUMBER;
   V_PRODUENCARGO NUMBER;
   V_CSTTEMP      VARCHAR2(18);
   V_CFOPTEMP     VARCHAR2(18);
   V_FISOPEITEM   VARCHAR2(11);
   V_FISOPEITEMICMS VARCHAR2(11);
   V_SITUTRIBPR   VARCHAR2(03);
   V_IDPMCBASECAL VARCHAR2(01);
   V_CDIMPOSTRIB  VARCHAR2(250);
   V_VRIMPITAFAUX NUMBER;
   V_CDGRUPOFISC VARCHAR2(3);
   V_IDTRATIMPBC   VARCHAR2(1);
   V_IDIMPORECUPBC VARCHAR2(1);
   V_IDVRIMPSTRIB VARCHAR2(01);
   V_IMPOSTOICMS  VARCHAR2(02);
   V_CTSTRIB      NUMBER;
   V_TRATIMPST    VARCHAR(1);
   V_IDINTEREST   VARCHAR2(01);
   V_IDINTERNA    VARCHAR2(01);
   V_VRFATOCONVES     FLOAT;
   V_CDGRUPOFISCAUX   VARCHAR2(03);
   V_CDSITTRIBTMP    CODSITUTRIBNFE.CDSITUTRIBNFE%TYPE;
   V_CDSITTRIBTMPICMS CODSITUTRIBNFE.CDSITUTRIBNFE%TYPE;
   V_IDMODBCNFE      CODSITUTRIBNFE.IDMODBCNFE%TYPE;
   V_IDMODBCSTNFE    CODSITUTRIBNFE.IDMODBCSTNFE%TYPE;
   V_CDTPCRPICOSTNFE CODSITUTRIBNFE.CDTPCRPICOSTNFE%TYPE;
   V_CDNATUBCPCSTNFE CODSITUTRIBNFE.CDNATUBCPCSTNFE%TYPE;
   V_IDMOTIDESSTNFE  CODSITUTRIBNFE.IDMOTIDESSTNFE%TYPE;
   V_BCMAJ            FLOAT;
   V_IMP              FLOAT;
   V_CDFISCOPERREGRA  VARCHAR2(10);
   V_CFOP1DIGT        VARCHAR2(01);
   V_VRADICALIQFECP    FLOAT(126);
   V_TOTALITEM        FLOAT;

   V_IDIMPSTCARGTRIB   VARCHAR2(01);
   V_VRPRODUTOBC       FLOAT;
   V_CDIMPOSTOICMS     VARCHAR2(02);
   V_BASECALCICMS      FLOAT;
   V_VRIMPOSTOICMS     FLOAT;
   V_UTGRUPFISCS       VARCHAR2(01);
   V_CDGRPFISCSV       VARCHAR2(03);
   V_VRALIQUTMPDEST    VARCHAR2(14);
   V_IDOPTANTESNCLIE   VARCHAR2(01);
   V_CDCNAECLIE        VARCHAR2(07);
   V_VRMVA             FLOAT;
   V_IDCONSMVAPMC      VARCHAR2(01);
   V_IDCONSCOMPBCPMC   VARCHAR2(01);
   V_IDOPERACAO        FLOAT;

   V_VRIMPOSTOITPED    NUMBER;
   V_IDENDCFOP      VARCHAR2(01);
   V_IDENDEMIS      VARCHAR2(01);
   V_CDEMPRESA      VARCHAR2(02);
   V_CDINSCESTA     VARCHAR2(16);

   V_TAXAALIQUOTAFECP  FLOAT;
   V_BCMAJFECP         FLOAT;
   V_IMPOSTOITFECP     FLOAT;
   V_IMPFECP           FLOAT;
   V_VRIMPOSTOREP      NUMBER;

   V_IDIMPODESONERA  VARCHAR2(01);
   V_IDCONSDESONVRNF VARCHAR2(01);
   V_IMPOSDESONERA NUMBER;
BEGIN
   --Variaveis com a chave para as tabelas temporarias
   --Variaveis para consulta de Aliquota, Incidencia
   V_CSTTEMP  := P_FILIAL||P_NRPEDIDO||SUBSTR(P_NRITEMPED,7,4);
   V_CFOPTEMP := P_FILIAL||P_NRPEDIDO||SUBSTR(P_NRITEMPED,7,4);
   V_ALIQTEMP := P_FILIAL||P_NRPEDIDO||SUBSTR(P_NRITEMPED,7,4);
   V_INCITEMP := P_FILIAL||P_NRPEDIDO||SUBSTR(P_NRITEMPED,7,4);

   IF P_IDTIPOOPER = 'C' THEN
      CONSULTA_GRUPOFISCAL('C', P_CLIENTE, P_FILIAL, 'S', P_CDTIPOOPER, V_CDGRUPOFISC);
   ELSIF P_IDTIPOOPER = 'L' THEN
      CONSULTA_GRUPOFISCAL('L', P_FILIDEST, P_FILIAL, 'S', P_CDTIPOOPER, V_CDGRUPOFISC);
   END IF;
---------------------------------------------------------------------------------------------------------
   SELECT MAX(CDPAIS), MAX(SGESTADO)
     INTO V_PAISORIG,  V_ESTAORIG
   FROM INSCRESTAD
   WHERE (CDEMPRESA = P_EMPRESA) AND
         (CDINSCESTA = P_INSCESTA);

   IF P_IDTIPOOPER = 'C' THEN
      BEGIN
         SELECT CDCNAECLIE,
                NVL(IDOPTANTESNCLIE,'N')
           INTO V_CDCNAECLIE,
                V_IDOPTANTESNCLIE
           FROM CLIENTE
          WHERE (CDCLIENTE = P_CLIENTE);
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_CDCNAECLIE := NULL;
            V_IDOPTANTESNCLIE := 'N';
      END;

      SELECT MAX(CDPAIS), MAX(SGESTADO), MAX(CDMUNICIPIO)
        INTO V_PAISDEST, V_ESTADEST, V_MUNIDEST
        FROM ENDECLIE
       WHERE (CDCLIENTE   = P_CLIENTE) AND
             (IDTPENDECLIE = 'P');
   ELSIF P_IDTIPOOPER = 'L' THEN
      Begin
         SELECT NVL(IDENDCFOPFIL,'I'), NVL(IDENDEMISFIL,'I'), CDEMPRESA  , CDINSCESTA
           INTO   V_IDENDCFOP          , V_IDENDEMIS          , V_CDEMPRESA, V_CDINSCESTA
           FROM FILIAL
          WHERE (CDFILIAL = P_FILIDEST);
      Exception
         When No_Data_Found Then
            V_IDENDCFOP := 'I'; V_IDENDEMIS := 'I'; V_CDEMPRESA := Null; V_CDINSCESTA := Null;
      End;

      IF V_IDENDEMIS = 'I' THEN
         Begin
            SELECT CDPAIS    , SGESTADO  , CDMUNICIPIO
              INTO   V_PAISDEST, V_ESTADEST, V_MUNIDEST
              FROM INSCRESTAD
              WHERE CDEMPRESA  = V_CDEMPRESA AND
                    CDINSCESTA = V_CDINSCESTA;
         Exception
            When No_Data_Found Then
               V_PAISDEST := Null; V_ESTADEST := Null; V_MUNIDEST := Null;
         End;
      ELSE
         Begin
            SELECT CDPAIS    , SGESTADO  , CDMUNICIPIO
              INTO   V_PAISDEST, V_ESTADEST, V_MUNIDEST
              FROM ENDEFILI
             WHERE CDFILIAL     = P_FILIDEST AND
                   IDTPENDEFILI = V_IDENDEMIS;
         Exception
            When No_Data_Found Then
               V_PAISDEST := Null; V_ESTADEST := Null; V_MUNIDEST := Null;
         End;
      END IF;
   END IF;

   SELECT MAX(IDCARACFIS)
     INTO V_CARACFIS
     FROM PARAMFILIAL
    WHERE CDFILIAL = P_FILIAL;

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

-- Atualizacao da tabela de ItemPedido
   IF (NVL(P_IDMOBILE, 'N') = 'N') THEN
      DELETE IMPITEMPEDIDO
      WHERE (CDFILIAL    = P_FILIAL)    AND
            (NRPEDIDO    = P_NRPEDIDO)  AND
            (NRITEMPED   = P_NRITEMPED);
   END IF;

   IF (NVL(P_IDMOBILE, 'N') = 'S') THEN
      DELETE IMPITEMPEDIDOTRAB
      WHERE (CDFILIAL    = P_FILIAL)
        AND (NRPEDIDO    = P_NRPEDIDO)
        AND (NRITEMPED   = P_NRITEMPED);
   END IF;


   FOR C_ITEMPEDIDO IN (SELECT QTITEMPED, VRPREUNITPED, VRDESITPED, ITEMPEDIDO.CDPRODUTO,
                               CDARVPROD, DTENTITPED
                          FROM ITEMPEDIDO, PRODUTO
                         WHERE NRPEDIDO  = P_NRPEDIDO
                           AND NRITEMPED = P_NRITEMPED
                           AND CDFILIAL  = P_FILIAL
                           AND ITEMPEDIDO.CDPRODUTO = PRODUTO.CDPRODUTO
                           AND NVL(P_IDMOBILE, 'N') = 'N'
                        UNION ALL
                        SELECT QTITEMPED, VRPREUNITPED, VRDESITPED, ITEMPEDIDOTRAB.CDPRODUTO,
                               CDARVPROD, DTENTITPED
                          FROM ITEMPEDIDOTRAB, PRODUTO
                         WHERE NRPEDIDO  = P_NRPEDIDO
                           AND NRITEMPED = P_NRITEMPED
                           AND CDFILIAL  = P_FILIAL
                           AND ITEMPEDIDOTRAB.CDPRODUTO = PRODUTO.CDPRODUTO
                           AND NVL(P_IDMOBILE, 'N') = 'S') LOOP

      IF (C_ITEMPEDIDO.VRDESITPED IS NULL) THEN
         V_LIQUIITEM := (C_ITEMPEDIDO.VRPREUNITPED * C_ITEMPEDIDO.QTITEMPED);
      ELSE
         V_LIQUIITEM := (C_ITEMPEDIDO.VRPREUNITPED * C_ITEMPEDIDO.QTITEMPED) - C_ITEMPEDIDO.VRDESITPED;
      END IF;

      CONSULTA_CST('S', P_EMPRESA, C_ITEMPEDIDO.CDARVPROD, P_INSCESTA, P_CDTIPOOPER, P_FILIAL||P_NRPEDIDO, SUBSTR(P_NRITEMPED,7,4), V_CDGRUPOFISC, V_IDINTEREST, V_IDINTERNA, 'PED','N');

      V_SITUTRIBPR := NULL;
      SELECT MAX(CDSITTRIBTMP)
        INTO V_SITUTRIBPR
        FROM CSTTEMP
       WHERE NRCSTTEMP = V_CSTTEMP;

      CONSULTA_INCIDENCIA('S', P_EMPRESA, C_ITEMPEDIDO.CDARVPROD, P_INSCESTA, P_CDTIPOOPER, P_FILIAL||P_NRPEDIDO, SUBSTR(P_NRITEMPED,7,4), V_CDGRUPOFISC,
                          NULL, V_IDINTEREST, 'PED', P_FILIDEST, NULL, P_CLIENTE, P_INIVALID, NULL, V_LIQUIITEM, NULL, 'N');

---------------------------------------------------------------------------------------------------------
      --Atualizacao da tabela de impostos por item.
         /*IMPOSTO DE SUBSTITUICAO TRIBUTARIA*/

      FOR C_INCIDENCIA IN (SELECT DISTINCT (CDIMPOTEMP) AS CDIMPOTEMP, CDFISCTEMP,
                                  IP.IDTRATIMPO, IP.IDIMPORECUP, IP.IDUTVLRQTDEIMPO, IP.IDDESONERAIMPO,
                                  IP.CDIMPOSTONFE, IT.CDREGRATMP
                           FROM INCITEMP IT, IMPOSTO IP
                           WHERE (IT.NRINCITEMP = V_INCITEMP)    AND
                                 (IP.CDIMPOSTO  = IT.CDIMPOTEMP) AND
                                 (IP.IDPRODNOTA = 'P') AND
                                 (IP.CDIMPOSTRIB IS NOT NULL)
                          UNION
                          SELECT DISTINCT (CDIMPOTEMP) AS CDIMPOTEMP, CDFISCTEMP,
                                  IP.IDTRATIMPO, IP.IDIMPORECUP, IP.IDUTVLRQTDEIMPO, IP.IDDESONERAIMPO,
                                  IP.CDIMPOSTONFE, IT.CDREGRATMP
                           FROM INCITEMP IT, IMPOSTO IP
                           WHERE (IT.NRINCITEMP = V_INCITEMP)    AND
                                 (IP.CDIMPOSTO  = IT.CDIMPOTEMP) AND
                                 (IP.IDPRODNOTA = 'P') AND
                                 (IP.CDIMPOSTRIB IS NULL)) LOOP/*UNION PARA QUE O FORCE IMPOSTO ICMS COMUM A SER O PRIMEIRO DA LKISTA, POIS PARA QUE
                                                                 O ICMS POSSA SER DIMINUIDO DO ST, O E PRECISO QUE COMUM SEJA CALCULADO PRIMEIRA*/

         IF NVL(P_IDMOBILE, 'N') = 'N' THEN
            UPDATE ITEMPEDIDO
               SET IDUTILBCPMC = 'N'
             WHERE CDFILIAL = P_FILIAL
               AND NRPEDIDO = P_NRPEDIDO
               AND NRITEMPED = P_NRITEMPED;
         ELSE
           UPDATE ITEMPEDIDOTRAB
              SET IDUTILBCPMC = 'N'
            WHERE CDFILIAL = P_FILIAL
              AND NRPEDIDO = P_NRPEDIDO
              AND NRITEMPED = P_NRITEMPED;
         END IF;

         CONSULTA_ALIQUOTA(C_INCIDENCIA.CDIMPOTEMP, 'S', P_EMPRESA, C_ITEMPEDIDO.CDARVPROD, P_INSCESTA, V_PAISORIG,
                           V_ESTAORIG, V_PAISDEST, V_ESTADEST, V_MUNIDEST, V_LIQUIITEM, P_INIVALID, P_FILIAL||P_NRPEDIDO, SUBSTR(P_NRITEMPED,7,4), V_CDGRUPOFISC, P_CDTIPOOPER,
                           'PED', NULL, NULL, NULL, NULL, NULL, 'N');

         V_CDGRUPOFISCAUX := NULL;

         SELECT NVL(MAX(G.CDGRUPOFISC), V_CDGRUPOFISCAUX)
           INTO V_CDGRUPOFISCAUX
           FROM GRFISIMPIECLIE G
          WHERE G.CDEMPRESA = P_EMPRESA
            AND G.CDINSCESTA = P_INSCESTA
            AND G.CDCLIENTE = P_CLIENTE
            AND NOT G.CDGRUPOFISC IS NULL
            AND G.CDIMPOSTO = C_INCIDENCIA.CDIMPOTEMP;

         IF V_CDGRUPOFISCAUX IS NULL THEN
            CONSULTA_CST_NFE('S', P_EMPRESA, C_ITEMPEDIDO.CDARVPROD, P_INSCESTA, P_CDTIPOOPER, V_CDGRUPOFISC,C_INCIDENCIA.CDIMPOTEMP, V_IDINTEREST, V_IDINTERNA, SUBSTR(P_NRITEMPED,7,4),
                             P_FILIAL||P_NRPEDIDO, P_CLIENTE, V_CDSITTRIBTMP, V_IDMODBCNFE, V_IDMODBCSTNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE);

            IF V_CDSITTRIBTMPICMS IS NULL THEN
               IF V_CDSITTRIBTMP IS NULL THEN
                  IF (LENGTH(V_SITUTRIBPR) > 2) THEN
                     V_CDSITTRIBTMP := SUBSTR(V_SITUTRIBPR, 2, 2);
                  ELSE
                     V_CDSITTRIBTMP := V_SITUTRIBPR;
                  END IF;
               END IF;
            ELSE
               V_CDSITTRIBTMP := V_CDSITTRIBTMPICMS;
            END IF;

            CONSULTA_CFOP('S', P_EMPRESA, C_ITEMPEDIDO.CDARVPROD, P_INSCESTA, P_CDTIPOOPER, V_PAISORIG,
                           V_ESTAORIG, V_PAISDEST, V_ESTADEST, P_FILIAL||P_NRPEDIDO, SUBSTR(P_NRITEMPED,7,4),
                           V_CDGRUPOFISC, P_INIVALID);
         ELSE
            CONSULTA_CST_NFE('S', P_EMPRESA, C_ITEMPEDIDO.CDARVPROD, P_INSCESTA, P_CDTIPOOPER, V_CDGRUPOFISCAUX,C_INCIDENCIA.CDIMPOTEMP, V_IDINTEREST, V_IDINTERNA, SUBSTR(P_NRITEMPED,7,4),
                             P_FILIAL||P_NRPEDIDO, P_CLIENTE, V_CDSITTRIBTMP, V_IDMODBCNFE, V_IDMODBCSTNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE);

            IF V_CDSITTRIBTMPICMS IS NULL THEN
               IF V_CDSITTRIBTMP IS NULL THEN
                  IF (LENGTH(V_SITUTRIBPR) > 2) THEN
                     V_CDSITTRIBTMP := SUBSTR(V_SITUTRIBPR, 2, 2);
                  ELSE
                     V_CDSITTRIBTMP := V_SITUTRIBPR;
                  END IF;
               END IF;
            ELSE
               V_CDSITTRIBTMP := V_CDSITTRIBTMPICMS;
            END IF;

            CONSULTA_CFOP('S', P_EMPRESA, C_ITEMPEDIDO.CDARVPROD, P_INSCESTA, P_CDTIPOOPER, V_PAISORIG,
                           V_ESTAORIG, V_PAISDEST, V_ESTADEST, P_FILIAL||P_NRPEDIDO, SUBSTR(P_NRITEMPED,7,4),
                           V_CDGRUPOFISCAUX, P_INIVALID);
         END IF;

         IF V_FISOPEITEMICMS IS NULL THEN
            SELECT MAX(CDFISOPETMP) INTO V_FISOPEITEM
              FROM CFOPTEMP
             WHERE NRCFOPTEMP = V_CFOPTEMP;
         ELSE
            V_FISOPEITEM := V_FISOPEITEMICMS;
         END IF;

         V_PARDEDTMP  := 0;
         V_PREDBCTMP  := 0;
         V_ALIQUTMP   := 0;
         V_IMPOSTOIT  := 0;
         V_IDPMCBASECAL := 'N';
         V_CDFISCOPERREGRA := NULL;
         V_CFOP1DIGT := NULL;
         V_VRADICALIQFECP := NULL;

         SELECT MAX(VRPARDEDTMP), MAX(VRPREDBCTMP), MAX(VRALIQUTMP), MAX(VRPBCSTTMP), MAX(IDTRIMPSTTMP), NVL(MAX(IDPMCBSCALTMP), 'N'),
                MAX(CDFISCOPER),  MAX(VRADICALIQFECP), MAX(IDCONSMVAPMC), MAX(IDCONSCOMPBCPMC), MAX(VRIMPOSTOREP),
                NVL(MAX(IDIMPODESONERA), 'N'), NVL(MAX(IDCONSDESONVRNF), 'N')
           INTO V_PARDEDTMP, V_PREDBCTMP, V_ALIQUTMP, V_VRPBCSTTMP, V_TRATIMPST, V_IDPMCBASECAL,
                V_CDFISCOPERREGRA, V_VRADICALIQFECP, V_IDCONSMVAPMC, V_IDCONSCOMPBCPMC, V_VRIMPOSTOREP,
                V_IDIMPODESONERA,  V_IDCONSDESONVRNF
           FROM ALIQTEMP
          WHERE (NRALIQTEMP = V_ALIQTEMP);

         IF (V_CDFISCOPERREGRA IS NOT NULL) THEN
            IF (V_PAISORIG = V_PAISDEST) THEN
               IF (V_ESTAORIG = V_ESTADEST) THEN
                  V_CFOP1DIGT := '5';
               ELSE
                  V_CFOP1DIGT := '6';
               END IF;
            ELSE
               V_CFOP1DIGT := '7';
            END IF;
            V_FISOPEITEM := V_CFOP1DIGT||V_CDFISCOPERREGRA;
         END IF;

         IF (V_VRPBCSTTMP IS NULL) THEN
            V_VRPBCSTTMP := 0;
         END IF;

         IF (V_ALIQUTMP IS NULL) THEN
            V_ALIQUTMP := 0;
         END IF;

         IF (V_PREDBCTMP IS NULL) THEN
            V_PREDBCTMP := 0;
         END IF;

         IF (V_PARDEDTMP IS NULL) THEN
             V_PARDEDTMP := 0;
         END IF;

         V_IDVRIMPSTRIB := NULL;

         SELECT COUNT(CDIMPOSTO)
           INTO V_CTSTRIB
           FROM IMPOSTO
          WHERE CDIMPOSTRIB = C_INCIDENCIA.CDIMPOTEMP;

         /*IMPOSTO ICMS ORIGINAL*/
         IF NVL(P_IDMOBILE, 'N') = 'N' THEN
            BEGIN
               SELECT MAX(IP.CDIMPOSTO), MAX(IP.IDVRIMPSTRIB)
                 INTO V_IMPOSTOICMS, V_IDVRIMPSTRIB
                 FROM IMPOSTO IP, IMPITEMPEDIDO II
                WHERE (II.CDFILIAL    = P_FILIAL)
                  AND (II.NRPEDIDO    = P_NRPEDIDO)
                  AND (II.NRITEMPED   = P_NRITEMPED)
                  AND IP.CDIMPOSTO = II.CDIMPOSTO
                  AND IP.CDIMPOSTRIB IS NOT NULL;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
               V_IMPOSTOICMS := NULL; V_IDVRIMPSTRIB:= NULL;
            END;
         ELSE
            BEGIN
               SELECT MAX(IP.CDIMPOSTO), MAX(IP.IDVRIMPSTRIB)
                 INTO V_IMPOSTOICMS, V_IDVRIMPSTRIB
                 FROM IMPOSTO IP, IMPITEMPEDIDOTRAB II
                WHERE (II.CDFILIAL    = P_FILIAL)
                  AND (II.NRPEDIDO    = P_NRPEDIDO)
                  AND (II.NRITEMPED   = P_NRITEMPED)
                  AND IP.CDIMPOSTO = II.CDIMPOSTO
                  AND IP.CDIMPOSTRIB IS NOT NULL;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
               V_IMPOSTOICMS := NULL; V_IDVRIMPSTRIB:= NULL;
            END;
         END IF;
         /*IMPOSTO DE SUBSTITUICAO TRIBUTARIA*/
         FOR C_ICMSST IN (SELECT CDIMPOSTO
                            FROM IMPOSTO
                           WHERE CDIMPOSTRIB = C_INCIDENCIA.CDIMPOTEMP)
         LOOP
            V_CDIMPOSTRIB := V_CDIMPOSTRIB||C_ICMSST.CDIMPOSTO||'_';
         END LOOP;

         V_TAXABASECALC := (100 - V_PREDBCTMP)/100;
         V_TAXAALIQUOTA := (V_ALIQUTMP/100);
         V_TAXABCSUBSTR := 1 + (V_VRPBCSTTMP / 100);
         V_BASECALCIT   := V_LIQUIITEM * V_TAXABASECALC;

         IF (V_VRIMPOSTOREP IS NOT NULL) THEN  /* Caso esteja cadastrao Valor a ser Repassado, a base de Calculo e o valor do imposto e calculado de acordo com este cadastro */
            IF (V_VRIMPOSTOREP > 0) THEN
               V_IMPOSTOIT  := (C_ITEMPEDIDO.QTITEMPED * V_VRIMPOSTOREP);
               V_BASECALCIT := (V_LIQUIITEM * (V_IMPOSTOIT)) / (V_LIQUIITEM * (V_ALIQUTMP/100));
            END IF;
         ELSE

            IF V_CDIMPOSTRIB IS NULL THEN
               V_PERDBCALC    := 100 - V_PREDBCTMP;

               IF V_VRPBCSTTMP > 0 THEN
                  V_BASECALCIT := (V_BASECALCIT * V_TAXABCSUBSTR);
                  V_PREDBCTMP  := V_VRPBCSTTMP;
                  V_PERDBCALC  := 0;
               END IF;

               IF (C_INCIDENCIA.IDUTVLRQTDEIMPO = 'S') THEN
                  SELECT NVL(MAX(VRFATOCONVES), 1)
                    INTO V_VRFATOCONVES
                    FROM CONVPRODUTO
                   WHERE CDPRODUTO = C_ITEMPEDIDO.CDPRODUTO;

                  V_BASECALCIT  := (C_ITEMPEDIDO.QTITEMPED * V_VRFATOCONVES);
                  V_IMPOSTOIT   := ((C_ITEMPEDIDO.QTITEMPED * V_VRFATOCONVES) * V_ALIQUTMP) - V_PARDEDTMP;
               ELSE
                  V_IMPOSTOIT   := (V_BASECALCIT * V_TAXAALIQUOTA) - V_PARDEDTMP;
               END IF;
            ELSE
               IF NVL(V_IDPMCBASECAL, 'N') = 'S' THEN
                  CALCULA_BC_PMC(P_FILIAL,
                                 '',
                                 '',
                                 C_INCIDENCIA.CDIMPOTEMP,
                                 V_IDCONSMVAPMC,
                                 V_IDCONSCOMPBCPMC,
                                 V_PAISDEST,
                                 V_ESTADEST,
                                 C_ITEMPEDIDO.CDPRODUTO,
                                 C_ITEMPEDIDO.QTITEMPED,
                                 C_ITEMPEDIDO.VRPREUNITPED,
                                 C_ITEMPEDIDO.VRDESITPED,
                                 V_VRPBCSTTMP,
                                 V_PREDBCTMP,
                                 P_NRPEDIDO,
                                 P_NRITEMPED,
                                 'S',
                                 P_CDTIPOOPER,
                                 P_IDTIPOOPER,
                                 P_CLIENTE,
                                 NVL(P_IDMOBILE, 'N'),
                                 V_BASECALCIT);

                  V_IMPOSTOIT := (V_BASECALCIT * V_TAXAALIQUOTA) - V_PARDEDTMP;
                  IF (V_VRADICALIQFECP IS NOT NULL) AND (V_VRADICALIQFECP > 0) THEN
                     V_TAXAALIQUOTAFECP := (V_VRADICALIQFECP/100);
                     V_IMPOSTOITFECP    := (V_BASECALCIT * V_TAXAALIQUOTAFECP) - V_PARDEDTMP;
                     /*ponto de alteracao acrescer ok*/
                     V_ALIQUTMP := V_ALIQUTMP + V_VRADICALIQFECP;
                     /*ponto de alteracao*/
                     V_IMPOSTOIT        := ROUND(V_IMPOSTOIT, 2) + ROUND(V_IMPOSTOITFECP, 2);
                  END IF;
               ELSE
                  V_PERDBCALC    := 100 - V_PREDBCTMP;

                  IF V_VRPBCSTTMP > 0 THEN
                     V_BASECALCIT := (V_BASECALCIT * V_TAXABCSUBSTR);
                     V_PREDBCTMP  := V_VRPBCSTTMP;
                     V_PERDBCALC  := 0;
                  END IF;

                  V_IMPOSTOIT   := (V_BASECALCIT * V_TAXAALIQUOTA) - V_PARDEDTMP;
                  IF (V_VRADICALIQFECP IS NOT NULL) AND (V_VRADICALIQFECP > 0) THEN

                     V_TAXAALIQUOTAFECP := (V_VRADICALIQFECP/100);
                     V_IMPOSTOITFECP    := (V_BASECALCIT * V_TAXAALIQUOTAFECP) - V_PARDEDTMP;
                     /*ponto de alteracao acrescer*/
                     V_ALIQUTMP := V_ALIQUTMP + V_VRADICALIQFECP;
                     /*ponto de alteracao*/
                     V_IMPOSTOIT        := ROUND(V_IMPOSTOIT, 2) + ROUND(V_IMPOSTOITFECP, 2);

                  END IF;
               END IF;
            END IF;
         END IF;

         V_CONTABCALC := V_LIQUIITEM - (V_LIQUIITEM * V_TAXABASECALC);
         V_PERDBCALC  := 100 - V_PREDBCTMP;

         /* Rotina para calcular o ICMS ST considerando % carga tributaria media */
         V_IDIMPSTCARGTRIB := 'N';

         IF V_CDIMPOSTRIB IS NOT NULL THEN
            BEGIN
               SELECT NVL(VRCNAEBC, 0),
                      NVL(VRMVA, 0)
                 INTO V_VRPRODUTOBC,
                      V_VRMVA
                 FROM VRPERCCARGTRIBCNAE
                WHERE IDENTRSAIDOP = 'S'
                  AND CDTIPOOPER   = P_CDTIPOOPER
                  AND CDPAIS       = V_PAISDEST
                  AND SGESTADO     = V_ESTADEST
                  AND CDCNAE       = V_CDCNAECLIE
                  AND IDOPTANTESN  = V_IDOPTANTESNCLIE;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_VRPRODUTOBC := 0;
                  V_VRMVA := 0;
            END;
            IF V_VRPRODUTOBC = 0 THEN
               BEGIN
                  SELECT NVL(VRPRODUTOBC, 0)
                    INTO V_VRPRODUTOBC
                    FROM VRPERCCARGTRIB
                   WHERE IDENTRSAIDOP = 'S'
                     AND CDTIPOOPER   = P_CDTIPOOPER
                     AND CDPAIS       = V_PAISDEST
                     AND SGESTADO     = V_ESTADEST
                     AND CDPRODUTO    = C_ITEMPEDIDO.CDPRODUTO;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN
                     V_VRPRODUTOBC := 0;
               END;
            END IF;

            IF (V_VRPRODUTOBC > 0) THEN
               IF NVL(P_IDMOBILE, 'N') = 'N' THEN
                  BEGIN
                     SELECT I.CDIMPOSTO, I.VRIMPOSTOITPED, I.VRBASECALCITPED
                       INTO V_CDIMPOSTOICMS, V_VRIMPOSTOICMS, V_BASECALCICMS
                       FROM IMPOSTONFE N, IMPOSTO P, IMPITEMPEDIDO I
                      WHERE N.CDIMPOSTONFE = P.CDIMPOSTONFE
                        AND N.CDIMPOSTONFE = '00'
                        AND I.CDFILIAL     = P_FILIAL
                        AND I.NRPEDIDO     = P_NRPEDIDO
                        AND I.NRITEMPED    = P_NRITEMPED
                        AND I.CDIMPOSTO    = NVL(P.CDIMPOSREGNORMA, P.CDIMPOSTO);
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                        V_CDIMPOSTOICMS := NULL; V_VRIMPOSTOICMS := 0; V_BASECALCICMS := 0;
                  END;
               ELSE
                  BEGIN
                     SELECT I.CDIMPOSTO, I.VRIMPOSTOITPED, I.VRBASECALCITPED
                       INTO V_CDIMPOSTOICMS, V_VRIMPOSTOICMS, V_BASECALCICMS
                       FROM IMPOSTONFE N, IMPOSTO P, IMPITEMPEDIDOTRAB I
                      WHERE N.CDIMPOSTONFE = P.CDIMPOSTONFE
                        AND N.CDIMPOSTONFE = '00'
                        AND I.CDFILIAL     = P_FILIAL
                        AND I.NRPEDIDO     = P_NRPEDIDO
                        AND I.NRITEMPED    = P_NRITEMPED
                        AND I.CDIMPOSTO    = NVL(P.CDIMPOSREGNORMA, P.CDIMPOSTO);
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                        V_CDIMPOSTOICMS := NULL; V_VRIMPOSTOICMS := 0; V_BASECALCICMS := 0;
                  END;
               END IF;

               DELETE ALIQTEMP
                WHERE NRALIQTEMP = V_ALIQTEMP;

               /* Verifica grupo fiscal */
               SELECT MAX(IDGRUPFISSRV)
                 INTO V_UTGRUPFISCS
                 FROM IMPOSTO
                WHERE CDIMPOSTO = V_CDIMPOSTOICMS;

               IF V_UTGRUPFISCS = 'S' THEN
                  V_CDGRPFISCSV := NULL;
                  SELECT MAX(CDGRUPOFISC)
                    INTO V_CDGRPFISCSV
                    FROM SERVCLIE SC, SERVICO S
                   WHERE SC.CDFILIAL   = P_FILIAL     AND
                         SC.CDCLIENTE  = P_CLIENTE    AND
                         SC.CDFILIAL   = S.CDFILIAL   AND
                         SC.CDSERVFILI = S.CDSERVFILI AND
                         S.CDPRODUTO   = C_ITEMPEDIDO.CDPRODUTO;

                  IF V_CDGRPFISCSV IS NULL THEN
                     SELECT MAX(CDGRUPOFISC)
                       INTO V_CDGRPFISCSV
                       FROM CLIENFILIAL
                      WHERE CDFILIAL   = P_FILIAL AND
                            CDCLIENTE  = P_CLIENTE;
                  END IF;

                  CONSULTA_ALIQUOTA(V_CDIMPOSTOICMS, 'S', P_EMPRESA, C_ITEMPEDIDO.CDARVPROD, P_INSCESTA, V_PAISDEST,
                                    V_ESTADEST, V_PAISDEST, V_ESTADEST, V_MUNIDEST, V_LIQUIITEM, P_INIVALID, P_FILIAL||P_NRPEDIDO, SUBSTR(P_NRITEMPED,7,4), V_CDGRPFISCSV, P_CDTIPOOPER,
                                    'PED', NULL, NULL, NULL, NULL, NULL, 'N');
               ELSE
                  CONSULTA_ALIQUOTA(V_CDIMPOSTOICMS, 'S', P_EMPRESA, C_ITEMPEDIDO.CDARVPROD, P_INSCESTA, V_PAISDEST,
                                    V_ESTADEST, V_PAISDEST, V_ESTADEST, V_MUNIDEST, V_LIQUIITEM, P_INIVALID, P_FILIAL||P_NRPEDIDO, SUBSTR(P_NRITEMPED,7,4), V_CDGRUPOFISC, P_CDTIPOOPER,
                                    'PED', NULL, NULL, NULL, NULL, NULL, 'N');
               END IF;

               SELECT NVL(MAX(VRALIQUTMP),0)
                 INTO V_VRALIQUTMPDEST
                 FROM ALIQTEMP
                WHERE (NRALIQTEMP = V_ALIQTEMP);

               IF (V_VRALIQUTMPDEST <> 0) THEN
                  IF V_VRMVA > 0 THEN
                     V_BASECALCICMS :=  /*V_BASECALCICMS + */(V_BASECALCICMS * (V_VRMVA/100));
                  END IF;
                  V_IMPOSTOIT  :=  ROUND(V_BASECALCICMS * (V_VRPRODUTOBC/100), 2);
                  V_BASECALCIT :=  ROUND(((V_VRIMPOSTOICMS + V_IMPOSTOIT) / (V_VRALIQUTMPDEST/100)),2);
                  V_IDIMPSTCARGTRIB := 'S';
               END IF;

               DELETE ALIQTEMP
                WHERE NRALIQTEMP = V_ALIQTEMP;
            END IF;
         END IF;

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

         V_ADIMPAF := 0;

         IF (C_INCIDENCIA.IDTRATIMPO = 'A') OR (V_TRATIMPST = '0') THEN
            V_ADIMPAF := V_IMPOSTOIT;

         ELSIF (C_INCIDENCIA.IDTRATIMPO = 'D') THEN
            V_ADIMPAF := - V_IMPOSTOIT;

         END IF;


         V_VRIMPITAFAUX := NULL;
         IF NVL(P_IDMOBILE, 'N') = 'N' THEN
            SELECT MAX(VRIMPOSTOITPED)
              INTO V_VRIMPITAFAUX
              FROM IMPITEMPEDIDO
             WHERE (CDFILIAL    = P_FILIAL)    AND
                  (NRPEDIDO    = P_NRPEDIDO)  AND
                  (NRITEMPED   = P_NRITEMPED) AND
                  (CDIMPOSTO   = C_INCIDENCIA.CDIMPOTEMP);
         ELSE
            SELECT MAX(VRIMPOSTOITPED)
              INTO V_VRIMPITAFAUX
              FROM IMPITEMPEDIDOTRAB
             WHERE (CDFILIAL    = P_FILIAL)    AND
                   (NRPEDIDO    = P_NRPEDIDO)  AND
                   (NRITEMPED   = P_NRITEMPED) AND
                   (CDIMPOSTO   = C_INCIDENCIA.CDIMPOTEMP);
         END IF;

         IF (C_INCIDENCIA.IDTRATIMPO = 'A') OR
            (C_INCIDENCIA.IDTRATIMPO = 'D') THEN
            IF ((C_INCIDENCIA.IDTRATIMPO = 'D') AND (C_INCIDENCIA.IDDESONERAIMPO = 'N')) THEN
               V_VRIMPITAFAUX := -V_VRIMPITAFAUX;
            END IF;

            IF NVL(P_IDMOBILE, 'N') = 'N' THEN
               UPDATE IMPITEMPEDIDO
               SET VRACREDESCITPED = ROUND(V_VRIMPITAFAUX, 2)
               WHERE (CDFILIAL    = P_FILIAL)    AND
                     (NRPEDIDO    = P_NRPEDIDO)  AND
                     (NRITEMPED   = P_NRITEMPED) AND
                     (CDIMPOSTO   = C_INCIDENCIA.CDIMPOTEMP);
            ELSE
               UPDATE IMPITEMPEDIDOTRAB
               SET VRACREDESCITPED = ROUND(V_VRIMPITAFAUX, 2)
               WHERE (CDFILIAL    = P_FILIAL)    AND
                     (NRPEDIDO    = P_NRPEDIDO)  AND
                     (NRITEMPED   = P_NRITEMPED) AND
                     (CDIMPOSTO   = C_INCIDENCIA.CDIMPOTEMP);
            END IF;
         END IF;

         IF C_INCIDENCIA.CDIMPOSTONFE = '00' THEN
            V_FISOPEITEMICMS := V_FISOPEITEM;
            V_CDSITTRIBTMPICMS := V_CDSITTRIBTMP;
         END IF;

         IF V_VRIMPITAFAUX IS NULL THEN
            IF NVL(P_IDMOBILE, 'N') = 'N' THEN
               INSERT INTO IMPITEMPEDIDO
                  (CDFILIAL, NRPEDIDO, NRITEMPED,
                  CDIMPOSTO, VRIMPOSTOITPED, VRPERREDBCITPED, VRBASECALCITPED,
                  VRALIQITPED, VRPARCDEDITPED, CDFISCALITPED, VRACREDESCITPED,
                  CDSITUTRIBITPED, CDFISCOPERITPED, VRALIQITFECP, CDREGRAIT, VRBASECALCITFECP, VRIMPOSTOREP,
                  IDDESONERAREGRA, IDCONSDESONVRNF)
               VALUES
                  (P_FILIAL, P_NRPEDIDO, P_NRITEMPED,
                  C_INCIDENCIA.CDIMPOTEMP, ROUND(V_IMPOSTOIT, 2), V_PREDBCTMP, ROUND(V_BASECALCIT,2),
                  V_ALIQUTMP, V_PARDEDTMP, C_INCIDENCIA.CDFISCTEMP, ROUND(V_ADIMPAF, 2),
                  V_CDSITTRIBTMP, V_FISOPEITEM,V_VRADICALIQFECP, C_INCIDENCIA.CDREGRATMP, V_BASECALCIT, V_VRIMPOSTOREP,
                  NVL(V_IDIMPODESONERA, 'N'), NVL(V_IDCONSDESONVRNF, 'N'));
            ELSE
               INSERT INTO IMPITEMPEDIDOTRAB
                  (CDFILIAL, NRPEDIDO, NRITEMPED,
                  CDIMPOSTO, VRIMPOSTOITPED, VRPERREDBCITPED, VRBASECALCITPED,
                  VRALIQITPED, VRPARCDEDITPED, CDFISCALITPED, VRACREDESCITPED,
                  CDSITUTRIBITPED, CDFISCOPERITPED, VRALIQITFECP, CDREGRAIT, VRBASECALCITFECP, VRIMPOSTOREP,
                  IDDESONERAREGRA, IDCONSDESONVRNF)
               VALUES
                  (P_FILIAL, P_NRPEDIDO, P_NRITEMPED,
                  C_INCIDENCIA.CDIMPOTEMP, ROUND(V_IMPOSTOIT, 2), V_PREDBCTMP, ROUND(V_BASECALCIT,2),
                  V_ALIQUTMP, V_PARDEDTMP, C_INCIDENCIA.CDFISCTEMP, ROUND(V_ADIMPAF, 2),
                  V_CDSITTRIBTMP, V_FISOPEITEM,V_VRADICALIQFECP, C_INCIDENCIA.CDREGRATMP, V_BASECALCIT, V_VRIMPOSTOREP,
                  NVL(V_IDIMPODESONERA, 'N'), NVL(V_IDCONSDESONVRNF, 'N'));

            END IF;
         END IF;

         DELETE ALIQTEMP
         WHERE NRALIQTEMP = V_ALIQTEMP;

         DELETE FROM CFOPTEMP
         WHERE NRCFOPTEMP = V_CFOPTEMP;

      END LOOP;
      DELETE FROM CSTTEMP
      WHERE NRCSTTEMP = V_CSTTEMP;

      DELETE INCITEMP
      WHERE NRINCITEMP = V_INCITEMP;


   END LOOP;
   CONSULTA_DESONERACAO_PED(P_FILIAL,
                            P_NRPEDIDO,
                            V_PAISDEST,
                            V_ESTADEST,
                            V_IMPOSDESONERA);

   FOR C_CFOPICMS IN (SELECT II.CDIMPOSTO, I.CDIMPOSTRIB, II.NRITEMPED, II.CDFISCOPERITPED, II.CDSITUTRIBITPED
                        FROM IMPITEMPEDIDO II, IMPOSTO I
                       WHERE (II.CDFILIAL    = P_FILIAL )
                         AND (II.NRPEDIDO    = P_NRPEDIDO)
                         AND (II.CDIMPOSTO   = I.CDIMPOSTO)
                         AND (I.CDIMPOSTONFE = '00')
                         AND (NVL(P_IDMOBILE, 'N') = 'N')
                      UNION ALL
                      SELECT II.CDIMPOSTO, I.CDIMPOSTRIB, II.NRITEMPED, II.CDFISCOPERITPED, II.CDSITUTRIBITPED
                        FROM IMPITEMPEDIDOTRAB II, IMPOSTO I
                       WHERE (II.CDFILIAL    = P_FILIAL )
                         AND (II.NRPEDIDO    = P_NRPEDIDO)
                         AND (II.CDIMPOSTO   = I.CDIMPOSTO)
                         AND (I.CDIMPOSTONFE = '00')
                         AND (NVL(P_IDMOBILE, 'N') = 'S')
                      ORDER BY 1 DESC
                     ) LOOP

      IF (NVL(P_IDMOBILE, 'N') = 'N') THEN
         UPDATE IMPITEMPEDIDO
         SET CDFISCOPERITPED = C_CFOPICMS.CDFISCOPERITPED,
             CDSITUTRIBITPED = C_CFOPICMS.CDSITUTRIBITPED
         WHERE CDFILIAL = P_FILIAL
           AND NRPEDIDO = P_NRPEDIDO
           AND NRITEMPED = C_CFOPICMS.NRITEMPED
           AND CDIMPOSTO = C_CFOPICMS.CDIMPOSTRIB;

         UPDATE IMPITEMPEDIDO
         SET CDFISCOPERITPED = C_CFOPICMS.CDFISCOPERITPED,
             CDSITUTRIBITPED = C_CFOPICMS.CDSITUTRIBITPED
         WHERE CDFILIAL = P_FILIAL
           AND NRPEDIDO = P_NRPEDIDO
           AND NRITEMPED = C_CFOPICMS.NRITEMPED
           AND CDIMPOSTO NOT IN (SELECT II2.CDIMPOSTO
                                   FROM IMPITEMPEDIDO II2, IMPOSTO I
                                  WHERE II2.CDFILIAL = P_FILIAL
                                    AND II2.NRPEDIDO = P_NRPEDIDO
                                    AND II2.NRITEMPED = C_CFOPICMS.NRITEMPED
                                    AND II2.CDIMPOSTO = I.CDIMPOSTO
                                    AND I.CDIMPOSTONFE = '00')
           AND CDIMPOSTO NOT IN (SELECT I.CDIMPOSTRIB
                                   FROM IMPITEMPEDIDO II2, IMPOSTO I
                                  WHERE II2.CDFILIAL = P_FILIAL
                                    AND II2.NRPEDIDO = P_NRPEDIDO
                                    AND II2.NRITEMPED = C_CFOPICMS.NRITEMPED
                                    AND II2.CDIMPOSTO = I.CDIMPOSTO
                                    AND I.CDIMPOSTONFE = '00');
      ELSE
         UPDATE IMPITEMPEDIDOTRAB
         SET CDFISCOPERITPED = C_CFOPICMS.CDFISCOPERITPED,
             CDSITUTRIBITPED = C_CFOPICMS.CDSITUTRIBITPED
         WHERE CDFILIAL = P_FILIAL
           AND NRPEDIDO = P_NRPEDIDO
           AND NRITEMPED = C_CFOPICMS.NRITEMPED
           AND CDIMPOSTO = C_CFOPICMS.CDIMPOSTRIB;

         UPDATE IMPITEMPEDIDOTRAB
         SET CDFISCOPERITPED = C_CFOPICMS.CDFISCOPERITPED,
             CDSITUTRIBITPED = C_CFOPICMS.CDSITUTRIBITPED
         WHERE CDFILIAL = P_FILIAL
           AND NRPEDIDO = P_NRPEDIDO
           AND NRITEMPED = C_CFOPICMS.NRITEMPED
           AND CDIMPOSTO NOT IN (SELECT II2.CDIMPOSTO
                                   FROM IMPITEMPEDIDOTRAB II2, IMPOSTO I
                                  WHERE II2.CDFILIAL = P_FILIAL
                                    AND II2.NRPEDIDO = P_NRPEDIDO
                                    AND II2.NRITEMPED = C_CFOPICMS.NRITEMPED
                                    AND II2.CDIMPOSTO = I.CDIMPOSTO
                                    AND I.CDIMPOSTONFE = '00')
           AND CDIMPOSTO NOT IN (SELECT I.CDIMPOSTRIB
                                   FROM IMPITEMPEDIDOTRAB II2, IMPOSTO I
                                  WHERE II2.CDFILIAL = P_FILIAL
                                    AND II2.NRPEDIDO = P_NRPEDIDO
                                    AND II2.NRITEMPED = C_CFOPICMS.NRITEMPED
                                    AND II2.CDIMPOSTO = I.CDIMPOSTO
                                    AND I.CDIMPOSTONFE = '00');
      END IF;
   END LOOP;


   -- CALCULO DO VALOR DOS IMPOSTOS QUE CONTEM ENCARGOS NA SUA BASE DE CALCULO
   FOR C_ENCARGOPED IN (SELECT EP.CDENCARGO, EP.VRENCARGOPED, EN.IDENCARGO, EN.CDCTBLENTREN,
                               EN.CDCTBLSAIDEN
                          FROM ENCARGOPED EP, ENCARGO EN
                         WHERE (EP.CDFILIAL = P_FILIAL)
                           AND (EP.NRPEDIDO = P_NRPEDIDO)
                           AND (EP.VRENCARGOPED > 0)
                           AND (EP.CDENCARGO = EN.CDENCARGO)
                           AND (NVL(P_IDMOBILE, 'N') = 'N')
                        UNION ALL
                        SELECT EP.CDENCARGO, EP.VRENCARGOPED, EN.IDENCARGO, EN.CDCTBLENTREN,
                               EN.CDCTBLSAIDEN
                          FROM ENCARGOPEDTRAB EP, ENCARGO EN
                         WHERE (EP.CDFILIAL = P_FILIAL)
                           AND (EP.NRPEDIDO = P_NRPEDIDO)
                           AND (EP.VRENCARGOPED > 0)
                           AND (EP.CDENCARGO = EN.CDENCARGO)
                           AND (NVL(P_IDMOBILE, 'N') = 'S')) LOOP

      V_PRODUENCARGO := 0;
      FOR C_IMPITEMPEDIDO IN (SELECT II.NRITEMPED, II.CDIMPOSTO, II.VRPERREDBCITPED, II.VRALIQITPED,
                                     II.VRPARCDEDITPED, ROUND(ROUND((QTITEMPED * VRPREUNITPED),2) - VRDESITPED, 2) AS VRLIQUIDO,
                                     IP.IDUTILBCPMC, I.CDIMPOSTONFE, II.VRALIQITFECP, II.VRIMPOSTOREP
                                FROM IMPITEMPEDIDO II, ITEMPEDIDO IP, BASECALC BC, IMPOSTO I
                               WHERE (IP.CDFILIAL = P_FILIAL)
                                 AND (IP.NRPEDIDO = P_NRPEDIDO)
                                 AND (IP.CDFILIAL = II.CDFILIAL)
                                 AND (IP.NRPEDIDO = II.NRPEDIDO)
                                 AND (IP.NRITEMPED = II.NRITEMPED)
                                 AND (BC.CDIMPOSTO = II.CDIMPOSTO)
                                 AND (BC.CDENCARGO = C_ENCARGOPED.CDENCARGO)
                                 AND (IP.NRITEMPED = P_NRITEMPED)
                                 AND (I.CDIMPOSTO = II.CDIMPOSTO)
                                 AND (NVL(P_IDMOBILE, 'N') = 'N')
                              UNION ALL
                              SELECT II.NRITEMPED, II.CDIMPOSTO, II.VRPERREDBCITPED, II.VRALIQITPED,
                                     II.VRPARCDEDITPED, ROUND(ROUND((QTITEMPED * VRPREUNITPED),2) - VRDESITPED, 2) AS VRLIQUIDO,
                                     IP.IDUTILBCPMC, I.CDIMPOSTONFE, II.VRALIQITFECP, II.VRIMPOSTOREP
                                FROM IMPITEMPEDIDOTRAB II, ITEMPEDIDOTRAB IP, BASECALC BC, IMPOSTO I
                               WHERE (IP.CDFILIAL = P_FILIAL)
                                 AND (IP.NRPEDIDO = P_NRPEDIDO)
                                 AND (IP.CDFILIAL = II.CDFILIAL)
                                 AND (IP.NRPEDIDO = II.NRPEDIDO)
                                 AND (IP.NRITEMPED = II.NRITEMPED)
                                 AND (BC.CDIMPOSTO = II.CDIMPOSTO)
                                 AND (BC.CDENCARGO = C_ENCARGOPED.CDENCARGO)
                                 AND (IP.NRITEMPED = P_NRITEMPED)
                                 AND (I.CDIMPOSTO = II.CDIMPOSTO)
                                 AND (NVL(P_IDMOBILE, 'N') = 'S')
                              ORDER BY 1, 2) LOOP

         IF (C_IMPITEMPEDIDO.VRIMPOSTOREP IS NULL) THEN
            IF NOT((C_IMPITEMPEDIDO.IDUTILBCPMC = 'S') AND (C_IMPITEMPEDIDO.CDIMPOSTONFE = '80')) OR (C_IMPITEMPEDIDO.IDUTILBCPMC <> 'S') THEN
               IF (NVL(P_IDMOBILE, 'N') = 'N') THEN
                  SELECT SUM(ROUND(ROUND((QTITEMPED * VRPREUNITPED),2) - VRDESITPED, 2))
                    INTO V_TOTALITEM
                    FROM ITEMPEDIDO IP
                   WHERE IP.CDFILIAL = P_FILIAL
                     AND IP.NRPEDIDO = P_NRPEDIDO;
               ELSE
                  SELECT SUM(ROUND(ROUND((QTITEMPED * VRPREUNITPED),2) - VRDESITPED, 2))
                    INTO V_TOTALITEM
                    FROM ITEMPEDIDOTRAB IP
                   WHERE IP.CDFILIAL = P_FILIAL
                     AND IP.NRPEDIDO = P_NRPEDIDO;
               END IF;

               IF (V_TOTALITEM <> 0) THEN
                  IF (C_ENCARGOPED.IDENCARGO = 'A') THEN
                     V_PRODUENCARGO := +(C_ENCARGOPED.VRENCARGOPED/V_TOTALITEM)*C_IMPITEMPEDIDO.VRLIQUIDO;
                  ELSIF (C_ENCARGOPED.IDENCARGO = 'D') THEN
                     V_PRODUENCARGO := -(C_ENCARGOPED.VRENCARGOPED/V_TOTALITEM)*C_IMPITEMPEDIDO.VRLIQUIDO;
                  ELSE
                     V_PRODUENCARGO := 0;
                  END IF;
               END IF;

               /*IMPOSTO PARA SUBSTITUICAO TRIBUTARIA*/
               V_CDIMPOSTRIB  := NULL;
               V_IDVRIMPSTRIB := NULL;
               FOR C_ICMSST IN (SELECT CDIMPOSTO
                                  FROM IMPOSTO
                                 WHERE CDIMPOSTRIB = C_IMPITEMPEDIDO.CDIMPOSTO)
               LOOP
                  V_CDIMPOSTRIB := V_CDIMPOSTRIB||C_ICMSST.CDIMPOSTO||'_';
               END LOOP;

               IF V_CDIMPOSTRIB IS NULL THEN
                  V_TAXABASECALC := (100 - C_IMPITEMPEDIDO.VRPERREDBCITPED)/100;
                  V_TAXAALIQUOTA := (C_IMPITEMPEDIDO.VRALIQITPED/100);

                  IF (NVL(P_IDMOBILE, 'N') = 'N') THEN
                     UPDATE IMPITEMPEDIDO
                        SET VRBASECALCITPED = VRBASECALCITPED + (V_PRODUENCARGO * V_TAXABASECALC),
                            VRIMPOSTOITPED  = (VRIMPOSTOITPED + ((V_PRODUENCARGO * V_TAXABASECALC) * V_TAXAALIQUOTA)),
                            VRACREDESCITPED = (VRIMPOSTOITPED + ((V_PRODUENCARGO * V_TAXABASECALC) * V_TAXAALIQUOTA)),
                            VRBASECALCITFECP = VRBASECALCITPED + (V_PRODUENCARGO * V_TAXABASECALC)
                      WHERE (CDFILIAL = P_FILIAL)
                        AND (NRPEDIDO = P_NRPEDIDO)
                        AND (NRITEMPED = C_IMPITEMPEDIDO.NRITEMPED)
                        AND (CDIMPOSTO = C_IMPITEMPEDIDO.CDIMPOSTO);
                  ELSE
                     UPDATE IMPITEMPEDIDOTRAB
                     SET VRBASECALCITPED = VRBASECALCITPED + (V_PRODUENCARGO * V_TAXABASECALC),
                         VRIMPOSTOITPED  = (VRIMPOSTOITPED + ((V_PRODUENCARGO * V_TAXABASECALC) * V_TAXAALIQUOTA)),
                         VRACREDESCITPED = (VRIMPOSTOITPED + ((V_PRODUENCARGO * V_TAXABASECALC) * V_TAXAALIQUOTA)),
                         VRBASECALCITFECP = VRBASECALCITPED + (V_PRODUENCARGO * V_TAXABASECALC)
                     WHERE (CDFILIAL = P_FILIAL)
                       AND (NRPEDIDO = P_NRPEDIDO)
                       AND (NRITEMPED = C_IMPITEMPEDIDO.NRITEMPED)
                       AND (CDIMPOSTO = C_IMPITEMPEDIDO.CDIMPOSTO);
                  END IF;
               ELSE
                  V_TAXABASECALC := 1 + (C_IMPITEMPEDIDO.VRPERREDBCITPED)/100;

                  IF (NVL(P_IDMOBILE, 'N') = 'N') THEN
                     SELECT MAX(VRBASECALCITPED)
                       INTO V_BCMAJ
                       FROM IMPITEMPEDIDO
                      WHERE (CDFILIAL = P_FILIAL)
                        AND (NRPEDIDO = P_NRPEDIDO)
                        AND (NRITEMPED = P_NRITEMPED)
                        AND (CDIMPOSTO = C_IMPITEMPEDIDO.CDIMPOSTO);
                  ELSE
                     SELECT MAX(VRBASECALCITPED)
                       INTO V_BCMAJ
                       FROM IMPITEMPEDIDOTRAB
                      WHERE (CDFILIAL = P_FILIAL)
                        AND (NRPEDIDO = P_NRPEDIDO)
                        AND (NRITEMPED = P_NRITEMPED)
                        AND (CDIMPOSTO = C_IMPITEMPEDIDO.CDIMPOSTO);
                  END IF;

                  V_BCMAJ := (V_BCMAJ + (V_PRODUENCARGO*V_TAXABASECALC));

                  V_TAXAALIQUOTA := (C_IMPITEMPEDIDO.VRALIQITPED/100);
                  V_IMP := V_BCMAJ * V_TAXAALIQUOTA;

                  IF (C_IMPITEMPEDIDO.VRALIQITFECP IS NOT NULL) AND (C_IMPITEMPEDIDO.VRALIQITFECP > 0) THEN
                     V_TAXAALIQUOTA := ((C_IMPITEMPEDIDO.VRALIQITPED-C_IMPITEMPEDIDO.VRALIQITFECP)/100);
                     V_IMP := V_BCMAJ * V_TAXAALIQUOTA;

                     V_TAXAALIQUOTAFECP := (C_IMPITEMPEDIDO.VRALIQITFECP/100);
                     V_IMPOSTOITFECP    := (V_BCMAJ * V_TAXAALIQUOTAFECP);

                     V_IMP := ROUND(V_IMP, 2) + ROUND(V_IMPOSTOITFECP, 2);

                  END IF;

                  IF (NVL(P_IDMOBILE, 'N') = 'N') THEN
                     UPDATE IMPITEMPEDIDO
                        SET VRBASECALCITPED = V_BCMAJ,
                            VRIMPOSTOITPED  = V_IMP,
                            VRACREDESCITPED = V_IMP,
                            VRBASECALCITFECP = V_BCMAJ
                      WHERE (CDFILIAL = P_FILIAL)
                        AND (NRPEDIDO = P_NRPEDIDO)
                        AND (NRITEMPED = C_IMPITEMPEDIDO.NRITEMPED)
                        AND (CDIMPOSTO = C_IMPITEMPEDIDO.CDIMPOSTO);
                  ELSE
                     UPDATE IMPITEMPEDIDOTRAB
                     SET VRBASECALCITPED = V_BCMAJ,
                         VRIMPOSTOITPED  = V_IMP,
                         VRACREDESCITPED = V_IMP,
                         VRBASECALCITFECP = V_BCMAJ
                     WHERE (CDFILIAL = P_FILIAL)
                       AND (NRPEDIDO = P_NRPEDIDO)
                       AND (NRITEMPED = C_IMPITEMPEDIDO.NRITEMPED)
                       AND (CDIMPOSTO = C_IMPITEMPEDIDO.CDIMPOSTO);
                  END IF;

               END IF;

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

   -- CALCULO DO VALOR DOS IMPOSTOS QUE CONTEM OUTROS IMPOSTOS NA SUA BASE DE CALCULO
   FOR C_PRODUTO IN (SELECT IP.CDPRODUTO, IP.DTENTITPED, II.CDIMPOSTO, VRPERREDBCITPED,
                            VRALIQITPED , VRPARCDEDITPED, CDFISCALITPED, VRIMPOSTOITPED, IDTRATIMPO,
                            IM.IDIMPORECUP, PR.CDARVPROD, CDSITUTRIBITPED, CDFISCOPERITPED,
                            IM.CDIMPOSTONFE, IP.IDUTILBCPMC, II.VRALIQITFECP, II.VRIMPOSTOREP
                     FROM ITEMPEDIDO IP, IMPITEMPEDIDO II, IMPOSTO IM, PRODUTO PR
                     WHERE (IP.CDFILIAL = P_FILIAL)
                       AND (IP.NRPEDIDO = P_NRPEDIDO)
                       AND (IP.NRITEMPED = P_NRITEMPED)
                       AND (II.NRPEDIDO = IP.NRPEDIDO)
                       AND (II.NRITEMPED = IP.NRITEMPED)
                       AND (II.CDFILIAL = IP.CDFILIAL)
                       AND (PR.CDPRODUTO = IP.CDPRODUTO)
                       AND (IM.CDIMPOSTO = II.CDIMPOSTO)
                       AND (NVL(P_IDMOBILE, 'N') = 'N')
                     UNION ALL
                     SELECT IP.CDPRODUTO, IP.DTENTITPED, II.CDIMPOSTO, VRPERREDBCITPED,
                            VRALIQITPED , VRPARCDEDITPED, CDFISCALITPED, VRIMPOSTOITPED, IDTRATIMPO,
                            IM.IDIMPORECUP, PR.CDARVPROD, CDSITUTRIBITPED, CDFISCOPERITPED,
                            IM.CDIMPOSTONFE, IP.IDUTILBCPMC, II.VRALIQITFECP, II.VRIMPOSTOREP
                     FROM ITEMPEDIDOTRAB IP, IMPITEMPEDIDOTRAB II, IMPOSTO IM, PRODUTO PR
                     WHERE (IP.CDFILIAL = P_FILIAL)
                       AND (IP.NRPEDIDO = P_NRPEDIDO)
                       AND (IP.NRITEMPED = P_NRITEMPED)
                       AND (II.NRPEDIDO = IP.NRPEDIDO)
                       AND (II.NRITEMPED = IP.NRITEMPED)
                       AND (II.CDFILIAL = IP.CDFILIAL)
                       AND (PR.CDPRODUTO = IP.CDPRODUTO)
                       AND (IM.CDIMPOSTO = II.CDIMPOSTO)
                       AND (NVL(P_IDMOBILE, 'N') = 'S')
                     ORDER BY CDPRODUTO, CDIMPOSTO)
   LOOP

      IF (C_PRODUTO.VRIMPOSTOREP IS NULL) THEN
         IF NOT((C_PRODUTO.IDUTILBCPMC = 'S') AND (C_PRODUTO.CDIMPOSTONFE = '80')) OR (C_PRODUTO.IDUTILBCPMC <> 'S') THEN
            V_IDOPERACAO := NULL;
            V_TOTAIMPBCALC := 0;
            V_ADIMPAF := 0;

            FOR C_IMPOSTOBC IN (SELECT 0 AS IDOPERACAO,
                                       II.VRIMPOSTOITPED,
                                       I.IDTRATIMPO,
                                       I.IDIMPORECUP,
                                       NVL(BC.IDTRATIMPOBCTPOP, I.IDTRATIMPO) AS IDTRATIMPOBC
                                  FROM ITEMPEDIDO IP, IMPITEMPEDIDO II, BASECALCTPOPER BC, IMPOSTO I
                                 WHERE (IP.CDFILIAL = P_FILIAL)
                                   AND (IP.NRPEDIDO = P_NRPEDIDO)
                                   AND (IP.NRITEMPED = P_NRITEMPED)
                                   AND (IP.CDPRODUTO = C_PRODUTO.CDPRODUTO)
                                   AND (IP.DTENTITPED = C_PRODUTO.DTENTITPED)
                                   AND (II.CDFILIAL = IP.CDFILIAL)
                                   AND (II.NRPEDIDO = IP.NRPEDIDO)
                                   AND (II.NRITEMPED = IP.NRITEMPED)
                                   AND (II.CDIMPOSTO = BC.CDIMPOBCALC)
                                   AND (I.CDIMPOSTO = BC.CDIMPOBCALC)
                                   AND (BC.CDIMPOSTO = C_PRODUTO.CDIMPOSTO)
                                   AND (BC.IDENTRSAIDOP = 'S')
                                   AND (BC.CDTIPOOPER = P_CDTIPOOPER)
                                   AND ((II.CDSITUTRIBITPED = BC.CDCSTIMPOTPOP) OR (BC.CDCSTIMPOTPOP IS NULL))
                                   AND ((SUBSTR(II.CDFISCOPERITPED, 2, 3) = BC.CDCFOPIMPOTPOP) OR (BC.CDCFOPIMPOTPOP IS NULL))
                                   AND ((II.CDFISCALITPED = BC.CDFISCIMPOTPOP) OR (BC.CDFISCIMPOTPOP IS NULL))
                                   AND (NVL(P_IDMOBILE, 'N') = 'N')
                                UNION ALL
                                SELECT 1 AS IDOPERACAO,
                                       II.VRIMPOSTOITPED,
                                       I.IDTRATIMPO,
                                       I.IDIMPORECUP,
                                       NVL(BC.IDTRATIMPOBC, I.IDTRATIMPO) AS IDTRATIMPOBC
                                  FROM ITEMPEDIDO IP, IMPITEMPEDIDO II, BASECALC BC, IMPOSTO I
                                 WHERE (IP.CDFILIAL = P_FILIAL)
                                   AND (IP.NRPEDIDO = P_NRPEDIDO)
                                   AND (IP.NRITEMPED = P_NRITEMPED)
                                   AND (IP.CDPRODUTO = C_PRODUTO.CDPRODUTO)
                                   AND (IP.DTENTITPED = C_PRODUTO.DTENTITPED)
                                   AND (II.CDFILIAL = IP.CDFILIAL)
                                   AND (II.NRPEDIDO = IP.NRPEDIDO)
                                   AND (II.NRITEMPED = IP.NRITEMPED)
                                   AND (II.CDIMPOSTO = BC.CDIMPOBCALC)
                                   AND (I.CDIMPOSTO = BC.CDIMPOBCALC)
                                   AND (BC.CDIMPOSTO = C_PRODUTO.CDIMPOSTO)
                                   AND ((II.CDSITUTRIBITPED = BC.CDCSTIMPO) OR (BC.CDCSTIMPO IS NULL))
                                   AND ((SUBSTR(II.CDFISCOPERITPED, 2, 3) = BC.CDCFOPIMPO) OR (BC.CDCFOPIMPO IS NULL))
                                   AND ((II.CDFISCALITPED = BC.CDFISCIMPO) OR (BC.CDFISCIMPO IS NULL))
                                   AND (BC.IDTIPOBCALC = 'I')
                                   AND (NVL(P_IDMOBILE, 'N') = 'N')
                                UNION ALL

                                SELECT 0 AS IDOPERACAO,
                                       II.VRIMPOSTOITPED,
                                       I.IDTRATIMPO,
                                       I.IDIMPORECUP,
                                       NVL(BC.IDTRATIMPOBCTPOP, I.IDTRATIMPO) AS IDTRATIMPOBC
                                  FROM ITEMPEDIDOTRAB IP, IMPITEMPEDIDOTRAB II, BASECALCTPOPER BC, IMPOSTO I
                                 WHERE (IP.CDFILIAL = P_FILIAL)
                                   AND (IP.NRPEDIDO = P_NRPEDIDO)
                                   AND (IP.NRITEMPED = P_NRITEMPED)
                                   AND (IP.CDPRODUTO = C_PRODUTO.CDPRODUTO)
                                   AND (IP.DTENTITPED = C_PRODUTO.DTENTITPED)
                                   AND (II.CDFILIAL = IP.CDFILIAL)
                                   AND (II.NRPEDIDO = IP.NRPEDIDO)
                                   AND (II.NRITEMPED = IP.NRITEMPED)
                                   AND (II.CDIMPOSTO = BC.CDIMPOBCALC)
                                   AND (I.CDIMPOSTO = BC.CDIMPOBCALC)
                                   AND (BC.CDIMPOSTO = C_PRODUTO.CDIMPOSTO)
                                   AND (BC.IDENTRSAIDOP = 'S')
                                   AND (BC.CDTIPOOPER = P_CDTIPOOPER)
                                   AND ((II.CDSITUTRIBITPED = BC.CDCSTIMPOTPOP) OR (BC.CDCSTIMPOTPOP IS NULL))
                                   AND ((SUBSTR(II.CDFISCOPERITPED, 2, 3) = BC.CDCFOPIMPOTPOP) OR (BC.CDCFOPIMPOTPOP IS NULL))
                                   AND ((II.CDFISCALITPED = BC.CDFISCIMPOTPOP) OR (BC.CDFISCIMPOTPOP IS NULL))
                                   AND (NVL(P_IDMOBILE, 'N') = 'S')
                                UNION ALL
                                SELECT 1 AS IDOPERACAO,
                                       II.VRIMPOSTOITPED,
                                       I.IDTRATIMPO,
                                       I.IDIMPORECUP,
                                       NVL(BC.IDTRATIMPOBC, I.IDTRATIMPO) AS IDTRATIMPOBC
                                  FROM ITEMPEDIDOTRAB IP, IMPITEMPEDIDOTRAB II, BASECALC BC, IMPOSTO I
                                 WHERE (IP.CDFILIAL = P_FILIAL)
                                   AND (IP.NRPEDIDO = P_NRPEDIDO)
                                   AND (IP.NRITEMPED = P_NRITEMPED)
                                   AND (IP.CDPRODUTO = C_PRODUTO.CDPRODUTO)
                                   AND (IP.DTENTITPED = C_PRODUTO.DTENTITPED)
                                   AND (II.CDFILIAL = IP.CDFILIAL)
                                   AND (II.NRPEDIDO = IP.NRPEDIDO)
                                   AND (II.NRITEMPED = IP.NRITEMPED)
                                   AND (II.CDIMPOSTO = BC.CDIMPOBCALC)
                                   AND (I.CDIMPOSTO = BC.CDIMPOBCALC)
                                   AND (BC.CDIMPOSTO = C_PRODUTO.CDIMPOSTO)
                                   AND ((II.CDSITUTRIBITPED = BC.CDCSTIMPO) OR (BC.CDCSTIMPO IS NULL))
                                   AND ((SUBSTR(II.CDFISCOPERITPED, 2, 3) = BC.CDCFOPIMPO) OR (BC.CDCFOPIMPO IS NULL))
                                   AND ((II.CDFISCALITPED = BC.CDFISCIMPO) OR (BC.CDFISCIMPO IS NULL))
                                   AND (BC.IDTIPOBCALC = 'I')
                                   AND (NVL(P_IDMOBILE, 'N') = 'S')

                                  ORDER BY 1)
            LOOP

               IF V_IDOPERACAO IS NULL THEN
                  V_IDOPERACAO := C_IMPOSTOBC.IDOPERACAO;
               END IF;

               -- ESSA CONDICAO FOI COLOCADA PARA DEFINIR A PRIORIDADE. QUANDO EXISTIR REGISTRO NA BASECALCTPOPER O IDOPERACAO SERA ZERO (0)
               -- E COM ISTO O SISTEMA SO REALIZARA O CALCULO EM CIMA DA BASECALCTPOPER. CASO NAO TENHA REGISTRO NESTA TABELA O SISTEMA REALIARA
               -- O CALCULO PARA O IDOPERACAO 1, OU SEJA, SOMENTE OS REGISTROS DA TABELA BASECALC
               IF V_IDOPERACAO = C_IMPOSTOBC.IDOPERACAO THEN

                  IF C_IMPOSTOBC.IDTRATIMPOBC = 'D' THEN
                     V_TOTAIMPBCALC := C_IMPOSTOBC.VRIMPOSTOITPED * (- 1);
                  ELSE
                     V_TOTAIMPBCALC := C_IMPOSTOBC.VRIMPOSTOITPED;
                  END IF;

                  SELECT COUNT(CDIMPOSTO)
                    INTO V_CTSTRIB
                    FROM IMPOSTO
                   WHERE CDIMPOSTRIB = C_PRODUTO.CDIMPOSTO;

                  IF V_CTSTRIB > 0 THEN
                     V_TAXABASECALC := 1 + (C_PRODUTO.VRPERREDBCITPED/100);
                  ELSE
                     V_TAXABASECALC := (100 - C_PRODUTO.VRPERREDBCITPED)/100;
                  END IF;

                  V_TAXAALIQUOTA := (C_PRODUTO.VRALIQITPED/100);

                  V_ADIMPAF := 0;

                  IF (C_IMPOSTOBC.IDTRATIMPO = 'A') THEN
                     V_ADIMPAF := ((V_TOTAIMPBCALC * V_TAXABASECALC) * V_TAXAALIQUOTA);

                  ELSIF (C_IMPOSTOBC.IDTRATIMPO = 'D') THEN
                     V_ADIMPAF := - ((V_TOTAIMPBCALC * V_TAXABASECALC) * V_TAXAALIQUOTA);

                  ELSIF (C_IMPOSTOBC.IDTRATIMPO  = 'R') THEN
                     IF (C_IMPOSTOBC.IDIMPORECUP = 'S') THEN
                        IF (C_PRODUTO.CDFISCALITPED <> '1') THEN
                           V_ADIMPAF := - ((V_TOTAIMPBCALC * V_TAXABASECALC) * V_TAXAALIQUOTA);
                        END IF;
                     ELSE
                        V_ADIMPAF := - ((V_TOTAIMPBCALC * V_TAXABASECALC) * V_TAXAALIQUOTA);
                     END IF;
                  END IF;

                  IF (C_PRODUTO.IDTRATIMPO = 'V') OR (C_PRODUTO.IDTRATIMPO = 'R') THEN
                     V_ADIMPAF := 0;
                  END IF;

                  /*ponto de alteracao ok*/
                  IF V_CTSTRIB > 0 THEN
                     IF (NVL(P_IDMOBILE, 'N') = 'N') THEN
                        SELECT MAX(VRBASECALCITPED)
                          INTO V_BCMAJ
                          FROM IMPITEMPEDIDO
                         WHERE (CDFILIAL = P_FILIAL)
                           AND (NRPEDIDO = P_NRPEDIDO)
                           AND (NRITEMPED = P_NRITEMPED)
                           AND (CDIMPOSTO = C_PRODUTO.CDIMPOSTO);
                     ELSE
                        SELECT MAX(VRBASECALCITPED)
                          INTO V_BCMAJ
                          FROM IMPITEMPEDIDOTRAB
                         WHERE (CDFILIAL = P_FILIAL)
                           AND (NRPEDIDO = P_NRPEDIDO)
                           AND (NRITEMPED = P_NRITEMPED)
                           AND (CDIMPOSTO = C_PRODUTO.CDIMPOSTO);
                     END IF;
                     V_BCMAJ := (V_BCMAJ + (V_TOTAIMPBCALC*V_TAXABASECALC));
                     V_IMP := V_BCMAJ * V_TAXAALIQUOTA;

                     IF (C_PRODUTO.VRALIQITFECP IS NOT NULL) AND (C_PRODUTO.VRALIQITFECP > 0) THEN
                        V_TAXAALIQUOTA := ((C_PRODUTO.VRALIQITPED-C_PRODUTO.VRALIQITFECP)/100);
                        V_IMP := V_BCMAJ * V_TAXAALIQUOTA;

                        V_TAXAALIQUOTAFECP := (C_PRODUTO.VRALIQITFECP/100);
                        V_IMPOSTOITFECP    := (V_BCMAJ * V_TAXAALIQUOTAFECP);

                        V_IMP := ROUND(V_IMP, 2) + ROUND(V_IMPOSTOITFECP, 2);

                     END IF;

                     IF (NVL(P_IDMOBILE, 'N') = 'N') THEN
                        UPDATE IMPITEMPEDIDO
                           SET VRBASECALCITPED = V_BCMAJ,
                               VRIMPOSTOITPED = V_IMP,
                               VRACREDESCITPED = V_IMP,
                               VRBASECALCITFECP = V_BCMAJ
                         WHERE (CDFILIAL = P_FILIAL)
                           AND (NRPEDIDO = P_NRPEDIDO)
                           AND (NRITEMPED = P_NRITEMPED)
                           AND (CDIMPOSTO = C_PRODUTO.CDIMPOSTO);
                     ELSE
                        UPDATE IMPITEMPEDIDOTRAB
                        SET VRBASECALCITPED = V_BCMAJ,
                            VRIMPOSTOITPED = V_IMP,
                            VRACREDESCITPED = V_IMP,
                            VRBASECALCITFECP = V_BCMAJ
                         WHERE (CDFILIAL = P_FILIAL)
                           AND (NRPEDIDO = P_NRPEDIDO)
                           AND (NRITEMPED = P_NRITEMPED)
                           AND (CDIMPOSTO = C_PRODUTO.CDIMPOSTO);
                     END IF;
                  ELSE
                     IF (NVL(P_IDMOBILE, 'N') = 'N') THEN
                        UPDATE IMPITEMPEDIDO
                           SET VRBASECALCITPED = VRBASECALCITPED + (V_TOTAIMPBCALC * V_TAXABASECALC),
                               VRIMPOSTOITPED  = ROUND(VRIMPOSTOITPED  + ((V_TOTAIMPBCALC * V_TAXABASECALC) * V_TAXAALIQUOTA), 2),
                               VRACREDESCITPED = ROUND(VRACREDESCITPED + V_ADIMPAF, 2),
                               VRBASECALCITFECP = V_BCMAJ
                         WHERE (CDFILIAL = P_FILIAL)
                           AND (NRPEDIDO = P_NRPEDIDO)
                           AND (NRITEMPED = P_NRITEMPED)
                           AND (CDIMPOSTO = C_PRODUTO.CDIMPOSTO);
                     ELSE
                        UPDATE IMPITEMPEDIDOTRAB
                           SET VRBASECALCITPED = VRBASECALCITPED + (V_TOTAIMPBCALC * V_TAXABASECALC),
                               VRIMPOSTOITPED  = ROUND(VRIMPOSTOITPED  + ((V_TOTAIMPBCALC * V_TAXABASECALC) * V_TAXAALIQUOTA), 2),
                               VRACREDESCITPED = ROUND(VRACREDESCITPED + V_ADIMPAF, 2),
                               VRBASECALCITFECP = V_BCMAJ
                         WHERE (CDFILIAL = P_FILIAL)
                           AND (NRPEDIDO = P_NRPEDIDO)
                           AND (NRITEMPED = P_NRITEMPED)
                           AND (CDIMPOSTO = C_PRODUTO.CDIMPOSTO);
                     END IF;
                  END IF;
               END IF;
            END LOOP;

            -- ROTINA PARA RECALCULAR O ICMS ST CONSIDERANDO % CARGA TRIBUTARIA MEDIA
            SELECT COUNT(CDIMPOSTO)
              INTO V_CTSTRIB
              FROM IMPOSTO
             WHERE CDIMPOSTRIB = C_PRODUTO.CDIMPOSTO;

            IF V_CTSTRIB > 0 THEN
               BEGIN
                  SELECT NVL(VRCNAEBC, 0),
                         NVL(VRMVA, 0)
                    INTO V_VRPRODUTOBC,
                         V_VRMVA
                    FROM VRPERCCARGTRIBCNAE
                   WHERE IDENTRSAIDOP = 'S'
                     AND CDTIPOOPER = P_CDTIPOOPER
                     AND CDPAIS = V_PAISDEST
                     AND SGESTADO = V_ESTADEST
                     AND CDCNAE = V_CDCNAECLIE
                     AND IDOPTANTESN = V_IDOPTANTESNCLIE;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN
                     V_VRPRODUTOBC := 0;
                     V_VRMVA := 0;
               END;
               IF V_VRPRODUTOBC = 0 THEN
                  BEGIN
                     SELECT NVL(VRPRODUTOBC, 0)
                       INTO V_VRPRODUTOBC
                       FROM VRPERCCARGTRIB
                      WHERE IDENTRSAIDOP = 'S'
                        AND CDTIPOOPER = P_CDTIPOOPER
                        AND CDPAIS = V_PAISDEST
                        AND SGESTADO = V_ESTADEST
                        AND CDPRODUTO = C_PRODUTO.CDPRODUTO;
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                        V_VRPRODUTOBC := 0;
                  END;
               END IF;

               IF (V_VRPRODUTOBC > 0) THEN
                  IF (NVL(P_IDMOBILE, 'N') = 'N') THEN
                     BEGIN
                        SELECT I.CDIMPOSTO, I.VRIMPOSTOITPED, I.VRBASECALCITPED
                          INTO V_CDIMPOSTOICMS, V_VRIMPOSTOICMS, V_BASECALCICMS
                          FROM IMPOSTONFE N, IMPOSTO P, IMPITEMPEDIDO I
                         WHERE N.CDIMPOSTONFE = P.CDIMPOSTONFE
                           AND N.CDIMPOSTONFE = '00'
                           AND I.CDFILIAL = P_FILIAL
                           AND I.NRPEDIDO = P_NRPEDIDO
                           AND I.NRITEMPED = P_NRITEMPED
                           AND I.CDIMPOSTO = NVL(P.CDIMPOSREGNORMA, P.CDIMPOSTO);
                     EXCEPTION
                        WHEN NO_DATA_FOUND THEN
                           V_CDIMPOSTOICMS := NULL;
                           V_VRIMPOSTOICMS := 0;
                           V_BASECALCICMS := 0;
                     END;
                  ELSE
                     BEGIN
                        SELECT I.CDIMPOSTO, I.VRIMPOSTOITPED, I.VRBASECALCITPED
                          INTO V_CDIMPOSTOICMS, V_VRIMPOSTOICMS, V_BASECALCICMS
                          FROM IMPOSTONFE N, IMPOSTO P, IMPITEMPEDIDOTRAB I
                         WHERE N.CDIMPOSTONFE = P.CDIMPOSTONFE
                           AND N.CDIMPOSTONFE = '00'
                           AND I.CDFILIAL = P_FILIAL
                           AND I.NRPEDIDO = P_NRPEDIDO
                           AND I.NRITEMPED = P_NRITEMPED
                           AND I.CDIMPOSTO = NVL(P.CDIMPOSREGNORMA, P.CDIMPOSTO);
                     EXCEPTION
                        WHEN NO_DATA_FOUND THEN
                           V_CDIMPOSTOICMS := NULL;
                           V_VRIMPOSTOICMS := 0;
                           V_BASECALCICMS := 0;
                     END;
                  END IF;

                  DELETE ALIQTEMP
                   WHERE NRALIQTEMP = V_ALIQTEMP;

                  SELECT MAX(IDGRUPFISSRV)
                  INTO V_UTGRUPFISCS
                  FROM IMPOSTO
                  WHERE CDIMPOSTO = V_CDIMPOSTOICMS;

                  IF V_UTGRUPFISCS = 'S' THEN
                     V_CDGRPFISCSV := NULL;
                     SELECT MAX(CDGRUPOFISC)
                       INTO V_CDGRPFISCSV
                       FROM SERVCLIE SC, SERVICO S
                      WHERE SC.CDFILIAL = P_FILIAL
                        AND SC.CDCLIENTE = P_CLIENTE
                        AND SC.CDFILIAL = S.CDFILIAL
                        AND SC.CDSERVFILI = S.CDSERVFILI
                        AND S.CDPRODUTO = C_PRODUTO.CDPRODUTO;

                     IF V_CDGRPFISCSV IS NULL THEN
                        SELECT MAX(CDGRUPOFISC)
                          INTO V_CDGRPFISCSV
                          FROM CLIENFILIAL
                         WHERE CDFILIAL   = P_FILIAL
                           AND CDCLIENTE  = P_CLIENTE;
                     END IF;

                     CONSULTA_ALIQUOTA(V_CDIMPOSTOICMS,
                                       'S',
                                       P_EMPRESA,
                                       C_PRODUTO.CDARVPROD,
                                       P_INSCESTA,
                                       V_PAISDEST,
                                       V_ESTADEST,
                                       V_PAISDEST,
                                       V_ESTADEST,
                                       V_MUNIDEST,
                                       V_LIQUIITEM,
                                       P_INIVALID,
                                       P_FILIAL||P_NRPEDIDO,
                                       SUBSTR(P_NRITEMPED,7,4),
                                       V_CDGRPFISCSV,
                                       P_CDTIPOOPER,
                                       'PED',
                                       NULL,
                                       NULL,
                                       NULL,
                                       NULL,
                                       NULL,
                                       'N');
                  ELSE
                     CONSULTA_ALIQUOTA(V_CDIMPOSTOICMS,
                                       'S',
                                       P_EMPRESA,
                                       C_PRODUTO.CDARVPROD,
                                       P_INSCESTA,
                                       V_PAISDEST,
                                       V_ESTADEST,
                                       V_PAISDEST,
                                       V_ESTADEST,
                                       V_MUNIDEST,
                                       V_LIQUIITEM,
                                       P_INIVALID,
                                       P_FILIAL||P_NRPEDIDO,
                                       SUBSTR(P_NRITEMPED,7,4),
                                       V_CDGRUPOFISC,
                                       P_CDTIPOOPER,
                                       'PED',
                                       NULL,
                                       NULL,
                                       NULL,
                                       NULL,
                                       NULL,
                                       'N');
                  END IF;

                  SELECT NVL(MAX(VRALIQUTMP),0)
                    INTO V_VRALIQUTMPDEST
                    FROM ALIQTEMP
                   WHERE (NRALIQTEMP = V_ALIQTEMP);

                  IF (V_VRALIQUTMPDEST <> 0) THEN
                     IF V_VRMVA > 0 THEN
                        V_BASECALCICMS := (V_BASECALCICMS * (V_VRMVA/100));
                     END IF;
                     V_IMPOSTOIT :=  ROUND(V_BASECALCICMS * (V_VRPRODUTOBC/100), 2);
                     V_BASECALCIT :=  ROUND(((V_VRIMPOSTOICMS + V_IMPOSTOIT) / (V_VRALIQUTMPDEST/100)),2);

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

                     IF (NVL(P_IDMOBILE, 'N') = 'N') THEN
                        UPDATE IMPITEMPEDIDO
                           SET VRBASECALCITPED = V_BASECALCIT,
                               VRIMPOSTOITPED = V_IMPOSTOIT,
                               VRACREDESCITPED = V_IMPOSTOIT
                         WHERE CDFILIAL = P_FILIAL
                           AND NRPEDIDO = P_NRPEDIDO
                           AND NRITEMPED = P_NRITEMPED
                           AND CDIMPOSTO = C_PRODUTO.CDIMPOSTO;
                     ELSE
                        UPDATE IMPITEMPEDIDOTRAB
                           SET VRBASECALCITPED = V_BASECALCIT,
                               VRIMPOSTOITPED = V_IMPOSTOIT,
                               VRACREDESCITPED = V_IMPOSTOIT
                         WHERE CDFILIAL = P_FILIAL
                           AND NRPEDIDO = P_NRPEDIDO
                           AND NRITEMPED = P_NRITEMPED
                           AND CDIMPOSTO = C_PRODUTO.CDIMPOSTO;
                     END IF;
                  END IF;

                  DELETE ALIQTEMP
                   WHERE NRALIQTEMP = V_ALIQTEMP;
               END IF;
            END IF;
         END IF;
      END IF;
   END LOOP;

   FOR C_IMPOSTOS IN (SELECT IP.CDFILIAL,
                             IP.NRPEDIDO,
                             IP.NRITEMPED,
                             IP.CDIMPOSTO,
                             I.CDIMPOSTRIB,
                             IP.VRIMPOSTOITPED
                        FROM IMPITEMPEDIDO IP JOIN IMPOSTO I ON (IP.CDIMPOSTO = I.CDIMPOSTO)
                       WHERE IP.CDFILIAL = P_FILIAL
                         AND IP.NRPEDIDO = P_NRPEDIDO
                         AND IP.NRITEMPED = P_NRITEMPED
                         AND I.CDIMPOSTRIB IS NOT NULL
                         AND NVL(IDVRIMPSTRIB, 'S') = 'S'
                         AND EXISTS (SELECT CDFILIAL, NRPEDIDO, NRITEMPED
                                       FROM IMPITEMPEDIDO A
                                      WHERE A.CDFILIAL = IP.CDFILIAL
                                        AND A.NRPEDIDO = IP.NRPEDIDO
                                        AND A.NRITEMPED = IP.NRITEMPED
                                        AND A.CDIMPOSTO = I.CDIMPOSTRIB)
                         AND (NVL(P_IDMOBILE, 'N') = 'N')
                      UNION ALL
                      SELECT IP.CDFILIAL,
                             IP.NRPEDIDO,
                             IP.NRITEMPED,
                             IP.CDIMPOSTO,
                             I.CDIMPOSTRIB,
                             IP.VRIMPOSTOITPED
                        FROM IMPITEMPEDIDOTRAB IP JOIN IMPOSTO I ON (IP.CDIMPOSTO = I.CDIMPOSTO)
                       WHERE IP.CDFILIAL = P_FILIAL
                         AND IP.NRPEDIDO = P_NRPEDIDO
                         AND IP.NRITEMPED = P_NRITEMPED
                         AND I.CDIMPOSTRIB IS NOT NULL
                         AND NVL(IDVRIMPSTRIB, 'S') = 'S'
                         AND EXISTS (SELECT CDFILIAL, NRPEDIDO, NRITEMPED
                                       FROM IMPITEMPEDIDOTRAB A
                                      WHERE A.CDFILIAL = IP.CDFILIAL
                                        AND A.NRPEDIDO = IP.NRPEDIDO
                                        AND A.NRITEMPED = IP.NRITEMPED
                                        AND A.CDIMPOSTO = I.CDIMPOSTRIB)
                         AND (NVL(P_IDMOBILE, 'N') = 'S')               )
   LOOP
      SELECT NVL(VRIMPOSTOITPED,0)
        INTO V_VRIMPOSTOITPED
        FROM IMPITEMPEDIDO
       WHERE CDFILIAL = C_IMPOSTOS.CDFILIAL
         AND NRPEDIDO = C_IMPOSTOS.NRPEDIDO
         AND NRITEMPED = C_IMPOSTOS.NRITEMPED
         AND CDIMPOSTO = C_IMPOSTOS.CDIMPOSTRIB;
      IF C_IMPOSTOS.VRIMPOSTOITPED > V_VRIMPOSTOITPED THEN
         IF (NVL(P_IDMOBILE, 'N') = 'N') THEN
            UPDATE IMPITEMPEDIDO
               SET VRIMPOSTOITPED  = 0,
                   VRACREDESCITPED = 0
             WHERE CDFILIAL = C_IMPOSTOS.CDFILIAL
               AND NRPEDIDO = C_IMPOSTOS.NRPEDIDO
               AND NRITEMPED = C_IMPOSTOS.NRITEMPED
               AND CDIMPOSTO = C_IMPOSTOS.CDIMPOSTRIB;
         ELSE
            UPDATE IMPITEMPEDIDOTRAB
               SET VRIMPOSTOITPED  = 0,
                   VRACREDESCITPED = 0
             WHERE CDFILIAL = C_IMPOSTOS.CDFILIAL
               AND NRPEDIDO = C_IMPOSTOS.NRPEDIDO
               AND NRITEMPED = C_IMPOSTOS.NRITEMPED
               AND CDIMPOSTO = C_IMPOSTOS.CDIMPOSTRIB;
         END IF;
      ELSE
         IF (NVL(P_IDMOBILE, 'N') = 'N') THEN
            UPDATE IMPITEMPEDIDO
               SET VRIMPOSTOITPED = VRIMPOSTOITPED - C_IMPOSTOS.VRIMPOSTOITPED,
                   VRACREDESCITPED = VRACREDESCITPED- C_IMPOSTOS.VRIMPOSTOITPED
             WHERE CDFILIAL = C_IMPOSTOS.CDFILIAL
               AND NRPEDIDO = C_IMPOSTOS.NRPEDIDO
               AND NRITEMPED = C_IMPOSTOS.NRITEMPED
               AND CDIMPOSTO = C_IMPOSTOS.CDIMPOSTRIB;
         ELSE
            UPDATE IMPITEMPEDIDOTRAB
               SET VRIMPOSTOITPED = VRIMPOSTOITPED - C_IMPOSTOS.VRIMPOSTOITPED,
                   VRACREDESCITPED = VRACREDESCITPED- C_IMPOSTOS.VRIMPOSTOITPED
             WHERE CDFILIAL = C_IMPOSTOS.CDFILIAL
               AND NRPEDIDO = C_IMPOSTOS.NRPEDIDO
               AND NRITEMPED = C_IMPOSTOS.NRITEMPED
               AND CDIMPOSTO = C_IMPOSTOS.CDIMPOSTRIB;
         END IF;
      END IF;
   END LOOP;
END;
/