

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_doc_itemDocumento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_doc_itemDocumento]
GO
CREATE PROCEDURE [reg_doc_itemDocumento]
	@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],
		[itemDocumento] AS [itemDocumento],
		[itemDocumento_codigo] AS [itemDocumento_codigo],
		[detalle] AS [detalle],
		[horasOrdinales] AS [horasOrdinales],
		[horasFestivas] AS [horasFestivas],
		[horasNocturnas] AS [horasNocturnas],
		[cantidadPlaneado] AS [cantidadPlaneado],
		[cantidadEjecutado] AS [cantidadEjecutado],
		[valorUnitarioPlaneado] AS [valorUnitarioPlaneado],
		[valorUnitarioEjecutado] AS [valorUnitarioEjecutado],
		[subtotalPlaneado] AS [subtotalPlaneado],
		[subtotalEjecutado] AS [subtotalEjecutado],
		[totalPlaneado] AS [totalPlaneado],
		[totalEjecutado] AS [totalEjecutado],
		[porciva] AS [porciva],
		[ivaPlaneado] AS [ivaPlaneado],
		[ivaEjecutado] AS [ivaEjecutado],
		[porcdto] AS [porcdto],
		[dtoPlaneado] AS [dtoPlaneado],
		[dtoEjecutado] AS [dtoEjecutado],
		[costear] AS [costear],
		[esUrgente] AS [esUrgente],
		[vencimientoGarantia_ff] AS [vencimientoGarantia_ff],
		[horasGarantia] AS [horasGarantia],
		[porcentajeCliente] AS [porcentajeCliente],
		[porcentajeDistribuidor] AS [porcentajeDistribuidor],
		[porcentajeFabricante] AS [porcentajeFabricante],
		[afectoInventario] AS [afectoInventario],
		[id_documento] AS [id_documento],
		[id_itemDocumento] AS [id_itemDocumento],
		[id_centroCosto] AS [id_centroCosto],
		[id_catalogo] AS [id_catalogo],
		[id_bodega] AS [id_bodega],
		[id_incoterm] AS [id_incoterm],
		[id_itemDocumento_superior] AS [id_itemDocumento_superior],
		[costoPlaneado] AS [costoPlaneado],
		[costoEjecutado] AS [costoEjecutado],
		[id_listaprecio] AS [id_listaprecio]
	FROM [doc_itemDocumento]
	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_doc_itemDocumento]')) DROP VIEW [dbo].[view_doc_itemDocumento]
GO
CREATE  VIEW [view_doc_itemDocumento] AS
SELECT 	[doc_itemDocumento].[id] AS [id],
		[doc_itemDocumento].[uid] AS [uid],
		[doc_itemDocumento].[eid] AS [eid],
		[doc_itemDocumento].[id_usuario_modifico] AS [id_usuario_modifico],
		[doc_itemDocumento].[id_usuario_creo] AS [id_usuario_creo],
		[doc_itemDocumento].[fechaModificacion] AS [fechaModificacion],
		[doc_itemDocumento].[fechaCreacion] AS [fechaCreacion],
		[doc_itemDocumento].[active] AS [active],
		[doc_itemDocumento].[itemDocumento] AS [itemDocumento],
		[doc_itemDocumento].[itemDocumento_codigo] AS [itemDocumento_codigo],
		[doc_itemDocumento].[detalle] AS [detalle],
		[doc_itemDocumento].[horasOrdinales] AS [horasOrdinales],
		[doc_itemDocumento].[horasFestivas] AS [horasFestivas],
		[doc_itemDocumento].[horasNocturnas] AS [horasNocturnas],
		[doc_itemDocumento].[cantidadPlaneado] AS [cantidadPlaneado],
		[doc_itemDocumento].[cantidadEjecutado] AS [cantidadEjecutado],
		[doc_itemDocumento].[valorUnitarioPlaneado] AS [valorUnitarioPlaneado],
		[doc_itemDocumento].[valorUnitarioEjecutado] AS [valorUnitarioEjecutado],
		[doc_itemDocumento].[subtotalPlaneado] AS [subtotalPlaneado],
		[doc_itemDocumento].[subtotalEjecutado] AS [subtotalEjecutado],
		[doc_itemDocumento].[totalPlaneado] AS [totalPlaneado],
		[doc_itemDocumento].[totalEjecutado] AS [totalEjecutado],
		[doc_itemDocumento].[porciva] AS [porciva],
		[doc_itemDocumento].[ivaPlaneado] AS [ivaPlaneado],
		[doc_itemDocumento].[ivaEjecutado] AS [ivaEjecutado],
		[doc_itemDocumento].[porcdto] AS [porcdto],
		[doc_itemDocumento].[dtoPlaneado] AS [dtoPlaneado],
		[doc_itemDocumento].[dtoEjecutado] AS [dtoEjecutado],
		[doc_itemDocumento].[costear] AS [costear],
		[doc_itemDocumento].[esUrgente] AS [esUrgente],
		[doc_itemDocumento].[vencimientoGarantia_ff] AS [vencimientoGarantia_ff],
		[doc_itemDocumento].[horasGarantia] AS [horasGarantia],
		[doc_itemDocumento].[porcentajeCliente] AS [porcentajeCliente],
		[doc_itemDocumento].[porcentajeDistribuidor] AS [porcentajeDistribuidor],
		[doc_itemDocumento].[porcentajeFabricante] AS [porcentajeFabricante],
		[doc_itemDocumento].[afectoInventario] AS [afectoInventario],
		[doc_itemDocumento].[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],
		[doc_itemDocumento].[id_itemDocumento] AS [id_itemDocumento],
