CREATE OR REPLACE PROCEDURE IMPOSTO_ITEMAF(
   P_EMPRESA    IN ALIQUOTA.CDEMPRESA%TYPE,
   P_INSCESTA   IN ALIQUOTA.CDINSCESTA%TYPE,
   P_FORNECEDOR IN FORNECEDOR.CDFORNECED%TYPE,
   P_FILDEST    IN AUTOFORNE.CDFILDEST%TYPE,
   P_FILLANC    IN AUTOFORNE.CDFILLANC%TYPE,
   P_LANCTOAF   IN AUTOFORNE.NRAUTOFORN%TYPE,
   P_INIVALID   IN ALIQUOTA.DTINIVALID%TYPE)

IS

   V_PAISORIG      VARCHAR2(04);
   V_ESTAORIG      VARCHAR2(02);
   V_MUNIORIG      VARCHAR2(04);
   V_PAISDEST      VARCHAR2(04);
   V_ESTADEST      VARCHAR2(02);
   V_ALIQTEMP      VARCHAR2(18);
   V_INCITEMP      VARCHAR2(18);
   V_TIPOOPER      VARCHAR2(02);
   V_CARACFIS      VARCHAR2(01);
   V_LIQUIITEM     NUMBER;
   V_PARDEDTMP     NUMBER;
   V_PREDBCTMP     NUMBER;
   V_ALIQUTMP      NUMBER;
   V_IMPOSTOIT     NUMBER;
   V_TAXABASECALC  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_IDCSTINTEREST VARCHAR2(01);

   V_FISOPEITEM   VARCHAR2(11);
   V_SITUTRIBPR   VARCHAR2(03);

   V_VRIMPITAFAUX NUMBER;


   V_CDGRUPOFISC VARCHAR2(3);

   V_IDTRATIMPBC   VARCHAR2(1);
   V_IDIMPORECUPBC VARCHAR2(1);
   V_IDCSTINTERNA  VARCHAR2(01);
   V_CDIMPOBCALTPOP IMPOSTO.CDIMPOSTO%TYPE;
   V_TAXABCSUBSTR NUMBER;
   V_PBCSUBTRIB   NUMBER;
   V_BASEVLRMAX   NUMBER;
   V_VRMAXVENDA   NUMBER;
   V_CALCVLRMAX   NUMBER;
   V_VRPEMAXVENDA NUMBER;
   V_IDPMCBASECAL VARCHAR2(01);
   V_CDIMPOSTRIB  VARCHAR2(2000);
   V_IDVRIMPSTRIB VARCHAR2(01);
   V_VRIMPITMAF   NUMBER;
   V_IDUTILVRMAX      VARCHAR2(01);
   V_VRDESCMAXVENDA   FLOAT;
   V_FINALVRMAXVENDA  FLOAT;
