
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_cnt_visitaFija]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_cnt_visitaFija]
GO
CREATE PROCEDURE [reg_cnt_visitaFija]
	@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],
		[visitaFija] AS [visitaFija],
		[visitaFija_codigo] AS [visitaFija_codigo],
		[fecha_ff] AS [fecha_ff],
		[fechaCreada_ff] AS [fechaCreada_ff],
		[horometro] AS [horometro],
		[id_documento.ot] AS [id_documento.ot],
		[id_contratoEquipo] AS [id_contratoEquipo],
		[id_contratoDetalleVisita] AS [id_contratoDetalleVisita],
		[id_catalogo.tempario] AS [id_catalogo.tempario],
		[esPorTrabajo] AS [esPorTrabajo],
		[correoRecordatorio] AS [correoRecordatorio]
	FROM [cnt_visitaFija]
	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_cnt_visitaFija]')) DROP VIEW [dbo].[view_cnt_visitaFija]
GO
CREATE  VIEW [view_cnt_visitaFija] AS
SELECT 	[cnt_visitaFija].[id] AS [id],
		[cnt_visitaFija].[uid] AS [uid],
		[cnt_visitaFija].[eid] AS [eid],
		[cnt_visitaFija].[id_usuario_modifico] AS [id_usuario_modifico],
		[cnt_visitaFija].[id_usuario_creo] AS [id_usuario_creo],
		[cnt_visitaFija].[fechaModificacion] AS [fechaModificacion],
		[cnt_visitaFija].[fechaCreacion] AS [fechaCreacion],
		[cnt_visitaFija].[active] AS [active],
		[cnt_visitaFija].[visitaFija] AS [visitaFija],
		[cnt_visitaFija].[visitaFija_codigo] AS [visitaFija_codigo],
		[cnt_visitaFija].[fecha_ff] AS [fecha_ff],
		[cnt_visitaFija].[fechaCreada_ff] AS [fechaCreada_ff],
		[cnt_visitaFija].[horometro] AS [horometro],
		[cnt_visitaFija].[id_documento.ot] AS [id_documento.ot],
