

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_cat_catalogo_listaPrecio]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_cat_catalogo_listaPrecio]
GO
CREATE PROCEDURE [reg_cat_catalogo_listaPrecio]
	@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],
		[catalogo_listaPrecio] AS [catalogo_listaPrecio],
		[PrecioVenta] AS [PrecioVenta],
		[id_catalogo] AS [id_catalogo],
		[id_listaPrecio] AS [id_listaPrecio]
	FROM [cat_catalogo_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 ON
GO
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[view_cat_catalogo_listaPrecio]')) DROP VIEW [dbo].[view_cat_catalogo_listaPrecio]
GO
CREATE  VIEW [view_cat_catalogo_listaPrecio] AS
SELECT 	[cat_catalogo_listaPrecio].[id] AS [id],
		[cat_catalogo_listaPrecio].[uid] AS [uid],
		[cat_catalogo_listaPrecio].[eid] AS [eid],
		[cat_catalogo_listaPrecio].[id_usuario_modifico] AS [id_usuario_modifico],
		[cat_catalogo_listaPrecio].[id_usuario_creo] AS [id_usuario_creo],
		[cat_catalogo_listaPrecio].[fechaModificacion] AS [fechaModificacion],
		[cat_catalogo_listaPrecio].[fechaCreacion] AS [fechaCreacion],
		[cat_catalogo_listaPrecio].[active] AS [active],
		[cat_catalogo_listaPrecio].[catalogo_listaPrecio] AS [catalogo_listaPrecio],
		[cat_catalogo_listaPrecio].[PrecioVenta] AS [PrecioVenta],
		[cat_catalogo_listaPrecio].[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],
		[cat_catalogo_listaPrecio].[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 [cat_catalogo_listaPrecio]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[cat_catalogo_listaPrecio].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[cat_catalogo_listaPrecio].[id_usuario_creo]
	INNER JOIN [cat_catalogo] ON [cat_catalogo].id=[cat_catalogo_listaPrecio].[id_catalogo]
	INNER JOIN [cat_listaPrecio] ON [cat_listaPrecio].id=[cat_catalogo_listaPrecio].[id_listaPrecio]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [cat_catalogo_listaPrecio].eid = [empresaCodigo].[codigo] and empresaCodigo.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_cat_catalogo_listaPreciosXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_catalogo_listaPreciosXusuario_modifico]
GO
CREATE PROCEDURE [sel_cat_catalogo_listaPreciosXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_cat_catalogo_listaPrecio]
	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_cat_catalogo_listaPreciosXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_catalogo_listaPreciosXusuario_creo]
GO
CREATE PROCEDURE [sel_cat_catalogo_listaPreciosXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_cat_catalogo_listaPrecio]
	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_cat_catalogo_listaPreciosXcatalogo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_catalogo_listaPreciosXcatalogo]
GO
CREATE PROCEDURE [sel_cat_catalogo_listaPreciosXcatalogo]
	@p_id_catalogo INT
AS
BEGIN
	SELECT * 
	FROM [view_cat_catalogo_listaPrecio]
	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_cat_catalogo_listaPreciosXlistaPrecio]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_catalogo_listaPreciosXlistaPrecio]
GO
CREATE PROCEDURE [sel_cat_catalogo_listaPreciosXlistaPrecio]
	@p_id_listaPrecio INT
AS
BEGIN
	SELECT * 
	FROM [view_cat_catalogo_listaPrecio]
	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_cat_catalogo_listaPrecios]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_catalogo_listaPrecios]
GO
CREATE PROCEDURE [sel_cat_catalogo_listaPrecios]
	
	@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_cat_catalogo_listaPrecio]
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_cat_catalogo_listaPrecios]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_cat_catalogo_listaPrecios]
GO
CREATE PROCEDURE [lis_cat_catalogo_listaPrecios]
	AS
BEGIN
	SELECT id AS id,
[catalogo_listaPrecio] AS [cat_catalogo_listaPrecio]
	FROM [cat_catalogo_listaPrecio]
	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_cat_catalogo_listaPrecio]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_cat_catalogo_listaPrecio]
GO
CREATE PROCEDURE [upd_cat_catalogo_listaPrecio]
	@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_catalogo_listaPrecio varchar(300) = null,
		@p_PrecioVenta money = null,
		@p_id_catalogo int = null,
		@p_id_listaPrecio int = null
