CREATE OR REPLACE PROCEDURE GERA_COMISSAOVEND(
   P_CDFILIAL     IN ITEMPEDIDO.CDFILIAL%TYPE,
   P_NRPEDIDO     IN ITEMPEDIDO.NRPEDIDO%TYPE,
   P_CDVENDEDOR   IN VENDITEMPED.CDVENDEDOR%TYPE,
   P_CDOPERADOR   IN OPERADOR.CDOPERADOR%TYPE,
   P_NRITEMPED    IN ITEMPEDIDO.NRITEMPED%TYPE,
   P_IDSITITEMPED IN ITEMPEDIDO.IDSITITEMPED%TYPE DEFAULT NULL,
   P_CDPRODUTO    IN ITEMPEDIDO.CDPRODUTO%TYPE DEFAULT NULL,
   P_VRPREUNITPED IN ITEMPEDIDO.VRPREUNITPED%TYPE DEFAULT NULL,
   P_OLDCDVENDE   IN VENDITEMPED.CDVENDEDOR%TYPE DEFAULT NULL,
   P_COMISPERIODO IN PAREXPEDICAO.IDCOMISPERIODO%TYPE DEFAULT 'N'
) IS
   V_CTPEVEND        NUMBER;
   V_VRCOMISANT      NUMBER;
   V_VRPECOMIVEND    FLOAT;
   V_VRITRENTABIL    FLOAT;
   V_CTREGISTRO      NUMBER;
   V_CDPRECOBASE     PRECOBASE.CDPRECOBASE%TYPE;
   V_IDCTRLVENEXP    PAREXPEDICAO.IDCTRLVENEXP%TYPE;
   V_IDITPEDDIFER    ITEMPEDIDO.IDITPEDDIFER%TYPE;
   V_CDCLIENTE       CLIENTE.CDCLIENTE%TYPE;
   V_IDGERACOMISVEND VENDEDOR.IDGERACOMISVEND%TYPE;
   V_VRDESCCOMISS    ITEMPEDIDO.VRDESCCOMISS%TYPE;
   V_IDULTPRECPROMO  PRECOBASE.IDULTPRECPROMO%TYPE;
   V_VRPRECO         ITEMPEDIDO.VRPREUNITPED%TYPE;
   V_VRMARGITPED     ITEMPEDIDO.VRMARGITPED%TYPE;
   V_NRSEQMARGEM     ITEMPEDIDO.NRSEQMARGEM%TYPE;
