

if not exists(select * FROM INFORMATION_SCHEMA.COLUMNS  where Table_Name='equ_detallealquiler' and column_name = 'estadoalquiler' )
begin
alter table [equ_detallealquiler] add [estadoalquiler] int
end




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_documento.alquiler] AS [id_documento.alquiler],
		[id_sucursal] AS [id_sucursal],
		[id_tercero] AS [id_tercero],
		[fechaUltimaFactura_fh] AS [fechaUltimaFactura_fh],
		[valorMes] AS [valorMes],
		[fechaInicioFactura_fh] AS [fechaInicioFactura_fh],
		[cantidadSolicitada] AS [cantidadSolicitada],
		[cantidadAlquilada] AS [cantidadAlquilada],
		[id_catalogo] AS [id_catalogo],
		[estadoalquiler] AS [estadoalquiler]
	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_documento.alquiler] AS [id_documento.alquiler],
[doc_documento.alquiler].[documento.alquiler] as [doc_documento.alquiler_documento.alquiler],
[doc_documento.alquiler].[fechaFin_ff] as [doc_documento.alquiler_fechaFin_ff],
[doc_documento.alquiler].[mesVencido] as [doc_documento.alquiler_mesVencido],
[doc_documento.alquiler].[observaciones] as [doc_documento.alquiler_observaciones],
		[doc_documento_alquiler].[documento_codigo] AS [doc_documento_alquiler_documento_codigo],
		[doc_documento_alquiler].[documento_numero] AS [doc_documento_alquiler_documento_numero],
		[doc_documento_alquiler].[prefijo] AS [doc_documento_alquiler_prefijo],
		[doc_documento_alquiler].[subtotal] AS [doc_documento_alquiler_subtotal],
		[doc_documento_alquiler].[descuento] AS [doc_documento_alquiler_descuento],
		[doc_documento_alquiler].[iva] AS [doc_documento_alquiler_iva],
		[doc_documento_alquiler].[total] AS [doc_documento_alquiler_total],
		[doc_documento_alquiler].[costo] AS [doc_documento_alquiler_costo],
		[doc_documento_alquiler].[rentabilidad] AS [doc_documento_alquiler_rentabilidad],
		[doc_documento_alquiler].[trm] AS [doc_documento_alquiler_trm],
		[doc_documento_alquiler].[fecha_fh] AS [doc_documento_alquiler_fecha_fh],
		[doc_documento_alquiler].[fechaSugerida_fh] AS [doc_documento_alquiler_fechaSugerida_fh],
		[doc_documento_alquiler].[fechaCierre_ff] AS [doc_documento_alquiler_fechaCierre_ff],
		[doc_documento_alquiler].[fechaLimite_fh] AS [doc_documento_alquiler_fechaLimite_fh],
		[doc_documento_alquiler].[id_subtipoDocumento] AS [doc_documento_alquiler_id_subtipoDocumento],
		[doc_documento_alquiler].[id_tercero_cliente] AS [doc_documento_alquiler_id_tercero_cliente],
		[doc_documento_alquiler].[id_documento] AS [doc_documento_alquiler_id_documento],
		[doc_documento_alquiler].[id_tercero_proveedor] AS [doc_documento_alquiler_id_tercero_proveedor],
		[doc_documento_alquiler].[id_prioridadDocumento] AS [doc_documento_alquiler_id_prioridadDocumento],
		[doc_documento_alquiler].[id_moneda] AS [doc_documento_alquiler_id_moneda],
		[doc_documento_alquiler].[id_estadoTipoDocumento] AS [doc_documento_alquiler_id_estadoTipoDocumento],
		[doc_documento_alquiler].[id_motivoestado] AS [doc_documento_alquiler_id_motivoestado],
		[doc_documento_alquiler].[subtotalPlaneado] AS [doc_documento_alquiler_subtotalPlaneado],
		[doc_documento_alquiler].[descuentoPlaneado] AS [doc_documento_alquiler_descuentoPlaneado],
		[doc_documento_alquiler].[ivaPlaneado] AS [doc_documento_alquiler_ivaPlaneado],
		[doc_documento_alquiler].[totalPlaneado] AS [doc_documento_alquiler_totalPlaneado],
		[doc_documento_alquiler].[costoPlaneado] AS [doc_documento_alquiler_costoPlaneado],
		[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],
[ter_sucursal].[codigorecursiva] as [ter_sucursal_codigorecursiva],
		[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],
[ter_tercero].[esProspecto] as [ter_tercero_esProspecto],
		[equ_detalleAlquiler].[fechaUltimaFactura_fh] AS [fechaUltimaFactura_fh],
		[equ_detalleAlquiler].[valorMes] AS [valorMes],
		[equ_detalleAlquiler].[fechaInicioFactura_fh] AS [fechaInicioFactura_fh],
		[equ_detalleAlquiler].[cantidadSolicitada] AS [cantidadSolicitada],
		[equ_detalleAlquiler].[cantidadAlquilada] AS [cantidadAlquilada],
		[equ_detalleAlquiler].[id_catalogo] AS [id_catalogo],
