SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

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

CREATE PROCEDURE WMAN_Terminales_GetInfo
	@id_terminal INT,
	@vEliminado  BIT
AS
BEGIN

	SELECT id_terminal, LTRIM(RTRIM(nombre)) nombre, activa
	INTO   #TempTerminales
	FROM   Terminales 

	IF @id_terminal  > 0 
	BEGIN 
	   DELETE #TempTerminales WHERE id_terminal  <> @id_terminal 
	END
	
	IF @vEliminado <> 1
	BEGIN 
	   DELETE #TempTerminales WHERE activa  <> 1 
	END

	SELECT * 
	FROM   #TempTerminales
	ORDER BY nombre
	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'WMAN_Terminales_RepInfo') )
DROP PROCEDURE WMAN_Terminales_RepInfo
GO

CREATE PROCEDURE WMAN_Terminales_RepInfo
	@vEliminado BIT
AS
BEGIN

	SELECT id_terminal, LTRIM(RTRIM(nombre))nombre, activa 
	INTO   #TempTerminalesRep
	FROM   Terminales 
	
	IF @vEliminado <> 1
	BEGIN 
	   DELETE #TempTerminalesRep WHERE activa  <> 1 
	END

	SELECT * 
	FROM   #TempTerminalesRep
	ORDER BY nombre

	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'WMAN_Terminales_PutInfo') )
DROP PROCEDURE WMAN_Terminales_PutInfo
GO

CREATE PROCEDURE WMAN_Terminales_PutInfo
	@id_terminal INT,
	@nombre VARCHAR(100),
	@eliminado BIT
AS

SET @nombre=LTRIM(RTRIM(@nombre));

BEGIN
	DECLARE @MensajeError VARCHAR(255) = ''

	IF @eliminado = 0
	BEGIN
	
	IF (@nombre = '')
		BEGIN
			SET @MensajeError = 'ERR_GEN_FAL_DAT'
			--Falta datos, no deje campos vacios 
			GOTO ERROR
		END
	
			IF EXISTS (SELECT nombre FROM Terminales WHERE nombre = @nombre) 
			BEGIN
				SET @MensajeError = 'ERR_GEN_REG_DUP'
				GOTO ERROR
			END

		IF @id_terminal = 0
		BEGIN
		
			declare @valor_llave INT
			SET @valor_llave= IDENT_CURRENT ('Terminales');
			
			UPDATE syswinFOR
			SET valor_llave = @valor_llave+1
			WHERE desc_tabla = 'Terminales'
			
			declare @p3 float
			Set @p3 = @valor_llave+1
					
			-- Crear 
			
			SET IDENTITY_INSERT Terminales ON
		
			INSERT INTO Terminales
			(
				id_terminal, nombre, activa
			)
			VALUES
			(
				@p3, @nombre, 1
			)
			
			SET IDENTITY_INSERT Terminales OFF

			SET @id_terminal = @p3
		
		END
		ELSE
		BEGIN
		
			-- Editar
		
			UPDATE Terminales
			SET nombre = @nombre
			WHERE id_terminal = @id_terminal
			
		END

	END
	ELSE
	BEGIN
		IF EXISTS (SELECT id_terminal FROM Terminales WHERE id_terminal = @id_terminal AND activa = 1)
		BEGIN
			UPDATE Terminales 
			SET    Activa = 0
			WHERE  id_terminal = @id_terminal
			--DELETE FROM Terminales WHERE id_terminal = @id_terminal
		END
		ELSE
		BEGIN
			UPDATE Terminales 
			SET    Activa = 0
			WHERE  id_terminal = @id_terminal
		END
	END

	SELECT @id_terminal AS Id
	
	RETURN

	ERROR:
		RAISERROR (@MensajeError, 16, 1)

END
GO