


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


IF NOT  EXISTS (SELECT * FROM sys.foreign_keys WHERE parent_object_id = OBJECT_ID(N'[dbo].[alq_detalleLiquidacion]') AND object_id = OBJECT_ID(N'[dbo].[FK_alq_detalleLiquidacion_id_tarifa]')  )
begin

ALTER TABLE [alq_detalleLiquidacion] ADD CONSTRAINT
	FK_alq_detalleLiquidacion_id_tarifa FOREIGN KEY
	([id_tarifa]) REFERENCES [alq_tarifa]
	(id) ON UPDATE  NO ACTION ON DELETE  NO ACTION 
end




IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_alq_detalleLiquidacion]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_alq_detalleLiquidacion]
GO
CREATE PROCEDURE [reg_alq_detalleLiquidacion]
	@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],
		[detalleLiquidacion] AS [detalleLiquidacion],
		[detalleLiquidacion_codigo] AS [detalleLiquidacion_codigo],
		[cantidad] AS [cantidad],
		[dias] AS [dias],
		[fechaInicio_fh] AS [fechaInicio_fh],
		[fechaFin_fh] AS [fechaFin_fh],
		[id_itemDocumento] AS [id_itemDocumento],
		[id_detalleAlquiler] AS [id_detalleAlquiler],
		[saldo] AS [saldo],
		[id_documento_movimiento] AS [id_documento_movimiento],
		[horometro] AS [horometro],
		[id_tarifa] AS [id_tarifa]
	FROM [alq_detalleLiquidacion]
	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_alq_detalleLiquidacion]')) DROP VIEW [dbo].[view_alq_detalleLiquidacion]
GO
CREATE  VIEW [view_alq_detalleLiquidacion] AS
SELECT 	[alq_detalleLiquidacion].[id] AS [id],
		[alq_detalleLiquidacion].[uid] AS [uid],
		[alq_detalleLiquidacion].[eid] AS [eid],
		[alq_detalleLiquidacion].[id_usuario_modifico] AS [id_usuario_modifico],
		[alq_detalleLiquidacion].[id_usuario_creo] AS [id_usuario_creo],
		[alq_detalleLiquidacion].[fechaModificacion] AS [fechaModificacion],
		[alq_detalleLiquidacion].[fechaCreacion] AS [fechaCreacion],
		[alq_detalleLiquidacion].[active] AS [active],
		[alq_detalleLiquidacion].[detalleLiquidacion] AS [detalleLiquidacion],
		[alq_detalleLiquidacion].[detalleLiquidacion_codigo] AS [detalleLiquidacion_codigo],
		[alq_detalleLiquidacion].[cantidad] AS [cantidad],
		[alq_detalleLiquidacion].[dias] AS [dias],
		[alq_detalleLiquidacion].[fechaInicio_fh] AS [fechaInicio_fh],
		[alq_detalleLiquidacion].[fechaFin_fh] AS [fechaFin_fh],
		[alq_detalleLiquidacion].[id_itemDocumento] AS [id_itemDocumento],
