
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_ter_sucursal]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_ter_sucursal]
GO
CREATE PROCEDURE [reg_ter_sucursal]
	@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],
		[sucursal] AS [sucursal],
		[sucursal_codigo] AS [sucursal_codigo],
		[contacto] AS [contacto],
		[telefono] AS [telefono],
		[Fax] AS [Fax],
		[Email] AS [Email],
		[Direccion] AS [Direccion],
		[paraVenta] AS [paraVenta],
		[paraSoporte] AS [paraSoporte],
		[paraAlquiler] AS [paraAlquiler],
		[id_tercero] AS [id_tercero],
		[id_zona] AS [id_zona],
		[id_usuario_asesor] AS [id_usuario_asesor],
		[id_usuario_tecnico] AS [id_usuario_tecnico],
		[id_sucursal] AS [id_sucursal],
		[id_estadoTercero] AS [id_estadoTercero],
		[codigorecursiva] AS [codigorecursiva],
		[cargo] AS [cargo]
	FROM [ter_sucursal]
	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_sucursal]')) DROP VIEW [dbo].[view_ter_sucursal]
GO
CREATE  VIEW [view_ter_sucursal] AS
SELECT 	[ter_sucursal].[id] AS [id],
		[ter_sucursal].[uid] AS [uid],
		[ter_sucursal].[eid] AS [eid],
		[ter_sucursal].[id_usuario_modifico] AS [id_usuario_modifico],
		[ter_sucursal].[id_usuario_creo] AS [id_usuario_creo],
		[ter_sucursal].[fechaModificacion] AS [fechaModificacion],
		[ter_sucursal].[fechaCreacion] AS [fechaCreacion],
		[ter_sucursal].[active] AS [active],
		[ter_sucursal].[sucursal] AS [sucursal],
		[ter_sucursal].[sucursal_codigo] AS [sucursal_codigo],
		[ter_sucursal].[contacto] AS [contacto],
		[ter_sucursal].[telefono] AS [telefono],
		[ter_sucursal].[Fax] AS [Fax],
		[ter_sucursal].[Email] AS [Email],
		[ter_sucursal].[Direccion] AS [Direccion],
		[ter_sucursal].[paraVenta] AS [paraVenta],
		[ter_sucursal].[paraSoporte] AS [paraSoporte],
		[ter_sucursal].[paraAlquiler] AS [paraAlquiler],
		[ter_sucursal].[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_sucursal].[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_sucursal].[id_usuario_asesor] AS [id_usuario_asesor],
[seg_usuario_asesor].[usuario] as [seg_usuario_asesor_usuario],
[seg_usuario_asesor].[usuario_codigo] as [seg_usuario_asesor_usuario_codigo],
[seg_usuario_asesor].[intentosFallidos] as [seg_usuario_asesor_intentosFallidos],
[seg_usuario_asesor].[numeroIngresos] as [seg_usuario_asesor_numeroIngresos],
[seg_usuario_asesor].[fechaUltimoIngreso_fh] as [seg_usuario_asesor_fechaUltimoIngreso_fh],
[seg_usuario_asesor].[fechaIntentoFallido_fh] as [seg_usuario_asesor_fechaIntentoFallido_fh],
[seg_usuario_asesor].[clave] as [seg_usuario_asesor_clave],
[seg_usuario_asesor].[nombre] as [seg_usuario_asesor_nombre],
[seg_usuario_asesor].[profesion] as [seg_usuario_asesor_profesion],
[seg_usuario_asesor].[email] as [seg_usuario_asesor_email],
[seg_usuario_asesor].[direccion] as [seg_usuario_asesor_direccion],
[seg_usuario_asesor].[telefono] as [seg_usuario_asesor_telefono],
[seg_usuario_asesor].[costoHora] as [seg_usuario_asesor_costoHora],
[seg_usuario_asesor].[porcentajeComision] as [seg_usuario_asesor_porcentajeComision],
[seg_usuario_asesor].[accesoActivo] as [seg_usuario_asesor_accesoActivo],
[seg_usuario_asesor].[cambiarClave] as [seg_usuario_asesor_cambiarClave],
[seg_usuario_asesor].[fechaUltimoCambioClave_fh] as [seg_usuario_asesor_fechaUltimoCambioClave_fh],
[seg_usuario_asesor].[aplicaRecargo] as [seg_usuario_asesor_aplicaRecargo],
[seg_usuario_asesor].[sexo] as [seg_usuario_asesor_sexo],
[seg_usuario_asesor].[id_perfil] as [seg_usuario_asesor_id_perfil],
[seg_usuario_asesor].[id_cargo] as [seg_usuario_asesor_id_cargo],
[seg_usuario_asesor].[id_grupo] as [seg_usuario_asesor_id_grupo],
[seg_usuario_asesor].[id_zona] as [seg_usuario_asesor_id_zona],
[seg_usuario_asesor].[identificacion] as [seg_usuario_asesor_identificacion],
[seg_usuario_asesor].[id_usuario] as [seg_usuario_asesor_id_usuario],
[seg_usuario_asesor].[preferencias] as [seg_usuario_asesor_preferencias],
[seg_usuario_asesor].[habilitado] as [seg_usuario_asesor_habilitado],
		[ter_sucursal].[id_usuario_tecnico] AS [id_usuario_tecnico],
