IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_gui_aplicacion]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_gui_aplicacion]
GO
CREATE PROCEDURE [reg_gui_aplicacion]
	@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],
		[aplicacion] AS [aplicacion],
		[aplicacion_codigo] AS [aplicacion_codigo]
	FROM [gui_aplicacion]
	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_gui_aplicacion]')) DROP VIEW [dbo].[view_gui_aplicacion]
GO
CREATE  VIEW [view_gui_aplicacion] AS
SELECT 	[gui_aplicacion].[id] AS [id],
		[gui_aplicacion].[uid] AS [uid],
		[gui_aplicacion].[eid] AS [eid],
		[gui_aplicacion].[id_usuario_modifico] AS [id_usuario_modifico],
		[gui_aplicacion].[id_usuario_creo] AS [id_usuario_creo],
		[gui_aplicacion].[fechaModificacion] AS [fechaModificacion],
		[gui_aplicacion].[fechaCreacion] AS [fechaCreacion],
		[gui_aplicacion].[active] AS [active],
		[gui_aplicacion].[aplicacion] AS [aplicacion],
		[gui_aplicacion].[aplicacion_codigo] AS [aplicacion_codigo],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [gui_aplicacion]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[gui_aplicacion].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[gui_aplicacion].[id_usuario_creo]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [gui_aplicacion].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [gui_aplicacion].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_gui_aplicacionesXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gui_aplicacionesXusuario_modifico]
GO
CREATE PROCEDURE [sel_gui_aplicacionesXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_gui_aplicacion]
	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_gui_aplicacionesXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gui_aplicacionesXusuario_creo]
GO
CREATE PROCEDURE [sel_gui_aplicacionesXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_gui_aplicacion]
	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_gui_aplicaciones]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gui_aplicaciones]
GO
CREATE PROCEDURE [sel_gui_aplicaciones]
	
	@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_gui_aplicacion]
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_gui_aplicaciones]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_gui_aplicaciones]
GO
CREATE PROCEDURE [lis_gui_aplicaciones]
	AS
BEGIN
	SELECT id AS id,
[aplicacion] AS [gui_aplicacion]
	FROM [gui_aplicacion]
	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_gui_aplicacion]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_gui_aplicacion]
GO
CREATE PROCEDURE [upd_gui_aplicacion]
	@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_aplicacion varchar(300) = null,
		@p_aplicacion_codigo varchar(100) = null
AS
BEGIN
		UPDATE [gui_aplicacion]
		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(),
			[aplicacion] = isnull(@p_aplicacion,[aplicacion]),
			[aplicacion_codigo] = isnull(@p_aplicacion_codigo,[aplicacion_codigo])
		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_gui_aplicacion]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_gui_aplicacion]
GO
CREATE PROCEDURE [ins_gui_aplicacion]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_aplicacion varchar(300),
		@p_aplicacion_codigo varchar(100)
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [gui_aplicacion]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[aplicacion],
			[aplicacion_codigo])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_aplicacion,
			@p_aplicacion_codigo)
		SET @v_id = scope_identity()
UPDATE [gui_aplicacion] 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_gui_aplicacion]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_gui_aplicacion]
GO
CREATE PROCEDURE [act_gui_aplicacion]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [gui_aplicacion]
	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_gui_aplicacion_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_gui_aplicacion_m]
GO
CREATE PROCEDURE [reg_gui_aplicacion_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],
		[aplicacion] AS [aplicacion],
		[aplicacion_codigo] AS [aplicacion_codigo]
	FROM [gui_aplicacion]
	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_gui_aplicacionesXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gui_aplicacionesXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_gui_aplicacionesXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_gui_aplicacion]
	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_gui_aplicacionesXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gui_aplicacionesXusuario_creo_m]
GO
CREATE PROCEDURE [sel_gui_aplicacionesXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_gui_aplicacion]
	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_gui_aplicaciones_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gui_aplicaciones_m]
GO
CREATE PROCEDURE [sel_gui_aplicaciones_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_gui_aplicacion]
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_gui_aplicaciones_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_gui_aplicaciones_m]
GO
CREATE PROCEDURE [lis_gui_aplicaciones_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[aplicacion] AS [gui_aplicacion]
	FROM [gui_aplicacion]
	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_gui_aplicacion]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_gui_aplicacion]
GO
CREATE PROCEDURE [aud_gui_aplicacion]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [gui_aplicacion].id as id_tabla,
	[gui_aplicacion].id_usuario_modifico as id_usuario_modifico,
	[gui_aplicacion].id_usuario_creo as id_usuario_creo,
	[gui_aplicacion].fechaModificacion as fechaModificacion,
	[gui_aplicacion].fechaCreacion as fechaCreacion,
	[gui_aplicacion].active as active,
	[gui_aplicacion].[aplicacion] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [gui_aplicacion]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[gui_aplicacion].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[gui_aplicacion].[id_usuario_creo]
	where ' + @p_filtro)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'gui_aplicacion'
-----------------------------------25/11/21 13:28:45