SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_ort_reporteTecnico_ot]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_ort_reporteTecnico_ot]
GO
CREATE PROCEDURE [reg_ort_reporteTecnico_ot]
	@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],
		[id_reporteTecnico] AS [id_reporteTecnico],
		[id_ot] AS [id_ot],
		[observaciones] AS [observaciones]
	FROM [ort_reporteTecnico_ot]
	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_ort_reporteTecnico_ot]')) DROP VIEW [dbo].[view_ort_reporteTecnico_ot]
GO
CREATE  VIEW [view_ort_reporteTecnico_ot] AS
SELECT 	[ort_reporteTecnico_ot].[id] AS [id],
		[ort_reporteTecnico_ot].[uid] AS [uid],
		[ort_reporteTecnico_ot].[eid] AS [eid],
		[ort_reporteTecnico_ot].[id_usuario_modifico] AS [id_usuario_modifico],
		[ort_reporteTecnico_ot].[id_usuario_creo] AS [id_usuario_creo],
		[ort_reporteTecnico_ot].[fechaModificacion] AS [fechaModificacion],
		[ort_reporteTecnico_ot].[fechaCreacion] AS [fechaCreacion],
		[ort_reporteTecnico_ot].[active] AS [active],
		[ort_reporteTecnico_ot].[id_reporteTecnico] AS [id_reporteTecnico],
[ort_reporteTecnico].[reporteTecnico] as [ort_reporteTecnico_reporteTecnico],
[ort_reporteTecnico].[reporteTecnico_codigo] as [ort_reporteTecnico_reporteTecnico_codigo],
[ort_reporteTecnico].[numero] as [ort_reporteTecnico_numero],
[ort_reporteTecnico].[trabajos] as [ort_reporteTecnico_trabajos],
[ort_reporteTecnico].[recomendaciones] as [ort_reporteTecnico_recomendaciones],
[ort_reporteTecnico].[compromisos] as [ort_reporteTecnico_compromisos],
[ort_reporteTecnico].[id_gasto] as [ort_reporteTecnico_id_gasto],
		[ort_reporteTecnico_ot].[id_ot] AS [id_ot],
		[ort_reporteTecnico_ot].[observaciones] AS [observaciones],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [ort_reporteTecnico_ot]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[ort_reporteTecnico_ot].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[ort_reporteTecnico_ot].[id_usuario_creo]
	INNER JOIN [ort_reporteTecnico] ON [ort_reporteTecnico].id=[ort_reporteTecnico_ot].[id_reporteTecnico]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [ort_reporteTecnico_ot].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_ort_reporteTecnico_otesXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ort_reporteTecnico_otesXusuario_modifico]
GO
CREATE PROCEDURE [sel_ort_reporteTecnico_otesXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_ort_reporteTecnico_ot]
	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_ort_reporteTecnico_otesXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ort_reporteTecnico_otesXusuario_creo]
GO
CREATE PROCEDURE [sel_ort_reporteTecnico_otesXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_ort_reporteTecnico_ot]
	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_ort_reporteTecnico_otesXreporteTecnico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ort_reporteTecnico_otesXreporteTecnico]
GO
CREATE PROCEDURE [sel_ort_reporteTecnico_otesXreporteTecnico]
	@p_id_reporteTecnico INT
AS
BEGIN
	SELECT * 
	FROM [view_ort_reporteTecnico_ot]
	WHERE ([id_reporteTecnico]= @p_id_reporteTecnico)
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_ort_reporteTecnico_otesXot]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ort_reporteTecnico_otesXot]
GO
CREATE PROCEDURE [sel_ort_reporteTecnico_otesXot]
	@p_id_ot INT
AS
BEGIN
	SELECT * 
	FROM [view_ort_reporteTecnico_ot]
	WHERE ([id_ot]= @p_id_ot)
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_ort_reporteTecnico_otes]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ort_reporteTecnico_otes]
GO
CREATE PROCEDURE [sel_ort_reporteTecnico_otes]
	
	@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_ort_reporteTecnico_ot]
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_ort_reporteTecnico_otes]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_ort_reporteTecnico_otes]
GO
CREATE PROCEDURE [lis_ort_reporteTecnico_otes]
	AS
