IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_pro_etapa]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_pro_etapa]
GO
CREATE PROCEDURE [reg_pro_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],
		[etapa] AS [etapa],
		[etapa_codigo] AS [etapa_codigo],
		[descripcion] AS [descripcion],
		[fechaInicioPlaneada] AS [fechaInicioPlaneada],
		[fechaInicioProyectada] AS [fechaInicioProyectada],
		[fechaInicioReal] AS [fechaInicioReal],
		[fechaFinPlaneada] AS [fechaFinPlaneada],
		[fechaFinProyectada] AS [fechaFinProyectada],
		[fechaFinReal] AS [fechaFinReal],
		[orden] AS [orden],
		[completado] AS [completado],
		[porcentajePresupuesto] AS [porcentajePresupuesto],
		[valorPresupuesto] AS [valorPresupuesto]
	FROM [pro_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_etapa]')) DROP VIEW [dbo].[view_pro_etapa]
GO
CREATE  VIEW [view_pro_etapa] AS
SELECT 	[pro_etapa].[id] AS [id],
		[pro_etapa].[uid] AS [uid],
		[pro_etapa].[eid] AS [eid],
		[pro_etapa].[id_usuario_modifico] AS [id_usuario_modifico],
		[pro_etapa].[id_usuario_creo] AS [id_usuario_creo],
		[pro_etapa].[fechaModificacion] AS [fechaModificacion],
		[pro_etapa].[fechaCreacion] AS [fechaCreacion],
		[pro_etapa].[active] AS [active],
		[pro_etapa].[etapa] AS [etapa],
		[pro_etapa].[etapa_codigo] AS [etapa_codigo],
		[pro_etapa].[descripcion] AS [descripcion],
		[pro_etapa].[fechaInicioPlaneada] AS [fechaInicioPlaneada],
		[pro_etapa].[fechaInicioProyectada] AS [fechaInicioProyectada],
		[pro_etapa].[fechaInicioReal] AS [fechaInicioReal],
		[pro_etapa].[fechaFinPlaneada] AS [fechaFinPlaneada],
		[pro_etapa].[fechaFinProyectada] AS [fechaFinProyectada],
		[pro_etapa].[fechaFinReal] AS [fechaFinReal],
		[pro_etapa].[orden] AS [orden],
		[pro_etapa].[completado] AS [completado],
		[pro_etapa].[porcentajePresupuesto] AS [porcentajePresupuesto],
		[pro_etapa].[valorPresupuesto] AS [valorPresupuesto],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [pro_etapa]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[pro_etapa].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[pro_etapa].[id_usuario_creo]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [pro_etapa].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [pro_etapa].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_etapasXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_etapasXusuario_modifico]
GO
CREATE PROCEDURE [sel_pro_etapasXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_pro_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_etapasXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_etapasXusuario_creo]
GO
CREATE PROCEDURE [sel_pro_etapasXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_pro_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_etapas]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_etapas]
GO
CREATE PROCEDURE [sel_pro_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_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_etapas]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_pro_etapas]
GO
CREATE PROCEDURE [lis_pro_etapas]
	AS
BEGIN
	SELECT id AS id,
[etapa] AS [pro_etapa]
	FROM [pro_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_etapa]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_pro_etapa]
GO
CREATE PROCEDURE [upd_pro_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_etapa varchar(300) = null,
		@p_etapa_codigo varchar(100) = null,
		@p_descripcion varchar(500) = null,
		@p_fechaInicioPlaneada date = null,
		@p_fechaInicioProyectada date = null,
		@p_fechaInicioReal date = null,
		@p_fechaFinPlaneada date = null,
		@p_fechaFinProyectada date = null,
		@p_fechaFinReal date = null,
		@p_orden int = null,
		@p_completado int = null,
		@p_porcentajePresupuesto float = null,
		@p_valorPresupuesto money = null
AS
BEGIN
		UPDATE [pro_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(),
			[etapa] = isnull(@p_etapa,[etapa]),
			[etapa_codigo] = isnull(@p_etapa_codigo,[etapa_codigo]),
			[descripcion] = isnull(@p_descripcion,[descripcion]),
			[fechaInicioPlaneada] = isnull(@p_fechaInicioPlaneada,[fechaInicioPlaneada]),
			[fechaInicioProyectada] = isnull(@p_fechaInicioProyectada,[fechaInicioProyectada]),
			[fechaInicioReal] = isnull(@p_fechaInicioReal,[fechaInicioReal]),
			[fechaFinPlaneada] = isnull(@p_fechaFinPlaneada,[fechaFinPlaneada]),
			[fechaFinProyectada] = isnull(@p_fechaFinProyectada,[fechaFinProyectada]),
			[fechaFinReal] = isnull(@p_fechaFinReal,[fechaFinReal]),
			[orden] = isnull(@p_orden,[orden]),
			[completado] = isnull(@p_completado,[completado]),
			[porcentajePresupuesto] = isnull(@p_porcentajePresupuesto,[porcentajePresupuesto]),
			[valorPresupuesto] = isnull(@p_valorPresupuesto,[valorPresupuesto])
		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_etapa]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_pro_etapa]
