SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'WGP_Monedas_GetInfo') )
DROP PROCEDURE WGP_Monedas_GetInfo
GO

CREATE PROCEDURE WGP_Monedas_GetInfo
	@id_mone  INT,
	@vEliminado BIT
AS
BEGIN

	SELECT id_mone, cod_mone, LTRIM(RTRIM(desc_mone))desc_mone, tasa, comision, decimales, convertible, facturable, fecha_ult, refer_mone, eliminado
	INTO #TempMonedas
	FROM SICLAMON 

	IF @id_mone  > 0 
	BEGIN 
	   DELETE #TempMonedas WHERE id_mone  <> @id_mone 
	END
		
		IF @vEliminado <> 1
	BEGIN 
	   DELETE #TempMonedas WHERE eliminado  <> 0
	END

	SELECT * 
	FROM   #TempMonedas
	ORDER BY desc_mone
	RETURN

END
GO

---------------------------------------------------------------------------------
---------------------------------------------------------------------------------

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'WGP_Monedas_RepInfo') )
DROP PROCEDURE WGP_Monedas_RepInfo
GO

CREATE PROCEDURE WGP_Monedas_RepInfo
	@vEliminado BIT
AS
BEGIN

	SELECT id_mone, cod_mone, desc_mone, tasa, comision, decimales, fecha_ult, convertible, facturable, refer_mone, eliminado
	INTO   #TempMonedasRep
	FROM SICLAMON 
	
	IF @vEliminado <> 1
	BEGIN 
	   DELETE #TempMonedasRep WHERE eliminado  <> 0
	END

	SELECT * 
	FROM   #TempMonedasRep
	ORDER BY desc_mone
	RETURN

	RETURN

END
GO

---------------------------------------------------------------------------------
---------------------------------------------------------------------------------

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'WGP_Monedas_PutInfo') )
DROP PROCEDURE WGP_Monedas_PutInfo
GO

CREATE PROCEDURE WGP_Monedas_PutInfo
	@id_mone INT,
	@cod_mone VARCHAR(3),
	@desc_mone VARCHAR(18),
	@tasa NUMERIC(13,8),
	@comision NUMERIC(5,2),
	@decimales INT,
	@convertible BIT,
	@facturable BIT,
	@fecha_ult DATETIME,
	@refer_mone BIT,
	@eliminado BIT
AS

SET @fecha_ult = GETDATE();

BEGIN
	DECLARE @MensajeError VARCHAR(255) = ''

	IF @eliminado = 0
	BEGIN
	
		IF (@cod_mone = '' OR @desc_mone = '')
		BEGIN
			SET @MensajeError = 'ERR_GEN_FAL_DAT'
			--Falta datos, no deje campos vacios 
			GOTO ERROR
		END
	
		IF EXISTS(select desc_mone from SICLAMON where desc_mone=@desc_mone and id_mone!=@id_mone)
			BEGIN
				SET @MensajeError = 'ERR_GEN_REG_DUP'
				GOTO ERROR
			END

		IF @id_mone = 0
		BEGIN
		
			-- Crear select * from SICLAMON
		
			INSERT INTO SICLAMON
			(
				cod_mone, desc_mone, tasa, comision, decimales, fecha_ult, convertible, facturable, refer_mone, eliminado
			)
			VALUES
			(
				@cod_mone, @desc_mone, @tasa, @comision, @decimales, @fecha_ult, @convertible, @facturable, @refer_mone, 0
			)

			SET @id_mone = @@IDENTITY
		
		END
		ELSE
		BEGIN
		
			-- Editar select * from SICLAMON
			
			IF EXISTS(select eliminado from SICLAMON where id_mone=@id_mone and eliminado=1)
			BEGIN
				SET @MensajeError = 'ERR_GEN_REG_DES'
				GOTO ERROR
			END
			
			IF EXISTS(select cod_mone from SICLAMON where id_mone=@id_mone and cod_mone != @cod_mone or id_mone = @id_mone and facturable != @facturable)
			BEGIN
				SET @MensajeError = 'ERR_MON_REG_MOD'
				GOTO ERROR
			END
				UPDATE SICLAMON
				SET --cod_mone = @cod_mone,
					desc_mone = @desc_mone,
					tasa = @tasa,
					comision = @comision,
					decimales = @decimales,
					convertible = @convertible,
					--facturable = @facturable,
					fecha_ult = @fecha_ult,
					refer_mone = @refer_mone,
					eliminado = 0
				WHERE id_mone = @id_mone
		END

	END
	ELSE
	BEGIN
		
		IF EXISTS (SELECT id_mone FROM SICLAMON WHERE id_mone = @id_mone AND eliminado = 1)
		BEGIN
			UPDATE SICLAMON 
			SET    eliminado = 0
			WHERE  id_mone = @id_mone
		END
		ELSE
		BEGIN
			--DELETE FROM SICLAMON WHERE id_mone = @id_mone
			UPDATE SICLAMON 
			SET    eliminado = 1
			WHERE  id_mone = @id_mone
		END

	END

	SELECT @id_mone AS Id
	
	RETURN

	ERROR:
		RAISERROR (@MensajeError, 16, 1)

END
GO