---Insercion de columna 'esFalla' en  'equ_motivoEvento'
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
declare @uid varchar(100)
select @uid=uid from seg_usuario where id=1

if not exists (select * from information_schema.columns where table_name='dis_motivoEvento' and column_name='esFalla')
begin
alter table [dis_motivoEvento] add [esFalla] bit NOT NULL DEFAULT 0
insert into _columnas (tabla,columna,mostrarEnGrilla,ordenGrilla,requerido,tipo,defecto,tamano) select table_name,column_name,1,1,(case is_nullable when 'YES' then 0 else 1 end) as requerido,data_type as tipo, column_default as defecto, character_maximum_length as tamano from INFORMATION_SCHEMA.COLUMNS where table_name='dis_motivoEvento' and column_name='esFalla';
end
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO

---30/03/2011 01:06:41 p.m.
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_dis_evento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_dis_evento]
GO
CREATE PROCEDURE [reg_dis_evento]
	@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],
		[evento] AS [evento],
		[evento_codigo] AS [evento_codigo],
		[fechaHoraInicio_ff] AS [fechaHoraInicio_ff],
		[duracion_hh] AS [duracion_hh],
		[afectaProduccion] AS [afectaProduccion],
		[notas] AS [notas],
		[id_equipo] AS [id_equipo],
		[id_motivoEvento] AS [id_motivoEvento],
		[id_usuario] AS [id_usuario]
	FROM [dis_evento]
	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_dis_evento]')) DROP VIEW [dbo].[view_dis_evento]
GO
CREATE  VIEW [view_dis_evento] AS
SELECT 	[dis_evento].[id] AS [id],
		[dis_evento].[uid] AS [uid],
		[dis_evento].[eid] AS [eid],
		[dis_evento].[id_usuario_modifico] AS [id_usuario_modifico],
		[dis_evento].[id_usuario_creo] AS [id_usuario_creo],
		[dis_evento].[fechaModificacion] AS [fechaModificacion],
		[dis_evento].[fechaCreacion] AS [fechaCreacion],
		[dis_evento].[active] AS [active],
		[dis_evento].[evento] AS [evento],
		[dis_evento].[evento_codigo] AS [evento_codigo],
		[dis_evento].[fechaHoraInicio_ff] AS [fechaHoraInicio_ff],
		[dis_evento].[duracion_hh] AS [duracion_hh],
		[dis_evento].[afectaProduccion] AS [afectaProduccion],
		[dis_evento].[notas] AS [notas],
		[dis_evento].[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],
		[dis_evento].[id_motivoEvento] AS [id_motivoEvento],
[dis_motivoEvento].[motivoEvento] as [dis_motivoEvento_motivoEvento],
[dis_motivoEvento].[motivoEvento_codigo] as [dis_motivoEvento_motivoEvento_codigo],
[dis_motivoEvento].[responsableServicio] as [dis_motivoEvento_responsableServicio],
[dis_motivoEvento].[VaradaProduccion] as [dis_motivoEvento_VaradaProduccion],
[dis_motivoEvento].[esFalla] as [dis_motivoEvento_esFalla],
		[dis_evento].[id_usuario] AS [id_usuario],
[seg_usuario].[usuario] as [seg_usuario_usuario],
[seg_usuario].[usuario_codigo] as [seg_usuario_usuario_codigo],
[seg_usuario].[intentosFallidos] as [seg_usuario_intentosFallidos],
[seg_usuario].[numeroIngresos] as [seg_usuario_numeroIngresos],
[seg_usuario].[fechaUltimoIngreso_fh] as [seg_usuario_fechaUltimoIngreso_fh],
[seg_usuario].[fechaIntentoFallido_fh] as [seg_usuario_fechaIntentoFallido_fh],
[seg_usuario].[clave] as [seg_usuario_clave],
[seg_usuario].[nombre] as [seg_usuario_nombre],
[seg_usuario].[profesion] as [seg_usuario_profesion],
[seg_usuario].[email] as [seg_usuario_email],
[seg_usuario].[direccion] as [seg_usuario_direccion],
[seg_usuario].[telefono] as [seg_usuario_telefono],
[seg_usuario].[costoHora] as [seg_usuario_costoHora],
[seg_usuario].[porcentajeComision] as [seg_usuario_porcentajeComision],
[seg_usuario].[accesoActivo] as [seg_usuario_accesoActivo],
[seg_usuario].[cambiarClave] as [seg_usuario_cambiarClave],
[seg_usuario].[fechaUltimoCambioClave_fh] as [seg_usuario_fechaUltimoCambioClave_fh],
[seg_usuario].[aplicaRecargo] as [seg_usuario_aplicaRecargo],
[seg_usuario].[sexo] as [seg_usuario_sexo],
[seg_usuario].[id_perfil] as [seg_usuario_id_perfil],
[seg_usuario].[id_cargo] as [seg_usuario_id_cargo],
[seg_usuario].[id_grupo] as [seg_usuario_id_grupo],
[seg_usuario].[id_zona] as [seg_usuario_id_zona],
[seg_usuario].[identificacion] as [seg_usuario_identificacion],
[seg_usuario].[id_usuario] as [seg_usuario_id_usuario],
[seg_usuario].[preferencias] as [seg_usuario_preferencias],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [dis_evento]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[dis_evento].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[dis_evento].[id_usuario_creo]
	INNER JOIN [equ_equipo] ON [equ_equipo].id=[dis_evento].[id_equipo]
	INNER JOIN [dis_motivoEvento] ON [dis_motivoEvento].id=[dis_evento].[id_motivoEvento]
	INNER JOIN [seg_usuario] ON [seg_usuario].id=[dis_evento].[id_usuario]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [dis_evento].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [dis_evento].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_dis_eventosXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_dis_eventosXusuario_modifico]
