

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_etapa]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_pro_actividad_etapa]
GO
CREATE PROCEDURE [reg_pro_actividad_etapa]
	@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_etapa] AS [actividad_etapa],
		[id_etapa] AS [id_etapa],
		[id_actividad] AS [id_actividad]
	FROM [pro_actividad_etapa]
	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_etapa]')) DROP VIEW [dbo].[view_pro_actividad_etapa]
GO
CREATE  VIEW [view_pro_actividad_etapa] AS
SELECT 	[pro_actividad_etapa].[id] AS [id],
		[pro_actividad_etapa].[uid] AS [uid],
		[pro_actividad_etapa].[eid] AS [eid],
		[pro_actividad_etapa].[id_usuario_modifico] AS [id_usuario_modifico],
		[pro_actividad_etapa].[id_usuario_creo] AS [id_usuario_creo],
		[pro_actividad_etapa].[fechaModificacion] AS [fechaModificacion],
		[pro_actividad_etapa].[fechaCreacion] AS [fechaCreacion],
		[pro_actividad_etapa].[active] AS [active],
		[pro_actividad_etapa].[actividad_etapa] AS [actividad_etapa],
		[pro_actividad_etapa].[id_etapa] AS [id_etapa],
[pro_etapa].[etapa] as [pro_etapa_etapa],
[pro_etapa].[etapa_codigo] as [pro_etapa_etapa_codigo],
[pro_etapa].[descripcion] as [pro_etapa_descripcion],
[pro_etapa].[fechaInicioPlaneada_ff] as [pro_etapa_fechaInicioPlaneada_ff],
[pro_etapa].[fechaInicioProyectada_ff] as [pro_etapa_fechaInicioProyectada_ff],
[pro_etapa].[fechaInicioReal_ff] as [pro_etapa_fechaInicioReal_ff],
[pro_etapa].[fechaFinPlaneada_ff] as [pro_etapa_fechaFinPlaneada_ff],
[pro_etapa].[fechaFinProyectada_ff] as [pro_etapa_fechaFinProyectada_ff],
[pro_etapa].[fechaFinReal_ff] as [pro_etapa_fechaFinReal_ff],
[pro_etapa].[orden] as [pro_etapa_orden],
[pro_etapa].[completado] as [pro_etapa_completado],
[pro_etapa].[porcentajePresupuesto] as [pro_etapa_porcentajePresupuesto],
[pro_etapa].[valorPresupuesto] as [pro_etapa_valorPresupuesto],
[pro_etapa].[fechaInicioPlaneada] as [pro_etapa_fechaInicioPlaneada],
[pro_etapa].[fechaInicioProyectada] as [pro_etapa_fechaInicioProyectada],
[pro_etapa].[fechaInicioReal] as [pro_etapa_fechaInicioReal],
[pro_etapa].[fechaFinPlaneada] as [pro_etapa_fechaFinPlaneada],
[pro_etapa].[fechaFinProyectada] as [pro_etapa_fechaFinProyectada],
[pro_etapa].[fechaFinReal] as [pro_etapa_fechaFinReal],
		[pro_actividad_etapa].[id_actividad] AS [id_actividad],
[pro_actividad].[actividad] as [pro_actividad_actividad],
[pro_actividad].[actividad_codigo] as [pro_actividad_actividad_codigo],
[pro_actividad].[descripcionDetallada] as [pro_actividad_descripcionDetallada],
[pro_actividad].[fechainicioPlaneada] as [pro_actividad_fechainicioPlaneada],
[pro_actividad].[fechainicioProyectada] as [pro_actividad_fechainicioProyectada],
[pro_actividad].[fechaInicioReal] as [pro_actividad_fechaInicioReal],
[pro_actividad].[duracion] as [pro_actividad_duracion],
[pro_actividad].[fechaTerminacionPlaneada] as [pro_actividad_fechaTerminacionPlaneada],
[pro_actividad].[fechaTerminacionProyectada] as [pro_actividad_fechaTerminacionProyectada],
[pro_actividad].[fechaTerminacionReal] as [pro_actividad_fechaTerminacionReal],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [pro_actividad_etapa]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[pro_actividad_etapa].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[pro_actividad_etapa].[id_usuario_creo]
	INNER JOIN [pro_etapa] ON [pro_etapa].id=[pro_actividad_etapa].[id_etapa]
	INNER JOIN [pro_actividad] ON [pro_actividad].id=[pro_actividad_etapa].[id_actividad]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [pro_actividad_etapa].eid = [empresaCodigo].[codigo] and empresaCodigo.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_actividad_etapasXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_actividad_etapasXusuario_modifico]
GO
CREATE PROCEDURE [sel_pro_actividad_etapasXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_pro_actividad_etapa]
	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_actividad_etapasXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_actividad_etapasXusuario_creo]