[doc_itemDocumento_padre].[itemDocumento] as [doc_itemDocumento_padre_itemDocumento],
[doc_itemDocumento_padre].[itemDocumento_codigo] as [doc_itemDocumento_padre_itemDocumento_codigo],
[doc_itemDocumento_padre].[detalle] as [doc_itemDocumento_padre_detalle],
[doc_itemDocumento_padre].[horasOrdinales] as [doc_itemDocumento_padre_horasOrdinales],
[doc_itemDocumento_padre].[horasFestivas] as [doc_itemDocumento_padre_horasFestivas],
[doc_itemDocumento_padre].[horasNocturnas] as [doc_itemDocumento_padre_horasNocturnas],
[doc_itemDocumento_padre].[cantidadPlaneado] as [doc_itemDocumento_padre_cantidadPlaneado],
[doc_itemDocumento_padre].[cantidadEjecutado] as [doc_itemDocumento_padre_cantidadEjecutado],
[doc_itemDocumento_padre].[valorUnitarioPlaneado] as [doc_itemDocumento_padre_valorUnitarioPlaneado],
[doc_itemDocumento_padre].[valorUnitarioEjecutado] as [doc_itemDocumento_padre_valorUnitarioEjecutado],
[doc_itemDocumento_padre].[subtotalPlaneado] as [doc_itemDocumento_padre_subtotalPlaneado],
[doc_itemDocumento_padre].[subtotalEjecutado] as [doc_itemDocumento_padre_subtotalEjecutado],
[doc_itemDocumento_padre].[totalPlaneado] as [doc_itemDocumento_padre_totalPlaneado],
[doc_itemDocumento_padre].[totalEjecutado] as [doc_itemDocumento_padre_totalEjecutado],
[doc_itemDocumento_padre].[porciva] as [doc_itemDocumento_padre_porciva],
[doc_itemDocumento_padre].[ivaPlaneado] as [doc_itemDocumento_padre_ivaPlaneado],
[doc_itemDocumento_padre].[ivaEjecutado] as [doc_itemDocumento_padre_ivaEjecutado],
[doc_itemDocumento_padre].[porcdto] as [doc_itemDocumento_padre_porcdto],
[doc_itemDocumento_padre].[dtoPlaneado] as [doc_itemDocumento_padre_dtoPlaneado],
[doc_itemDocumento_padre].[dtoEjecutado] as [doc_itemDocumento_padre_dtoEjecutado],
[doc_itemDocumento_padre].[costear] as [doc_itemDocumento_padre_costear],
[doc_itemDocumento_padre].[esUrgente] as [doc_itemDocumento_padre_esUrgente],
[doc_itemDocumento_padre].[vencimientoGarantia_ff] as [doc_itemDocumento_padre_vencimientoGarantia_ff],
[doc_itemDocumento_padre].[horasGarantia] as [doc_itemDocumento_padre_horasGarantia],
[doc_itemDocumento_padre].[porcentajeCliente] as [doc_itemDocumento_padre_porcentajeCliente],
[doc_itemDocumento_padre].[porcentajeDistribuidor] as [doc_itemDocumento_padre_porcentajeDistribuidor],
[doc_itemDocumento_padre].[porcentajeFabricante] as [doc_itemDocumento_padre_porcentajeFabricante],
[doc_itemDocumento_padre].[afectoInventario] as [doc_itemDocumento_padre_afectoInventario],
[doc_itemDocumento_padre].[id_documento] as [doc_itemDocumento_padre_id_documento],
[doc_itemDocumento_padre].[id_itemDocumento] as [doc_itemDocumento_padre_id_itemDocumento],
[doc_itemDocumento_padre].[id_centroCosto] as [doc_itemDocumento_padre_id_centroCosto],
[doc_itemDocumento_padre].[id_catalogo] as [doc_itemDocumento_padre_id_catalogo],
[doc_itemDocumento_padre].[id_bodega] as [doc_itemDocumento_padre_id_bodega],
[doc_itemDocumento_padre].[id_incoterm] as [doc_itemDocumento_padre_id_incoterm],
[doc_itemDocumento_padre].[id_itemDocumento_superior] as [doc_itemDocumento_padre_id_itemDocumento_superior],
[doc_itemDocumento_padre].[costoPlaneado] as [doc_itemDocumento_padre_costoPlaneado],
[doc_itemDocumento_padre].[costoEjecutado] as [doc_itemDocumento_padre_costoEjecutado],
[doc_itemDocumento_padre].[id_listaprecio] as [doc_itemDocumento_padre_id_listaprecio],
		[doc_itemDocumento].[id_centroCosto] AS [id_centroCosto],
[doc_centroCosto].[centroCosto] as [doc_centroCosto_centroCosto],
[doc_centroCosto].[centroCosto_codigo] as [doc_centroCosto_centroCosto_codigo],
		[doc_itemDocumento].[id_catalogo] AS [id_catalogo],