BEGIN
	SELECT id AS id
	FROM [ort_reporteTecnico_ot]
	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_ort_reporteTecnico_ot]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_ort_reporteTecnico_ot]
GO
CREATE PROCEDURE [upd_ort_reporteTecnico_ot]
	@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_reporteTecnico int = null,
		@p_id_ot int = null,
		@p_observaciones varchar(2000) = null
AS
BEGIN
		UPDATE [ort_reporteTecnico_ot]
		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_reporteTecnico] = isnull(@p_id_reporteTecnico,[id_reporteTecnico]),
			[id_ot] = isnull(@p_id_ot,[id_ot]),
			[observaciones] = isnull(@p_observaciones,[observaciones])
		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_ort_reporteTecnico_ot]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_ort_reporteTecnico_ot]
GO
CREATE PROCEDURE [ins_ort_reporteTecnico_ot]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_id_reporteTecnico int,
		@p_id_ot int,
		@p_observaciones varchar(2000)
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [ort_reporteTecnico_ot]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[id_reporteTecnico],
			[id_ot],
			[observaciones])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_id_reporteTecnico,
			@p_id_ot,
			@p_observaciones)
		SET @v_id = scope_identity()
UPDATE [ort_reporteTecnico_ot] 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_ort_reporteTecnico_ot]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_ort_reporteTecnico_ot]
GO
CREATE PROCEDURE [act_ort_reporteTecnico_ot]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [ort_reporteTecnico_ot]
	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_ort_reporteTecnico_ot_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_ort_reporteTecnico_ot_m]
GO
CREATE PROCEDURE [reg_ort_reporteTecnico_ot_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],
		[id_reporteTecnico] AS [id_reporteTecnico],
		[id_ot] AS [id_ot],
		[observaciones] AS [observaciones]
	FROM [ort_reporteTecnico_ot]
	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_ort_reporteTecnico_otesXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ort_reporteTecnico_otesXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_ort_reporteTecnico_otesXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ort_reporteTecnico_ot]
	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_ort_reporteTecnico_otesXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ort_reporteTecnico_otesXusuario_creo_m]
GO
CREATE PROCEDURE [sel_ort_reporteTecnico_otesXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ort_reporteTecnico_ot]
	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_ort_reporteTecnico_otesXreporteTecnico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ort_reporteTecnico_otesXreporteTecnico_m]
GO
CREATE PROCEDURE [sel_ort_reporteTecnico_otesXreporteTecnico_m]
	@p_id_reporteTecnico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ort_reporteTecnico_ot]
	WHERE ([id_reporteTecnico]= @p_id_reporteTecnico)
	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_ort_reporteTecnico_otesXot_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ort_reporteTecnico_otesXot_m]
GO
CREATE PROCEDURE [sel_ort_reporteTecnico_otesXot_m]
	@p_id_ot INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ort_reporteTecnico_ot]
	WHERE ([id_ot]= @p_id_ot)
	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_ort_reporteTecnico_otes_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ort_reporteTecnico_otes_m]
GO
CREATE PROCEDURE [sel_ort_reporteTecnico_otes_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_ort_reporteTecnico_ot]
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_ort_reporteTecnico_otes_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_ort_reporteTecnico_otes_m]
GO
CREATE PROCEDURE [lis_ort_reporteTecnico_otes_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id
	FROM [ort_reporteTecnico_ot]
	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_ort_reporteTecnico_ot]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_ort_reporteTecnico_ot]
GO
CREATE PROCEDURE [aud_ort_reporteTecnico_ot]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [ort_reporteTecnico_ot].id as id_tabla,
	[ort_reporteTecnico_ot].id_usuario_modifico as id_usuario_modifico,
	[ort_reporteTecnico_ot].id_usuario_creo as id_usuario_creo,
	[ort_reporteTecnico_ot].fechaModificacion as fechaModificacion,
	[ort_reporteTecnico_ot].fechaCreacion as fechaCreacion,
	[ort_reporteTecnico_ot].active as active,
	[ort_reporteTecnico_ot].[reporteTecnico_ot] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [ort_reporteTecnico_ot]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[ort_reporteTecnico_ot].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[ort_reporteTecnico_ot].[id_usuario_creo]
	where ' + @p_filtro)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'ort_reporteTecnico_ot'
-----------------------------------05/08/2013 10:10:32