BEGIN
   V_VRPECOMIVEND := 0;

   BEGIN
      SELECT IDCTRLVENEXP
      INTO V_IDCTRLVENEXP
      FROM PAREXPEDICAO
      WHERE CDFILIAL = P_CDFILIAL;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_IDCTRLVENEXP := '';
   END;

   BEGIN
      SELECT IDGERACOMISVEND
        INTO V_IDGERACOMISVEND
        FROM VENDEDOR
       WHERE CDVENDEDOR = P_CDVENDEDOR;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
        V_IDGERACOMISVEND := 'N';
   END;

   BEGIN
      SELECT MAX(P.CDPRECOBASE), MAX(P.CDCLIENTE), MAX(PB.IDULTPRECPROMO)
        INTO V_CDPRECOBASE, V_CDCLIENTE, V_IDULTPRECPROMO
        FROM PEDIDO P JOIN PRECOBASE PB
                        ON P.CDPRECOBASE = PB.CDPRECOBASE
       WHERE P.CDFILIAL = P_CDFILIAL
         AND P.NRPEDIDO = P_NRPEDIDO;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_CDPRECOBASE := NULL;
         V_CDCLIENTE   := NULL;
   END;

   IF V_IDGERACOMISVEND = 'S' THEN
      FOR C_VEND IN (
                     SELECT CDVENDEDOR, IDGERACOMISVEND, IDCARGOVEND
                       FROM (
                             SELECT VV.CDVENDEDOR, VV.IDGERACOMISVEND, VV.IDCARGOVEND
                               FROM VENDEDOR VV
                              WHERE VV.CDVENDEDOR    = P_CDVENDEDOR

                              UNION

                             SELECT VA.CDVENDEDOR, VA.IDGERACOMISVEND, VA.IDCARGOVEND
                               FROM VENDEDOR VV,
                                    VENDEDOR VA
                              WHERE VV.CDVENDEDORAUX   = VA.CDVENDEDOR (+)
                                AND VV.CDVENDEDOR      = P_CDVENDEDOR
                                AND VA.IDGERACOMISVEND = 'S'
                                AND NVL(P_COMISPERIODO, 'N') = 'N'
                              UNION

                             SELECT VS.CDVENDEDOR, VS.IDGERACOMISVEND, VS.IDCARGOVEND
                               FROM VENDEDOR VV,
                                    VENDEDOR VS
                              WHERE VV.CDVENDEDORSUP   = VS.CDVENDEDOR (+)
                                AND VV.CDVENDEDOR      = P_CDVENDEDOR
                                AND VS.IDGERACOMISVEND = 'S'
                                AND NVL(P_COMISPERIODO, 'N') = 'N'
                              UNION

                             SELECT VG.CDVENDEDOR, VG.IDGERACOMISVEND, VG.IDCARGOVEND
                               FROM VENDEDOR VV,
                                    VENDEDOR VG
                              WHERE VV.CDVENDEDORGER   = VG.CDVENDEDOR (+)
                                AND VV.CDVENDEDOR      = P_CDVENDEDOR
                                AND VG.IDGERACOMISVEND = 'S'
                                AND NVL(P_COMISPERIODO, 'N') = 'N'
                              UNION
                             SELECT VC.CDVENDEDOR, VA1.IDGERACOMISVEND, VA1.IDCARGOVEND
                               FROM VENDADICCLIE VC, VENDEDOR VA1
                              WHERE VC.CDCLIENTE = V_CDCLIENTE
                                AND VC.CDVENDEDOR = VA1.CDVENDEDOR
                                AND NVL(P_COMISPERIODO, 'N') = 'N'
                              )
                        ORDER BY CDVENDEDOR
      ) LOOP

         IF C_VEND.IDGERACOMISVEND = 'S' THEN
            IF V_IDCTRLVENEXP IN ('S', 'R') THEN
               IF (P_NRITEMPED IS NULL) THEN
                  FOR C_ITEM IN (SELECT I.NRITEMPED, I.IDSITITEMPED, I.VRPREUNITPED, I.CDPRODUTO, NVL(I.IDITPEDDIFER, 'N') AS IDITPEDDIFER,
                                        NVL(I.VRDESCCOMISS,0) VRDESCCOMISS, ROUND((I.VRPREUNITPED - (NVL(I.VRDESITPED,0)/I.QTITEMPED)),2) AS VRUNITDESCITEM,
                                        I.VRMARGITPED, I.NRSEQMARGEM
                                   FROM ITEMPEDIDO I
                                  WHERE (I.CDFILIAL = P_CDFILIAL)
                                    AND (I.NRPEDIDO = P_NRPEDIDO)
                  ) LOOP
                     IF V_IDULTPRECPROMO = 'M' THEN
                        V_VRMARGITPED := C_ITEM.VRMARGITPED;
                        IF (V_VRMARGITPED IS NULL) THEN
                           BEGIN
                              SELECT VRMARGEM
                                INTO V_VRMARGITPED
                                FROM MARGEMITPREBASE
                               WHERE CDPRECOBASE = V_CDPRECOBASE
                                 AND NRSEQMARGEM = C_ITEM.NRSEQMARGEM
                                 AND CDPRODUTO = C_ITEM.CDPRODUTO;
                           EXCEPTION
                              WHEN NO_DATA_FOUND THEN
                                 V_VRMARGITPED := 0;
                           END;
                        END IF;
                        V_VRPRECO := V_VRMARGITPED;
                     ELSE
                        V_VRPRECO := C_ITEM.VRUNITDESCITEM;
                     END IF;
                     BUSCA_COMISSAO(P_CDFILIAL, C_VEND.CDVENDEDOR, C_ITEM.CDPRODUTO, V_VRPRECO , V_VRPECOMIVEND, C_ITEM.IDITPEDDIFER, V_CDCLIENTE, V_IDCTRLVENEXP, V_CDPRECOBASE, C_ITEM.VRDESCCOMISS);

                     SELECT COUNT(*) INTO V_CTPEVEND
                       FROM VENDITEMPED
                      WHERE (CDFILIAL   = P_CDFILIAL) AND
                            (NRPEDIDO   = P_NRPEDIDO) AND
                            (NRITEMPED  = C_ITEM.NRITEMPED) AND
                            (CDVENDEDOR = C_VEND.CDVENDEDOR);

                     IF V_CTPEVEND = 0 THEN
                        IF V_VRPECOMIVEND > 0 THEN
                           INSERT INTO VENDITEMPED
                             (CDFILIAL, NRPEDIDO, NRITEMPED,
                              CDVENDEDOR, VRPECOMITPED, IDCARGOVEND)
                           VALUES
                             (P_CDFILIAL, P_NRPEDIDO, C_ITEM.NRITEMPED,
                              C_VEND.CDVENDEDOR, V_VRPECOMIVEND, C_VEND.IDCARGOVEND);
                        END IF;

                        V_VRCOMISANT := NULL;
                        IF NOT P_OLDCDVENDE IS NULL THEN
                           SELECT COUNT(*) INTO V_CTREGISTRO
                             FROM VENDITEMPED
                            WHERE CDFILIAL   = P_CDFILIAL
                              AND NRPEDIDO   = P_NRPEDIDO
                              AND NRITEMPED  = C_ITEM.NRITEMPED
                              AND CDVENDEDOR = P_OLDCDVENDE ;

                           IF V_CTREGISTRO > 0 THEN
                              SELECT VRPECOMITPED INTO V_VRCOMISANT
                                FROM VENDITEMPED
                               WHERE CDFILIAL   = P_CDFILIAL
                                 AND NRPEDIDO   = P_NRPEDIDO
                                 AND NRITEMPED  = C_ITEM.NRITEMPED
                                 AND CDVENDEDOR = P_OLDCDVENDE;
                           END IF;

                           FOR C_CARGO IN (
                              SELECT CDVENDEDOR, IDGERACOMISVEND
                                FROM (SELECT VV.CDVENDEDOR, VV.IDGERACOMISVEND
                                        FROM VENDEDOR VV
                                       WHERE VV.CDVENDEDOR    = P_OLDCDVENDE

                                      UNION

                                      SELECT VA.CDVENDEDOR, VA.IDGERACOMISVEND
                                        FROM VENDEDOR VV, VENDEDOR VA
                                       WHERE VV.CDVENDEDORAUX   = VA.CDVENDEDOR (+)
                                         AND VV.CDVENDEDOR      = P_OLDCDVENDE
                                         AND VA.IDGERACOMISVEND = 'S'

                                      UNION

                                      SELECT VS.CDVENDEDOR, VS.IDGERACOMISVEND
                                        FROM VENDEDOR VV, VENDEDOR VS
                                       WHERE VV.CDVENDEDORSUP   = VS.CDVENDEDOR (+)
                                         AND VV.CDVENDEDOR      = P_OLDCDVENDE
                                         AND VS.IDGERACOMISVEND = 'S'

                                      UNION

                                      SELECT VG.CDVENDEDOR, VG.IDGERACOMISVEND
                                        FROM VENDEDOR VV, VENDEDOR VG
                                       WHERE VV.CDVENDEDORGER   = VG.CDVENDEDOR (+)
                                         AND VV.CDVENDEDOR      = P_OLDCDVENDE
                                         AND VG.IDGERACOMISVEND = 'S'
                                    )
                                 ORDER BY CDVENDEDOR
                           ) LOOP
                              DELETE FROM VENDITEMPED
                               WHERE CDFILIAL   = P_CDFILIAL
                                 AND NRPEDIDO   = P_NRPEDIDO
                                 AND NRITEMPED  = C_ITEM.NRITEMPED
                                 AND CDVENDEDOR = C_CARGO.CDVENDEDOR;
                           END LOOP;
                        END IF;
                     ELSE
                        SELECT COUNT(*) INTO V_CTREGISTRO
                          FROM VENDITEMPED
                         WHERE (CDFILIAL   = P_CDFILIAL) AND
                               (NRPEDIDO   = P_NRPEDIDO) AND
                               (NRITEMPED  = C_ITEM.NRITEMPED) AND
                               (CDVENDEDOR = C_VEND.CDVENDEDOR);

                        IF V_CTREGISTRO >0 THEN
                           SELECT VRPECOMITPED INTO V_VRCOMISANT
                             FROM VENDITEMPED
                            WHERE (CDFILIAL   = P_CDFILIAL) AND
                                  (NRPEDIDO   = P_NRPEDIDO) AND
                                  (NRITEMPED  = C_ITEM.NRITEMPED) AND
                                  (CDVENDEDOR = C_VEND.CDVENDEDOR);
                        END IF;

                        UPDATE VENDITEMPED
                           SET VRPECOMITPED = V_VRPECOMIVEND
                         WHERE (CDFILIAL   = P_CDFILIAL) AND
                               (NRPEDIDO   = P_NRPEDIDO) AND
                               (NRITEMPED  = C_ITEM.NRITEMPED) AND
                               (CDVENDEDOR = C_VEND.CDVENDEDOR);
                     END IF;

           		      INSERT INTO HISITEMPED
                         (CDFILIAL, IDSITATUHIST, NRITEMPED, IDSITANTHIST, IDALTHISTITE, DTHRHIST,
                          NRPEDIDO, CDOPEMANHIST,CDVENDORIG, VRCOMISSORIG,CDVENDATUAL,VRCOMISATUAL)
                     VALUES
                         (P_CDFILIAL, NVL(C_ITEM.IDSITITEMPED,'1'), C_ITEM.NRITEMPED, NVL(C_ITEM.IDSITITEMPED,'1'), '3', SYSDATE + 2/64/64/24,
                          P_NRPEDIDO, P_CDOPERADOR, P_OLDCDVENDE, V_VRCOMISANT, C_VEND.CDVENDEDOR, V_VRPECOMIVEND);

                  END LOOP;
               ELSE
                  /* raise_application_error(-20504,'1000', True); */
                  BEGIN
                     SELECT NVL(I.IDITPEDDIFER, 'N'), NVL(I.VRDESCCOMISS,0) VRDESCCOMISS, I.VRMARGITPED, I.NRSEQMARGEM
                       INTO V_IDITPEDDIFER, V_VRDESCCOMISS, V_VRMARGITPED, V_NRSEQMARGEM
                       FROM ITEMPEDIDO I
                      WHERE (I.CDFILIAL   = P_CDFILIAL) AND
                            (I.NRPEDIDO   = P_NRPEDIDO) AND
                            (I.NRITEMPED  = P_NRITEMPED);
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                        V_IDITPEDDIFER := 'N';
                        V_VRDESCCOMISS := 0;
                        V_VRMARGITPED  := NULL;
                        V_NRSEQMARGEM  := NULL;
                     END;

                  IF V_IDULTPRECPROMO = 'M' THEN
                     IF (V_VRMARGITPED IS NULL) THEN
                        BEGIN
                           SELECT VRMARGEM
                             INTO V_VRMARGITPED
                             FROM MARGEMITPREBASE
                            WHERE CDPRECOBASE = V_CDPRECOBASE
                              AND NRSEQMARGEM = V_NRSEQMARGEM
                              AND CDPRODUTO   = P_CDPRODUTO;
                        EXCEPTION
                           WHEN NO_DATA_FOUND THEN
                              V_VRMARGITPED := 0;
                        END;
                     END IF;
                     V_VRPRECO := V_VRMARGITPED;
                  ELSE
                     V_VRPRECO := P_VRPREUNITPED;
                  END IF;
                  BUSCA_COMISSAO(P_CDFILIAL, C_VEND.CDVENDEDOR, P_CDPRODUTO, V_VRPRECO, V_VRPECOMIVEND, V_IDITPEDDIFER, V_CDCLIENTE, V_IDCTRLVENEXP, V_CDPRECOBASE, V_VRDESCCOMISS);

                  SELECT COUNT(*) INTO V_CTPEVEND
                    FROM VENDITEMPED
                   WHERE (CDFILIAL   = P_CDFILIAL) AND
                         (NRPEDIDO   = P_NRPEDIDO) AND
                         (NRITEMPED  = P_NRITEMPED) AND
                         (CDVENDEDOR = C_VEND.CDVENDEDOR);

                  IF V_CTPEVEND = 0 THEN
                     IF V_VRPECOMIVEND > 0 THEN
                        INSERT INTO VENDITEMPED
                          (CDFILIAL, NRPEDIDO, NRITEMPED,
                           CDVENDEDOR, VRPECOMITPED, IDCARGOVEND)
                        VALUES
                          (P_CDFILIAL, P_NRPEDIDO, P_NRITEMPED,
                           C_VEND.CDVENDEDOR, V_VRPECOMIVEND, C_VEND.IDCARGOVEND);
                     END IF;

                     V_VRCOMISANT := NULL;
                     IF NOT P_OLDCDVENDE IS NULL THEN
                        SELECT COUNT(*) INTO V_CTREGISTRO
                          FROM VENDITEMPED
                         WHERE CDFILIAL   = P_CDFILIAL
                           AND NRPEDIDO   = P_NRPEDIDO
                           AND NRITEMPED  = P_NRITEMPED
                           AND CDVENDEDOR = P_OLDCDVENDE;

                        IF V_CTREGISTRO >0 THEN
                           SELECT VRPECOMITPED INTO V_VRCOMISANT
                             FROM VENDITEMPED
                            WHERE CDFILIAL   = P_CDFILIAL
                              AND NRPEDIDO   = P_NRPEDIDO
                              AND NRITEMPED  = P_NRITEMPED
                              AND CDVENDEDOR = P_OLDCDVENDE;
                        END IF;

                        FOR C_CARGO IN (
                           SELECT CDVENDEDOR, IDGERACOMISVEND
                             FROM (SELECT VV.CDVENDEDOR, VV.IDGERACOMISVEND
                                     FROM VENDEDOR VV
                                    WHERE VV.CDVENDEDOR    = P_OLDCDVENDE

                                   UNION

                                   SELECT VA.CDVENDEDOR, VA.IDGERACOMISVEND
                                     FROM VENDEDOR VV, VENDEDOR VA
                                    WHERE VV.CDVENDEDORAUX   = VA.CDVENDEDOR (+)
                                      AND VV.CDVENDEDOR      = P_OLDCDVENDE
                                      AND VA.IDGERACOMISVEND = 'S'

                                   UNION

                                   SELECT VS.CDVENDEDOR, VS.IDGERACOMISVEND
                                     FROM VENDEDOR VV, VENDEDOR VS
                                    WHERE VV.CDVENDEDORSUP   = VS.CDVENDEDOR (+)
                                      AND VV.CDVENDEDOR      = P_OLDCDVENDE
                                      AND VS.IDGERACOMISVEND = 'S'

                                   UNION

                                   SELECT VG.CDVENDEDOR, VG.IDGERACOMISVEND
                                     FROM VENDEDOR VV, VENDEDOR VG
                                    WHERE VV.CDVENDEDORGER   = VG.CDVENDEDOR (+)
                                      AND VV.CDVENDEDOR      = P_OLDCDVENDE
                                      AND VG.IDGERACOMISVEND = 'S'
                                 )
                              ORDER BY CDVENDEDOR
                        ) LOOP
                           DELETE FROM VENDITEMPED
                         WHERE CDFILIAL   = P_CDFILIAL
                           AND NRPEDIDO   = P_NRPEDIDO
                           AND NRITEMPED  = P_NRITEMPED
                           AND CDVENDEDOR = C_CARGO.CDVENDEDOR;
                        END LOOP;
                     END IF;
                  ELSE
                     SELECT COUNT(*) INTO V_CTREGISTRO
                       FROM VENDITEMPED
                      WHERE (CDFILIAL   = P_CDFILIAL) AND
                            (NRPEDIDO   = P_NRPEDIDO) AND
                            (NRITEMPED  = P_NRITEMPED) AND
                            (CDVENDEDOR = C_VEND.CDVENDEDOR);

                     IF V_CTREGISTRO >0 THEN
                        SELECT VRPECOMITPED INTO V_VRCOMISANT
                          FROM VENDITEMPED
                         WHERE (CDFILIAL   = P_CDFILIAL) AND
                               (NRPEDIDO   = P_NRPEDIDO) AND
                               (NRITEMPED  = P_NRITEMPED) AND
                               (CDVENDEDOR = C_VEND.CDVENDEDOR);
                     END IF;

                     UPDATE VENDITEMPED
                        SET VRPECOMITPED = V_VRPECOMIVEND
                      WHERE (CDFILIAL   = P_CDFILIAL) AND
                            (NRPEDIDO   = P_NRPEDIDO) AND
                            (NRITEMPED  = P_NRITEMPED) AND
                            (CDVENDEDOR = C_VEND.CDVENDEDOR);
                  END IF;

                  INSERT INTO HISITEMPED
                      (CDFILIAL, IDSITATUHIST, NRITEMPED, IDSITANTHIST, IDALTHISTITE, DTHRHIST,
                       NRPEDIDO, CDOPEMANHIST, CDVENDORIG, VRCOMISSORIG,CDVENDATUAL,VRCOMISATUAL)
                  VALUES
                      (P_CDFILIAL, NVL(P_IDSITITEMPED,'1'), P_NRITEMPED, NVL(P_IDSITITEMPED,'1'), '3', SYSDATE + 2/64/64/24,
                       P_NRPEDIDO, P_CDOPERADOR, P_OLDCDVENDE, V_VRCOMISANT, C_VEND.CDVENDEDOR, V_VRPECOMIVEND);
               END IF;
            ELSIF V_IDCTRLVENEXP = 'I' THEN
               IF (P_NRITEMPED IS NULL) THEN
                  FOR C_ITEM IN (SELECT I.NRITEMPED, I.IDSITITEMPED, I.VRPREUNITPED, I.CDPRODUTO, NVL(I.IDITPEDDIFER, 'N') AS IDITPEDDIFER,
                                        I.VRITRENTABIL, NVL(I.VRDESCCOMISS,0) VRDESCCOMISS, ROUND((I.VRPREUNITPED - (I.VRDESITPED/I.QTITEMPED)),2) AS VRUNITDESCITEM,
                                        I.VRMARGITPED, I.NRSEQMARGEM
                                   FROM ITEMPEDIDO I
                                  WHERE (I.CDFILIAL = P_CDFILIAL)
                                    AND (I.NRPEDIDO = P_NRPEDIDO)
                  ) LOOP

                     IF V_IDULTPRECPROMO = 'M' THEN
                        V_VRMARGITPED := C_ITEM.VRMARGITPED;
                        IF (V_VRMARGITPED IS NULL) THEN
                           BEGIN
                              SELECT VRMARGEM
                                INTO V_VRMARGITPED
                                FROM MARGEMITPREBASE
                               WHERE CDPRECOBASE = V_CDPRECOBASE
                                 AND NRSEQMARGEM = C_ITEM.NRSEQMARGEM
                                 AND CDPRODUTO   = C_ITEM.CDPRODUTO;
                           EXCEPTION
                              WHEN NO_DATA_FOUND THEN
                                 V_VRMARGITPED := 0;
                           END;
                        END IF;
                        V_VRPRECO := C_ITEM.VRMARGITPED;
                     ELSE
                        V_VRPRECO := C_ITEM.VRUNITDESCITEM;
                     END IF;
                     BUSCA_COMISSAO(P_CDFILIAL, C_VEND.CDVENDEDOR, C_ITEM.CDPRODUTO, V_VRPRECO, V_VRPECOMIVEND, C_ITEM.IDITPEDDIFER, V_CDCLIENTE, V_IDCTRLVENEXP, V_CDPRECOBASE, C_ITEM.VRDESCCOMISS);

                     SELECT COUNT(*) INTO V_CTPEVEND
                       FROM VENDITEMPED
                      WHERE (CDFILIAL   = P_CDFILIAL         )
                        AND (NRPEDIDO   = P_NRPEDIDO         )
                        AND (NRITEMPED  = C_ITEM.NRITEMPED   )
                        AND (CDVENDEDOR = C_VEND.CDVENDEDOR  );
                     IF V_CTPEVEND = 0 THEN
                        IF V_VRPECOMIVEND > 0 THEN

                              INSERT INTO VENDITEMPED
                                (CDFILIAL, NRPEDIDO, NRITEMPED,
                                 CDVENDEDOR, VRPECOMITPED, IDCARGOVEND)
                              VALUES
                                (P_CDFILIAL, P_NRPEDIDO, C_ITEM.NRITEMPED,
                                 C_VEND.CDVENDEDOR, V_VRPECOMIVEND, C_VEND.IDCARGOVEND);
                        END IF;
                     ELSE
                        SELECT COUNT(*) INTO V_CTREGISTRO
                          FROM VENDITEMPED
                         WHERE (CDFILIAL   = P_CDFILIAL         )
                           AND (NRPEDIDO   = P_NRPEDIDO         )
                           AND (NRITEMPED  = C_ITEM.NRITEMPED   )
                           AND (CDVENDEDOR = C_VEND.CDVENDEDOR  );

                        IF V_CTREGISTRO >0 THEN
                           SELECT VRPECOMITPED INTO V_VRCOMISANT
                             FROM VENDITEMPED
                            WHERE (CDFILIAL   = P_CDFILIAL         )
                              AND (NRPEDIDO   = P_NRPEDIDO         )
                              AND (NRITEMPED  = C_ITEM.NRITEMPED   )
                              AND (CDVENDEDOR = C_VEND.CDVENDEDOR  );
                        END IF;

                        UPDATE VENDITEMPED
                           SET VRPECOMITPED = V_VRPECOMIVEND
                         WHERE (CDFILIAL   = P_CDFILIAL         )
                           AND (NRPEDIDO   = P_NRPEDIDO         )
                           AND (NRITEMPED  = C_ITEM.NRITEMPED   )
                           AND (CDVENDEDOR = C_VEND.CDVENDEDOR  );
                     END IF;

                  END LOOP;
               ELSE
                  /* raise_application_error(-20504,'1000', True); */
                  BEGIN
                     SELECT NVL(I.IDITPEDDIFER, 'N'),  NVL(I.VRDESCCOMISS,0) VRDESCCOMISS, I.VRMARGITPED, I.NRSEQMARGEM
                       INTO V_IDITPEDDIFER,  V_VRDESCCOMISS, V_VRMARGITPED, V_NRSEQMARGEM
                       FROM ITEMPEDIDO I
                      WHERE (I.CDFILIAL  = P_CDFILIAL )
                        AND (I.NRPEDIDO  = P_NRPEDIDO )
                        AND (I.NRITEMPED = P_NRITEMPED);
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                        V_IDITPEDDIFER := 'N';
                        V_VRDESCCOMISS := 0;
                        V_VRMARGITPED  := NULL;
                        V_NRSEQMARGEM  := NULL;
                  END;

                  IF V_IDULTPRECPROMO = 'M' THEN
                     IF (V_VRMARGITPED IS NULL) THEN
                        BEGIN
                           SELECT VRMARGEM
                             INTO V_VRMARGITPED
                             FROM MARGEMITPREBASE
                            WHERE CDPRECOBASE = V_CDPRECOBASE
                              AND NRSEQMARGEM = V_NRSEQMARGEM
                              AND CDPRODUTO   = P_CDPRODUTO;
                        EXCEPTION
                           WHEN NO_DATA_FOUND THEN
                              V_VRMARGITPED := 0;
                        END;
                     END IF;
                     V_VRPRECO := V_VRMARGITPED;
                  ELSE
                     V_VRPRECO := P_VRPREUNITPED;
                  END IF;
                  BUSCA_COMISSAO(P_CDFILIAL, C_VEND.CDVENDEDOR, P_CDPRODUTO, V_VRPRECO, V_VRPECOMIVEND, V_IDITPEDDIFER, V_CDCLIENTE, V_IDCTRLVENEXP, V_CDPRECOBASE, V_VRDESCCOMISS);

                  SELECT COUNT(*) INTO V_CTPEVEND
                    FROM VENDITEMPED
                   WHERE (CDFILIAL   = P_CDFILIAL)
                     AND (NRPEDIDO   = P_NRPEDIDO)
                     AND (NRITEMPED  = P_NRITEMPED)
                     AND (CDVENDEDOR = C_VEND.CDVENDEDOR);

                  IF V_CTPEVEND = 0 THEN
                     IF V_VRPECOMIVEND > 0 THEN

                           INSERT INTO VENDITEMPED
                             (CDFILIAL, NRPEDIDO, NRITEMPED,
                              CDVENDEDOR, VRPECOMITPED, IDCARGOVEND)
                           VALUES
                             (P_CDFILIAL, P_NRPEDIDO, P_NRITEMPED,
                              C_VEND.CDVENDEDOR, V_VRPECOMIVEND, C_VEND.IDCARGOVEND);
                     END IF;
                  ELSE
                     SELECT COUNT(*) INTO V_CTREGISTRO
                       FROM VENDITEMPED
                      WHERE (CDFILIAL   = P_CDFILIAL)
                        AND (NRPEDIDO   = P_NRPEDIDO)
                        AND (NRITEMPED  = P_NRITEMPED)
                        AND (CDVENDEDOR = C_VEND.CDVENDEDOR);

                     IF V_CTREGISTRO >0 THEN
                        SELECT VRPECOMITPED INTO V_VRCOMISANT
                          FROM VENDITEMPED
                         WHERE (CDFILIAL   = P_CDFILIAL)
                           AND (NRPEDIDO   = P_NRPEDIDO)
                           AND (NRITEMPED  = P_NRITEMPED)
                           AND (CDVENDEDOR = C_VEND.CDVENDEDOR);
                     END IF;

                     UPDATE VENDITEMPED
                        SET VRPECOMITPED = V_VRPECOMIVEND
                      WHERE (CDFILIAL    = P_CDFILIAL         )
                        AND (NRPEDIDO    = P_NRPEDIDO         )
                        AND (NRITEMPED   = P_NRITEMPED        )
                        AND (CDVENDEDOR  = C_VEND.CDVENDEDOR  );
                  END IF;
               END IF;
            ELSE IF V_IDCTRLVENEXP = 'M' THEN
               FOR C_ITEM IN (SELECT NRITEMPED, IDSITITEMPED, VRPERCMARKUPIT
                                FROM ITEMPEDIDO
                                WHERE (CDFILIAL  = P_CDFILIAL) AND
                                      (NRPEDIDO  = P_NRPEDIDO)AND
                                      (NRITEMPED =  NVL(P_NRITEMPED, NRITEMPED))
               ) LOOP
                  V_VRCOMISANT := NULL;
                  IF C_ITEM.VRPERCMARKUPIT > 0 THEN
                     BEGIN
                         SELECT MAX(I.VRPECOMISSAO)
                          INTO V_VRPECOMIVEND
                          FROM VENDEDOR V, ITCOMMARKUP I
                         WHERE V.CDVENDEDOR = C_VEND.CDVENDEDOR
                           AND V.CDMARKUP = I.CDMARKUP
                           AND I.CDFILIAL = P_CDFILIAL
                           AND C_ITEM.VRPERCMARKUPIT BETWEEN VRINICACRES AND VRFINACRES;
                     EXCEPTION
                        WHEN NO_DATA_FOUND THEN
                           V_VRPECOMIVEND := 0;
                     END;

                     IF V_VRPECOMIVEND > 0 THEN
                        BEGIN
                           SELECT VRPECOMITPED
                             INTO V_VRCOMISANT
                             FROM VENDITEMPED
                            WHERE CDFILIAL   = P_CDFILIAL
                              AND NRPEDIDO   = P_NRPEDIDO
                              AND NRITEMPED  = C_ITEM.NRITEMPED
                              AND CDVENDEDOR = NVL(P_OLDCDVENDE, C_VEND.CDVENDEDOR);
                        EXCEPTION
                           WHEN NO_DATA_FOUND THEN
                              V_VRCOMISANT := NULL;
                        END;
                        DELETE FROM VENDITEMPED
                         WHERE (CDFILIAL   = P_CDFILIAL) AND
                               (NRPEDIDO   = P_NRPEDIDO) AND
                               (NRITEMPED  = C_ITEM.NRITEMPED) AND
                               (CDVENDEDOR = C_VEND.CDVENDEDOR);

                        INSERT INTO VENDITEMPED
                           (CDFILIAL, NRPEDIDO, NRITEMPED, CDVENDEDOR, VRPECOMITPED, IDCARGOVEND)
                        VALUES
                           (P_CDFILIAL, P_NRPEDIDO, C_ITEM.NRITEMPED, C_VEND.CDVENDEDOR, V_VRPECOMIVEND, C_VEND.IDCARGOVEND);
                     END IF;

                     IF NOT P_OLDCDVENDE IS NULL THEN
                        BEGIN
                           SELECT VRPECOMITPED
                             INTO V_VRCOMISANT
                             FROM VENDITEMPED
                            WHERE CDFILIAL   = P_CDFILIAL
                              AND NRPEDIDO   = P_NRPEDIDO
                              AND NRITEMPED  = C_ITEM.NRITEMPED
                              AND CDVENDEDOR = P_OLDCDVENDE ;

                        EXCEPTION
                           WHEN NO_DATA_FOUND THEN
                              V_VRCOMISANT := NULL;
                        END;
                     END IF;

                     INSERT INTO HISITEMPED
                           (CDFILIAL, IDSITATUHIST, NRITEMPED, IDSITANTHIST, IDALTHISTITE, DTHRHIST,
                            NRPEDIDO, CDOPEMANHIST,CDVENDORIG, VRCOMISSORIG,CDVENDATUAL,VRCOMISATUAL)
                     VALUES
                           (P_CDFILIAL, NVL(C_ITEM.IDSITITEMPED,'1'), C_ITEM.NRITEMPED, NVL(C_ITEM.IDSITITEMPED,'1'), '3', SYSDATE + 2/64/64/24,
                            P_NRPEDIDO, P_CDOPERADOR, P_OLDCDVENDE, V_VRCOMISANT, C_VEND.CDVENDEDOR, V_VRPECOMIVEND);

                     IF NOT P_OLDCDVENDE IS NULL THEN
                        DELETE FROM VENDITEMPED
                         WHERE CDFILIAL   = P_CDFILIAL
                           AND NRPEDIDO   = P_NRPEDIDO
                           AND NRITEMPED  = C_ITEM.NRITEMPED
                           AND CDVENDEDOR = P_OLDCDVENDE;
                     END IF;
                  ELSE
                     DELETE FROM VENDITEMPED
                      WHERE
                         CDFILIAL  = P_CDFILIAL AND
                         NRPEDIDO  = P_NRPEDIDO AND
                         NRITEMPED = C_ITEM.NRITEMPED;
                  END IF;
               END LOOP;
            END IF;
            END IF;
         END IF;
      END LOOP;
   END IF;
END;
/