[cat_catalogo].[catalogo] as [cat_catalogo_catalogo],
[cat_catalogo].[catalogo_codigo] as [cat_catalogo_catalogo_codigo],
[cat_catalogo].[codigoInventario] as [cat_catalogo_codigoInventario],
[cat_catalogo].[precioVenta] as [cat_catalogo_precioVenta],
[cat_catalogo].[tiempoConsecucion] as [cat_catalogo_tiempoConsecucion],
[cat_catalogo].[esNacional] as [cat_catalogo_esNacional],
[cat_catalogo].[id_familia] as [cat_catalogo_id_familia],
[cat_catalogo].[id_subTipoCatalogo] as [cat_catalogo_id_subTipoCatalogo],
[cat_catalogo].[id_unidad] as [cat_catalogo_id_unidad],
[cat_catalogo].[id_impuesto] as [cat_catalogo_id_impuesto],
[cat_catalogo].[descripcion] as [cat_catalogo_descripcion],
[cat_catalogo].[bloquearCostear] as [cat_catalogo_bloquearCostear],
		[doc_itemDocumento].[id_bodega] AS [id_bodega],
[gen_bodega].[bodega] as [gen_bodega_bodega],
[gen_bodega].[bodega_codigo] as [gen_bodega_bodega_codigo],
[gen_bodega].[salidasEnRojo] as [gen_bodega_salidasEnRojo],
[gen_bodega].[usaLocalizacion] as [gen_bodega_usaLocalizacion],
[gen_bodega].[id_sucursal] as [gen_bodega_id_sucursal],
		[doc_itemDocumento].[id_incoterm] AS [id_incoterm],
[doc_incoterm].[incoterm] as [doc_incoterm_incoterm],
[doc_incoterm].[incoterm_codigo] as [doc_incoterm_incoterm_codigo],
		[doc_itemDocumento].[id_itemDocumento_superior] AS [id_itemDocumento_superior],
[doc_itemDocumento_superior].[itemDocumento] as [doc_itemDocumento_superior_itemDocumento],
[doc_itemDocumento_superior].[itemDocumento_codigo] as [doc_itemDocumento_superior_itemDocumento_codigo],
[doc_itemDocumento_superior].[detalle] as [doc_itemDocumento_superior_detalle],
[doc_itemDocumento_superior].[horasOrdinales] as [doc_itemDocumento_superior_horasOrdinales],
[doc_itemDocumento_superior].[horasFestivas] as [doc_itemDocumento_superior_horasFestivas],
[doc_itemDocumento_superior].[horasNocturnas] as [doc_itemDocumento_superior_horasNocturnas],
[doc_itemDocumento_superior].[cantidadPlaneado] as [doc_itemDocumento_superior_cantidadPlaneado],
[doc_itemDocumento_superior].[cantidadEjecutado] as [doc_itemDocumento_superior_cantidadEjecutado],
[doc_itemDocumento_superior].[valorUnitarioPlaneado] as [doc_itemDocumento_superior_valorUnitarioPlaneado],
[doc_itemDocumento_superior].[valorUnitarioEjecutado] as [doc_itemDocumento_superior_valorUnitarioEjecutado],
[doc_itemDocumento_superior].[subtotalPlaneado] as [doc_itemDocumento_superior_subtotalPlaneado],
[doc_itemDocumento_superior].[subtotalEjecutado] as [doc_itemDocumento_superior_subtotalEjecutado],
[doc_itemDocumento_superior].[totalPlaneado] as [doc_itemDocumento_superior_totalPlaneado],
[doc_itemDocumento_superior].[totalEjecutado] as [doc_itemDocumento_superior_totalEjecutado],
[doc_itemDocumento_superior].[porciva] as [doc_itemDocumento_superior_porciva],
[doc_itemDocumento_superior].[ivaPlaneado] as [doc_itemDocumento_superior_ivaPlaneado],
[doc_itemDocumento_superior].[ivaEjecutado] as [doc_itemDocumento_superior_ivaEjecutado],
[doc_itemDocumento_superior].[porcdto] as [doc_itemDocumento_superior_porcdto],
[doc_itemDocumento_superior].[dtoPlaneado] as [doc_itemDocumento_superior_dtoPlaneado],
[doc_itemDocumento_superior].[dtoEjecutado] as [doc_itemDocumento_superior_dtoEjecutado],
[doc_itemDocumento_superior].[costear] as [doc_itemDocumento_superior_costear],
[doc_itemDocumento_superior].[esUrgente] as [doc_itemDocumento_superior_esUrgente],
[doc_itemDocumento_superior].[vencimientoGarantia_ff] as [doc_itemDocumento_superior_vencimientoGarantia_ff],
[doc_itemDocumento_superior].[horasGarantia] as [doc_itemDocumento_superior_horasGarantia],
[doc_itemDocumento_superior].[porcentajeCliente] as [doc_itemDocumento_superior_porcentajeCliente],
[doc_itemDocumento_superior].[porcentajeDistribuidor] as [doc_itemDocumento_superior_porcentajeDistribuidor],
[doc_itemDocumento_superior].[porcentajeFabricante] as [doc_itemDocumento_superior_porcentajeFabricante],
[doc_itemDocumento_superior].[afectoInventario] as [doc_itemDocumento_superior_afectoInventario],
[doc_itemDocumento_superior].[id_documento] as [doc_itemDocumento_superior_id_documento],
[doc_itemDocumento_superior].[id_itemDocumento] as [doc_itemDocumento_superior_id_itemDocumento],
[doc_itemDocumento_superior].[id_centroCosto] as [doc_itemDocumento_superior_id_centroCosto],
[doc_itemDocumento_superior].[id_catalogo] as [doc_itemDocumento_superior_id_catalogo],
[doc_itemDocumento_superior].[id_bodega] as [doc_itemDocumento_superior_id_bodega],
[doc_itemDocumento_superior].[id_incoterm] as [doc_itemDocumento_superior_id_incoterm],
[doc_itemDocumento_superior].[id_itemDocumento_superior] as [doc_itemDocumento_superior_id_itemDocumento_superior],
[doc_itemDocumento_superior].[costoPlaneado] as [doc_itemDocumento_superior_costoPlaneado],
[doc_itemDocumento_superior].[costoEjecutado] as [doc_itemDocumento_superior_costoEjecutado],
[doc_itemDocumento_superior].[id_listaprecio] as [doc_itemDocumento_superior_id_listaprecio],
		[doc_itemDocumento].[costoPlaneado] AS [costoPlaneado],
		[doc_itemDocumento].[costoEjecutado] AS [costoEjecutado],
		[doc_itemDocumento].[id_listaprecio] AS [id_listaprecio],