GO
CREATE PROCEDURE [ins_pro_etapa]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_etapa varchar(300),
		@p_etapa_codigo varchar(100),
		@p_descripcion varchar(500),
		@p_fechaInicioPlaneada date,
		@p_fechaInicioProyectada date,
		@p_fechaInicioReal date,
		@p_fechaFinPlaneada date,
		@p_fechaFinProyectada date,
		@p_fechaFinReal date,
		@p_orden int,
		@p_completado int,
		@p_porcentajePresupuesto float,
		@p_valorPresupuesto money
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [pro_etapa]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[etapa],
			[etapa_codigo],
			[descripcion],
			[fechaInicioPlaneada],
			[fechaInicioProyectada],
			[fechaInicioReal],
			[fechaFinPlaneada],
			[fechaFinProyectada],
			[fechaFinReal],
			[orden],
			[completado],
			[porcentajePresupuesto],
			[valorPresupuesto])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_etapa,
			@p_etapa_codigo,
			@p_descripcion,
			@p_fechaInicioPlaneada,
			@p_fechaInicioProyectada,
			@p_fechaInicioReal,
			@p_fechaFinPlaneada,
			@p_fechaFinProyectada,
			@p_fechaFinReal,
			@p_orden,
			@p_completado,
			@p_porcentajePresupuesto,
			@p_valorPresupuesto)
		SET @v_id = scope_identity()
UPDATE [pro_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_etapa]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_pro_etapa]
GO
CREATE PROCEDURE [act_pro_etapa]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [pro_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_etapa_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_pro_etapa_m]
GO
CREATE PROCEDURE [reg_pro_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],
		[etapa] AS [etapa],
		[etapa_codigo] AS [etapa_codigo],
		[descripcion] AS [descripcion],
		[fechaInicioPlaneada] AS [fechaInicioPlaneada],
		[fechaInicioProyectada] AS [fechaInicioProyectada],
		[fechaInicioReal] AS [fechaInicioReal],
		[fechaFinPlaneada] AS [fechaFinPlaneada],
		[fechaFinProyectada] AS [fechaFinProyectada],
		[fechaFinReal] AS [fechaFinReal],
		[orden] AS [orden],
		[completado] AS [completado],
		[porcentajePresupuesto] AS [porcentajePresupuesto],
		[valorPresupuesto] AS [valorPresupuesto]
	FROM [pro_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_etapasXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_etapasXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_pro_etapasXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_pro_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_etapasXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_etapasXusuario_creo_m]
GO
CREATE PROCEDURE [sel_pro_etapasXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_pro_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_etapas_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_etapas_m]
GO
CREATE PROCEDURE [sel_pro_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_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_etapas_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_pro_etapas_m]
GO
CREATE PROCEDURE [lis_pro_etapas_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[etapa] AS [pro_etapa]
	FROM [pro_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_etapa]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_pro_etapa]
GO
CREATE PROCEDURE [aud_pro_etapa]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [pro_etapa].id as id_tabla,
	[pro_etapa].id_usuario_modifico as id_usuario_modifico,
	[pro_etapa].id_usuario_creo as id_usuario_creo,
	[pro_etapa].fechaModificacion as fechaModificacion,
	[pro_etapa].fechaCreacion as fechaCreacion,
	[pro_etapa].active as active,
	[pro_etapa].[etapa] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [pro_etapa]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[pro_etapa].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[pro_etapa].[id_usuario_creo]
	where ' + @p_filtro)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'pro_etapa'
-----------------------------------15/07/2015 8:32:12