IF EXISTS (
			SELECT *
			  FROM SYSOBJECTS O, SYSUSERS U
			 WHERE (O.NAME = 'GERA_COMISSAOVEND')
				AND (TYPE	= 'P'  )
				AND (U.UID  = O.UID)
				AND (U.NAME = USER))
DROP PROCEDURE GERA_COMISSAOVEND
GO
CREATE PROCEDURE GERA_COMISSAOVEND 
	@P_CDFILIAL			VARCHAR(04),
	@P_NRPEDIDO			VARCHAR(10),
	@P_CDVENDEDOR		VARCHAR(04),
	@P_CDOPERADOR		VARCHAR(12),
	@P_NRITEMPED		VARCHAR(10),
	@P_IDSITITEMPED	VARCHAR(01) = NULL,
	@P_CDPRODUTO		VARCHAR(10) = NULL,
	@P_VRPREUNITPED	DOUBLE PRECISION = NULL,
	@P_OLDCDVENDE		VARCHAR(04) = NULL
AS
	DECLARE @V_NRITEMPED			VARCHAR(10);
	DECLARE @V_IDSITITEMPED		VARCHAR(01);
	DECLARE @V_CTPEVEND			INTEGER;
	DECLARE @V_VRCOMISANT		DOUBLE PRECISION;
	DECLARE @V_VRPECOMIVEND		DOUBLE PRECISION;
	DECLARE @V_VRITRENTABIL		DOUBLE PRECISION;
	DECLARE @V_VRPREUNITPED		DOUBLE PRECISION;
	DECLARE @V_CDPRODUTO			VARCHAR(10);
	DECLARE @V_IDCTRLVENEXP		VARCHAR(1);
	DECLARE @V_VRPERCMARKUPIT	DOUBLE PRECISION;
	DECLARE @V_IDITPEDDIFER		VARCHAR(1);
	DECLARE @V_CDCLIENTE			VARCHAR(21);
	DECLARE @V_IDGERACOMISVEND	VARCHAR(1);
	DECLARE @C_VEND_CDVENDEDOR	VARCHAR(04);
	DECLARE @C_VEND_IDGERCOMIS	DOUBLE PRECISION;