[doc_itemDocumento].[itemDocumento] as [doc_itemDocumento_itemDocumento],
[doc_itemDocumento].[itemDocumento_codigo] as [doc_itemDocumento_itemDocumento_codigo],
[doc_itemDocumento].[detalle] as [doc_itemDocumento_detalle],
[doc_itemDocumento].[horasOrdinales] as [doc_itemDocumento_horasOrdinales],
[doc_itemDocumento].[horasFestivas] as [doc_itemDocumento_horasFestivas],
[doc_itemDocumento].[horasNocturnas] as [doc_itemDocumento_horasNocturnas],
[doc_itemDocumento].[cantidadPlaneado] as [doc_itemDocumento_cantidadPlaneado],
[doc_itemDocumento].[cantidadEjecutado] as [doc_itemDocumento_cantidadEjecutado],
[doc_itemDocumento].[valorUnitarioPlaneado] as [doc_itemDocumento_valorUnitarioPlaneado],
[doc_itemDocumento].[valorUnitarioEjecutado] as [doc_itemDocumento_valorUnitarioEjecutado],
[doc_itemDocumento].[subtotalPlaneado] as [doc_itemDocumento_subtotalPlaneado],
[doc_itemDocumento].[subtotalEjecutado] as [doc_itemDocumento_subtotalEjecutado],
[doc_itemDocumento].[totalPlaneado] as [doc_itemDocumento_totalPlaneado],
[doc_itemDocumento].[totalEjecutado] as [doc_itemDocumento_totalEjecutado],
[doc_itemDocumento].[porciva] as [doc_itemDocumento_porciva],
[doc_itemDocumento].[ivaPlaneado] as [doc_itemDocumento_ivaPlaneado],
[doc_itemDocumento].[ivaEjecutado] as [doc_itemDocumento_ivaEjecutado],
[doc_itemDocumento].[porcdto] as [doc_itemDocumento_porcdto],
[doc_itemDocumento].[dtoPlaneado] as [doc_itemDocumento_dtoPlaneado],
[doc_itemDocumento].[dtoEjecutado] as [doc_itemDocumento_dtoEjecutado],
[doc_itemDocumento].[costear] as [doc_itemDocumento_costear],
[doc_itemDocumento].[esUrgente] as [doc_itemDocumento_esUrgente],
[doc_itemDocumento].[vencimientoGarantia_ff] as [doc_itemDocumento_vencimientoGarantia_ff],
[doc_itemDocumento].[horasGarantia] as [doc_itemDocumento_horasGarantia],
[doc_itemDocumento].[porcentajeCliente] as [doc_itemDocumento_porcentajeCliente],
[doc_itemDocumento].[porcentajeDistribuidor] as [doc_itemDocumento_porcentajeDistribuidor],
[doc_itemDocumento].[porcentajeFabricante] as [doc_itemDocumento_porcentajeFabricante],
[doc_itemDocumento].[afectoInventario] as [doc_itemDocumento_afectoInventario],
[doc_itemDocumento].[id_documento] as [doc_itemDocumento_id_documento],
[doc_itemDocumento].[id_itemDocumento] as [doc_itemDocumento_id_itemDocumento],
[doc_itemDocumento].[id_centroCosto] as [doc_itemDocumento_id_centroCosto],
[doc_itemDocumento].[id_catalogo] as [doc_itemDocumento_id_catalogo],
[doc_itemDocumento].[id_bodega] as [doc_itemDocumento_id_bodega],
[doc_itemDocumento].[id_incoterm] as [doc_itemDocumento_id_incoterm],
[doc_itemDocumento].[id_itemDocumento_superior] as [doc_itemDocumento_id_itemDocumento_superior],
[doc_itemDocumento].[costoPlaneado] as [doc_itemDocumento_costoPlaneado],
[doc_itemDocumento].[costoEjecutado] as [doc_itemDocumento_costoEjecutado],
		[alq_detalleLiquidacion].[id_detalleAlquiler] AS [id_detalleAlquiler],
