create or replace PROCEDURE CALCULA_BC_PMC(
   P_CDFILIAL        IN VARCHAR2,
   P_NRLANCTONF      IN VARCHAR2,
   P_NRSEQITPROD     IN VARCHAR2,
   P_CDIMPOSTO       IN VARCHAR2,
   P_IDCONSMVAPMC    IN VARCHAR2,
   P_IDCONSCOMPBCPMC IN VARCHAR2,
   P_CDPAISDESTINO      VARCHAR2,
   P_SGESTADODESTINO    VARCHAR2,
   P_CDPRODUTO          VARCHAR2,
   P_QTITEMPROD      IN FLOAT,
   P_VRUNITITEM      IN FLOAT,
   P_VRDESCITEM      IN FLOAT,
   P_VRMAJORACAOBC   IN FLOAT,
   P_VRREDUCAOBC     IN FLOAT,
   P_NRPEDIDO        IN VARCHAR2,
   P_NRITEMPED       IN VARCHAR2,
   P_IDENTRSAIDOP    IN VARCHAR2,
   P_CDTIPOOPER      IN VARCHAR2,
   P_IDORIGDESTNF    IN VARCHAR2,
   P_CDDESTINATARIO  IN VARCHAR2,
   P_IDMOBILE        IN VARCHAR2 DEFAULT 'N',
   P_VRBASECALCIT OUT FLOAT)