[doc_documento.ot].[documento.ot] as [doc_documento.ot_documento.ot],
[doc_documento.ot].[motivoServicio] as [doc_documento.ot_motivoServicio],
[doc_documento.ot].[enTaller] as [doc_documento.ot_enTaller],
[doc_documento.ot].[diagnosticoInicial] as [doc_documento.ot_diagnosticoInicial],
[doc_documento.ot].[contacto] as [doc_documento.ot_contacto],
[doc_documento.ot].[cargo] as [doc_documento.ot_cargo],
[doc_documento.ot].[telefono] as [doc_documento.ot_telefono],
[doc_documento.ot].[fax] as [doc_documento.ot_fax],
[doc_documento.ot].[email] as [doc_documento.ot_email],
[doc_documento.ot].[direccionUbicacion] as [doc_documento.ot_direccionUbicacion],
[doc_documento.ot].[duracionEstimada] as [doc_documento.ot_duracionEstimada],
[doc_documento.ot].[fechaGarantia_ff] as [doc_documento.ot_fechaGarantia_ff],
[doc_documento.ot].[fechaCompromiso_fh] as [doc_documento.ot_fechaCompromiso_fh],
[doc_documento.ot].[trabajos] as [doc_documento.ot_trabajos],
[doc_documento.ot].[recomendaciones] as [doc_documento.ot_recomendaciones],
[doc_documento.ot].[compromisos] as [doc_documento.ot_compromisos],
[doc_documento.ot].[reclamacionFabrica] as [doc_documento.ot_reclamacionFabrica],
[doc_documento.ot].[porcentajeCliente] as [doc_documento.ot_porcentajeCliente],
[doc_documento.ot].[porcentajeDistribuidor] as [doc_documento.ot_porcentajeDistribuidor],
[doc_documento.ot].[porcentajeFabricante] as [doc_documento.ot_porcentajeFabricante],
[doc_documento.ot].[segunTempario] as [doc_documento.ot_segunTempario],
[doc_documento.ot].[distribuyeCentros] as [doc_documento.ot_distribuyeCentros],
[doc_documento.ot].[id_tipoServicio] as [doc_documento.ot_id_tipoServicio],
[doc_documento.ot].[id_equipo] as [doc_documento.ot_id_equipo],
[doc_documento.ot].[id_usuario_coordinador] as [doc_documento.ot_id_usuario_coordinador],
[doc_documento.ot].[id_motivoCancelacion] as [doc_documento.ot_id_motivoCancelacion],
[doc_documento.ot].[id_sucursal] as [doc_documento.ot_id_sucursal],
[doc_documento.ot].[id_sistema] as [doc_documento.ot_id_sistema],
[doc_documento.ot].[id_departamentoSolicitud] as [doc_documento.ot_id_departamentoSolicitud],
[doc_documento.ot].[id_contrato] as [doc_documento.ot_id_contrato],
[doc_documento.ot].[id_zona] as [doc_documento.ot_id_zona],
		[doc_documento_ot].[documento_codigo] AS [doc_documento_ot_documento_codigo],
		[doc_documento_ot].[documento_numero] AS [doc_documento_ot_documento_numero],
		[doc_documento_ot].[prefijo] AS [doc_documento_ot_prefijo],
		[doc_documento_ot].[subtotal] AS [doc_documento_ot_subtotal],
		[doc_documento_ot].[descuento] AS [doc_documento_ot_descuento],
		[doc_documento_ot].[iva] AS [doc_documento_ot_iva],
		[doc_documento_ot].[total] AS [doc_documento_ot_total],
		[doc_documento_ot].[costo] AS [doc_documento_ot_costo],
		[doc_documento_ot].[rentabilidad] AS [doc_documento_ot_rentabilidad],
		[doc_documento_ot].[trm] AS [doc_documento_ot_trm],
		[doc_documento_ot].[fecha_fh] AS [doc_documento_ot_fecha_fh],
		[doc_documento_ot].[fechaSugerida_fh] AS [doc_documento_ot_fechaSugerida_fh],
		[doc_documento_ot].[fechaCierre_ff] AS [doc_documento_ot_fechaCierre_ff],
		[doc_documento_ot].[fechaLimite_fh] AS [doc_documento_ot_fechaLimite_fh],
		[doc_documento_ot].[id_subtipoDocumento] AS [doc_documento_ot_id_subtipoDocumento],
		[doc_documento_ot].[id_tercero_cliente] AS [doc_documento_ot_id_tercero_cliente],
		[doc_documento_ot].[id_documento] AS [doc_documento_ot_id_documento],
		[doc_documento_ot].[id_tercero_proveedor] AS [doc_documento_ot_id_tercero_proveedor],
		[doc_documento_ot].[id_prioridadDocumento] AS [doc_documento_ot_id_prioridadDocumento],
		[doc_documento_ot].[id_moneda] AS [doc_documento_ot_id_moneda],
		[doc_documento_ot].[id_estadoTipoDocumento] AS [doc_documento_ot_id_estadoTipoDocumento],
		[doc_documento_ot].[id_motivoestado] AS [doc_documento_ot_id_motivoestado],
		[doc_documento_ot].[subtotalPlaneado] AS [doc_documento_ot_subtotalPlaneado],
		[doc_documento_ot].[descuentoPlaneado] AS [doc_documento_ot_descuentoPlaneado],
		[doc_documento_ot].[ivaPlaneado] AS [doc_documento_ot_ivaPlaneado],
		[doc_documento_ot].[totalPlaneado] AS [doc_documento_ot_totalPlaneado],
		[doc_documento_ot].[costoPlaneado] AS [doc_documento_ot_costoPlaneado],
		[cnt_visitaFija].[id_contratoEquipo] AS [id_contratoEquipo],
