
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_ort_reporteTecnico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_ort_reporteTecnico]
GO
CREATE PROCEDURE [reg_ort_reporteTecnico]
	@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],
		[reporteTecnico] AS [reporteTecnico],
		[reporteTecnico_codigo] AS [reporteTecnico_codigo],
		[numero] AS [numero],
		[trabajos] AS [trabajos],
		[recomendaciones] AS [recomendaciones],
		[compromisos] AS [compromisos],
		[id_gasto] AS [id_gasto],
		[id_canalAtencion] AS [id_canalAtencion]
	FROM [ort_reporteTecnico]
	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]')) DROP VIEW [dbo].[view_ort_reporteTecnico]
GO
CREATE  VIEW [view_ort_reporteTecnico] AS
SELECT 	[ort_reporteTecnico].[id] AS [id],
		[ort_reporteTecnico].[uid] AS [uid],
		[ort_reporteTecnico].[eid] AS [eid],
		[ort_reporteTecnico].[id_usuario_modifico] AS [id_usuario_modifico],
		[ort_reporteTecnico].[id_usuario_creo] AS [id_usuario_creo],
		[ort_reporteTecnico].[fechaModificacion] AS [fechaModificacion],
		[ort_reporteTecnico].[fechaCreacion] AS [fechaCreacion],
		[ort_reporteTecnico].[active] AS [active],
		[ort_reporteTecnico].[reporteTecnico] AS [reporteTecnico],
		[ort_reporteTecnico].[reporteTecnico_codigo] AS [reporteTecnico_codigo],
		[ort_reporteTecnico].[numero] AS [numero],
		[ort_reporteTecnico].[trabajos] AS [trabajos],
		[ort_reporteTecnico].[recomendaciones] AS [recomendaciones],
		[ort_reporteTecnico].[compromisos] AS [compromisos],
		[ort_reporteTecnico].[id_gasto] AS [id_gasto],
[gas_gasto].[gasto] as [gas_gasto_gasto],
[gas_gasto].[gasto_codigo] as [gas_gasto_gasto_codigo],
[gas_gasto].[numero] as [gas_gasto_numero],
[gas_gasto].[fecha_ff] as [gas_gasto_fecha_ff],
[gas_gasto].[dias] as [gas_gasto_dias],
[gas_gasto].[valor] as [gas_gasto_valor],
[gas_gasto].[concepto] as [gas_gasto_concepto],
[gas_gasto].[consignado] as [gas_gasto_consignado],
[gas_gasto].[id_tipoGasto] as [gas_gasto_id_tipoGasto],
[gas_gasto].[id_usuario_aprobo] as [gas_gasto_id_usuario_aprobo],
[gas_gasto].[id_usuario_solicitante] as [gas_gasto_id_usuario_solicitante],
[gas_gasto].[id_estadoTipoDocumento] as [gas_gasto_id_estadoTipoDocumento],
		[ort_reporteTecnico].[id_canalAtencion] AS [id_canalAtencion],
[ort_canalAtencion].[canalAtencion] as [ort_canalAtencion_canalAtencion],
[ort_canalAtencion].[canalAtencion_codigo] as [ort_canalAtencion_canalAtencion_codigo],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [ort_reporteTecnico]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[ort_reporteTecnico].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[ort_reporteTecnico].[id_usuario_creo]
	INNER JOIN [gas_gasto] ON [gas_gasto].id=[ort_reporteTecnico].[id_gasto]
	INNER JOIN [ort_canalAtencion] ON [ort_canalAtencion].id=[ort_reporteTecnico].[id_canalAtencion]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [ort_reporteTecnico].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [ort_reporteTecnico].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_reporteTecnicosXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ort_reporteTecnicosXusuario_modifico]
GO
CREATE PROCEDURE [sel_ort_reporteTecnicosXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_ort_reporteTecnico]
	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_reporteTecnicosXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ort_reporteTecnicosXusuario_creo]
GO
CREATE PROCEDURE [sel_ort_reporteTecnicosXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_ort_reporteTecnico]
	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_reporteTecnicosXgasto]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ort_reporteTecnicosXgasto]
GO
CREATE PROCEDURE [sel_ort_reporteTecnicosXgasto]
	@p_id_gasto INT
AS
BEGIN
	SELECT * 
	FROM [view_ort_reporteTecnico]
	WHERE ([id_gasto]= @p_id_gasto)
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_reporteTecnicosXcanalAtencion]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ort_reporteTecnicosXcanalAtencion]
GO
CREATE PROCEDURE [sel_ort_reporteTecnicosXcanalAtencion]
	@p_id_canalAtencion INT
AS
BEGIN
	SELECT * 
	FROM [view_ort_reporteTecnico]
	WHERE ([id_canalAtencion]= @p_id_canalAtencion)
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_reporteTecnicos]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ort_reporteTecnicos]
GO
CREATE PROCEDURE [sel_ort_reporteTecnicos]
	
	@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]
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_reporteTecnicos]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_ort_reporteTecnicos]
GO
CREATE PROCEDURE [lis_ort_reporteTecnicos]
	AS
BEGIN
	SELECT id AS id,