AS
   V_VRBASECALCIT FLOAT;
   V_IDENTRSAIDOP VARCHAR2(01);
   V_CDTIPOOPER VARCHAR2(02);
   V_IDORIGDESTNF VARCHAR2(01);
   V_CDFILIEMIT VARCHAR2(04);
   V_CDFILIRECE VARCHAR2(04);
   V_CDCLIENTE VARCHAR2(21);
   V_CDFORNECED VARCHAR2(21);
   V_NRINSCSUFRAMA VARCHAR2(09);
   V_VRMAXVENDA FLOAT;
   V_VRPEMAXVENDA FLOAT;
   V_IDUTILVRMAX VARCHAR2(01);
   V_VRDESCMAXVENDA FLOAT;
   V_VRTOTAITEM FLOAT;
   V_TAXAMAJORACAOBC FLOAT;
   V_TAXAREDUCAOBC FLOAT;
   V_VRUNITPMC FLOAT;
   V_VRUNITPMCCOMPARA FLOAT;
   V_VRSOMAIMPOSTOBC FLOAT;
   V_VRSOMAENCARGOBC FLOAT;
   V_VRLIQUIDOITEM FLOAT;
   V_VRIMPOSTOIPI FLOAT;
   V_IDVDVALOPER VARCHAR2(01);
   V_VALOROPERACAO FLOAT;
   V_TOTALENCARGONF FLOAT;
   V_TROCOU VARCHAR2(01);
   V_IPICOMPOEBASE FLOAT;
   V_VRENCARGOBCIPI FLOAT;
   V_CDIMPOSTOIPI   VARCHAR2(10);
   V_VRPMPF FLOAT;
   V_VRBASEPMPF FLOAT;
   V_VRPERVALOPER FLOAT;
   BEGIN
   V_TROCOU := 'N';
   IF P_NRPEDIDO IS NULL THEN
      BEGIN
         SELECT IDENTRSAIDOP,
                CDTIPOOPER,
                IDORIGDESTNF,
                CDFILIEMIT,
                CDFILIRECE,
                CDCLIENTE,
                CDFORNECED
           INTO V_IDENTRSAIDOP,
                V_CDTIPOOPER,
                V_IDORIGDESTNF,
                V_CDFILIEMIT,
                V_CDFILIRECE,
                V_CDCLIENTE,
                V_CDFORNECED
           FROM NOTAFISCAL
          WHERE CDFILIAL = P_CDFILIAL
            AND NRLANCTONF = P_NRLANCTONF;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN BEGIN
            V_IDENTRSAIDOP := NULL;
            V_CDTIPOOPER := NULL;
            V_IDORIGDESTNF := NULL;
            V_CDFILIEMIT := NULL;
            V_CDFILIRECE := NULL;
            V_CDCLIENTE := NULL;
            V_CDFORNECED := NULL;
         END;
      END;
   ELSE
      V_IDENTRSAIDOP := P_IDENTRSAIDOP;
      V_IDORIGDESTNF := P_IDORIGDESTNF;
      IF V_IDORIGDESTNF = 'C' THEN
         V_CDCLIENTE := P_CDDESTINATARIO;
      ELSIF V_IDORIGDESTNF = 'F' THEN
         V_CDFORNECED := P_CDDESTINATARIO;
      ELSIF V_IDORIGDESTNF = 'L' THEN
         IF V_IDENTRSAIDOP = 'S' THEN
            V_CDFILIEMIT := P_CDDESTINATARIO;
         ELSE
            V_CDFILIRECE := P_CDDESTINATARIO;
         END IF;
      END IF;
   END IF;
   -- REALIZANDO O PROCESSO DA COMPOSICAO DA BASE DE CALCULO
   V_VRSOMAIMPOSTOBC := 0;
   V_VRSOMAENCARGOBC := 0;
   V_VRLIQUIDOITEM := ROUND(ROUND((P_QTITEMPROD * P_VRUNITITEM),2) - P_VRDESCITEM, 2);
   IF P_NRPEDIDO IS NULL THEN
      -- SOMATORIO DOS OUTROS IMPOSTOS POR TIPO DE OPERACAO
      BEGIN
         SELECT NVL(SUM(CASE NVL(B.IDTRATIMPOBCTPOP, M.IDTRATIMPO) WHEN 'D' THEN I.VRIMPOSTOIT * (- 1) ELSE I.VRIMPOSTOIT END), 0)
           INTO V_VRSOMAIMPOSTOBC
           FROM ITEMPROIMPOS I JOIN BASECALCTPOPER B ON (I.CDIMPOSTO = B.CDIMPOBCALC)
                               JOIN IMPOSTO M ON (M.CDIMPOSTO = B.CDIMPOBCALC)
                               JOIN ITEMPRODUTO IT ON (I.CDFILIAL = IT.CDFILIAL)
                                                  AND (I.NRLANCTONF = IT.NRLANCTONF)
                                                  AND (I.NRSEQITPROD = IT.NRSEQITPROD)
          WHERE B.CDIMPOSTO = P_CDIMPOSTO
            AND B.IDENTRSAIDOP = V_IDENTRSAIDOP
            AND B.CDTIPOOPER = V_CDTIPOOPER
            AND I.CDFILIAL = P_CDFILIAL
            AND I.NRLANCTONF = P_NRLANCTONF
            AND I.NRSEQITPROD = P_NRSEQITPROD
            AND ((SUBSTR(IT.CDSITUTRIBPR, 2, 2) = B.CDCSTIMPOTPOP) OR (B.CDCSTIMPOTPOP IS NULL))
            AND ((SUBSTR(IT.CDFISOPEITEM, 2, 4) = B.CDCFOPIMPOTPOP) OR (B.CDCFOPIMPOTPOP IS NULL))
            AND ((I.CDFISCALIT = B.CDFISCIMPOTPOP) OR (B.CDFISCIMPOTPOP IS NULL));
      EXCEPTION
         WHEN NO_DATA_FOUND THEN BEGIN
            V_VRSOMAIMPOSTOBC := 0;
         END;
      END;
      IF V_VRSOMAIMPOSTOBC = 0 THEN
         -- SOMATORIO DOS OUTROS IMPOSTOS PADRAO
         BEGIN
            SELECT NVL(SUM(CASE NVL(B.IDTRATIMPOBC, M.IDTRATIMPO) WHEN 'D' THEN I.VRIMPOSTOIT * (- 1) ELSE I.VRIMPOSTOIT END), 0)
              INTO V_VRSOMAIMPOSTOBC
              FROM ITEMPROIMPOS I JOIN BASECALC B ON (I.CDIMPOSTO = B.CDIMPOBCALC)
                                  JOIN IMPOSTO M ON (M.CDIMPOSTO = B.CDIMPOBCALC)
                                  JOIN ITEMPRODUTO IT ON (I.CDFILIAL = IT.CDFILIAL)
                                                     AND (I.NRLANCTONF = IT.NRLANCTONF)
                                                     AND (I.NRSEQITPROD = IT.NRSEQITPROD)
             WHERE B.CDIMPOSTO = P_CDIMPOSTO
               AND I.CDFILIAL = P_CDFILIAL
               AND I.NRLANCTONF = P_NRLANCTONF
               AND I.NRSEQITPROD = P_NRSEQITPROD
               AND B.IDTIPOBCALC = 'I'
               AND ((SUBSTR(IT.CDSITUTRIBPR, 2, 2) = B.CDCSTIMPO) OR (B.CDCSTIMPO IS NULL))
               AND ((SUBSTR(IT.CDFISOPEITEM, 2, 4) = B.CDCFOPIMPO) OR (B.CDCFOPIMPO IS NULL))
               AND ((I.CDFISCALIT = B.CDFISCIMPO) OR (B.CDFISCIMPO IS NULL));
         EXCEPTION
            WHEN NO_DATA_FOUND THEN BEGIN
               V_VRSOMAIMPOSTOBC := 0;
            END;
         END;
      END IF;
      -- SOMATORIO DOS ENCARGOS
      BEGIN
         SELECT NVL(SUM(CASE NVL(B.IDTRATIMPOBCENC, E.IDENCARGO) WHEN 'D' THEN
                           (((I.VRITEMENCA / A.VRSOMATOTALITEM) * V_VRLIQUIDOITEM) * (- 1))
                        ELSE
                           ((I.VRITEMENCA / A.VRSOMATOTALITEM) * V_VRLIQUIDOITEM)
                        END), 0)
           INTO V_VRSOMAENCARGOBC
           FROM ITEMENCARGO I JOIN BASECALC B ON (I.CDENCARGO = B.CDENCARGO)
                              JOIN ENCARGO E ON (E.CDENCARGO = B.CDENCARGO),
                (SELECT SUM(ROUND(ROUND((IP.QTITEMPROD * IP.VRUNITITEM), 2) - IP.VRDESCITEM, 2)) AS VRSOMATOTALITEM
                   FROM ITEMPRODUTO IP
                  WHERE IP.CDFILIAL = P_CDFILIAL
                    AND IP.NRLANCTONF = P_NRLANCTONF) A
          WHERE B.CDIMPOSTO = P_CDIMPOSTO
            AND I.CDFILIAL = P_CDFILIAL
            AND I.NRLANCTONF = P_NRLANCTONF
            AND B.IDTIPOBCALC = 'E'
            AND E.IDENCARGO IN ('A', 'D');
      EXCEPTION
         WHEN NO_DATA_FOUND THEN BEGIN
            V_VRSOMAENCARGOBC := 0;
         END;
      END;
   ELSE
      -- SOMATORIO DOS OUTROS IMPOSTOS POR TIPO DE OPERACAO
      IF (NVL(P_IDMOBILE, 'N') = 'N') THEN
         BEGIN
            SELECT NVL(SUM(CASE NVL(B.IDTRATIMPOBCTPOP, M.IDTRATIMPO) WHEN 'D' THEN I.VRIMPOSTOITPED * (- 1) ELSE I.VRIMPOSTOITPED END), 0)
              INTO V_VRSOMAIMPOSTOBC
              FROM IMPITEMPEDIDO I JOIN BASECALCTPOPER B ON (I.CDIMPOSTO = B.CDIMPOBCALC)
                                   JOIN IMPOSTO M ON (M.CDIMPOSTO = B.CDIMPOBCALC)
                                   JOIN ITEMPEDIDO IT ON (I.CDFILIAL = IT.CDFILIAL)
                                                     AND (I.NRPEDIDO = IT.NRPEDIDO)
                                                     AND (I.NRITEMPED = IT.NRITEMPED)
             WHERE B.CDIMPOSTO = P_CDIMPOSTO
               AND B.IDENTRSAIDOP = V_IDENTRSAIDOP
               AND B.CDTIPOOPER = V_CDTIPOOPER
               AND I.CDFILIAL = P_CDFILIAL
               AND I.NRPEDIDO = P_NRPEDIDO
               AND I.NRITEMPED = P_NRITEMPED
               AND ((I.CDSITUTRIBITPED = B.CDCSTIMPOTPOP) OR (B.CDCSTIMPOTPOP IS NULL))
               AND ((SUBSTR(I.CDFISCOPERITPED, 2, 4) = B.CDCFOPIMPOTPOP) OR (B.CDCFOPIMPOTPOP IS NULL))
               AND ((I.CDFISCALITPED = B.CDFISCIMPOTPOP) OR (B.CDFISCIMPOTPOP IS NULL));
         EXCEPTION
            WHEN NO_DATA_FOUND THEN BEGIN
               V_VRSOMAIMPOSTOBC := 0;
            END;
         END;
         IF V_VRSOMAIMPOSTOBC = 0 THEN
            -- SOMATORIO DOS OUTROS IMPOSTOS PADRAO
            BEGIN
               SELECT NVL(SUM(CASE NVL(B.IDTRATIMPOBC, M.IDTRATIMPO) WHEN 'D' THEN I.VRIMPOSTOITPED * (- 1) ELSE I.VRIMPOSTOITPED END), 0)
                 INTO V_VRSOMAIMPOSTOBC
                 FROM IMPITEMPEDIDO I JOIN BASECALC B ON (I.CDIMPOSTO = B.CDIMPOBCALC)
                                      JOIN IMPOSTO M ON (M.CDIMPOSTO = B.CDIMPOBCALC)
                                      JOIN ITEMPEDIDO IT ON (I.CDFILIAL = IT.CDFILIAL)
                                                        AND (I.NRPEDIDO = IT.NRPEDIDO)
                                                        AND (I.NRITEMPED = IT.NRITEMPED)
                WHERE B.CDIMPOSTO = P_CDIMPOSTO
                  AND I.CDFILIAL = P_CDFILIAL
                  AND I.NRPEDIDO = P_NRPEDIDO
                  AND I.NRITEMPED = P_NRITEMPED
                  AND B.IDTIPOBCALC = 'I'
                  AND ((I.CDSITUTRIBITPED = B.CDCSTIMPO) OR (B.CDCSTIMPO IS NULL))
                  AND ((SUBSTR(I.CDFISCOPERITPED, 2, 4) = B.CDCFOPIMPO) OR (B.CDCFOPIMPO IS NULL))
                  AND ((I.CDFISCALITPED = B.CDFISCIMPO) OR (B.CDFISCIMPO IS NULL));
            EXCEPTION
               WHEN NO_DATA_FOUND THEN BEGIN
                  V_VRSOMAIMPOSTOBC := 0;
               END;
            END;
         END IF;
         -- SOMATORIO DOS ENCARGOS
         BEGIN
            SELECT NVL(SUM(CASE NVL(B.IDTRATIMPOBCENC, E.IDENCARGO) WHEN 'D' THEN
                              (((I.VRENCARGOPED / A.VRSOMATOTALITEM) * V_VRLIQUIDOITEM) * (- 1))
                           ELSE
                              ((I.VRENCARGOPED / A.VRSOMATOTALITEM) * V_VRLIQUIDOITEM)
                           END), 0)
              INTO V_VRSOMAENCARGOBC
              FROM ENCARGOPED I JOIN BASECALC B ON (I.CDENCARGO = B.CDENCARGO)
                                JOIN ENCARGO E ON (E.CDENCARGO = B.CDENCARGO),
                   (SELECT SUM(ROUND(ROUND((QTITEMPED * VRPREUNITPED), 2) - VRDESITPED, 2)) AS VRSOMATOTALITEM
                      FROM ITEMPEDIDO IP
                     WHERE IP.CDFILIAL = P_CDFILIAL
                       AND IP.NRPEDIDO = P_NRPEDIDO) A
             WHERE B.CDIMPOSTO = P_CDIMPOSTO
               AND I.CDFILIAL = P_CDFILIAL
               AND I.NRPEDIDO = P_NRPEDIDO
               AND B.IDTIPOBCALC = 'E'
               AND E.IDENCARGO IN ('A', 'D');
         EXCEPTION
            WHEN NO_DATA_FOUND THEN BEGIN
               V_VRSOMAENCARGOBC := 0;
            END;
         END;
      ELSE
         BEGIN
            SELECT NVL(SUM(CASE NVL(B.IDTRATIMPOBCTPOP, M.IDTRATIMPO) WHEN 'D' THEN I.VRIMPOSTOITPED * (- 1) ELSE I.VRIMPOSTOITPED END), 0)
              INTO V_VRSOMAIMPOSTOBC
              FROM IMPITEMPEDIDOTRAB I JOIN BASECALCTPOPER B ON (I.CDIMPOSTO = B.CDIMPOBCALC)
                                       JOIN IMPOSTO M ON (M.CDIMPOSTO = B.CDIMPOBCALC)
                                       JOIN ITEMPEDIDOTRAB IT ON (I.CDFILIAL = IT.CDFILIAL)
                                                     AND (I.NRPEDIDO = IT.NRPEDIDO)
                                                     AND (I.NRITEMPED = IT.NRITEMPED)
             WHERE B.CDIMPOSTO = P_CDIMPOSTO
               AND B.IDENTRSAIDOP = V_IDENTRSAIDOP
               AND B.CDTIPOOPER = V_CDTIPOOPER
               AND I.CDFILIAL = P_CDFILIAL
               AND I.NRPEDIDO = P_NRPEDIDO
               AND I.NRITEMPED = P_NRITEMPED
               AND ((I.CDSITUTRIBITPED = B.CDCSTIMPOTPOP) OR (B.CDCSTIMPOTPOP IS NULL))
               AND ((SUBSTR(I.CDFISCOPERITPED, 2, 4) = B.CDCFOPIMPOTPOP) OR (B.CDCFOPIMPOTPOP IS NULL))
               AND ((I.CDFISCALITPED = B.CDFISCIMPOTPOP) OR (B.CDFISCIMPOTPOP IS NULL));
         EXCEPTION
            WHEN NO_DATA_FOUND THEN BEGIN
               V_VRSOMAIMPOSTOBC := 0;
            END;
         END;
         IF V_VRSOMAIMPOSTOBC = 0 THEN
            -- SOMATORIO DOS OUTROS IMPOSTOS PADRAO
            BEGIN
               SELECT NVL(SUM(CASE NVL(B.IDTRATIMPOBC, M.IDTRATIMPO) WHEN 'D' THEN I.VRIMPOSTOITPED * (- 1) ELSE I.VRIMPOSTOITPED END), 0)
                 INTO V_VRSOMAIMPOSTOBC
                 FROM IMPITEMPEDIDOTRAB I JOIN BASECALC B ON (I.CDIMPOSTO = B.CDIMPOBCALC)
                                          JOIN IMPOSTO M ON (M.CDIMPOSTO = B.CDIMPOBCALC)
                                          JOIN ITEMPEDIDOTRAB IT ON (I.CDFILIAL = IT.CDFILIAL)
                                                            AND (I.NRPEDIDO = IT.NRPEDIDO)
                                                            AND (I.NRITEMPED = IT.NRITEMPED)
                WHERE B.CDIMPOSTO = P_CDIMPOSTO
                  AND I.CDFILIAL = P_CDFILIAL
                  AND I.NRPEDIDO = P_NRPEDIDO
                  AND I.NRITEMPED = P_NRITEMPED
                  AND B.IDTIPOBCALC = 'I'
                  AND ((I.CDSITUTRIBITPED = B.CDCSTIMPO) OR (B.CDCSTIMPO IS NULL))
                  AND ((SUBSTR(I.CDFISCOPERITPED, 2, 4) = B.CDCFOPIMPO) OR (B.CDCFOPIMPO IS NULL))
                  AND ((I.CDFISCALITPED = B.CDFISCIMPO) OR (B.CDFISCIMPO IS NULL));
            EXCEPTION
               WHEN NO_DATA_FOUND THEN BEGIN
                  V_VRSOMAIMPOSTOBC := 0;
               END;
            END;
         END IF;
         -- SOMATORIO DOS ENCARGOS
         BEGIN
            SELECT NVL(SUM(CASE NVL(B.IDTRATIMPOBCENC, E.IDENCARGO) WHEN 'D' THEN
                              (((I.VRENCARGOPED / A.VRSOMATOTALITEM) * V_VRLIQUIDOITEM) * (- 1))
                           ELSE
                              ((I.VRENCARGOPED / A.VRSOMATOTALITEM) * V_VRLIQUIDOITEM)
                           END), 0)
              INTO V_VRSOMAENCARGOBC
              FROM ENCARGOPEDTRAB I JOIN BASECALC B ON (I.CDENCARGO = B.CDENCARGO)
                                    JOIN ENCARGO E ON (E.CDENCARGO = B.CDENCARGO),
                   (SELECT SUM(ROUND(ROUND((QTITEMPED * VRPREUNITPED), 2) - VRDESITPED, 2)) AS VRSOMATOTALITEM
                      FROM ITEMPEDIDOTRAB IP
                     WHERE IP.CDFILIAL = P_CDFILIAL
                       AND IP.NRPEDIDO = P_NRPEDIDO) A
             WHERE B.CDIMPOSTO = P_CDIMPOSTO
               AND I.CDFILIAL = P_CDFILIAL
               AND I.NRPEDIDO = P_NRPEDIDO
               AND B.IDTIPOBCALC = 'E'
               AND E.IDENCARGO IN ('A', 'D');
         EXCEPTION
            WHEN NO_DATA_FOUND THEN BEGIN
               V_VRSOMAENCARGOBC := 0;
            END;
         END;
      END IF;
   END IF;
   IF V_IDORIGDESTNF = 'L' THEN
      -- DESTINATARIO FILIAL
      BEGIN
         SELECT NRINSCSUFRAMAFI
           INTO V_NRINSCSUFRAMA
           FROM FILIAL
          WHERE CDFILIAL = CASE V_IDENTRSAIDOP WHEN 'S' THEN V_CDFILIEMIT ELSE V_CDFILIRECE END;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN BEGIN
            V_NRINSCSUFRAMA := NULL;
         END;
      END;
   ELSIF V_IDORIGDESTNF = 'F' THEN
      -- DESTINATARIO FORNECEDOR
      BEGIN
         SELECT NRINSCSUFRAMAFO
           INTO V_NRINSCSUFRAMA
           FROM FORNECEDOR
          WHERE CDFORNECED = V_CDFORNECED;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN BEGIN
            V_NRINSCSUFRAMA := NULL;
         END;
      END;
   ELSE
      -- DESTINATARIO CLIENTE
      BEGIN
         SELECT NRINSCSUFRAMACL
           INTO V_NRINSCSUFRAMA
           FROM CLIENTE
          WHERE (CDCLIENTE = V_CDCLIENTE);
      EXCEPTION
         WHEN NO_DATA_FOUND THEN BEGIN
            V_NRINSCSUFRAMA := NULL;
         END;
      END;
   END IF;
	--197811
   BEGIN
      SELECT NVL(VRPERVALOPER, 0)
        INTO V_VRPERVALOPER
        FROM ESTADO
       WHERE CDPAIS = P_CDPAISDESTINO
         AND SGESTADO = P_SGESTADODESTINO;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN BEGIN
         V_VRPERVALOPER := 0;
      END;
   END;
   IF V_NRINSCSUFRAMA IS NULL THEN
      BEGIN
         SELECT NVL(A.VRMAXVENDA, 0),
                NVL(A.VRPEMAXVENDA, 0),
                NVL(A.IDUTILVRMAX, 'C'),
                NVL(A.VRDESCMAXVENDA, 0),
                NVL(A.IDVDVALOPER, 'N'),
                NVL(A.VRPMPF, 0)
           INTO V_VRMAXVENDA,
                V_VRPEMAXVENDA,
                V_IDUTILVRMAX,
                V_VRDESCMAXVENDA,
                V_IDVDVALOPER,
                V_VRPMPF
           FROM VRMAXVENPROD A
          WHERE A.CDPAIS = P_CDPAISDESTINO
            AND A.SGESTADO = P_SGESTADODESTINO
            AND A.CDPRODUTO = P_CDPRODUTO;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN BEGIN
            V_VRMAXVENDA := 0;
            V_VRPEMAXVENDA := 0;
            V_IDUTILVRMAX := 'C';
            V_VRDESCMAXVENDA := 0;
            V_IDVDVALOPER := 'N';
            V_VRPMPF := 0;
         END;
      END;
   ELSE
      BEGIN
         SELECT NVL(VRMAXVENDA, 0),
                NVL(VRPEMAXVENDA, 0),
                NVL(IDUTILVRMAX, 'C'),
                NVL(VRDESCMAXVENDA, 0),
                NVL(A.IDVDVALOPER, 'N'),
                NVL(A.VRPMPF, 0)
           INTO V_VRMAXVENDA,
                V_VRPEMAXVENDA,
                V_IDUTILVRMAX,
                V_VRDESCMAXVENDA,
                V_IDVDVALOPER,
                V_VRPMPF
           FROM VRMAXVENPROD A
          WHERE CDPAIS = P_CDPAISDESTINO
            AND SGESTADO = 'SF'
            AND A.CDPRODUTO = P_CDPRODUTO;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN BEGIN
            V_VRMAXVENDA := 0;
            V_VRPEMAXVENDA := 0;
            V_IDUTILVRMAX := 'C';
            V_VRDESCMAXVENDA := 0;
            V_IDVDVALOPER := 'N';
            V_VRPMPF := 0;
         END;
      END;
   END IF;
   IF P_NRPEDIDO IS NULL THEN
      BEGIN
         SELECT NVL(II.VRIMPOSTOIT, 0), IM.CDIMPOSTO
           INTO V_VRIMPOSTOIPI, V_CDIMPOSTOIPI
           FROM ITEMPROIMPOS II, IMPOSTO IM
          WHERE II.CDIMPOSTO = IM.CDIMPOSTO
            AND IM.CDIMPOSTONFE = '10'
            AND II.CDFILIAL = P_CDFILIAL
            AND II.NRLANCTONF = P_NRLANCTONF
            AND II.NRSEQITPROD = P_NRSEQITPROD
            AND II.VRIMPOSTOIT > 0;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_VRIMPOSTOIPI := 0;
      END;

      IF (V_VRIMPOSTOIPI > 0) THEN
        -- SOMATORIO DOS ENCARGOS QUE COMPOE A BC DO IPI
         BEGIN
            SELECT NVL(SUM(CASE NVL(B.IDTRATIMPOBCENC, E.IDENCARGO) WHEN 'D' THEN
                              (((I.VRITEMENCA / A.VRSOMATOTALITEM) * V_VRLIQUIDOITEM) * (- 1))
                           ELSE
                              ((I.VRITEMENCA / A.VRSOMATOTALITEM) * V_VRLIQUIDOITEM)
                           END), 0)
              INTO V_VRENCARGOBCIPI
              FROM ITEMENCARGO I JOIN BASECALC B ON (I.CDENCARGO = B.CDENCARGO)
                                 JOIN ENCARGO E ON (E.CDENCARGO = B.CDENCARGO),
                   (SELECT SUM(ROUND(ROUND((IP.QTITEMPROD * IP.VRUNITITEM), 2) - IP.VRDESCITEM, 2)) AS VRSOMATOTALITEM
                      FROM ITEMPRODUTO IP
                     WHERE IP.CDFILIAL = P_CDFILIAL
                       AND IP.NRLANCTONF = P_NRLANCTONF) A
             WHERE B.CDIMPOSTO = V_CDIMPOSTOIPI
               AND I.CDFILIAL = P_CDFILIAL
               AND I.NRLANCTONF = P_NRLANCTONF
               AND B.IDTIPOBCALC = 'E'
               AND E.IDENCARGO IN ('A', 'D');
         EXCEPTION
            WHEN NO_DATA_FOUND THEN BEGIN
               V_VRSOMAENCARGOBC := 0;
            END;
         END;
      END IF;

      IF V_VRENCARGOBCIPI > 0 THEN
         BEGIN
            SELECT NVL(II.VRIMPOSTOIT, 0) + ((V_VRENCARGOBCIPI * (II.VRALIQITEM/100)))
              INTO V_VRIMPOSTOIPI
              FROM ITEMPROIMPOS II, IMPOSTO IM
             WHERE II.CDIMPOSTO = IM.CDIMPOSTO
               AND IM.CDIMPOSTONFE = '10'
               AND II.CDFILIAL = P_CDFILIAL
               AND II.NRLANCTONF = P_NRLANCTONF
               AND II.NRSEQITPROD = P_NRSEQITPROD
               AND II.VRIMPOSTOIT > 0;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_VRIMPOSTOIPI := 0;
         END;
      END IF;
   ELSE
      IF (NVL(P_IDMOBILE, 'N') = 'N') THEN
         BEGIN
            SELECT NVL(II.VRIMPOSTOITPED, 0), IM.CDIMPOSTO
              INTO V_VRIMPOSTOIPI, V_CDIMPOSTOIPI
              FROM IMPITEMPEDIDO II, IMPOSTO IM
             WHERE II.CDIMPOSTO = IM.CDIMPOSTO
               AND IM.CDIMPOSTONFE = '10'
               AND II.CDFILIAL = P_CDFILIAL
               AND II.NRPEDIDO = P_NRPEDIDO
               AND II.NRITEMPED = P_NRITEMPED
               AND II.VRIMPOSTOITPED > 0;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_VRIMPOSTOIPI := 0;
         END;
         IF (V_VRIMPOSTOIPI > 0) THEN
           -- SOMATORIO DOS ENCARGOS QUE COMPOE A BC DO IPI
            BEGIN
               SELECT NVL(SUM(CASE NVL(B.IDTRATIMPOBCENC, E.IDENCARGO) WHEN 'D' THEN
                                 (((I.VRENCARGOPED / A.VRSOMATOTALITEM) * V_VRLIQUIDOITEM) * (- 1))
                              ELSE
                                 ((I.VRENCARGOPED / A.VRSOMATOTALITEM) * V_VRLIQUIDOITEM)
                              END), 0)
                 INTO V_VRENCARGOBCIPI
                 FROM ENCARGOPED I JOIN BASECALC B ON (I.CDENCARGO = B.CDENCARGO)
                                   JOIN ENCARGO E ON (E.CDENCARGO = B.CDENCARGO),
                      (SELECT SUM(ROUND(ROUND((IP.QTITEMPED * IP.VRPREUNITPED), 2) - IP.VRDESITPED, 2)) AS VRSOMATOTALITEM
                         FROM ITEMPEDIDO IP
                        WHERE IP.CDFILIAL = P_CDFILIAL
                          AND IP.NRPEDIDO = P_NRPEDIDO) A
                WHERE B.CDIMPOSTO = V_CDIMPOSTOIPI
                  AND I.CDFILIAL = P_CDFILIAL
                  AND I.NRPEDIDO = P_NRPEDIDO
                  AND B.IDTIPOBCALC = 'E'
                  AND E.IDENCARGO IN ('A', 'D');
            EXCEPTION
               WHEN NO_DATA_FOUND THEN BEGIN
                  V_VRSOMAENCARGOBC := 0;
               END;
            END;
         END IF;
         IF V_VRENCARGOBCIPI > 0 THEN
            BEGIN
               SELECT NVL(II.VRIMPOSTOITPED, 0) + ((V_VRENCARGOBCIPI * (II.VRALIQITPED/100)))
                 INTO V_VRIMPOSTOIPI
                 FROM IMPITEMPEDIDO II, IMPOSTO IM
                WHERE II.CDIMPOSTO = IM.CDIMPOSTO
                  AND IM.CDIMPOSTONFE = '10'
                  AND II.CDFILIAL = P_CDFILIAL
                  AND II.NRPEDIDO = P_NRPEDIDO
                  AND II.NRITEMPED = P_NRITEMPED
                  AND II.VRIMPOSTOITPED > 0;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_VRIMPOSTOIPI := 0;
            END;
         END IF;
      ELSE
         BEGIN
            SELECT NVL(II.VRIMPOSTOITPED, 0), IM.CDIMPOSTO
              INTO V_VRIMPOSTOIPI, V_CDIMPOSTOIPI
              FROM IMPITEMPEDIDOTRAB II, IMPOSTO IM
             WHERE II.CDIMPOSTO = IM.CDIMPOSTO
               AND IM.CDIMPOSTONFE = '10'
               AND II.CDFILIAL = P_CDFILIAL
               AND II.NRPEDIDO = P_NRPEDIDO
               AND II.NRITEMPED = P_NRITEMPED
               AND II.VRIMPOSTOITPED > 0;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_VRIMPOSTOIPI := 0;
         END;
         IF (V_VRIMPOSTOIPI > 0) THEN
           -- SOMATORIO DOS ENCARGOS QUE COMPOE A BC DO IPI
            BEGIN
               SELECT NVL(SUM(CASE NVL(B.IDTRATIMPOBCENC, E.IDENCARGO) WHEN 'D' THEN
                                 (((I.VRENCARGOPED / A.VRSOMATOTALITEM) * V_VRLIQUIDOITEM) * (- 1))
                              ELSE
                                 ((I.VRENCARGOPED / A.VRSOMATOTALITEM) * V_VRLIQUIDOITEM)
                              END), 0)
                 INTO V_VRENCARGOBCIPI
                 FROM ENCARGOPEDTRAB I JOIN BASECALC B ON (I.CDENCARGO = B.CDENCARGO)
                                   JOIN ENCARGO E ON (E.CDENCARGO = B.CDENCARGO),
                      (SELECT SUM(ROUND(ROUND((IP.QTITEMPED * IP.VRPREUNITPED), 2) - IP.VRDESITPED, 2)) AS VRSOMATOTALITEM
                         FROM ITEMPEDIDOTRAB IP
                        WHERE IP.CDFILIAL = P_CDFILIAL
                          AND IP.NRPEDIDO = P_NRPEDIDO) A
                WHERE B.CDIMPOSTO = V_CDIMPOSTOIPI
                  AND I.CDFILIAL = P_CDFILIAL
                  AND I.NRPEDIDO = P_NRPEDIDO
                  AND B.IDTIPOBCALC = 'E'
                  AND E.IDENCARGO IN ('A', 'D');
            EXCEPTION
               WHEN NO_DATA_FOUND THEN BEGIN
                  V_VRSOMAENCARGOBC := 0;
               END;
            END;
         END IF;
         IF V_VRENCARGOBCIPI > 0 THEN
            BEGIN
               SELECT NVL(II.VRIMPOSTOITPED, 0) + ((V_VRENCARGOBCIPI * (II.VRALIQITPED/100)))
                 INTO V_VRIMPOSTOIPI
                 FROM IMPITEMPEDIDOTRAB II, IMPOSTO IM
                WHERE II.CDIMPOSTO = IM.CDIMPOSTO
                  AND IM.CDIMPOSTONFE = '10'
                  AND II.CDFILIAL = P_CDFILIAL
                  AND II.NRPEDIDO = P_NRPEDIDO
                  AND II.NRITEMPED = P_NRITEMPED
                  AND II.VRIMPOSTOITPED > 0;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_VRIMPOSTOIPI := 0;
            END;
         END IF;
      END IF;
   END IF;
   IF P_NRPEDIDO IS NULL THEN
      BEGIN
         SELECT COUNT(*)
           INTO V_IPICOMPOEBASE
           FROM ITEMPROIMPOS I JOIN BASECALCTPOPER B ON (I.CDIMPOSTO = B.CDIMPOBCALC)
                               JOIN IMPOSTO M ON (M.CDIMPOSTO = B.CDIMPOBCALC)
                               JOIN ITEMPRODUTO IT ON (I.CDFILIAL = IT.CDFILIAL)
                                                  AND (I.NRLANCTONF = IT.NRLANCTONF)
                                                  AND (I.NRSEQITPROD = IT.NRSEQITPROD)
          WHERE B.CDIMPOSTO = P_CDIMPOSTO
            AND B.IDENTRSAIDOP = V_IDENTRSAIDOP
            AND B.CDTIPOOPER = V_CDTIPOOPER
            AND I.CDFILIAL = P_CDFILIAL
            AND I.NRLANCTONF = P_NRLANCTONF
            AND I.NRSEQITPROD = P_NRSEQITPROD
            AND ((SUBSTR(IT.CDSITUTRIBPR, 2, 2) = B.CDCSTIMPOTPOP) OR (B.CDCSTIMPOTPOP IS NULL))
            AND ((SUBSTR(IT.CDFISOPEITEM, 2, 4) = B.CDCFOPIMPOTPOP) OR (B.CDCFOPIMPOTPOP IS NULL))
            AND ((I.CDFISCALIT = B.CDFISCIMPOTPOP) OR (B.CDFISCIMPOTPOP IS NULL))
            AND M.CDIMPOSTONFE = '10';
      EXCEPTION
         WHEN NO_DATA_FOUND THEN BEGIN
            V_IPICOMPOEBASE := 0;
         END;
      END;
      IF V_IPICOMPOEBASE = 0 THEN
         BEGIN
            SELECT COUNT(*)
              INTO V_IPICOMPOEBASE
              FROM ITEMPROIMPOS I JOIN BASECALC B ON (I.CDIMPOSTO = B.CDIMPOBCALC)
                                  JOIN IMPOSTO M ON (M.CDIMPOSTO = B.CDIMPOBCALC)
                                  JOIN ITEMPRODUTO IT ON (I.CDFILIAL = IT.CDFILIAL)
                                                     AND (I.NRLANCTONF = IT.NRLANCTONF)
                                                     AND (I.NRSEQITPROD = IT.NRSEQITPROD)
             WHERE B.CDIMPOSTO = P_CDIMPOSTO
               AND I.CDFILIAL = P_CDFILIAL
               AND I.NRLANCTONF = P_NRLANCTONF
               AND I.NRSEQITPROD = P_NRSEQITPROD
               AND B.IDTIPOBCALC = 'I'
               AND ((SUBSTR(IT.CDSITUTRIBPR, 2, 2) = B.CDCSTIMPO) OR (B.CDCSTIMPO IS NULL))
               AND ((SUBSTR(IT.CDFISOPEITEM, 2, 4) = B.CDCFOPIMPO) OR (B.CDCFOPIMPO IS NULL))
               AND ((I.CDFISCALIT = B.CDFISCIMPO) OR (B.CDFISCIMPO IS NULL))
               AND M.CDIMPOSTONFE = '10';
         EXCEPTION
            WHEN NO_DATA_FOUND THEN BEGIN
               V_IPICOMPOEBASE := 0;
            END;
         END;
      END IF;
      BEGIN
         SELECT NVL(SUM(CASE E.IDENCARGO WHEN 'D' THEN
                           (((I.VRITEMENCA / A.VRSOMATOTALITEM) * V_VRLIQUIDOITEM) * (- 1))
                        ELSE
                           ((I.VRITEMENCA / A.VRSOMATOTALITEM) * V_VRLIQUIDOITEM)
                        END), 0)
           INTO V_TOTALENCARGONF
           FROM ITEMENCARGO I JOIN ENCARGO E ON (E.CDENCARGO = I.CDENCARGO),
                (SELECT SUM(ROUND(ROUND((IP.QTITEMPROD * IP.VRUNITITEM), 2) - IP.VRDESCITEM, 2)) AS VRSOMATOTALITEM
                   FROM ITEMPRODUTO IP
                  WHERE IP.CDFILIAL = P_CDFILIAL
                    AND IP.NRLANCTONF = P_NRLANCTONF) A
          WHERE I.CDFILIAL = P_CDFILIAL
            AND I.NRLANCTONF = P_NRLANCTONF
            AND E.IDENCARGO IN ('A', 'D');
      EXCEPTION
         WHEN NO_DATA_FOUND THEN BEGIN
            V_TOTALENCARGONF := 0;
         END;
      END;
   ELSE
      IF (NVL(P_IDMOBILE, 'N') = 'N') THEN
         BEGIN
            SELECT COUNT(*)
              INTO V_IPICOMPOEBASE
              FROM IMPITEMPEDIDO I JOIN BASECALCTPOPER B ON (I.CDIMPOSTO = B.CDIMPOBCALC)
                                   JOIN IMPOSTO M ON (M.CDIMPOSTO = B.CDIMPOBCALC)
                                   JOIN ITEMPEDIDO IT ON (I.CDFILIAL = IT.CDFILIAL)
                                                     AND (I.NRPEDIDO = IT.NRPEDIDO)
                                                     AND (I.NRITEMPED = IT.NRITEMPED)
             WHERE B.CDIMPOSTO = P_CDIMPOSTO
               AND B.IDENTRSAIDOP = V_IDENTRSAIDOP
               AND B.CDTIPOOPER = V_CDTIPOOPER
               AND I.CDFILIAL = P_CDFILIAL
               AND I.NRPEDIDO = P_NRPEDIDO
               AND I.NRITEMPED = P_NRITEMPED
               AND ((I.CDSITUTRIBITPED = B.CDCSTIMPOTPOP) OR (B.CDCSTIMPOTPOP IS NULL))
               AND ((SUBSTR(I.CDFISCOPERITPED, 2, 4) = B.CDCFOPIMPOTPOP) OR (B.CDCFOPIMPOTPOP IS NULL))
               AND ((I.CDFISCALITPED = B.CDFISCIMPOTPOP) OR (B.CDFISCIMPOTPOP IS NULL))
               AND M.CDIMPOSTONFE = '10';
         EXCEPTION
            WHEN NO_DATA_FOUND THEN BEGIN
               V_IPICOMPOEBASE := 0;
            END;
         END;
         IF V_IPICOMPOEBASE = 0 THEN
            BEGIN
               SELECT COUNT(*)
                 INTO V_IPICOMPOEBASE
                 FROM IMPITEMPEDIDO I JOIN BASECALC B ON (I.CDIMPOSTO = B.CDIMPOBCALC)
                                      JOIN IMPOSTO M ON (M.CDIMPOSTO = B.CDIMPOBCALC)
                                      JOIN ITEMPEDIDO IT ON (I.CDFILIAL = IT.CDFILIAL)
                                                        AND (I.NRPEDIDO = IT.NRPEDIDO)
                                                        AND (I.NRITEMPED = IT.NRITEMPED)
                WHERE B.CDIMPOSTO = P_CDIMPOSTO
                  AND I.CDFILIAL = P_CDFILIAL
                  AND I.NRPEDIDO = P_NRPEDIDO
                  AND I.NRITEMPED = P_NRITEMPED
                  AND B.IDTIPOBCALC = 'I'
                  AND ((I.CDSITUTRIBITPED = B.CDCSTIMPO) OR (B.CDCSTIMPO IS NULL))
                  AND ((SUBSTR(I.CDFISCOPERITPED, 2, 4) = B.CDCFOPIMPO) OR (B.CDCFOPIMPO IS NULL))
                  AND ((I.CDFISCALITPED = B.CDFISCIMPO) OR (B.CDFISCIMPO IS NULL))
                  AND M.CDIMPOSTONFE = '10';
            EXCEPTION
               WHEN NO_DATA_FOUND THEN BEGIN
                  V_IPICOMPOEBASE := 0;
               END;
            END;
         END IF;
         BEGIN
            SELECT NVL(SUM(CASE E.IDENCARGO WHEN 'D' THEN
                              (((I.VRENCARGOPED / A.VRSOMATOTALITEM) * V_VRLIQUIDOITEM) * (- 1))
                           ELSE
                              ((I.VRENCARGOPED / A.VRSOMATOTALITEM) * V_VRLIQUIDOITEM)
                           END), 0)
              INTO V_TOTALENCARGONF
              FROM ENCARGOPED I JOIN ENCARGO E ON (E.CDENCARGO = I.CDENCARGO),
                   (SELECT SUM(ROUND(ROUND((QTITEMPED * VRPREUNITPED), 2) - VRDESITPED, 2)) AS VRSOMATOTALITEM
                      FROM ITEMPEDIDO IP
                     WHERE IP.CDFILIAL = P_CDFILIAL
                       AND IP.NRPEDIDO = P_NRPEDIDO) A
             WHERE I.CDFILIAL = P_CDFILIAL
               AND I.NRPEDIDO = P_NRPEDIDO
               AND E.IDENCARGO IN ('A', 'D');
         EXCEPTION
            WHEN NO_DATA_FOUND THEN BEGIN
               V_TOTALENCARGONF := 0;
            END;
         END;
      ELSE
         BEGIN
            SELECT COUNT(*)
              INTO V_IPICOMPOEBASE
              FROM IMPITEMPEDIDOTRAB I JOIN BASECALCTPOPER B ON (I.CDIMPOSTO = B.CDIMPOBCALC)
                                       JOIN IMPOSTO M ON (M.CDIMPOSTO = B.CDIMPOBCALC)
                                       JOIN ITEMPEDIDOTRAB IT ON (I.CDFILIAL = IT.CDFILIAL)
                                                     AND (I.NRPEDIDO = IT.NRPEDIDO)
                                                     AND (I.NRITEMPED = IT.NRITEMPED)
             WHERE B.CDIMPOSTO = P_CDIMPOSTO
               AND B.IDENTRSAIDOP = V_IDENTRSAIDOP
               AND B.CDTIPOOPER = V_CDTIPOOPER
               AND I.CDFILIAL = P_CDFILIAL
               AND I.NRPEDIDO = P_NRPEDIDO
               AND I.NRITEMPED = P_NRITEMPED
               AND ((I.CDSITUTRIBITPED = B.CDCSTIMPOTPOP) OR (B.CDCSTIMPOTPOP IS NULL))
               AND ((SUBSTR(I.CDFISCOPERITPED, 2, 4) = B.CDCFOPIMPOTPOP) OR (B.CDCFOPIMPOTPOP IS NULL))
               AND ((I.CDFISCALITPED = B.CDFISCIMPOTPOP) OR (B.CDFISCIMPOTPOP IS NULL))
               AND M.CDIMPOSTONFE = '10';
         EXCEPTION
            WHEN NO_DATA_FOUND THEN BEGIN
               V_IPICOMPOEBASE := 0;
            END;
         END;
         IF V_IPICOMPOEBASE = 0 THEN
            BEGIN
               SELECT COUNT(*)
                 INTO V_IPICOMPOEBASE
                 FROM IMPITEMPEDIDOTRAB I JOIN BASECALC B ON (I.CDIMPOSTO = B.CDIMPOBCALC)
                                          JOIN IMPOSTO M ON (M.CDIMPOSTO = B.CDIMPOBCALC)
                                          JOIN ITEMPEDIDOTRAB IT ON (I.CDFILIAL = IT.CDFILIAL)
                                                            AND (I.NRPEDIDO = IT.NRPEDIDO)
                                                            AND (I.NRITEMPED = IT.NRITEMPED)
                WHERE B.CDIMPOSTO = P_CDIMPOSTO
                  AND I.CDFILIAL = P_CDFILIAL
                  AND I.NRPEDIDO = P_NRPEDIDO
                  AND I.NRITEMPED = P_NRITEMPED
                  AND B.IDTIPOBCALC = 'I'
                  AND ((I.CDSITUTRIBITPED = B.CDCSTIMPO) OR (B.CDCSTIMPO IS NULL))
                  AND ((SUBSTR(I.CDFISCOPERITPED, 2, 4) = B.CDCFOPIMPO) OR (B.CDCFOPIMPO IS NULL))
                  AND ((I.CDFISCALITPED = B.CDFISCIMPO) OR (B.CDFISCIMPO IS NULL))
                  AND M.CDIMPOSTONFE = '10';
            EXCEPTION
               WHEN NO_DATA_FOUND THEN BEGIN
                  V_IPICOMPOEBASE := 0;
               END;
            END;
         END IF;
         BEGIN
            SELECT NVL(SUM(CASE E.IDENCARGO WHEN 'D' THEN
                              (((I.VRENCARGOPED / A.VRSOMATOTALITEM) * V_VRLIQUIDOITEM) * (- 1))
                           ELSE
                              ((I.VRENCARGOPED / A.VRSOMATOTALITEM) * V_VRLIQUIDOITEM)
                           END), 0)
              INTO V_TOTALENCARGONF
              FROM ENCARGOPEDTRAB I JOIN ENCARGO E ON (E.CDENCARGO = I.CDENCARGO),
                   (SELECT SUM(ROUND(ROUND((QTITEMPED * VRPREUNITPED), 2) - VRDESITPED, 2)) AS VRSOMATOTALITEM
                      FROM ITEMPEDIDOTRAB IP
                     WHERE IP.CDFILIAL = P_CDFILIAL
                       AND IP.NRPEDIDO = P_NRPEDIDO) A
             WHERE I.CDFILIAL = P_CDFILIAL
               AND I.NRPEDIDO = P_NRPEDIDO
               AND E.IDENCARGO IN ('A', 'D');
         EXCEPTION
            WHEN NO_DATA_FOUND THEN BEGIN
               V_TOTALENCARGONF := 0;
            END;
         END;
      END IF;
   END IF;
   V_TAXAMAJORACAOBC := 1 + (P_VRMAJORACAOBC / 100);
   V_TAXAREDUCAOBC := P_VRREDUCAOBC / 100;
   V_VRBASECALCIT := 0;
   V_VRUNITPMC := V_VRMAXVENDA - (V_VRMAXVENDA * (V_VRDESCMAXVENDA / 100));
   V_VALOROPERACAO := V_VRLIQUIDOITEM + V_VRIMPOSTOIPI + V_TOTALENCARGONF;
   V_TROCOU := 'N';
   IF V_IDUTILVRMAX = 'C' THEN
      V_VRUNITPMCCOMPARA := V_VRUNITPMC * (V_VRPEMAXVENDA / 100);
   ELSE
      V_VRUNITPMCCOMPARA := V_VRUNITPMC;
   END IF;
   IF P_IDCONSMVAPMC = '1' THEN
      IF V_IDUTILVRMAX = 'C' THEN
         IF P_VRUNITITEM <= V_VRUNITPMCCOMPARA THEN
            V_VRTOTAITEM := P_QTITEMPROD * V_VRUNITPMC;
            IF (V_IDVDVALOPER = 'S') AND (V_VRPERVALOPER <> 0) AND (V_VALOROPERACAO >= (V_VRTOTAITEM * (V_VRPERVALOPER / 100))) THEN
               V_VRBASECALCIT := V_VALOROPERACAO;
               V_TROCOU := 'S';
            ELSE
               V_VRBASECALCIT := V_VRTOTAITEM;
            END IF;
            V_VRBASECALCIT := V_VRBASECALCIT * V_TAXAMAJORACAOBC;
            V_VRBASECALCIT := V_VRBASECALCIT - (V_VRBASECALCIT * V_TAXAREDUCAOBC);
            IF P_IDCONSCOMPBCPMC IN ('1', '3') THEN
               IF (V_IPICOMPOEBASE > 0) AND (V_TROCOU = 'S') THEN
                  V_VRSOMAIMPOSTOBC := (V_VRSOMAIMPOSTOBC - V_VRIMPOSTOIPI) * V_TAXAMAJORACAOBC;
                  V_VRSOMAIMPOSTOBC := V_VRSOMAIMPOSTOBC - (V_VRSOMAIMPOSTOBC * V_TAXAREDUCAOBC);
               ELSE
                  V_VRSOMAIMPOSTOBC := V_VRSOMAIMPOSTOBC * V_TAXAMAJORACAOBC;
                  V_VRSOMAIMPOSTOBC := V_VRSOMAIMPOSTOBC - (V_VRSOMAIMPOSTOBC * V_TAXAREDUCAOBC);
               END IF;
               IF (V_TROCOU = 'S') THEN
                  V_VRSOMAENCARGOBC := V_TOTALENCARGONF * V_TAXAMAJORACAOBC;
                  V_VRSOMAENCARGOBC := V_VRSOMAENCARGOBC - (V_VRSOMAENCARGOBC * V_TAXAREDUCAOBC);
               ELSE
                  V_VRSOMAENCARGOBC := V_VRSOMAENCARGOBC * V_TAXAMAJORACAOBC;
                  V_VRSOMAENCARGOBC := V_VRSOMAENCARGOBC - (V_VRSOMAENCARGOBC * V_TAXAREDUCAOBC);                  
               END IF;
               IF (V_TROCOU = 'N') THEN 
                  V_VRBASECALCIT := V_VRBASECALCIT + V_VRSOMAIMPOSTOBC + V_VRSOMAENCARGOBC;
               END IF;
               
            END IF;
         ELSE
            V_VRTOTAITEM := P_QTITEMPROD * P_VRUNITITEM;
            V_VRBASECALCIT := V_VRTOTAITEM;
            IF P_IDCONSCOMPBCPMC IN ('2', '3') THEN
               V_VRBASECALCIT := V_VRBASECALCIT + V_VRSOMAIMPOSTOBC + V_VRSOMAENCARGOBC;
            END IF;
         END IF;
      ELSE
         V_VRTOTAITEM := P_QTITEMPROD * V_VRUNITPMC;
         IF (V_IDVDVALOPER = 'S') AND (V_VRPERVALOPER <> 0) AND (V_VALOROPERACAO >= (V_VRTOTAITEM * (V_VRPERVALOPER / 100))) THEN
            V_VRBASECALCIT := V_VALOROPERACAO;
            V_TROCOU := 'S';
         ELSE
            V_VRBASECALCIT := V_VRTOTAITEM;
         END IF;
         IF P_VRUNITITEM <= V_VRUNITPMCCOMPARA THEN
            V_VRBASECALCIT := V_VRBASECALCIT * V_TAXAMAJORACAOBC;
            V_VRBASECALCIT := V_VRBASECALCIT - (V_VRBASECALCIT * V_TAXAREDUCAOBC);
            IF P_IDCONSCOMPBCPMC IN ('1', '3') THEN
               IF (V_IPICOMPOEBASE > 0) AND (V_TROCOU = 'S') THEN
                  V_VRSOMAIMPOSTOBC := (V_VRSOMAIMPOSTOBC - V_VRIMPOSTOIPI) * V_TAXAMAJORACAOBC;
                  V_VRSOMAIMPOSTOBC := V_VRSOMAIMPOSTOBC - (V_VRSOMAIMPOSTOBC * V_TAXAREDUCAOBC);
               ELSE
                  V_VRSOMAIMPOSTOBC := V_VRSOMAIMPOSTOBC * V_TAXAMAJORACAOBC;
                  V_VRSOMAIMPOSTOBC := V_VRSOMAIMPOSTOBC - (V_VRSOMAIMPOSTOBC * V_TAXAREDUCAOBC);
               END IF;
               IF (V_TROCOU = 'S') THEN
                  V_VRSOMAENCARGOBC := V_TOTALENCARGONF * V_TAXAMAJORACAOBC;
                  V_VRSOMAENCARGOBC := V_VRSOMAENCARGOBC - (V_VRSOMAENCARGOBC * V_TAXAREDUCAOBC);
               ELSE
                  V_VRSOMAENCARGOBC := V_VRSOMAENCARGOBC * V_TAXAMAJORACAOBC;
                  V_VRSOMAENCARGOBC := V_VRSOMAENCARGOBC - (V_VRSOMAENCARGOBC * V_TAXAREDUCAOBC);
               END IF;               
               IF (V_TROCOU = 'N') THEN 
                  V_VRBASECALCIT := V_VRBASECALCIT + V_VRSOMAIMPOSTOBC + V_VRSOMAENCARGOBC;
               END IF;
            END IF;
         ELSE
            IF P_IDCONSCOMPBCPMC IN ('2', '3') THEN
               V_VRBASECALCIT := V_VRBASECALCIT + V_VRSOMAIMPOSTOBC + V_VRSOMAENCARGOBC;
            END IF;
         END IF;
      END IF;
   ELSIF P_IDCONSMVAPMC = '2' THEN
      IF V_IDUTILVRMAX = 'C' THEN
         IF P_VRUNITITEM > V_VRUNITPMCCOMPARA THEN
            V_VRTOTAITEM := P_QTITEMPROD * P_VRUNITITEM;
            V_VRBASECALCIT := V_VRTOTAITEM * V_TAXAMAJORACAOBC;
            V_VRBASECALCIT := V_VRBASECALCIT - (V_VRBASECALCIT * V_TAXAREDUCAOBC);
            IF P_IDCONSCOMPBCPMC IN ('2', '3') THEN
               V_VRSOMAIMPOSTOBC := V_VRSOMAIMPOSTOBC * V_TAXAMAJORACAOBC;
               V_VRSOMAIMPOSTOBC := V_VRSOMAIMPOSTOBC - (V_VRSOMAIMPOSTOBC * V_TAXAREDUCAOBC);
               V_VRSOMAENCARGOBC := V_VRSOMAENCARGOBC * V_TAXAMAJORACAOBC;
               V_VRSOMAENCARGOBC := V_VRSOMAENCARGOBC - (V_VRSOMAENCARGOBC * V_TAXAREDUCAOBC);
               V_VRBASECALCIT := V_VRBASECALCIT + V_VRSOMAIMPOSTOBC + V_VRSOMAENCARGOBC;
            END IF;
         ELSE
            V_VRTOTAITEM := P_QTITEMPROD * V_VRUNITPMC;
            IF (V_IDVDVALOPER = 'S') AND (V_VRPERVALOPER <> 0) AND (V_VALOROPERACAO >= (V_VRTOTAITEM * (V_VRPERVALOPER / 100))) THEN
               V_VRBASECALCIT := V_VALOROPERACAO;
               V_VRBASECALCIT := V_VRBASECALCIT * V_TAXAMAJORACAOBC;
               V_VRBASECALCIT := V_VRBASECALCIT - (V_VRBASECALCIT * V_TAXAREDUCAOBC);
               V_TROCOU := 'S';  
            ELSE
               V_VRBASECALCIT := V_VRTOTAITEM;
            END IF;
            IF P_IDCONSCOMPBCPMC IN ('1', '3') THEN
               V_VRBASECALCIT := V_VRBASECALCIT + V_VRSOMAIMPOSTOBC + V_VRSOMAENCARGOBC;
            END IF;
         END IF;
      ELSE
         V_VRTOTAITEM := P_QTITEMPROD * V_VRUNITPMC;
         IF (V_IDVDVALOPER = 'S') AND (V_VRPERVALOPER <> 0) AND (V_VALOROPERACAO >= (V_VRTOTAITEM * (V_VRPERVALOPER / 100))) THEN
               V_VRBASECALCIT := V_VALOROPERACAO;
               V_TROCOU := 'S';  
            ELSE
               V_VRBASECALCIT := V_VRTOTAITEM;
            END IF;
         IF P_VRUNITITEM > V_VRUNITPMCCOMPARA THEN
            V_VRBASECALCIT := V_VRBASECALCIT * V_TAXAMAJORACAOBC;
            V_VRBASECALCIT := V_VRBASECALCIT - (V_VRBASECALCIT * V_TAXAREDUCAOBC);
            IF P_IDCONSCOMPBCPMC IN ('2', '3') THEN
               IF (V_IPICOMPOEBASE > 0) AND (V_TROCOU = 'S') THEN
                  V_VRSOMAIMPOSTOBC := (V_VRSOMAIMPOSTOBC - V_VRIMPOSTOIPI) * V_TAXAMAJORACAOBC;
                  V_VRSOMAIMPOSTOBC := V_VRSOMAIMPOSTOBC - (V_VRSOMAIMPOSTOBC * V_TAXAREDUCAOBC);
               ELSE
                  V_VRSOMAIMPOSTOBC := V_VRSOMAIMPOSTOBC * V_TAXAMAJORACAOBC;
                  V_VRSOMAIMPOSTOBC := V_VRSOMAIMPOSTOBC - (V_VRSOMAIMPOSTOBC * V_TAXAREDUCAOBC);
               END IF;
               IF (V_TROCOU = 'S') THEN
                  V_VRSOMAENCARGOBC := V_TOTALENCARGONF * V_TAXAMAJORACAOBC;
                  V_VRSOMAENCARGOBC := V_VRSOMAENCARGOBC - (V_VRSOMAENCARGOBC * V_TAXAREDUCAOBC);
               ELSE
                  V_VRSOMAENCARGOBC := V_VRSOMAENCARGOBC * V_TAXAMAJORACAOBC;
                  V_VRSOMAENCARGOBC := V_VRSOMAENCARGOBC - (V_VRSOMAENCARGOBC * V_TAXAREDUCAOBC);
               END IF;
               IF (V_TROCOU = 'N') THEN 
                  V_VRBASECALCIT := V_VRBASECALCIT + V_VRSOMAIMPOSTOBC + V_VRSOMAENCARGOBC;
               END IF;
            END IF;
         ELSE
            IF P_IDCONSCOMPBCPMC IN ('1', '3') THEN
               IF (V_IPICOMPOEBASE > 0) AND (V_TROCOU = 'S') THEN
                  V_VRSOMAIMPOSTOBC := (V_VRSOMAIMPOSTOBC - V_VRIMPOSTOIPI) * V_TAXAMAJORACAOBC;
                  V_VRSOMAIMPOSTOBC := V_VRSOMAIMPOSTOBC - (V_VRSOMAIMPOSTOBC * V_TAXAREDUCAOBC);
               ELSE
                  V_VRSOMAIMPOSTOBC := V_VRSOMAIMPOSTOBC * V_TAXAMAJORACAOBC;
                  V_VRSOMAIMPOSTOBC := V_VRSOMAIMPOSTOBC - (V_VRSOMAIMPOSTOBC * V_TAXAREDUCAOBC);
               END IF;
               IF (V_TROCOU = 'S') THEN
                  V_VRSOMAENCARGOBC := V_TOTALENCARGONF * V_TAXAMAJORACAOBC;
                  V_VRSOMAENCARGOBC := V_VRSOMAENCARGOBC - (V_VRSOMAENCARGOBC * V_TAXAREDUCAOBC);
               ELSE
                  V_VRSOMAENCARGOBC := V_VRSOMAENCARGOBC * V_TAXAMAJORACAOBC;
                  V_VRSOMAENCARGOBC := V_VRSOMAENCARGOBC - (V_VRSOMAENCARGOBC * V_TAXAREDUCAOBC);
               END IF;
               IF (V_TROCOU = 'N') THEN 
                  V_VRBASECALCIT := V_VRBASECALCIT + V_VRSOMAIMPOSTOBC + V_VRSOMAENCARGOBC;
               END IF;
            END IF;
         END IF;
      END IF;
   ELSIF P_IDCONSMVAPMC = '3' THEN
      IF V_IDUTILVRMAX = 'C' THEN
         IF P_VRUNITITEM <= V_VRUNITPMCCOMPARA THEN
            V_VRTOTAITEM := P_QTITEMPROD * V_VRUNITPMC;
         ELSE
            V_VRTOTAITEM := P_QTITEMPROD * P_VRUNITITEM;
         END IF;
      ELSE
         V_VRTOTAITEM := P_QTITEMPROD * V_VRUNITPMC;
      END IF;
     IF (V_IDVDVALOPER = 'S') AND (V_VRPERVALOPER <> 0) AND (V_VALOROPERACAO >= (V_VRTOTAITEM * (V_VRPERVALOPER / 100))) THEN
         V_VRBASECALCIT := V_VALOROPERACAO;
         V_TROCOU := 'S';
       ELSE
          V_VRBASECALCIT := V_VRTOTAITEM;
     END IF;
      V_VRBASECALCIT := V_VRBASECALCIT * V_TAXAMAJORACAOBC;
      V_VRBASECALCIT := V_VRBASECALCIT - (V_VRBASECALCIT * V_TAXAREDUCAOBC);
      IF P_VRUNITITEM <= V_VRUNITPMCCOMPARA THEN
         IF P_IDCONSCOMPBCPMC IN ('1', '3') THEN
            IF (V_IPICOMPOEBASE > 0) AND (V_TROCOU = 'S') THEN
                V_VRSOMAIMPOSTOBC := (V_VRSOMAIMPOSTOBC - V_VRIMPOSTOIPI) * V_TAXAMAJORACAOBC;
                V_VRSOMAIMPOSTOBC := V_VRSOMAIMPOSTOBC - (V_VRSOMAIMPOSTOBC * V_TAXAREDUCAOBC);
             ELSE
                V_VRSOMAIMPOSTOBC := V_VRSOMAIMPOSTOBC * V_TAXAMAJORACAOBC;
                V_VRSOMAIMPOSTOBC := V_VRSOMAIMPOSTOBC - (V_VRSOMAIMPOSTOBC * V_TAXAREDUCAOBC);
             END IF;
             IF (V_TROCOU = 'S') THEN
                V_VRSOMAENCARGOBC := V_TOTALENCARGONF * V_TAXAMAJORACAOBC;
                V_VRSOMAENCARGOBC := V_VRSOMAENCARGOBC - (V_VRSOMAENCARGOBC * V_TAXAREDUCAOBC);
             ELSE
                V_VRSOMAENCARGOBC := V_VRSOMAENCARGOBC * V_TAXAMAJORACAOBC;
                V_VRSOMAENCARGOBC := V_VRSOMAENCARGOBC - (V_VRSOMAENCARGOBC * V_TAXAREDUCAOBC);
             END IF;
             IF (V_TROCOU = 'N') THEN 
                V_VRBASECALCIT := V_VRBASECALCIT + V_VRSOMAIMPOSTOBC + V_VRSOMAENCARGOBC;
             END IF;
         END IF;
      ELSE
         IF P_IDCONSCOMPBCPMC IN ('2', '3') THEN
            IF (V_IPICOMPOEBASE > 0) AND (V_TROCOU = 'S') THEN
                V_VRSOMAIMPOSTOBC := (V_VRSOMAIMPOSTOBC - V_VRIMPOSTOIPI) * V_TAXAMAJORACAOBC;
                V_VRSOMAIMPOSTOBC := V_VRSOMAIMPOSTOBC - (V_VRSOMAIMPOSTOBC * V_TAXAREDUCAOBC);
             ELSE
                V_VRSOMAIMPOSTOBC := V_VRSOMAIMPOSTOBC * V_TAXAMAJORACAOBC;
                V_VRSOMAIMPOSTOBC := V_VRSOMAIMPOSTOBC - (V_VRSOMAIMPOSTOBC * V_TAXAREDUCAOBC);
             END IF;
             IF (V_TROCOU = 'S') THEN
                V_VRSOMAENCARGOBC := V_TOTALENCARGONF * V_TAXAMAJORACAOBC;
                V_VRSOMAENCARGOBC := V_VRSOMAENCARGOBC - (V_VRSOMAENCARGOBC * V_TAXAREDUCAOBC);
             ELSE
                V_VRSOMAENCARGOBC := V_VRSOMAENCARGOBC * V_TAXAMAJORACAOBC;
                V_VRSOMAENCARGOBC := V_VRSOMAENCARGOBC - (V_VRSOMAENCARGOBC * V_TAXAREDUCAOBC);
             END IF;
             IF (V_TROCOU = 'N') THEN 
                 V_VRBASECALCIT := V_VRBASECALCIT + V_VRSOMAIMPOSTOBC + V_VRSOMAENCARGOBC;
             END IF;
         END IF;
      END IF;
   ELSIF P_IDCONSMVAPMC = '4' THEN
      IF V_IDUTILVRMAX = 'C' THEN
         IF P_VRUNITITEM > V_VRUNITPMCCOMPARA THEN
            V_VRTOTAITEM := P_QTITEMPROD * P_VRUNITITEM;
            IF (V_IDVDVALOPER = 'S') AND (V_VRPERVALOPER <> 0) AND (V_VALOROPERACAO >= (V_VRTOTAITEM * (V_VRPERVALOPER / 100))) THEN
               V_VRBASECALCIT := V_VALOROPERACAO;
               V_TROCOU := 'S';
            ELSE
               V_VRBASECALCIT := V_VRTOTAITEM;
            END IF;
            IF P_IDCONSCOMPBCPMC IN ('2', '3') THEN
               V_VRBASECALCIT := V_VRBASECALCIT + V_VRSOMAIMPOSTOBC + V_VRSOMAENCARGOBC;
            END IF;
         ELSE
            V_VRTOTAITEM := P_QTITEMPROD * V_VRUNITPMC;
            IF (V_IDVDVALOPER = 'S') AND (V_VRPERVALOPER <> 0) AND (V_VALOROPERACAO >= (V_VRTOTAITEM * (V_VRPERVALOPER / 100))) THEN
               V_VRBASECALCIT := V_VALOROPERACAO;
			   
               V_TROCOU := 'S';
            ELSE
               V_VRBASECALCIT := V_VRTOTAITEM;
            END IF;
            IF P_IDCONSCOMPBCPMC IN ('1', '3') THEN
               IF (V_TROCOU = 'N') THEN 
                  V_VRBASECALCIT := V_VRBASECALCIT + V_VRSOMAIMPOSTOBC + V_VRSOMAENCARGOBC;
               END IF;
            END IF;
         END IF;
      ELSE
         V_VRTOTAITEM := P_QTITEMPROD * V_VRUNITPMC;
         IF (V_IDVDVALOPER = 'S') AND (V_VRPERVALOPER <> 0) AND (V_VALOROPERACAO >= (V_VRTOTAITEM * (V_VRPERVALOPER / 100))) THEN
               V_VRBASECALCIT := V_VALOROPERACAO;
               V_TROCOU := 'S';
            ELSE
               V_VRBASECALCIT := V_VRTOTAITEM;
            END IF;
         IF P_VRUNITITEM > V_VRUNITPMCCOMPARA THEN
            IF P_IDCONSCOMPBCPMC IN ('2', '3') THEN
               IF (V_TROCOU = 'N') THEN 
                  V_VRBASECALCIT := V_VRBASECALCIT + V_VRSOMAIMPOSTOBC + V_VRSOMAENCARGOBC;
               END IF;
            END IF;
         ELSE
            IF P_IDCONSCOMPBCPMC IN ('1', '3') THEN
               IF (V_TROCOU = 'N') THEN 
                  V_VRBASECALCIT := V_VRBASECALCIT + V_VRSOMAIMPOSTOBC + V_VRSOMAENCARGOBC;
               END IF;
            END IF;
         END IF;
      END IF;
   END IF;
   IF V_VRPMPF > 0 THEN
      V_VRBASEPMPF := (P_QTITEMPROD * V_VRPMPF) + V_VRSOMAIMPOSTOBC + V_VRSOMAENCARGOBC;
      IF V_VRBASECALCIT < V_VRBASEPMPF THEN
         P_VRBASECALCIT := V_VRBASECALCIT;
      ELSE
         P_VRBASECALCIT := V_VRBASEPMPF;
      END IF;
   ELSE
	  P_VRBASECALCIT := V_VRBASECALCIT;
   END IF;
   IF P_NRPEDIDO IS NULL THEN
	  IF V_VRPMPF > 0 THEN
         UPDATE ITEMPRODUTO
            SET IDUTILBCPMC = 'S',
                VRMAXVENDA = V_VRPMPF
          WHERE CDFILIAL = P_CDFILIAL
            AND NRLANCTONF = P_NRLANCTONF
            AND NRSEQITPROD = P_NRSEQITPROD;
      ELSE      
	      UPDATE ITEMPRODUTO
	         SET IDUTILBCPMC = 'S',
	             VRMAXVENDA = V_VRMAXVENDA
	       WHERE CDFILIAL = P_CDFILIAL
	         AND NRLANCTONF = P_NRLANCTONF
	         AND NRSEQITPROD = P_NRSEQITPROD;
     END IF;
   ELSE
      IF (NVL(P_IDMOBILE, 'N') = 'N') THEN
         UPDATE ITEMPEDIDO
            SET IDUTILBCPMC = 'S'
          WHERE CDFILIAL = P_CDFILIAL
            AND NRPEDIDO = P_NRPEDIDO
            AND NRITEMPED = P_NRITEMPED;
      ELSE
         UPDATE ITEMPEDIDOTRAB
            SET IDUTILBCPMC = 'S'
          WHERE CDFILIAL = P_CDFILIAL
            AND NRPEDIDO = P_NRPEDIDO
            AND NRITEMPED = P_NRITEMPED;
      END IF;
   END IF;
END;
/