[cat_listaPrecio].[listaPrecio] as [cat_listaPrecio_listaPrecio],
[cat_listaPrecio].[listaPrecio_codigo] as [cat_listaPrecio_listaPrecio_codigo],
[cat_listaPrecio].[porcentajeDescuento] as [cat_listaPrecio_porcentajeDescuento],
[cat_listaPrecio].[esPrivado] as [cat_listaPrecio_esPrivado],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [doc_itemDocumento]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[doc_itemDocumento].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[doc_itemDocumento].[id_usuario_creo]
	INNER JOIN [doc_documento] ON [doc_documento].id=[doc_itemDocumento].[id_documento]
	INNER JOIN [doc_itemDocumento] AS [doc_itemDocumento_padre] ON [doc_itemDocumento_padre].id=[doc_itemDocumento].[id_itemDocumento]
	INNER JOIN [doc_centroCosto] ON [doc_centroCosto].id=[doc_itemDocumento].[id_centroCosto]
	INNER JOIN [cat_catalogo] ON [cat_catalogo].id=[doc_itemDocumento].[id_catalogo]
	INNER JOIN [gen_bodega] ON [gen_bodega].id=[doc_itemDocumento].[id_bodega]
	INNER JOIN [doc_incoterm] ON [doc_incoterm].id=[doc_itemDocumento].[id_incoterm]
	INNER JOIN [doc_itemDocumento] AS [doc_itemDocumento_superior] ON [doc_itemDocumento_superior].id=[doc_itemDocumento].[id_itemDocumento_superior]
	INNER JOIN [cat_listaPrecio] ON [cat_listaPrecio].id=[doc_itemDocumento].[id_listaprecio]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [doc_itemDocumento].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [doc_itemDocumento].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_doc_itemDocumentosXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_itemDocumentosXusuario_modifico]
GO
CREATE PROCEDURE [sel_doc_itemDocumentosXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_doc_itemDocumento]
	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_doc_itemDocumentosXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_itemDocumentosXusuario_creo]
GO
CREATE PROCEDURE [sel_doc_itemDocumentosXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_doc_itemDocumento]
	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_doc_itemDocumentosXdocumento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_itemDocumentosXdocumento]
GO
CREATE PROCEDURE [sel_doc_itemDocumentosXdocumento]
	@p_id_documento INT
AS
BEGIN
	SELECT * 
	FROM [view_doc_itemDocumento]
	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_doc_itemDocumentosXitemDocumento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_itemDocumentosXitemDocumento]
GO
CREATE PROCEDURE [sel_doc_itemDocumentosXitemDocumento]
	@p_id_itemDocumento INT
AS
BEGIN
	SELECT * 
	FROM [view_doc_itemDocumento]
	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_doc_itemDocumentosXcentroCosto]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_itemDocumentosXcentroCosto]
GO
CREATE PROCEDURE [sel_doc_itemDocumentosXcentroCosto]
	@p_id_centroCosto INT
AS
BEGIN
	SELECT * 
	FROM [view_doc_itemDocumento]
	WHERE ([id_centroCosto]= @p_id_centroCosto)
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_doc_itemDocumentosXcatalogo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_itemDocumentosXcatalogo]
GO
CREATE PROCEDURE [sel_doc_itemDocumentosXcatalogo]
	@p_id_catalogo INT
AS
BEGIN
	SELECT * 
	FROM [view_doc_itemDocumento]
	WHERE ([id_catalogo]= @p_id_catalogo)
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_doc_itemDocumentosXbodega]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_itemDocumentosXbodega]
GO
CREATE PROCEDURE [sel_doc_itemDocumentosXbodega]
	@p_id_bodega INT
AS
BEGIN
	SELECT * 
	FROM [view_doc_itemDocumento]
	WHERE ([id_bodega]= @p_id_bodega)
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_doc_itemDocumentosXincoterm]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_itemDocumentosXincoterm]
GO
CREATE PROCEDURE [sel_doc_itemDocumentosXincoterm]
	@p_id_incoterm INT
AS
BEGIN
	SELECT * 
	FROM [view_doc_itemDocumento]
	WHERE ([id_incoterm]= @p_id_incoterm)
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_doc_itemDocumentosXitemDocumento_superior]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_itemDocumentosXitemDocumento_superior]
GO
CREATE PROCEDURE [sel_doc_itemDocumentosXitemDocumento_superior]
	@p_id_itemDocumento_superior INT
AS
BEGIN
	SELECT * 
	FROM [view_doc_itemDocumento]
	WHERE ([id_itemDocumento_superior]= @p_id_itemDocumento_superior)
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_doc_itemDocumentosXlistaprecio]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_itemDocumentosXlistaprecio]
GO
CREATE PROCEDURE [sel_doc_itemDocumentosXlistaprecio]
	@p_id_listaprecio INT
