

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_ter_contacto]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_ter_contacto]
GO
CREATE PROCEDURE [reg_ter_contacto]
	@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],
		[contacto] AS [contacto],
		[contacto_codigo] AS [contacto_codigo],
		[telefono] AS [telefono],
		[Fax] AS [Fax],
		[Email] AS [Email],
		[Direccion] AS [Direccion],
		[telefonoMovil] AS [telefonoMovil],
		[id_tercero] AS [id_tercero],
		[id_usuario] AS [id_usuario],
		[cargo] AS [cargo],
		[codigoAcceso] AS [codigoAcceso],
		[id_cargoContacto] AS [id_cargoContacto],
		[accesoActivo] AS [accesoActivo]
	FROM [ter_contacto]
	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_ter_contacto]')) DROP VIEW [dbo].[view_ter_contacto]
GO
CREATE  VIEW [view_ter_contacto] AS
SELECT 	[ter_contacto].[id] AS [id],
		[ter_contacto].[uid] AS [uid],
		[ter_contacto].[eid] AS [eid],
		[ter_contacto].[id_usuario_modifico] AS [id_usuario_modifico],
		[ter_contacto].[id_usuario_creo] AS [id_usuario_creo],
		[ter_contacto].[fechaModificacion] AS [fechaModificacion],
		[ter_contacto].[fechaCreacion] AS [fechaCreacion],
		[ter_contacto].[active] AS [active],
		[ter_contacto].[contacto] AS [contacto],
		[ter_contacto].[contacto_codigo] AS [contacto_codigo],
		[ter_contacto].[telefono] AS [telefono],
		[ter_contacto].[Fax] AS [Fax],
		[ter_contacto].[Email] AS [Email],
		[ter_contacto].[Direccion] AS [Direccion],
		[ter_contacto].[telefonoMovil] AS [telefonoMovil],
		[ter_contacto].[id_tercero] AS [id_tercero],
[ter_tercero].[tercero] as [ter_tercero_tercero],
[ter_tercero].[tercero_codigo] as [ter_tercero_tercero_codigo],
[ter_tercero].[tercero_nit] as [ter_tercero_tercero_nit],
[ter_tercero].[direccion] as [ter_tercero_direccion],
[ter_tercero].[contacto] as [ter_tercero_contacto],
[ter_tercero].[telefono] as [ter_tercero_telefono],
[ter_tercero].[fax] as [ter_tercero_fax],
[ter_tercero].[email] as [ter_tercero_email],
[ter_tercero].[website] as [ter_tercero_website],
[ter_tercero].[representante] as [ter_tercero_representante],
[ter_tercero].[notas] as [ter_tercero_notas],
[ter_tercero].[tercero_nombreCorto] as [ter_tercero_tercero_nombreCorto],
[ter_tercero].[esCliente] as [ter_tercero_esCliente],
[ter_tercero].[esProveedor] as [ter_tercero_esProveedor],
[ter_tercero].[esTransportador] as [ter_tercero_esTransportador],
[ter_tercero].[esFabricante] as [ter_tercero_esFabricante],
[ter_tercero].[esEmpresaPropia] as [ter_tercero_esEmpresaPropia],
[ter_tercero].[clasificacion] as [ter_tercero_clasificacion],
[ter_tercero].[id_zona] as [ter_tercero_id_zona],
[ter_tercero].[id_estadoTercero] as [ter_tercero_id_estadoTercero],
[ter_tercero].[id_naturalezaTercero] as [ter_tercero_id_naturalezaTercero],
[ter_tercero].[esProspecto] as [ter_tercero_esProspecto],
		[ter_contacto].[id_usuario] AS [id_usuario],
