---Procedimientos almacenados y vistas para equ_alquiler, equ_detalleAlquiler
---equ_trazabilidad y equ_pagoAlquiler

---28/02/2011 08:16:34 a.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_equ_alquiler]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_equ_alquiler]
GO
CREATE PROCEDURE [reg_equ_alquiler]
	@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],
		[alquiler] AS [alquiler],
		[alquiler_codigo] AS [alquiler_codigo],
		[fechaCompromiso_ff] AS [fechaCompromiso_ff],
		[id_documento.ot] AS [id_documento.ot],
		[id_sucursal] AS [id_sucursal],
		[id_tercero] AS [id_tercero],
		[fechaFin_ff] AS [fechaFin_ff],
		[id_subtipoDocumento] AS [id_subtipoDocumento],
		[mesVencido] AS [mesVencido],
		[alquiler_numero] AS [alquiler_numero]
	FROM [equ_alquiler]
	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_alquiler]')) DROP VIEW [dbo].[view_equ_alquiler]
GO
CREATE  VIEW [view_equ_alquiler] AS
SELECT 	[equ_alquiler].[id] AS [id],
		[equ_alquiler].[uid] AS [uid],
		[equ_alquiler].[eid] AS [eid],
		[equ_alquiler].[id_usuario_modifico] AS [id_usuario_modifico],
		[equ_alquiler].[id_usuario_creo] AS [id_usuario_creo],
		[equ_alquiler].[fechaModificacion] AS [fechaModificacion],
		[equ_alquiler].[fechaCreacion] AS [fechaCreacion],
		[equ_alquiler].[active] AS [active],
		[equ_alquiler].[alquiler] AS [alquiler],
		[equ_alquiler].[alquiler_codigo] AS [alquiler_codigo],
		[equ_alquiler].[fechaCompromiso_ff] AS [fechaCompromiso_ff],
		[equ_alquiler].[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],
		[equ_alquiler].[id_sucursal] AS [id_sucursal],
[ter_sucursal].[sucursal] as [ter_sucursal_sucursal],
[ter_sucursal].[sucursal_codigo] as [ter_sucursal_sucursal_codigo],
[ter_sucursal].[contacto] as [ter_sucursal_contacto],
[ter_sucursal].[telefono] as [ter_sucursal_telefono],
[ter_sucursal].[Fax] as [ter_sucursal_Fax],
[ter_sucursal].[Email] as [ter_sucursal_Email],
[ter_sucursal].[Direccion] as [ter_sucursal_Direccion],
[ter_sucursal].[paraVenta] as [ter_sucursal_paraVenta],
[ter_sucursal].[paraSoporte] as [ter_sucursal_paraSoporte],
[ter_sucursal].[paraAlquiler] as [ter_sucursal_paraAlquiler],
[ter_sucursal].[id_tercero] as [ter_sucursal_id_tercero],
[ter_sucursal].[id_zona] as [ter_sucursal_id_zona],
[ter_sucursal].[id_usuario_asesor] as [ter_sucursal_id_usuario_asesor],
[ter_sucursal].[id_usuario_tecnico] as [ter_sucursal_id_usuario_tecnico],
[ter_sucursal].[id_sucursal] as [ter_sucursal_id_sucursal],
[ter_sucursal].[id_listaPrecio] as [ter_sucursal_id_listaPrecio],
[ter_sucursal].[id_estadoTercero] as [ter_sucursal_id_estadoTercero],
		[equ_alquiler].[id_tercero] AS [id_tercero],
[ter_tercero].[tercero] as [ter_tercero_tercero],
[ter_tercero].[tercero_codigo] as [ter_tercero_tercero_codigo],
[ter_tercero].[tercero_nit] as [ter_tercero_tercero_nit],
[ter_tercero].[direccion] as [ter_tercero_direccion],
[ter_tercero].[contacto] as [ter_tercero_contacto],
[ter_tercero].[telefono] as [ter_tercero_telefono],
[ter_tercero].[fax] as [ter_tercero_fax],
[ter_tercero].[email] as [ter_tercero_email],
[ter_tercero].[website] as [ter_tercero_website],
[ter_tercero].[representante] as [ter_tercero_representante],
[ter_tercero].[notas] as [ter_tercero_notas],
[ter_tercero].[tercero_nombreCorto] as [ter_tercero_tercero_nombreCorto],
[ter_tercero].[esCliente] as [ter_tercero_esCliente],
[ter_tercero].[esProveedor] as [ter_tercero_esProveedor],
[ter_tercero].[esTransportador] as [ter_tercero_esTransportador],
[ter_tercero].[esFabricante] as [ter_tercero_esFabricante],
[ter_tercero].[esEmpresaPropia] as [ter_tercero_esEmpresaPropia],
[ter_tercero].[clasificacion] as [ter_tercero_clasificacion],
[ter_tercero].[id_zona] as [ter_tercero_id_zona],
[ter_tercero].[id_estadoTercero] as [ter_tercero_id_estadoTercero],
[ter_tercero].[id_listaPrecio] as [ter_tercero_id_listaPrecio],
[ter_tercero].[id_naturalezaTercero] as [ter_tercero_id_naturalezaTercero],
		[equ_alquiler].[fechaFin_ff] AS [fechaFin_ff],
		[equ_alquiler].[id_subtipoDocumento] AS [id_subtipoDocumento],
[doc_subtipoDocumento].[subtipoDocumento] as [doc_subtipoDocumento_subtipoDocumento],
[doc_subtipoDocumento].[subtipoDocumento_codigo] as [doc_subtipoDocumento_subtipoDocumento_codigo],
[doc_subtipoDocumento].[prefijo] as [doc_subtipoDocumento_prefijo],
[doc_subtipoDocumento].[consecutivo] as [doc_subtipoDocumento_consecutivo],
[doc_subtipoDocumento].[id_tipoDocumento] as [doc_subtipoDocumento_id_tipoDocumento],
		[equ_alquiler].[mesVencido] AS [mesVencido],
		[equ_alquiler].[alquiler_numero] AS [alquiler_numero],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [equ_alquiler]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[equ_alquiler].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[equ_alquiler].[id_usuario_creo]
	INNER JOIN [doc_documento.ot] ON [doc_documento.ot].id=[equ_alquiler].[id_documento.ot]
	INNER JOIN [doc_documento] aS [doc_documento_ot] ON [doc_documento.ot].id = [doc_documento_ot].id
	INNER JOIN [ter_sucursal] ON [ter_sucursal].id=[equ_alquiler].[id_sucursal]
	INNER JOIN [ter_tercero] ON [ter_tercero].id=[equ_alquiler].[id_tercero]
	INNER JOIN [doc_subtipoDocumento] ON [doc_subtipoDocumento].id=[equ_alquiler].[id_subtipoDocumento]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [equ_alquiler].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [equ_alquiler].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_alquileresXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_alquileresXusuario_modifico]
GO
CREATE PROCEDURE [sel_equ_alquileresXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_alquiler]
	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_alquileresXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_alquileresXusuario_creo]
GO
CREATE PROCEDURE [sel_equ_alquileresXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_alquiler]
	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_alquileresXdocumento_ot]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_alquileresXdocumento_ot]
GO
CREATE PROCEDURE [sel_equ_alquileresXdocumento_ot]
	@p_id_documento_ot INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_alquiler]
	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_equ_alquileresXsucursal]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_alquileresXsucursal]
GO
CREATE PROCEDURE [sel_equ_alquileresXsucursal]
	@p_id_sucursal INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_alquiler]
	WHERE ([id_sucursal]= @p_id_sucursal)
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_alquileresXtercero]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_alquileresXtercero]
GO
CREATE PROCEDURE [sel_equ_alquileresXtercero]
	@p_id_tercero INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_alquiler]
	WHERE ([id_tercero]= @p_id_tercero)
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_alquileresXsubtipoDocumento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_alquileresXsubtipoDocumento]
GO
CREATE PROCEDURE [sel_equ_alquileresXsubtipoDocumento]
	@p_id_subtipoDocumento INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_alquiler]
	WHERE ([id_subtipoDocumento]= @p_id_subtipoDocumento)
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_alquileres]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_alquileres]
GO
CREATE PROCEDURE [sel_equ_alquileres]
	
	@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_alquiler]
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_alquileres]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_equ_alquileres]
GO
CREATE PROCEDURE [lis_equ_alquileres]
	AS
BEGIN
	SELECT id AS id,
[alquiler] AS [equ_alquiler]
	FROM [equ_alquiler]
	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_alquiler]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_equ_alquiler]
GO
CREATE PROCEDURE [upd_equ_alquiler]
	@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_alquiler varchar(300) = null,
		@p_alquiler_codigo varchar(100) = null,
		@p_fechaCompromiso_ff smalldatetime = null,
		@p_id_documento_ot int = null,
		@p_id_sucursal int = null,
		@p_id_tercero int = null,
		@p_fechaFin_ff smalldatetime = null,
		@p_id_subtipoDocumento int = null,
		@p_mesVencido bit = null,
		@p_alquiler_numero int = null