BEGIN
	SET @V_VRPECOMIVEND = 0;
	SET @V_VRITRENTABIL = 0;

	SELECT @V_IDCTRLVENEXP = ISNULL(IDCTRLVENEXP, '')
	  FROM PAREXPEDICAO
	 WHERE (CDFILIAL = @P_CDFILIAL);

	SELECT @V_IDGERACOMISVEND = IDGERACOMISVEND
	  FROM VENDEDOR
	 WHERE (CDVENDEDOR = @P_CDVENDEDOR);

	IF (@V_IDGERACOMISVEND = 'S') BEGIN

		DECLARE C_VEND CURSOR FOR
			SELECT VV.CDVENDEDOR, VV.IDGERACOMISVEND
			  FROM VENDEDOR VV
			 WHERE (VV.CDVENDEDOR = @P_CDVENDEDOR)

			UNION ALL

			SELECT VA.CDVENDEDOR, VA.IDGERACOMISVEND
			  FROM VENDEDOR VV LEFT JOIN VENDEDOR VA ON (VV.CDVENDEDORAUX = VA.CDVENDEDOR)
			 WHERE (VV.CDVENDEDOR = @P_CDVENDEDOR)
				AND (VA.IDGERACOMISVEND = 'S')

			UNION ALL

			SELECT VS.CDVENDEDOR, VS.IDGERACOMISVEND
			  FROM VENDEDOR VV LEFT JOIN VENDEDOR VS ON (VV.CDVENDEDORSUP = VS.CDVENDEDOR)
			 WHERE (VV.CDVENDEDOR = @P_CDVENDEDOR)
				AND (VS.IDGERACOMISVEND = 'S')

			UNION ALL

			SELECT VG.CDVENDEDOR, VG.IDGERACOMISVEND
			  FROM VENDEDOR VV LEFT JOIN VENDEDOR VG ON (VV.CDVENDEDORGER = VG.CDVENDEDOR)
			 WHERE (VV.CDVENDEDOR = @P_CDVENDEDOR)
				AND (VG.IDGERACOMISVEND = 'S');

		OPEN C_VEND;

		FETCH NEXT FROM C_VEND INTO @C_VEND_CDVENDEDOR, @C_VEND_IDGERCOMIS;

		WHILE (@@FETCH_STATUS = 0) BEGIN
			IF (@V_IDCTRLVENEXP IN ('S', 'R')) BEGIN

				IF (@P_NRITEMPED IS NULL) BEGIN
					DECLARE C_ITEM CURSOR FOR
						SELECT NRITEMPED, IDSITITEMPED, VRPREUNITPED, CDPRODUTO, ISNULL(IDITPEDDIFER, 'N'), P.CDCLIENTE
						  FROM ITEMPEDIDO I, PEDIDO P
						 WHERE (I.CDFILIAL = @P_CDFILIAL) 
							AND (I.NRPEDIDO = @P_NRPEDIDO) 
							AND (I.CDFILIAL = P.CDFILIAL )
							AND (I.NRPEDIDO = P.NRPEDIDO )

					OPEN C_ITEM;

					FETCH NEXT FROM C_ITEM INTO @V_NRITEMPED, @V_IDSITITEMPED, @V_VRPREUNITPED, @V_CDPRODUTO, @V_IDITPEDDIFER, @V_CDCLIENTE;

					WHILE (@@FETCH_STATUS = 0) BEGIN

						IF (@V_IDITPEDDIFER IN ('N', 'S')) BEGIN
							SET @V_VRPECOMIVEND = TEKNISA.BUSCA_COMISSAO(@P_CDFILIAL, @C_VEND_CDVENDEDOR, @V_CDPRODUTO, @P_VRPREUNITPED, @V_IDITPEDDIFER, @V_CDCLIENTE, @V_IDCTRLVENEXP);
						END
						ELSE BEGIN
							SELECT @V_VRPECOMIVEND = ISNULL (VRCOMPROVEND,0)
							  FROM VENDEDOR
							 WHERE (CDVENDEDOR = @C_VEND_CDVENDEDOR);
						END;

						SELECT @V_CTPEVEND = COUNT(*)
						  FROM VENDITEMPED
						 WHERE (CDFILIAL	= @P_CDFILIAL )
							AND (NRPEDIDO	= @P_NRPEDIDO )
							AND (NRITEMPED  = @V_NRITEMPED)
							AND (CDVENDEDOR = @C_VEND_CDVENDEDOR);

						IF (@V_CTPEVEND = 0) BEGIN
							IF (@V_VRPECOMIVEND > 0) BEGIN
								INSERT INTO VENDITEMPED
									(CDFILIAL, NRPEDIDO, NRITEMPED,
									CDVENDEDOR, VRPECOMITPED)
								VALUES
									(@P_CDFILIAL, @P_NRPEDIDO, @V_NRITEMPED,
									@C_VEND_CDVENDEDOR, @V_VRPECOMIVEND);
							END;

							SET @V_VRCOMISANT = NULL;
							IF NOT (@P_OLDCDVENDE IS NULL) BEGIN
								SELECT @V_VRCOMISANT = VRPECOMITPED
								  FROM VENDITEMPED
								 WHERE (CDFILIAL	= @P_CDFILIAL  )
									AND (NRPEDIDO	= @P_NRPEDIDO  )
									AND (NRITEMPED  = @V_NRITEMPED )
									AND (CDVENDEDOR = @P_OLDCDVENDE);
							END;

							IF NOT (@P_OLDCDVENDE IS NULL) BEGIN
								DELETE FROM VENDITEMPED
								 WHERE (CDFILIAL	= @P_CDFILIAL  )
									AND (NRPEDIDO	= @P_NRPEDIDO  )
									AND (NRITEMPED  = @V_NRITEMPED )
									AND (CDVENDEDOR = @P_OLDCDVENDE);
							END;
						END
						ELSE BEGIN
							SELECT @V_VRCOMISANT = VRPECOMITPED
							  FROM VENDITEMPED
							 WHERE (CDFILIAL	= @P_CDFILIAL )
								AND (NRPEDIDO	= @P_NRPEDIDO )
								AND (NRITEMPED  = @V_NRITEMPED)
								AND (CDVENDEDOR = @C_VEND_CDVENDEDOR);

							UPDATE VENDITEMPED
								SET VRPECOMITPED = @V_VRPECOMIVEND
							 WHERE (CDFILIAL	 = @P_CDFILIAL )
								AND (NRPEDIDO	 = @P_NRPEDIDO )
								AND (NRITEMPED	= @V_NRITEMPED)
								AND (CDVENDEDOR  = @C_VEND_CDVENDEDOR);
						END;

						INSERT INTO HISITEMPED
							(CDFILIAL, IDSITATUHIST, NRITEMPED, IDSITANTHIST, IDALTHISTITE, DTHRHIST,
							 NRPEDIDO, CDOPEMANHIST,CDVENDORIG, VRCOMISSORIG,CDVENDATUAL,VRCOMISATUAL)
						VALUES
							(@P_CDFILIAL, @V_IDSITITEMPED, @V_NRITEMPED, @V_IDSITITEMPED, '3', DATEADD(ss, 2, GETDATE()),
							 @P_NRPEDIDO, @P_CDOPERADOR, @P_OLDCDVENDE, @V_VRCOMISANT, @C_VEND_CDVENDEDOR, @V_VRPECOMIVEND);


						FETCH NEXT FROM C_ITEM INTO @V_NRITEMPED, @V_IDSITITEMPED, @V_VRPREUNITPED, @V_CDPRODUTO;
					END;
					CLOSE C_ITEM;
					DEALLOCATE C_ITEM;
				END
				ELSE BEGIN
				  /* raise_application_error(-20504,'1000', True); */

					SELECT @V_IDITPEDDIFER = ISNULL(IDITPEDDIFER, 'N'), @V_CDCLIENTE = P.CDCLIENTE
					  FROM ITEMPEDIDO I, PEDIDO P
					 WHERE (P.CDFILIAL  = @P_CDFILIAL ) 
						AND (P.NRPEDIDO  = @P_NRPEDIDO ) 
						AND (I.NRITEMPED = @P_NRITEMPED) 
						AND (P.CDFILIAL  = I.CDFILIAL  )
						AND (P.NRPEDIDO  = I.NRPEDIDO  );

					IF (@V_IDITPEDDIFER IN ('N', 'S')) BEGIN
						SET @V_VRPECOMIVEND = TEKNISA.BUSCA_COMISSAO(@P_CDFILIAL, @C_VEND_CDVENDEDOR, @P_CDPRODUTO, @P_VRPREUNITPED, @V_IDITPEDDIFER, @V_CDCLIENTE, @V_IDCTRLVENEXP);
					END
					ELSE BEGIN
						SELECT @V_VRPECOMIVEND = ISNULL(VRCOMPROVEND,0)
						  FROM VENDEDOR
						 WHERE (CDVENDEDOR = @C_VEND_CDVENDEDOR);
					END;

					SELECT @V_CTPEVEND = COUNT(*)
					  FROM VENDITEMPED
					 WHERE (CDFILIAL	= @P_CDFILIAL )
						AND (NRPEDIDO	= @P_NRPEDIDO )
						AND (NRITEMPED  = @P_NRITEMPED)
						AND (CDVENDEDOR = @C_VEND_CDVENDEDOR);

					IF (@V_CTPEVEND = 0) BEGIN


						IF (@V_VRPECOMIVEND > 0) BEGIN
							INSERT INTO VENDITEMPED
							  (CDFILIAL, NRPEDIDO, NRITEMPED,
								CDVENDEDOR, VRPECOMITPED)
							VALUES
							  (@P_CDFILIAL, @P_NRPEDIDO, @P_NRITEMPED,
								@C_VEND_CDVENDEDOR, @V_VRPECOMIVEND);
						END

						SET @V_VRCOMISANT = NULL;
						IF NOT (@P_OLDCDVENDE IS NULL) BEGIN
							SELECT @V_VRCOMISANT = VRPECOMITPED
							  FROM VENDITEMPED
							 WHERE (CDFILIAL	= @P_CDFILIAL  )
								AND (NRPEDIDO	= @P_NRPEDIDO  )
								AND (NRITEMPED  = @P_NRITEMPED )
								AND (CDVENDEDOR = @P_OLDCDVENDE);
						END;

						IF NOT (@P_OLDCDVENDE IS NULL) BEGIN
							DELETE FROM VENDITEMPED
							WHERE (CDFILIAL	= @P_CDFILIAL  )
							  AND (NRPEDIDO	= @P_NRPEDIDO  )
							  AND (NRITEMPED  = @P_NRITEMPED )
							  AND (CDVENDEDOR = @P_OLDCDVENDE);
						END;

					END
					ELSE BEGIN
						SELECT @V_VRCOMISANT = VRPECOMITPED
						  FROM VENDITEMPED
						 WHERE (CDFILIAL	= @P_CDFILIAL )
							AND (NRPEDIDO	= @P_NRPEDIDO )
							AND (NRITEMPED  = @P_NRITEMPED)
							AND (CDVENDEDOR = @C_VEND_CDVENDEDOR);

						UPDATE VENDITEMPED
							SET VRPECOMITPED = @V_VRPECOMIVEND
						 WHERE (CDFILIAL	 = @P_CDFILIAL )
							AND (NRPEDIDO	 = @P_NRPEDIDO )
							AND (NRITEMPED	= @P_NRITEMPED)
							AND (CDVENDEDOR  = @C_VEND_CDVENDEDOR);
					END;

					INSERT INTO HISITEMPED
						(CDFILIAL, IDSITATUHIST, NRITEMPED, IDSITANTHIST, IDALTHISTITE, DTHRHIST,
						 NRPEDIDO, CDOPEMANHIST,CDVENDORIG, VRCOMISSORIG,CDVENDATUAL,VRCOMISATUAL)
					VALUES
						(@P_CDFILIAL, @P_IDSITITEMPED, @P_NRITEMPED, @P_IDSITITEMPED, '3', DATEADD(ss, 2, GETDATE()),
						 @P_NRPEDIDO, @P_CDOPERADOR, @C_VEND_CDVENDEDOR, @V_VRCOMISANT, @C_VEND_CDVENDEDOR, @V_VRPECOMIVEND);

				END;
			END
			ELSE IF (@V_IDCTRLVENEXP = 'I') BEGIN

				IF (@P_NRITEMPED IS NULL) BEGIN
					DECLARE C_ITEM CURSOR FOR
						SELECT NRITEMPED, IDSITITEMPED, VRPREUNITPED, CDPRODUTO, ISNULL(IDITPEDDIFER, 'N'), P.CDCLIENTE, I.VRITRENTABIL
						  FROM ITEMPEDIDO I, PEDIDO P
						 WHERE (I.CDFILIAL = @P_CDFILIAL) 
							AND (I.NRPEDIDO = @P_NRPEDIDO) 
							AND (I.CDFILIAL = P.CDFILIAL )
							AND (I.NRPEDIDO = P.NRPEDIDO )

					OPEN C_ITEM;

					FETCH NEXT FROM C_ITEM INTO @V_NRITEMPED, @V_IDSITITEMPED, @V_VRPREUNITPED, @V_CDPRODUTO, @V_IDITPEDDIFER, @V_CDCLIENTE, @V_VRITRENTABIL;

					WHILE (@@FETCH_STATUS = 0) BEGIN

						IF (@V_IDITPEDDIFER IN ('N', 'S')) BEGIN
							SET @V_VRPECOMIVEND = TEKNISA.BUSCA_COMISSAO(@P_CDFILIAL, @C_VEND_CDVENDEDOR, @V_CDPRODUTO, @V_VRITRENTABIL, @V_IDITPEDDIFER, @V_CDCLIENTE, @V_IDCTRLVENEXP);
						END
						ELSE BEGIN
							SELECT @V_VRPECOMIVEND = ISNULL (VRCOMPROVEND,0)
							  FROM VENDEDOR
							 WHERE (CDVENDEDOR = @C_VEND_CDVENDEDOR);
						END;

						SELECT @V_CTPEVEND = COUNT(*)
						  FROM VENDITEMPED
						 WHERE (CDFILIAL	= @P_CDFILIAL )
							AND (NRPEDIDO	= @P_NRPEDIDO )
							AND (NRITEMPED  = @V_NRITEMPED)
							AND (CDVENDEDOR = @C_VEND_CDVENDEDOR);

						IF (@V_CTPEVEND = 0) BEGIN
							IF (@V_VRPECOMIVEND > 0) BEGIN
								INSERT INTO VENDITEMPED
									(CDFILIAL, NRPEDIDO, NRITEMPED,
									 CDVENDEDOR, VRPECOMITPED)
								VALUES
									(@P_CDFILIAL, @P_NRPEDIDO, @V_NRITEMPED,
									 @C_VEND_CDVENDEDOR, @V_VRPECOMIVEND);
							END;

							SET @V_VRCOMISANT = NULL;
							IF NOT (@P_OLDCDVENDE IS NULL) BEGIN
								SELECT @V_VRCOMISANT = VRPECOMITPED
								  FROM VENDITEMPED
								 WHERE (CDFILIAL	= @P_CDFILIAL  )
									AND (NRPEDIDO	= @P_NRPEDIDO  )
									AND (NRITEMPED  = @V_NRITEMPED )
									AND (CDVENDEDOR = @P_OLDCDVENDE);
							END;

							IF NOT (@P_OLDCDVENDE IS NULL) BEGIN
								DELETE FROM VENDITEMPED
								 WHERE (CDFILIAL	= @P_CDFILIAL  )
									AND (NRPEDIDO	= @P_NRPEDIDO  )
									AND (NRITEMPED  = @V_NRITEMPED )
									AND (CDVENDEDOR = @P_OLDCDVENDE);
							END;
						END
						ELSE BEGIN
							SELECT @V_VRCOMISANT = VRPECOMITPED
							  FROM VENDITEMPED
							 WHERE (CDFILIAL	= @P_CDFILIAL )
								AND (NRPEDIDO	= @P_NRPEDIDO )
								AND (NRITEMPED  = @V_NRITEMPED)
								AND (CDVENDEDOR = @C_VEND_CDVENDEDOR);

							UPDATE VENDITEMPED
								SET VRPECOMITPED = @V_VRPECOMIVEND
							 WHERE (CDFILIAL	 = @P_CDFILIAL )
								AND (NRPEDIDO	 = @P_NRPEDIDO )
								AND (NRITEMPED	= @V_NRITEMPED)
								AND (CDVENDEDOR  = @C_VEND_CDVENDEDOR);
						END;

						INSERT INTO HISITEMPED
							(CDFILIAL, IDSITATUHIST, NRITEMPED, IDSITANTHIST, IDALTHISTITE, DTHRHIST,
							 NRPEDIDO, CDOPEMANHIST,CDVENDORIG, VRCOMISSORIG,CDVENDATUAL,VRCOMISATUAL)
						VALUES
							(@P_CDFILIAL, @V_IDSITITEMPED, @V_NRITEMPED, @V_IDSITITEMPED, '3', DATEADD(ss, 2, GETDATE()),
							 @P_NRPEDIDO, @P_CDOPERADOR, @P_OLDCDVENDE, @V_VRCOMISANT, @C_VEND_CDVENDEDOR, @V_VRPECOMIVEND);


						FETCH NEXT FROM C_ITEM INTO @V_NRITEMPED, @V_IDSITITEMPED, @V_VRPREUNITPED, @V_CDPRODUTO, @V_IDITPEDDIFER, @V_CDCLIENTE, @V_VRITRENTABIL;
					END;
					CLOSE C_ITEM;
					DEALLOCATE C_ITEM;
				END
				ELSE BEGIN
				  /* raise_application_error(-20504,'1000', True); */

					SELECT @V_IDITPEDDIFER = ISNULL(IDITPEDDIFER, 'N'), @V_CDCLIENTE = P.CDCLIENTE, @V_VRITRENTABIL = I.VRITRENTABIL
					  FROM ITEMPEDIDO I, PEDIDO P
					 WHERE (P.CDFILIAL  = @P_CDFILIAL ) 
						AND (P.NRPEDIDO  = @P_NRPEDIDO ) 
						AND (I.NRITEMPED = @P_NRITEMPED) 
						AND (P.CDFILIAL  = I.CDFILIAL  )
						AND (P.NRPEDIDO  = I.NRPEDIDO  );

					IF (@V_IDITPEDDIFER IN ('N', 'S')) BEGIN
						SET @V_VRPECOMIVEND = TEKNISA.BUSCA_COMISSAO(@P_CDFILIAL, @C_VEND_CDVENDEDOR, @P_CDPRODUTO, @V_VRITRENTABIL, @V_IDITPEDDIFER, @V_CDCLIENTE, @V_IDCTRLVENEXP);
					END
					ELSE BEGIN
						SELECT @V_VRPECOMIVEND = ISNULL(VRCOMPROVEND,0)
						  FROM VENDEDOR
						 WHERE (CDVENDEDOR = @C_VEND_CDVENDEDOR);
					END;

					SELECT @V_CTPEVEND = COUNT(*)
					  FROM VENDITEMPED
					 WHERE (CDFILIAL	= @P_CDFILIAL )
						AND (NRPEDIDO	= @P_NRPEDIDO )
						AND (NRITEMPED  = @P_NRITEMPED)
						AND (CDVENDEDOR = @C_VEND_CDVENDEDOR);

					IF (@V_CTPEVEND = 0) BEGIN


						IF (@V_VRPECOMIVEND > 0) BEGIN
							INSERT INTO VENDITEMPED
								(CDFILIAL, NRPEDIDO, NRITEMPED,
								 CDVENDEDOR, VRPECOMITPED)
							VALUES
								(@P_CDFILIAL, @P_NRPEDIDO, @P_NRITEMPED,
								 @C_VEND_CDVENDEDOR, @V_VRPECOMIVEND);
						END

						SET @V_VRCOMISANT = NULL;
						IF NOT (@P_OLDCDVENDE IS NULL) BEGIN
							SELECT @V_VRCOMISANT = VRPECOMITPED
							  FROM VENDITEMPED
							 WHERE (CDFILIAL	= @P_CDFILIAL  )
								AND (NRPEDIDO	= @P_NRPEDIDO  )
								AND (NRITEMPED  = @P_NRITEMPED )
								AND (CDVENDEDOR = @P_OLDCDVENDE);
						END;

						IF NOT (@P_OLDCDVENDE IS NULL) BEGIN
							DELETE FROM VENDITEMPED
							 WHERE (CDFILIAL	= @P_CDFILIAL  )
								AND (NRPEDIDO	= @P_NRPEDIDO  )
								AND (NRITEMPED  = @P_NRITEMPED )
								AND (CDVENDEDOR = @P_OLDCDVENDE);
						END;

					END
					ELSE BEGIN
						SELECT @V_VRCOMISANT = VRPECOMITPED
						 FROM VENDITEMPED
						 WHERE (CDFILIAL	= @P_CDFILIAL )
							AND (NRPEDIDO	= @P_NRPEDIDO )
							AND (NRITEMPED  = @P_NRITEMPED)
							AND (CDVENDEDOR = @C_VEND_CDVENDEDOR);

						UPDATE VENDITEMPED
							SET VRPECOMITPED = @V_VRPECOMIVEND
						 WHERE (CDFILIAL	 = @P_CDFILIAL )
							AND (NRPEDIDO	 = @P_NRPEDIDO )
							AND (NRITEMPED	= @P_NRITEMPED)
							AND (CDVENDEDOR  = @C_VEND_CDVENDEDOR);
					END;

					INSERT INTO HISITEMPED
						(CDFILIAL, IDSITATUHIST, NRITEMPED, IDSITANTHIST, IDALTHISTITE, DTHRHIST,
						 NRPEDIDO, CDOPEMANHIST,CDVENDORIG, VRCOMISSORIG,CDVENDATUAL,VRCOMISATUAL)
					VALUES
						(@P_CDFILIAL, @P_IDSITITEMPED, @P_NRITEMPED, @P_IDSITITEMPED, '3', DATEADD(ss, 2, GETDATE()),
						 @P_NRPEDIDO, @P_CDOPERADOR, @C_VEND_CDVENDEDOR, @V_VRCOMISANT, @C_VEND_CDVENDEDOR, @V_VRPECOMIVEND);

				END;
			END
			ELSE BEGIN
				IF (@V_IDCTRLVENEXP = 'M') BEGIN

					DECLARE C_REG CURSOR FOR
						SELECT NRITEMPED, IDSITITEMPED, VRPERCMARKUPIT
						  FROM ITEMPEDIDO
						 WHERE (CDFILIAL  = @P_CDFILIAL) 
							AND (NRPEDIDO  = @P_NRPEDIDO)
							AND (NRITEMPED = ISNULL(@P_NRITEMPED, NRITEMPED));

					OPEN C_REG;

					FETCH NEXT FROM C_REG INTO @V_NRITEMPED, @V_IDSITITEMPED, @V_VRPERCMARKUPIT;

					WHILE (@@FETCH_STATUS = 0) BEGIN

						SET @V_VRCOMISANT = NULL;

						IF (@V_VRPERCMARKUPIT > 0) BEGIN

							SELECT @V_CTPEVEND = COUNT(*)
							 FROM VENDEDOR V, ITCOMMARKUP I
							 WHERE (V.CDVENDEDOR = @C_VEND_CDVENDEDOR)
								AND (V.CDMARKUP	= I.CDMARKUP )
								AND (I.CDFILIAL	= @P_CDFILIAL)
								AND (@V_VRPERCMARKUPIT BETWEEN VRINICACRES AND VRFINACRES);

							IF (@V_CTPEVEND = 0) BEGIN
								SET @V_VRPECOMIVEND = 0;
							END
							ELSE BEGIN

								SELECT @V_VRPECOMIVEND = ISNULL(MAX(I.VRPECOMISSAO),0)
								  FROM VENDEDOR V, ITCOMMARKUP I
								 WHERE (V.CDVENDEDOR = @C_VEND_CDVENDEDOR)
									AND (V.CDMARKUP	= I.CDMARKUP )
									AND (I.CDFILIAL	= @P_CDFILIAL)
									AND (@V_VRPERCMARKUPIT BETWEEN VRINICACRES AND VRFINACRES);
							END;

							IF (@V_VRPECOMIVEND > 0) BEGIN

								SELECT @V_VRCOMISANT = ISNULL(VRPECOMITPED,0)
								  FROM VENDITEMPED
								 WHERE (CDFILIAL	= @P_CDFILIAL )
									AND (NRPEDIDO	= @P_NRPEDIDO )
									AND (NRITEMPED  = @V_NRITEMPED)
									AND (CDVENDEDOR = ISNULL(@P_OLDCDVENDE, @C_VEND_CDVENDEDOR));


		/* PARA DELETAR A COMISSAO ANTERIOR SE O VALOR DO ITEM FOR TROCADO E O VENDEDOR FOR O MEMSO*/
								DELETE FROM VENDITEMPED
								 WHERE (CDFILIAL	= @P_CDFILIAL )
									AND (NRPEDIDO	= @P_NRPEDIDO )
									AND (NRITEMPED  = @V_NRITEMPED)
									AND (CDVENDEDOR = @C_VEND_CDVENDEDOR);

								INSERT INTO VENDITEMPED (CDFILIAL, NRPEDIDO, NRITEMPED, CDVENDEDOR, VRPECOMITPED)
										 VALUES (@P_CDFILIAL, @P_NRPEDIDO, @V_NRITEMPED, @C_VEND_CDVENDEDOR, @V_VRPECOMIVEND);
							END;

							IF (@P_OLDCDVENDE IS NOT NULL) BEGIN
								SELECT @V_VRCOMISANT  = VRPECOMITPED
								  FROM VENDITEMPED
								 WHERE (CDFILIAL	= @P_CDFILIAL  )
									AND (NRPEDIDO	= @P_NRPEDIDO  )
									AND (NRITEMPED  = @V_NRITEMPED )
									AND (CDVENDEDOR = @P_OLDCDVENDE);
							END;

							INSERT INTO HISITEMPED
								  (CDFILIAL, IDSITATUHIST, NRITEMPED, IDSITANTHIST, IDALTHISTITE, DTHRHIST,
									NRPEDIDO, CDOPEMANHIST,CDVENDORIG, VRCOMISSORIG,CDVENDATUAL,VRCOMISATUAL)
							 VALUES
								  (@P_CDFILIAL, @V_IDSITITEMPED, @V_NRITEMPED, @V_IDSITITEMPED, '3', GETDATE() + 2/64/64/24,
									@P_NRPEDIDO, @P_CDOPERADOR, @P_OLDCDVENDE, @V_VRCOMISANT, @C_VEND_CDVENDEDOR, @V_VRPECOMIVEND);

							IF (@P_OLDCDVENDE IS NOT NULL) BEGIN
								DELETE FROM VENDITEMPED
								 WHERE (CDFILIAL	= @P_CDFILIAL  )
									AND (NRPEDIDO	= @P_NRPEDIDO  )
									AND (NRITEMPED  = @V_NRITEMPED )
									AND (CDVENDEDOR = @P_OLDCDVENDE);
							END;

						END
						ELSE BEGIN
							DELETE FROM VENDITEMPED
							 WHERE (CDFILIAL  = @P_CDFILIAL )
								AND (NRPEDIDO  = @P_NRPEDIDO )
								AND (NRITEMPED = @V_NRITEMPED);
						END;
						FETCH NEXT FROM C_REG INTO @V_NRITEMPED, @V_IDSITITEMPED, @V_VRPERCMARKUPIT;
					END;
					CLOSE C_REG;
					DEALLOCATE C_REG;
				END;
			END;
		END;
		CLOSE C_VEND;
		DEALLOCATE C_VEND;
	END;
END;
GO

-- VERSAO --
EXEC ATUVEROBJ 'GERA_COMISSAOVEND','6.35.025';
GO