AS
BEGIN
	SELECT * 
	FROM [view_doc_itemDocumento]
	WHERE ([id_listaprecio]= @p_id_listaprecio)
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_doc_itemDocumentos]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_itemDocumentos]
GO
CREATE PROCEDURE [sel_doc_itemDocumentos]
	
	@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_doc_itemDocumento]
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_doc_itemDocumentos]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_doc_itemDocumentos]
GO
CREATE PROCEDURE [lis_doc_itemDocumentos]
	AS
BEGIN
	SELECT id AS id,
[itemDocumento] AS [doc_itemDocumento]
	FROM [doc_itemDocumento]
	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_doc_itemDocumento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_doc_itemDocumento]
GO
CREATE PROCEDURE [upd_doc_itemDocumento]
	@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_itemDocumento varchar(300) = null,
		@p_itemDocumento_codigo varchar(100) = null,
		@p_detalle varchar(300) = null,
		@p_horasOrdinales float = null,
		@p_horasFestivas float = null,
		@p_horasNocturnas float = null,
		@p_cantidadPlaneado float = null,
		@p_cantidadEjecutado float = null,
		@p_valorUnitarioPlaneado money = null,
		@p_valorUnitarioEjecutado money = null,
		@p_subtotalPlaneado money = null,
		@p_subtotalEjecutado money = null,
		@p_totalPlaneado money = null,
		@p_totalEjecutado money = null,
		@p_porciva float = null,
		@p_ivaPlaneado money = null,
		@p_ivaEjecutado money = null,
		@p_porcdto float = null,
		@p_dtoPlaneado money = null,
		@p_dtoEjecutado money = null,
		@p_costear bit = null,
		@p_esUrgente bit = null,
		@p_vencimientoGarantia_ff smalldatetime = null,
		@p_horasGarantia float = null,
		@p_porcentajeCliente float = null,
		@p_porcentajeDistribuidor float = null,
		@p_porcentajeFabricante float = null,
		@p_afectoInventario bit = null,
		@p_id_documento int = null,
		@p_id_itemDocumento int = null,
		@p_id_centroCosto int = null,
		@p_id_catalogo int = null,
		@p_id_bodega int = null,
		@p_id_incoterm int = null,
		@p_id_itemDocumento_superior int = null,
		@p_costoPlaneado float = null,
		@p_costoEjecutado float = null,
		@p_id_listaprecio int = null
AS
BEGIN
		UPDATE [doc_itemDocumento]
		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(),
			[itemDocumento] = isnull(@p_itemDocumento,[itemDocumento]),
			[itemDocumento_codigo] = isnull(@p_itemDocumento_codigo,[itemDocumento_codigo]),
			[detalle] = isnull(@p_detalle,[detalle]),
			[horasOrdinales] = isnull(@p_horasOrdinales,[horasOrdinales]),
			[horasFestivas] = isnull(@p_horasFestivas,[horasFestivas]),
			[horasNocturnas] = isnull(@p_horasNocturnas,[horasNocturnas]),
			[cantidadPlaneado] = isnull(@p_cantidadPlaneado,[cantidadPlaneado]),
			[cantidadEjecutado] = isnull(@p_cantidadEjecutado,[cantidadEjecutado]),
			[valorUnitarioPlaneado] = isnull(@p_valorUnitarioPlaneado,[valorUnitarioPlaneado]),
			[valorUnitarioEjecutado] = isnull(@p_valorUnitarioEjecutado,[valorUnitarioEjecutado]),
			[subtotalPlaneado] = isnull(@p_subtotalPlaneado,[subtotalPlaneado]),
			[subtotalEjecutado] = isnull(@p_subtotalEjecutado,[subtotalEjecutado]),
			[totalPlaneado] = isnull(@p_totalPlaneado,[totalPlaneado]),
			[totalEjecutado] = isnull(@p_totalEjecutado,[totalEjecutado]),
			[porciva] = isnull(@p_porciva,[porciva]),
			[ivaPlaneado] = isnull(@p_ivaPlaneado,[ivaPlaneado]),
			[ivaEjecutado] = isnull(@p_ivaEjecutado,[ivaEjecutado]),
			[porcdto] = isnull(@p_porcdto,[porcdto]),
			[dtoPlaneado] = isnull(@p_dtoPlaneado,[dtoPlaneado]),
			[dtoEjecutado] = isnull(@p_dtoEjecutado,[dtoEjecutado]),
			[costear] = isnull(@p_costear,[costear]),
			[esUrgente] = isnull(@p_esUrgente,[esUrgente]),
			[vencimientoGarantia_ff] = isnull(@p_vencimientoGarantia_ff,[vencimientoGarantia_ff]),
			[horasGarantia] = isnull(@p_horasGarantia,[horasGarantia]),
			[porcentajeCliente] = isnull(@p_porcentajeCliente,[porcentajeCliente]),
			[porcentajeDistribuidor] = isnull(@p_porcentajeDistribuidor,[porcentajeDistribuidor]),
			[porcentajeFabricante] = isnull(@p_porcentajeFabricante,[porcentajeFabricante]),
			[afectoInventario] = isnull(@p_afectoInventario,[afectoInventario]),
			[id_documento] = isnull(@p_id_documento,[id_documento]),
			[id_itemDocumento] = isnull(@p_id_itemDocumento,[id_itemDocumento]),
			[id_centroCosto] = isnull(@p_id_centroCosto,[id_centroCosto]),
			[id_catalogo] = isnull(@p_id_catalogo,[id_catalogo]),
			[id_bodega] = isnull(@p_id_bodega,[id_bodega]),
			[id_incoterm] = isnull(@p_id_incoterm,[id_incoterm]),
			[id_itemDocumento_superior] = isnull(@p_id_itemDocumento_superior,[id_itemDocumento_superior]),
			[costoPlaneado] = isnull(@p_costoPlaneado,[costoPlaneado]),
			[costoEjecutado] = isnull(@p_costoEjecutado,[costoEjecutado]),
			[id_listaprecio] = isnull(@p_id_listaprecio,[id_listaprecio])
		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_doc_itemDocumento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_doc_itemDocumento]