[seg_usuario].[usuario] as [seg_usuario_usuario],
[seg_usuario].[usuario_codigo] as [seg_usuario_usuario_codigo],
[seg_usuario].[intentosFallidos] as [seg_usuario_intentosFallidos],
[seg_usuario].[numeroIngresos] as [seg_usuario_numeroIngresos],
[seg_usuario].[fechaUltimoIngreso_fh] as [seg_usuario_fechaUltimoIngreso_fh],
[seg_usuario].[fechaIntentoFallido_fh] as [seg_usuario_fechaIntentoFallido_fh],
[seg_usuario].[clave] as [seg_usuario_clave],
[seg_usuario].[nombre] as [seg_usuario_nombre],
[seg_usuario].[profesion] as [seg_usuario_profesion],
[seg_usuario].[email] as [seg_usuario_email],
[seg_usuario].[direccion] as [seg_usuario_direccion],
[seg_usuario].[telefono] as [seg_usuario_telefono],
[seg_usuario].[costoHora] as [seg_usuario_costoHora],
[seg_usuario].[porcentajeComision] as [seg_usuario_porcentajeComision],
[seg_usuario].[accesoActivo] as [seg_usuario_accesoActivo],
[seg_usuario].[cambiarClave] as [seg_usuario_cambiarClave],
[seg_usuario].[fechaUltimoCambioClave_fh] as [seg_usuario_fechaUltimoCambioClave_fh],
[seg_usuario].[aplicaRecargo] as [seg_usuario_aplicaRecargo],
[seg_usuario].[sexo] as [seg_usuario_sexo],
[seg_usuario].[id_perfil] as [seg_usuario_id_perfil],
[seg_usuario].[id_cargo] as [seg_usuario_id_cargo],
[seg_usuario].[id_grupo] as [seg_usuario_id_grupo],
[seg_usuario].[id_zona] as [seg_usuario_id_zona],
[seg_usuario].[identificacion] as [seg_usuario_identificacion],
[seg_usuario].[id_usuario] as [seg_usuario_id_usuario],
[seg_usuario].[preferencias] as [seg_usuario_preferencias],
[seg_usuario].[habilitado] as [seg_usuario_habilitado],
		[ter_contacto].[cargo] AS [cargo],
		[ter_contacto].[codigoAcceso] AS [codigoAcceso],
		[ter_contacto].[id_cargoContacto] AS [id_cargoContacto],
[ter_cargoContacto].[cargoContacto] as [ter_cargoContacto_cargoContacto],
[ter_cargoContacto].[cargoContacto_codigo] as [ter_cargoContacto_cargoContacto_codigo],
		[ter_contacto].[accesoActivo] AS [accesoActivo],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [ter_contacto]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[ter_contacto].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[ter_contacto].[id_usuario_creo]
	INNER JOIN [ter_tercero] ON [ter_tercero].id=[ter_contacto].[id_tercero]
	INNER JOIN [seg_usuario] ON [seg_usuario].id=[ter_contacto].[id_usuario]
	INNER JOIN [ter_cargoContacto] ON [ter_cargoContacto].id=[ter_contacto].[id_cargoContacto]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [ter_contacto].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [ter_contacto].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_ter_contactosXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_contactosXusuario_modifico]
GO
CREATE PROCEDURE [sel_ter_contactosXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_ter_contacto]
	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_ter_contactosXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_contactosXusuario_creo]
GO
CREATE PROCEDURE [sel_ter_contactosXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_ter_contacto]
	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_ter_contactosXtercero]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_contactosXtercero]
GO
CREATE PROCEDURE [sel_ter_contactosXtercero]
	@p_id_tercero INT
AS
BEGIN
	SELECT * 
	FROM [view_ter_contacto]
	WHERE ([id_tercero]= @p_id_tercero)
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_ter_contactosXusuario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_contactosXusuario]
GO
CREATE PROCEDURE [sel_ter_contactosXusuario]
	@p_id_usuario INT
AS
BEGIN
	SELECT * 
	FROM [view_ter_contacto]
	WHERE ([id_usuario]= @p_id_usuario)
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_ter_contactosXcargoContacto]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_contactosXcargoContacto]
GO
CREATE PROCEDURE [sel_ter_contactosXcargoContacto]
	@p_id_cargoContacto INT
AS
BEGIN
	SELECT * 
	FROM [view_ter_contacto]
	WHERE ([id_cargoContacto]= @p_id_cargoContacto)
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_ter_contactos]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_contactos]
GO
CREATE PROCEDURE [sel_ter_contactos]
	
	@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_ter_contacto]
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_ter_contactos]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_ter_contactos]
GO
CREATE PROCEDURE [lis_ter_contactos]
	AS
BEGIN
	SELECT id AS id,
[contacto] AS [ter_contacto]
	FROM [ter_contacto]
	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_ter_contacto]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_ter_contacto]
GO
CREATE PROCEDURE [upd_ter_contacto]
	@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_contacto varchar(300) = null,
		@p_contacto_codigo varchar(100) = null,
		@p_telefono varchar(100) = null,
		@p_Fax varchar(100) = null,
		@p_Email varchar(100) = null,
		@p_Direccion varchar(300) = null,
		@p_telefonoMovil varchar(100) = null,
		@p_id_tercero int = null,
		@p_id_usuario int = null,
		@p_cargo nvarchar(100) = null,
		@p_codigoAcceso varchar(300) = null,
		@p_id_cargoContacto int = null,
		@p_accesoActivo bit = null