AS
BEGIN
		UPDATE [equ_alquiler]
		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(),
			[alquiler] = isnull(@p_alquiler,[alquiler]),
			[alquiler_codigo] = isnull(@p_alquiler_codigo,[alquiler_codigo]),
			[fechaCompromiso_ff] = isnull(@p_fechaCompromiso_ff,[fechaCompromiso_ff]),
			[id_documento.ot] = isnull(@p_id_documento_ot,[id_documento.ot]),
			[id_sucursal] = isnull(@p_id_sucursal,[id_sucursal]),
			[id_tercero] = isnull(@p_id_tercero,[id_tercero]),
			[fechaFin_ff] = isnull(@p_fechaFin_ff,[fechaFin_ff]),
			[id_subtipoDocumento] = isnull(@p_id_subtipoDocumento,[id_subtipoDocumento]),
			[mesVencido] = isnull(@p_mesVencido,[mesVencido]),
			[alquiler_numero] = isnull(@p_alquiler_numero,[alquiler_numero])
		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_alquiler]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_equ_alquiler]
GO
CREATE PROCEDURE [ins_equ_alquiler]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_alquiler varchar(300),
		@p_alquiler_codigo varchar(100),
		@p_fechaCompromiso_ff smalldatetime,
		@p_id_documento_ot int,
		@p_id_sucursal int,
		@p_id_tercero int,
		@p_fechaFin_ff smalldatetime,
		@p_id_subtipoDocumento int,
		@p_mesVencido bit,
		@p_alquiler_numero int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [equ_alquiler]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[alquiler],
			[alquiler_codigo],
			[fechaCompromiso_ff],
			[id_documento.ot],
			[id_sucursal],
			[id_tercero],
			[fechaFin_ff],
			[id_subtipoDocumento],
			[mesVencido],
			[alquiler_numero])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_alquiler,
			@p_alquiler_codigo,
			@p_fechaCompromiso_ff,
			@p_id_documento_ot,
			@p_id_sucursal,
			@p_id_tercero,
			@p_fechaFin_ff,
			@p_id_subtipoDocumento,
			@p_mesVencido,
			@p_alquiler_numero)
		SET @v_id = scope_identity()
UPDATE [equ_alquiler] 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_alquiler]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_equ_alquiler]
GO
CREATE PROCEDURE [act_equ_alquiler]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [equ_alquiler]
	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_alquiler_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_equ_alquiler_m]
GO
CREATE PROCEDURE [reg_equ_alquiler_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],
		[alquiler] AS [alquiler],
		[alquiler_codigo] AS [alquiler_codigo],
		[fechaCompromiso_ff] AS [fechaCompromiso_ff],
		[id_documento.ot] AS [id_documento.ot],
		[id_sucursal] AS [id_sucursal],
		[id_tercero] AS [id_tercero],
		[fechaFin_ff] AS [fechaFin_ff],
		[id_subtipoDocumento] AS [id_subtipoDocumento],
		[mesVencido] AS [mesVencido],
		[alquiler_numero] AS [alquiler_numero]
	FROM [equ_alquiler]
	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_alquileresXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_alquileresXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_equ_alquileresXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_alquiler]
	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_alquileresXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_alquileresXusuario_creo_m]
GO
CREATE PROCEDURE [sel_equ_alquileresXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_alquiler]
	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_alquileresXdocumento_ot_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_alquileresXdocumento_ot_m]
GO
CREATE PROCEDURE [sel_equ_alquileresXdocumento_ot_m]
	@p_id_documento_ot INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_alquiler]
	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_equ_alquileresXsucursal_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_alquileresXsucursal_m]
GO
CREATE PROCEDURE [sel_equ_alquileresXsucursal_m]
	@p_id_sucursal INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_alquiler]
	WHERE ([id_sucursal]= @p_id_sucursal)
	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_alquileresXtercero_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_alquileresXtercero_m]
GO
CREATE PROCEDURE [sel_equ_alquileresXtercero_m]
	@p_id_tercero INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_alquiler]
	WHERE ([id_tercero]= @p_id_tercero)
	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_alquileresXsubtipoDocumento_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_alquileresXsubtipoDocumento_m]
GO
CREATE PROCEDURE [sel_equ_alquileresXsubtipoDocumento_m]
	@p_id_subtipoDocumento INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_alquiler]
	WHERE ([id_subtipoDocumento]= @p_id_subtipoDocumento)
	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_alquileres_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_alquileres_m]
GO
CREATE PROCEDURE [sel_equ_alquileres_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_alquiler]
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_alquileres_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_equ_alquileres_m]
GO
CREATE PROCEDURE [lis_equ_alquileres_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[alquiler] AS [equ_alquiler]
	FROM [equ_alquiler]
	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_alquiler]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_equ_alquiler]
GO
CREATE PROCEDURE [aud_equ_alquiler]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [equ_alquiler].id as id_tabla,
	[equ_alquiler].id_usuario_modifico as id_usuario_modifico,
	[equ_alquiler].id_usuario_creo as id_usuario_creo,
	[equ_alquiler].fechaModificacion as fechaModificacion,
	[equ_alquiler].fechaCreacion as fechaCreacion,
	[equ_alquiler].active as active,
	[equ_alquiler].[alquiler] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [equ_alquiler]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[equ_alquiler].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[equ_alquiler].[id_usuario_creo]
	where ' + @p_filtro)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'equ_alquiler'
--------------------------------
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_detalleAlquiler]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_equ_detalleAlquiler]
GO
CREATE PROCEDURE [reg_equ_detalleAlquiler]
	@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],
		[detalleAlquiler] AS [detalleAlquiler],
		[detalleAlquiler_codigo] AS [detalleAlquiler_codigo],
		[fechaDevolucion_ff] AS [fechaDevolucion_ff],
		[valorHora] AS [valorHora],
		[ValorHoraStandBy] AS [ValorHoraStandBy],
		[valorPeriodo] AS [valorPeriodo],
		[horometro] AS [horometro],
		[valorMulta] AS [valorMulta],
		[fechaDespacho_ff] AS [fechaDespacho_ff],
		[id_equipo] AS [id_equipo],
		[id_alquiler] AS [id_alquiler],
		[id_sucursal] AS [id_sucursal],
		[id_tercero] AS [id_tercero],
		[fechaUltimaFactura_fh] AS [fechaUltimaFactura_fh],
		[fechaCompromisoDevolucion_ff] AS [fechaCompromisoDevolucion_ff],
		[valorMes] AS [valorMes],
		[fechaInicioFactura_fh] AS [fechaInicioFactura_fh]
	FROM [equ_detalleAlquiler]
	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_detalleAlquiler]')) DROP VIEW [dbo].[view_equ_detalleAlquiler]
GO
CREATE  VIEW [view_equ_detalleAlquiler] AS
SELECT 	[equ_detalleAlquiler].[id] AS [id],
		[equ_detalleAlquiler].[uid] AS [uid],
		[equ_detalleAlquiler].[eid] AS [eid],
		[equ_detalleAlquiler].[id_usuario_modifico] AS [id_usuario_modifico],
		[equ_detalleAlquiler].[id_usuario_creo] AS [id_usuario_creo],
		[equ_detalleAlquiler].[fechaModificacion] AS [fechaModificacion],
		[equ_detalleAlquiler].[fechaCreacion] AS [fechaCreacion],
		[equ_detalleAlquiler].[active] AS [active],
		[equ_detalleAlquiler].[detalleAlquiler] AS [detalleAlquiler],
		[equ_detalleAlquiler].[detalleAlquiler_codigo] AS [detalleAlquiler_codigo],
		[equ_detalleAlquiler].[fechaDevolucion_ff] AS [fechaDevolucion_ff],
		[equ_detalleAlquiler].[valorHora] AS [valorHora],
		[equ_detalleAlquiler].[ValorHoraStandBy] AS [ValorHoraStandBy],
		[equ_detalleAlquiler].[valorPeriodo] AS [valorPeriodo],
		[equ_detalleAlquiler].[horometro] AS [horometro],
		[equ_detalleAlquiler].[valorMulta] AS [valorMulta],
		[equ_detalleAlquiler].[fechaDespacho_ff] AS [fechaDespacho_ff],
		[equ_detalleAlquiler].[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_detalleAlquiler].[id_alquiler] AS [id_alquiler],
