
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_ter_tercero]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_ter_tercero]
GO
CREATE PROCEDURE [reg_ter_tercero]
	@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],
		[tercero] AS [tercero],
		[tercero_codigo] AS [tercero_codigo],
		[tercero_nit] AS [tercero_nit],
		[direccion] AS [direccion],
		[contacto] AS [contacto],
		[telefono] AS [telefono],
		[fax] AS [fax],
		[email] AS [email],
		[website] AS [website],
		[representante] AS [representante],
		[notas] AS [notas],
		[tercero_nombreCorto] AS [tercero_nombreCorto],
		[esCliente] AS [esCliente],
		[esProveedor] AS [esProveedor],
		[esTransportador] AS [esTransportador],
		[esFabricante] AS [esFabricante],
		[esEmpresaPropia] AS [esEmpresaPropia],
		[clasificacion] AS [clasificacion],
		[id_zona] AS [id_zona],
		[id_estadoTercero] AS [id_estadoTercero],
		[id_naturalezaTercero] AS [id_naturalezaTercero],
		[esProspecto] AS [esProspecto],
		[tiempoEntrega] AS [tiempoEntrega],
		[id_formaDePago] AS [id_formaDePago]
	FROM [ter_tercero]
	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_tercero]')) DROP VIEW [dbo].[view_ter_tercero]
GO
CREATE  VIEW [view_ter_tercero] AS
SELECT 	[ter_tercero].[id] AS [id],
		[ter_tercero].[uid] AS [uid],
		[ter_tercero].[eid] AS [eid],
		[ter_tercero].[id_usuario_modifico] AS [id_usuario_modifico],
		[ter_tercero].[id_usuario_creo] AS [id_usuario_creo],
		[ter_tercero].[fechaModificacion] AS [fechaModificacion],
		[ter_tercero].[fechaCreacion] AS [fechaCreacion],
		[ter_tercero].[active] AS [active],
		[ter_tercero].[tercero] AS [tercero],
		[ter_tercero].[tercero_codigo] AS [tercero_codigo],
		[ter_tercero].[tercero_nit] AS [tercero_nit],
		[ter_tercero].[direccion] AS [direccion],
		[ter_tercero].[contacto] AS [contacto],
		[ter_tercero].[telefono] AS [telefono],
		[ter_tercero].[fax] AS [fax],
		[ter_tercero].[email] AS [email],
		[ter_tercero].[website] AS [website],
		[ter_tercero].[representante] AS [representante],
		[ter_tercero].[notas] AS [notas],
		[ter_tercero].[tercero_nombreCorto] AS [tercero_nombreCorto],
		[ter_tercero].[esCliente] AS [esCliente],
		[ter_tercero].[esProveedor] AS [esProveedor],
		[ter_tercero].[esTransportador] AS [esTransportador],
		[ter_tercero].[esFabricante] AS [esFabricante],
		[ter_tercero].[esEmpresaPropia] AS [esEmpresaPropia],
		[ter_tercero].[clasificacion] AS [clasificacion],
		[ter_tercero].[id_zona] AS [id_zona],
[gen_zona].[zona] as [gen_zona_zona],
[gen_zona].[zona_codigo] as [gen_zona_zona_codigo],
[gen_zona].[tiempoDesplazamiento] as [gen_zona_tiempoDesplazamiento],
[gen_zona].[nivel] as [gen_zona_nivel],
[gen_zona].[id_zona] as [gen_zona_id_zona],
[gen_zona].[codigoExterno] as [gen_zona_codigoExterno],
		[ter_tercero].[id_estadoTercero] AS [id_estadoTercero],
[ter_estadoTercero].[estadoTercero] as [ter_estadoTercero_estadoTercero],
[ter_estadoTercero].[estadoTercero_codigo] as [ter_estadoTercero_estadoTercero_codigo],
		[ter_tercero].[id_naturalezaTercero] AS [id_naturalezaTercero],
[ter_naturalezaTercero].[naturalezaTercero] as [ter_naturalezaTercero_naturalezaTercero],
[ter_naturalezaTercero].[naturalezaTercero_codigo] as [ter_naturalezaTercero_naturalezaTercero_codigo],
		[ter_tercero].[esProspecto] AS [esProspecto],
		[ter_tercero].[tiempoEntrega] AS [tiempoEntrega],
		[ter_tercero].[id_formaDePago] AS [id_formaDePago],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [ter_tercero]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[ter_tercero].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[ter_tercero].[id_usuario_creo]
	INNER JOIN [gen_zona] ON [gen_zona].id=[ter_tercero].[id_zona]
	INNER JOIN [ter_estadoTercero] ON [ter_estadoTercero].id=[ter_tercero].[id_estadoTercero]
	INNER JOIN [ter_naturalezaTercero] ON [ter_naturalezaTercero].id=[ter_tercero].[id_naturalezaTercero]
	INNER JOIN [doc_formapago] ON [doc_formapago].id=[ter_tercero].[id_formaDePago]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [ter_tercero].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [ter_tercero].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_tercerosXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_tercerosXusuario_modifico]
