

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_firmaContacto]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_gen_firmaContacto]
GO
CREATE PROCEDURE [reg_gen_firmaContacto]
	@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],
		[firmaContacto] AS [firmaContacto],
		[firmaContacto_codigo] AS [firmaContacto_codigo],
		[id_contacto] AS [id_contacto],
		[codigoacceso] AS [codigoacceso],
		[archivobinario] AS [archivobinario]
	FROM [gen_firmaContacto]
	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_firmaContacto]')) DROP VIEW [dbo].[view_gen_firmaContacto]
GO
CREATE  VIEW [view_gen_firmaContacto] AS
SELECT 	[gen_firmaContacto].[id] AS [id],
		[gen_firmaContacto].[uid] AS [uid],
		[gen_firmaContacto].[eid] AS [eid],
		[gen_firmaContacto].[id_usuario_modifico] AS [id_usuario_modifico],
		[gen_firmaContacto].[id_usuario_creo] AS [id_usuario_creo],
		[gen_firmaContacto].[fechaModificacion] AS [fechaModificacion],
		[gen_firmaContacto].[fechaCreacion] AS [fechaCreacion],
		[gen_firmaContacto].[active] AS [active],
		[gen_firmaContacto].[firmaContacto] AS [firmaContacto],
		[gen_firmaContacto].[firmaContacto_codigo] AS [firmaContacto_codigo],
		[gen_firmaContacto].[id_contacto] AS [id_contacto],
[ter_contacto].[contacto] as [ter_contacto_contacto],
[ter_contacto].[contacto_codigo] as [ter_contacto_contacto_codigo],
[ter_contacto].[telefono] as [ter_contacto_telefono],
[ter_contacto].[Fax] as [ter_contacto_Fax],
[ter_contacto].[Email] as [ter_contacto_Email],
[ter_contacto].[Direccion] as [ter_contacto_Direccion],
[ter_contacto].[telefonoMovil] as [ter_contacto_telefonoMovil],
[ter_contacto].[id_tercero] as [ter_contacto_id_tercero],
[ter_contacto].[id_usuario] as [ter_contacto_id_usuario],
[ter_contacto].[cargo] as [ter_contacto_cargo],
[ter_contacto].[codigoAcceso] as [ter_contacto_codigoAcceso],
[ter_contacto].[id_cargoContacto] as [ter_contacto_id_cargoContacto],
[ter_contacto].[accesoActivo] as [ter_contacto_accesoActivo],
		[gen_firmaContacto].[codigoacceso] AS [codigoacceso],
		[gen_firmaContacto].[archivobinario] AS [archivobinario],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [gen_firmaContacto]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[gen_firmaContacto].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[gen_firmaContacto].[id_usuario_creo]
	INNER JOIN [ter_contacto] ON [ter_contacto].id=[gen_firmaContacto].[id_contacto]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [gen_firmaContacto].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [gen_firmaContacto].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_firmaContactosXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_firmaContactosXusuario_modifico]
GO
CREATE PROCEDURE [sel_gen_firmaContactosXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_gen_firmaContacto]
	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_firmaContactosXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_firmaContactosXusuario_creo]
GO
CREATE PROCEDURE [sel_gen_firmaContactosXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_gen_firmaContacto]
	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_firmaContactosXcontacto]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_firmaContactosXcontacto]
GO
CREATE PROCEDURE [sel_gen_firmaContactosXcontacto]
	@p_id_contacto INT
AS
BEGIN
	SELECT * 
	FROM [view_gen_firmaContacto]
	WHERE ([id_contacto]= @p_id_contacto)
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_firmaContactos]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_firmaContactos]
GO
CREATE PROCEDURE [sel_gen_firmaContactos]
	
	@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_firmaContacto]
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_firmaContactos]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_gen_firmaContactos]
GO
CREATE PROCEDURE [lis_gen_firmaContactos]
	AS
BEGIN
	SELECT id AS id,
