IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_seg_perfil_funcionalidad]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_seg_perfil_funcionalidad]
GO
CREATE PROCEDURE [reg_seg_perfil_funcionalidad]
	@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],
		[perfil_funcionalidad] AS [perfil_funcionalidad],
		[id_perfil] AS [id_perfil],
		[id_funcionalidad] AS [id_funcionalidad]
	FROM [seg_perfil_funcionalidad]
	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_seg_perfil_funcionalidad]')) DROP VIEW [dbo].[view_seg_perfil_funcionalidad]
GO
CREATE  VIEW [view_seg_perfil_funcionalidad] AS
SELECT 	[seg_perfil_funcionalidad].[id] AS [id],
		[seg_perfil_funcionalidad].[uid] AS [uid],
		[seg_perfil_funcionalidad].[eid] AS [eid],
		[seg_perfil_funcionalidad].[id_usuario_modifico] AS [id_usuario_modifico],
		[seg_perfil_funcionalidad].[id_usuario_creo] AS [id_usuario_creo],
		[seg_perfil_funcionalidad].[fechaModificacion] AS [fechaModificacion],
		[seg_perfil_funcionalidad].[fechaCreacion] AS [fechaCreacion],
		[seg_perfil_funcionalidad].[active] AS [active],
		[seg_perfil_funcionalidad].[perfil_funcionalidad] AS [perfil_funcionalidad],
		[seg_perfil_funcionalidad].[id_perfil] AS [id_perfil],
[seg_perfil].[perfil] as [seg_perfil_perfil],
[seg_perfil].[perfil_codigo] as [seg_perfil_perfil_codigo],
[seg_perfil].[descripcion] as [seg_perfil_descripcion],
[seg_perfil].[mostrarBotones] as [seg_perfil_mostrarBotones],
[seg_perfil].[paraCliente] as [seg_perfil_paraCliente],
[seg_perfil].[paraProveedor] as [seg_perfil_paraProveedor],
		[seg_perfil_funcionalidad].[id_funcionalidad] AS [id_funcionalidad],
[gui_funcionalidad].[funcionalidad] as [gui_funcionalidad_funcionalidad],
[gui_funcionalidad].[funcionalidad_codigo] as [gui_funcionalidad_funcionalidad_codigo],
[gui_funcionalidad].[urlImagen] as [gui_funcionalidad_urlImagen],
[gui_funcionalidad].[urlFormulario] as [gui_funcionalidad_urlFormulario],
[gui_funcionalidad].[targetUrl] as [gui_funcionalidad_targetUrl],
[gui_funcionalidad].[url] as [gui_funcionalidad_url],
[gui_funcionalidad].[toolTip] as [gui_funcionalidad_toolTip],
[gui_funcionalidad].[esMenu] as [gui_funcionalidad_esMenu],
[gui_funcionalidad].[nombreControl] as [gui_funcionalidad_nombreControl],
[gui_funcionalidad].[nombreComando] as [gui_funcionalidad_nombreComando],
[gui_funcionalidad].[orden] as [gui_funcionalidad_orden],
[gui_funcionalidad].[id_funcionalidad] as [gui_funcionalidad_id_funcionalidad],
[gui_funcionalidad].[id_tipoFuncionalidad] as [gui_funcionalidad_id_tipoFuncionalidad],
[gui_funcionalidad].[esMvc] as [gui_funcionalidad_esMvc],
[gui_funcionalidad].[id_aplicacion] as [gui_funcionalidad_id_aplicacion],
funPadre.nombreControl as controlPadre,
		[empresaCodigo].[empresa] As [multiempresa]
FROM [seg_perfil_funcionalidad]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[seg_perfil_funcionalidad].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[seg_perfil_funcionalidad].[id_usuario_creo]
	INNER JOIN [seg_perfil] ON [seg_perfil].id=[seg_perfil_funcionalidad].[id_perfil]
	INNER JOIN [gui_funcionalidad] ON [gui_funcionalidad].id=[seg_perfil_funcionalidad].[id_funcionalidad]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [seg_perfil_funcionalidad].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
inner join gui_funcionalidad as funPadre on gui_funcionalidad.id_funcionalidad = funPadre.id
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_seg_perfil_funcionalidadesXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_seg_perfil_funcionalidadesXusuario_modifico]
GO
CREATE PROCEDURE [sel_seg_perfil_funcionalidadesXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_seg_perfil_funcionalidad]
	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_seg_perfil_funcionalidadesXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_seg_perfil_funcionalidadesXusuario_creo]
GO
CREATE PROCEDURE [sel_seg_perfil_funcionalidadesXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_seg_perfil_funcionalidad]
	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_seg_perfil_funcionalidadesXperfil]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_seg_perfil_funcionalidadesXperfil]
GO
CREATE PROCEDURE [sel_seg_perfil_funcionalidadesXperfil]
	@p_id_perfil INT
AS
BEGIN
	SELECT * 
	FROM [view_seg_perfil_funcionalidad]
	WHERE ([id_perfil]= @p_id_perfil)
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_seg_perfil_funcionalidadesXfuncionalidad]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_seg_perfil_funcionalidadesXfuncionalidad]
GO
CREATE PROCEDURE [sel_seg_perfil_funcionalidadesXfuncionalidad]
	@p_id_funcionalidad INT