GO
CREATE PROCEDURE [sel_pro_actividad_etapasXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_pro_actividad_etapa]
	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_actividad_etapasXetapa]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_actividad_etapasXetapa]
GO
CREATE PROCEDURE [sel_pro_actividad_etapasXetapa]
	@p_id_etapa INT
AS
BEGIN
	SELECT * 
	FROM [view_pro_actividad_etapa]
	WHERE ([id_etapa]= @p_id_etapa)
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_actividad_etapasXactividad]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_actividad_etapasXactividad]
GO
CREATE PROCEDURE [sel_pro_actividad_etapasXactividad]
	@p_id_actividad INT
AS
BEGIN
	SELECT * 
	FROM [view_pro_actividad_etapa]
	WHERE ([id_actividad]= @p_id_actividad)
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_actividad_etapas]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_actividad_etapas]
GO
CREATE PROCEDURE [sel_pro_actividad_etapas]
	
	@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_etapa]
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_actividad_etapas]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_pro_actividad_etapas]
GO
CREATE PROCEDURE [lis_pro_actividad_etapas]
	AS
BEGIN
	SELECT id AS id,
[actividad_etapa] AS [pro_actividad_etapa]
	FROM [pro_actividad_etapa]
	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_etapa]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_pro_actividad_etapa]
GO
CREATE PROCEDURE [upd_pro_actividad_etapa]
	@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_etapa varchar(300) = null,
		@p_id_etapa int = null,
		@p_id_actividad int = null
AS
BEGIN
		UPDATE [pro_actividad_etapa]
		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_etapa] = isnull(@p_actividad_etapa,[actividad_etapa]),
			[id_etapa] = isnull(@p_id_etapa,[id_etapa]),
			[id_actividad] = isnull(@p_id_actividad,[id_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 OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ins_pro_actividad_etapa]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_pro_actividad_etapa]
GO
CREATE PROCEDURE [ins_pro_actividad_etapa]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_actividad_etapa varchar(300),
		@p_id_etapa int,
		@p_id_actividad int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [pro_actividad_etapa]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[actividad_etapa],
			[id_etapa],
			[id_actividad])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_actividad_etapa,
			@p_id_etapa,
			@p_id_actividad)
		SET @v_id = scope_identity()
UPDATE [pro_actividad_etapa] 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_etapa]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_pro_actividad_etapa]
GO
CREATE PROCEDURE [act_pro_actividad_etapa]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [pro_actividad_etapa]
	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_etapa_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_pro_actividad_etapa_m]
GO
CREATE PROCEDURE [reg_pro_actividad_etapa_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_etapa] AS [actividad_etapa],
		[id_etapa] AS [id_etapa],
		[id_actividad] AS [id_actividad]
	FROM [pro_actividad_etapa]
	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_actividad_etapasXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_actividad_etapasXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_pro_actividad_etapasXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_pro_actividad_etapa]
	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_actividad_etapasXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_actividad_etapasXusuario_creo_m]
GO
CREATE PROCEDURE [sel_pro_actividad_etapasXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_pro_actividad_etapa]
	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_actividad_etapasXetapa_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_actividad_etapasXetapa_m]
GO
CREATE PROCEDURE [sel_pro_actividad_etapasXetapa_m]
	@p_id_etapa INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_pro_actividad_etapa]
	WHERE ([id_etapa]= @p_id_etapa)
	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_actividad_etapasXactividad_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_actividad_etapasXactividad_m]
GO
CREATE PROCEDURE [sel_pro_actividad_etapasXactividad_m]
	@p_id_actividad INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_pro_actividad_etapa]
	WHERE ([id_actividad]= @p_id_actividad)
	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_actividad_etapas_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_actividad_etapas_m]
GO
CREATE PROCEDURE [sel_pro_actividad_etapas_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_etapa]
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_actividad_etapas_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_pro_actividad_etapas_m]
GO
CREATE PROCEDURE [lis_pro_actividad_etapas_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[actividad_etapa] AS [pro_actividad_etapa]
	FROM [pro_actividad_etapa]
	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_etapa]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_pro_actividad_etapa]
GO
CREATE PROCEDURE [aud_pro_actividad_etapa]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [pro_actividad_etapa].id as id_tabla,
	[pro_actividad_etapa].id_usuario_modifico as id_usuario_modifico,
	[pro_actividad_etapa].id_usuario_creo as id_usuario_creo,
	[pro_actividad_etapa].fechaModificacion as fechaModificacion,
	[pro_actividad_etapa].fechaCreacion as fechaCreacion,
	[pro_actividad_etapa].active as active,
	[pro_actividad_etapa].[actividad_etapa] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [pro_actividad_etapa]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[pro_actividad_etapa].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[pro_actividad_etapa].[id_usuario_creo]
	where ' + @p_filtro)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'pro_actividad_etapa'
-----------------------------------11/08/2020 10:17:50 a. m.