AS
BEGIN
		UPDATE [ter_contacto]
		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(),
			[contacto] = isnull(@p_contacto,[contacto]),
			[contacto_codigo] = isnull(@p_contacto_codigo,[contacto_codigo]),
			[telefono] = isnull(@p_telefono,[telefono]),
			[Fax] = isnull(@p_Fax,[Fax]),
			[Email] = isnull(@p_Email,[Email]),
			[Direccion] = isnull(@p_Direccion,[Direccion]),
			[telefonoMovil] = isnull(@p_telefonoMovil,[telefonoMovil]),
			[id_tercero] = isnull(@p_id_tercero,[id_tercero]),
			[id_usuario] = isnull(@p_id_usuario,[id_usuario]),
			[cargo] = isnull(@p_cargo,[cargo]),
			[codigoAcceso] = isnull(@p_codigoAcceso,[codigoAcceso]),
			[id_cargoContacto] = isnull(@p_id_cargoContacto,[id_cargoContacto]),
			[accesoActivo] = isnull(@p_accesoActivo,[accesoActivo])
		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_ter_contacto]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_ter_contacto]
GO
CREATE PROCEDURE [ins_ter_contacto]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_contacto varchar(300),
		@p_contacto_codigo varchar(100),
		@p_telefono varchar(100),
		@p_Fax varchar(100),
		@p_Email varchar(100),
		@p_Direccion varchar(300),
		@p_telefonoMovil varchar(100),
		@p_id_tercero int,
		@p_id_usuario int,
		@p_cargo nvarchar(100),
		@p_codigoAcceso varchar(300),
		@p_id_cargoContacto int,
		@p_accesoActivo bit
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [ter_contacto]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[contacto],
			[contacto_codigo],
			[telefono],
			[Fax],
			[Email],
			[Direccion],
			[telefonoMovil],
			[id_tercero],
			[id_usuario],
			[cargo],
			[codigoAcceso],
			[id_cargoContacto],
			[accesoActivo])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_contacto,
			@p_contacto_codigo,
			@p_telefono,
			@p_Fax,
			@p_Email,
			@p_Direccion,
			@p_telefonoMovil,
			@p_id_tercero,
			@p_id_usuario,
			@p_cargo,
			@p_codigoAcceso,
			@p_id_cargoContacto,
			@p_accesoActivo)
		SET @v_id = scope_identity()
UPDATE [ter_contacto] 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_ter_contacto]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_ter_contacto]
GO
CREATE PROCEDURE [act_ter_contacto]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [ter_contacto]
	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_ter_contacto_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_ter_contacto_m]
GO
CREATE PROCEDURE [reg_ter_contacto_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],
		[contacto] AS [contacto],
		[contacto_codigo] AS [contacto_codigo],
		[telefono] AS [telefono],
		[Fax] AS [Fax],
		[Email] AS [Email],
		[Direccion] AS [Direccion],
		[telefonoMovil] AS [telefonoMovil],
		[id_tercero] AS [id_tercero],
		[id_usuario] AS [id_usuario],
		[cargo] AS [cargo],
		[codigoAcceso] AS [codigoAcceso],
		[id_cargoContacto] AS [id_cargoContacto],
		[accesoActivo] AS [accesoActivo]
	FROM [ter_contacto]
	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_ter_contactosXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_contactosXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_ter_contactosXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ter_contacto]
	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_ter_contactosXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_contactosXusuario_creo_m]
GO
CREATE PROCEDURE [sel_ter_contactosXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ter_contacto]
	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_ter_contactosXtercero_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_contactosXtercero_m]
GO
CREATE PROCEDURE [sel_ter_contactosXtercero_m]
	@p_id_tercero INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ter_contacto]
	WHERE ([id_tercero]= @p_id_tercero)
	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_ter_contactosXusuario_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_contactosXusuario_m]
GO
CREATE PROCEDURE [sel_ter_contactosXusuario_m]
	@p_id_usuario INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ter_contacto]
	WHERE ([id_usuario]= @p_id_usuario)
	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_ter_contactosXcargoContacto_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_contactosXcargoContacto_m]
GO
CREATE PROCEDURE [sel_ter_contactosXcargoContacto_m]
	@p_id_cargoContacto INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ter_contacto]
	WHERE ([id_cargoContacto]= @p_id_cargoContacto)
	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_ter_contactos_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_contactos_m]
GO
CREATE PROCEDURE [sel_ter_contactos_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_ter_contacto]
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_ter_contactos_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_ter_contactos_m]
GO
CREATE PROCEDURE [lis_ter_contactos_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[contacto] AS [ter_contacto]
	FROM [ter_contacto]
	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_ter_contacto]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_ter_contacto]