[cnt_contratoEquipo].[contratoEquipo] as [cnt_contratoEquipo_contratoEquipo],
[cnt_contratoEquipo].[contratoEquipo_codigo] as [cnt_contratoEquipo_contratoEquipo_codigo],
[cnt_contratoEquipo].[precioVisita] as [cnt_contratoEquipo_precioVisita],
[cnt_contratoEquipo].[precioServicio] as [cnt_contratoEquipo_precioServicio],
[cnt_contratoEquipo].[fecha_ff] as [cnt_contratoEquipo_fecha_ff],
[cnt_contratoEquipo].[promedio] as [cnt_contratoEquipo_promedio],
[cnt_contratoEquipo].[id_equipo] as [cnt_contratoEquipo_id_equipo],
[cnt_contratoEquipo].[id_contrato] as [cnt_contratoEquipo_id_contrato],
[cnt_contratoEquipo].[id_contratoDetalleVisita] as [cnt_contratoEquipo_id_contratoDetalleVisita],
		[cnt_visitaFija].[id_contratoDetalleVisita] AS [id_contratoDetalleVisita],
[cnt_contratoDetalleVisita].[contratoDetalleVisita] as [cnt_contratoDetalleVisita_contratoDetalleVisita],
[cnt_contratoDetalleVisita].[contratoDetalleVisita_codigo] as [cnt_contratoDetalleVisita_contratoDetalleVisita_codigo],
[cnt_contratoDetalleVisita].[frecuencia] as [cnt_contratoDetalleVisita_frecuencia],
[cnt_contratoDetalleVisita].[cadaN] as [cnt_contratoDetalleVisita_cadaN],
[cnt_contratoDetalleVisita].[dia] as [cnt_contratoDetalleVisita_dia],
[cnt_contratoDetalleVisita].[estrategia] as [cnt_contratoDetalleVisita_estrategia],
[cnt_contratoDetalleVisita].[porTrabajo] as [cnt_contratoDetalleVisita_porTrabajo],
[cnt_contratoDetalleVisita].[incluirMenores] as [cnt_contratoDetalleVisita_incluirMenores],
[cnt_contratoDetalleVisita].[aproximarA] as [cnt_contratoDetalleVisita_aproximarA],
[cnt_contratoDetalleVisita].[fecha_ff] as [cnt_contratoDetalleVisita_fecha_ff],
[cnt_contratoDetalleVisita].[id_contrato] as [cnt_contratoDetalleVisita_id_contrato],
[cnt_contratoDetalleVisita].[diaHabil] as [cnt_contratoDetalleVisita_diaHabil],
		[cnt_visitaFija].[id_catalogo.tempario] AS [id_catalogo.tempario],