[seg_usuario_tecnico].[usuario] as [seg_usuario_tecnico_usuario],
[seg_usuario_tecnico].[usuario_codigo] as [seg_usuario_tecnico_usuario_codigo],
[seg_usuario_tecnico].[intentosFallidos] as [seg_usuario_tecnico_intentosFallidos],
[seg_usuario_tecnico].[numeroIngresos] as [seg_usuario_tecnico_numeroIngresos],
[seg_usuario_tecnico].[fechaUltimoIngreso_fh] as [seg_usuario_tecnico_fechaUltimoIngreso_fh],
[seg_usuario_tecnico].[fechaIntentoFallido_fh] as [seg_usuario_tecnico_fechaIntentoFallido_fh],
[seg_usuario_tecnico].[clave] as [seg_usuario_tecnico_clave],
[seg_usuario_tecnico].[nombre] as [seg_usuario_tecnico_nombre],
[seg_usuario_tecnico].[profesion] as [seg_usuario_tecnico_profesion],
[seg_usuario_tecnico].[email] as [seg_usuario_tecnico_email],
[seg_usuario_tecnico].[direccion] as [seg_usuario_tecnico_direccion],
[seg_usuario_tecnico].[telefono] as [seg_usuario_tecnico_telefono],
[seg_usuario_tecnico].[costoHora] as [seg_usuario_tecnico_costoHora],
[seg_usuario_tecnico].[porcentajeComision] as [seg_usuario_tecnico_porcentajeComision],
[seg_usuario_tecnico].[accesoActivo] as [seg_usuario_tecnico_accesoActivo],
[seg_usuario_tecnico].[cambiarClave] as [seg_usuario_tecnico_cambiarClave],
[seg_usuario_tecnico].[fechaUltimoCambioClave_fh] as [seg_usuario_tecnico_fechaUltimoCambioClave_fh],
[seg_usuario_tecnico].[aplicaRecargo] as [seg_usuario_tecnico_aplicaRecargo],
[seg_usuario_tecnico].[sexo] as [seg_usuario_tecnico_sexo],
[seg_usuario_tecnico].[id_perfil] as [seg_usuario_tecnico_id_perfil],
[seg_usuario_tecnico].[id_cargo] as [seg_usuario_tecnico_id_cargo],
[seg_usuario_tecnico].[id_grupo] as [seg_usuario_tecnico_id_grupo],
[seg_usuario_tecnico].[id_zona] as [seg_usuario_tecnico_id_zona],
[seg_usuario_tecnico].[identificacion] as [seg_usuario_tecnico_identificacion],
[seg_usuario_tecnico].[id_usuario] as [seg_usuario_tecnico_id_usuario],
[seg_usuario_tecnico].[preferencias] as [seg_usuario_tecnico_preferencias],
[seg_usuario_tecnico].[habilitado] as [seg_usuario_tecnico_habilitado],
		[ter_sucursal].[id_sucursal] AS [id_sucursal],
