

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.ot] AS [id_documento.ot],
		[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.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],
[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.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
-----------------
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_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.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]),
			[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
/****** Objeto:  StoredProcedure [dbo].[ins_equ_alquiler]    Fecha de la secuencia de comandos: 05/20/2013 08:51:57 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
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_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,
		@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.ot],
			[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_ot,
			@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].[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],
		[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
print 'equ_alquiler'
-----------------------------------19/05/2013 10:38:07 a.m.