IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_doc_documento_contacto]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_doc_documento_contacto]
GO
CREATE PROCEDURE [reg_doc_documento_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],
		[documento_contacto] AS [documento_contacto],
		[id_documento] AS [id_documento],
		[id_contacto] AS [id_contacto]
	FROM [doc_documento_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_doc_documento_contacto]')) DROP VIEW [dbo].[view_doc_documento_contacto]
GO
CREATE  VIEW [view_doc_documento_contacto] AS
SELECT 	[doc_documento_contacto].[id] AS [id],
		[doc_documento_contacto].[uid] AS [uid],
		[doc_documento_contacto].[eid] AS [eid],
		[doc_documento_contacto].[id_usuario_modifico] AS [id_usuario_modifico],
		[doc_documento_contacto].[id_usuario_creo] AS [id_usuario_creo],
		[doc_documento_contacto].[fechaModificacion] AS [fechaModificacion],
		[doc_documento_contacto].[fechaCreacion] AS [fechaCreacion],
		[doc_documento_contacto].[active] AS [active],
		[doc_documento_contacto].[documento_contacto] AS [documento_contacto],
		[doc_documento_contacto].[id_documento] AS [id_documento],
[doc_documento].[documento] as [doc_documento_documento],
[doc_documento].[documento_codigo] as [doc_documento_documento_codigo],
[doc_documento].[documento_numero] as [doc_documento_documento_numero],
[doc_documento].[prefijo] as [doc_documento_prefijo],
[doc_documento].[subtotal] as [doc_documento_subtotal],
[doc_documento].[descuento] as [doc_documento_descuento],
[doc_documento].[iva] as [doc_documento_iva],
[doc_documento].[total] as [doc_documento_total],
[doc_documento].[costo] as [doc_documento_costo],
[doc_documento].[rentabilidad] as [doc_documento_rentabilidad],
[doc_documento].[trm] as [doc_documento_trm],
[doc_documento].[fecha_fh] as [doc_documento_fecha_fh],
[doc_documento].[fechaSugerida_fh] as [doc_documento_fechaSugerida_fh],
[doc_documento].[fechaCierre_ff] as [doc_documento_fechaCierre_ff],
[doc_documento].[fechaLimite_fh] as [doc_documento_fechaLimite_fh],
[doc_documento].[id_subtipoDocumento] as [doc_documento_id_subtipoDocumento],
[doc_documento].[id_tercero_cliente] as [doc_documento_id_tercero_cliente],
[doc_documento].[id_documento] as [doc_documento_id_documento],
[doc_documento].[id_tercero_proveedor] as [doc_documento_id_tercero_proveedor],
[doc_documento].[id_prioridadDocumento] as [doc_documento_id_prioridadDocumento],
[doc_documento].[id_moneda] as [doc_documento_id_moneda],
[doc_documento].[id_estadoTipoDocumento] as [doc_documento_id_estadoTipoDocumento],
[doc_documento].[id_motivoestado] as [doc_documento_id_motivoestado],
[doc_documento].[subtotalPlaneado] as [doc_documento_subtotalPlaneado],
[doc_documento].[descuentoPlaneado] as [doc_documento_descuentoPlaneado],
[doc_documento].[ivaPlaneado] as [doc_documento_ivaPlaneado],
[doc_documento].[totalPlaneado] as [doc_documento_totalPlaneado],
[doc_documento].[costoPlaneado] as [doc_documento_costoPlaneado],
[doc_documento].[id_usuario_asignado] as [doc_documento_id_usuario_asignado],
		[doc_documento_contacto].[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],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [doc_documento_contacto]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[doc_documento_contacto].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[doc_documento_contacto].[id_usuario_creo]
	INNER JOIN [doc_documento] ON [doc_documento].id=[doc_documento_contacto].[id_documento]
	INNER JOIN [ter_contacto] ON [ter_contacto].id=[doc_documento_contacto].[id_contacto]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [doc_documento_contacto].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

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sel_doc_documento_contactosXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_documento_contactosXusuario_modifico]
GO
CREATE PROCEDURE [sel_doc_documento_contactosXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_doc_documento_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_doc_documento_contactosXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_documento_contactosXusuario_creo]
GO
CREATE PROCEDURE [sel_doc_documento_contactosXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_doc_documento_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_doc_documento_contactosXdocumento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_documento_contactosXdocumento]
GO
CREATE PROCEDURE [sel_doc_documento_contactosXdocumento]
	@p_id_documento INT
