

-----------

if exists(
select * from gui_funcionalidad where nombrecomando ='verOts' and id_funcionalidad=(select id from gui_funcionalidad where nombreControl='equ_alquiler' and id_tipoFuncionalidad=3)
)
begin
update gui_funcionalidad set funcionalidad='Ver COT',url='/forms/doc/doc_documento_cotizacion.aspx',nombrecomando ='verCots' 
where 
nombrecomando ='verOts' and id_funcionalidad=(select id from gui_funcionalidad where nombreControl='equ_alquiler' and id_tipoFuncionalidad=3)
end

------------------

if exists(select name from sys.columns where Name = N'fechaCompromisoDevolucion_ff' and Object_ID = Object_ID(N'equ_detalleAlquiler'))
begin

declare @constantSQL varchar(1000)

SELECT TOP 1 @constantSQL = 'ALTER TABLE dbo.equ_detalleAlquiler DROP CONSTRAINT ' + dc.NAME 
	from sys.default_constraints dc JOIN sys.columns c ON c.default_object_id = dc.object_id
    WHERE dc.parent_object_id = OBJECT_ID('equ_detalleAlquiler') AND dc.name LIKE '%DF__equ_detal__fecha%'
	AND c.name='fechaCompromisoDevolucion_ff'
	
exec (@constantSQL)

end
            
 if exists(select * from sys.columns where Name = N'fechaCompromisoDevolucion_ff' and Object_ID = Object_ID(N'equ_detalleAlquiler'))
begin       

BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION

ALTER TABLE dbo.equ_detalleAlquiler	DROP COLUMN fechaCompromisoDevolucion_ff

COMMIT

end

GO
---------------
/****** Object:  View [dbo].[view_equ_detalleAlquiler]    Script Date: 10/21/2013 13:58:09 ******/
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[view_equ_detalleAlquiler]'))
DROP VIEW [dbo].[view_equ_detalleAlquiler]
GO

GO

/****** Object:  View [dbo].[view_equ_detalleAlquiler]    Script Date: 10/21/2013 13:58:09 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE  VIEW [dbo].[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] as [equ_alquiler_id_documento],
[equ_alquiler].[id_sucursal] as [equ_alquiler_id_sucursal],
[equ_alquiler].[id_tercero] as [equ_alquiler_id_tercero],
[equ_alquiler].[id_subtipoDocumento] as [equ_alquiler_id_subtipoDocumento],
[equ_alquiler].[fechaFin_ff] as [equ_alquiler_fechaFin_ff],
[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],
[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],
		[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



GO

/****** Object:  StoredProcedure [dbo].[ins_equ_detalleAlquiler]    Script Date: 10/21/2013 14:01:03 ******/
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

/****** Object:  StoredProcedure [dbo].[ins_equ_detalleAlquiler]    Script Date: 10/21/2013 14:01:03 ******/
SET ANSI_NULLS OFF
GO

SET QUOTED_IDENTIFIER OFF
GO

CREATE PROCEDURE [dbo].[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_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],
			[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_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



GO

/****** Object:  StoredProcedure [dbo].[reg_equ_detalleAlquiler]    Script Date: 10/21/2013 14:01:54 ******/
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


/****** Object:  StoredProcedure [dbo].[reg_equ_detalleAlquiler]    Script Date: 10/21/2013 14:01:54 ******/
SET ANSI_NULLS OFF
GO

SET QUOTED_IDENTIFIER OFF
GO

CREATE PROCEDURE [dbo].[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],
		[valorMes] AS [valorMes],
		[fechaInicioFactura_fh] AS [fechaInicioFactura_fh]
	FROM [equ_detalleAlquiler]
	WHERE	(id = @p_id)

END

GO

GO

/****** Object:  StoredProcedure [dbo].[reg_equ_detalleAlquiler_m]    Script Date: 10/21/2013 14:02:33 ******/
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

/****** Object:  StoredProcedure [dbo].[reg_equ_detalleAlquiler_m]    Script Date: 10/21/2013 14:02:33 ******/
SET ANSI_NULLS OFF
GO

SET QUOTED_IDENTIFIER OFF
GO

CREATE PROCEDURE [dbo].[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],
		[valorMes] AS [valorMes],
		[fechaInicioFactura_fh] AS [fechaInicioFactura_fh]
	FROM [equ_detalleAlquiler]
	WHERE	(id = @p_id)
	AND	(eid LIKE @p_eid+'%')

END

GO

GO

/****** Object:  StoredProcedure [dbo].[upd_equ_detalleAlquiler]    Script Date: 10/21/2013 14:03:16 ******/
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


/****** Object:  StoredProcedure [dbo].[upd_equ_detalleAlquiler]    Script Date: 10/21/2013 14:03:16 ******/
SET ANSI_NULLS OFF
GO

SET QUOTED_IDENTIFIER OFF
GO

CREATE PROCEDURE [dbo].[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_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]),
			[valorMes] = isnull(@p_valorMes,[valorMes]),
			[fechaInicioFactura_fh] = isnull(@p_fechaInicioFactura_fh,[fechaInicioFactura_fh])
		WHERE (id = @p_id)
END

GO