GO
CREATE PROCEDURE [ins_doc_itemDocumento]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_itemDocumento varchar(300),
		@p_itemDocumento_codigo varchar(100),
		@p_detalle varchar(300),
		@p_horasOrdinales float,
		@p_horasFestivas float,
		@p_horasNocturnas float,
		@p_cantidadPlaneado float,
		@p_cantidadEjecutado float,
		@p_valorUnitarioPlaneado money,
		@p_valorUnitarioEjecutado money,
		@p_subtotalPlaneado money,
		@p_subtotalEjecutado money,
		@p_totalPlaneado money,
		@p_totalEjecutado money,
		@p_porciva float,
		@p_ivaPlaneado money,
		@p_ivaEjecutado money,
		@p_porcdto float,
		@p_dtoPlaneado money,
		@p_dtoEjecutado money,
		@p_costear bit,
		@p_esUrgente bit,
		@p_vencimientoGarantia_ff smalldatetime,
		@p_horasGarantia float,
		@p_porcentajeCliente float,
		@p_porcentajeDistribuidor float,
		@p_porcentajeFabricante float,
		@p_afectoInventario bit,
		@p_id_documento int,
		@p_id_itemDocumento int,
		@p_id_centroCosto int,
		@p_id_catalogo int,
		@p_id_bodega int,
		@p_id_incoterm int,
		@p_id_itemDocumento_superior int,
		@p_costoPlaneado float,
		@p_costoEjecutado float,
		@p_id_listaprecio int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [doc_itemDocumento]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[itemDocumento],
			[itemDocumento_codigo],
			[detalle],
			[horasOrdinales],
			[horasFestivas],
			[horasNocturnas],
			[cantidadPlaneado],
			[cantidadEjecutado],
			[valorUnitarioPlaneado],
			[valorUnitarioEjecutado],
			[subtotalPlaneado],
			[subtotalEjecutado],
			[totalPlaneado],
			[totalEjecutado],
			[porciva],
			[ivaPlaneado],
			[ivaEjecutado],
			[porcdto],
			[dtoPlaneado],
			[dtoEjecutado],
			[costear],
			[esUrgente],
			[vencimientoGarantia_ff],
			[horasGarantia],
			[porcentajeCliente],
			[porcentajeDistribuidor],
			[porcentajeFabricante],
			[afectoInventario],
			[id_documento],
			[id_itemDocumento],
			[id_centroCosto],
			[id_catalogo],
			[id_bodega],
			[id_incoterm],
			[id_itemDocumento_superior],
			[costoPlaneado],
			[costoEjecutado],
			[id_listaprecio])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_itemDocumento,
			@p_itemDocumento_codigo,
			@p_detalle,
			@p_horasOrdinales,
			@p_horasFestivas,
			@p_horasNocturnas,
			@p_cantidadPlaneado,
			@p_cantidadEjecutado,
			@p_valorUnitarioPlaneado,
			@p_valorUnitarioEjecutado,
			@p_subtotalPlaneado,
			@p_subtotalEjecutado,
			@p_totalPlaneado,
			@p_totalEjecutado,
			@p_porciva,
			@p_ivaPlaneado,
			@p_ivaEjecutado,
			@p_porcdto,
			@p_dtoPlaneado,
			@p_dtoEjecutado,
			@p_costear,
			@p_esUrgente,
			@p_vencimientoGarantia_ff,
			@p_horasGarantia,
			@p_porcentajeCliente,
			@p_porcentajeDistribuidor,
			@p_porcentajeFabricante,
			@p_afectoInventario,
			@p_id_documento,
			@p_id_itemDocumento,
			@p_id_centroCosto,
			@p_id_catalogo,
			@p_id_bodega,
			@p_id_incoterm,
			@p_id_itemDocumento_superior,
			@p_costoPlaneado,
			@p_costoEjecutado,
			@p_id_listaprecio)
		SET @v_id = scope_identity()
UPDATE [doc_itemDocumento] 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_doc_itemDocumento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_doc_itemDocumento]
GO
CREATE PROCEDURE [act_doc_itemDocumento]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [doc_itemDocumento]
	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_doc_itemDocumento_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_doc_itemDocumento_m]