[equ_detalleAlquiler].[detalleAlquiler] as [equ_detalleAlquiler_detalleAlquiler],
[equ_detalleAlquiler].[detalleAlquiler_codigo] as [equ_detalleAlquiler_detalleAlquiler_codigo],
[equ_detalleAlquiler].[fechaDevolucion_ff] as [equ_detalleAlquiler_fechaDevolucion_ff],
[equ_detalleAlquiler].[valorHora] as [equ_detalleAlquiler_valorHora],
[equ_detalleAlquiler].[ValorHoraStandBy] as [equ_detalleAlquiler_ValorHoraStandBy],
[equ_detalleAlquiler].[valorPeriodo] as [equ_detalleAlquiler_valorPeriodo],
[equ_detalleAlquiler].[horometro] as [equ_detalleAlquiler_horometro],
[equ_detalleAlquiler].[valorMulta] as [equ_detalleAlquiler_valorMulta],
[equ_detalleAlquiler].[fechaDespacho_ff] as [equ_detalleAlquiler_fechaDespacho_ff],
[equ_detalleAlquiler].[id_equipo] as [equ_detalleAlquiler_id_equipo],
[equ_detalleAlquiler].[id_documento.alquiler] as [equ_detalleAlquiler_id_documento.alquiler],
[equ_detalleAlquiler].[id_sucursal] as [equ_detalleAlquiler_id_sucursal],
[equ_detalleAlquiler].[id_tercero] as [equ_detalleAlquiler_id_tercero],
[equ_detalleAlquiler].[fechaUltimaFactura_fh] as [equ_detalleAlquiler_fechaUltimaFactura_fh],
[equ_detalleAlquiler].[valorMes] as [equ_detalleAlquiler_valorMes],
[equ_detalleAlquiler].[fechaInicioFactura_fh] as [equ_detalleAlquiler_fechaInicioFactura_fh],
[equ_detalleAlquiler].[cantidadSolicitada] as [equ_detalleAlquiler_cantidadSolicitada],
[equ_detalleAlquiler].[cantidadAlquilada] as [equ_detalleAlquiler_cantidadAlquilada],
[equ_detalleAlquiler].[id_catalogo] as [equ_detalleAlquiler_id_catalogo],
[equ_detalleAlquiler].[estadoalquiler] as [equ_detalleAlquiler_estadoalquiler],
		[alq_detalleLiquidacion].[saldo] AS [saldo],
		[alq_detalleLiquidacion].[id_documento_movimiento] AS [id_documento_movimiento],
[doc_documento_movimiento].[documento] as [doc_documento_movimiento_documento],
[doc_documento_movimiento].[documento_codigo] as [doc_documento_movimiento_documento_codigo],
[doc_documento_movimiento].[documento_numero] as [doc_documento_movimiento_documento_numero],
[doc_documento_movimiento].[prefijo] as [doc_documento_movimiento_prefijo],
[doc_documento_movimiento].[subtotal] as [doc_documento_movimiento_subtotal],
[doc_documento_movimiento].[descuento] as [doc_documento_movimiento_descuento],
[doc_documento_movimiento].[iva] as [doc_documento_movimiento_iva],
[doc_documento_movimiento].[total] as [doc_documento_movimiento_total],
[doc_documento_movimiento].[costo] as [doc_documento_movimiento_costo],
[doc_documento_movimiento].[rentabilidad] as [doc_documento_movimiento_rentabilidad],
[doc_documento_movimiento].[trm] as [doc_documento_movimiento_trm],
[doc_documento_movimiento].[fecha_fh] as [doc_documento_movimiento_fecha_fh],
[doc_documento_movimiento].[fechaSugerida_fh] as [doc_documento_movimiento_fechaSugerida_fh],
[doc_documento_movimiento].[fechaCierre_ff] as [doc_documento_movimiento_fechaCierre_ff],
[doc_documento_movimiento].[fechaLimite_fh] as [doc_documento_movimiento_fechaLimite_fh],
[doc_documento_movimiento].[id_subtipoDocumento] as [doc_documento_movimiento_id_subtipoDocumento],
[doc_documento_movimiento].[id_tercero_cliente] as [doc_documento_movimiento_id_tercero_cliente],
[doc_documento_movimiento].[id_documento] as [doc_documento_movimiento_id_documento],
[doc_documento_movimiento].[id_tercero_proveedor] as [doc_documento_movimiento_id_tercero_proveedor],
[doc_documento_movimiento].[id_prioridadDocumento] as [doc_documento_movimiento_id_prioridadDocumento],
[doc_documento_movimiento].[id_moneda] as [doc_documento_movimiento_id_moneda],
[doc_documento_movimiento].[id_estadoTipoDocumento] as [doc_documento_movimiento_id_estadoTipoDocumento],
[doc_documento_movimiento].[id_motivoestado] as [doc_documento_movimiento_id_motivoestado],
[doc_documento_movimiento].[subtotalPlaneado] as [doc_documento_movimiento_subtotalPlaneado],
[doc_documento_movimiento].[descuentoPlaneado] as [doc_documento_movimiento_descuentoPlaneado],
[doc_documento_movimiento].[ivaPlaneado] as [doc_documento_movimiento_ivaPlaneado],
[doc_documento_movimiento].[totalPlaneado] as [doc_documento_movimiento_totalPlaneado],
[doc_documento_movimiento].[costoPlaneado] as [doc_documento_movimiento_costoPlaneado],
		[alq_detalleLiquidacion].[horometro] AS [horometro],
		[alq_detalleLiquidacion].[id_tarifa] AS [id_tarifa],
