
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]') 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] AS [id_documento],
		[id_sucursal] AS [id_sucursal],
		[id_tercero] AS [id_tercero],
		[id_subtipoDocumento] AS [id_subtipoDocumento],
		[fechaFin_ff] AS [fechaFin_ff],
		[mesVencido] AS [mesVencido],
		[alquiler_numero] AS [alquiler_numero],
		[observaciones] AS [observaciones]
	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 [dbo].[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] 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_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],
[ter_tercero].[esProspecto] as [ter_tercero_esProspecto],
		[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].[fechaFin_ff] AS [fechaFin_ff],
		[equ_alquiler].[mesVencido] AS [mesVencido],
		[equ_alquiler].[alquiler_numero] AS [alquiler_numero],
		[equ_alquiler].[observaciones] AS [observaciones],
		[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] ON [doc_documento].id=[equ_alquiler].[id_documento]
	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]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_alquileresXdocumento]
GO
CREATE PROCEDURE [sel_equ_alquileresXdocumento]
	@p_id_documento INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_alquiler]
	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_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 int = null,
		@p_id_sucursal int = null,
		@p_id_tercero int = null,
		@p_id_subtipoDocumento int = null,
		@p_fechaFin_ff smalldatetime = null,
		@p_mesVencido bit = null,
		@p_alquiler_numero int = null,
		@p_observaciones varchar(500) = 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] = isnull(@p_id_documento,[id_documento]),
			[id_sucursal] = isnull(@p_id_sucursal,[id_sucursal]),
			[id_tercero] = isnull(@p_id_tercero,[id_tercero]),
			[id_subtipoDocumento] = isnull(@p_id_subtipoDocumento,[id_subtipoDocumento]),
			[fechaFin_ff] = isnull(@p_fechaFin_ff,[fechaFin_ff]),
			[mesVencido] = isnull(@p_mesVencido,[mesVencido]),
			[alquiler_numero] = isnull(@p_alquiler_numero,[alquiler_numero]),
			[observaciones] = isnull(@p_observaciones,[observaciones])
		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 [dbo].[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 int,
		@p_id_sucursal int,
		@p_id_tercero int,
		@p_fechaFin_ff smalldatetime,
		@p_id_subtipoDocumento int,
		@p_mesVencido bit,
		@p_alquiler_numero int,
		@p_observaciones varchar(500)
		
AS
BEGIN
---El tipo de documento para el alquiler, siempre debe ser 9

declare @v_idtipo int 
set @v_idtipo= 9

select @p_alquiler_numero=max(consecutivo) 
from doc_subtipodocumento
where prefijo = (select top 1 prefijo from doc_subtipoDocumento where id_tipoDocumento=9) and active = 1
and id_tipodocumento = @v_idtipo

	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],
			[id_sucursal],
			[id_tercero],
			[fechaFin_ff],
			[id_subtipoDocumento],
			[mesVencido],
			[alquiler_numero],
			[observaciones])
		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,
			@p_id_sucursal,
			@p_id_tercero,
			@p_fechaFin_ff,
			@p_id_subtipoDocumento,
			@p_mesVencido,
			@p_alquiler_numero,
			@p_observaciones)
		SET @v_id = scope_identity()
UPDATE [equ_alquiler] set [uid] = [uid] + '_' + convert(varchar(20),@v_id) where [id]=@v_id
UPDATE doc_subtipodocumento
set consecutivo=@p_alquiler_numero+1 
where id_tipodocumento= @v_idtipo and prefijo = (select top 1 prefijo from doc_subtipoDocumento where id_tipoDocumento=9)
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] AS [id_documento],
		[id_sucursal] AS [id_sucursal],
		[id_tercero] AS [id_tercero],
		[id_subtipoDocumento] AS [id_subtipoDocumento],
		[fechaFin_ff] AS [fechaFin_ff],
		[mesVencido] AS [mesVencido],
		[alquiler_numero] AS [alquiler_numero],
		[observaciones] AS [observaciones]
	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_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_alquileresXdocumento_m]
GO
CREATE PROCEDURE [sel_equ_alquileresXdocumento_m]
	@p_id_documento INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_alquiler]
	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_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'
-----------------------------------29/05/2013 02:34:17 p.m.