SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_cat_sistema]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_cat_sistema]
GO
CREATE PROCEDURE [reg_cat_sistema]
	@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],
		[sistema] AS [sistema],
		[sistema_codigo] AS [sistema_codigo],
		[id_sistema] AS [id_sistema]
	FROM [cat_sistema]
	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_cat_sistema]')) DROP VIEW [dbo].[view_cat_sistema]
GO
CREATE  VIEW [view_cat_sistema] AS
SELECT 	[cat_sistema].[id] AS [id],
		[cat_sistema].[uid] AS [uid],
		[cat_sistema].[eid] AS [eid],
		[cat_sistema].[id_usuario_modifico] AS [id_usuario_modifico],
		[cat_sistema].[id_usuario_creo] AS [id_usuario_creo],
		[cat_sistema].[fechaModificacion] AS [fechaModificacion],
		[cat_sistema].[fechaCreacion] AS [fechaCreacion],
		[cat_sistema].[active] AS [active],
		[cat_sistema].[sistema] AS [sistema],
		[cat_sistema].[sistema_codigo] AS [sistema_codigo],
		[cat_sistema].[id_sistema] AS [id_sistema],
[cat_sistema_padre].[sistema] as [cat_sistema_padre_sistema],
[cat_sistema_padre].[sistema_codigo] as [cat_sistema_padre_sistema_codigo],
[cat_sistema_padre].[id_sistema] as [cat_sistema_padre_id_sistema],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [cat_sistema]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[cat_sistema].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[cat_sistema].[id_usuario_creo]
	INNER JOIN [cat_sistema] AS [cat_sistema_padre] ON [cat_sistema_padre].id=[cat_sistema].[id_sistema]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [cat_sistema].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [cat_sistema].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_cat_sistemasXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_sistemasXusuario_modifico]
GO
CREATE PROCEDURE [sel_cat_sistemasXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_cat_sistema]
	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_cat_sistemasXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_sistemasXusuario_creo]
GO
CREATE PROCEDURE [sel_cat_sistemasXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_cat_sistema]
	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_cat_sistemasXsistema]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_sistemasXsistema]
GO
CREATE PROCEDURE [sel_cat_sistemasXsistema]
	@p_id_sistema INT
AS
BEGIN
	SELECT * 
	FROM [view_cat_sistema]
	WHERE ([id_sistema]= @p_id_sistema)
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_cat_sistemas]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_sistemas]
GO
CREATE PROCEDURE [sel_cat_sistemas]
	
	@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_cat_sistema]
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_cat_sistemas]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_cat_sistemas]
GO
CREATE PROCEDURE [lis_cat_sistemas]
	AS
BEGIN
	SELECT id AS id,
[sistema] AS [cat_sistema]
	FROM [cat_sistema]
	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_cat_sistema]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_cat_sistema]
GO
CREATE PROCEDURE [upd_cat_sistema]
	@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_sistema varchar(300) = null,
		@p_sistema_codigo varchar(100) = null,
		@p_id_sistema int = null
AS
BEGIN
		UPDATE [cat_sistema]
		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(),
			[sistema] = isnull(@p_sistema,[sistema]),
			[sistema_codigo] = isnull(@p_sistema_codigo,[sistema_codigo]),
			[id_sistema] = isnull(@p_id_sistema,[id_sistema])
		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_cat_sistema]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_cat_sistema]
GO
CREATE PROCEDURE [ins_cat_sistema]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_sistema varchar(300),
		@p_sistema_codigo varchar(100),
		@p_id_sistema int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [cat_sistema]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[sistema],
			[sistema_codigo],
			[id_sistema])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_sistema,
			@p_sistema_codigo,
			@p_id_sistema)
		SET @v_id = scope_identity()
UPDATE [cat_sistema] 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_cat_sistema]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_cat_sistema]
GO
CREATE PROCEDURE [act_cat_sistema]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [cat_sistema]
	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_cat_sistema_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_cat_sistema_m]
GO
CREATE PROCEDURE [reg_cat_sistema_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],
		[sistema] AS [sistema],
		[sistema_codigo] AS [sistema_codigo],
		[id_sistema] AS [id_sistema]
	FROM [cat_sistema]
	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_cat_sistemasXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_sistemasXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_cat_sistemasXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cat_sistema]
	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_cat_sistemasXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_sistemasXusuario_creo_m]
GO
CREATE PROCEDURE [sel_cat_sistemasXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cat_sistema]
	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_cat_sistemasXsistema_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_sistemasXsistema_m]
GO
CREATE PROCEDURE [sel_cat_sistemasXsistema_m]
	@p_id_sistema INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cat_sistema]
	WHERE ([id_sistema]= @p_id_sistema)
	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_cat_sistemas_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_sistemas_m]
GO
CREATE PROCEDURE [sel_cat_sistemas_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_cat_sistema]
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_cat_sistemas_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_cat_sistemas_m]
GO
CREATE PROCEDURE [lis_cat_sistemas_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[sistema] AS [cat_sistema]
	FROM [cat_sistema]
	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_cat_sistema]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_cat_sistema]
GO
CREATE PROCEDURE [aud_cat_sistema]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [cat_sistema].id as id_tabla,
	[cat_sistema].id_usuario_modifico as id_usuario_modifico,
	[cat_sistema].id_usuario_creo as id_usuario_creo,
	[cat_sistema].fechaModificacion as fechaModificacion,
	[cat_sistema].fechaCreacion as fechaCreacion,
	[cat_sistema].active as active,
	[cat_sistema].[sistema] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [cat_sistema]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[cat_sistema].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[cat_sistema].[id_usuario_creo]
	where ' + @p_filtro)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'cat_sistema'
-----------------------------------25/07/2012 12:11:19 p.m.