IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_pro_actividad]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_pro_actividad]
GO
CREATE PROCEDURE [reg_pro_actividad]
	@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],
		[actividad] AS [actividad],
		[actividad_codigo] AS [actividad_codigo],
		[descripcionDetallada] AS [descripcionDetallada],
		[fechainicioPlaneada] AS [fechainicioPlaneada],
		[fechainicioProyectada] AS [fechainicioProyectada],
		[fechaInicioReal] AS [fechaInicioReal],
		[duracion] AS [duracion],
		[fechaTerminacionPlaneada] AS [fechaTerminacionPlaneada],
		[fechaTerminacionProyectada] AS [fechaTerminacionProyectada],
		[fechaTerminacionReal] AS [fechaTerminacionReal]
	FROM [pro_actividad]
	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_pro_actividad]')) DROP VIEW [dbo].[view_pro_actividad]
GO
CREATE  VIEW [view_pro_actividad] AS
SELECT 	[pro_actividad].[id] AS [id],
		[pro_actividad].[uid] AS [uid],
		[pro_actividad].[eid] AS [eid],
		[pro_actividad].[id_usuario_modifico] AS [id_usuario_modifico],
		[pro_actividad].[id_usuario_creo] AS [id_usuario_creo],
		[pro_actividad].[fechaModificacion] AS [fechaModificacion],
		[pro_actividad].[fechaCreacion] AS [fechaCreacion],
		[pro_actividad].[active] AS [active],
		[pro_actividad].[actividad] AS [actividad],
		[pro_actividad].[actividad_codigo] AS [actividad_codigo],
		[pro_actividad].[descripcionDetallada] AS [descripcionDetallada],
		[pro_actividad].[fechainicioPlaneada] AS [fechainicioPlaneada],
		[pro_actividad].[fechainicioProyectada] AS [fechainicioProyectada],
		[pro_actividad].[fechaInicioReal] AS [fechaInicioReal],
		[pro_actividad].[duracion] AS [duracion],
		[pro_actividad].[fechaTerminacionPlaneada] AS [fechaTerminacionPlaneada],
		[pro_actividad].[fechaTerminacionProyectada] AS [fechaTerminacionProyectada],
		[pro_actividad].[fechaTerminacionReal] AS [fechaTerminacionReal],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [pro_actividad]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[pro_actividad].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[pro_actividad].[id_usuario_creo]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [pro_actividad].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [pro_actividad].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_pro_actividadesXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_actividadesXusuario_modifico]
GO
CREATE PROCEDURE [sel_pro_actividadesXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_pro_actividad]
	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_pro_actividadesXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_actividadesXusuario_creo]
GO
CREATE PROCEDURE [sel_pro_actividadesXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_pro_actividad]
	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_pro_actividades]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_actividades]
GO
CREATE PROCEDURE [sel_pro_actividades]
	
	@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_pro_actividad]
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_pro_actividades]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_pro_actividades]
GO
CREATE PROCEDURE [lis_pro_actividades]
	AS
BEGIN
	SELECT id AS id,
[actividad] AS [pro_actividad]
	FROM [pro_actividad]
	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_pro_actividad]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_pro_actividad]
GO
CREATE PROCEDURE [upd_pro_actividad]
	@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_actividad varchar(300) = null,
		@p_actividad_codigo varchar(100) = null,
		@p_descripcionDetallada varchar(8000) = null,
		@p_fechainicioPlaneada smalldatetime = null,
		@p_fechainicioProyectada smalldatetime = null,
		@p_fechaInicioReal smalldatetime = null,
		@p_duracion int = null,
		@p_fechaTerminacionPlaneada smalldatetime = null,
		@p_fechaTerminacionProyectada smalldatetime = null,
		@p_fechaTerminacionReal smalldatetime = null
