
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_gen_registroEnvio]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_gen_registroEnvio]
GO
CREATE PROCEDURE [reg_gen_registroEnvio]
	@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],
		[registroEnvio] AS [registroEnvio],
		[registroEnvio_codigo] AS [registroEnvio_codigo],
		[tabla] AS [tabla],
		[reintentar] AS [reintentar],
		[id_envioCorreo] AS [id_envioCorreo],
		[id_registroEnvio] AS [id_registroEnvio]
	FROM [gen_registroEnvio]
	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_gen_registroEnvio]')) DROP VIEW [dbo].[view_gen_registroEnvio]
GO
CREATE  VIEW [view_gen_registroEnvio] AS
SELECT 	[gen_registroEnvio].[id] AS [id],
		[gen_registroEnvio].[uid] AS [uid],
		[gen_registroEnvio].[eid] AS [eid],
		[gen_registroEnvio].[id_usuario_modifico] AS [id_usuario_modifico],
		[gen_registroEnvio].[id_usuario_creo] AS [id_usuario_creo],
		[gen_registroEnvio].[fechaModificacion] AS [fechaModificacion],
		[gen_registroEnvio].[fechaCreacion] AS [fechaCreacion],
		[gen_registroEnvio].[active] AS [active],
		[gen_registroEnvio].[registroEnvio] AS [registroEnvio],
		[gen_registroEnvio].[registroEnvio_codigo] AS [registroEnvio_codigo],
		[gen_registroEnvio].[tabla] AS [tabla],
		[gen_registroEnvio].[reintentar] AS [reintentar],
		[gen_registroEnvio].[id_envioCorreo] AS [id_envioCorreo],
[gen_envioCorreo].[envioCorreo] as [gen_envioCorreo_envioCorreo],
[gen_envioCorreo].[envioCorreo_codigo] as [gen_envioCorreo_envioCorreo_codigo],
[gen_envioCorreo].[estrategia] as [gen_envioCorreo_estrategia],
[gen_envioCorreo].[tabla] as [gen_envioCorreo_tabla],
[gen_envioCorreo].[id_reporte] as [gen_envioCorreo_id_reporte],
[gen_envioCorreo].[correoAdicional] as [gen_envioCorreo_correoAdicional],
[gen_envioCorreo].[plantillaAdjunto] as [gen_envioCorreo_plantillaAdjunto],
[gen_envioCorreo].[id_subtipoDocumento] as [gen_envioCorreo_id_subtipoDocumento],
[gen_envioCorreo].[id_estadoTipoDocumento] as [gen_envioCorreo_id_estadoTipoDocumento],
[gen_envioCorreo].[cantidadMensajes] as [gen_envioCorreo_cantidadMensajes],
[gen_envioCorreo].[tiempo_por_mensajes] as [gen_envioCorreo_tiempo_por_mensajes],
		[gen_registroEnvio].[id_registroEnvio] AS [id_registroEnvio],
[gen_registroEnvio_padre].[registroEnvio] as [gen_registroEnvio_padre_registroEnvio],
[gen_registroEnvio_padre].[registroEnvio_codigo] as [gen_registroEnvio_padre_registroEnvio_codigo],
[gen_registroEnvio_padre].[tabla] as [gen_registroEnvio_padre_tabla],
[gen_registroEnvio_padre].[reintentar] as [gen_registroEnvio_padre_reintentar],
[gen_registroEnvio_padre].[id_envioCorreo] as [gen_registroEnvio_padre_id_envioCorreo],
[gen_registroEnvio_padre].[id_registroEnvio] as [gen_registroEnvio_padre_id_registroEnvio],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [gen_registroEnvio]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[gen_registroEnvio].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[gen_registroEnvio].[id_usuario_creo]
	INNER JOIN [gen_envioCorreo] ON [gen_envioCorreo].id=[gen_registroEnvio].[id_envioCorreo]
	INNER JOIN [gen_registroEnvio] AS [gen_registroEnvio_padre] ON [gen_registroEnvio_padre].id=[gen_registroEnvio].[id_registroEnvio]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [gen_registroEnvio].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [gen_registroEnvio].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_gen_registroEnviosXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_registroEnviosXusuario_modifico]
GO
CREATE PROCEDURE [sel_gen_registroEnviosXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_gen_registroEnvio]
	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_gen_registroEnviosXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_registroEnviosXusuario_creo]
GO
CREATE PROCEDURE [sel_gen_registroEnviosXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_gen_registroEnvio]
	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_gen_registroEnviosXenvioCorreo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_registroEnviosXenvioCorreo]
GO
CREATE PROCEDURE [sel_gen_registroEnviosXenvioCorreo]
	@p_id_envioCorreo INT
AS
BEGIN
	SELECT * 
	FROM [view_gen_registroEnvio]
	WHERE ([id_envioCorreo]= @p_id_envioCorreo)
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_gen_registroEnviosXregistroEnvio]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_registroEnviosXregistroEnvio]
GO
CREATE PROCEDURE [sel_gen_registroEnviosXregistroEnvio]
	@p_id_registroEnvio INT
