
GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_gen_zona]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_gen_zona]
GO
CREATE PROCEDURE [reg_gen_zona]
	@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],
		[zona] AS [zona],
		[zona_codigo] AS [zona_codigo],
		[tiempoDesplazamiento] AS [tiempoDesplazamiento],
		[nivel] AS [nivel],
		[id_zona] AS [id_zona],
		[codigoExterno] AS [codigoExterno]
	FROM [gen_zona]
	WHERE	(id = @p_id)

END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[view_gen_zona]')) DROP VIEW [dbo].[view_gen_zona]
GO
CREATE  VIEW [view_gen_zona] AS
SELECT 	[gen_zona].[id] AS [id],
		[gen_zona].[uid] AS [uid],
		[gen_zona].[eid] AS [eid],
		[gen_zona].[id_usuario_modifico] AS [id_usuario_modifico],
		[gen_zona].[id_usuario_creo] AS [id_usuario_creo],
		[gen_zona].[fechaModificacion] AS [fechaModificacion],
		[gen_zona].[fechaCreacion] AS [fechaCreacion],
		[gen_zona].[active] AS [active],
		[gen_zona].[zona] AS [zona],
		[gen_zona].[zona_codigo] AS [zona_codigo],
		[gen_zona].[tiempoDesplazamiento] AS [tiempoDesplazamiento],
		[gen_zona].[nivel] AS [nivel],
		[gen_zona].[id_zona] AS [id_zona],
[gen_zona_padre].[zona] as [gen_zona_padre_zona],
[gen_zona_padre].[zona_codigo] as [gen_zona_padre_zona_codigo],
[gen_zona_padre].[tiempoDesplazamiento] as [gen_zona_padre_tiempoDesplazamiento],
[gen_zona_padre].[nivel] as [gen_zona_padre_nivel],
[gen_zona_padre].[id_zona] as [gen_zona_padre_id_zona],
[gen_zona_padre].[codigoExterno] as [gen_zona_padre_codigoExterno],
		[gen_zona].[codigoExterno] AS [codigoExterno],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [gen_zona]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[gen_zona].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[gen_zona].[id_usuario_creo]
	INNER JOIN [gen_zona] AS [gen_zona_padre] ON [gen_zona_padre].id=[gen_zona].[id_zona]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [gen_zona].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [gen_zona].active=1
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sel_gen_zonasXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_zonasXusuario_modifico]
GO
CREATE PROCEDURE [sel_gen_zonasXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_gen_zona]
	WHERE ([id_usuario_modifico]= @p_id_usuario_modifico)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sel_gen_zonasXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_zonasXusuario_creo]
GO
CREATE PROCEDURE [sel_gen_zonasXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_gen_zona]
	WHERE ([id_usuario_creo]= @p_id_usuario_creo)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sel_gen_zonasXzona]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_zonasXzona]
GO
CREATE PROCEDURE [sel_gen_zonasXzona]
	@p_id_zona INT
AS
BEGIN
	SELECT * 
	FROM [view_gen_zona]
	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_gen_zonas]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_zonas]
GO
CREATE PROCEDURE [sel_gen_zonas]
	
	@p_filtro as varchar (8000) = '1=1',
	@p_orden as varchar (8000) = 'id',
	@p_campos as varchar (8000) = '*' AS
BEGIN
exec ('SELECT '+ @p_campos +
' FROM [view_gen_zona]
WHERE (' + @p_filtro + ') ORDER BY ' + @p_orden)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[lis_gen_zonas]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_gen_zonas]
GO
CREATE PROCEDURE [lis_gen_zonas]
	AS
BEGIN
	SELECT id AS id,
[zona] AS [gen_zona]
	FROM [gen_zona]
	WHERE active = 1
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[upd_gen_zona]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_gen_zona]
GO
CREATE PROCEDURE [upd_gen_zona]
	@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_zona varchar(300) = null,
		@p_zona_codigo varchar(100) = null,
		@p_tiempoDesplazamiento float = null,
		@p_nivel int = null,
		@p_id_zona int = null,
		@p_codigoExterno varchar(50) = null
AS
BEGIN
		UPDATE [gen_zona]
		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(),
			[zona] = isnull(@p_zona,[zona]),
			[zona_codigo] = isnull(@p_zona_codigo,[zona_codigo]),
			[tiempoDesplazamiento] = isnull(@p_tiempoDesplazamiento,[tiempoDesplazamiento]),
			[nivel] = isnull(@p_nivel,[nivel]),
			[id_zona] = isnull(@p_id_zona,[id_zona]),
			[codigoExterno] = isnull(@p_codigoExterno,[codigoExterno])
		WHERE (id = @p_id)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ins_gen_zona]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_gen_zona]
GO
CREATE PROCEDURE [ins_gen_zona]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_zona varchar(300),
		@p_zona_codigo varchar(100),
		@p_tiempoDesplazamiento float,
		@p_nivel int,
		@p_id_zona int,
		@p_codigoExterno varchar(50)
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [gen_zona]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[zona],
			[zona_codigo],
			[tiempoDesplazamiento],
			[nivel],
			[id_zona],
			[codigoExterno])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_zona,
			@p_zona_codigo,
			@p_tiempoDesplazamiento,
			@p_nivel,
			@p_id_zona,
			@p_codigoExterno)
		SET @v_id = scope_identity()