[cat_catalogo.tempario].[catalogo.tempario] as [cat_catalogo.tempario_catalogo.tempario],
[cat_catalogo.tempario].[duracionEstimada] as [cat_catalogo.tempario_duracionEstimada],
[cat_catalogo.tempario].[enTaller] as [cat_catalogo.tempario_enTaller],
[cat_catalogo.tempario].[esCiclico] as [cat_catalogo.tempario_esCiclico],
[cat_catalogo.tempario].[detieneEquipos] as [cat_catalogo.tempario_detieneEquipos],
[cat_catalogo.tempario].[horasDetencion] as [cat_catalogo.tempario_horasDetencion],
[cat_catalogo.tempario].[periodoHoras] as [cat_catalogo.tempario_periodoHoras],
[cat_catalogo.tempario].[facturaTodo] as [cat_catalogo.tempario_facturaTodo],
[cat_catalogo.tempario].[periodoDias] as [cat_catalogo.tempario_periodoDias],
[cat_catalogo.tempario].[porcGarantia] as [cat_catalogo.tempario_porcGarantia],
[cat_catalogo.tempario].[porcTropicalizacion] as [cat_catalogo.tempario_porcTropicalizacion],
[cat_catalogo.tempario].[id_tipoServicio] as [cat_catalogo.tempario_id_tipoServicio],
[cat_catalogo.tempario].[proyectar] as [cat_catalogo.tempario_proyectar],
[cat_catalogo.tempario].[incluirMenores] as [cat_catalogo.tempario_incluirMenores],
[cat_catalogo.tempario].[modoAjuste] as [cat_catalogo.tempario_modoAjuste],
		[cat_catalogo_tempario].[catalogo_codigo] AS [cat_catalogo_tempario_catalogo_codigo],
		[cat_catalogo_tempario].[codigoInventario] AS [cat_catalogo_tempario_codigoInventario],
		[cat_catalogo_tempario].[precioVenta] AS [cat_catalogo_tempario_precioVenta],
		[cat_catalogo_tempario].[tiempoConsecucion] AS [cat_catalogo_tempario_tiempoConsecucion],
		[cat_catalogo_tempario].[esNacional] AS [cat_catalogo_tempario_esNacional],
		[cat_catalogo_tempario].[id_familia] AS [cat_catalogo_tempario_id_familia],
		[cat_catalogo_tempario].[id_subTipoCatalogo] AS [cat_catalogo_tempario_id_subTipoCatalogo],
		[cat_catalogo_tempario].[id_unidad] AS [cat_catalogo_tempario_id_unidad],
		[cat_catalogo_tempario].[id_impuesto] AS [cat_catalogo_tempario_id_impuesto],
		[cat_catalogo_tempario].[descripcion] AS [cat_catalogo_tempario_descripcion],
		[cat_catalogo_tempario].[bloquearCostear] AS [cat_catalogo_tempario_bloquearCostear],
		[cnt_visitaFija].[esPorTrabajo] AS [esPorTrabajo],
		[cnt_visitaFija].[correoRecordatorio] AS [correoRecordatorio],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [cnt_visitaFija]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[cnt_visitaFija].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[cnt_visitaFija].[id_usuario_creo]
	INNER JOIN [doc_documento.ot] ON [doc_documento.ot].id=[cnt_visitaFija].[id_documento.ot]
	INNER JOIN [doc_documento] aS [doc_documento_ot] ON [doc_documento.ot].id = [doc_documento_ot].id
	INNER JOIN [cnt_contratoEquipo] ON [cnt_contratoEquipo].id=[cnt_visitaFija].[id_contratoEquipo]
	INNER JOIN [cnt_contratoDetalleVisita] ON [cnt_contratoDetalleVisita].id=[cnt_visitaFija].[id_contratoDetalleVisita]
	INNER JOIN [cat_catalogo.tempario] ON [cat_catalogo.tempario].id=[cnt_visitaFija].[id_catalogo.tempario]
	INNER JOIN [cat_catalogo] aS [cat_catalogo_tempario] ON [cat_catalogo.tempario].id = [cat_catalogo_tempario].id
	INNER JOIN [gen_empresa] AS empresaCodigo ON [cnt_visitaFija].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [cnt_visitaFija].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_cnt_visitaFijasXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_visitaFijasXusuario_modifico]
GO
CREATE PROCEDURE [sel_cnt_visitaFijasXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_cnt_visitaFija]
	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_cnt_visitaFijasXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_visitaFijasXusuario_creo]
GO
CREATE PROCEDURE [sel_cnt_visitaFijasXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_cnt_visitaFija]
	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_cnt_visitaFijasXdocumento_ot]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_visitaFijasXdocumento_ot]
GO
CREATE PROCEDURE [sel_cnt_visitaFijasXdocumento_ot]
	@p_id_documento_ot INT
AS
BEGIN
	SELECT * 
	FROM [view_cnt_visitaFija]
	WHERE ([id_documento.ot]= @p_id_documento_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_cnt_visitaFijasXcontratoEquipo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_visitaFijasXcontratoEquipo]
GO
CREATE PROCEDURE [sel_cnt_visitaFijasXcontratoEquipo]
	@p_id_contratoEquipo INT
AS
BEGIN
	SELECT * 
	FROM [view_cnt_visitaFija]
	WHERE ([id_contratoEquipo]= @p_id_contratoEquipo)
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_cnt_visitaFijasXcontratoDetalleVisita]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_visitaFijasXcontratoDetalleVisita]
GO
CREATE PROCEDURE [sel_cnt_visitaFijasXcontratoDetalleVisita]
	@p_id_contratoDetalleVisita INT
