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;
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 CDPRECOBASE
        INTO V_CDPRECOBASE
        FROM PEDIDO
       WHERE CDFILIAL = P_CDFILIAL
         AND NRPEDIDO = P_NRPEDIDO;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_CDPRECOBASE := NULL;
   END;

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

                              UNION

                             SELECT VA.CDVENDEDOR, VA.IDGERACOMISVEND
                               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
                               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
                               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'
                              )
                        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,
                                        P.CDCLIENTE, NVL(I.VRDESCCOMISS,0) VRDESCCOMISS, ROUND((I.VRPREUNITPED - (I.VRDESITPED/I.QTITEMPED)),2) AS VRUNITDESCITEM
                                   FROM ITEMPEDIDO I, PEDIDO P
                                  WHERE (P.CDFILIAL = P_CDFILIAL) AND
                                        (P.NRPEDIDO = P_NRPEDIDO) AND
                                        (P.CDFILIAL = I.CDFILIAL) AND
                                        (P.NRPEDIDO = I.NRPEDIDO)
                  ) LOOP

                     BUSCA_COMISSAO(P_CDFILIAL, C_VEND.CDVENDEDOR, C_ITEM.CDPRODUTO, C_ITEM.VRUNITDESCITEM , V_VRPECOMIVEND, C_ITEM.IDITPEDDIFER, C_ITEM.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);

                     /* SE RETORNAR > ZERO O USUARIO ESTA ALTERANDO SOMENTE O VALOR */

                     IF V_CTPEVEND = 0 THEN
                        IF V_VRPECOMIVEND > 0 THEN
                           INSERT INTO VENDITEMPED
                             (CDFILIAL, NRPEDIDO, NRITEMPED,
                              CDVENDEDOR, VRPECOMITPED)
                           VALUES
                             (P_CDFILIAL, P_NRPEDIDO, C_ITEM.NRITEMPED,
                              C_VEND.CDVENDEDOR, V_VRPECOMIVEND);
                        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(IDITPEDDIFER, 'N'), P.CDCLIENTE, NVL(I.VRDESCCOMISS,0) VRDESCCOMISS
                       INTO V_IDITPEDDIFER, V_CDCLIENTE, V_VRDESCCOMISS
                       FROM ITEMPEDIDO I, PEDIDO P
                      WHERE (I.CDFILIAL   = P_CDFILIAL) AND
                            (I.NRPEDIDO   = P_NRPEDIDO) AND
                            (I.NRITEMPED  = P_NRITEMPED) AND
                            (I.CDFILIAL = P.CDFILIAL)AND
                            (I.NRPEDIDO = P.NRPEDIDO);
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                        V_IDITPEDDIFER := 'N';
                        V_VRDESCCOMISS := 0;
                     END;

                  BUSCA_COMISSAO(P_CDFILIAL, C_VEND.CDVENDEDOR, P_CDPRODUTO, P_VRPREUNITPED, 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)
                        VALUES
                          (P_CDFILIAL, P_NRPEDIDO, P_NRITEMPED,
                           C_VEND.CDVENDEDOR, V_VRPECOMIVEND);
                     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,
                                        P.CDCLIENTE, I.VRITRENTABIL, NVL(I.VRDESCCOMISS,0) VRDESCCOMISS, ROUND((I.VRPREUNITPED - (I.VRDESITPED/I.QTITEMPED)),2) AS VRUNITDESCITEM
                                   FROM ITEMPEDIDO I, PEDIDO P
                                  WHERE (P.CDFILIAL = P_CDFILIAL)
                                    AND (P.NRPEDIDO = P_NRPEDIDO)
                                    AND (P.CDFILIAL = I.CDFILIAL)
                                    AND (P.NRPEDIDO = I.NRPEDIDO)
                  ) LOOP

                     BUSCA_COMISSAO(P_CDFILIAL, C_VEND.CDVENDEDOR, C_ITEM.CDPRODUTO, C_ITEM.VRITRENTABIL, V_VRPECOMIVEND, C_ITEM.IDITPEDDIFER, C_ITEM.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  );

                     /* SE RETORNAR > ZERO O USUARIO ESTA ALTERANDO SOMENTE O VALOR */

                     IF V_CTPEVEND = 0 THEN
                        IF V_VRPECOMIVEND > 0 THEN

                              INSERT INTO VENDITEMPED
                                (CDFILIAL, NRPEDIDO, NRITEMPED,
                                 CDVENDEDOR, VRPECOMITPED)
                              VALUES
                                (P_CDFILIAL, P_NRPEDIDO, C_ITEM.NRITEMPED,
                                 C_VEND.CDVENDEDOR, V_VRPECOMIVEND);
                        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(IDITPEDDIFER, 'N'), P.CDCLIENTE, I.VRITRENTABIL,
                            NVL(I.VRDESCCOMISS,0) VRDESCCOMISS
                       INTO V_IDITPEDDIFER, V_CDCLIENTE, V_VRITRENTABIL, V_VRDESCCOMISS
                       FROM ITEMPEDIDO I, PEDIDO P
                      WHERE (I.CDFILIAL  = P_CDFILIAL )
                        AND (I.NRPEDIDO  = P_NRPEDIDO )
                        AND (I.NRITEMPED = P_NRITEMPED)
                        AND (I.CDFILIAL  = P.CDFILIAL )
                        AND (I.NRPEDIDO  = P.NRPEDIDO );
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                        V_IDITPEDDIFER := 'N';
                  END;

                  BUSCA_COMISSAO(P_CDFILIAL, C_VEND.CDVENDEDOR, P_CDPRODUTO, V_VRITRENTABIL, 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)
                           VALUES
                             (P_CDFILIAL, P_NRPEDIDO, P_NRITEMPED,
                              C_VEND.CDVENDEDOR, V_VRPECOMIVEND);
                     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;
                        /* PARA DELETAR A COMISSAO ANTERIOR SE O VALOR DO ITEM FOR TROCADO E O VENDEDOR FOR O MESMO */
                        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)
                        VALUES
                           (P_CDFILIAL, P_NRPEDIDO, C_ITEM.NRITEMPED, C_VEND.CDVENDEDOR, V_VRPECOMIVEND);
                     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;
/