AS
BEGIN
	SELECT * 
	FROM [view_seg_perfil_funcionalidad]
	WHERE ([id_funcionalidad]= @p_id_funcionalidad)
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_seg_perfil_funcionalidades]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_seg_perfil_funcionalidades]
GO
CREATE PROCEDURE [sel_seg_perfil_funcionalidades]
	
	@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_seg_perfil_funcionalidad]
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_seg_perfil_funcionalidades]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_seg_perfil_funcionalidades]
GO
CREATE PROCEDURE [lis_seg_perfil_funcionalidades]
	AS
BEGIN
	SELECT id AS id,
[perfil_funcionalidad] AS [seg_perfil_funcionalidad]
	FROM [seg_perfil_funcionalidad]
	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_seg_perfil_funcionalidad]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_seg_perfil_funcionalidad]
GO
CREATE PROCEDURE [upd_seg_perfil_funcionalidad]
	@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_perfil_funcionalidad varchar(300) = null,
		@p_id_perfil int = null,
		@p_id_funcionalidad int = null
AS
BEGIN
		UPDATE [seg_perfil_funcionalidad]
		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(),
			[perfil_funcionalidad] = isnull(@p_perfil_funcionalidad,[perfil_funcionalidad]),
			[id_perfil] = isnull(@p_id_perfil,[id_perfil]),
			[id_funcionalidad] = isnull(@p_id_funcionalidad,[id_funcionalidad])
		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_seg_perfil_funcionalidad]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_seg_perfil_funcionalidad]
GO
CREATE PROCEDURE [ins_seg_perfil_funcionalidad]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_perfil_funcionalidad varchar(300),
		@p_id_perfil int,
		@p_id_funcionalidad int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [seg_perfil_funcionalidad]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[perfil_funcionalidad],
			[id_perfil],
			[id_funcionalidad])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_perfil_funcionalidad,
			@p_id_perfil,
			@p_id_funcionalidad)
		SET @v_id = scope_identity()
UPDATE [seg_perfil_funcionalidad] 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_seg_perfil_funcionalidad]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_seg_perfil_funcionalidad]
GO
CREATE PROCEDURE [act_seg_perfil_funcionalidad]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [seg_perfil_funcionalidad]
	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_seg_perfil_funcionalidad_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_seg_perfil_funcionalidad_m]
GO
CREATE PROCEDURE [reg_seg_perfil_funcionalidad_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],
		[perfil_funcionalidad] AS [perfil_funcionalidad],
		[id_perfil] AS [id_perfil],
		[id_funcionalidad] AS [id_funcionalidad]
	FROM [seg_perfil_funcionalidad]
	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_seg_perfil_funcionalidadesXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_seg_perfil_funcionalidadesXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_seg_perfil_funcionalidadesXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_seg_perfil_funcionalidad]
	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_seg_perfil_funcionalidadesXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_seg_perfil_funcionalidadesXusuario_creo_m]
GO
CREATE PROCEDURE [sel_seg_perfil_funcionalidadesXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_seg_perfil_funcionalidad]
	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_seg_perfil_funcionalidadesXperfil_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_seg_perfil_funcionalidadesXperfil_m]
GO
CREATE PROCEDURE [sel_seg_perfil_funcionalidadesXperfil_m]
	@p_id_perfil INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_seg_perfil_funcionalidad]
	WHERE ([id_perfil]= @p_id_perfil)
	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_seg_perfil_funcionalidadesXfuncionalidad_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_seg_perfil_funcionalidadesXfuncionalidad_m]
GO
CREATE PROCEDURE [sel_seg_perfil_funcionalidadesXfuncionalidad_m]
	@p_id_funcionalidad INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_seg_perfil_funcionalidad]
	WHERE ([id_funcionalidad]= @p_id_funcionalidad)
	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_seg_perfil_funcionalidades_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_seg_perfil_funcionalidades_m]
GO
CREATE PROCEDURE [sel_seg_perfil_funcionalidades_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_seg_perfil_funcionalidad]
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_seg_perfil_funcionalidades_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_seg_perfil_funcionalidades_m]
GO
CREATE PROCEDURE [lis_seg_perfil_funcionalidades_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[perfil_funcionalidad] AS [seg_perfil_funcionalidad]
	FROM [seg_perfil_funcionalidad]
	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_seg_perfil_funcionalidad]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_seg_perfil_funcionalidad]
GO
CREATE PROCEDURE [aud_seg_perfil_funcionalidad]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [seg_perfil_funcionalidad].id as id_tabla,
	[seg_perfil_funcionalidad].id_usuario_modifico as id_usuario_modifico,
	[seg_perfil_funcionalidad].id_usuario_creo as id_usuario_creo,
	[seg_perfil_funcionalidad].fechaModificacion as fechaModificacion,
	[seg_perfil_funcionalidad].fechaCreacion as fechaCreacion,
	[seg_perfil_funcionalidad].active as active,
	[seg_perfil_funcionalidad].[perfil_funcionalidad] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

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