

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_entregable]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_pro_entregable]
GO
CREATE PROCEDURE [reg_pro_entregable]
	@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],
		[entregable] AS [entregable],
		[entregable_codigo] AS [entregable_codigo]
	FROM [pro_entregable]
	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_entregable]')) DROP VIEW [dbo].[view_pro_entregable]
GO
CREATE  VIEW [view_pro_entregable] AS
SELECT 	[pro_entregable].[id] AS [id],
		[pro_entregable].[uid] AS [uid],
		[pro_entregable].[eid] AS [eid],
		[pro_entregable].[id_usuario_modifico] AS [id_usuario_modifico],
		[pro_entregable].[id_usuario_creo] AS [id_usuario_creo],
		[pro_entregable].[fechaModificacion] AS [fechaModificacion],
		[pro_entregable].[fechaCreacion] AS [fechaCreacion],
		[pro_entregable].[active] AS [active],
		[pro_entregable].[entregable] AS [entregable],
		[pro_entregable].[entregable_codigo] AS [entregable_codigo],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [pro_entregable]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[pro_entregable].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[pro_entregable].[id_usuario_creo]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [pro_entregable].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [pro_entregable].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_entregablesXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_entregablesXusuario_modifico]
GO
CREATE PROCEDURE [sel_pro_entregablesXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_pro_entregable]
	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_entregablesXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_entregablesXusuario_creo]
GO
CREATE PROCEDURE [sel_pro_entregablesXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_pro_entregable]
	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_entregables]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_entregables]
GO
CREATE PROCEDURE [sel_pro_entregables]
	
	@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_entregable]
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_entregables]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_pro_entregables]
GO
CREATE PROCEDURE [lis_pro_entregables]
	AS
BEGIN
	SELECT id AS id,
[entregable] AS [pro_entregable]
	FROM [pro_entregable]
	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_entregable]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_pro_entregable]
GO
CREATE PROCEDURE [upd_pro_entregable]
	@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_entregable varchar(300) = null,
		@p_entregable_codigo varchar(100) = null
AS
BEGIN
		UPDATE [pro_entregable]
		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(),
			[entregable] = isnull(@p_entregable,[entregable]),
			[entregable_codigo] = isnull(@p_entregable_codigo,[entregable_codigo])
		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_entregable]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_pro_entregable]
GO
CREATE PROCEDURE [ins_pro_entregable]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_entregable varchar(300),
		@p_entregable_codigo varchar(100)
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [pro_entregable]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[entregable],
			[entregable_codigo])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_entregable,
			@p_entregable_codigo)
		SET @v_id = scope_identity()
UPDATE [pro_entregable] 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_entregable]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_pro_entregable]
GO
CREATE PROCEDURE [act_pro_entregable]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [pro_entregable]
	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_entregable_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_pro_entregable_m]
GO
CREATE PROCEDURE [reg_pro_entregable_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],
		[entregable] AS [entregable],
		[entregable_codigo] AS [entregable_codigo]
	FROM [pro_entregable]
	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_entregablesXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_entregablesXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_pro_entregablesXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_pro_entregable]
	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_entregablesXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_entregablesXusuario_creo_m]
GO
CREATE PROCEDURE [sel_pro_entregablesXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_pro_entregable]
	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_entregables_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_entregables_m]
GO
CREATE PROCEDURE [sel_pro_entregables_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_entregable]
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_entregables_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_pro_entregables_m]
GO
CREATE PROCEDURE [lis_pro_entregables_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[entregable] AS [pro_entregable]
	FROM [pro_entregable]
	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_entregable]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_pro_entregable]
GO
CREATE PROCEDURE [aud_pro_entregable]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [pro_entregable].id as id_tabla,
	[pro_entregable].id_usuario_modifico as id_usuario_modifico,
	[pro_entregable].id_usuario_creo as id_usuario_creo,
	[pro_entregable].fechaModificacion as fechaModificacion,
	[pro_entregable].fechaCreacion as fechaCreacion,
	[pro_entregable].active as active,
	[pro_entregable].[entregable] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [pro_entregable]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[pro_entregable].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[pro_entregable].[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_doc_documento_proyectosXentregable]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_documento_proyectosXentregable]
GO
CREATE PROCEDURE [sel_doc_documento_proyectosXentregable]
	@p_id_pro_entregable INT
AS
BEGIN
	SELECT 	[doc_documento.proyecto_entregable].[id] AS [I_id],
		[doc_documento.proyecto_entregable].[uid] AS [I_uid],
		[doc_documento.proyecto_entregable].[eid] AS [I_eid],
		[doc_documento.proyecto_entregable].[fechaModificacion] AS [I_fechaModificacion],
		[doc_documento.proyecto_entregable].[fechaCreacion] AS [I_fechaCreacion],
		[doc_documento.proyecto_entregable].[active] AS [I_active],
		[doc_documento.proyecto_entregable].[documento.proyecto_entregable] AS [I_documento.proyecto_entregable]
	FROM doc_documento.proyecto_entregable INNER JOIN doc_documento.proyecto
	 ON [doc_documento.proyecto_entregable].[id_documento.proyecto] = [doc_documento.proyecto].id
	WHERE	([doc_documento.proyecto_entregable].[id_entregable]= @p_id_pro_entregable)
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_doc_documento_proyecto_entregable]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_doc_documento_proyecto_entregable]
GO
CREATE PROCEDURE [ins_doc_documento_proyecto_entregable]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_documento_proyecto_entregable varchar(300),
		@p_id_documento_proyecto int,
		@p_id_entregable int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [doc_documento.proyecto_entregable]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[documento.proyecto_entregable],
			[id_documento.proyecto],
			[id_entregable])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_documento_proyecto_entregable,
			@p_id_documento_proyecto,
			@p_id_entregable)
		SET @v_id = scope_identity()
UPDATE [doc_documento.proyecto_entregable] 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_doc_documento_proyecto_entregable]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_doc_documento_proyecto_entregable]
GO
CREATE PROCEDURE [upd_doc_documento_proyecto_entregable]
	@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_documento_proyecto_entregable varchar(300) = null,
		@p_id_documento_proyecto int = null,
		@p_id_entregable int = null
AS
BEGIN
		UPDATE [doc_documento.proyecto_entregable]
		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(),
			[documento.proyecto_entregable] = isnull(@p_documento_proyecto_entregable,[documento.proyecto_entregable]),
			[id_documento.proyecto] = isnull(@p_id_documento_proyecto,[id_documento.proyecto]),
			[id_entregable] = isnull(@p_id_entregable,[id_entregable])
		WHERE (id = @p_id)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'pro_entregable'
-----------------------------------17/06/2015 04:24:07 p.m.