
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_origenDato]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_rep_origenDato]
GO
CREATE PROCEDURE [reg_rep_origenDato]
	@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],
		[origenDato] AS [origenDato],
		[origenDato_codigo] AS [origenDato_codigo],
		[id_reporte] AS [id_reporte],
		[duracion] AS [duracion]
	FROM [rep_origenDato]
	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_origenDato]')) DROP VIEW [dbo].[view_rep_origenDato]
GO
CREATE  VIEW [view_rep_origenDato] AS
SELECT 	[rep_origenDato].[id] AS [id],
		[rep_origenDato].[uid] AS [uid],
		[rep_origenDato].[eid] AS [eid],
		[rep_origenDato].[id_usuario_modifico] AS [id_usuario_modifico],
		[rep_origenDato].[id_usuario_creo] AS [id_usuario_creo],
		[rep_origenDato].[fechaModificacion] AS [fechaModificacion],
		[rep_origenDato].[fechaCreacion] AS [fechaCreacion],
		[rep_origenDato].[active] AS [active],
		[rep_origenDato].[origenDato] AS [origenDato],
		[rep_origenDato].[origenDato_codigo] AS [origenDato_codigo],
		[rep_origenDato].[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_origenDato].[duracion] AS [duracion],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [rep_origenDato]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[rep_origenDato].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[rep_origenDato].[id_usuario_creo]
	INNER JOIN [rep_reporte] ON [rep_reporte].id=[rep_origenDato].[id_reporte]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [rep_origenDato].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [rep_origenDato].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_origenDatosXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_rep_origenDatosXusuario_modifico]
GO
CREATE PROCEDURE [sel_rep_origenDatosXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_rep_origenDato]
	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_origenDatosXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_rep_origenDatosXusuario_creo]
GO
CREATE PROCEDURE [sel_rep_origenDatosXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_rep_origenDato]
	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_origenDatosXreporte]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_rep_origenDatosXreporte]
GO
CREATE PROCEDURE [sel_rep_origenDatosXreporte]
	@p_id_reporte INT
AS
BEGIN
	SELECT * 
	FROM [view_rep_origenDato]
	WHERE ([id_reporte]= @p_id_reporte)
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_origenDatos]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_rep_origenDatos]
GO
CREATE PROCEDURE [sel_rep_origenDatos]
	
	@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_origenDato]
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_origenDatos]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_rep_origenDatos]
GO
CREATE PROCEDURE [lis_rep_origenDatos]
	AS
BEGIN
	SELECT id AS id,
[origenDato] AS [rep_origenDato]
	FROM [rep_origenDato]
	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_origenDato]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_rep_origenDato]
GO
CREATE PROCEDURE [upd_rep_origenDato]
	@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_origenDato varchar(300) = null,
		@p_origenDato_codigo varchar(100) = null,
		@p_id_reporte int = null,
		@p_duracion int = null
AS
BEGIN
		UPDATE [rep_origenDato]
		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(),
			[origenDato] = isnull(@p_origenDato,[origenDato]),
			[origenDato_codigo] = isnull(@p_origenDato_codigo,[origenDato_codigo]),
			[id_reporte] = isnull(@p_id_reporte,[id_reporte]),
			[duracion] = isnull(@p_duracion,[duracion])
		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_origenDato]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_rep_origenDato]
GO
CREATE PROCEDURE [ins_rep_origenDato]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_origenDato varchar(300),
		@p_origenDato_codigo varchar(100),
		@p_id_reporte int,
		@p_duracion int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [rep_origenDato]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[origenDato],
			[origenDato_codigo],
			[id_reporte],
			[duracion])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_origenDato,
			@p_origenDato_codigo,
			@p_id_reporte,
			@p_duracion)
		SET @v_id = scope_identity()
UPDATE [rep_origenDato] 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_origenDato]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_rep_origenDato]
GO
CREATE PROCEDURE [act_rep_origenDato]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [rep_origenDato]
	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_origenDato_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_rep_origenDato_m]
GO
CREATE PROCEDURE [reg_rep_origenDato_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],
		[origenDato] AS [origenDato],
		[origenDato_codigo] AS [origenDato_codigo],
		[id_reporte] AS [id_reporte],
		[duracion] AS [duracion]
	FROM [rep_origenDato]
	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_origenDatosXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_rep_origenDatosXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_rep_origenDatosXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_rep_origenDato]
	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_origenDatosXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_rep_origenDatosXusuario_creo_m]
GO
CREATE PROCEDURE [sel_rep_origenDatosXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_rep_origenDato]
	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_origenDatosXreporte_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_rep_origenDatosXreporte_m]
GO
CREATE PROCEDURE [sel_rep_origenDatosXreporte_m]
	@p_id_reporte INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_rep_origenDato]
	WHERE ([id_reporte]= @p_id_reporte)
	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_origenDatos_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_rep_origenDatos_m]
GO
CREATE PROCEDURE [sel_rep_origenDatos_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_origenDato]
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_origenDatos_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_rep_origenDatos_m]
GO
CREATE PROCEDURE [lis_rep_origenDatos_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[origenDato] AS [rep_origenDato]
	FROM [rep_origenDato]
	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_origenDato]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_rep_origenDato]
GO
CREATE PROCEDURE [aud_rep_origenDato]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [rep_origenDato].id as id_tabla,
	[rep_origenDato].id_usuario_modifico as id_usuario_modifico,
	[rep_origenDato].id_usuario_creo as id_usuario_creo,
	[rep_origenDato].fechaModificacion as fechaModificacion,
	[rep_origenDato].fechaCreacion as fechaCreacion,
	[rep_origenDato].active as active,
	[rep_origenDato].[origenDato] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [rep_origenDato]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[rep_origenDato].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[rep_origenDato].[id_usuario_creo]
	where ' + @p_filtro)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'rep_origenDato'
-----------------------------------16/08/2012 02:09:19 p.m.