[equ_alquiler].[alquiler] as [equ_alquiler_alquiler],
[equ_alquiler].[alquiler_codigo] as [equ_alquiler_alquiler_codigo],
[equ_alquiler].[fechaCompromiso_ff] as [equ_alquiler_fechaCompromiso_ff],
[equ_alquiler].[id_documento.ot] as [equ_alquiler_id_documento.ot],
[equ_alquiler].[id_sucursal] as [equ_alquiler_id_sucursal],
[equ_alquiler].[id_tercero] as [equ_alquiler_id_tercero],
[equ_alquiler].[fechaFin_ff] as [equ_alquiler_fechaFin_ff],
[equ_alquiler].[id_subtipoDocumento] as [equ_alquiler_id_subtipoDocumento],
[equ_alquiler].[mesVencido] as [equ_alquiler_mesVencido],
[equ_alquiler].[alquiler_numero] as [equ_alquiler_alquiler_numero],
		[equ_detalleAlquiler].[id_sucursal] AS [id_sucursal],
[ter_sucursal].[sucursal] as [ter_sucursal_sucursal],
[ter_sucursal].[sucursal_codigo] as [ter_sucursal_sucursal_codigo],
[ter_sucursal].[contacto] as [ter_sucursal_contacto],
[ter_sucursal].[telefono] as [ter_sucursal_telefono],
[ter_sucursal].[Fax] as [ter_sucursal_Fax],
[ter_sucursal].[Email] as [ter_sucursal_Email],
[ter_sucursal].[Direccion] as [ter_sucursal_Direccion],
[ter_sucursal].[paraVenta] as [ter_sucursal_paraVenta],
[ter_sucursal].[paraSoporte] as [ter_sucursal_paraSoporte],
[ter_sucursal].[paraAlquiler] as [ter_sucursal_paraAlquiler],
[ter_sucursal].[id_tercero] as [ter_sucursal_id_tercero],
[ter_sucursal].[id_zona] as [ter_sucursal_id_zona],
[ter_sucursal].[id_usuario_asesor] as [ter_sucursal_id_usuario_asesor],
[ter_sucursal].[id_usuario_tecnico] as [ter_sucursal_id_usuario_tecnico],
[ter_sucursal].[id_sucursal] as [ter_sucursal_id_sucursal],
[ter_sucursal].[id_listaPrecio] as [ter_sucursal_id_listaPrecio],
[ter_sucursal].[id_estadoTercero] as [ter_sucursal_id_estadoTercero],
		[equ_detalleAlquiler].[id_tercero] AS [id_tercero],
[ter_tercero].[tercero] as [ter_tercero_tercero],
[ter_tercero].[tercero_codigo] as [ter_tercero_tercero_codigo],
[ter_tercero].[tercero_nit] as [ter_tercero_tercero_nit],
[ter_tercero].[direccion] as [ter_tercero_direccion],
[ter_tercero].[contacto] as [ter_tercero_contacto],
[ter_tercero].[telefono] as [ter_tercero_telefono],
[ter_tercero].[fax] as [ter_tercero_fax],
[ter_tercero].[email] as [ter_tercero_email],
[ter_tercero].[website] as [ter_tercero_website],
[ter_tercero].[representante] as [ter_tercero_representante],
[ter_tercero].[notas] as [ter_tercero_notas],
[ter_tercero].[tercero_nombreCorto] as [ter_tercero_tercero_nombreCorto],
[ter_tercero].[esCliente] as [ter_tercero_esCliente],
[ter_tercero].[esProveedor] as [ter_tercero_esProveedor],
[ter_tercero].[esTransportador] as [ter_tercero_esTransportador],
[ter_tercero].[esFabricante] as [ter_tercero_esFabricante],
[ter_tercero].[esEmpresaPropia] as [ter_tercero_esEmpresaPropia],
[ter_tercero].[clasificacion] as [ter_tercero_clasificacion],
[ter_tercero].[id_zona] as [ter_tercero_id_zona],
[ter_tercero].[id_estadoTercero] as [ter_tercero_id_estadoTercero],
[ter_tercero].[id_listaPrecio] as [ter_tercero_id_listaPrecio],
[ter_tercero].[id_naturalezaTercero] as [ter_tercero_id_naturalezaTercero],
		[equ_detalleAlquiler].[fechaUltimaFactura_fh] AS [fechaUltimaFactura_fh],
		[equ_detalleAlquiler].[fechaCompromisoDevolucion_ff] AS [fechaCompromisoDevolucion_ff],
		[equ_detalleAlquiler].[valorMes] AS [valorMes],
		[equ_detalleAlquiler].[fechaInicioFactura_fh] AS [fechaInicioFactura_fh],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [equ_detalleAlquiler]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[equ_detalleAlquiler].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[equ_detalleAlquiler].[id_usuario_creo]
	INNER JOIN [equ_equipo] ON [equ_equipo].id=[equ_detalleAlquiler].[id_equipo]
	INNER JOIN [equ_alquiler] ON [equ_alquiler].id=[equ_detalleAlquiler].[id_alquiler]
	INNER JOIN [ter_sucursal] ON [ter_sucursal].id=[equ_detalleAlquiler].[id_sucursal]
	INNER JOIN [ter_tercero] ON [ter_tercero].id=[equ_detalleAlquiler].[id_tercero]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [equ_detalleAlquiler].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [equ_detalleAlquiler].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_detalleAlquileresXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_detalleAlquileresXusuario_modifico]
GO
CREATE PROCEDURE [sel_equ_detalleAlquileresXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_detalleAlquiler]
	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_detalleAlquileresXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_detalleAlquileresXusuario_creo]
GO
CREATE PROCEDURE [sel_equ_detalleAlquileresXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_detalleAlquiler]
	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_detalleAlquileresXequipo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_detalleAlquileresXequipo]
GO
CREATE PROCEDURE [sel_equ_detalleAlquileresXequipo]
	@p_id_equipo INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_detalleAlquiler]
	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_detalleAlquileresXalquiler]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_detalleAlquileresXalquiler]
GO
CREATE PROCEDURE [sel_equ_detalleAlquileresXalquiler]
	@p_id_alquiler INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_detalleAlquiler]
	WHERE ([id_alquiler]= @p_id_alquiler)
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_detalleAlquileresXsucursal]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_detalleAlquileresXsucursal]
GO
CREATE PROCEDURE [sel_equ_detalleAlquileresXsucursal]
	@p_id_sucursal INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_detalleAlquiler]
	WHERE ([id_sucursal]= @p_id_sucursal)
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_detalleAlquileresXtercero]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_detalleAlquileresXtercero]
GO
CREATE PROCEDURE [sel_equ_detalleAlquileresXtercero]
	@p_id_tercero INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_detalleAlquiler]
	WHERE ([id_tercero]= @p_id_tercero)
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_detalleAlquileres]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_detalleAlquileres]
GO
CREATE PROCEDURE [sel_equ_detalleAlquileres]
	
	@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_detalleAlquiler]
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_detalleAlquileres]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_equ_detalleAlquileres]
GO
CREATE PROCEDURE [lis_equ_detalleAlquileres]
	AS
BEGIN
	SELECT id AS id,
[detalleAlquiler] AS [equ_detalleAlquiler]
	FROM [equ_detalleAlquiler]
	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_detalleAlquiler]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_equ_detalleAlquiler]
GO
CREATE PROCEDURE [upd_equ_detalleAlquiler]
	@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_detalleAlquiler varchar(300) = null,
		@p_detalleAlquiler_codigo varchar(100) = null,
		@p_fechaDevolucion_ff smalldatetime = null,
		@p_valorHora money = null,
		@p_ValorHoraStandBy money = null,
		@p_valorPeriodo money = null,
		@p_horometro float = null,
		@p_valorMulta money = null,
		@p_fechaDespacho_ff smalldatetime = null,
		@p_id_equipo int = null,
		@p_id_alquiler int = null,
		@p_id_sucursal int = null,
		@p_id_tercero int = null,
		@p_fechaUltimaFactura_fh smalldatetime = null,
		@p_fechaCompromisoDevolucion_ff smalldatetime = null,
		@p_valorMes float = null,
		@p_fechaInicioFactura_fh smalldatetime = null
AS
BEGIN
		UPDATE [equ_detalleAlquiler]
		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(),
			[detalleAlquiler] = isnull(@p_detalleAlquiler,[detalleAlquiler]),
			[detalleAlquiler_codigo] = isnull(@p_detalleAlquiler_codigo,[detalleAlquiler_codigo]),
			[fechaDevolucion_ff] = isnull(@p_fechaDevolucion_ff,[fechaDevolucion_ff]),
			[valorHora] = isnull(@p_valorHora,[valorHora]),
			[ValorHoraStandBy] = isnull(@p_ValorHoraStandBy,[ValorHoraStandBy]),
			[valorPeriodo] = isnull(@p_valorPeriodo,[valorPeriodo]),
			[horometro] = isnull(@p_horometro,[horometro]),
			[valorMulta] = isnull(@p_valorMulta,[valorMulta]),
			[fechaDespacho_ff] = isnull(@p_fechaDespacho_ff,[fechaDespacho_ff]),
			[id_equipo] = isnull(@p_id_equipo,[id_equipo]),
			[id_alquiler] = isnull(@p_id_alquiler,[id_alquiler]),
			[id_sucursal] = isnull(@p_id_sucursal,[id_sucursal]),
			[id_tercero] = isnull(@p_id_tercero,[id_tercero]),
			[fechaUltimaFactura_fh] = isnull(@p_fechaUltimaFactura_fh,[fechaUltimaFactura_fh]),
			[fechaCompromisoDevolucion_ff] = isnull(@p_fechaCompromisoDevolucion_ff,[fechaCompromisoDevolucion_ff]),
			[valorMes] = isnull(@p_valorMes,[valorMes]),
			[fechaInicioFactura_fh] = isnull(@p_fechaInicioFactura_fh,[fechaInicioFactura_fh])
		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_detalleAlquiler]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_equ_detalleAlquiler]