GO
CREATE PROCEDURE [sel_dis_eventosXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_dis_evento]
	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_dis_eventosXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_dis_eventosXusuario_creo]
GO
CREATE PROCEDURE [sel_dis_eventosXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_dis_evento]
	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_dis_eventosXequipo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_dis_eventosXequipo]
GO
CREATE PROCEDURE [sel_dis_eventosXequipo]
	@p_id_equipo INT
AS
BEGIN
	SELECT * 
	FROM [view_dis_evento]
	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_dis_eventosXmotivoEvento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_dis_eventosXmotivoEvento]
GO
CREATE PROCEDURE [sel_dis_eventosXmotivoEvento]
	@p_id_motivoEvento INT
AS
BEGIN
	SELECT * 
	FROM [view_dis_evento]
	WHERE ([id_motivoEvento]= @p_id_motivoEvento)
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_dis_eventosXusuario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_dis_eventosXusuario]
GO
CREATE PROCEDURE [sel_dis_eventosXusuario]
	@p_id_usuario INT
AS
BEGIN
	SELECT * 
	FROM [view_dis_evento]
	WHERE ([id_usuario]= @p_id_usuario)
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_dis_eventos]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_dis_eventos]
GO
CREATE PROCEDURE [sel_dis_eventos]
	
	@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_dis_evento]
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_dis_eventos]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_dis_eventos]
GO
CREATE PROCEDURE [lis_dis_eventos]
	AS
BEGIN
	SELECT id AS id,
[evento] AS [dis_evento]
	FROM [dis_evento]
	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_dis_evento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_dis_evento]
GO
CREATE PROCEDURE [upd_dis_evento]
	@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_evento varchar(300) = null,
		@p_evento_codigo varchar(100) = null,
		@p_fechaHoraInicio_ff smalldatetime = null,
		@p_duracion_hh smalldatetime = null,
		@p_afectaProduccion bit = null,
		@p_notas varchar(1000) = null,
		@p_id_equipo int = null,
		@p_id_motivoEvento int = null,
		@p_id_usuario int = null
AS
BEGIN
		UPDATE [dis_evento]
		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(),
			[evento] = isnull(@p_evento,[evento]),
			[evento_codigo] = isnull(@p_evento_codigo,[evento_codigo]),
			[fechaHoraInicio_ff] = isnull(@p_fechaHoraInicio_ff,[fechaHoraInicio_ff]),
			[duracion_hh] = isnull(@p_duracion_hh,[duracion_hh]),
			[afectaProduccion] = isnull(@p_afectaProduccion,[afectaProduccion]),
			[notas] = isnull(@p_notas,[notas]),
			[id_equipo] = isnull(@p_id_equipo,[id_equipo]),
			[id_motivoEvento] = isnull(@p_id_motivoEvento,[id_motivoEvento]),
			[id_usuario] = isnull(@p_id_usuario,[id_usuario])
		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_dis_evento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_dis_evento]
GO
CREATE PROCEDURE [ins_dis_evento]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_evento varchar(300),
		@p_evento_codigo varchar(100),
		@p_fechaHoraInicio_ff smalldatetime,
		@p_duracion_hh smalldatetime,
		@p_afectaProduccion bit,
		@p_notas varchar(1000),
		@p_id_equipo int,
		@p_id_motivoEvento int,
		@p_id_usuario int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [dis_evento]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[evento],
			[evento_codigo],
			[fechaHoraInicio_ff],
			[duracion_hh],
			[afectaProduccion],
			[notas],
			[id_equipo],
			[id_motivoEvento],
			[id_usuario])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_evento,
			@p_evento_codigo,
			@p_fechaHoraInicio_ff,
			@p_duracion_hh,
			@p_afectaProduccion,
			@p_notas,
			@p_id_equipo,
			@p_id_motivoEvento,
			@p_id_usuario)
		SET @v_id = scope_identity()