AS
BEGIN
	SELECT * 
	FROM [view_cnt_visitaFija]
	WHERE ([id_contratoDetalleVisita]= @p_id_contratoDetalleVisita)
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_cnt_visitaFijasXcatalogo_tempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_visitaFijasXcatalogo_tempario]
GO
CREATE PROCEDURE [sel_cnt_visitaFijasXcatalogo_tempario]
	@p_id_catalogo_tempario INT
AS
BEGIN
	SELECT * 
	FROM [view_cnt_visitaFija]
	WHERE ([id_catalogo.tempario]= @p_id_catalogo_tempario)
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_cnt_visitaFijas]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_visitaFijas]
GO
CREATE PROCEDURE [sel_cnt_visitaFijas]
	
	@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_cnt_visitaFija]
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_cnt_visitaFijas]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_cnt_visitaFijas]
GO
CREATE PROCEDURE [lis_cnt_visitaFijas]
	AS
BEGIN
	SELECT id AS id,
[visitaFija] AS [cnt_visitaFija]
	FROM [cnt_visitaFija]
	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_cnt_visitaFija]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_cnt_visitaFija]
GO
CREATE PROCEDURE [upd_cnt_visitaFija]
	@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_visitaFija varchar(300) = null,
		@p_visitaFija_codigo varchar(100) = null,
		@p_fecha_ff smalldatetime = null,
		@p_fechaCreada_ff smalldatetime = null,
		@p_horometro float = null,
		@p_id_documento_ot int = null,
		@p_id_contratoEquipo int = null,
		@p_id_contratoDetalleVisita int = null,
		@p_id_catalogo_tempario int = null,
		@p_esPorTrabajo bit = null,
		@p_correoRecordatorio varchar(500) = null
AS
BEGIN
		UPDATE [cnt_visitaFija]
		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(),
			[visitaFija] = isnull(@p_visitaFija,[visitaFija]),
			[visitaFija_codigo] = isnull(@p_visitaFija_codigo,[visitaFija_codigo]),
			[fecha_ff] = isnull(@p_fecha_ff,[fecha_ff]),
			[fechaCreada_ff] = isnull(@p_fechaCreada_ff,[fechaCreada_ff]),
			[horometro] = isnull(@p_horometro,[horometro]),
			[id_documento.ot] = isnull(@p_id_documento_ot,[id_documento.ot]),
			[id_contratoEquipo] = isnull(@p_id_contratoEquipo,[id_contratoEquipo]),
			[id_contratoDetalleVisita] = isnull(@p_id_contratoDetalleVisita,[id_contratoDetalleVisita]),
			[id_catalogo.tempario] = isnull(@p_id_catalogo_tempario,[id_catalogo.tempario]),
			[esPorTrabajo] = isnull(@p_esPorTrabajo,[esPorTrabajo]),
			[correoRecordatorio] = isnull(@p_correoRecordatorio,[correoRecordatorio])
		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_cnt_visitaFija]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_cnt_visitaFija]
GO
CREATE PROCEDURE [ins_cnt_visitaFija]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_visitaFija varchar(300),
		@p_visitaFija_codigo varchar(100),
		@p_fecha_ff smalldatetime,
		@p_fechaCreada_ff smalldatetime,
		@p_horometro float,
		@p_id_documento_ot int,
		@p_id_contratoEquipo int,
		@p_id_contratoDetalleVisita int,
		@p_id_catalogo_tempario int,
		@p_esPorTrabajo bit,
		@p_correoRecordatorio varchar(500)
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [cnt_visitaFija]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[visitaFija],
			[visitaFija_codigo],
			[fecha_ff],
			[fechaCreada_ff],
			[horometro],
			[id_documento.ot],
			[id_contratoEquipo],
			[id_contratoDetalleVisita],
			[id_catalogo.tempario],
			[esPorTrabajo],
			[correoRecordatorio])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_visitaFija,
			@p_visitaFija_codigo,
			@p_fecha_ff,
			@p_fechaCreada_ff,
			@p_horometro,
			@p_id_documento_ot,
			@p_id_contratoEquipo,
			@p_id_contratoDetalleVisita,
			@p_id_catalogo_tempario,
			@p_esPorTrabajo,
			@p_correoRecordatorio)
		SET @v_id = scope_identity()