GO
CREATE PROCEDURE [reg_doc_itemDocumento_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],
		[itemDocumento] AS [itemDocumento],
		[itemDocumento_codigo] AS [itemDocumento_codigo],
		[detalle] AS [detalle],
		[horasOrdinales] AS [horasOrdinales],
		[horasFestivas] AS [horasFestivas],
		[horasNocturnas] AS [horasNocturnas],
		[cantidadPlaneado] AS [cantidadPlaneado],
		[cantidadEjecutado] AS [cantidadEjecutado],
		[valorUnitarioPlaneado] AS [valorUnitarioPlaneado],
		[valorUnitarioEjecutado] AS [valorUnitarioEjecutado],
		[subtotalPlaneado] AS [subtotalPlaneado],
		[subtotalEjecutado] AS [subtotalEjecutado],
		[totalPlaneado] AS [totalPlaneado],
		[totalEjecutado] AS [totalEjecutado],
		[porciva] AS [porciva],
		[ivaPlaneado] AS [ivaPlaneado],
		[ivaEjecutado] AS [ivaEjecutado],
		[porcdto] AS [porcdto],
		[dtoPlaneado] AS [dtoPlaneado],
		[dtoEjecutado] AS [dtoEjecutado],
		[costear] AS [costear],
		[esUrgente] AS [esUrgente],
		[vencimientoGarantia_ff] AS [vencimientoGarantia_ff],
		[horasGarantia] AS [horasGarantia],
		[porcentajeCliente] AS [porcentajeCliente],
		[porcentajeDistribuidor] AS [porcentajeDistribuidor],
		[porcentajeFabricante] AS [porcentajeFabricante],
		[afectoInventario] AS [afectoInventario],
		[id_documento] AS [id_documento],
		[id_itemDocumento] AS [id_itemDocumento],
		[id_centroCosto] AS [id_centroCosto],
		[id_catalogo] AS [id_catalogo],
		[id_bodega] AS [id_bodega],
		[id_incoterm] AS [id_incoterm],
		[id_itemDocumento_superior] AS [id_itemDocumento_superior],
		[costoPlaneado] AS [costoPlaneado],
		[costoEjecutado] AS [costoEjecutado],
		[id_listaprecio] AS [id_listaprecio]
	FROM [doc_itemDocumento]
	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_doc_itemDocumentosXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_itemDocumentosXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_doc_itemDocumentosXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_doc_itemDocumento]
	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_doc_itemDocumentosXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_itemDocumentosXusuario_creo_m]
GO
CREATE PROCEDURE [sel_doc_itemDocumentosXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_doc_itemDocumento]
	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_doc_itemDocumentosXdocumento_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_itemDocumentosXdocumento_m]
GO
CREATE PROCEDURE [sel_doc_itemDocumentosXdocumento_m]
	@p_id_documento INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_doc_itemDocumento]
	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_doc_itemDocumentosXitemDocumento_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_itemDocumentosXitemDocumento_m]
GO
CREATE PROCEDURE [sel_doc_itemDocumentosXitemDocumento_m]
	@p_id_itemDocumento INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_doc_itemDocumento]
	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_doc_itemDocumentosXcentroCosto_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_itemDocumentosXcentroCosto_m]
GO
CREATE PROCEDURE [sel_doc_itemDocumentosXcentroCosto_m]
	@p_id_centroCosto INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_doc_itemDocumento]
	WHERE ([id_centroCosto]= @p_id_centroCosto)
	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_doc_itemDocumentosXcatalogo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_itemDocumentosXcatalogo_m]
GO
CREATE PROCEDURE [sel_doc_itemDocumentosXcatalogo_m]
	@p_id_catalogo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_doc_itemDocumento]
	WHERE ([id_catalogo]= @p_id_catalogo)
	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_doc_itemDocumentosXbodega_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_itemDocumentosXbodega_m]
GO
CREATE PROCEDURE [sel_doc_itemDocumentosXbodega_m]
	@p_id_bodega INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_doc_itemDocumento]
	WHERE ([id_bodega]= @p_id_bodega)
	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_doc_itemDocumentosXincoterm_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_itemDocumentosXincoterm_m]
GO
CREATE PROCEDURE [sel_doc_itemDocumentosXincoterm_m]
	@p_id_incoterm INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_doc_itemDocumento]
	WHERE ([id_incoterm]= @p_id_incoterm)
	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_doc_itemDocumentosXitemDocumento_superior_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_itemDocumentosXitemDocumento_superior_m]
GO
CREATE PROCEDURE [sel_doc_itemDocumentosXitemDocumento_superior_m]
	@p_id_itemDocumento_superior INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_doc_itemDocumento]
	WHERE ([id_itemDocumento_superior]= @p_id_itemDocumento_superior)
	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_doc_itemDocumentosXlistaprecio_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_itemDocumentosXlistaprecio_m]
GO
CREATE PROCEDURE [sel_doc_itemDocumentosXlistaprecio_m]
	@p_id_listaprecio INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_doc_itemDocumento]
	WHERE ([id_listaprecio]= @p_id_listaprecio)
	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_doc_itemDocumentos_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_itemDocumentos_m]
GO
CREATE PROCEDURE [sel_doc_itemDocumentos_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_doc_itemDocumento]
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_doc_itemDocumentos_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_doc_itemDocumentos_m]
GO
CREATE PROCEDURE [lis_doc_itemDocumentos_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[itemDocumento] AS [doc_itemDocumento]
	FROM [doc_itemDocumento]
	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_doc_itemDocumento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_doc_itemDocumento]
GO
CREATE PROCEDURE [aud_doc_itemDocumento]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [doc_itemDocumento].id as id_tabla,
	[doc_itemDocumento].id_usuario_modifico as id_usuario_modifico,
	[doc_itemDocumento].id_usuario_creo as id_usuario_creo,
	[doc_itemDocumento].fechaModificacion as fechaModificacion,
	[doc_itemDocumento].fechaCreacion as fechaCreacion,
	[doc_itemDocumento].active as active,
	[doc_itemDocumento].[itemDocumento] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [doc_itemDocumento]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[doc_itemDocumento].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[doc_itemDocumento].[id_usuario_creo]
	where ' + @p_filtro)
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_detalleAlquileresXitemDocumento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_detalleAlquileresXitemDocumento]
GO
CREATE PROCEDURE [sel_equ_detalleAlquileresXitemDocumento]
	@p_id_doc_itemDocumento INT