GO
CREATE PROCEDURE [ins_equ_detalleAlquiler]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_detalleAlquiler varchar(300),
		@p_detalleAlquiler_codigo varchar(100),
		@p_fechaDevolucion_ff smalldatetime,
		@p_valorHora money,
		@p_ValorHoraStandBy money,
		@p_valorPeriodo money,
		@p_horometro float,
		@p_valorMulta money,
		@p_fechaDespacho_ff smalldatetime,
		@p_id_equipo int,
		@p_id_alquiler int,
		@p_id_sucursal int,
		@p_id_tercero int,
		@p_fechaUltimaFactura_fh smalldatetime,
		@p_fechaCompromisoDevolucion_ff smalldatetime,
		@p_valorMes float,
		@p_fechaInicioFactura_fh smalldatetime
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [equ_detalleAlquiler]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[detalleAlquiler],
			[detalleAlquiler_codigo],
			[fechaDevolucion_ff],
			[valorHora],
			[ValorHoraStandBy],
			[valorPeriodo],
			[horometro],
			[valorMulta],
			[fechaDespacho_ff],
			[id_equipo],
			[id_alquiler],
			[id_sucursal],
			[id_tercero],
			[fechaUltimaFactura_fh],
			[fechaCompromisoDevolucion_ff],
			[valorMes],
			[fechaInicioFactura_fh])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_detalleAlquiler,
			@p_detalleAlquiler_codigo,
			@p_fechaDevolucion_ff,
			@p_valorHora,
			@p_ValorHoraStandBy,
			@p_valorPeriodo,
			@p_horometro,
			@p_valorMulta,
			@p_fechaDespacho_ff,
			@p_id_equipo,
			@p_id_alquiler,
			@p_id_sucursal,
			@p_id_tercero,
			@p_fechaUltimaFactura_fh,
			@p_fechaCompromisoDevolucion_ff,
			@p_valorMes,
			@p_fechaInicioFactura_fh)
		SET @v_id = scope_identity()
UPDATE [equ_detalleAlquiler] 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_detalleAlquiler]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_equ_detalleAlquiler]
GO
CREATE PROCEDURE [act_equ_detalleAlquiler]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [equ_detalleAlquiler]
	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_detalleAlquiler_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_equ_detalleAlquiler_m]
GO
CREATE PROCEDURE [reg_equ_detalleAlquiler_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],
		[detalleAlquiler] AS [detalleAlquiler],
		[detalleAlquiler_codigo] AS [detalleAlquiler_codigo],
		[fechaDevolucion_ff] AS [fechaDevolucion_ff],
		[valorHora] AS [valorHora],
		[ValorHoraStandBy] AS [ValorHoraStandBy],
		[valorPeriodo] AS [valorPeriodo],
		[horometro] AS [horometro],
		[valorMulta] AS [valorMulta],
		[fechaDespacho_ff] AS [fechaDespacho_ff],
		[id_equipo] AS [id_equipo],
		[id_alquiler] AS [id_alquiler],
		[id_sucursal] AS [id_sucursal],
		[id_tercero] AS [id_tercero],
		[fechaUltimaFactura_fh] AS [fechaUltimaFactura_fh],
		[fechaCompromisoDevolucion_ff] AS [fechaCompromisoDevolucion_ff],
		[valorMes] AS [valorMes],
		[fechaInicioFactura_fh] AS [fechaInicioFactura_fh]
	FROM [equ_detalleAlquiler]
	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_detalleAlquileresXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_detalleAlquileresXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_equ_detalleAlquileresXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_detalleAlquiler]
	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_detalleAlquileresXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_detalleAlquileresXusuario_creo_m]
GO
CREATE PROCEDURE [sel_equ_detalleAlquileresXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_detalleAlquiler]
	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_detalleAlquileresXequipo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_detalleAlquileresXequipo_m]
GO
CREATE PROCEDURE [sel_equ_detalleAlquileresXequipo_m]
	@p_id_equipo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_detalleAlquiler]
	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_detalleAlquileresXalquiler_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_detalleAlquileresXalquiler_m]
GO
CREATE PROCEDURE [sel_equ_detalleAlquileresXalquiler_m]
	@p_id_alquiler INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_detalleAlquiler]
	WHERE ([id_alquiler]= @p_id_alquiler)
	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_detalleAlquileresXsucursal_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_detalleAlquileresXsucursal_m]
GO
CREATE PROCEDURE [sel_equ_detalleAlquileresXsucursal_m]
	@p_id_sucursal INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_detalleAlquiler]
	WHERE ([id_sucursal]= @p_id_sucursal)
	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_detalleAlquileresXtercero_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_detalleAlquileresXtercero_m]
GO
CREATE PROCEDURE [sel_equ_detalleAlquileresXtercero_m]
	@p_id_tercero INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_detalleAlquiler]
	WHERE ([id_tercero]= @p_id_tercero)
	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_detalleAlquileres_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_detalleAlquileres_m]
GO
CREATE PROCEDURE [sel_equ_detalleAlquileres_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_detalleAlquiler]
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_detalleAlquileres_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_equ_detalleAlquileres_m]
GO
CREATE PROCEDURE [lis_equ_detalleAlquileres_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[detalleAlquiler] AS [equ_detalleAlquiler]
	FROM [equ_detalleAlquiler]
	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_detalleAlquiler]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_equ_detalleAlquiler]
GO
CREATE PROCEDURE [aud_equ_detalleAlquiler]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [equ_detalleAlquiler].id as id_tabla,
	[equ_detalleAlquiler].id_usuario_modifico as id_usuario_modifico,
	[equ_detalleAlquiler].id_usuario_creo as id_usuario_creo,
	[equ_detalleAlquiler].fechaModificacion as fechaModificacion,
	[equ_detalleAlquiler].fechaCreacion as fechaCreacion,
	[equ_detalleAlquiler].active as active,
	[equ_detalleAlquiler].[detalleAlquiler] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [equ_detalleAlquiler]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[equ_detalleAlquiler].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[equ_detalleAlquiler].[id_usuario_creo]
	where ' + @p_filtro)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'equ_detalleAlquiler'
--------------------------------
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_pagoAlquiler]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_equ_pagoAlquiler]
GO
CREATE PROCEDURE [reg_equ_pagoAlquiler]
	@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],
		[pagoAlquiler] AS [pagoAlquiler],
		[pagoAlquiler_codigo] AS [pagoAlquiler_codigo],
		[valorPorCobrar] AS [valorPorCobrar],
		[valorFacturado] AS [valorFacturado],
		[numeroFactura] AS [numeroFactura],
		[fechaEsperadaPago_ff] AS [fechaEsperadaPago_ff],
		[fecha_factura_ff] AS [fecha_factura_ff],
		[id_alquiler] AS [id_alquiler]
	FROM [equ_pagoAlquiler]
	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_pagoAlquiler]')) DROP VIEW [dbo].[view_equ_pagoAlquiler]
GO
CREATE  VIEW [view_equ_pagoAlquiler] AS
SELECT 	[equ_pagoAlquiler].[id] AS [id],
		[equ_pagoAlquiler].[uid] AS [uid],
		[equ_pagoAlquiler].[eid] AS [eid],
		[equ_pagoAlquiler].[id_usuario_modifico] AS [id_usuario_modifico],
		[equ_pagoAlquiler].[id_usuario_creo] AS [id_usuario_creo],
		[equ_pagoAlquiler].[fechaModificacion] AS [fechaModificacion],
		[equ_pagoAlquiler].[fechaCreacion] AS [fechaCreacion],
		[equ_pagoAlquiler].[active] AS [active],
		[equ_pagoAlquiler].[pagoAlquiler] AS [pagoAlquiler],
		[equ_pagoAlquiler].[pagoAlquiler_codigo] AS [pagoAlquiler_codigo],
		[equ_pagoAlquiler].[valorPorCobrar] AS [valorPorCobrar],
		[equ_pagoAlquiler].[valorFacturado] AS [valorFacturado],
		[equ_pagoAlquiler].[numeroFactura] AS [numeroFactura],
		[equ_pagoAlquiler].[fechaEsperadaPago_ff] AS [fechaEsperadaPago_ff],
		[equ_pagoAlquiler].[fecha_factura_ff] AS [fecha_factura_ff],
		[equ_pagoAlquiler].[id_alquiler] AS [id_alquiler],