[cat_catalogo].[catalogo] as [cat_catalogo_catalogo],
[cat_catalogo].[catalogo_codigo] as [cat_catalogo_catalogo_codigo],
[cat_catalogo].[codigoInventario] as [cat_catalogo_codigoInventario],
[cat_catalogo].[precioVenta] as [cat_catalogo_precioVenta],
[cat_catalogo].[tiempoConsecucion] as [cat_catalogo_tiempoConsecucion],
[cat_catalogo].[esNacional] as [cat_catalogo_esNacional],
[cat_catalogo].[id_familia] as [cat_catalogo_id_familia],
[cat_catalogo].[id_subTipoCatalogo] as [cat_catalogo_id_subTipoCatalogo],
[cat_catalogo].[id_unidad] as [cat_catalogo_id_unidad],
[cat_catalogo].[id_impuesto] as [cat_catalogo_id_impuesto],
[cat_catalogo].[descripcion] as [cat_catalogo_descripcion],
[cat_catalogo].[bloquearCostear] as [cat_catalogo_bloquearCostear],
		[equ_detalleAlquiler].[estadoalquiler] AS [estadoalquiler],
		[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 [doc_documento.alquiler] ON [doc_documento.alquiler].id=[equ_detalleAlquiler].[id_documento.alquiler]
	INNER JOIN [doc_documento] aS [doc_documento_alquiler] ON [doc_documento.alquiler].id = [doc_documento_alquiler].id
	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 [cat_catalogo] ON [cat_catalogo].id=[equ_detalleAlquiler].[id_catalogo]
	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_detalleAlquileresXdocumento_alquiler]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_detalleAlquileresXdocumento_alquiler]
GO
CREATE PROCEDURE [sel_equ_detalleAlquileresXdocumento_alquiler]
	@p_id_documento_alquiler INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_detalleAlquiler]
	WHERE ([id_documento.alquiler]= @p_id_documento_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_detalleAlquileresXcatalogo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_detalleAlquileresXcatalogo]
GO
CREATE PROCEDURE [sel_equ_detalleAlquileresXcatalogo]
	@p_id_catalogo INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_detalleAlquiler]
	WHERE ([id_catalogo]= @p_id_catalogo)
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_documento_alquiler int = null,
		@p_id_sucursal int = null,
		@p_id_tercero int = null,
		@p_fechaUltimaFactura_fh smalldatetime = null,
		@p_valorMes float = null,
		@p_fechaInicioFactura_fh smalldatetime = null,
		@p_cantidadSolicitada int = null,
		@p_cantidadAlquilada int = null,
		@p_id_catalogo int = null,
		@p_estadoalquiler int = 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_documento.alquiler] = isnull(@p_id_documento_alquiler,[id_documento.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]),
			[valorMes] = isnull(@p_valorMes,[valorMes]),
			[fechaInicioFactura_fh] = isnull(@p_fechaInicioFactura_fh,[fechaInicioFactura_fh]),
			[cantidadSolicitada] = isnull(@p_cantidadSolicitada,[cantidadSolicitada]),
			[cantidadAlquilada] = isnull(@p_cantidadAlquilada,[cantidadAlquilada]),
			[id_catalogo] = isnull(@p_id_catalogo,[id_catalogo]),
			[estadoalquiler] = isnull(@p_estadoalquiler,[estadoalquiler])
		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_documento_alquiler int,
		@p_id_sucursal int,
		@p_id_tercero int,
		@p_fechaUltimaFactura_fh smalldatetime,
		@p_valorMes float,
		@p_fechaInicioFactura_fh smalldatetime,
		@p_cantidadSolicitada int,
		@p_cantidadAlquilada int,
		@p_id_catalogo int,
		@p_estadoalquiler int
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_documento.alquiler],
			[id_sucursal],
			[id_tercero],
			[fechaUltimaFactura_fh],
			[valorMes],
			[fechaInicioFactura_fh],
			[cantidadSolicitada],
			[cantidadAlquilada],
			[id_catalogo],
			[estadoalquiler])
		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_documento_alquiler,
			@p_id_sucursal,
			@p_id_tercero,
			@p_fechaUltimaFactura_fh,
			@p_valorMes,
			@p_fechaInicioFactura_fh,
			@p_cantidadSolicitada,
			@p_cantidadAlquilada,
			@p_id_catalogo,
			@p_estadoalquiler)
		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_documento.alquiler] AS [id_documento.alquiler],
		[id_sucursal] AS [id_sucursal],
		[id_tercero] AS [id_tercero],
		[fechaUltimaFactura_fh] AS [fechaUltimaFactura_fh],
		[valorMes] AS [valorMes],
		[fechaInicioFactura_fh] AS [fechaInicioFactura_fh],
		[cantidadSolicitada] AS [cantidadSolicitada],
		[cantidadAlquilada] AS [cantidadAlquilada],
		[id_catalogo] AS [id_catalogo],
		[estadoalquiler] AS [estadoalquiler]
	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_detalleAlquileresXdocumento_alquiler_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_detalleAlquileresXdocumento_alquiler_m]
GO
CREATE PROCEDURE [sel_equ_detalleAlquileresXdocumento_alquiler_m]
	@p_id_documento_alquiler INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_detalleAlquiler]
	WHERE ([id_documento.alquiler]= @p_id_documento_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_detalleAlquileresXcatalogo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_detalleAlquileresXcatalogo_m]
GO
CREATE PROCEDURE [sel_equ_detalleAlquileresXcatalogo_m]
	@p_id_catalogo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_detalleAlquiler]
	WHERE ([id_catalogo]= @p_id_catalogo)
	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'
-----------------------------------16/12/2015 10:45:05