[ter_sucursal_padre].[sucursal] as [ter_sucursal_padre_sucursal],
[ter_sucursal_padre].[sucursal_codigo] as [ter_sucursal_padre_sucursal_codigo],
[ter_sucursal_padre].[contacto] as [ter_sucursal_padre_contacto],
[ter_sucursal_padre].[telefono] as [ter_sucursal_padre_telefono],
[ter_sucursal_padre].[Fax] as [ter_sucursal_padre_Fax],
[ter_sucursal_padre].[Email] as [ter_sucursal_padre_Email],
[ter_sucursal_padre].[Direccion] as [ter_sucursal_padre_Direccion],
[ter_sucursal_padre].[paraVenta] as [ter_sucursal_padre_paraVenta],
[ter_sucursal_padre].[paraSoporte] as [ter_sucursal_padre_paraSoporte],
[ter_sucursal_padre].[paraAlquiler] as [ter_sucursal_padre_paraAlquiler],
[ter_sucursal_padre].[id_tercero] as [ter_sucursal_padre_id_tercero],
[ter_sucursal_padre].[id_zona] as [ter_sucursal_padre_id_zona],
[ter_sucursal_padre].[id_usuario_asesor] as [ter_sucursal_padre_id_usuario_asesor],
[ter_sucursal_padre].[id_usuario_tecnico] as [ter_sucursal_padre_id_usuario_tecnico],
[ter_sucursal_padre].[id_sucursal] as [ter_sucursal_padre_id_sucursal],
[ter_sucursal_padre].[id_estadoTercero] as [ter_sucursal_padre_id_estadoTercero],
[ter_sucursal_padre].[codigorecursiva] as [ter_sucursal_padre_codigorecursiva],
[ter_sucursal_padre].[cargo] as [ter_sucursal_padre_cargo],
		[ter_sucursal].[id_estadoTercero] AS [id_estadoTercero],
[ter_estadoTercero].[estadoTercero] as [ter_estadoTercero_estadoTercero],
[ter_estadoTercero].[estadoTercero_codigo] as [ter_estadoTercero_estadoTercero_codigo],
		[ter_sucursal].[codigorecursiva] AS [codigorecursiva],
		[ter_sucursal].[cargo] AS [cargo],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [ter_sucursal]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[ter_sucursal].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[ter_sucursal].[id_usuario_creo]
	INNER JOIN [ter_tercero] ON [ter_tercero].id=[ter_sucursal].[id_tercero]
	INNER JOIN [gen_zona] ON [gen_zona].id=[ter_sucursal].[id_zona]
	INNER JOIN [seg_usuario] AS [seg_usuario_asesor] ON [seg_usuario_asesor].id=[ter_sucursal].[id_usuario_asesor]
	INNER JOIN [seg_usuario] AS [seg_usuario_tecnico] ON [seg_usuario_tecnico].id=[ter_sucursal].[id_usuario_tecnico]
	INNER JOIN [ter_sucursal] AS [ter_sucursal_padre] ON [ter_sucursal_padre].id=[ter_sucursal].[id_sucursal]
	INNER JOIN [ter_estadoTercero] ON [ter_estadoTercero].id=[ter_sucursal].[id_estadoTercero]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [ter_sucursal].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [ter_sucursal].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_sucursalesXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_sucursalesXusuario_modifico]
GO
CREATE PROCEDURE [sel_ter_sucursalesXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_ter_sucursal]
	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_sucursalesXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_sucursalesXusuario_creo]
GO
CREATE PROCEDURE [sel_ter_sucursalesXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_ter_sucursal]
	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_sucursalesXtercero]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_sucursalesXtercero]
GO
CREATE PROCEDURE [sel_ter_sucursalesXtercero]
	@p_id_tercero INT
AS
BEGIN
	SELECT * 
	FROM [view_ter_sucursal]
	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_sucursalesXzona]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_sucursalesXzona]
GO
CREATE PROCEDURE [sel_ter_sucursalesXzona]
	@p_id_zona INT
AS
BEGIN
	SELECT * 
	FROM [view_ter_sucursal]
	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_sucursalesXusuario_asesor]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_sucursalesXusuario_asesor]
GO
CREATE PROCEDURE [sel_ter_sucursalesXusuario_asesor]
	@p_id_usuario_asesor INT
AS
BEGIN
	SELECT * 
	FROM [view_ter_sucursal]
	WHERE ([id_usuario_asesor]= @p_id_usuario_asesor)
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_sucursalesXusuario_tecnico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_sucursalesXusuario_tecnico]
GO
CREATE PROCEDURE [sel_ter_sucursalesXusuario_tecnico]
	@p_id_usuario_tecnico INT