[equ_alquiler].[alquiler] as [equ_alquiler_alquiler],
[equ_alquiler].[alquiler_codigo] as [equ_alquiler_alquiler_codigo],
[equ_alquiler].[fechaCompromiso_ff] as [equ_alquiler_fechaCompromiso_ff],
[equ_alquiler].[id_documento.ot] as [equ_alquiler_id_documento.ot],
[equ_alquiler].[id_sucursal] as [equ_alquiler_id_sucursal],
[equ_alquiler].[id_tercero] as [equ_alquiler_id_tercero],
[equ_alquiler].[fechaFin_ff] as [equ_alquiler_fechaFin_ff],
[equ_alquiler].[id_subtipoDocumento] as [equ_alquiler_id_subtipoDocumento],
[equ_alquiler].[mesVencido] as [equ_alquiler_mesVencido],
[equ_alquiler].[alquiler_numero] as [equ_alquiler_alquiler_numero],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [equ_pagoAlquiler]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[equ_pagoAlquiler].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[equ_pagoAlquiler].[id_usuario_creo]
	INNER JOIN [equ_alquiler] ON [equ_alquiler].id=[equ_pagoAlquiler].[id_alquiler]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [equ_pagoAlquiler].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [equ_pagoAlquiler].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_pagoAlquileresXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_pagoAlquileresXusuario_modifico]
GO
CREATE PROCEDURE [sel_equ_pagoAlquileresXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_pagoAlquiler]
	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_pagoAlquileresXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_pagoAlquileresXusuario_creo]
GO
CREATE PROCEDURE [sel_equ_pagoAlquileresXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_pagoAlquiler]
	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_pagoAlquileresXalquiler]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_pagoAlquileresXalquiler]
GO
CREATE PROCEDURE [sel_equ_pagoAlquileresXalquiler]
	@p_id_alquiler INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_pagoAlquiler]
	WHERE ([id_alquiler]= @p_id_alquiler)
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_pagoAlquileres]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_pagoAlquileres]
GO
CREATE PROCEDURE [sel_equ_pagoAlquileres]
	
	@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_pagoAlquiler]
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_pagoAlquileres]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_equ_pagoAlquileres]
GO
CREATE PROCEDURE [lis_equ_pagoAlquileres]
	AS
BEGIN
	SELECT id AS id,
[pagoAlquiler] AS [equ_pagoAlquiler]
	FROM [equ_pagoAlquiler]
	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_pagoAlquiler]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_equ_pagoAlquiler]
GO
CREATE PROCEDURE [upd_equ_pagoAlquiler]
	@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_pagoAlquiler varchar(300) = null,
		@p_pagoAlquiler_codigo varchar(100) = null,
		@p_valorPorCobrar money = null,
		@p_valorFacturado money = null,
		@p_numeroFactura varchar(100) = null,
		@p_fechaEsperadaPago_ff smalldatetime = null,
		@p_fecha_factura_ff smalldatetime = null,
		@p_id_alquiler int = null
AS
BEGIN
		UPDATE [equ_pagoAlquiler]
		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(),
			[pagoAlquiler] = isnull(@p_pagoAlquiler,[pagoAlquiler]),
			[pagoAlquiler_codigo] = isnull(@p_pagoAlquiler_codigo,[pagoAlquiler_codigo]),
			[valorPorCobrar] = isnull(@p_valorPorCobrar,[valorPorCobrar]),
			[valorFacturado] = isnull(@p_valorFacturado,[valorFacturado]),
			[numeroFactura] = isnull(@p_numeroFactura,[numeroFactura]),
			[fechaEsperadaPago_ff] = isnull(@p_fechaEsperadaPago_ff,[fechaEsperadaPago_ff]),
			[fecha_factura_ff] = isnull(@p_fecha_factura_ff,[fecha_factura_ff]),
			[id_alquiler] = isnull(@p_id_alquiler,[id_alquiler])
		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_pagoAlquiler]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_equ_pagoAlquiler]
GO
CREATE PROCEDURE [ins_equ_pagoAlquiler]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_pagoAlquiler varchar(300),
		@p_pagoAlquiler_codigo varchar(100),
		@p_valorPorCobrar money,
		@p_valorFacturado money,
		@p_numeroFactura varchar(100),
		@p_fechaEsperadaPago_ff smalldatetime,
		@p_fecha_factura_ff smalldatetime,
		@p_id_alquiler int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [equ_pagoAlquiler]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[pagoAlquiler],
			[pagoAlquiler_codigo],
			[valorPorCobrar],
			[valorFacturado],
			[numeroFactura],
			[fechaEsperadaPago_ff],
			[fecha_factura_ff],
			[id_alquiler])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_pagoAlquiler,
			@p_pagoAlquiler_codigo,
			@p_valorPorCobrar,
			@p_valorFacturado,
			@p_numeroFactura,
			@p_fechaEsperadaPago_ff,
			@p_fecha_factura_ff,
			@p_id_alquiler)
		SET @v_id = scope_identity()
UPDATE [equ_pagoAlquiler] 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_pagoAlquiler]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_equ_pagoAlquiler]
GO
CREATE PROCEDURE [act_equ_pagoAlquiler]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [equ_pagoAlquiler]
	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_pagoAlquiler_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_equ_pagoAlquiler_m]
GO
CREATE PROCEDURE [reg_equ_pagoAlquiler_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],
		[pagoAlquiler] AS [pagoAlquiler],
		[pagoAlquiler_codigo] AS [pagoAlquiler_codigo],
		[valorPorCobrar] AS [valorPorCobrar],
		[valorFacturado] AS [valorFacturado],
		[numeroFactura] AS [numeroFactura],
		[fechaEsperadaPago_ff] AS [fechaEsperadaPago_ff],
		[fecha_factura_ff] AS [fecha_factura_ff],
		[id_alquiler] AS [id_alquiler]
	FROM [equ_pagoAlquiler]
	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_pagoAlquileresXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_pagoAlquileresXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_equ_pagoAlquileresXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_pagoAlquiler]
	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_pagoAlquileresXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_pagoAlquileresXusuario_creo_m]
GO
CREATE PROCEDURE [sel_equ_pagoAlquileresXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_pagoAlquiler]
	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_pagoAlquileresXalquiler_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_pagoAlquileresXalquiler_m]
GO
CREATE PROCEDURE [sel_equ_pagoAlquileresXalquiler_m]
	@p_id_alquiler INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_pagoAlquiler]
	WHERE ([id_alquiler]= @p_id_alquiler)
	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_pagoAlquileres_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_pagoAlquileres_m]
GO
CREATE PROCEDURE [sel_equ_pagoAlquileres_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_pagoAlquiler]
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_pagoAlquileres_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_equ_pagoAlquileres_m]
GO
CREATE PROCEDURE [lis_equ_pagoAlquileres_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[pagoAlquiler] AS [equ_pagoAlquiler]
	FROM [equ_pagoAlquiler]
	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_pagoAlquiler]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_equ_pagoAlquiler]
GO
CREATE PROCEDURE [aud_equ_pagoAlquiler]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [equ_pagoAlquiler].id as id_tabla,
	[equ_pagoAlquiler].id_usuario_modifico as id_usuario_modifico,
	[equ_pagoAlquiler].id_usuario_creo as id_usuario_creo,
	[equ_pagoAlquiler].fechaModificacion as fechaModificacion,
	[equ_pagoAlquiler].fechaCreacion as fechaCreacion,
	[equ_pagoAlquiler].active as active,
	[equ_pagoAlquiler].[pagoAlquiler] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [equ_pagoAlquiler]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[equ_pagoAlquiler].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[equ_pagoAlquiler].[id_usuario_creo]
	where ' + @p_filtro)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'equ_pagoAlquiler'
--------------------------------
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_trazabilidad]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_equ_trazabilidad]
GO
CREATE PROCEDURE [reg_equ_trazabilidad]
	@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],
		[trazabilidad] AS [trazabilidad],
		[trazabilidad_codigo] AS [trazabilidad_codigo],
		[horometro] AS [horometro],
		[fecha_ff] AS [fecha_ff],
		[esSoporte] AS [esSoporte],
		[id_equipo] AS [id_equipo],
		[id_equipo_padre] AS [id_equipo_padre],
		[id_tercero] AS [id_tercero],
		[id_sucursal] AS [id_sucursal],
		[id_alquiler] AS [id_alquiler],
		[id_prestamo] AS [id_prestamo],
		[id_reporteTecnico] AS [id_reporteTecnico],
		[id_evento] AS [id_evento],
		[id_documento] AS [id_documento],
		[id_tipoTrazabilidad] AS [id_tipoTrazabilidad]
	FROM [equ_trazabilidad]
	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_trazabilidad]')) DROP VIEW [dbo].[view_equ_trazabilidad]