AS
BEGIN
	SELECT * 
	FROM [view_gen_registroEnvio]
	WHERE ([id_registroEnvio]= @p_id_registroEnvio)
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_gen_registroEnvios]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_registroEnvios]
GO
CREATE PROCEDURE [sel_gen_registroEnvios]
	
	@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_gen_registroEnvio]
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_gen_registroEnvios]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_gen_registroEnvios]
GO
CREATE PROCEDURE [lis_gen_registroEnvios]
	AS
BEGIN
	SELECT id AS id,
[registroEnvio] AS [gen_registroEnvio]
	FROM [gen_registroEnvio]
	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_gen_registroEnvio]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_gen_registroEnvio]
GO
CREATE PROCEDURE [upd_gen_registroEnvio]
	@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_registroEnvio varchar(300) = null,
		@p_registroEnvio_codigo varchar(100) = null,
		@p_tabla varchar(200) = null,
		@p_reintentar bit = null,
		@p_id_envioCorreo int = null,
		@p_id_registroEnvio int = null
AS
BEGIN
		UPDATE [gen_registroEnvio]
		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(),
			[registroEnvio] = isnull(@p_registroEnvio,[registroEnvio]),
			[registroEnvio_codigo] = isnull(@p_registroEnvio_codigo,[registroEnvio_codigo]),
			[tabla] = isnull(@p_tabla,[tabla]),
			[reintentar] = isnull(@p_reintentar,[reintentar]),
			[id_envioCorreo] = isnull(@p_id_envioCorreo,[id_envioCorreo]),
			[id_registroEnvio] = isnull(@p_id_registroEnvio,[id_registroEnvio])
		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_gen_registroEnvio]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_gen_registroEnvio]
GO
CREATE PROCEDURE [ins_gen_registroEnvio]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_registroEnvio varchar(300),
		@p_registroEnvio_codigo varchar(100),
		@p_tabla varchar(200),
		@p_reintentar bit,
		@p_id_envioCorreo int,
		@p_id_registroEnvio int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [gen_registroEnvio]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[registroEnvio],
			[registroEnvio_codigo],
			[tabla],
			[reintentar],
			[id_envioCorreo],
			[id_registroEnvio])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_registroEnvio,
			@p_registroEnvio_codigo,
			@p_tabla,
			@p_reintentar,
			@p_id_envioCorreo,
			@p_id_registroEnvio)
		SET @v_id = scope_identity()
UPDATE [gen_registroEnvio] 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_gen_registroEnvio]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_gen_registroEnvio]
GO
CREATE PROCEDURE [act_gen_registroEnvio]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [gen_registroEnvio]
	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_gen_registroEnvio_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_gen_registroEnvio_m]
GO
CREATE PROCEDURE [reg_gen_registroEnvio_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],
		[registroEnvio] AS [registroEnvio],
		[registroEnvio_codigo] AS [registroEnvio_codigo],
		[tabla] AS [tabla],
		[reintentar] AS [reintentar],
		[id_envioCorreo] AS [id_envioCorreo],
		[id_registroEnvio] AS [id_registroEnvio]
	FROM [gen_registroEnvio]
	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_gen_registroEnviosXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_registroEnviosXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_gen_registroEnviosXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_gen_registroEnvio]
	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_gen_registroEnviosXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_registroEnviosXusuario_creo_m]
GO
CREATE PROCEDURE [sel_gen_registroEnviosXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_gen_registroEnvio]
	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_gen_registroEnviosXenvioCorreo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_registroEnviosXenvioCorreo_m]
GO
CREATE PROCEDURE [sel_gen_registroEnviosXenvioCorreo_m]
	@p_id_envioCorreo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_gen_registroEnvio]
	WHERE ([id_envioCorreo]= @p_id_envioCorreo)
	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_gen_registroEnviosXregistroEnvio_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_registroEnviosXregistroEnvio_m]
GO
CREATE PROCEDURE [sel_gen_registroEnviosXregistroEnvio_m]
	@p_id_registroEnvio INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_gen_registroEnvio]
	WHERE ([id_registroEnvio]= @p_id_registroEnvio)
	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_gen_registroEnvios_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_registroEnvios_m]
GO
CREATE PROCEDURE [sel_gen_registroEnvios_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_gen_registroEnvio]
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_gen_registroEnvios_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_gen_registroEnvios_m]
GO
CREATE PROCEDURE [lis_gen_registroEnvios_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[registroEnvio] AS [gen_registroEnvio]
	FROM [gen_registroEnvio]
	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_gen_registroEnvio]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_gen_registroEnvio]
GO
CREATE PROCEDURE [aud_gen_registroEnvio]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [gen_registroEnvio].id as id_tabla,
	[gen_registroEnvio].id_usuario_modifico as id_usuario_modifico,
	[gen_registroEnvio].id_usuario_creo as id_usuario_creo,
	[gen_registroEnvio].fechaModificacion as fechaModificacion,
	[gen_registroEnvio].fechaCreacion as fechaCreacion,
	[gen_registroEnvio].active as active,
	[gen_registroEnvio].[registroEnvio] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [gen_registroEnvio]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[gen_registroEnvio].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[gen_registroEnvio].[id_usuario_creo]
	where ' + @p_filtro)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'gen_registroEnvio'
-----------------------------------3/08/2017 4:05:56 p. m.