
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_equ_cicloHorometro]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_equ_cicloHorometro]
GO
CREATE PROCEDURE [reg_equ_cicloHorometro]
	@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],
		[cicloHorometro] AS [cicloHorometro],
		[cicloHorometro_codigo] AS [cicloHorometro_codigo],
		[id_equipo] AS [id_equipo],
		[horometroInicial] AS [horometroInicial],
		[horometroFinal] AS [horometroFinal],
		[afectarProyeccion] AS [afectarProyeccion],
		[Nciclo] AS [Nciclo],
		[reparacionMayor] AS [reparacionMayor],
		[reiniciarHorometro] AS [reiniciarHorometro]
	FROM [equ_cicloHorometro]
	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_equ_cicloHorometro]')) DROP VIEW [dbo].[view_equ_cicloHorometro]
GO
CREATE  VIEW [view_equ_cicloHorometro] AS
SELECT 	[equ_cicloHorometro].[id] AS [id],
		[equ_cicloHorometro].[uid] AS [uid],
		[equ_cicloHorometro].[eid] AS [eid],
		[equ_cicloHorometro].[id_usuario_modifico] AS [id_usuario_modifico],
		[equ_cicloHorometro].[id_usuario_creo] AS [id_usuario_creo],
		[equ_cicloHorometro].[fechaModificacion] AS [fechaModificacion],
		[equ_cicloHorometro].[fechaCreacion] AS [fechaCreacion],
		[equ_cicloHorometro].[active] AS [active],
		[equ_cicloHorometro].[cicloHorometro] AS [cicloHorometro],
		[equ_cicloHorometro].[cicloHorometro_codigo] AS [cicloHorometro_codigo],
		[equ_cicloHorometro].[id_equipo] AS [id_equipo],
[equ_equipo].[equipo] as [equ_equipo_equipo],
[equ_equipo].[equipo_codigo] as [equ_equipo_equipo_codigo],
[equ_equipo].[equipo_serial] as [equ_equipo_equipo_serial],
[equ_equipo].[nombreContacto] as [equ_equipo_nombreContacto],
[equ_equipo].[telefonoContacto] as [equ_equipo_telefonoContacto],
[equ_equipo].[ubicacion] as [equ_equipo_ubicacion],
[equ_equipo].[observaciones] as [equ_equipo_observaciones],
[equ_equipo].[fechaPuestaMarcha_fh] as [equ_equipo_fechaPuestaMarcha_fh],
[equ_equipo].[vencimientoGarantiaFabrica_ff] as [equ_equipo_vencimientoGarantiaFabrica_ff],
[equ_equipo].[vencimientoGarantiaDistribuidor_ff] as [equ_equipo_vencimientoGarantiaDistribuidor_ff],
[equ_equipo].[horasGarantia] as [equ_equipo_horasGarantia],
[equ_equipo].[ultimaLectura_fh] as [equ_equipo_ultimaLectura_fh],
[equ_equipo].[horometroActual] as [equ_equipo_horometroActual],
[equ_equipo].[promedioHoras] as [equ_equipo_promedioHoras],
[equ_equipo].[costo] as [equ_equipo_costo],
[equ_equipo].[fechaImportacion_ff] as [equ_equipo_fechaImportacion_ff],
[equ_equipo].[garantiaCancelada] as [equ_equipo_garantiaCancelada],
[equ_equipo].[id_catalogo.equipo] as [equ_equipo_id_catalogo.equipo],
[equ_equipo].[id_sistema] as [equ_equipo_id_sistema],
[equ_equipo].[id_sucursal] as [equ_equipo_id_sucursal],
[equ_equipo].[id_zona] as [equ_equipo_id_zona],
[equ_equipo].[id_versionEquipo] as [equ_equipo_id_versionEquipo],
[equ_equipo].[id_tercero_comprador] as [equ_equipo_id_tercero_comprador],
[equ_equipo].[id_tercero] as [equ_equipo_id_tercero],
[equ_equipo].[id_tercero_servicio] as [equ_equipo_id_tercero_servicio],
[equ_equipo].[id_equipo] as [equ_equipo_id_equipo],
[equ_equipo].[id_estadoEquipo] as [equ_equipo_id_estadoEquipo],
		[equ_cicloHorometro].[horometroInicial] AS [horometroInicial],
		[equ_cicloHorometro].[horometroFinal] AS [horometroFinal],
		[equ_cicloHorometro].[afectarProyeccion] AS [afectarProyeccion],
		[equ_cicloHorometro].[Nciclo] AS [Nciclo],
		[equ_cicloHorometro].[reparacionMayor] AS [reparacionMayor],
		[equ_cicloHorometro].[reiniciarHorometro] AS [reiniciarHorometro],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [equ_cicloHorometro]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[equ_cicloHorometro].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[equ_cicloHorometro].[id_usuario_creo]
	INNER JOIN [equ_equipo] ON [equ_equipo].id=[equ_cicloHorometro].[id_equipo]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [equ_cicloHorometro].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [equ_cicloHorometro].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_equ_cicloHorometrosXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_cicloHorometrosXusuario_modifico]
GO
CREATE PROCEDURE [sel_equ_cicloHorometrosXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_cicloHorometro]
	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_equ_cicloHorometrosXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_cicloHorometrosXusuario_creo]
GO
CREATE PROCEDURE [sel_equ_cicloHorometrosXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_cicloHorometro]
	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_equ_cicloHorometrosXequipo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_cicloHorometrosXequipo]
GO
CREATE PROCEDURE [sel_equ_cicloHorometrosXequipo]
	@p_id_equipo INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_cicloHorometro]
	WHERE ([id_equipo]= @p_id_equipo)
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_equ_cicloHorometros]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_cicloHorometros]
GO
CREATE PROCEDURE [sel_equ_cicloHorometros]
	
	@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_equ_cicloHorometro]
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_equ_cicloHorometros]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_equ_cicloHorometros]
GO
CREATE PROCEDURE [lis_equ_cicloHorometros]
	AS
