

if not exists(select * FROM INFORMATION_SCHEMA.TABLES  where Table_Name='alq_detalleLiquidacion')
BEGIN	
EXECUTE [_ini_creartabla] 'alq_detalleLiquidacion',1
END

if not exists(select * FROM INFORMATION_SCHEMA.COLUMNS  where Table_Name='alq_detalleLiquidacion' and column_name = 'cantidad' )
BEGIN
 alter table [alq_detalleLiquidacion] add [cantidad] int
END

if not exists(select * FROM INFORMATION_SCHEMA.COLUMNS  where Table_Name='alq_detalleLiquidacion' and column_name = 'dias' )
BEGIN
 alter table [alq_detalleLiquidacion] add [dias] int
END

if not exists(select * FROM INFORMATION_SCHEMA.COLUMNS  where Table_Name='alq_detalleLiquidacion' and column_name = 'saldo' )
BEGIN
 alter table [alq_detalleLiquidacion] add [saldo] int
END

if not exists(select * FROM INFORMATION_SCHEMA.COLUMNS  where Table_Name='alq_detalleLiquidacion' and column_name = 'fechaInicio_fh' )
BEGIN
 alter table [alq_detalleLiquidacion] add [fechaInicio_fh] smalldatetime 
END

if not exists(select * FROM INFORMATION_SCHEMA.COLUMNS  where Table_Name='alq_detalleLiquidacion' and column_name = 'fechaFin_fh' )
BEGIN
 alter table [alq_detalleLiquidacion] add [fechaFin_fh] smalldatetime 
END

if not exists(select * FROM INFORMATION_SCHEMA.COLUMNS  where Table_Name='alq_detalleLiquidacion' and column_name = 'id_itemDocumento' )
BEGIN
 alter table [alq_detalleLiquidacion] add [id_itemDocumento] int 
END

if not exists(select * FROM INFORMATION_SCHEMA.COLUMNS  where Table_Name='alq_detalleLiquidacion' and column_name = 'id_detalleAlquiler' )
BEGIN
 alter table [alq_detalleLiquidacion] add [id_detalleAlquiler] int 
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_itemDocumento]')  )
BEGIN
ALTER TABLE [dbo].[alq_detalleLiquidacion]  WITH CHECK ADD  
CONSTRAINT [FK_alq_detalleLiquidacion_id_itemDocumento] FOREIGN KEY([id_itemDocumento])
REFERENCES [dbo].[doc_itemDocumento] ([id])
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_detalleAlquiler]')  )
BEGIN
ALTER TABLE [dbo].[alq_detalleLiquidacion]  WITH CHECK ADD  
CONSTRAINT [FK_alq_detalleLiquidacion_id_detalleAlquiler] FOREIGN KEY([id_detalleAlquiler])
REFERENCES [dbo].[equ_detalleAlquiler] ([id])
END


---------------------SP's


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]') 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],
		[saldo] AS [saldo],
		[fechaInicio_fh] AS [fechaInicio_fh],
		[fechaFin_fh] AS [fechaFin_fh],
		[id_itemDocumento] AS [id_itemDocumento],
		[id_detalleAlquiler] AS [id_detalleAlquiler]
	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].[saldo] AS [saldo],
		[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_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].[id_documento.alquiler] as [equ_detalleAlquiler_id_documento.alquiler],
		[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 [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_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_saldo int = null,
		@p_fechaInicio_fh smalldatetime = null,
		@p_fechaFin_fh smalldatetime = null,
		@p_id_itemDocumento int = null,
		@p_id_detalleAlquiler 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]),
			[saldo] = isnull(@p_saldo,[saldo]),
			[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])
		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_saldo int,
		@p_fechaInicio_fh smalldatetime,
		@p_fechaFin_fh smalldatetime,
		@p_id_itemDocumento int,
		@p_id_detalleAlquiler 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],
			[saldo],
			[fechaInicio_fh],
			[fechaFin_fh],
			[id_itemDocumento],
			[id_detalleAlquiler])
		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_saldo,
			@p_fechaInicio_fh,
			@p_fechaFin_fh,
			@p_id_itemDocumento,
			@p_id_detalleAlquiler)
		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],
		[saldo] AS [saldo],
		[fechaInicio_fh] AS [fechaInicio_fh],
		[fechaFin_fh] AS [fechaFin_fh],
		[id_itemDocumento] AS [id_itemDocumento],
		[id_detalleAlquiler] AS [id_detalleAlquiler]
	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_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