UPDATE [dis_evento] 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_dis_evento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_dis_evento]
GO
CREATE PROCEDURE [act_dis_evento]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [dis_evento]
	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_dis_evento_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_dis_evento_m]
GO
CREATE PROCEDURE [reg_dis_evento_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],
		[evento] AS [evento],
		[evento_codigo] AS [evento_codigo],
		[fechaHoraInicio_ff] AS [fechaHoraInicio_ff],
		[duracion_hh] AS [duracion_hh],
		[afectaProduccion] AS [afectaProduccion],
		[notas] AS [notas],
		[id_equipo] AS [id_equipo],
		[id_motivoEvento] AS [id_motivoEvento],
		[id_usuario] AS [id_usuario]
	FROM [dis_evento]
	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_dis_eventosXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_dis_eventosXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_dis_eventosXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_dis_evento]
	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_dis_eventosXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_dis_eventosXusuario_creo_m]
GO
CREATE PROCEDURE [sel_dis_eventosXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_dis_evento]
	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_dis_eventosXequipo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_dis_eventosXequipo_m]
GO
CREATE PROCEDURE [sel_dis_eventosXequipo_m]
	@p_id_equipo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_dis_evento]
	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_dis_eventosXmotivoEvento_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_dis_eventosXmotivoEvento_m]
GO
CREATE PROCEDURE [sel_dis_eventosXmotivoEvento_m]
	@p_id_motivoEvento INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_dis_evento]
	WHERE ([id_motivoEvento]= @p_id_motivoEvento)
	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_dis_eventosXusuario_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_dis_eventosXusuario_m]
GO
CREATE PROCEDURE [sel_dis_eventosXusuario_m]
	@p_id_usuario INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_dis_evento]
	WHERE ([id_usuario]= @p_id_usuario)
	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_dis_eventos_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_dis_eventos_m]
GO
CREATE PROCEDURE [sel_dis_eventos_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_dis_evento]
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_dis_eventos_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_dis_eventos_m]
GO
CREATE PROCEDURE [lis_dis_eventos_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[evento] AS [dis_evento]
	FROM [dis_evento]
	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_dis_evento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_dis_evento]
GO
CREATE PROCEDURE [aud_dis_evento]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [dis_evento].id as id_tabla,
	[dis_evento].id_usuario_modifico as id_usuario_modifico,
	[dis_evento].id_usuario_creo as id_usuario_creo,
	[dis_evento].fechaModificacion as fechaModificacion,
	[dis_evento].fechaCreacion as fechaCreacion,
	[dis_evento].active as active,
	[dis_evento].[evento] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [dis_evento]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[dis_evento].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[dis_evento].[id_usuario_creo]
	where ' + @p_filtro)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'dis_evento'
--------------------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_dis_motivoEvento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_dis_motivoEvento]
GO
CREATE PROCEDURE [reg_dis_motivoEvento]
	@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],
		[motivoEvento] AS [motivoEvento],
		[motivoEvento_codigo] AS [motivoEvento_codigo],
		[responsableServicio] AS [responsableServicio],
		[VaradaProduccion] AS [VaradaProduccion],
		[esFalla] AS [esFalla]
	FROM [dis_motivoEvento]
	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_dis_motivoEvento]')) DROP VIEW [dbo].[view_dis_motivoEvento]
GO
CREATE  VIEW [view_dis_motivoEvento] AS
SELECT 	[dis_motivoEvento].[id] AS [id],
		[dis_motivoEvento].[uid] AS [uid],
		[dis_motivoEvento].[eid] AS [eid],
		[dis_motivoEvento].[id_usuario_modifico] AS [id_usuario_modifico],
		[dis_motivoEvento].[id_usuario_creo] AS [id_usuario_creo],
		[dis_motivoEvento].[fechaModificacion] AS [fechaModificacion],
		[dis_motivoEvento].[fechaCreacion] AS [fechaCreacion],
		[dis_motivoEvento].[active] AS [active],
		[dis_motivoEvento].[motivoEvento] AS [motivoEvento],
		[dis_motivoEvento].[motivoEvento_codigo] AS [motivoEvento_codigo],
		[dis_motivoEvento].[responsableServicio] AS [responsableServicio],
		[dis_motivoEvento].[VaradaProduccion] AS [VaradaProduccion],
		[dis_motivoEvento].[esFalla] AS [esFalla],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [dis_motivoEvento]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[dis_motivoEvento].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[dis_motivoEvento].[id_usuario_creo]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [dis_motivoEvento].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [dis_motivoEvento].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_dis_motivoEventosXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_dis_motivoEventosXusuario_modifico]
