SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

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

CREATE PROCEDURE WGP_Impuestos_GetInfo
	@id_impu  INT,
	@vEliminado BIT
AS
BEGIN

	SELECT id_impu, LTRIM(RTRIM(desc_impu))desc_impu, porc_impu, afecta_cange, nivel, apli_sup, eliminado
	INTO #TempImpuestos
	FROM SICLAIMP  

	IF @id_impu  > 0 
	BEGIN 
	   DELETE #TempImpuestos WHERE id_impu  <> @id_impu 
	END
	
		IF @vEliminado <> 1
	BEGIN 
	   DELETE #TempImpuestos WHERE eliminado  <> 0
	END

	SELECT * 
	FROM   #TempImpuestos
	ORDER BY desc_impu
	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_Impuestos_RepInfo') )
DROP PROCEDURE WGP_Impuestos_RepInfo
GO

CREATE PROCEDURE WGP_Impuestos_RepInfo
	@vEliminado BIT
AS
BEGIN

	SELECT id_impu, desc_impu, porc_impu, afecta_cange, nivel, apli_sup, eliminado 
	FROM SICLAIMP

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

CREATE PROCEDURE WGP_Impuestos_PutInfo
	@id_impu INT,
	@desc_impu VARCHAR(30),
	@porc_impu NUMERIC(5,2),
	@afecta_cange BIT,
	@nivel INT,
	@apli_sup BIT,
	@eliminado BIT
AS
BEGIN
	DECLARE @MensajeError VARCHAR(255) = ''

	IF @eliminado = 0
	BEGIN
	
		IF (@desc_impu = '')
		BEGIN
			SET @MensajeError = 'ERR_GEN_FAL_DAT'
			--Falta datos, no deje campos vacios 
			GOTO ERROR
		END
	
		IF EXISTS(select desc_impu from SICLAIMP where desc_impu=@desc_impu and id_impu!=@id_impu)
			BEGIN
				SET @MensajeError = 'ERR_GEN_REG_DUP'
				--Registro Duplicado 
				GOTO ERROR
		END
		
		IF (@porc_impu >= 100)
			BEGIN
				SET @MensajeError = 'ERR_GEN_POR_MAY'
				--Por Impu no puede ser igual o mayor de 100
				GOTO ERROR
		END

		IF @id_impu = 0
		BEGIN
		
			-- Crear select * from SICLAIMP
		
			INSERT INTO SICLAIMP
			(
				desc_impu, porc_impu, afecta_cange, nivel, apli_sup, eliminado
			)
			VALUES
			(
				LTRIM(RTRIM(@desc_impu)), @porc_impu, @afecta_cange, @nivel, @apli_sup, 0
			)

			SET @id_impu = @@IDENTITY
		
		END
		ELSE
		BEGIN
		
		SET @eliminado = 1; 
			-- Editar 
			
			IF EXISTS(select eliminado from SICLAIMP where id_impu=@id_impu and eliminado=1)
			BEGIN
				SET @MensajeError = 'ERR_GEN_REG_DES'
				GOTO ERROR
			END
		
			UPDATE SICLAIMP
			SET --desc_impu = @desc_impu,
			    --porc_impu = @porc_impu,
			    afecta_cange = @afecta_cange,
			    nivel = @nivel,
			    apli_sup = @apli_sup
			WHERE id_impu = @id_impu
			
		
		END

	END
	ELSE
	BEGIN

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

	END

	SELECT @id_impu AS Id
	
	RETURN

	ERROR:
		RAISERROR (@MensajeError, 16, 1)

END
GO