IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_gen_config]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_gen_config]
GO
CREATE PROCEDURE [reg_gen_config]
	@p_id INT
AS
BEGIN
	SELECT 	[id] AS [id],
		[uid] AS [uid],
		[eid] AS [eid],
		[id_usuario_modifico] AS [id_usuario_modifico],
		[id_usuario_creo] AS [id_usuario_creo],
		[fechaModificacion] AS [fechaModificacion],
		[fechaCreacion] AS [fechaCreacion],
		[active] AS [active],
		[config] AS [config],
		[config_codigo] AS [config_codigo],
		[modulo] AS [modulo],
		[valor] AS [valor],
		[descripcion] AS [descripcion],
		[idObjeto] AS [idObjeto]
	FROM [gen_config]
	WHERE	(id = @p_id)

END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[view_gen_config]')) DROP VIEW [dbo].[view_gen_config]
GO
CREATE  VIEW [view_gen_config] AS
SELECT 	[gen_config].[id] AS [id],
		[gen_config].[uid] AS [uid],
		[gen_config].[eid] AS [eid],
		[gen_config].[id_usuario_modifico] AS [id_usuario_modifico],
		[gen_config].[id_usuario_creo] AS [id_usuario_creo],
		[gen_config].[fechaModificacion] AS [fechaModificacion],
		[gen_config].[fechaCreacion] AS [fechaCreacion],
		[gen_config].[active] AS [active],
		[gen_config].[config] AS [config],
		[gen_config].[config_codigo] AS [config_codigo],
		[gen_config].[modulo] AS [modulo],
		[gen_config].[valor] AS [valor],
		[gen_config].[descripcion] AS [descripcion],
		[gen_config].[idObjeto] AS [idObjeto],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [gen_config]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[gen_config].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[gen_config].[id_usuario_creo]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [gen_config].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [gen_config].active=1
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sel_gen_configesXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_configesXusuario_modifico]
GO
CREATE PROCEDURE [sel_gen_configesXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_gen_config]
	WHERE ([id_usuario_modifico]= @p_id_usuario_modifico)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sel_gen_configesXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_configesXusuario_creo]
GO
CREATE PROCEDURE [sel_gen_configesXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_gen_config]
	WHERE ([id_usuario_creo]= @p_id_usuario_creo)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sel_gen_configes]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_configes]
GO
CREATE PROCEDURE [sel_gen_configes]
	
	@p_filtro as varchar (8000) = '1=1',
	@p_orden as varchar (8000) = 'id',
	@p_campos as varchar (8000) = '*' AS
BEGIN
exec ('SELECT '+ @p_campos +
' FROM [view_gen_config]
WHERE (' + @p_filtro + ') ORDER BY ' + @p_orden)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[lis_gen_configes]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_gen_configes]
GO
CREATE PROCEDURE [lis_gen_configes]
	AS
BEGIN
	SELECT id AS id,
[config] AS [gen_config]
	FROM [gen_config]
	WHERE active = 1
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[upd_gen_config]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_gen_config]
GO
CREATE PROCEDURE [upd_gen_config]
	@p_id int = null,
		@p_uid varchar(500) = null,
		@p_eid varchar(50) = null,
		@p_id_usuario_modifico int = null,
		@p_id_usuario_creo int = null,
		@p_fechaCreacion smalldatetime = null,
		@p_config varchar(300) = null,
		@p_config_codigo varchar(100) = null,
		@p_modulo varchar(100) = null,
		@p_valor varchar(1000) = null,
		@p_descripcion varchar(300) = null,
		@p_idObjeto int = null
AS
BEGIN
		UPDATE [gen_config]
		SET	[uid] = isnull(@p_uid,[uid]),
			[eid] = isnull(@p_eid,[eid]),
			[id_usuario_modifico] = isnull(@p_id_usuario_modifico,[id_usuario_modifico]),
			[id_usuario_creo] = isnull(@p_id_usuario_creo,[id_usuario_creo]),
			[fechaModificacion] = GETDATE(),
			[config] = isnull(@p_config,[config]),
			[config_codigo] = isnull(@p_config_codigo,[config_codigo]),
			[modulo] = isnull(@p_modulo,[modulo]),
			[valor] = isnull(@p_valor,[valor]),
			[descripcion] = isnull(@p_descripcion,[descripcion]),
			[idObjeto] = isnull(@p_idObjeto,[idObjeto])
		WHERE (id = @p_id)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ins_gen_config]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_gen_config]