[reporteTecnico] AS [ort_reporteTecnico]
	FROM [ort_reporteTecnico]
	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]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_ort_reporteTecnico]
GO
CREATE PROCEDURE [upd_ort_reporteTecnico]
	@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_reporteTecnico varchar(300) = null,
		@p_reporteTecnico_codigo varchar(100) = null,
		@p_numero varchar(100) = null,
		@p_trabajos varchar(4000) = null,
		@p_recomendaciones varchar(4000) = null,
		@p_compromisos varchar(4000) = null,
		@p_id_gasto int = null,
		@p_id_canalAtencion int = null
AS
BEGIN
		UPDATE [ort_reporteTecnico]
		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(),
			[reporteTecnico] = isnull(@p_reporteTecnico,[reporteTecnico]),
			[reporteTecnico_codigo] = isnull(@p_reporteTecnico_codigo,[reporteTecnico_codigo]),
			[numero] = isnull(@p_numero,[numero]),
			[trabajos] = isnull(@p_trabajos,[trabajos]),
			[recomendaciones] = isnull(@p_recomendaciones,[recomendaciones]),
			[compromisos] = isnull(@p_compromisos,[compromisos]),
			[id_gasto] = isnull(@p_id_gasto,[id_gasto]),
			[id_canalAtencion] = isnull(@p_id_canalAtencion,[id_canalAtencion])
		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]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_ort_reporteTecnico]
GO
CREATE PROCEDURE [ins_ort_reporteTecnico]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_reporteTecnico varchar(300),
		@p_reporteTecnico_codigo varchar(100),
		@p_numero varchar(100),
		@p_trabajos varchar(4000),
		@p_recomendaciones varchar(4000),
		@p_compromisos varchar(4000),
		@p_id_gasto int,
		@p_id_canalAtencion int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [ort_reporteTecnico]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[reporteTecnico],
			[reporteTecnico_codigo],
			[numero],
			[trabajos],
			[recomendaciones],
			[compromisos],
			[id_gasto],
			[id_canalAtencion])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_reporteTecnico,
			@p_reporteTecnico_codigo,
			@p_numero,
			@p_trabajos,
			@p_recomendaciones,
			@p_compromisos,
			@p_id_gasto,
			@p_id_canalAtencion)
		SET @v_id = scope_identity()
UPDATE [ort_reporteTecnico] 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]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_ort_reporteTecnico]
GO
CREATE PROCEDURE [act_ort_reporteTecnico]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [ort_reporteTecnico]
	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_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_ort_reporteTecnico_m]
GO
CREATE PROCEDURE [reg_ort_reporteTecnico_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],
		[reporteTecnico] AS [reporteTecnico],
		[reporteTecnico_codigo] AS [reporteTecnico_codigo],
		[numero] AS [numero],
		[trabajos] AS [trabajos],
		[recomendaciones] AS [recomendaciones],
		[compromisos] AS [compromisos],
		[id_gasto] AS [id_gasto],
		[id_canalAtencion] AS [id_canalAtencion]
	FROM [ort_reporteTecnico]
	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_reporteTecnicosXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ort_reporteTecnicosXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_ort_reporteTecnicosXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ort_reporteTecnico]
	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_reporteTecnicosXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ort_reporteTecnicosXusuario_creo_m]
GO
CREATE PROCEDURE [sel_ort_reporteTecnicosXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ort_reporteTecnico]
	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_reporteTecnicosXgasto_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ort_reporteTecnicosXgasto_m]
GO
CREATE PROCEDURE [sel_ort_reporteTecnicosXgasto_m]
	@p_id_gasto INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ort_reporteTecnico]
	WHERE ([id_gasto]= @p_id_gasto)
	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_reporteTecnicosXcanalAtencion_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ort_reporteTecnicosXcanalAtencion_m]
GO
CREATE PROCEDURE [sel_ort_reporteTecnicosXcanalAtencion_m]
	@p_id_canalAtencion INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ort_reporteTecnico]
	WHERE ([id_canalAtencion]= @p_id_canalAtencion)
	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_reporteTecnicos_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ort_reporteTecnicos_m]
GO
CREATE PROCEDURE [sel_ort_reporteTecnicos_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]
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_reporteTecnicos_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_ort_reporteTecnicos_m]
GO
CREATE PROCEDURE [lis_ort_reporteTecnicos_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[reporteTecnico] AS [ort_reporteTecnico]
	FROM [ort_reporteTecnico]
	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]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_ort_reporteTecnico]
GO
CREATE PROCEDURE [aud_ort_reporteTecnico]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [ort_reporteTecnico].id as id_tabla,
	[ort_reporteTecnico].id_usuario_modifico as id_usuario_modifico,
	[ort_reporteTecnico].id_usuario_creo as id_usuario_creo,
	[ort_reporteTecnico].fechaModificacion as fechaModificacion,
	[ort_reporteTecnico].fechaCreacion as fechaCreacion,
	[ort_reporteTecnico].active as active,
	[ort_reporteTecnico].[reporteTecnico] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [ort_reporteTecnico]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[ort_reporteTecnico].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[ort_reporteTecnico].[id_usuario_creo]
	where ' + @p_filtro)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'ort_reporteTecnico'
-----------------------------------28/09/2018 15:46:51