AS
BEGIN
		UPDATE [pro_actividad]
		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(),
			[actividad] = isnull(@p_actividad,[actividad]),
			[actividad_codigo] = isnull(@p_actividad_codigo,[actividad_codigo]),
			[descripcionDetallada] = isnull(@p_descripcionDetallada,[descripcionDetallada]),
			[fechainicioPlaneada] = isnull(@p_fechainicioPlaneada,[fechainicioPlaneada]),
			[fechainicioProyectada] = isnull(@p_fechainicioProyectada,[fechainicioProyectada]),
			[fechaInicioReal] = isnull(@p_fechaInicioReal,[fechaInicioReal]),
			[duracion] = isnull(@p_duracion,[duracion]),
			[fechaTerminacionPlaneada] = isnull(@p_fechaTerminacionPlaneada,[fechaTerminacionPlaneada]),
			[fechaTerminacionProyectada] = isnull(@p_fechaTerminacionProyectada,[fechaTerminacionProyectada]),
			[fechaTerminacionReal] = isnull(@p_fechaTerminacionReal,[fechaTerminacionReal])
		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_pro_actividad]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_pro_actividad]
GO
CREATE PROCEDURE [ins_pro_actividad]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_actividad varchar(300),
		@p_actividad_codigo varchar(100),
		@p_descripcionDetallada varchar(8000),
		@p_fechainicioPlaneada smalldatetime,
		@p_fechainicioProyectada smalldatetime,
		@p_fechaInicioReal smalldatetime,
		@p_duracion int,
		@p_fechaTerminacionPlaneada smalldatetime,
		@p_fechaTerminacionProyectada smalldatetime,
		@p_fechaTerminacionReal smalldatetime
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [pro_actividad]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[actividad],
			[actividad_codigo],
			[descripcionDetallada],
			[fechainicioPlaneada],
			[fechainicioProyectada],
			[fechaInicioReal],
			[duracion],
			[fechaTerminacionPlaneada],
			[fechaTerminacionProyectada],
			[fechaTerminacionReal])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_actividad,
			@p_actividad_codigo,
			@p_descripcionDetallada,
			@p_fechainicioPlaneada,
			@p_fechainicioProyectada,
			@p_fechaInicioReal,
			@p_duracion,
			@p_fechaTerminacionPlaneada,
			@p_fechaTerminacionProyectada,
			@p_fechaTerminacionReal)
		SET @v_id = scope_identity()
UPDATE [pro_actividad] 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_pro_actividad]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_pro_actividad]
GO
CREATE PROCEDURE [act_pro_actividad]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [pro_actividad]
	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_pro_actividad_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_pro_actividad_m]
GO
CREATE PROCEDURE [reg_pro_actividad_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],
		[actividad] AS [actividad],
		[actividad_codigo] AS [actividad_codigo],
		[descripcionDetallada] AS [descripcionDetallada],
		[fechainicioPlaneada] AS [fechainicioPlaneada],
		[fechainicioProyectada] AS [fechainicioProyectada],
		[fechaInicioReal] AS [fechaInicioReal],
		[duracion] AS [duracion],
		[fechaTerminacionPlaneada] AS [fechaTerminacionPlaneada],
		[fechaTerminacionProyectada] AS [fechaTerminacionProyectada],
		[fechaTerminacionReal] AS [fechaTerminacionReal]
	FROM [pro_actividad]
	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_pro_actividadesXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_actividadesXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_pro_actividadesXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_pro_actividad]
	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_pro_actividadesXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_actividadesXusuario_creo_m]
GO
CREATE PROCEDURE [sel_pro_actividadesXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_pro_actividad]
	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_pro_actividades_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_actividades_m]
GO
CREATE PROCEDURE [sel_pro_actividades_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_pro_actividad]
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_pro_actividades_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_pro_actividades_m]
GO
CREATE PROCEDURE [lis_pro_actividades_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[actividad] AS [pro_actividad]
	FROM [pro_actividad]
	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_pro_actividad]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_pro_actividad]
GO
CREATE PROCEDURE [aud_pro_actividad]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [pro_actividad].id as id_tabla,
	[pro_actividad].id_usuario_modifico as id_usuario_modifico,
	[pro_actividad].id_usuario_creo as id_usuario_creo,
	[pro_actividad].fechaModificacion as fechaModificacion,
	[pro_actividad].fechaCreacion as fechaCreacion,
	[pro_actividad].active as active,
	[pro_actividad].[actividad] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [pro_actividad]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[pro_actividad].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[pro_actividad].[id_usuario_creo]
	where ' + @p_filtro)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'pro_actividad'
-----------------------------------30/06/2016 16:05:16