AS
BEGIN
	SELECT 	[alq_detallealquiler_itemdocumento].[id] AS [I_id],
		[alq_detallealquiler_itemdocumento].[uid] AS [I_uid],
		[alq_detallealquiler_itemdocumento].[eid] AS [I_eid],
		[alq_detallealquiler_itemdocumento].[fechaModificacion] AS [I_fechaModificacion],
		[alq_detallealquiler_itemdocumento].[fechaCreacion] AS [I_fechaCreacion],
		[alq_detallealquiler_itemdocumento].[active] AS [I_active],
		[alq_detallealquiler_itemdocumento].[detallealquiler_itemdocumento] AS [I_detallealquiler_itemdocumento]
	FROM alq_detallealquiler_itemdocumento INNER JOIN equ_detalleAlquiler
	 ON [alq_detallealquiler_itemdocumento].[id_detalleAlquiler] = [equ_detalleAlquiler].id
	WHERE	([alq_detallealquiler_itemdocumento].[id_itemDocumento]= @p_id_doc_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].[ins_alq_detallealquiler_itemdocumento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_alq_detallealquiler_itemdocumento]
GO
CREATE PROCEDURE [ins_alq_detallealquiler_itemdocumento]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_detallealquiler_itemdocumento varchar(300),
		@p_id_detallealquiler int,
		@p_id_itemdocumento int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [alq_detallealquiler_itemdocumento]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[detallealquiler_itemdocumento],
			[id_detallealquiler],
			[id_itemdocumento])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_detallealquiler_itemdocumento,
			@p_id_detallealquiler,
			@p_id_itemdocumento)
		SET @v_id = scope_identity()
UPDATE [alq_detallealquiler_itemdocumento] 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].[upd_alq_detallealquiler_itemdocumento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_alq_detallealquiler_itemdocumento]
GO
CREATE PROCEDURE [upd_alq_detallealquiler_itemdocumento]
	@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_itemdocumento varchar(300) = null,
		@p_id_detallealquiler int = null,
		@p_id_itemdocumento int = null
AS
BEGIN
		UPDATE [alq_detallealquiler_itemdocumento]
		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_itemdocumento] = isnull(@p_detallealquiler_itemdocumento,[detallealquiler_itemdocumento]),
			[id_detallealquiler] = isnull(@p_id_detallealquiler,[id_detallealquiler]),
			[id_itemdocumento] = isnull(@p_id_itemdocumento,[id_itemdocumento])
		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].[sel_gen_localizacionesXitemDocumento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_localizacionesXitemDocumento]
GO
CREATE PROCEDURE [sel_gen_localizacionesXitemDocumento]
	@p_id_doc_itemDocumento INT
AS
BEGIN
	SELECT 	[gen_localizacion_itemDocumento].[id] AS [I_id],
		[gen_localizacion_itemDocumento].[uid] AS [I_uid],
		[gen_localizacion_itemDocumento].[eid] AS [I_eid],
		[gen_localizacion_itemDocumento].[fechaModificacion] AS [I_fechaModificacion],
		[gen_localizacion_itemDocumento].[fechaCreacion] AS [I_fechaCreacion],
		[gen_localizacion_itemDocumento].[active] AS [I_active],
		[gen_localizacion_itemDocumento].[localizacion_itemDocumento] AS [I_localizacion_itemDocumento],
		[gen_localizacion_itemDocumento].[cantidad] AS [I_cantidad]
	FROM gen_localizacion_itemDocumento INNER JOIN gen_localizacion
	 ON [gen_localizacion_itemDocumento].[id_localizacion] = [gen_localizacion].id
	WHERE	([gen_localizacion_itemDocumento].[id_itemDocumento]= @p_id_doc_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].[ins_gen_localizacion_itemDocumento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_gen_localizacion_itemDocumento]
GO
CREATE PROCEDURE [ins_gen_localizacion_itemDocumento]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_localizacion_itemDocumento varchar(300),
		@p_id_localizacion int,
		@p_id_itemDocumento int,
		@p_cantidad int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [gen_localizacion_itemDocumento]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[localizacion_itemDocumento],
			[id_localizacion],
			[id_itemDocumento],
			[cantidad])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_localizacion_itemDocumento,
			@p_id_localizacion,
			@p_id_itemDocumento,
			@p_cantidad)
		SET @v_id = scope_identity()
UPDATE [gen_localizacion_itemDocumento] 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].[upd_gen_localizacion_itemDocumento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_gen_localizacion_itemDocumento]
GO
CREATE PROCEDURE [upd_gen_localizacion_itemDocumento]
	@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_localizacion_itemDocumento varchar(300) = null,
		@p_id_localizacion int = null,
		@p_id_itemDocumento int = null,
		@p_cantidad int = null
AS
BEGIN
		UPDATE [gen_localizacion_itemDocumento]
		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(),
			[localizacion_itemDocumento] = isnull(@p_localizacion_itemDocumento,[localizacion_itemDocumento]),
			[id_localizacion] = isnull(@p_id_localizacion,[id_localizacion]),
			[id_itemDocumento] = isnull(@p_id_itemDocumento,[id_itemDocumento]),
			[cantidad] = isnull(@p_cantidad,[cantidad])
		WHERE (id = @p_id)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'doc_itemDocumento'
-----------------------------------07/04/2016 14:36:23