[firmaContacto] AS [gen_firmaContacto]
	FROM [gen_firmaContacto]
	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_firmaContacto]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_gen_firmaContacto]
GO
CREATE PROCEDURE [upd_gen_firmaContacto]
	@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_firmaContacto varchar(300) = null,
		@p_firmaContacto_codigo varchar(100) = null,
		@p_id_contacto int = null,
		@p_codigoacceso varchar(100) = null,
		@p_archivobinario image = null
AS
BEGIN
		UPDATE [gen_firmaContacto]
		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(),
			[firmaContacto] = isnull(@p_firmaContacto,[firmaContacto]),
			[firmaContacto_codigo] = isnull(@p_firmaContacto_codigo,[firmaContacto_codigo]),
			[id_contacto] = isnull(@p_id_contacto,[id_contacto]),
			[codigoacceso] = isnull(@p_codigoacceso,[codigoacceso]),
			[archivobinario] = isnull(@p_archivobinario,[archivobinario])
		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_firmaContacto]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_gen_firmaContacto]
GO
CREATE PROCEDURE [ins_gen_firmaContacto]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_firmaContacto varchar(300),
		@p_firmaContacto_codigo varchar(100),
		@p_id_contacto int,
		@p_codigoacceso varchar(100),
		@p_archivobinario image
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [gen_firmaContacto]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[firmaContacto],
			[firmaContacto_codigo],
			[id_contacto],
			[codigoacceso],
			[archivobinario])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_firmaContacto,
			@p_firmaContacto_codigo,
			@p_id_contacto,
			@p_codigoacceso,
			@p_archivobinario)
		SET @v_id = scope_identity()
UPDATE [gen_firmaContacto] 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_firmaContacto]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_gen_firmaContacto]
GO
CREATE PROCEDURE [act_gen_firmaContacto]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [gen_firmaContacto]
	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_firmaContacto_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_gen_firmaContacto_m]
GO
CREATE PROCEDURE [reg_gen_firmaContacto_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],
		[firmaContacto] AS [firmaContacto],
		[firmaContacto_codigo] AS [firmaContacto_codigo],
		[id_contacto] AS [id_contacto],
		[codigoacceso] AS [codigoacceso],
		[archivobinario] AS [archivobinario]
	FROM [gen_firmaContacto]
	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_firmaContactosXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_firmaContactosXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_gen_firmaContactosXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_gen_firmaContacto]
	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_firmaContactosXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_firmaContactosXusuario_creo_m]
GO
CREATE PROCEDURE [sel_gen_firmaContactosXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_gen_firmaContacto]
	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_firmaContactosXcontacto_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_firmaContactosXcontacto_m]
GO
CREATE PROCEDURE [sel_gen_firmaContactosXcontacto_m]
	@p_id_contacto INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_gen_firmaContacto]
	WHERE ([id_contacto]= @p_id_contacto)
	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_firmaContactos_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_firmaContactos_m]
GO
CREATE PROCEDURE [sel_gen_firmaContactos_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_firmaContacto]
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_firmaContactos_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_gen_firmaContactos_m]
GO
CREATE PROCEDURE [lis_gen_firmaContactos_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[firmaContacto] AS [gen_firmaContacto]
	FROM [gen_firmaContacto]
	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_firmaContacto]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_gen_firmaContacto]
GO
CREATE PROCEDURE [aud_gen_firmaContacto]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [gen_firmaContacto].id as id_tabla,
	[gen_firmaContacto].id_usuario_modifico as id_usuario_modifico,
	[gen_firmaContacto].id_usuario_creo as id_usuario_creo,
	[gen_firmaContacto].fechaModificacion as fechaModificacion,
	[gen_firmaContacto].fechaCreacion as fechaCreacion,
	[gen_firmaContacto].active as active,
	[gen_firmaContacto].[firmaContacto] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [gen_firmaContacto]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[gen_firmaContacto].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[gen_firmaContacto].[id_usuario_creo]
	where ' + @p_filtro)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'gen_firmaContacto'
-----------------------------------22/06/2015 16:53:16