BEGIN
	SELECT id AS id,
[cicloHorometro] AS [equ_cicloHorometro]
	FROM [equ_cicloHorometro]
	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_equ_cicloHorometro]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_equ_cicloHorometro]
GO
CREATE PROCEDURE [upd_equ_cicloHorometro]
	@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_cicloHorometro varchar(300) = null,
		@p_cicloHorometro_codigo varchar(100) = null,
		@p_id_equipo int = null,
		@p_horometroInicial float = null,
		@p_horometroFinal float = null,
		@p_afectarProyeccion bit = null,
		@p_Nciclo int = null,
		@p_reparacionMayor bit = null,
		@p_reiniciarHorometro bit = null
AS
BEGIN
		UPDATE [equ_cicloHorometro]
		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(),
			[cicloHorometro] = isnull(@p_cicloHorometro,[cicloHorometro]),
			[cicloHorometro_codigo] = isnull(@p_cicloHorometro_codigo,[cicloHorometro_codigo]),
			[id_equipo] = isnull(@p_id_equipo,[id_equipo]),
			[horometroInicial] = isnull(@p_horometroInicial,[horometroInicial]),
			[horometroFinal] = isnull(@p_horometroFinal,[horometroFinal]),
			[afectarProyeccion] = isnull(@p_afectarProyeccion,[afectarProyeccion]),
			[Nciclo] = isnull(@p_Nciclo,[Nciclo]),
			[reparacionMayor] = isnull(@p_reparacionMayor,[reparacionMayor]),
			[reiniciarHorometro] = isnull(@p_reiniciarHorometro,[reiniciarHorometro])
		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_equ_cicloHorometro]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_equ_cicloHorometro]
GO
CREATE PROCEDURE [ins_equ_cicloHorometro]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_cicloHorometro varchar(300),
		@p_cicloHorometro_codigo varchar(100),
		@p_id_equipo int,
		@p_horometroInicial float,
		@p_horometroFinal float,
		@p_afectarProyeccion bit,
		@p_Nciclo int,
		@p_reparacionMayor bit,
		@p_reiniciarHorometro bit
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [equ_cicloHorometro]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[cicloHorometro],
			[cicloHorometro_codigo],
			[id_equipo],
			[horometroInicial],
			[horometroFinal],
			[afectarProyeccion],
			[Nciclo],
			[reparacionMayor],
			[reiniciarHorometro])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_cicloHorometro,
			@p_cicloHorometro_codigo,
			@p_id_equipo,
			@p_horometroInicial,
			@p_horometroFinal,
			@p_afectarProyeccion,
			@p_Nciclo,
			@p_reparacionMayor,
			@p_reiniciarHorometro)
		SET @v_id = scope_identity()
UPDATE [equ_cicloHorometro] 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_equ_cicloHorometro]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_equ_cicloHorometro]
GO
CREATE PROCEDURE [act_equ_cicloHorometro]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [equ_cicloHorometro]
	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_equ_cicloHorometro_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_equ_cicloHorometro_m]
GO
CREATE PROCEDURE [reg_equ_cicloHorometro_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],
		[cicloHorometro] AS [cicloHorometro],
		[cicloHorometro_codigo] AS [cicloHorometro_codigo],
		[id_equipo] AS [id_equipo],
		[horometroInicial] AS [horometroInicial],
		[horometroFinal] AS [horometroFinal],
		[afectarProyeccion] AS [afectarProyeccion],
		[Nciclo] AS [Nciclo],
		[reparacionMayor] AS [reparacionMayor],
		[reiniciarHorometro] AS [reiniciarHorometro]
	FROM [equ_cicloHorometro]
	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_equ_cicloHorometrosXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_cicloHorometrosXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_equ_cicloHorometrosXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_cicloHorometro]
	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_equ_cicloHorometrosXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_cicloHorometrosXusuario_creo_m]
GO
CREATE PROCEDURE [sel_equ_cicloHorometrosXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_cicloHorometro]
	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_equ_cicloHorometrosXequipo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_cicloHorometrosXequipo_m]
GO
CREATE PROCEDURE [sel_equ_cicloHorometrosXequipo_m]
	@p_id_equipo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_cicloHorometro]
	WHERE ([id_equipo]= @p_id_equipo)
	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_equ_cicloHorometros_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_cicloHorometros_m]
GO
CREATE PROCEDURE [sel_equ_cicloHorometros_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_equ_cicloHorometro]
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_equ_cicloHorometros_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_equ_cicloHorometros_m]
GO
CREATE PROCEDURE [lis_equ_cicloHorometros_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[cicloHorometro] AS [equ_cicloHorometro]
	FROM [equ_cicloHorometro]
	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_equ_cicloHorometro]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_equ_cicloHorometro]
GO
CREATE PROCEDURE [aud_equ_cicloHorometro]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [equ_cicloHorometro].id as id_tabla,
	[equ_cicloHorometro].id_usuario_modifico as id_usuario_modifico,
	[equ_cicloHorometro].id_usuario_creo as id_usuario_creo,
	[equ_cicloHorometro].fechaModificacion as fechaModificacion,
	[equ_cicloHorometro].fechaCreacion as fechaCreacion,
	[equ_cicloHorometro].active as active,
	[equ_cicloHorometro].[cicloHorometro] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [equ_cicloHorometro]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[equ_cicloHorometro].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[equ_cicloHorometro].[id_usuario_creo]
	where ' + @p_filtro)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'equ_cicloHorometro'
-----------------------------------22/05/2012 03:39:45 p.m.