DROP PROCEDURE OP_CTRLFUELPRICEUpdProc
GO
CREATE PROCEDURE OP_CTRLFUELPRICEUpdProc
(	
	@SITEID		UD_IDSITE,
	@DATEPROCE	UD_FECHA,
	@INVTIDSKU	UD_IDCORPORATIVO,
	@SLS_PRICE	UD_PRECIO,
	@SLS_PRICENEW	UD_PRECIO,
	@QTYSALES	UD_STOCK,
	@USERID		UD_USERID
)
AS
BEGIN
	BEGIN TRAN

	DECLARE @CODMONEDABASE	UD_CURYID
	DECLARE @CODCATFUEL	CHAR(2)
	SET @CODMONEDABASE=(SELECT CURYORIG FROM SETUP)
	SET @CODCATFUEL=(SELECT CATEGORYFUEL FROM OP_SETUP)
	
	DELETE FROM OP_CTRLFUELPRICE
	WHERE SITEID=@SITEID AND DATEPROCE=@DATEPROCE AND INVTIDSKU=@INVTIDSKU AND SLS_PRICE=@SLS_PRICE 

	IF(@@ERROR!=0 OR @@ROWCOUNT=0)
	BEGIN
		ROLLBACK
		RAISERROR 20000 'Error Eliminando Registro'
		RETURN(1)
	END 

	INSERT 	INTO OP_CTRLFUELPRICE(SITEID,DATEPROCE,INVTIDSKU,SLS_PRICE,CURYID,QTYSALES,STATUS,USERID)
	VALUES (@SITEID,@DATEPROCE,@INVTIDSKU,@SLS_PRICENEW,@CODMONEDABASE,@QTYSALES,0,@USERID)
	
	IF(@@ERROR!=0 OR @@ROWCOUNT=0)
	BEGIN
		ROLLBACK
		RAISERROR 20000 'Error Insentando Registro'
		RETURN(1)
	END 

	COMMIT TRAN
	RETURN(0)
END 

SELECT * FROM OP_CTRLFUELPRICE