GO
CREATE  VIEW [view_equ_trazabilidad] AS
SELECT 	[equ_trazabilidad].[id] AS [id],
		[equ_trazabilidad].[uid] AS [uid],
		[equ_trazabilidad].[eid] AS [eid],
		[equ_trazabilidad].[id_usuario_modifico] AS [id_usuario_modifico],
		[equ_trazabilidad].[id_usuario_creo] AS [id_usuario_creo],
		[equ_trazabilidad].[fechaModificacion] AS [fechaModificacion],
		[equ_trazabilidad].[fechaCreacion] AS [fechaCreacion],
		[equ_trazabilidad].[active] AS [active],
		[equ_trazabilidad].[trazabilidad] AS [trazabilidad],
		[equ_trazabilidad].[trazabilidad_codigo] AS [trazabilidad_codigo],
		[equ_trazabilidad].[horometro] AS [horometro],
		[equ_trazabilidad].[fecha_ff] AS [fecha_ff],
		[equ_trazabilidad].[esSoporte] AS [esSoporte],
		[equ_trazabilidad].[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_trazabilidad].[id_equipo_padre] AS [id_equipo_padre],
[equ_equipo_padre].[equipo] as [equ_equipo_padre_equipo],
[equ_equipo_padre].[equipo_codigo] as [equ_equipo_padre_equipo_codigo],
[equ_equipo_padre].[equipo_serial] as [equ_equipo_padre_equipo_serial],
[equ_equipo_padre].[nombreContacto] as [equ_equipo_padre_nombreContacto],
[equ_equipo_padre].[telefonoContacto] as [equ_equipo_padre_telefonoContacto],
[equ_equipo_padre].[ubicacion] as [equ_equipo_padre_ubicacion],
[equ_equipo_padre].[observaciones] as [equ_equipo_padre_observaciones],
[equ_equipo_padre].[fechaPuestaMarcha_fh] as [equ_equipo_padre_fechaPuestaMarcha_fh],
[equ_equipo_padre].[vencimientoGarantiaFabrica_ff] as [equ_equipo_padre_vencimientoGarantiaFabrica_ff],
[equ_equipo_padre].[vencimientoGarantiaDistribuidor_ff] as [equ_equipo_padre_vencimientoGarantiaDistribuidor_ff],
[equ_equipo_padre].[horasGarantia] as [equ_equipo_padre_horasGarantia],
[equ_equipo_padre].[ultimaLectura_fh] as [equ_equipo_padre_ultimaLectura_fh],
[equ_equipo_padre].[horometroActual] as [equ_equipo_padre_horometroActual],
[equ_equipo_padre].[promedioHoras] as [equ_equipo_padre_promedioHoras],
[equ_equipo_padre].[costo] as [equ_equipo_padre_costo],
[equ_equipo_padre].[fechaImportacion_ff] as [equ_equipo_padre_fechaImportacion_ff],
[equ_equipo_padre].[garantiaCancelada] as [equ_equipo_padre_garantiaCancelada],
[equ_equipo_padre].[id_catalogo.equipo] as [equ_equipo_padre_id_catalogo.equipo],
[equ_equipo_padre].[id_sistema] as [equ_equipo_padre_id_sistema],
[equ_equipo_padre].[id_sucursal] as [equ_equipo_padre_id_sucursal],
[equ_equipo_padre].[id_zona] as [equ_equipo_padre_id_zona],
[equ_equipo_padre].[id_versionEquipo] as [equ_equipo_padre_id_versionEquipo],
[equ_equipo_padre].[id_tercero_comprador] as [equ_equipo_padre_id_tercero_comprador],
[equ_equipo_padre].[id_tercero] as [equ_equipo_padre_id_tercero],
[equ_equipo_padre].[id_tercero_servicio] as [equ_equipo_padre_id_tercero_servicio],
[equ_equipo_padre].[id_equipo] as [equ_equipo_padre_id_equipo],
[equ_equipo_padre].[id_estadoEquipo] as [equ_equipo_padre_id_estadoEquipo],
		[equ_trazabilidad].[id_tercero] AS [id_tercero],
[ter_tercero].[tercero] as [ter_tercero_tercero],
[ter_tercero].[tercero_codigo] as [ter_tercero_tercero_codigo],
[ter_tercero].[tercero_nit] as [ter_tercero_tercero_nit],
[ter_tercero].[direccion] as [ter_tercero_direccion],
[ter_tercero].[contacto] as [ter_tercero_contacto],
[ter_tercero].[telefono] as [ter_tercero_telefono],
[ter_tercero].[fax] as [ter_tercero_fax],
[ter_tercero].[email] as [ter_tercero_email],
[ter_tercero].[website] as [ter_tercero_website],
[ter_tercero].[representante] as [ter_tercero_representante],
[ter_tercero].[notas] as [ter_tercero_notas],
[ter_tercero].[tercero_nombreCorto] as [ter_tercero_tercero_nombreCorto],
[ter_tercero].[esCliente] as [ter_tercero_esCliente],
[ter_tercero].[esProveedor] as [ter_tercero_esProveedor],
[ter_tercero].[esTransportador] as [ter_tercero_esTransportador],
[ter_tercero].[esFabricante] as [ter_tercero_esFabricante],
[ter_tercero].[esEmpresaPropia] as [ter_tercero_esEmpresaPropia],
[ter_tercero].[clasificacion] as [ter_tercero_clasificacion],
[ter_tercero].[id_zona] as [ter_tercero_id_zona],
[ter_tercero].[id_estadoTercero] as [ter_tercero_id_estadoTercero],
[ter_tercero].[id_listaPrecio] as [ter_tercero_id_listaPrecio],
[ter_tercero].[id_naturalezaTercero] as [ter_tercero_id_naturalezaTercero],
		[equ_trazabilidad].[id_sucursal] AS [id_sucursal],
[ter_sucursal].[sucursal] as [ter_sucursal_sucursal],
[ter_sucursal].[sucursal_codigo] as [ter_sucursal_sucursal_codigo],
[ter_sucursal].[contacto] as [ter_sucursal_contacto],
[ter_sucursal].[telefono] as [ter_sucursal_telefono],
[ter_sucursal].[Fax] as [ter_sucursal_Fax],
[ter_sucursal].[Email] as [ter_sucursal_Email],
[ter_sucursal].[Direccion] as [ter_sucursal_Direccion],
[ter_sucursal].[paraVenta] as [ter_sucursal_paraVenta],
[ter_sucursal].[paraSoporte] as [ter_sucursal_paraSoporte],
[ter_sucursal].[paraAlquiler] as [ter_sucursal_paraAlquiler],
[ter_sucursal].[id_tercero] as [ter_sucursal_id_tercero],
[ter_sucursal].[id_zona] as [ter_sucursal_id_zona],
[ter_sucursal].[id_usuario_asesor] as [ter_sucursal_id_usuario_asesor],
[ter_sucursal].[id_usuario_tecnico] as [ter_sucursal_id_usuario_tecnico],
[ter_sucursal].[id_sucursal] as [ter_sucursal_id_sucursal],
[ter_sucursal].[id_listaPrecio] as [ter_sucursal_id_listaPrecio],
[ter_sucursal].[id_estadoTercero] as [ter_sucursal_id_estadoTercero],
		[equ_trazabilidad].[id_alquiler] AS [id_alquiler],
[equ_alquiler].[alquiler] as [equ_alquiler_alquiler],
[equ_alquiler].[alquiler_codigo] as [equ_alquiler_alquiler_codigo],
[equ_alquiler].[fechaCompromiso_ff] as [equ_alquiler_fechaCompromiso_ff],
[equ_alquiler].[id_documento.ot] as [equ_alquiler_id_documento.ot],
[equ_alquiler].[id_sucursal] as [equ_alquiler_id_sucursal],
[equ_alquiler].[id_tercero] as [equ_alquiler_id_tercero],
[equ_alquiler].[fechaFin_ff] as [equ_alquiler_fechaFin_ff],
[equ_alquiler].[id_subtipoDocumento] as [equ_alquiler_id_subtipoDocumento],
[equ_alquiler].[mesVencido] as [equ_alquiler_mesVencido],
[equ_alquiler].[alquiler_numero] as [equ_alquiler_alquiler_numero],
		[equ_trazabilidad].[id_prestamo] AS [id_prestamo],
[equ_prestamo].[prestamo] as [equ_prestamo_prestamo],
[equ_prestamo].[prestamo_codigo] as [equ_prestamo_prestamo_codigo],
[equ_prestamo].[fechaPrestamo_ff] as [equ_prestamo_fechaPrestamo_ff],
[equ_prestamo].[fechaCompromiso_ff] as [equ_prestamo_fechaCompromiso_ff],
[equ_prestamo].[notas] as [equ_prestamo_notas],
[equ_prestamo].[id_estadoPrestamo] as [equ_prestamo_id_estadoPrestamo],
[equ_prestamo].[id_usuario_entrego] as [equ_prestamo_id_usuario_entrego],
		[equ_trazabilidad].[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_documento.ot] as [ort_reporteTecnico_id_documento.ot],