AS
BEGIN
	SELECT * 
	FROM [view_ter_sucursal]
	WHERE ([id_usuario_tecnico]= @p_id_usuario_tecnico)
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_sucursalesXsucursal]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_sucursalesXsucursal]
GO
CREATE PROCEDURE [sel_ter_sucursalesXsucursal]
	@p_id_sucursal INT
AS
BEGIN
	SELECT * 
	FROM [view_ter_sucursal]
	WHERE ([id_sucursal]= @p_id_sucursal)
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_sucursalesXestadoTercero]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_sucursalesXestadoTercero]
GO
CREATE PROCEDURE [sel_ter_sucursalesXestadoTercero]
	@p_id_estadoTercero INT
AS
BEGIN
	SELECT * 
	FROM [view_ter_sucursal]
	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_sucursales]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_sucursales]
GO
CREATE PROCEDURE [sel_ter_sucursales]
	
	@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_sucursal]
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_sucursales]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_ter_sucursales]
GO
CREATE PROCEDURE [lis_ter_sucursales]
	AS
BEGIN
	SELECT id AS id,
[sucursal] AS [ter_sucursal]
	FROM [ter_sucursal]
	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_sucursal]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_ter_sucursal]
GO
CREATE PROCEDURE [upd_ter_sucursal]
	@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_sucursal varchar(300) = null,
		@p_sucursal_codigo varchar(100) = null,
		@p_contacto varchar(100) = null,
		@p_telefono varchar(100) = null,
		@p_Fax varchar(100) = null,
		@p_Email varchar(100) = null,
		@p_Direccion varchar(300) = null,
		@p_paraVenta bit = null,
		@p_paraSoporte bit = null,
		@p_paraAlquiler bit = null,
		@p_id_tercero int = null,
		@p_id_zona int = null,
		@p_id_usuario_asesor int = null,
		@p_id_usuario_tecnico int = null,
		@p_id_sucursal int = null,
		@p_id_estadoTercero int = null,
		@p_codigorecursiva varchar(5000) = null,
		@p_cargo varchar(300) = null
AS
BEGIN
		UPDATE [ter_sucursal]
		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(),
			[sucursal] = isnull(@p_sucursal,[sucursal]),
			[sucursal_codigo] = isnull(@p_sucursal_codigo,[sucursal_codigo]),
			[contacto] = isnull(@p_contacto,[contacto]),
			[telefono] = isnull(@p_telefono,[telefono]),
			[Fax] = isnull(@p_Fax,[Fax]),
			[Email] = isnull(@p_Email,[Email]),
			[Direccion] = isnull(@p_Direccion,[Direccion]),
			[paraVenta] = isnull(@p_paraVenta,[paraVenta]),
			[paraSoporte] = isnull(@p_paraSoporte,[paraSoporte]),
			[paraAlquiler] = isnull(@p_paraAlquiler,[paraAlquiler]),
			[id_tercero] = isnull(@p_id_tercero,[id_tercero]),
			[id_zona] = isnull(@p_id_zona,[id_zona]),
			[id_usuario_asesor] = isnull(@p_id_usuario_asesor,[id_usuario_asesor]),
			[id_usuario_tecnico] = isnull(@p_id_usuario_tecnico,[id_usuario_tecnico]),
			[id_sucursal] = isnull(@p_id_sucursal,[id_sucursal]),
			[id_estadoTercero] = isnull(@p_id_estadoTercero,[id_estadoTercero]),
			[codigorecursiva] = isnull(@p_codigorecursiva,[codigorecursiva]),
			[cargo] = isnull(@p_cargo,[cargo])
		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_sucursal]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_ter_sucursal]