[alq_tarifa].[tarifa] as [alq_tarifa_tarifa],
[alq_tarifa].[tarifa_codigo] as [alq_tarifa_tarifa_codigo],
[alq_tarifa].[id_itemDocumento] as [alq_tarifa_id_itemDocumento],
[alq_tarifa].[id_detalleAlquiler] as [alq_tarifa_id_detalleAlquiler],
[alq_tarifa].[id_tipoTarifa] as [alq_tarifa_id_tipoTarifa],
[alq_tarifa].[horasIncluidas] as [alq_tarifa_horasIncluidas],
[alq_tarifa].[cantidadIncluida] as [alq_tarifa_cantidadIncluida],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [alq_detalleLiquidacion]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[alq_detalleLiquidacion].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[alq_detalleLiquidacion].[id_usuario_creo]
	INNER JOIN [doc_itemDocumento] ON [doc_itemDocumento].id=[alq_detalleLiquidacion].[id_itemDocumento]
	INNER JOIN [equ_detalleAlquiler] ON [equ_detalleAlquiler].id=[alq_detalleLiquidacion].[id_detalleAlquiler]
	INNER JOIN [doc_documento] AS [doc_documento_movimiento] ON [doc_documento_movimiento].id=[alq_detalleLiquidacion].[id_documento_movimiento]
	INNER JOIN [alq_tarifa] ON [alq_tarifa].id=[alq_detalleLiquidacion].[id_tarifa]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [alq_detalleLiquidacion].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [alq_detalleLiquidacion].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_alq_detalleLiquidacionesXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_alq_detalleLiquidacionesXusuario_modifico]
GO
CREATE PROCEDURE [sel_alq_detalleLiquidacionesXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_alq_detalleLiquidacion]
	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_alq_detalleLiquidacionesXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_alq_detalleLiquidacionesXusuario_creo]
GO
CREATE PROCEDURE [sel_alq_detalleLiquidacionesXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_alq_detalleLiquidacion]
	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_alq_detalleLiquidacionesXitemDocumento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_alq_detalleLiquidacionesXitemDocumento]
GO
CREATE PROCEDURE [sel_alq_detalleLiquidacionesXitemDocumento]
	@p_id_itemDocumento INT
AS
BEGIN
	SELECT * 
	FROM [view_alq_detalleLiquidacion]
	WHERE ([id_itemDocumento]= @p_id_itemDocumento)
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_alq_detalleLiquidacionesXdetalleAlquiler]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_alq_detalleLiquidacionesXdetalleAlquiler]
GO
CREATE PROCEDURE [sel_alq_detalleLiquidacionesXdetalleAlquiler]
	@p_id_detalleAlquiler INT
AS
BEGIN
	SELECT * 
	FROM [view_alq_detalleLiquidacion]
	WHERE ([id_detalleAlquiler]= @p_id_detalleAlquiler)
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_alq_detalleLiquidacionesXdocumento_movimiento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_alq_detalleLiquidacionesXdocumento_movimiento]
GO
CREATE PROCEDURE [sel_alq_detalleLiquidacionesXdocumento_movimiento]
	@p_id_documento_movimiento INT
AS
BEGIN
	SELECT * 
	FROM [view_alq_detalleLiquidacion]
	WHERE ([id_documento_movimiento]= @p_id_documento_movimiento)
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_alq_detalleLiquidacionesXtarifa]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_alq_detalleLiquidacionesXtarifa]
GO
CREATE PROCEDURE [sel_alq_detalleLiquidacionesXtarifa]
	@p_id_tarifa INT
AS
BEGIN
	SELECT * 
	FROM [view_alq_detalleLiquidacion]
	WHERE ([id_tarifa]= @p_id_tarifa)
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_alq_detalleLiquidaciones]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_alq_detalleLiquidaciones]
GO
CREATE PROCEDURE [sel_alq_detalleLiquidaciones]
	
	@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_alq_detalleLiquidacion]
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_alq_detalleLiquidaciones]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_alq_detalleLiquidaciones]
GO
CREATE PROCEDURE [lis_alq_detalleLiquidaciones]
	AS