[ort_reporteTecnico].[id_gasto] as [ort_reporteTecnico_id_gasto],
		[equ_trazabilidad].[id_evento] AS [id_evento],
[dis_evento].[evento] as [dis_evento_evento],
[dis_evento].[evento_codigo] as [dis_evento_evento_codigo],
[dis_evento].[fechaHoraInicio_ff] as [dis_evento_fechaHoraInicio_ff],
[dis_evento].[duracion_hh] as [dis_evento_duracion_hh],
[dis_evento].[afectaProduccion] as [dis_evento_afectaProduccion],
[dis_evento].[notas] as [dis_evento_notas],
[dis_evento].[id_equipo] as [dis_evento_id_equipo],
[dis_evento].[id_motivoEvento] as [dis_evento_id_motivoEvento],
[dis_evento].[id_usuario] as [dis_evento_id_usuario],
		[equ_trazabilidad].[id_documento] AS [id_documento],
[doc_documento].[documento] as [doc_documento_documento],
[doc_documento].[documento_codigo] as [doc_documento_documento_codigo],
[doc_documento].[documento_numero] as [doc_documento_documento_numero],
[doc_documento].[prefijo] as [doc_documento_prefijo],
[doc_documento].[subtotal] as [doc_documento_subtotal],
[doc_documento].[descuento] as [doc_documento_descuento],
[doc_documento].[iva] as [doc_documento_iva],
[doc_documento].[total] as [doc_documento_total],
[doc_documento].[costo] as [doc_documento_costo],
[doc_documento].[rentabilidad] as [doc_documento_rentabilidad],
[doc_documento].[trm] as [doc_documento_trm],
[doc_documento].[fecha_fh] as [doc_documento_fecha_fh],
[doc_documento].[fechaSugerida_fh] as [doc_documento_fechaSugerida_fh],
[doc_documento].[fechaCierre_ff] as [doc_documento_fechaCierre_ff],
[doc_documento].[fechaLimite_fh] as [doc_documento_fechaLimite_fh],
[doc_documento].[id_subtipoDocumento] as [doc_documento_id_subtipoDocumento],
[doc_documento].[id_tercero_cliente] as [doc_documento_id_tercero_cliente],
[doc_documento].[id_documento] as [doc_documento_id_documento],
[doc_documento].[id_tercero_proveedor] as [doc_documento_id_tercero_proveedor],
[doc_documento].[id_prioridadDocumento] as [doc_documento_id_prioridadDocumento],
[doc_documento].[id_moneda] as [doc_documento_id_moneda],
[doc_documento].[id_estadoTipoDocumento] as [doc_documento_id_estadoTipoDocumento],
[doc_documento].[id_motivoestado] as [doc_documento_id_motivoestado],
[doc_documento].[subtotalPlaneado] as [doc_documento_subtotalPlaneado],
[doc_documento].[descuentoPlaneado] as [doc_documento_descuentoPlaneado],
[doc_documento].[ivaPlaneado] as [doc_documento_ivaPlaneado],
[doc_documento].[totalPlaneado] as [doc_documento_totalPlaneado],
[doc_documento].[costoPlaneado] as [doc_documento_costoPlaneado],
		[equ_trazabilidad].[id_tipoTrazabilidad] AS [id_tipoTrazabilidad],
[equ_tipoTrazabilidad].[tipoTrazabilidad] as [equ_tipoTrazabilidad_tipoTrazabilidad],
[equ_tipoTrazabilidad].[tipoTrazabilidad_codigo] as [equ_tipoTrazabilidad_tipoTrazabilidad_codigo],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [equ_trazabilidad]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[equ_trazabilidad].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[equ_trazabilidad].[id_usuario_creo]
	INNER JOIN [equ_equipo] ON [equ_equipo].id=[equ_trazabilidad].[id_equipo]
	INNER JOIN [equ_equipo] AS [equ_equipo_padre] ON [equ_equipo_padre].id=[equ_trazabilidad].[id_equipo_padre]
	INNER JOIN [ter_tercero] ON [ter_tercero].id=[equ_trazabilidad].[id_tercero]
	INNER JOIN [ter_sucursal] ON [ter_sucursal].id=[equ_trazabilidad].[id_sucursal]
	INNER JOIN [equ_alquiler] ON [equ_alquiler].id=[equ_trazabilidad].[id_alquiler]
	INNER JOIN [equ_prestamo] ON [equ_prestamo].id=[equ_trazabilidad].[id_prestamo]
	INNER JOIN [ort_reporteTecnico] ON [ort_reporteTecnico].id=[equ_trazabilidad].[id_reporteTecnico]
	INNER JOIN [dis_evento] ON [dis_evento].id=[equ_trazabilidad].[id_evento]
	INNER JOIN [doc_documento] ON [doc_documento].id=[equ_trazabilidad].[id_documento]
	INNER JOIN [equ_tipoTrazabilidad] ON [equ_tipoTrazabilidad].id=[equ_trazabilidad].[id_tipoTrazabilidad]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [equ_trazabilidad].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [equ_trazabilidad].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_trazabilidadesXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_trazabilidadesXusuario_modifico]
GO
CREATE PROCEDURE [sel_equ_trazabilidadesXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_trazabilidad]
	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_trazabilidadesXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_trazabilidadesXusuario_creo]
GO
CREATE PROCEDURE [sel_equ_trazabilidadesXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_trazabilidad]
	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_trazabilidadesXequipo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_trazabilidadesXequipo]
GO
CREATE PROCEDURE [sel_equ_trazabilidadesXequipo]
	@p_id_equipo INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_trazabilidad]
	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_trazabilidadesXequipo_padre]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_trazabilidadesXequipo_padre]
GO
CREATE PROCEDURE [sel_equ_trazabilidadesXequipo_padre]
	@p_id_equipo_padre INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_trazabilidad]
	WHERE ([id_equipo_padre]= @p_id_equipo_padre)
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_trazabilidadesXtercero]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_trazabilidadesXtercero]
GO
CREATE PROCEDURE [sel_equ_trazabilidadesXtercero]
	@p_id_tercero INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_trazabilidad]
	WHERE ([id_tercero]= @p_id_tercero)
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_trazabilidadesXsucursal]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_trazabilidadesXsucursal]
GO
CREATE PROCEDURE [sel_equ_trazabilidadesXsucursal]
	@p_id_sucursal INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_trazabilidad]
	WHERE ([id_sucursal]= @p_id_sucursal)
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_trazabilidadesXalquiler]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_trazabilidadesXalquiler]
GO
CREATE PROCEDURE [sel_equ_trazabilidadesXalquiler]
	@p_id_alquiler INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_trazabilidad]
	WHERE ([id_alquiler]= @p_id_alquiler)
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_trazabilidadesXprestamo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_trazabilidadesXprestamo]
GO
CREATE PROCEDURE [sel_equ_trazabilidadesXprestamo]
	@p_id_prestamo INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_trazabilidad]
	WHERE ([id_prestamo]= @p_id_prestamo)
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_trazabilidadesXreporteTecnico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_trazabilidadesXreporteTecnico]
GO
CREATE PROCEDURE [sel_equ_trazabilidadesXreporteTecnico]
	@p_id_reporteTecnico INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_trazabilidad]
	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_equ_trazabilidadesXevento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_trazabilidadesXevento]
GO
CREATE PROCEDURE [sel_equ_trazabilidadesXevento]
	@p_id_evento INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_trazabilidad]
	WHERE ([id_evento]= @p_id_evento)
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_trazabilidadesXdocumento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_trazabilidadesXdocumento]
GO
CREATE PROCEDURE [sel_equ_trazabilidadesXdocumento]
	@p_id_documento INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_trazabilidad]
	WHERE ([id_documento]= @p_id_documento)
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_trazabilidadesXtipoTrazabilidad]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_trazabilidadesXtipoTrazabilidad]
GO
CREATE PROCEDURE [sel_equ_trazabilidadesXtipoTrazabilidad]
	@p_id_tipoTrazabilidad INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_trazabilidad]
	WHERE ([id_tipoTrazabilidad]= @p_id_tipoTrazabilidad)
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_trazabilidades]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_trazabilidades]
GO
CREATE PROCEDURE [sel_equ_trazabilidades]
	
	@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_trazabilidad]
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_trazabilidades]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_equ_trazabilidades]
GO
CREATE PROCEDURE [lis_equ_trazabilidades]
	AS
BEGIN
	SELECT id AS id,
[trazabilidad] AS [equ_trazabilidad]
	FROM [equ_trazabilidad]
	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_trazabilidad]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_equ_trazabilidad]
