

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_calidad]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_pro_calidad]
GO
CREATE PROCEDURE [reg_pro_calidad]
	@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],
		[calidad] AS [calidad],
		[calidad_codigo] AS [calidad_codigo]
	FROM [pro_calidad]
	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_calidad]')) DROP VIEW [dbo].[view_pro_calidad]
GO
CREATE  VIEW [view_pro_calidad] AS
SELECT 	[pro_calidad].[id] AS [id],
		[pro_calidad].[uid] AS [uid],
		[pro_calidad].[eid] AS [eid],
		[pro_calidad].[id_usuario_modifico] AS [id_usuario_modifico],
		[pro_calidad].[id_usuario_creo] AS [id_usuario_creo],
		[pro_calidad].[fechaModificacion] AS [fechaModificacion],
		[pro_calidad].[fechaCreacion] AS [fechaCreacion],
		[pro_calidad].[active] AS [active],
		[pro_calidad].[calidad] AS [calidad],
		[pro_calidad].[calidad_codigo] AS [calidad_codigo],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [pro_calidad]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[pro_calidad].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[pro_calidad].[id_usuario_creo]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [pro_calidad].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [pro_calidad].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_calidadesXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_calidadesXusuario_modifico]
GO
CREATE PROCEDURE [sel_pro_calidadesXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_pro_calidad]
	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_calidadesXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_calidadesXusuario_creo]
GO
CREATE PROCEDURE [sel_pro_calidadesXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_pro_calidad]
	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_calidades]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_calidades]
GO
CREATE PROCEDURE [sel_pro_calidades]
	
	@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_calidad]
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_calidades]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_pro_calidades]
GO
CREATE PROCEDURE [lis_pro_calidades]
	AS
BEGIN
	SELECT id AS id,
[calidad] AS [pro_calidad]
	FROM [pro_calidad]
	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_calidad]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_pro_calidad]
GO
CREATE PROCEDURE [upd_pro_calidad]
	@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_calidad varchar(300) = null,
		@p_calidad_codigo varchar(100) = null
AS
BEGIN
		UPDATE [pro_calidad]
		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(),
			[calidad] = isnull(@p_calidad,[calidad]),
			[calidad_codigo] = isnull(@p_calidad_codigo,[calidad_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_calidad]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_pro_calidad]
GO
CREATE PROCEDURE [ins_pro_calidad]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_calidad varchar(300),
		@p_calidad_codigo varchar(100)
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [pro_calidad]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[calidad],
			[calidad_codigo])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_calidad,
			@p_calidad_codigo)
		SET @v_id = scope_identity()
UPDATE [pro_calidad] 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_calidad]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_pro_calidad]
GO
CREATE PROCEDURE [act_pro_calidad]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [pro_calidad]
	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_calidad_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_pro_calidad_m]
GO
CREATE PROCEDURE [reg_pro_calidad_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],
		[calidad] AS [calidad],
		[calidad_codigo] AS [calidad_codigo]
	FROM [pro_calidad]
	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_calidadesXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_calidadesXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_pro_calidadesXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_pro_calidad]
	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_calidadesXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_calidadesXusuario_creo_m]
GO
CREATE PROCEDURE [sel_pro_calidadesXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_pro_calidad]
	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_calidades_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_calidades_m]
GO
CREATE PROCEDURE [sel_pro_calidades_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_calidad]
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_calidades_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_pro_calidades_m]
GO
CREATE PROCEDURE [lis_pro_calidades_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[calidad] AS [pro_calidad]
	FROM [pro_calidad]
	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_calidad]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_pro_calidad]
GO
CREATE PROCEDURE [aud_pro_calidad]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [pro_calidad].id as id_tabla,
	[pro_calidad].id_usuario_modifico as id_usuario_modifico,
	[pro_calidad].id_usuario_creo as id_usuario_creo,
	[pro_calidad].fechaModificacion as fechaModificacion,
	[pro_calidad].fechaCreacion as fechaCreacion,
	[pro_calidad].active as active,
	[pro_calidad].[calidad] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [pro_calidad]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[pro_calidad].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[pro_calidad].[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_proyectosXcalidad]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_documento_proyectosXcalidad]
GO
CREATE PROCEDURE [sel_doc_documento_proyectosXcalidad]
	@p_id_pro_calidad INT
AS
BEGIN
	SELECT 	[doc_documento.proyecto_calidad].[id] AS [I_id],
		[doc_documento.proyecto_calidad].[uid] AS [I_uid],
		[doc_documento.proyecto_calidad].[eid] AS [I_eid],
		[doc_documento.proyecto_calidad].[fechaModificacion] AS [I_fechaModificacion],
		[doc_documento.proyecto_calidad].[fechaCreacion] AS [I_fechaCreacion],
		[doc_documento.proyecto_calidad].[active] AS [I_active]
	FROM doc_documento.proyecto_calidad INNER JOIN doc_documento.proyecto
	 ON [doc_documento.proyecto_calidad].[id_documento.proyecto] = [doc_documento.proyecto].id
	WHERE	([doc_documento.proyecto_calidad].[id_calidad]= @p_id_pro_calidad)
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_calidad]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_doc_documento_proyecto_calidad]
GO
CREATE PROCEDURE [ins_doc_documento_proyecto_calidad]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_id_documento_proyecto int,
		@p_id_calidad int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [doc_documento.proyecto_calidad]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[id_documento.proyecto],
			[id_calidad])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_id_documento_proyecto,
			@p_id_calidad)
		SET @v_id = scope_identity()
UPDATE [doc_documento.proyecto_calidad] 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_calidad]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_doc_documento_proyecto_calidad]
GO
CREATE PROCEDURE [upd_doc_documento_proyecto_calidad]
	@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_id_documento_proyecto int = null,
		@p_id_calidad int = null
AS
BEGIN
		UPDATE [doc_documento.proyecto_calidad]
		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(),
			[id_documento.proyecto] = isnull(@p_id_documento_proyecto,[id_documento.proyecto]),
			[id_calidad] = isnull(@p_id_calidad,[id_calidad])
		WHERE (id = @p_id)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'pro_calidad'
-----------------------------------18/06/2015 15:42:54