GO
CREATE PROCEDURE [sel_ter_tercerosXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_ter_tercero]
	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_tercerosXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_tercerosXusuario_creo]
GO
CREATE PROCEDURE [sel_ter_tercerosXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_ter_tercero]
	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_tercerosXzona]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_tercerosXzona]
GO
CREATE PROCEDURE [sel_ter_tercerosXzona]
	@p_id_zona INT
AS
BEGIN
	SELECT * 
	FROM [view_ter_tercero]
	WHERE ([id_zona]= @p_id_zona)
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_tercerosXestadoTercero]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_tercerosXestadoTercero]
GO
CREATE PROCEDURE [sel_ter_tercerosXestadoTercero]
	@p_id_estadoTercero INT
AS
BEGIN
	SELECT * 
	FROM [view_ter_tercero]
	WHERE ([id_estadoTercero]= @p_id_estadoTercero)
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_tercerosXnaturalezaTercero]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_tercerosXnaturalezaTercero]
GO
CREATE PROCEDURE [sel_ter_tercerosXnaturalezaTercero]
	@p_id_naturalezaTercero INT
AS
BEGIN
	SELECT * 
	FROM [view_ter_tercero]
	WHERE ([id_naturalezaTercero]= @p_id_naturalezaTercero)
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_tercerosXformaDePago]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_tercerosXformaDePago]
GO
CREATE PROCEDURE [sel_ter_tercerosXformaDePago]
	@p_id_formaDePago INT
AS
BEGIN
	SELECT * 
	FROM [view_ter_tercero]
	WHERE ([id_formaDePago]= @p_id_formaDePago)
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_terceros]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_terceros]
GO
CREATE PROCEDURE [sel_ter_terceros]
	
	@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_tercero]
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_terceros]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_ter_terceros]
GO
CREATE PROCEDURE [lis_ter_terceros]
	AS
BEGIN
	SELECT id AS id,
[tercero] AS [ter_tercero]
	FROM [ter_tercero]
	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_tercero]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_ter_tercero]
GO
CREATE PROCEDURE [upd_ter_tercero]
	@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_tercero varchar(300) = null,
		@p_tercero_codigo varchar(100) = null,
		@p_tercero_nit varchar(100) = null,
		@p_direccion varchar(300) = null,
		@p_contacto varchar(100) = null,
		@p_telefono varchar(100) = null,
		@p_fax varchar(100) = null,
		@p_email varchar(100) = null,
		@p_website varchar(100) = null,
		@p_representante varchar(100) = null,
		@p_notas varchar(8000) = null,
		@p_tercero_nombreCorto varchar(100) = null,
		@p_esCliente bit = null,
		@p_esProveedor bit = null,
		@p_esTransportador bit = null,
		@p_esFabricante bit = null,
		@p_esEmpresaPropia bit = null,
		@p_clasificacion varchar(50) = null,
		@p_id_zona int = null,
		@p_id_estadoTercero int = null,
		@p_id_naturalezaTercero int = null,
		@p_esProspecto bit = null,
		@p_tiempoEntrega int = null,
		@p_id_formaDePago int = null