BEGIN
   V_CSTTEMP  := P_FILDEST||P_FILLANC||P_LANCTOAF;
   V_CFOPTEMP := P_FILDEST||P_FILLANC||P_LANCTOAF;

   CONSULTA_GRUPOFISCAL('F', P_FORNECEDOR, '', 'E', '01', V_CDGRUPOFISC);

   V_ALIQTEMP := P_FILDEST||P_FILLANC||P_LANCTOAF;
   V_INCITEMP := P_FILDEST||P_FILLANC||P_LANCTOAF;
   SELECT MAX(CDPAIS), MAX(SGESTADO) INTO V_PAISDEST, V_ESTADEST
   FROM INSCRESTAD
   WHERE (CDEMPRESA = P_EMPRESA) AND
         (CDINSCESTA = P_INSCESTA);

   SELECT MAX(CDPAIS), MAX(SGESTADO), MAX(CDMUNICIPIO) INTO V_PAISORIG, V_ESTAORIG, V_MUNIORIG
   FROM ENDEFORN
   WHERE (CDFORNECED   = P_FORNECEDOR) AND
         (IDTPENDEFORN = 'P');

   V_IDCSTINTEREST := 'N';
   V_IDCSTINTERNA  := 'N';
   IF (V_PAISDEST = V_PAISORIG) AND (V_ESTADEST <> V_ESTAORIG) THEN
      V_IDCSTINTEREST := 'S';
   ELSIF (V_PAISDEST <> V_PAISORIG) THEN
      V_IDCSTINTERNA := 'S';
   END IF;

   SELECT
      NVL(MAX(PARCMPSUP.CDTIPOOPER), '01') INTO V_TIPOOPER
   FROM
      PARCMPSUP,
      FILIAL
   WHERE
      (FILIAL.CDFILIAL    = P_FILDEST) AND
      (PARCMPSUP.CDFILIAL = NVL(FILIAL.CDFILCENTCMP, FILIAL.CDFILIAL));

   SELECT MAX(IDCARACFIS) INTO V_CARACFIS
   FROM PARAMFILIAL
   WHERE CDFILIAL = P_FILDEST;
   FOR C_ITEMAUTF IN (SELECT QTPROGENTAF, VRPREUNIAF, VRPERDESAF, ITEMAUTF.CDPRODUTO,
                             CDARVPROD, CDMARCA, DTPROGENTAF
                      FROM ITEMAUTF,
                           PRODUTO
                      WHERE CDFILDEST  = P_FILDEST AND
                            CDFILLANC  = P_FILLANC AND
                            ITEMAUTF.CDPRODUTO = PRODUTO.CDPRODUTO AND
                            NRAUTOFORN = P_LANCTOAF) LOOP

      IF (C_ITEMAUTF.VRPERDESAF IS NULL) THEN
         V_LIQUIITEM := (C_ITEMAUTF.VRPREUNIAF * C_ITEMAUTF.QTPROGENTAF);
      ELSE
         V_LIQUIITEM := (C_ITEMAUTF.VRPREUNIAF * C_ITEMAUTF.QTPROGENTAF) * ((100 - C_ITEMAUTF.VRPERDESAF)/100);
      END IF;
      CONSULTA_INCIDENCIA('E', P_EMPRESA, C_ITEMAUTF.CDARVPROD, P_INSCESTA, V_TIPOOPER, P_FILDEST||P_FILLANC, P_LANCTOAF, V_CDGRUPOFISC, null, V_IDCSTINTEREST, null, null, P_FORNECEDOR);


      DELETE IMPITMAF
      WHERE (CDFILDEST   = P_FILDEST)  AND
            (CDFILLANC   = P_FILLANC)  AND
            (NRAUTOFORN  = P_LANCTOAF) AND
            (CDMARCA     = C_ITEMAUTF.CDMARCA)     AND
            (DTPROGENTAF = C_ITEMAUTF.DTPROGENTAF) AND
            (CDPRODUTO   = C_ITEMAUTF.CDPRODUTO) AND
            (IDIMPDIGAF  = 'N');


      FOR C_INCIDENCIA IN (SELECT DISTINCT (CDIMPOTEMP) AS CDIMPOTEMP, CDFISCTEMP,
                                  IP.IDTRATIMPO, IP.IDIMPORECUP
                           FROM INCITEMP IT, IMPOSTO IP
                           WHERE (IT.NRINCITEMP = V_INCITEMP)    AND
                                 (IP.CDIMPOSTO  = IT.CDIMPOTEMP) AND
                                 (IP.IDPRODNOTA = 'P')
                           ORDER BY CDIMPOTEMP) LOOP


         CONSULTA_ALIQUOTA(C_INCIDENCIA.CDIMPOTEMP, 'E', P_EMPRESA, C_ITEMAUTF.CDARVPROD, P_INSCESTA, V_PAISORIG,
                           V_ESTAORIG, V_PAISDEST, V_ESTADEST, V_MUNIORIG, V_LIQUIITEM, P_INIVALID, P_FILDEST||P_FILLANC, P_LANCTOAF, V_CDGRUPOFISC, V_TIPOOPER);

         V_PARDEDTMP  := 0;
         V_PREDBCTMP  := 0;
         V_ALIQUTMP   := 0;
         V_IMPOSTOIT  := 0;

         SELECT MAX(VRPARDEDTMP), MAX(VRPREDBCTMP), MAX(VRALIQUTMP), NVL(MAX(VRPBCSTTMP),0),  NVL(MAX(IDPMCBSCALTMP), 'S')
           INTO V_PARDEDTMP, V_PREDBCTMP, V_ALIQUTMP, V_PBCSUBTRIB, V_IDPMCBASECAL
           FROM ALIQTEMP
          WHERE (NRALIQTEMP = V_ALIQTEMP);

         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_TAXABASECALC := (100 - V_PREDBCTMP)/100;
         V_TAXAALIQUOTA := (V_ALIQUTMP/100);
         V_TAXABCSUBSTR := 1 + (V_PBCSUBTRIB/100);
         V_BASECALCIT   := V_LIQUIITEM * V_TAXABASECALC;

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

         V_BASEVLRMAX   := 0;
         V_VRPEMAXVENDA := 0;
         V_VRMAXVENDA   := 0;
         V_CALCVLRMAX   := 0;

         /*IMPOSTO DE SUBSTITUICAO TRIBUTARIA*/
         V_CDIMPOSTRIB := '';
         FOR C_ICMSST IN (SELECT CDIMPOSTO
                                FROM IMPOSTO
                               WHERE CDIMPOSTRIB = C_INCIDENCIA.CDIMPOTEMP)
         LOOP
            V_CDIMPOSTRIB := V_CDIMPOSTRIB||C_ICMSST.CDIMPOSTO||'_';
         END LOOP;

         IF V_CDIMPOSTRIB IS NULL THEN
            V_IMPOSTOIT    := ((V_LIQUIITEM * V_TAXABASECALC) * V_TAXAALIQUOTA) - V_PARDEDTMP;
         ELSE
            V_IMPOSTOIT   := (V_BASECALCIT * V_TAXAALIQUOTA) - V_PARDEDTMP;

            SELECT NVL(MAX(IDVRIMPSTRIB), 'N')
              INTO V_IDVRIMPSTRIB
              FROM IMPITMAF I, IMPOSTO II
             WHERE (I.CDFILDEST   = P_FILDEST)
               AND (I.CDFILLANC   = P_FILLANC)
               AND (I.NRAUTOFORN  = P_LANCTOAF)
               AND (I.CDMARCA     = C_ITEMAUTF.CDMARCA)
               AND (I.DTPROGENTAF = C_ITEMAUTF.DTPROGENTAF)
               AND (I.CDPRODUTO   = C_ITEMAUTF.CDPRODUTO)
               AND (INSTR(V_CDIMPOSTRIB,II.CDIMPOSTO||'_') > 0)
               AND (I.CDIMPOSTO = II.CDIMPOSTO);

            IF (NVL(V_IDVRIMPSTRIB,'N') = 'N') THEN
               V_VRIMPITMAF := 0;
            ELSE
               BEGIN
                  SELECT NVL(MAX(VRIMPITMAF),0)
                  INTO   V_VRIMPITMAF
                  FROM IMPITMAF I
                 WHERE (I.CDFILDEST   = P_FILDEST)
                   AND (I.CDFILLANC   = P_FILLANC)
                   AND (I.NRAUTOFORN  = P_LANCTOAF)
                   AND (I.CDMARCA     = C_ITEMAUTF.CDMARCA)
                   AND (I.DTPROGENTAF = C_ITEMAUTF.DTPROGENTAF)
                   AND (I.CDPRODUTO   = C_ITEMAUTF.CDPRODUTO)
                   AND (INSTR(V_CDIMPOSTRIB,I.CDIMPOSTO||'_') > 0);
               Exception
                  When No_Data_Found Then
                     V_VRIMPITMAF := 0;
               End;
            END IF;
            V_IMPOSTOIT := V_IMPOSTOIT - V_VRIMPITMAF;
         END IF;

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

         V_ADIMPAF := 0;

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

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

         END IF;

         BEGIN
            SELECT MAX(VRIMPITMAF) INTO V_VRIMPITAFAUX
            FROM IMPITMAF
            WHERE (CDFILDEST   = P_FILDEST)  AND
                  (CDFILLANC   = P_FILLANC)  AND
                  (NRAUTOFORN  = P_LANCTOAF) AND
                  (CDMARCA     = C_ITEMAUTF.CDMARCA)     AND
                  (DTPROGENTAF = C_ITEMAUTF.DTPROGENTAF) AND
                  (CDPRODUTO   = C_ITEMAUTF.CDPRODUTO)   AND
                  (CDIMPOSTO   = C_INCIDENCIA.CDIMPOTEMP);
            IF (C_INCIDENCIA.IDTRATIMPO = 'D') THEN
                V_VRIMPITAFAUX := -V_VRIMPITAFAUX;
            END IF;
            IF (C_INCIDENCIA.IDTRATIMPO <> 'V') THEN
               UPDATE IMPITMAF
               SET VRADIMPAF = ROUND(V_VRIMPITAFAUX, 2)
               WHERE (CDFILDEST   = P_FILDEST)  AND
                     (CDFILLANC   = P_FILLANC)  AND
                     (NRAUTOFORN  = P_LANCTOAF) AND
                     (CDMARCA     = C_ITEMAUTF.CDMARCA)      AND
                     (DTPROGENTAF = C_ITEMAUTF.DTPROGENTAF)  AND
                     (CDPRODUTO   = C_ITEMAUTF.CDPRODUTO)    AND
                     (CDIMPOSTO   = C_INCIDENCIA.CDIMPOTEMP);
               IF SQL%NOTFOUND THEN
                  INSERT INTO IMPITMAF
                     (CDFILDEST, CDFILLANC, NRAUTOFORN, CDPRODUTO, CDMARCA, DTPROGENTAF,
                      CDIMPOSTO, VRIMPITMAF, VRPERREDBCAF, VRBASECALCAF,
                      VRALIQITMAF, VRPARCDEDAF, CDFISCITMAF, VRADIMPAF)
                  VALUES
                     (P_FILDEST, P_FILLANC, P_LANCTOAF, C_ITEMAUTF.CDPRODUTO, C_ITEMAUTF.CDMARCA, C_ITEMAUTF.DTPROGENTAF,
                      C_INCIDENCIA.CDIMPOTEMP, ROUND(V_IMPOSTOIT, 2), V_PREDBCTMP, V_BASECALCIT, V_ALIQUTMP,
                      V_PARDEDTMP, C_INCIDENCIA.CDFISCTEMP, ROUND(V_ADIMPAF, 2));
               END IF;
            ELSE
               UPDATE IMPITMAF
               SET VRADIMPAF = ROUND(V_ADIMPAF, 2)
               WHERE (CDFILDEST   = P_FILDEST)  AND
                     (CDFILLANC   = P_FILLANC)  AND
                     (NRAUTOFORN  = P_LANCTOAF) AND
                     (CDMARCA     = C_ITEMAUTF.CDMARCA)      AND
                     (DTPROGENTAF = C_ITEMAUTF.DTPROGENTAF)  AND
                     (CDPRODUTO   = C_ITEMAUTF.CDPRODUTO)    AND
                     (CDIMPOSTO   = C_INCIDENCIA.CDIMPOTEMP);
               IF SQL%NOTFOUND THEN
                  INSERT INTO IMPITMAF
                     (CDFILDEST, CDFILLANC, NRAUTOFORN, CDPRODUTO, CDMARCA, DTPROGENTAF,
                      CDIMPOSTO, VRIMPITMAF, VRPERREDBCAF, VRBASECALCAF,
                      VRALIQITMAF, VRPARCDEDAF, CDFISCITMAF, VRADIMPAF)
                  VALUES
                     (P_FILDEST, P_FILLANC, P_LANCTOAF, C_ITEMAUTF.CDPRODUTO, C_ITEMAUTF.CDMARCA, C_ITEMAUTF.DTPROGENTAF,
                      C_INCIDENCIA.CDIMPOTEMP, ROUND(V_IMPOSTOIT, 2), V_PREDBCTMP, V_BASECALCIT, V_ALIQUTMP,
                      V_PARDEDTMP, C_INCIDENCIA.CDFISCTEMP, ROUND(V_ADIMPAF, 2));
               END IF;
            END IF;
         END;

         DELETE ALIQTEMP
         WHERE NRALIQTEMP = V_ALIQTEMP;
      END LOOP;

      DELETE INCITEMP
      WHERE NRINCITEMP = V_INCITEMP;

   END LOOP;

   FOR C_DATAITEM IN (SELECT DISTINCT DTPROGENTAF
                      FROM ITEMAUTF
                      WHERE (CDFILLANC   = P_FILLANC)  AND
                            (CDFILDEST   = P_FILDEST)  AND
                            (NRAUTOFORN  = P_LANCTOAF)) LOOP
      FOR C_ENCARGO IN (SELECT E.CDENCARGO, VRENCARGOAF, IDENCARGO
                        FROM ENCARGOAF E, ENCARGO EC
                        WHERE (CDFILLANC   = P_FILLANC)    AND
                              (CDFILDEST   = P_FILDEST)    AND
                              (NRAUTOFORN  = P_LANCTOAF)   AND
                              (VRENCARGOAF > 0)            AND
                              (E.CDENCARGO = EC.CDENCARGO) AND
                              (DTENTRENCAF = C_DATAITEM.DTPROGENTAF)) LOOP
         V_TOTAPRODENCA := 0;
         FOR C_VRLIQRATEIO IN (SELECT DISTINCT I.CDPRODUTO, I.CDMARCA,
                                      (VRPREUNIAF * QTPROGENTAF) AS VRLIQUIDO
                               FROM ITEMAUTF I, IMPITMAF II
                               WHERE (I.CDFILLANC   = P_FILLANC)              AND
                                     (I.CDFILDEST   = P_FILDEST)              AND
                                     (I.NRAUTOFORN  = P_LANCTOAF)             AND
                                     (II.CDFILLANC  = P_FILLANC)              AND
                                     (II.CDFILDEST  = P_FILDEST)              AND
                                     (II.NRAUTOFORN = P_LANCTOAF)             AND
                                     (I.CDPRODUTO   = II.CDPRODUTO)           AND
                                     (I.CDMARCA     = II.CDMARCA)             AND
                                     (I.DTPROGENTAF = C_DATAITEM.DTPROGENTAF) AND
                                     (I.DTPROGENTAF = II.DTPROGENTAF)         AND
                                     (II.IDIMPDIGAF  = 'N')                   AND
                                     (I.VRPERDESAF IS NULL)                   AND
                                     (II.CDIMPOSTO IN (SELECT MAX(BC.CDIMPOSTO)
                                                       FROM BASECALC BC
                                                       WHERE (BC.CDENCARGO = C_ENCARGO.CDENCARGO)))
                               UNION
                               SELECT DISTINCT I.CDPRODUTO, I.CDMARCA,
                                      ((VRPREUNIAF * QTPROGENTAF)*((100 - VRPERDESAF)/100)) AS VRLIQUIDO
                               FROM ITEMAUTF I, IMPITMAF II
                               WHERE (I.CDFILLANC   = P_FILLANC)              AND
                                     (I.CDFILDEST   = P_FILDEST)              AND
                                     (I.NRAUTOFORN  = P_LANCTOAF)             AND
                                     (II.CDFILLANC  = P_FILLANC)              AND
                                     (II.CDFILDEST  = P_FILDEST)              AND
                                     (II.NRAUTOFORN = P_LANCTOAF)             AND
                                     (I.CDPRODUTO   = II.CDPRODUTO)           AND
                                     (I.CDMARCA     = II.CDMARCA)             AND
                                     (I.DTPROGENTAF = C_DATAITEM.DTPROGENTAF) AND
                                     (I.DTPROGENTAF = II.DTPROGENTAF)         AND
                                     (II.IDIMPDIGAF  = 'N')                   AND
                                     (I.VRPERDESAF IS NOT NULL)               AND
                                     (II.CDIMPOSTO IN (SELECT MAX(BC.CDIMPOSTO)
                                                       FROM BASECALC BC
                                                       WHERE (BC.CDENCARGO = C_ENCARGO.CDENCARGO)))) LOOP
            IF NOT (C_VRLIQRATEIO.VRLIQUIDO IS NULL) THEN
               V_TOTAPRODENCA := V_TOTAPRODENCA + C_VRLIQRATEIO.VRLIQUIDO;
            END IF;
         END LOOP;

         IF V_TOTAPRODENCA > 0 THEN
            FOR C_IMPOSTOAF IN (SELECT I.CDPRODUTO, I.CDMARCA, VRPERREDBCAF, VRALIQITMAF,
                                       VRPARCDEDAF, VRIMPITMAF, II.CDIMPOSTO, IDTRATIMPO,
                                       CDFISCITMAF, IDIMPORECUP,
                                       (VRPREUNIAF * QTPROGENTAF) AS VRLIQUIDO
                                FROM ITEMAUTF I, IMPITMAF II, BASECALC B, IMPOSTO IM
                                WHERE (I.CDFILLANC   = P_FILLANC)              AND
                                      (I.CDFILDEST   = P_FILDEST)              AND
                                      (I.NRAUTOFORN  = P_LANCTOAF)             AND
                                      (II.CDFILLANC  = P_FILLANC)              AND
                                      (II.CDFILDEST  = P_FILDEST)              AND
                                      (II.NRAUTOFORN = P_LANCTOAF)             AND
                                      (I.CDPRODUTO   = II.CDPRODUTO)           AND
                                      (I.CDMARCA     = II.CDMARCA)             AND
                                      (I.DTPROGENTAF = C_DATAITEM.DTPROGENTAF) AND
                                      (I.DTPROGENTAF = II.DTPROGENTAF)         AND
                                      (II.IDIMPDIGAF  = 'N')                   AND
                                      (II.CDIMPOSTO  = B.CDIMPOSTO)            AND
                                      (II.CDIMPOSTO  = IM.CDIMPOSTO)           AND
                                      (I.VRPERDESAF IS NULL)                   AND
                                      (B.CDENCARGO   = C_ENCARGO.CDENCARGO)
                                UNION
                                SELECT I.CDPRODUTO, I.CDMARCA, VRPERREDBCAF, VRALIQITMAF,
                                       VRPARCDEDAF, VRIMPITMAF, II.CDIMPOSTO, IDTRATIMPO,
                                       CDFISCITMAF, IDIMPORECUP,
                                       ((VRPREUNIAF * QTPROGENTAF)*((100 - VRPERDESAF)/100)) AS VRLIQUIDO
                                FROM ITEMAUTF I, IMPITMAF II, BASECALC B, IMPOSTO IM
                                WHERE (I.CDFILLANC   = P_FILLANC)              AND
                                      (I.CDFILDEST   = P_FILDEST)              AND
                                      (I.NRAUTOFORN  = P_LANCTOAF)             AND
                                      (II.CDFILLANC  = P_FILLANC)              AND
                                      (II.CDFILDEST  = P_FILDEST)              AND
                                      (II.NRAUTOFORN = P_LANCTOAF)             AND
                                      (I.CDPRODUTO   = II.CDPRODUTO)           AND
                                      (I.CDMARCA     = II.CDMARCA)             AND
                                      (I.DTPROGENTAF = C_DATAITEM.DTPROGENTAF) AND
                                      (I.DTPROGENTAF = II.DTPROGENTAF)         AND
                                      (II.IDIMPDIGAF  = 'N')                   AND
                                      (II.CDIMPOSTO  = B.CDIMPOSTO)            AND
                                      (II.CDIMPOSTO  = IM.CDIMPOSTO)           AND
                                      (I.VRPERDESAF IS NOT NULL)               AND
                                      (B.CDENCARGO   = C_ENCARGO.CDENCARGO)) LOOP

               IF (C_ENCARGO.IDENCARGO = 'A') THEN
                  V_PRODUENCARGO := +(C_ENCARGO.VRENCARGOAF/V_TOTAPRODENCA)*C_IMPOSTOAF.VRLIQUIDO;
               ELSIF (C_ENCARGO.IDENCARGO = 'D') THEN
                  V_PRODUENCARGO := -(C_ENCARGO.VRENCARGOAF/V_TOTAPRODENCA)*C_IMPOSTOAF.VRLIQUIDO;
               ELSE
                  V_PRODUENCARGO := 0;
               END IF;

               V_TAXABASECALC := (100 - C_IMPOSTOAF.VRPERREDBCAF)/100;
               V_TAXAALIQUOTA := (C_IMPOSTOAF.VRALIQITMAF/100);

               V_ADIMPAF := 0;

               IF (C_IMPOSTOAF.IDTRATIMPO = 'A') THEN
                  V_ADIMPAF := ((V_PRODUENCARGO * V_TAXABASECALC) * V_TAXAALIQUOTA);

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

               END IF;
               IF (C_IMPOSTOAF.IDTRATIMPO <> 'V') THEN
                  UPDATE IMPITMAF
                  SET VRIMPITMAF   = ROUND(VRIMPITMAF   + ((V_PRODUENCARGO * V_TAXABASECALC) * V_TAXAALIQUOTA), 2),
                      VRBASECALCAF = VRBASECALCAF + (V_PRODUENCARGO * V_TAXABASECALC),
                      VRADIMPAF    = ROUND(VRADIMPAF    + V_ADIMPAF, 2)
                  WHERE (CDFILLANC   = P_FILLANC)              AND
                        (CDFILDEST   = P_FILDEST)              AND
                        (NRAUTOFORN  = P_LANCTOAF)             AND
                        (CDPRODUTO   = C_IMPOSTOAF.CDPRODUTO)  AND
                        (CDMARCA     = C_IMPOSTOAF.CDMARCA)    AND
                        (CDIMPOSTO   = C_IMPOSTOAF.CDIMPOSTO)  AND
                        (DTPROGENTAF = C_DATAITEM.DTPROGENTAF);
               END IF;
            END LOOP;
         END IF;
      END LOOP;
   END LOOP;

   FOR C_PRODUTO IN (SELECT IP.CDPRODUTO, IP.CDMARCA, IP.DTPROGENTAF, II.CDIMPOSTO, VRPERREDBCAF,
                            VRALIQITMAF, VRPARCDEDAF, CDFISCITMAF, VRIMPITMAF, IDTRATIMPO,
                            IM.IDIMPORECUP, PR.CDARVPROD
                     FROM ITEMAUTF IP, IMPITMAF II, IMPOSTO IM, PRODUTO PR
                     WHERE (IP.CDFILLANC   = P_FILLANC)      AND
                           (IP.CDFILDEST   = P_FILDEST)      AND
                           (IP.NRAUTOFORN  = P_LANCTOAF)     AND
                           (II.CDFILLANC   = P_FILLANC)      AND
                           (II.CDFILDEST   = P_FILDEST)      AND
                           (II.NRAUTOFORN  = P_LANCTOAF)     AND
                           (II.CDPRODUTO   = IP.CDPRODUTO)   AND
                           (II.CDMARCA     = IP.CDMARCA)     AND
                           (II.DTPROGENTAF = IP.DTPROGENTAF) AND
                           (PR.CDPRODUTO   = IP.CDPRODUTO)   AND
                           (II.IDIMPDIGAF  = 'N')            AND
                           (IM.CDIMPOSTO   = II.CDIMPOSTO))
   LOOP
      CONSULTA_CST('E', P_EMPRESA, C_PRODUTO.CDARVPROD, P_INSCESTA, V_TIPOOPER, P_FILDEST||P_FILLANC, P_LANCTOAF, V_CDGRUPOFISC, V_IDCSTINTEREST, V_IDCSTINTERNA);

      CONSULTA_CFOP('E', P_EMPRESA, C_PRODUTO.CDARVPROD, P_INSCESTA, V_TIPOOPER, V_PAISORIG,
                    V_ESTAORIG, V_PAISDEST, V_ESTADEST, P_FILDEST||P_FILLANC, P_LANCTOAF,
                    V_CDGRUPOFISC, C_PRODUTO.DTPROGENTAF);

      SELECT MAX(CDFISOPETMP) INTO V_FISOPEITEM
      FROM CFOPTEMP
      WHERE NRCFOPTEMP = V_CFOPTEMP;

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

      V_CDIMPOBCALTPOP := NULL;
      SELECT MAX(BC.CDIMPOBCALC), SUM(VRIMPITMAF) AS VRIMPOBCALC, MAX(IMPOSTO.IDTRATIMPO), MAX(IDIMPORECUP)
        INTO V_CDIMPOBCALTPOP, V_TOTAIMPBCALC, V_IDTRATIMPBC, V_IDIMPORECUPBC
        FROM ITEMAUTF IP, IMPITMAF II, BASECALCTPOPER BC, IMPOSTO
       WHERE (BC.IDENTRSAIDOP = 'E')
         AND (BC.CDTIPOOPER = V_TIPOOPER)
         AND (BC.CDIMPOSTO   = C_PRODUTO.CDIMPOSTO)
         AND (IP.CDFILLANC   = P_FILLANC)
         AND (IP.CDFILDEST   = P_FILDEST)
         AND (IP.NRAUTOFORN  = P_LANCTOAF)
         AND (II.CDFILLANC   = P_FILLANC)
         AND (II.CDFILDEST   = P_FILDEST)
         AND (II.NRAUTOFORN  = P_LANCTOAF)
         AND (IP.CDPRODUTO   = C_PRODUTO.CDPRODUTO)
         AND (IP.CDMARCA     = C_PRODUTO.CDMARCA)
         AND (IP.DTPROGENTAF = C_PRODUTO.DTPROGENTAF)
         AND (II.CDPRODUTO   = C_PRODUTO.CDPRODUTO)
         AND (II.CDMARCA     = C_PRODUTO.CDMARCA)
         AND (II.DTPROGENTAF = C_PRODUTO.DTPROGENTAF)
         AND (II.IDIMPDIGAF  = 'N')
         AND (II.CDIMPOSTO   = BC.CDIMPOBCALC)
         AND (IMPOSTO.CDIMPOSTO = BC.CDIMPOBCALC)
         AND ((SUBSTR(V_SITUTRIBPR,2,2) = BC.CDCSTIMPOTPOP)  OR (BC.CDCSTIMPOTPOP  IS NULL))
         AND ((SUBSTR(V_FISOPEITEM,2,10) = BC.CDCFOPIMPOTPOP) OR (BC.CDCFOPIMPOTPOP IS NULL))
         AND ((II.CDFISCITMAF = BC.CDFISCIMPOTPOP) OR (BC.CDFISCIMPOTPOP IS NULL))
         AND ((BC.CDEMPRESABCTP = P_EMPRESA) OR (BC.CDEMPRESABCTP IS NULL));

      IF V_CDIMPOBCALTPOP IS NULL THEN
         SELECT SUM(VRIMPITMAF) AS VRIMPOBCALC, MAX(IMPOSTO.IDTRATIMPO), MAX(IDIMPORECUP) INTO V_TOTAIMPBCALC, V_IDTRATIMPBC, V_IDIMPORECUPBC
         FROM ITEMAUTF IP, IMPITMAF II, BASECALC BC, IMPOSTO
         WHERE (BC.CDIMPOSTO   = C_PRODUTO.CDIMPOSTO)   AND
               (IP.CDFILLANC   = P_FILLANC)             AND
               (IP.CDFILDEST   = P_FILDEST)             AND
               (IP.NRAUTOFORN  = P_LANCTOAF)            AND
               (II.CDFILLANC   = P_FILLANC)             AND
               (II.CDFILDEST   = P_FILDEST)             AND
               (II.NRAUTOFORN  = P_LANCTOAF)            AND
               (IP.CDPRODUTO   = C_PRODUTO.CDPRODUTO)   AND
               (IP.CDMARCA     = C_PRODUTO.CDMARCA)     AND
               (IP.DTPROGENTAF = C_PRODUTO.DTPROGENTAF) AND
               (II.CDPRODUTO   = C_PRODUTO.CDPRODUTO)   AND
               (II.CDMARCA     = C_PRODUTO.CDMARCA)     AND
               (II.DTPROGENTAF = C_PRODUTO.DTPROGENTAF) AND
               (II.IDIMPDIGAF  = 'N')                   AND
               (II.CDIMPOSTO   = BC.CDIMPOBCALC)        AND
               (IMPOSTO.CDIMPOSTO = BC.CDIMPOBCALC)     AND
               ((SUBSTR(V_SITUTRIBPR,2,2) = BC.CDCSTIMPO)  OR (BC.CDCSTIMPO  IS NULL)) AND
               ((SUBSTR(V_FISOPEITEM,2,10) = BC.CDCFOPIMPO) OR (BC.CDCFOPIMPO IS NULL)) AND
               ((II.CDFISCITMAF = BC.CDFISCIMPO) OR (BC.CDFISCIMPO IS NULL)) AND
               (BC.IDTIPOBCALC  = 'I')  AND
               ((BC.CDEMPRESABC = P_EMPRESA) OR (BC.CDEMPRESABC IS NULL));
      END IF;

      DELETE FROM CFOPTEMP
      WHERE NRCFOPTEMP = V_CFOPTEMP;

      DELETE FROM CSTTEMP
      WHERE NRCSTTEMP = V_CSTTEMP;

      IF NOT (V_TOTAIMPBCALC IS NULL) THEN
         V_TAXABASECALC := (100 - C_PRODUTO.VRPERREDBCAF)/100;
         V_TAXAALIQUOTA := (C_PRODUTO.VRALIQITMAF/100);

         V_ADIMPAF := 0;

         IF (V_IDTRATIMPBC = 'A') THEN
            V_ADIMPAF := ((V_TOTAIMPBCALC * V_TAXABASECALC) * V_TAXAALIQUOTA);

         ELSIF (V_IDTRATIMPBC = 'D') THEN
            V_ADIMPAF := - ((V_TOTAIMPBCALC * V_TAXABASECALC) * V_TAXAALIQUOTA);

         ELSIF (V_IDTRATIMPBC  = 'R') THEN
            IF (V_IDIMPORECUPBC = 'S') THEN
               IF (/*V_CARACFIS = 'N' OR (V_CARACFIS  = 'C' AND */C_PRODUTO.CDFISCITMAF <> '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') THEN
            V_ADIMPAF := 0;
         END IF;

         UPDATE IMPITMAF
         SET VRBASECALCAF = VRBASECALCAF + (V_TOTAIMPBCALC * V_TAXABASECALC),
             VRIMPITMAF   = ROUND(VRIMPITMAF   + ((V_TOTAIMPBCALC * V_TAXABASECALC) * V_TAXAALIQUOTA), 2),
             VRADIMPAF    = ROUND(VRADIMPAF    + V_ADIMPAF, 2)
         WHERE (CDFILLANC   = P_FILLANC)             AND
               (CDFILDEST   = P_FILDEST)             AND
               (NRAUTOFORN  = P_LANCTOAF)            AND
               (CDPRODUTO   = C_PRODUTO.CDPRODUTO)   AND
               (CDMARCA     = C_PRODUTO.CDMARCA)     AND
               (DTPROGENTAF = C_PRODUTO.DTPROGENTAF) AND
               (CDIMPOSTO   = C_PRODUTO.CDIMPOSTO);

      END IF;
   END LOOP;
END;
/