GO
CREATE PROCEDURE [ins_ter_sucursal]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_sucursal varchar(300),
		@p_sucursal_codigo varchar(100),
		@p_contacto varchar(100),
		@p_telefono varchar(100),
		@p_Fax varchar(100),
		@p_Email varchar(100),
		@p_Direccion varchar(300),
		@p_paraVenta bit,
		@p_paraSoporte bit,
		@p_paraAlquiler bit,
		@p_id_tercero int,
		@p_id_zona int,
		@p_id_usuario_asesor int,
		@p_id_usuario_tecnico int,
		@p_id_sucursal int,
		@p_id_estadoTercero int,
		@p_codigorecursiva varchar(5000),
		@p_cargo varchar(300)
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [ter_sucursal]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[sucursal],
			[sucursal_codigo],
			[contacto],
			[telefono],
			[Fax],
			[Email],
			[Direccion],
			[paraVenta],
			[paraSoporte],
			[paraAlquiler],
			[id_tercero],
			[id_zona],
			[id_usuario_asesor],
			[id_usuario_tecnico],
			[id_sucursal],
			[id_estadoTercero],
			[codigorecursiva],
			[cargo])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_sucursal,
			@p_sucursal_codigo,
			@p_contacto,
			@p_telefono,
			@p_Fax,
			@p_Email,
			@p_Direccion,
			@p_paraVenta,
			@p_paraSoporte,
			@p_paraAlquiler,
			@p_id_tercero,
			@p_id_zona,
			@p_id_usuario_asesor,
			@p_id_usuario_tecnico,
			@p_id_sucursal,
			@p_id_estadoTercero,
			@p_codigorecursiva,
			@p_cargo)
		SET @v_id = scope_identity()
UPDATE [ter_sucursal] 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_sucursal]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_ter_sucursal]
GO
CREATE PROCEDURE [act_ter_sucursal]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [ter_sucursal]
	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_sucursal_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_ter_sucursal_m]
GO
CREATE PROCEDURE [reg_ter_sucursal_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],
		[sucursal] AS [sucursal],
		[sucursal_codigo] AS [sucursal_codigo],
		[contacto] AS [contacto],
		[telefono] AS [telefono],
		[Fax] AS [Fax],
		[Email] AS [Email],
		[Direccion] AS [Direccion],
		[paraVenta] AS [paraVenta],
		[paraSoporte] AS [paraSoporte],
		[paraAlquiler] AS [paraAlquiler],
		[id_tercero] AS [id_tercero],
		[id_zona] AS [id_zona],
		[id_usuario_asesor] AS [id_usuario_asesor],
		[id_usuario_tecnico] AS [id_usuario_tecnico],
		[id_sucursal] AS [id_sucursal],
		[id_estadoTercero] AS [id_estadoTercero],
		[codigorecursiva] AS [codigorecursiva],
		[cargo] AS [cargo]
	FROM [ter_sucursal]
	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_sucursalesXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_sucursalesXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_ter_sucursalesXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ter_sucursal]
	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_sucursalesXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_sucursalesXusuario_creo_m]
GO
CREATE PROCEDURE [sel_ter_sucursalesXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ter_sucursal]
	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_sucursalesXtercero_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_sucursalesXtercero_m]
GO
CREATE PROCEDURE [sel_ter_sucursalesXtercero_m]
	@p_id_tercero INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ter_sucursal]
	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_sucursalesXzona_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_sucursalesXzona_m]
GO
CREATE PROCEDURE [sel_ter_sucursalesXzona_m]
	@p_id_zona INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ter_sucursal]
	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_sucursalesXusuario_asesor_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_sucursalesXusuario_asesor_m]
GO
CREATE PROCEDURE [sel_ter_sucursalesXusuario_asesor_m]
	@p_id_usuario_asesor INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ter_sucursal]
	WHERE ([id_usuario_asesor]= @p_id_usuario_asesor)
	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_sucursalesXusuario_tecnico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_sucursalesXusuario_tecnico_m]
GO
CREATE PROCEDURE [sel_ter_sucursalesXusuario_tecnico_m]
	@p_id_usuario_tecnico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ter_sucursal]
	WHERE ([id_usuario_tecnico]= @p_id_usuario_tecnico)
	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_sucursalesXsucursal_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_sucursalesXsucursal_m]
GO
CREATE PROCEDURE [sel_ter_sucursalesXsucursal_m]
	@p_id_sucursal INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ter_sucursal]
	WHERE ([id_sucursal]= @p_id_sucursal)
	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_sucursalesXestadoTercero_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_sucursalesXestadoTercero_m]
GO
CREATE PROCEDURE [sel_ter_sucursalesXestadoTercero_m]
	@p_id_estadoTercero INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ter_sucursal]
	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_sucursales_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_sucursales_m]