BEGIN
	SELECT id AS id,
[detalleLiquidacion] AS [alq_detalleLiquidacion]
	FROM [alq_detalleLiquidacion]
	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_alq_detalleLiquidacion]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_alq_detalleLiquidacion]
GO
CREATE PROCEDURE [upd_alq_detalleLiquidacion]
	@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_detalleLiquidacion varchar(300) = null,
		@p_detalleLiquidacion_codigo varchar(100) = null,
		@p_cantidad int = null,
		@p_dias int = null,
		@p_fechaInicio_fh smalldatetime = null,
		@p_fechaFin_fh smalldatetime = null,
		@p_id_itemDocumento int = null,
		@p_id_detalleAlquiler int = null,
		@p_saldo int = null,
		@p_id_documento_movimiento int = null,
		@p_horometro float = null,
		@p_id_tarifa int = null
AS
BEGIN
		UPDATE [alq_detalleLiquidacion]
		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(),
			[detalleLiquidacion] = isnull(@p_detalleLiquidacion,[detalleLiquidacion]),
			[detalleLiquidacion_codigo] = isnull(@p_detalleLiquidacion_codigo,[detalleLiquidacion_codigo]),
			[cantidad] = isnull(@p_cantidad,[cantidad]),
			[dias] = isnull(@p_dias,[dias]),
			[fechaInicio_fh] = isnull(@p_fechaInicio_fh,[fechaInicio_fh]),
			[fechaFin_fh] = isnull(@p_fechaFin_fh,[fechaFin_fh]),
			[id_itemDocumento] = isnull(@p_id_itemDocumento,[id_itemDocumento]),
			[id_detalleAlquiler] = isnull(@p_id_detalleAlquiler,[id_detalleAlquiler]),
			[saldo] = isnull(@p_saldo,[saldo]),
			[id_documento_movimiento] = isnull(@p_id_documento_movimiento,[id_documento_movimiento]),
			[horometro] = isnull(@p_horometro,[horometro]),
			[id_tarifa] = isnull(@p_id_tarifa,[id_tarifa])
		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_alq_detalleLiquidacion]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_alq_detalleLiquidacion]
GO
CREATE PROCEDURE [ins_alq_detalleLiquidacion]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_detalleLiquidacion varchar(300),
		@p_detalleLiquidacion_codigo varchar(100),
		@p_cantidad int,
		@p_dias int,
		@p_fechaInicio_fh smalldatetime,
		@p_fechaFin_fh smalldatetime,
		@p_id_itemDocumento int,
		@p_id_detalleAlquiler int,
		@p_saldo int,
		@p_id_documento_movimiento int,
		@p_horometro float,
		@p_id_tarifa int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [alq_detalleLiquidacion]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[detalleLiquidacion],
			[detalleLiquidacion_codigo],
			[cantidad],
			[dias],
			[fechaInicio_fh],
			[fechaFin_fh],
			[id_itemDocumento],
			[id_detalleAlquiler],
			[saldo],
			[id_documento_movimiento],
			[horometro],
			[id_tarifa])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_detalleLiquidacion,
			@p_detalleLiquidacion_codigo,
			@p_cantidad,
			@p_dias,
			@p_fechaInicio_fh,
			@p_fechaFin_fh,
			@p_id_itemDocumento,
			@p_id_detalleAlquiler,
			@p_saldo,
			@p_id_documento_movimiento,
			@p_horometro,
			@p_id_tarifa)
		SET @v_id = scope_identity()
UPDATE [alq_detalleLiquidacion] 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_alq_detalleLiquidacion]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_alq_detalleLiquidacion]
GO
CREATE PROCEDURE [act_alq_detalleLiquidacion]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [alq_detalleLiquidacion]
	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_alq_detalleLiquidacion_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_alq_detalleLiquidacion_m]