GO
CREATE PROCEDURE [ins_gen_config]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_config varchar(300),
		@p_config_codigo varchar(100),
		@p_modulo varchar(100),
		@p_valor varchar(1000),
		@p_descripcion varchar(300),
		@p_idObjeto int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [gen_config]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[config],
			[config_codigo],
			[modulo],
			[valor],
			[descripcion],
			[idObjeto])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_config,
			@p_config_codigo,
			@p_modulo,
			@p_valor,
			@p_descripcion,
			@p_idObjeto)
		SET @v_id = scope_identity()
UPDATE [gen_config] set [uid] = [uid] + '_' + convert(varchar(20),@v_id) where [id]=@v_id
SELECT @v_id as id
RETURN @v_id
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[act_gen_config]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_gen_config]
GO
CREATE PROCEDURE [act_gen_config]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [gen_config]
	SET active=@p_active,
	[id_usuario_modifico] = isnull(@p_id_usuario_modifico,[id_usuario_modifico]),
	[fechaModificacion] = GETDATE()
	WHERE	(id = @p_id)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_gen_config_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_gen_config_m]
GO
CREATE PROCEDURE [reg_gen_config_m]
	@p_id INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT 	[id] AS [id],
		[uid] AS [uid],
		[eid] AS [eid],
		[id_usuario_modifico] AS [id_usuario_modifico],
		[id_usuario_creo] AS [id_usuario_creo],
		[fechaModificacion] AS [fechaModificacion],
		[fechaCreacion] AS [fechaCreacion],
		[active] AS [active],
		[config] AS [config],
		[config_codigo] AS [config_codigo],
		[modulo] AS [modulo],
		[valor] AS [valor],
		[descripcion] AS [descripcion],
		[idObjeto] AS [idObjeto]
	FROM [gen_config]
	WHERE	(id = @p_id)
	AND	(eid LIKE @p_eid+'%')

END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sel_gen_configesXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_configesXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_gen_configesXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_gen_config]
	WHERE ([id_usuario_modifico]= @p_id_usuario_modifico)
	AND	(eid LIKE @p_eid+'%')
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sel_gen_configesXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_configesXusuario_creo_m]
GO
CREATE PROCEDURE [sel_gen_configesXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_gen_config]
	WHERE ([id_usuario_creo]= @p_id_usuario_creo)
	AND	(eid LIKE @p_eid+'%')
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sel_gen_configes_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_configes_m]
GO
CREATE PROCEDURE [sel_gen_configes_m]
	@p_eid as varchar (50)='',
	@p_filtro as varchar (8000) = '1=1',
	@p_orden as varchar (8000) = 'id',
	@p_campos as varchar (8000) = '*' AS
BEGIN
exec ('SELECT '+ @p_campos +
' FROM [view_gen_config]
WHERE  (eid like '''+@p_eid+'%'+''' ) AND (' + @p_filtro + ') ORDER BY ' + @p_orden)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[lis_gen_configes_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_gen_configes_m]
GO
CREATE PROCEDURE [lis_gen_configes_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[config] AS [gen_config]
	FROM [gen_config]
	WHERE active = 1
	AND	(eid LIKE @p_eid+'%')
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[aud_gen_config]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_gen_config]
GO
CREATE PROCEDURE [aud_gen_config]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [gen_config].id as id_tabla,
	[gen_config].id_usuario_modifico as id_usuario_modifico,
	[gen_config].id_usuario_creo as id_usuario_creo,
	[gen_config].fechaModificacion as fechaModificacion,
	[gen_config].fechaCreacion as fechaCreacion,
	[gen_config].active as active,
	[gen_config].[config] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [gen_config]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[gen_config].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[gen_config].[id_usuario_creo]
	where ' + @p_filtro)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'gen_config'
-----------------------------------10/12/21 19:13:26