AS
BEGIN
		UPDATE [cat_catalogo_listaPrecio]
		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(),
			[catalogo_listaPrecio] = isnull(@p_catalogo_listaPrecio,[catalogo_listaPrecio]),
			[PrecioVenta] = isnull(@p_PrecioVenta,[PrecioVenta]),
			[id_catalogo] = isnull(@p_id_catalogo,[id_catalogo]),
			[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_cat_catalogo_listaPrecio]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_cat_catalogo_listaPrecio]
GO
CREATE PROCEDURE [ins_cat_catalogo_listaPrecio]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_catalogo_listaPrecio varchar(300),
		@p_PrecioVenta money,
		@p_id_catalogo int,
		@p_id_listaPrecio int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [cat_catalogo_listaPrecio]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[catalogo_listaPrecio],
			[PrecioVenta],
			[id_catalogo],
			[id_listaPrecio])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_catalogo_listaPrecio,
			@p_PrecioVenta,
			@p_id_catalogo,
			@p_id_listaPrecio)
		SET @v_id = scope_identity()
UPDATE [cat_catalogo_listaPrecio] 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_cat_catalogo_listaPrecio]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_cat_catalogo_listaPrecio]
GO
CREATE PROCEDURE [act_cat_catalogo_listaPrecio]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [cat_catalogo_listaPrecio]
	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_cat_catalogo_listaPrecio_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_cat_catalogo_listaPrecio_m]
GO
CREATE PROCEDURE [reg_cat_catalogo_listaPrecio_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],
		[catalogo_listaPrecio] AS [catalogo_listaPrecio],
		[PrecioVenta] AS [PrecioVenta],
		[id_catalogo] AS [id_catalogo],
		[id_listaPrecio] AS [id_listaPrecio]
	FROM [cat_catalogo_listaPrecio]
	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_cat_catalogo_listaPreciosXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_catalogo_listaPreciosXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_cat_catalogo_listaPreciosXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cat_catalogo_listaPrecio]
	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_cat_catalogo_listaPreciosXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_catalogo_listaPreciosXusuario_creo_m]
GO
CREATE PROCEDURE [sel_cat_catalogo_listaPreciosXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cat_catalogo_listaPrecio]
	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_cat_catalogo_listaPreciosXcatalogo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_catalogo_listaPreciosXcatalogo_m]
GO
CREATE PROCEDURE [sel_cat_catalogo_listaPreciosXcatalogo_m]
	@p_id_catalogo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cat_catalogo_listaPrecio]
	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_cat_catalogo_listaPreciosXlistaPrecio_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_catalogo_listaPreciosXlistaPrecio_m]
GO
CREATE PROCEDURE [sel_cat_catalogo_listaPreciosXlistaPrecio_m]
	@p_id_listaPrecio INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cat_catalogo_listaPrecio]
	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_cat_catalogo_listaPrecios_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_catalogo_listaPrecios_m]
GO
CREATE PROCEDURE [sel_cat_catalogo_listaPrecios_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_cat_catalogo_listaPrecio]
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_cat_catalogo_listaPrecios_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_cat_catalogo_listaPrecios_m]
GO
CREATE PROCEDURE [lis_cat_catalogo_listaPrecios_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[catalogo_listaPrecio] AS [cat_catalogo_listaPrecio]
	FROM [cat_catalogo_listaPrecio]
	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_cat_catalogo_listaPrecio]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_cat_catalogo_listaPrecio]
GO
CREATE PROCEDURE [aud_cat_catalogo_listaPrecio]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [cat_catalogo_listaPrecio].id as id_tabla,
	[cat_catalogo_listaPrecio].id_usuario_modifico as id_usuario_modifico,
	[cat_catalogo_listaPrecio].id_usuario_creo as id_usuario_creo,
	[cat_catalogo_listaPrecio].fechaModificacion as fechaModificacion,
	[cat_catalogo_listaPrecio].fechaCreacion as fechaCreacion,
	[cat_catalogo_listaPrecio].active as active,
	[cat_catalogo_listaPrecio].[catalogo_listaPrecio] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [cat_catalogo_listaPrecio]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[cat_catalogo_listaPrecio].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[cat_catalogo_listaPrecio].[id_usuario_creo]
	where ' + @p_filtro)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'cat_catalogo_listaPrecio'
-----------------------------------14/04/2016 11:03:32