AS
BEGIN
	SELECT * 
	FROM [view_doc_documento_contacto]
	WHERE ([id_documento]= @p_id_documento)
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_documento_contactosXcontacto]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_documento_contactosXcontacto]
GO
CREATE PROCEDURE [sel_doc_documento_contactosXcontacto]
	@p_id_contacto INT
AS
BEGIN
	SELECT * 
	FROM [view_doc_documento_contacto]
	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_doc_documento_contactos]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_documento_contactos]
GO
CREATE PROCEDURE [sel_doc_documento_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_doc_documento_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_doc_documento_contactos]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_doc_documento_contactos]
GO
CREATE PROCEDURE [lis_doc_documento_contactos]
	AS
BEGIN
	SELECT id AS id,
[documento_contacto] AS [doc_documento_contacto]
	FROM [doc_documento_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_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].[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].[act_doc_documento_contacto]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_doc_documento_contacto]
GO
CREATE PROCEDURE [act_doc_documento_contacto]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [doc_documento_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_doc_documento_contacto_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_doc_documento_contacto_m]
GO
CREATE PROCEDURE [reg_doc_documento_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],
		[documento_contacto] AS [documento_contacto],
		[id_documento] AS [id_documento],
		[id_contacto] AS [id_contacto]
	FROM [doc_documento_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_doc_documento_contactosXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_documento_contactosXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_doc_documento_contactosXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_doc_documento_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_doc_documento_contactosXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_documento_contactosXusuario_creo_m]
GO
CREATE PROCEDURE [sel_doc_documento_contactosXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_doc_documento_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_doc_documento_contactosXdocumento_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_documento_contactosXdocumento_m]
GO
CREATE PROCEDURE [sel_doc_documento_contactosXdocumento_m]
	@p_id_documento INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_doc_documento_contacto]
	WHERE ([id_documento]= @p_id_documento)
	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_doc_documento_contactosXcontacto_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_documento_contactosXcontacto_m]
GO
CREATE PROCEDURE [sel_doc_documento_contactosXcontacto_m]
	@p_id_contacto INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_doc_documento_contacto]
	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_doc_documento_contactos_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_documento_contactos_m]
GO
CREATE PROCEDURE [sel_doc_documento_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_doc_documento_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_doc_documento_contactos_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_doc_documento_contactos_m]
GO
CREATE PROCEDURE [lis_doc_documento_contactos_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[documento_contacto] AS [doc_documento_contacto]
	FROM [doc_documento_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_doc_documento_contacto]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_doc_documento_contacto]
GO
CREATE PROCEDURE [aud_doc_documento_contacto]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [doc_documento_contacto].id as id_tabla,
	[doc_documento_contacto].id_usuario_modifico as id_usuario_modifico,
	[doc_documento_contacto].id_usuario_creo as id_usuario_creo,
	[doc_documento_contacto].fechaModificacion as fechaModificacion,
	[doc_documento_contacto].fechaCreacion as fechaCreacion,
	[doc_documento_contacto].active as active,
	[doc_documento_contacto].[documento_contacto] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [doc_documento_contacto]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[doc_documento_contacto].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[doc_documento_contacto].[id_usuario_creo]
	where ' + @p_filtro)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'doc_documento_contacto'
-----------------------------------30/08/2017 14:33:34