GO
CREATE PROCEDURE [sel_dis_motivoEventosXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_dis_motivoEvento]
	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_dis_motivoEventosXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_dis_motivoEventosXusuario_creo]
GO
CREATE PROCEDURE [sel_dis_motivoEventosXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_dis_motivoEvento]
	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_dis_motivoEventos]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_dis_motivoEventos]
GO
CREATE PROCEDURE [sel_dis_motivoEventos]
	
	@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_dis_motivoEvento]
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_dis_motivoEventos]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_dis_motivoEventos]
GO
CREATE PROCEDURE [lis_dis_motivoEventos]
	AS
BEGIN
	SELECT id AS id,
[motivoEvento] AS [dis_motivoEvento]
	FROM [dis_motivoEvento]
	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_dis_motivoEvento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_dis_motivoEvento]
GO
CREATE PROCEDURE [upd_dis_motivoEvento]
	@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_motivoEvento varchar(300) = null,
		@p_motivoEvento_codigo varchar(100) = null,
		@p_responsableServicio bit = null,
		@p_VaradaProduccion bit = null,
		@p_esFalla bit = null
AS
BEGIN
		UPDATE [dis_motivoEvento]
		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(),
			[motivoEvento] = isnull(@p_motivoEvento,[motivoEvento]),
			[motivoEvento_codigo] = isnull(@p_motivoEvento_codigo,[motivoEvento_codigo]),
			[responsableServicio] = isnull(@p_responsableServicio,[responsableServicio]),
			[VaradaProduccion] = isnull(@p_VaradaProduccion,[VaradaProduccion]),
			[esFalla] = isnull(@p_esFalla,[esFalla])
		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_dis_motivoEvento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_dis_motivoEvento]
GO
CREATE PROCEDURE [ins_dis_motivoEvento]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_motivoEvento varchar(300),
		@p_motivoEvento_codigo varchar(100),
		@p_responsableServicio bit,
		@p_VaradaProduccion bit,
		@p_esFalla bit
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [dis_motivoEvento]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[motivoEvento],
			[motivoEvento_codigo],
			[responsableServicio],
			[VaradaProduccion],
			[esFalla])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_motivoEvento,
			@p_motivoEvento_codigo,
			@p_responsableServicio,
			@p_VaradaProduccion,
			@p_esFalla)
		SET @v_id = scope_identity()
UPDATE [dis_motivoEvento] 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_dis_motivoEvento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_dis_motivoEvento]
GO
CREATE PROCEDURE [act_dis_motivoEvento]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [dis_motivoEvento]
	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_dis_motivoEvento_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_dis_motivoEvento_m]
GO
CREATE PROCEDURE [reg_dis_motivoEvento_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],
		[motivoEvento] AS [motivoEvento],
		[motivoEvento_codigo] AS [motivoEvento_codigo],
		[responsableServicio] AS [responsableServicio],
		[VaradaProduccion] AS [VaradaProduccion],
		[esFalla] AS [esFalla]
	FROM [dis_motivoEvento]
	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_dis_motivoEventosXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_dis_motivoEventosXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_dis_motivoEventosXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_dis_motivoEvento]
	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_dis_motivoEventosXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_dis_motivoEventosXusuario_creo_m]
GO
CREATE PROCEDURE [sel_dis_motivoEventosXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_dis_motivoEvento]
	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_dis_motivoEventos_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_dis_motivoEventos_m]
GO
CREATE PROCEDURE [sel_dis_motivoEventos_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_dis_motivoEvento]
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_dis_motivoEventos_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_dis_motivoEventos_m]
GO
CREATE PROCEDURE [lis_dis_motivoEventos_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[motivoEvento] AS [dis_motivoEvento]
	FROM [dis_motivoEvento]
	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_dis_motivoEvento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_dis_motivoEvento]
GO
CREATE PROCEDURE [aud_dis_motivoEvento]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [dis_motivoEvento].id as id_tabla,
	[dis_motivoEvento].id_usuario_modifico as id_usuario_modifico,
	[dis_motivoEvento].id_usuario_creo as id_usuario_creo,
	[dis_motivoEvento].fechaModificacion as fechaModificacion,
	[dis_motivoEvento].fechaCreacion as fechaCreacion,
	[dis_motivoEvento].active as active,
	[dis_motivoEvento].[motivoEvento] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [dis_motivoEvento]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[dis_motivoEvento].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[dis_motivoEvento].[id_usuario_creo]
	where ' + @p_filtro)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'dis_motivoEvento'
--------------------------------