GO
CREATE PROCEDURE [sel_ter_sucursales_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_sucursal]
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_sucursales_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_ter_sucursales_m]
GO
CREATE PROCEDURE [lis_ter_sucursales_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[sucursal] AS [ter_sucursal]
	FROM [ter_sucursal]
	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_sucursal]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_ter_sucursal]
GO
CREATE PROCEDURE [aud_ter_sucursal]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [ter_sucursal].id as id_tabla,
	[ter_sucursal].id_usuario_modifico as id_usuario_modifico,
	[ter_sucursal].id_usuario_creo as id_usuario_creo,
	[ter_sucursal].fechaModificacion as fechaModificacion,
	[ter_sucursal].fechaCreacion as fechaCreacion,
	[ter_sucursal].active as active,
	[ter_sucursal].[sucursal] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [ter_sucursal]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[ter_sucursal].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[ter_sucursal].[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_cnt_contratosXsucursal]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_contratosXsucursal]
GO
CREATE PROCEDURE [sel_cnt_contratosXsucursal]
	@p_id_ter_sucursal INT
AS
BEGIN
	SELECT 	[cnt_contrato_sucursal].[id] AS [I_id],
		[cnt_contrato_sucursal].[uid] AS [I_uid],
		[cnt_contrato_sucursal].[eid] AS [I_eid],
		[cnt_contrato_sucursal].[fechaModificacion] AS [I_fechaModificacion],
		[cnt_contrato_sucursal].[fechaCreacion] AS [I_fechaCreacion],
		[cnt_contrato_sucursal].[active] AS [I_active],
		[cnt_contrato_sucursal].[contrato_sucursal] AS [I_contrato_sucursal],
		[cnt_contrato_sucursal].[precioVisita] AS [I_precioVisita],
		[cnt_contrato_sucursal].[precioServicio] AS [I_precioServicio],
		[cnt_contrato_sucursal].[presupuesto] AS [I_presupuesto]
	FROM cnt_contrato_sucursal INNER JOIN cnt_contrato
	 ON [cnt_contrato_sucursal].[id_contrato] = [cnt_contrato].id
	WHERE	([cnt_contrato_sucursal].[id_sucursal]= @p_id_ter_sucursal)
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_cnt_contrato_sucursal]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_cnt_contrato_sucursal]
GO
CREATE PROCEDURE [ins_cnt_contrato_sucursal]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_contrato_sucursal varchar(300),
		@p_precioVisita money,
		@p_precioServicio money,
		@p_id_contrato int,
		@p_id_sucursal int,
		@p_presupuesto money
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [cnt_contrato_sucursal]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[contrato_sucursal],
			[precioVisita],
			[precioServicio],
			[id_contrato],
			[id_sucursal],
			[presupuesto])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_contrato_sucursal,
			@p_precioVisita,
			@p_precioServicio,
			@p_id_contrato,
			@p_id_sucursal,
			@p_presupuesto)
		SET @v_id = scope_identity()
UPDATE [cnt_contrato_sucursal] 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_cnt_contrato_sucursal]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_cnt_contrato_sucursal]
GO
CREATE PROCEDURE [upd_cnt_contrato_sucursal]
	@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_contrato_sucursal varchar(300) = null,
		@p_precioVisita money = null,
		@p_precioServicio money = null,
		@p_id_contrato int = null,
		@p_id_sucursal int = null,
		@p_presupuesto money = null
AS
BEGIN
		UPDATE [cnt_contrato_sucursal]
		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(),
			[contrato_sucursal] = isnull(@p_contrato_sucursal,[contrato_sucursal]),
			[precioVisita] = isnull(@p_precioVisita,[precioVisita]),
			[precioServicio] = isnull(@p_precioServicio,[precioServicio]),
			[id_contrato] = isnull(@p_id_contrato,[id_contrato]),
			[id_sucursal] = isnull(@p_id_sucursal,[id_sucursal]),
			[presupuesto] = isnull(@p_presupuesto,[presupuesto])
		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_doc_documento_proyectosXsucursal]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_documento_proyectosXsucursal]
GO
CREATE PROCEDURE [sel_doc_documento_proyectosXsucursal]
	@p_id_ter_sucursal INT