AS
BEGIN
		UPDATE [ter_tercero]
		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(),
			[tercero] = isnull(@p_tercero,[tercero]),
			[tercero_codigo] = isnull(@p_tercero_codigo,[tercero_codigo]),
			[tercero_nit] = isnull(@p_tercero_nit,[tercero_nit]),
			[direccion] = isnull(@p_direccion,[direccion]),
			[contacto] = isnull(@p_contacto,[contacto]),
			[telefono] = isnull(@p_telefono,[telefono]),
			[fax] = isnull(@p_fax,[fax]),
			[email] = isnull(@p_email,[email]),
			[website] = isnull(@p_website,[website]),
			[representante] = isnull(@p_representante,[representante]),
			[notas] = isnull(@p_notas,[notas]),
			[tercero_nombreCorto] = isnull(@p_tercero_nombreCorto,[tercero_nombreCorto]),
			[esCliente] = isnull(@p_esCliente,[esCliente]),
			[esProveedor] = isnull(@p_esProveedor,[esProveedor]),
			[esTransportador] = isnull(@p_esTransportador,[esTransportador]),
			[esFabricante] = isnull(@p_esFabricante,[esFabricante]),
			[esEmpresaPropia] = isnull(@p_esEmpresaPropia,[esEmpresaPropia]),
			[clasificacion] = isnull(@p_clasificacion,[clasificacion]),
			[id_zona] = isnull(@p_id_zona,[id_zona]),
			[id_estadoTercero] = isnull(@p_id_estadoTercero,[id_estadoTercero]),
			[id_naturalezaTercero] = isnull(@p_id_naturalezaTercero,[id_naturalezaTercero]),
			[esProspecto] = isnull(@p_esProspecto,[esProspecto]),
			[tiempoEntrega] = isnull(@p_tiempoEntrega,[tiempoEntrega]),
			[id_formaDePago] = isnull(@p_id_formaDePago,[id_formaDePago])
		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_tercero]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_ter_tercero]
GO
CREATE PROCEDURE [ins_ter_tercero]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_tercero varchar(300),
		@p_tercero_codigo varchar(100),
		@p_tercero_nit varchar(100),
		@p_direccion varchar(300),
		@p_contacto varchar(100),
		@p_telefono varchar(100),
		@p_fax varchar(100),
		@p_email varchar(100),
		@p_website varchar(100),
		@p_representante varchar(100),
		@p_notas varchar(8000),
		@p_tercero_nombreCorto varchar(100),
		@p_esCliente bit,
		@p_esProveedor bit,
		@p_esTransportador bit,
		@p_esFabricante bit,
		@p_esEmpresaPropia bit,
		@p_clasificacion varchar(50),
		@p_id_zona int,
		@p_id_estadoTercero int,
		@p_id_naturalezaTercero int,
		@p_esProspecto bit,
		@p_tiempoEntrega int,
		@p_id_formaDePago int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [ter_tercero]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[tercero],
			[tercero_codigo],
			[tercero_nit],
			[direccion],
			[contacto],
			[telefono],
			[fax],
			[email],
			[website],
			[representante],
			[notas],
			[tercero_nombreCorto],
			[esCliente],
			[esProveedor],
			[esTransportador],
			[esFabricante],
			[esEmpresaPropia],
			[clasificacion],
			[id_zona],
			[id_estadoTercero],
			[id_naturalezaTercero],
			[esProspecto],
			[tiempoEntrega],
			[id_formaDePago])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_tercero,
			@p_tercero_codigo,
			@p_tercero_nit,
			@p_direccion,
			@p_contacto,
			@p_telefono,
			@p_fax,
			@p_email,
			@p_website,
			@p_representante,
			@p_notas,
			@p_tercero_nombreCorto,
			@p_esCliente,
			@p_esProveedor,
			@p_esTransportador,
			@p_esFabricante,
			@p_esEmpresaPropia,
			@p_clasificacion,
			@p_id_zona,
			@p_id_estadoTercero,
			@p_id_naturalezaTercero,
			@p_esProspecto,
			@p_tiempoEntrega,
			@p_id_formaDePago)
		SET @v_id = scope_identity()
UPDATE [ter_tercero] 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_tercero]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_ter_tercero]
GO
CREATE PROCEDURE [act_ter_tercero]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [ter_tercero]
	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_tercero_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_ter_tercero_m]
GO
CREATE PROCEDURE [reg_ter_tercero_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],
		[tercero] AS [tercero],
		[tercero_codigo] AS [tercero_codigo],
		[tercero_nit] AS [tercero_nit],
		[direccion] AS [direccion],
		[contacto] AS [contacto],
		[telefono] AS [telefono],
		[fax] AS [fax],
		[email] AS [email],
		[website] AS [website],
		[representante] AS [representante],
		[notas] AS [notas],
		[tercero_nombreCorto] AS [tercero_nombreCorto],
		[esCliente] AS [esCliente],
		[esProveedor] AS [esProveedor],
		[esTransportador] AS [esTransportador],
		[esFabricante] AS [esFabricante],
		[esEmpresaPropia] AS [esEmpresaPropia],
		[clasificacion] AS [clasificacion],
		[id_zona] AS [id_zona],
		[id_estadoTercero] AS [id_estadoTercero],
		[id_naturalezaTercero] AS [id_naturalezaTercero],
		[esProspecto] AS [esProspecto],
		[tiempoEntrega] AS [tiempoEntrega],
		[id_formaDePago] AS [id_formaDePago]
	FROM [ter_tercero]
	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_tercerosXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_tercerosXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_ter_tercerosXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ter_tercero]
	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_tercerosXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_tercerosXusuario_creo_m]