GO
CREATE PROCEDURE [reg_alq_detalleLiquidacion_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],
		[detalleLiquidacion] AS [detalleLiquidacion],
		[detalleLiquidacion_codigo] AS [detalleLiquidacion_codigo],
		[cantidad] AS [cantidad],
		[dias] AS [dias],
		[fechaInicio_fh] AS [fechaInicio_fh],
		[fechaFin_fh] AS [fechaFin_fh],
		[id_itemDocumento] AS [id_itemDocumento],
		[id_detalleAlquiler] AS [id_detalleAlquiler],
		[saldo] AS [saldo],
		[id_documento_movimiento] AS [id_documento_movimiento],
		[horometro] AS [horometro],
		[id_tarifa] AS [id_tarifa]
	FROM [alq_detalleLiquidacion]
	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_alq_detalleLiquidacionesXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_alq_detalleLiquidacionesXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_alq_detalleLiquidacionesXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_alq_detalleLiquidacion]
	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_alq_detalleLiquidacionesXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_alq_detalleLiquidacionesXusuario_creo_m]
GO
CREATE PROCEDURE [sel_alq_detalleLiquidacionesXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_alq_detalleLiquidacion]
	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_alq_detalleLiquidacionesXitemDocumento_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_alq_detalleLiquidacionesXitemDocumento_m]
GO
CREATE PROCEDURE [sel_alq_detalleLiquidacionesXitemDocumento_m]
	@p_id_itemDocumento INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_alq_detalleLiquidacion]
	WHERE ([id_itemDocumento]= @p_id_itemDocumento)
	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_alq_detalleLiquidacionesXdetalleAlquiler_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_alq_detalleLiquidacionesXdetalleAlquiler_m]
GO
CREATE PROCEDURE [sel_alq_detalleLiquidacionesXdetalleAlquiler_m]
	@p_id_detalleAlquiler INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_alq_detalleLiquidacion]
	WHERE ([id_detalleAlquiler]= @p_id_detalleAlquiler)
	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_alq_detalleLiquidacionesXdocumento_movimiento_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_alq_detalleLiquidacionesXdocumento_movimiento_m]
GO
CREATE PROCEDURE [sel_alq_detalleLiquidacionesXdocumento_movimiento_m]
	@p_id_documento_movimiento INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_alq_detalleLiquidacion]
	WHERE ([id_documento_movimiento]= @p_id_documento_movimiento)
	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_alq_detalleLiquidacionesXtarifa_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_alq_detalleLiquidacionesXtarifa_m]
GO
CREATE PROCEDURE [sel_alq_detalleLiquidacionesXtarifa_m]
	@p_id_tarifa INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_alq_detalleLiquidacion]
	WHERE ([id_tarifa]= @p_id_tarifa)
	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_alq_detalleLiquidaciones_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_alq_detalleLiquidaciones_m]
GO
CREATE PROCEDURE [sel_alq_detalleLiquidaciones_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_alq_detalleLiquidacion]
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_alq_detalleLiquidaciones_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_alq_detalleLiquidaciones_m]
GO
CREATE PROCEDURE [lis_alq_detalleLiquidaciones_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[detalleLiquidacion] AS [alq_detalleLiquidacion]
	FROM [alq_detalleLiquidacion]
	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_alq_detalleLiquidacion]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_alq_detalleLiquidacion]
GO
CREATE PROCEDURE [aud_alq_detalleLiquidacion]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [alq_detalleLiquidacion].id as id_tabla,
	[alq_detalleLiquidacion].id_usuario_modifico as id_usuario_modifico,
	[alq_detalleLiquidacion].id_usuario_creo as id_usuario_creo,
	[alq_detalleLiquidacion].fechaModificacion as fechaModificacion,
	[alq_detalleLiquidacion].fechaCreacion as fechaCreacion,
	[alq_detalleLiquidacion].active as active,
	[alq_detalleLiquidacion].[detalleLiquidacion] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [alq_detalleLiquidacion]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[alq_detalleLiquidacion].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[alq_detalleLiquidacion].[id_usuario_creo]
	where ' + @p_filtro)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'alq_detalleLiquidacion'
-----------------------------------23/12/2015 9:22:40