GO
CREATE PROCEDURE [aud_ter_contacto]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [ter_contacto].id as id_tabla,
	[ter_contacto].id_usuario_modifico as id_usuario_modifico,
	[ter_contacto].id_usuario_creo as id_usuario_creo,
	[ter_contacto].fechaModificacion as fechaModificacion,
	[ter_contacto].fechaCreacion as fechaCreacion,
	[ter_contacto].active as active,
	[ter_contacto].[contacto] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [ter_contacto]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[ter_contacto].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[ter_contacto].[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 OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sel_doc_documentosXcontacto]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_documentosXcontacto]
GO
CREATE PROCEDURE [sel_doc_documentosXcontacto]
	@p_id_ter_contacto INT
AS
BEGIN
	SELECT 	[doc_documento_contacto].[id] AS [I_id],
		[doc_documento_contacto].[uid] AS [I_uid],
		[doc_documento_contacto].[eid] AS [I_eid],
		[doc_documento_contacto].[fechaModificacion] AS [I_fechaModificacion],
		[doc_documento_contacto].[fechaCreacion] AS [I_fechaCreacion],
		[doc_documento_contacto].[active] AS [I_active],
		[doc_documento_contacto].[documento_contacto] AS [I_documento_contacto]
	FROM doc_documento_contacto INNER JOIN doc_documento
	 ON [doc_documento_contacto].[id_documento] = [doc_documento].id
	WHERE	([doc_documento_contacto].[id_contacto]= @p_id_ter_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].[ins_doc_documento_contacto]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_doc_documento_contacto]
GO
CREATE PROCEDURE [ins_doc_documento_contacto]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_documento_contacto varchar(300),
		@p_id_documento int,
		@p_id_contacto int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [doc_documento_contacto]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[documento_contacto],
			[id_documento],
			[id_contacto])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_documento_contacto,
			@p_id_documento,
			@p_id_contacto)
		SET @v_id = scope_identity()
UPDATE [doc_documento_contacto] 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].[upd_doc_documento_contacto]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_doc_documento_contacto]
GO
CREATE PROCEDURE [upd_doc_documento_contacto]
	@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_documento_contacto varchar(300) = null,
		@p_id_documento int = null,
		@p_id_contacto int = null
AS
BEGIN
		UPDATE [doc_documento_contacto]
		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(),
			[documento_contacto] = isnull(@p_documento_contacto,[documento_contacto]),
			[id_documento] = isnull(@p_id_documento,[id_documento]),
			[id_contacto] = isnull(@p_id_contacto,[id_contacto])
		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].[sel_pro_rolesXcontacto]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_rolesXcontacto]
GO
CREATE PROCEDURE [sel_pro_rolesXcontacto]
	@p_id_ter_contacto INT
AS
BEGIN
	SELECT 	[pro_rol_contacto].[id] AS [I_id],
		[pro_rol_contacto].[uid] AS [I_uid],
		[pro_rol_contacto].[eid] AS [I_eid],
		[pro_rol_contacto].[fechaModificacion] AS [I_fechaModificacion],
		[pro_rol_contacto].[fechaCreacion] AS [I_fechaCreacion],
		[pro_rol_contacto].[active] AS [I_active],
		[pro_rol_contacto].[rol_contacto] AS [I_rol_contacto]
	FROM pro_rol_contacto INNER JOIN pro_rol
	 ON [pro_rol_contacto].[id_rol] = [pro_rol].id
	WHERE	([pro_rol_contacto].[id_contacto]= @p_id_ter_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].[ins_pro_rol_contacto]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_pro_rol_contacto]
GO
CREATE PROCEDURE [ins_pro_rol_contacto]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_rol_contacto varchar(300),
		@p_id_rol int,
		@p_id_contacto int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [pro_rol_contacto]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[rol_contacto],
			[id_rol],
			[id_contacto])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_rol_contacto,
			@p_id_rol,
			@p_id_contacto)
		SET @v_id = scope_identity()
UPDATE [pro_rol_contacto] 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].[upd_pro_rol_contacto]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_pro_rol_contacto]
GO
CREATE PROCEDURE [upd_pro_rol_contacto]
	@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_rol_contacto varchar(300) = null,
		@p_id_rol int = null,
		@p_id_contacto int = null
AS
BEGIN
		UPDATE [pro_rol_contacto]
		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(),
			[rol_contacto] = isnull(@p_rol_contacto,[rol_contacto]),
			[id_rol] = isnull(@p_id_rol,[id_rol]),
			[id_contacto] = isnull(@p_id_contacto,[id_contacto])
		WHERE (id = @p_id)
END

