

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_geo_ubicacion]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_geo_ubicacion]
GO
CREATE PROCEDURE [reg_geo_ubicacion]
	@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],
		[ubicacion] AS [ubicacion],
		[ubicacion_codigo] AS [ubicacion_codigo],
		[tabla] AS [tabla],
		[idObjeto] AS [idObjeto],
		[longitud] AS [longitud],
		[latitud] AS [latitud],
		[fecha_fh] AS [fecha_fh]
	FROM [geo_ubicacion]
	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_geo_ubicacion]')) DROP VIEW [dbo].[view_geo_ubicacion]
GO
CREATE  VIEW [view_geo_ubicacion] AS
SELECT 	[geo_ubicacion].[id] AS [id],
		[geo_ubicacion].[uid] AS [uid],
		[geo_ubicacion].[eid] AS [eid],
		[geo_ubicacion].[id_usuario_modifico] AS [id_usuario_modifico],
		[geo_ubicacion].[id_usuario_creo] AS [id_usuario_creo],
		[geo_ubicacion].[fechaModificacion] AS [fechaModificacion],
		[geo_ubicacion].[fechaCreacion] AS [fechaCreacion],
		[geo_ubicacion].[active] AS [active],
		[geo_ubicacion].[ubicacion] AS [ubicacion],
		[geo_ubicacion].[ubicacion_codigo] AS [ubicacion_codigo],
		[geo_ubicacion].[tabla] AS [tabla],
		[geo_ubicacion].[idObjeto] AS [idObjeto],
		[geo_ubicacion].[longitud] AS [longitud],
		[geo_ubicacion].[latitud] AS [latitud],
		[geo_ubicacion].[fecha_fh] AS [fecha_fh],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [geo_ubicacion]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[geo_ubicacion].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[geo_ubicacion].[id_usuario_creo]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [geo_ubicacion].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [geo_ubicacion].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_geo_ubicacionesXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_geo_ubicacionesXusuario_modifico]
GO
CREATE PROCEDURE [sel_geo_ubicacionesXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_geo_ubicacion]
	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_geo_ubicacionesXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_geo_ubicacionesXusuario_creo]
GO
CREATE PROCEDURE [sel_geo_ubicacionesXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_geo_ubicacion]
	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_geo_ubicaciones]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_geo_ubicaciones]
GO
CREATE PROCEDURE [sel_geo_ubicaciones]
	
	@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_geo_ubicacion]
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_geo_ubicaciones]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_geo_ubicaciones]
GO
CREATE PROCEDURE [lis_geo_ubicaciones]
	AS
BEGIN
	SELECT id AS id,
[ubicacion] AS [geo_ubicacion]
	FROM [geo_ubicacion]
	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_geo_ubicacion]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_geo_ubicacion]
GO
CREATE PROCEDURE [upd_geo_ubicacion]
	@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_ubicacion varchar(300) = null,
		@p_ubicacion_codigo varchar(300) = null,
		@p_tabla varchar(100) = null,
		@p_idObjeto int = null,
		@p_longitud float = null,
		@p_latitud float = null,
		@p_fecha_fh smalldatetime = null
AS
BEGIN
		UPDATE [geo_ubicacion]
		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(),
			[ubicacion] = isnull(@p_ubicacion,[ubicacion]),
			[ubicacion_codigo] = isnull(@p_ubicacion_codigo,[ubicacion_codigo]),
			[tabla] = isnull(@p_tabla,[tabla]),
			[idObjeto] = isnull(@p_idObjeto,[idObjeto]),
			[longitud] = isnull(@p_longitud,[longitud]),
			[latitud] = isnull(@p_latitud,[latitud]),
			[fecha_fh] = isnull(@p_fecha_fh,[fecha_fh])
		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_geo_ubicacion]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_geo_ubicacion]
GO
CREATE PROCEDURE [ins_geo_ubicacion]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_ubicacion varchar(300),
		@p_ubicacion_codigo varchar(300),
		@p_tabla varchar(100),
		@p_idObjeto int,
		@p_longitud float,
		@p_latitud float,
		@p_fecha_fh smalldatetime
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [geo_ubicacion]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[ubicacion],
			[ubicacion_codigo],
			[tabla],
			[idObjeto],
			[longitud],
			[latitud],
			[fecha_fh])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_ubicacion,
			@p_ubicacion_codigo,
			@p_tabla,
			@p_idObjeto,
			@p_longitud,
			@p_latitud,
			@p_fecha_fh)
		SET @v_id = scope_identity()
UPDATE [geo_ubicacion] 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_geo_ubicacion]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_geo_ubicacion]
GO
CREATE PROCEDURE [act_geo_ubicacion]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [geo_ubicacion]
	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_geo_ubicacion_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_geo_ubicacion_m]
GO
CREATE PROCEDURE [reg_geo_ubicacion_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],
		[ubicacion] AS [ubicacion],
		[ubicacion_codigo] AS [ubicacion_codigo],
		[tabla] AS [tabla],
		[idObjeto] AS [idObjeto],
		[longitud] AS [longitud],
		[latitud] AS [latitud],
		[fecha_fh] AS [fecha_fh]
	FROM [geo_ubicacion]
	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_geo_ubicacionesXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_geo_ubicacionesXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_geo_ubicacionesXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_geo_ubicacion]
	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_geo_ubicacionesXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_geo_ubicacionesXusuario_creo_m]
GO
CREATE PROCEDURE [sel_geo_ubicacionesXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_geo_ubicacion]
	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_geo_ubicaciones_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_geo_ubicaciones_m]
GO
CREATE PROCEDURE [sel_geo_ubicaciones_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_geo_ubicacion]
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_geo_ubicaciones_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_geo_ubicaciones_m]
GO
CREATE PROCEDURE [lis_geo_ubicaciones_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[ubicacion] AS [geo_ubicacion]
	FROM [geo_ubicacion]
	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_geo_ubicacion]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_geo_ubicacion]
GO
CREATE PROCEDURE [aud_geo_ubicacion]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [geo_ubicacion].id as id_tabla,
	[geo_ubicacion].id_usuario_modifico as id_usuario_modifico,
	[geo_ubicacion].id_usuario_creo as id_usuario_creo,
	[geo_ubicacion].fechaModificacion as fechaModificacion,
	[geo_ubicacion].fechaCreacion as fechaCreacion,
	[geo_ubicacion].active as active,
	[geo_ubicacion].[ubicacion] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [geo_ubicacion]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[geo_ubicacion].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[geo_ubicacion].[id_usuario_creo]
	where ' + @p_filtro)
END