UPDATE [cnt_visitaFija] 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_cnt_visitaFija]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_cnt_visitaFija]
GO
CREATE PROCEDURE [act_cnt_visitaFija]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [cnt_visitaFija]
	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_cnt_visitaFija_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_cnt_visitaFija_m]
GO
CREATE PROCEDURE [reg_cnt_visitaFija_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],
		[visitaFija] AS [visitaFija],
		[visitaFija_codigo] AS [visitaFija_codigo],
		[fecha_ff] AS [fecha_ff],
		[fechaCreada_ff] AS [fechaCreada_ff],
		[horometro] AS [horometro],
		[id_documento.ot] AS [id_documento.ot],
		[id_contratoEquipo] AS [id_contratoEquipo],
		[id_contratoDetalleVisita] AS [id_contratoDetalleVisita],
		[id_catalogo.tempario] AS [id_catalogo.tempario],
		[esPorTrabajo] AS [esPorTrabajo],
		[correoRecordatorio] AS [correoRecordatorio]
	FROM [cnt_visitaFija]
	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_cnt_visitaFijasXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_visitaFijasXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_cnt_visitaFijasXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cnt_visitaFija]
	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_cnt_visitaFijasXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_visitaFijasXusuario_creo_m]
GO
CREATE PROCEDURE [sel_cnt_visitaFijasXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cnt_visitaFija]
	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_cnt_visitaFijasXdocumento_ot_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_visitaFijasXdocumento_ot_m]
GO
CREATE PROCEDURE [sel_cnt_visitaFijasXdocumento_ot_m]
	@p_id_documento_ot INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cnt_visitaFija]
	WHERE ([id_documento.ot]= @p_id_documento_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_cnt_visitaFijasXcontratoEquipo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_visitaFijasXcontratoEquipo_m]
GO
CREATE PROCEDURE [sel_cnt_visitaFijasXcontratoEquipo_m]
	@p_id_contratoEquipo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cnt_visitaFija]
	WHERE ([id_contratoEquipo]= @p_id_contratoEquipo)
	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_cnt_visitaFijasXcontratoDetalleVisita_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_visitaFijasXcontratoDetalleVisita_m]
GO
CREATE PROCEDURE [sel_cnt_visitaFijasXcontratoDetalleVisita_m]
	@p_id_contratoDetalleVisita INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cnt_visitaFija]
	WHERE ([id_contratoDetalleVisita]= @p_id_contratoDetalleVisita)
	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_cnt_visitaFijasXcatalogo_tempario_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_visitaFijasXcatalogo_tempario_m]
GO
CREATE PROCEDURE [sel_cnt_visitaFijasXcatalogo_tempario_m]
	@p_id_catalogo_tempario INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cnt_visitaFija]
	WHERE ([id_catalogo.tempario]= @p_id_catalogo_tempario)
	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_cnt_visitaFijas_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_visitaFijas_m]
GO
CREATE PROCEDURE [sel_cnt_visitaFijas_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_cnt_visitaFija]
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_cnt_visitaFijas_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_cnt_visitaFijas_m]
GO
CREATE PROCEDURE [lis_cnt_visitaFijas_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[visitaFija] AS [cnt_visitaFija]
	FROM [cnt_visitaFija]
	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_cnt_visitaFija]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_cnt_visitaFija]
GO
CREATE PROCEDURE [aud_cnt_visitaFija]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [cnt_visitaFija].id as id_tabla,
	[cnt_visitaFija].id_usuario_modifico as id_usuario_modifico,
	[cnt_visitaFija].id_usuario_creo as id_usuario_creo,
	[cnt_visitaFija].fechaModificacion as fechaModificacion,
	[cnt_visitaFija].fechaCreacion as fechaCreacion,
	[cnt_visitaFija].active as active,
	[cnt_visitaFija].[visitaFija] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

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