



SET QUOTED_IDENTIFIER OFF

GO

SET ANSI_NULLS OFF

GO


IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_rep_reporte]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_rep_reporte]
GO
CREATE PROCEDURE [reg_rep_reporte]
	@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],
		[reporte] AS [reporte],
		[reporte_codigo] AS [reporte_codigo],
		[id_funcionalidad] AS [id_funcionalidad],
		[esFormato] AS [esFormato],
		[tieneGraficos] AS [tieneGraficos],
		[cabecera] AS [cabecera],
		[orden] AS [orden]
	FROM [rep_reporte]
	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_rep_reporte]')) DROP VIEW [dbo].[view_rep_reporte]
GO
CREATE  VIEW [view_rep_reporte] AS
SELECT 	[rep_reporte].[id] AS [id],
		[rep_reporte].[uid] AS [uid],
		[rep_reporte].[eid] AS [eid],
		[rep_reporte].[id_usuario_modifico] AS [id_usuario_modifico],
		[rep_reporte].[id_usuario_creo] AS [id_usuario_creo],
		[rep_reporte].[fechaModificacion] AS [fechaModificacion],
		[rep_reporte].[fechaCreacion] AS [fechaCreacion],
		[rep_reporte].[active] AS [active],
		[rep_reporte].[reporte] AS [reporte],
		[rep_reporte].[reporte_codigo] AS [reporte_codigo],
		[rep_reporte].[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],
		[rep_reporte].[esFormato] AS [esFormato],
		[rep_reporte].[tieneGraficos] AS [tieneGraficos],
		[rep_reporte].[cabecera] AS [cabecera],
		[rep_reporte].[orden] AS [orden],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [rep_reporte]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[rep_reporte].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[rep_reporte].[id_usuario_creo]
	INNER JOIN [gui_funcionalidad] ON [gui_funcionalidad].id=[rep_reporte].[id_funcionalidad]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [rep_reporte].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [rep_reporte].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_rep_reportesXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_rep_reportesXusuario_modifico]
GO
CREATE PROCEDURE [sel_rep_reportesXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_rep_reporte]
	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_rep_reportesXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_rep_reportesXusuario_creo]
GO
CREATE PROCEDURE [sel_rep_reportesXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_rep_reporte]
	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_rep_reportesXfuncionalidad]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_rep_reportesXfuncionalidad]
GO
CREATE PROCEDURE [sel_rep_reportesXfuncionalidad]
	@p_id_funcionalidad INT
AS
BEGIN
	SELECT * 
	FROM [view_rep_reporte]
	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_rep_reportes]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_rep_reportes]
GO
CREATE PROCEDURE [sel_rep_reportes]
	
	@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_rep_reporte]
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_rep_reportes]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_rep_reportes]
GO
CREATE PROCEDURE [lis_rep_reportes]
	AS
BEGIN
	SELECT id AS id,
[reporte] AS [rep_reporte]
	FROM [rep_reporte]
	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_rep_reporte]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_rep_reporte]
GO
CREATE PROCEDURE [upd_rep_reporte]
	@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_reporte varchar(300) = null,
		@p_reporte_codigo varchar(100) = null,
		@p_id_funcionalidad int = null,
		@p_esFormato bit = null,
		@p_tieneGraficos bit = null,
		@p_cabecera varchar(1) = null,
		@p_orden int = null
AS
BEGIN
		UPDATE [rep_reporte]
		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(),
			[reporte] = isnull(@p_reporte,[reporte]),
			[reporte_codigo] = isnull(@p_reporte_codigo,[reporte_codigo]),
			[id_funcionalidad] = isnull(@p_id_funcionalidad,[id_funcionalidad]),
			[esFormato] = isnull(@p_esFormato,[esFormato]),
			[tieneGraficos] = isnull(@p_tieneGraficos,[tieneGraficos]),
			[cabecera] = isnull(@p_cabecera,[cabecera]),
			[orden] = isnull(@p_orden,[orden])
		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_rep_reporte]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_rep_reporte]
GO
CREATE PROCEDURE [ins_rep_reporte]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_reporte varchar(300),
		@p_reporte_codigo varchar(100),
		@p_id_funcionalidad int,
		@p_esFormato bit,
		@p_tieneGraficos bit,
		@p_cabecera varchar(1),
		@p_orden int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [rep_reporte]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[reporte],
			[reporte_codigo],
			[id_funcionalidad],
			[esFormato],
			[tieneGraficos],
			[cabecera],
			[orden])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_reporte,
			@p_reporte_codigo,
			@p_id_funcionalidad,
			@p_esFormato,
			@p_tieneGraficos,
			@p_cabecera,
			@p_orden)
		SET @v_id = scope_identity()