GO
CREATE PROCEDURE [upd_equ_trazabilidad]
	@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_trazabilidad varchar(300) = null,
		@p_trazabilidad_codigo varchar(100) = null,
		@p_horometro float = null,
		@p_fecha_ff smalldatetime = null,
		@p_esSoporte bit = null,
		@p_id_equipo int = null,
		@p_id_equipo_padre int = null,
		@p_id_tercero int = null,
		@p_id_sucursal int = null,
		@p_id_alquiler int = null,
		@p_id_prestamo int = null,
		@p_id_reporteTecnico int = null,
		@p_id_evento int = null,
		@p_id_documento int = null,
		@p_id_tipoTrazabilidad int = null
AS
BEGIN
		UPDATE [equ_trazabilidad]
		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(),
			[trazabilidad] = isnull(@p_trazabilidad,[trazabilidad]),
			[trazabilidad_codigo] = isnull(@p_trazabilidad_codigo,[trazabilidad_codigo]),
			[horometro] = isnull(@p_horometro,[horometro]),
			[fecha_ff] = isnull(@p_fecha_ff,[fecha_ff]),
			[esSoporte] = isnull(@p_esSoporte,[esSoporte]),
			[id_equipo] = isnull(@p_id_equipo,[id_equipo]),
			[id_equipo_padre] = isnull(@p_id_equipo_padre,[id_equipo_padre]),
			[id_tercero] = isnull(@p_id_tercero,[id_tercero]),
			[id_sucursal] = isnull(@p_id_sucursal,[id_sucursal]),
			[id_alquiler] = isnull(@p_id_alquiler,[id_alquiler]),
			[id_prestamo] = isnull(@p_id_prestamo,[id_prestamo]),
			[id_reporteTecnico] = isnull(@p_id_reporteTecnico,[id_reporteTecnico]),
			[id_evento] = isnull(@p_id_evento,[id_evento]),
			[id_documento] = isnull(@p_id_documento,[id_documento]),
			[id_tipoTrazabilidad] = isnull(@p_id_tipoTrazabilidad,[id_tipoTrazabilidad])
		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_trazabilidad]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_equ_trazabilidad]
GO
CREATE PROCEDURE [ins_equ_trazabilidad]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_trazabilidad varchar(300),
		@p_trazabilidad_codigo varchar(100),
		@p_horometro float,
		@p_fecha_ff smalldatetime,
		@p_esSoporte bit,
		@p_id_equipo int,
		@p_id_equipo_padre int,
		@p_id_tercero int,
		@p_id_sucursal int,
		@p_id_alquiler int,
		@p_id_prestamo int,
		@p_id_reporteTecnico int,
		@p_id_evento int,
		@p_id_documento int,
		@p_id_tipoTrazabilidad int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [equ_trazabilidad]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[trazabilidad],
			[trazabilidad_codigo],
			[horometro],
			[fecha_ff],
			[esSoporte],
			[id_equipo],
			[id_equipo_padre],
			[id_tercero],
			[id_sucursal],
			[id_alquiler],
			[id_prestamo],
			[id_reporteTecnico],
			[id_evento],
			[id_documento],
			[id_tipoTrazabilidad])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_trazabilidad,
			@p_trazabilidad_codigo,
			@p_horometro,
			@p_fecha_ff,
			@p_esSoporte,
			@p_id_equipo,
			@p_id_equipo_padre,
			@p_id_tercero,
			@p_id_sucursal,
			@p_id_alquiler,
			@p_id_prestamo,
			@p_id_reporteTecnico,
			@p_id_evento,
			@p_id_documento,
			@p_id_tipoTrazabilidad)
		SET @v_id = scope_identity()
UPDATE [equ_trazabilidad] 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_trazabilidad]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_equ_trazabilidad]
GO
CREATE PROCEDURE [act_equ_trazabilidad]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [equ_trazabilidad]
	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_trazabilidad_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_equ_trazabilidad_m]
GO
CREATE PROCEDURE [reg_equ_trazabilidad_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],
		[trazabilidad] AS [trazabilidad],
		[trazabilidad_codigo] AS [trazabilidad_codigo],
		[horometro] AS [horometro],
		[fecha_ff] AS [fecha_ff],
		[esSoporte] AS [esSoporte],
		[id_equipo] AS [id_equipo],
		[id_equipo_padre] AS [id_equipo_padre],
		[id_tercero] AS [id_tercero],
		[id_sucursal] AS [id_sucursal],
		[id_alquiler] AS [id_alquiler],
		[id_prestamo] AS [id_prestamo],
		[id_reporteTecnico] AS [id_reporteTecnico],
		[id_evento] AS [id_evento],
		[id_documento] AS [id_documento],
		[id_tipoTrazabilidad] AS [id_tipoTrazabilidad]
	FROM [equ_trazabilidad]
	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_trazabilidadesXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_trazabilidadesXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_equ_trazabilidadesXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_trazabilidad]
	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_trazabilidadesXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_trazabilidadesXusuario_creo_m]
GO
CREATE PROCEDURE [sel_equ_trazabilidadesXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_trazabilidad]
	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_trazabilidadesXequipo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_trazabilidadesXequipo_m]
GO
CREATE PROCEDURE [sel_equ_trazabilidadesXequipo_m]
	@p_id_equipo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_trazabilidad]
	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_trazabilidadesXequipo_padre_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_trazabilidadesXequipo_padre_m]
GO
CREATE PROCEDURE [sel_equ_trazabilidadesXequipo_padre_m]
	@p_id_equipo_padre INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_trazabilidad]
	WHERE ([id_equipo_padre]= @p_id_equipo_padre)
	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_trazabilidadesXtercero_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_trazabilidadesXtercero_m]
GO
CREATE PROCEDURE [sel_equ_trazabilidadesXtercero_m]
	@p_id_tercero INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_trazabilidad]
	WHERE ([id_tercero]= @p_id_tercero)
	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_trazabilidadesXsucursal_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_trazabilidadesXsucursal_m]
GO
CREATE PROCEDURE [sel_equ_trazabilidadesXsucursal_m]
	@p_id_sucursal INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_trazabilidad]
	WHERE ([id_sucursal]= @p_id_sucursal)
	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_trazabilidadesXalquiler_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_trazabilidadesXalquiler_m]
GO
CREATE PROCEDURE [sel_equ_trazabilidadesXalquiler_m]
	@p_id_alquiler INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_trazabilidad]
	WHERE ([id_alquiler]= @p_id_alquiler)
	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_trazabilidadesXprestamo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_trazabilidadesXprestamo_m]
GO
CREATE PROCEDURE [sel_equ_trazabilidadesXprestamo_m]
	@p_id_prestamo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_trazabilidad]
	WHERE ([id_prestamo]= @p_id_prestamo)
	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_trazabilidadesXreporteTecnico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_trazabilidadesXreporteTecnico_m]
GO
CREATE PROCEDURE [sel_equ_trazabilidadesXreporteTecnico_m]
	@p_id_reporteTecnico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_trazabilidad]
	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_equ_trazabilidadesXevento_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_trazabilidadesXevento_m]
GO
CREATE PROCEDURE [sel_equ_trazabilidadesXevento_m]
	@p_id_evento INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_trazabilidad]
	WHERE ([id_evento]= @p_id_evento)
	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_trazabilidadesXdocumento_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_trazabilidadesXdocumento_m]
GO
CREATE PROCEDURE [sel_equ_trazabilidadesXdocumento_m]
	@p_id_documento INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_trazabilidad]
	WHERE ([id_documento]= @p_id_documento)
	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_trazabilidadesXtipoTrazabilidad_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_trazabilidadesXtipoTrazabilidad_m]
GO
CREATE PROCEDURE [sel_equ_trazabilidadesXtipoTrazabilidad_m]
	@p_id_tipoTrazabilidad INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_trazabilidad]
	WHERE ([id_tipoTrazabilidad]= @p_id_tipoTrazabilidad)
	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_trazabilidades_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_trazabilidades_m]
GO
CREATE PROCEDURE [sel_equ_trazabilidades_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_trazabilidad]
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_trazabilidades_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_equ_trazabilidades_m]
GO
CREATE PROCEDURE [lis_equ_trazabilidades_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[trazabilidad] AS [equ_trazabilidad]
	FROM [equ_trazabilidad]
	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_trazabilidad]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_equ_trazabilidad]
GO
CREATE PROCEDURE [aud_equ_trazabilidad]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [equ_trazabilidad].id as id_tabla,
	[equ_trazabilidad].id_usuario_modifico as id_usuario_modifico,
	[equ_trazabilidad].id_usuario_creo as id_usuario_creo,
	[equ_trazabilidad].fechaModificacion as fechaModificacion,
	[equ_trazabilidad].fechaCreacion as fechaCreacion,
	[equ_trazabilidad].active as active,
	[equ_trazabilidad].[trazabilidad] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

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