AS
BEGIN
	SELECT 	[doc_documento.proyecto_sucursal].[id] AS [I_id],
		[doc_documento.proyecto_sucursal].[uid] AS [I_uid],
		[doc_documento.proyecto_sucursal].[eid] AS [I_eid],
		[doc_documento.proyecto_sucursal].[fechaModificacion] AS [I_fechaModificacion],
		[doc_documento.proyecto_sucursal].[fechaCreacion] AS [I_fechaCreacion],
		[doc_documento.proyecto_sucursal].[active] AS [I_active],
		[doc_documento.proyecto_sucursal].[documento.proyecto_sucursal] AS [I_documento.proyecto_sucursal]
	FROM doc_documento.proyecto_sucursal INNER JOIN doc_documento.proyecto
	 ON [doc_documento.proyecto_sucursal].[id_documento.proyecto] = [doc_documento.proyecto].id
	WHERE	([doc_documento.proyecto_sucursal].[id_sucursal]= @p_id_ter_sucursal)
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_proyecto_sucursal]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_doc_documento_proyecto_sucursal]
GO
CREATE PROCEDURE [ins_doc_documento_proyecto_sucursal]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_documento_proyecto_sucursal varchar(300),
		@p_id_documento_proyecto int,
		@p_id_sucursal int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [doc_documento.proyecto_sucursal]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[documento.proyecto_sucursal],
			[id_documento.proyecto],
			[id_sucursal])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_documento_proyecto_sucursal,
			@p_id_documento_proyecto,
			@p_id_sucursal)
		SET @v_id = scope_identity()
UPDATE [doc_documento.proyecto_sucursal] 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_proyecto_sucursal]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_doc_documento_proyecto_sucursal]
GO
CREATE PROCEDURE [upd_doc_documento_proyecto_sucursal]
	@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_proyecto_sucursal varchar(300) = null,
		@p_id_documento_proyecto int = null,
		@p_id_sucursal int = null
AS
BEGIN
		UPDATE [doc_documento.proyecto_sucursal]
		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.proyecto_sucursal] = isnull(@p_documento_proyecto_sucursal,[documento.proyecto_sucursal]),
			[id_documento.proyecto] = isnull(@p_id_documento_proyecto,[id_documento.proyecto]),
			[id_sucursal] = isnull(@p_id_sucursal,[id_sucursal])
		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_ter_contactosXsucursal]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_contactosXsucursal]
GO
CREATE PROCEDURE [sel_ter_contactosXsucursal]
	@p_id_ter_sucursal INT
AS
BEGIN
	SELECT 	[ter_contacto_sucursal].[id] AS [I_id],
		[ter_contacto_sucursal].[uid] AS [I_uid],
		[ter_contacto_sucursal].[eid] AS [I_eid],
		[ter_contacto_sucursal].[fechaModificacion] AS [I_fechaModificacion],
		[ter_contacto_sucursal].[fechaCreacion] AS [I_fechaCreacion],
		[ter_contacto_sucursal].[active] AS [I_active],
		[ter_contacto_sucursal].[contacto_sucursal] AS [I_contacto_sucursal]
	FROM ter_contacto_sucursal INNER JOIN ter_contacto
	 ON [ter_contacto_sucursal].[id_contacto] = [ter_contacto].id
	WHERE	([ter_contacto_sucursal].[id_sucursal]= @p_id_ter_sucursal)
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_sucursal]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_ter_contacto_sucursal]
GO
CREATE PROCEDURE [ins_ter_contacto_sucursal]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_contacto_sucursal varchar(300),
		@p_id_contacto int,
		@p_id_sucursal int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [ter_contacto_sucursal]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[contacto_sucursal],
			[id_contacto],
			[id_sucursal])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_contacto_sucursal,
			@p_id_contacto,
			@p_id_sucursal)
		SET @v_id = scope_identity()
UPDATE [ter_contacto_sucursal] 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_ter_contacto_sucursal]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_ter_contacto_sucursal]
GO
CREATE PROCEDURE [upd_ter_contacto_sucursal]
	@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_sucursal varchar(300) = null,
		@p_id_contacto int = null,
		@p_id_sucursal int = null
AS
BEGIN
		UPDATE [ter_contacto_sucursal]
		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_sucursal] = isnull(@p_contacto_sucursal,[contacto_sucursal]),
			[id_contacto] = isnull(@p_id_contacto,[id_contacto]),
			[id_sucursal] = isnull(@p_id_sucursal,[id_sucursal])
		WHERE (id = @p_id)
END