UPDATE [rep_reporte] 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_rep_reporte]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_rep_reporte]
GO
CREATE PROCEDURE [act_rep_reporte]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [rep_reporte]
	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_rep_reporte_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_rep_reporte_m]
GO
CREATE PROCEDURE [reg_rep_reporte_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],
		[reporte] AS [reporte],
		[reporte_codigo] AS [reporte_codigo],
		[id_funcionalidad] AS [id_funcionalidad],
		[esFormato] AS [esFormato],
		[tieneGraficos] AS [tieneGraficos],
		[cabecera] AS [cabecera],
		[orden] AS [orden]
	FROM [rep_reporte]
	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_rep_reportesXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_rep_reportesXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_rep_reportesXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_rep_reporte]
	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_rep_reportesXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_rep_reportesXusuario_creo_m]
GO
CREATE PROCEDURE [sel_rep_reportesXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_rep_reporte]
	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_rep_reportesXfuncionalidad_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_rep_reportesXfuncionalidad_m]
GO
CREATE PROCEDURE [sel_rep_reportesXfuncionalidad_m]
	@p_id_funcionalidad INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_rep_reporte]
	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_rep_reportes_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_rep_reportes_m]
GO
CREATE PROCEDURE [sel_rep_reportes_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_rep_reporte]
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_rep_reportes_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_rep_reportes_m]
GO
CREATE PROCEDURE [lis_rep_reportes_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[reporte] AS [rep_reporte]
	FROM [rep_reporte]
	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_rep_reporte]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_rep_reporte]
GO
CREATE PROCEDURE [aud_rep_reporte]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [rep_reporte].id as id_tabla,
	[rep_reporte].id_usuario_modifico as id_usuario_modifico,
	[rep_reporte].id_usuario_creo as id_usuario_creo,
	[rep_reporte].fechaModificacion as fechaModificacion,
	[rep_reporte].fechaCreacion as fechaCreacion,
	[rep_reporte].active as active,
	[rep_reporte].[reporte] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [rep_reporte]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[rep_reporte].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[rep_reporte].[id_usuario_creo]
	where ' + @p_filtro)
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_rep_reporte_categoria]')) DROP VIEW [dbo].[view_rep_reporte_categoria]
GO
CREATE  VIEW [view_rep_reporte_categoria] AS
SELECT 	[rep_reporte_categoria].[id] AS [id],
		[rep_reporte_categoria].[uid] AS [uid],
		[rep_reporte_categoria].[eid] AS [eid],
		[rep_reporte_categoria].[id_usuario_modifico] AS [id_usuario_modifico],
		[rep_reporte_categoria].[id_usuario_creo] AS [id_usuario_creo],
		[rep_reporte_categoria].[fechaModificacion] AS [fechaModificacion],
		[rep_reporte_categoria].[fechaCreacion] AS [fechaCreacion],
		[rep_reporte_categoria].[active] AS [active],
		[rep_reporte_categoria].[reporte_categoria] AS [reporte_categoria],
		[rep_reporte_categoria].[id_categoria] AS [id_categoria],
[rep_categoria].[categoria] as [rep_categoria_categoria],
[rep_categoria].[categoria_codigo] as [rep_categoria_categoria_codigo],
[rep_categoria].[id_categoria] as [rep_categoria_id_categoria],
		[rep_reporte_categoria].[id_reporte] AS [id_reporte],
[rep_reporte].[reporte] as [rep_reporte_reporte],
[rep_reporte].[reporte_codigo] as [rep_reporte_reporte_codigo],
[rep_reporte].[id_funcionalidad] as [rep_reporte_id_funcionalidad],
[rep_reporte].[esFormato] as [rep_reporte_esFormato],
[rep_reporte].[tieneGraficos] as [rep_reporte_tieneGraficos],
[rep_reporte].[cabecera] as [rep_reporte_cabecera],
[rep_reporte].[orden] as [rep_reporte_orden],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [rep_reporte_categoria]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[rep_reporte_categoria].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[rep_reporte_categoria].[id_usuario_creo]
	INNER JOIN [rep_categoria] ON [rep_categoria].id=[rep_reporte_categoria].[id_categoria]
	INNER JOIN [rep_reporte] ON [rep_reporte].id=[rep_reporte_categoria].[id_reporte]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [rep_reporte_categoria].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