GO
CREATE PROCEDURE [sel_ter_tercerosXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ter_tercero]
	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_tercerosXzona_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_tercerosXzona_m]
GO
CREATE PROCEDURE [sel_ter_tercerosXzona_m]
	@p_id_zona INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ter_tercero]
	WHERE ([id_zona]= @p_id_zona)
	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_tercerosXestadoTercero_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_tercerosXestadoTercero_m]
GO
CREATE PROCEDURE [sel_ter_tercerosXestadoTercero_m]
	@p_id_estadoTercero INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ter_tercero]
	WHERE ([id_estadoTercero]= @p_id_estadoTercero)
	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_tercerosXnaturalezaTercero_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_tercerosXnaturalezaTercero_m]
GO
CREATE PROCEDURE [sel_ter_tercerosXnaturalezaTercero_m]
	@p_id_naturalezaTercero INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ter_tercero]
	WHERE ([id_naturalezaTercero]= @p_id_naturalezaTercero)
	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_tercerosXformaDePago_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_tercerosXformaDePago_m]
GO
CREATE PROCEDURE [sel_ter_tercerosXformaDePago_m]
	@p_id_formaDePago INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ter_tercero]
	WHERE ([id_formaDePago]= @p_id_formaDePago)
	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_terceros_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_terceros_m]
GO
CREATE PROCEDURE [sel_ter_terceros_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_tercero]
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_terceros_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_ter_terceros_m]
GO
CREATE PROCEDURE [lis_ter_terceros_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[tercero] AS [ter_tercero]
	FROM [ter_tercero]
	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_tercero]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_ter_tercero]
GO
CREATE PROCEDURE [aud_ter_tercero]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [ter_tercero].id as id_tabla,
	[ter_tercero].id_usuario_modifico as id_usuario_modifico,
	[ter_tercero].id_usuario_creo as id_usuario_creo,
	[ter_tercero].fechaModificacion as fechaModificacion,
	[ter_tercero].fechaCreacion as fechaCreacion,
	[ter_tercero].active as active,
	[ter_tercero].[tercero] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [ter_tercero]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[ter_tercero].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[ter_tercero].[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_seg_perfilesXtercero]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_seg_perfilesXtercero]
GO
CREATE PROCEDURE [sel_seg_perfilesXtercero]
	@p_id_ter_tercero INT
AS
BEGIN
	SELECT 	[seg_perfil_tercero].[id] AS [I_id],
		[seg_perfil_tercero].[uid] AS [I_uid],
		[seg_perfil_tercero].[eid] AS [I_eid],
		[seg_perfil_tercero].[fechaModificacion] AS [I_fechaModificacion],
		[seg_perfil_tercero].[fechaCreacion] AS [I_fechaCreacion],
		[seg_perfil_tercero].[active] AS [I_active],
		[seg_perfil_tercero].[perfil_tercero] AS [I_perfil_tercero]
	FROM seg_perfil_tercero INNER JOIN seg_perfil
	 ON [seg_perfil_tercero].[id_perfil] = [seg_perfil].id
	WHERE	([seg_perfil_tercero].[id_tercero]= @p_id_ter_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].[ins_seg_perfil_tercero]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_seg_perfil_tercero]
GO
CREATE PROCEDURE [ins_seg_perfil_tercero]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_perfil_tercero varchar(300),
		@p_id_perfil int,
		@p_id_tercero int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [seg_perfil_tercero]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[perfil_tercero],
			[id_perfil],
			[id_tercero])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_perfil_tercero,
			@p_id_perfil,
			@p_id_tercero)
		SET @v_id = scope_identity()
UPDATE [seg_perfil_tercero] 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_seg_perfil_tercero]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_seg_perfil_tercero]
GO
CREATE PROCEDURE [upd_seg_perfil_tercero]
	@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_perfil_tercero varchar(300) = null,
		@p_id_perfil int = null,
		@p_id_tercero int = null
AS
BEGIN
		UPDATE [seg_perfil_tercero]
		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(),
			[perfil_tercero] = isnull(@p_perfil_tercero,[perfil_tercero]),
			[id_perfil] = isnull(@p_id_perfil,[id_perfil]),
			[id_tercero] = isnull(@p_id_tercero,[id_tercero])
		WHERE (id = @p_id)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'ter_tercero'
-----------------------------------26/05/2021 12:07:43 p. m.