UPDATE [gen_zona] set [uid] = [uid] + '_' + convert(varchar(20),@v_id) where [id]=@v_id
SELECT @v_id as id
RETURN @v_id
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[act_gen_zona]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_gen_zona]
GO
CREATE PROCEDURE [act_gen_zona]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [gen_zona]
	SET active=@p_active,
	[id_usuario_modifico] = isnull(@p_id_usuario_modifico,[id_usuario_modifico]),
	[fechaModificacion] = GETDATE()
	WHERE	(id = @p_id)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_gen_zona_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_gen_zona_m]
GO
CREATE PROCEDURE [reg_gen_zona_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],
		[zona] AS [zona],
		[zona_codigo] AS [zona_codigo],
		[tiempoDesplazamiento] AS [tiempoDesplazamiento],
		[nivel] AS [nivel],
		[id_zona] AS [id_zona],
		[codigoExterno] AS [codigoExterno]
	FROM [gen_zona]
	WHERE	(id = @p_id)
	AND	(eid LIKE @p_eid+'%')

END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sel_gen_zonasXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_zonasXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_gen_zonasXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_gen_zona]
	WHERE ([id_usuario_modifico]= @p_id_usuario_modifico)
	AND	(eid LIKE @p_eid+'%')
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sel_gen_zonasXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_zonasXusuario_creo_m]
GO
CREATE PROCEDURE [sel_gen_zonasXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_gen_zona]
	WHERE ([id_usuario_creo]= @p_id_usuario_creo)
	AND	(eid LIKE @p_eid+'%')
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sel_gen_zonasXzona_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_zonasXzona_m]
GO
CREATE PROCEDURE [sel_gen_zonasXzona_m]
	@p_id_zona INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_gen_zona]
	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_gen_zonas_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_zonas_m]
GO
CREATE PROCEDURE [sel_gen_zonas_m]
	@p_eid as varchar (50)='',
	@p_filtro as varchar (8000) = '1=1',
	@p_orden as varchar (8000) = 'id',
	@p_campos as varchar (8000) = '*' AS
BEGIN
exec ('SELECT '+ @p_campos +
' FROM [view_gen_zona]
WHERE  (eid like '''+@p_eid+'%'+''' ) AND (' + @p_filtro + ') ORDER BY ' + @p_orden)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[lis_gen_zonas_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_gen_zonas_m]
GO
CREATE PROCEDURE [lis_gen_zonas_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[zona] AS [gen_zona]
	FROM [gen_zona]
	WHERE active = 1
	AND	(eid LIKE @p_eid+'%')
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[aud_gen_zona]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_gen_zona]
GO
CREATE PROCEDURE [aud_gen_zona]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [gen_zona].id as id_tabla,
	[gen_zona].id_usuario_modifico as id_usuario_modifico,
	[gen_zona].id_usuario_creo as id_usuario_creo,
	[gen_zona].fechaModificacion as fechaModificacion,
	[gen_zona].fechaCreacion as fechaCreacion,
	[gen_zona].active as active,
	[gen_zona].[zona] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [gen_zona]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[gen_zona].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[gen_zona].[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_contratosXzona]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_contratosXzona]
GO
CREATE PROCEDURE [sel_cnt_contratosXzona]
	@p_id_gen_zona INT
AS
BEGIN
	SELECT 	[cnt_contrato_zona].[id] AS [I_id],
		[cnt_contrato_zona].[uid] AS [I_uid],
		[cnt_contrato_zona].[eid] AS [I_eid],
		[cnt_contrato_zona].[fechaModificacion] AS [I_fechaModificacion],
		[cnt_contrato_zona].[fechaCreacion] AS [I_fechaCreacion],
		[cnt_contrato_zona].[active] AS [I_active],
		[cnt_contrato_zona].[contrato_zona] AS [I_contrato_zona],
		[cnt_contrato_zona].[precioVisita] AS [I_precioVisita],
		[cnt_contrato_zona].[precioServicio] AS [I_precioServicio]
	FROM cnt_contrato_zona INNER JOIN cnt_contrato
	 ON [cnt_contrato_zona].[id_contrato] = [cnt_contrato].id
	WHERE	([cnt_contrato_zona].[id_zona]= @p_id_gen_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].[ins_cnt_contrato_zona]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_cnt_contrato_zona]
GO
CREATE PROCEDURE [ins_cnt_contrato_zona]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_contrato_zona varchar(300),
		@p_precioVisita money,
		@p_precioServicio money,
		@p_id_contrato int,
		@p_id_zona int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [cnt_contrato_zona]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[contrato_zona],
			[precioVisita],
			[precioServicio],
			[id_contrato],
			[id_zona])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_contrato_zona,
			@p_precioVisita,
			@p_precioServicio,
			@p_id_contrato,
			@p_id_zona)
		SET @v_id = scope_identity()
UPDATE [cnt_contrato_zona] 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_zona]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_cnt_contrato_zona]
GO
CREATE PROCEDURE [upd_cnt_contrato_zona]
	@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_zona varchar(300) = null,
		@p_precioVisita money = null,
		@p_precioServicio money = null,
		@p_id_contrato int = null,
		@p_id_zona int = null
AS
BEGIN
		UPDATE [cnt_contrato_zona]
		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_zona] = isnull(@p_contrato_zona,[contrato_zona]),
			[precioVisita] = isnull(@p_precioVisita,[precioVisita]),
			[precioServicio] = isnull(@p_precioServicio,[precioServicio]),
			[id_contrato] = isnull(@p_id_contrato,[id_contrato]),
			[id_zona] = isnull(@p_id_zona,[id_zona])
		WHERE (id = @p_id)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'gen_zona'
-----------------------------------12/10/2012 04:21:15 p.m.