

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_ter_sucursal_listaprecio]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_ter_sucursal_listaprecio]
GO
CREATE PROCEDURE [reg_ter_sucursal_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],
		[sucursal_listaprecio] AS [sucursal_listaprecio],
		[id_sucursal] AS [id_sucursal],
		[id_listaprecio] AS [id_listaprecio]
	FROM [ter_sucursal_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_ter_sucursal_listaprecio]')) DROP VIEW [dbo].[view_ter_sucursal_listaprecio]
GO
CREATE  VIEW [view_ter_sucursal_listaprecio] AS
SELECT 	[ter_sucursal_listaprecio].[id] AS [id],
		[ter_sucursal_listaprecio].[uid] AS [uid],
		[ter_sucursal_listaprecio].[eid] AS [eid],
		[ter_sucursal_listaprecio].[id_usuario_modifico] AS [id_usuario_modifico],
		[ter_sucursal_listaprecio].[id_usuario_creo] AS [id_usuario_creo],
		[ter_sucursal_listaprecio].[fechaModificacion] AS [fechaModificacion],
		[ter_sucursal_listaprecio].[fechaCreacion] AS [fechaCreacion],
		[ter_sucursal_listaprecio].[active] AS [active],
		[ter_sucursal_listaprecio].[sucursal_listaprecio] AS [sucursal_listaprecio],
		[ter_sucursal_listaprecio].[id_sucursal] AS [id_sucursal],
[ter_sucursal].[sucursal] as [ter_sucursal_sucursal],
[ter_sucursal].[sucursal_codigo] as [ter_sucursal_sucursal_codigo],
[ter_sucursal].[contacto] as [ter_sucursal_contacto],
[ter_sucursal].[telefono] as [ter_sucursal_telefono],
[ter_sucursal].[Fax] as [ter_sucursal_Fax],
[ter_sucursal].[Email] as [ter_sucursal_Email],
[ter_sucursal].[Direccion] as [ter_sucursal_Direccion],
[ter_sucursal].[paraVenta] as [ter_sucursal_paraVenta],
[ter_sucursal].[paraSoporte] as [ter_sucursal_paraSoporte],
[ter_sucursal].[paraAlquiler] as [ter_sucursal_paraAlquiler],
[ter_sucursal].[id_tercero] as [ter_sucursal_id_tercero],
[ter_sucursal].[id_zona] as [ter_sucursal_id_zona],
[ter_sucursal].[id_usuario_asesor] as [ter_sucursal_id_usuario_asesor],
[ter_sucursal].[id_usuario_tecnico] as [ter_sucursal_id_usuario_tecnico],
[ter_sucursal].[id_sucursal] as [ter_sucursal_id_sucursal],
[ter_sucursal].[id_estadoTercero] as [ter_sucursal_id_estadoTercero],
[ter_sucursal].[codigorecursiva] as [ter_sucursal_codigorecursiva],
		[ter_sucursal_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 [ter_sucursal_listaprecio]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[ter_sucursal_listaprecio].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[ter_sucursal_listaprecio].[id_usuario_creo]
	INNER JOIN [ter_sucursal] ON [ter_sucursal].id=[ter_sucursal_listaprecio].[id_sucursal]
	INNER JOIN [cat_listaPrecio] ON [cat_listaPrecio].id=[ter_sucursal_listaprecio].[id_listaprecio]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [ter_sucursal_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_ter_sucursal_listapreciosXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_sucursal_listapreciosXusuario_modifico]
GO
CREATE PROCEDURE [sel_ter_sucursal_listapreciosXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_ter_sucursal_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_ter_sucursal_listapreciosXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_sucursal_listapreciosXusuario_creo]
GO
CREATE PROCEDURE [sel_ter_sucursal_listapreciosXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_ter_sucursal_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_ter_sucursal_listapreciosXsucursal]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_sucursal_listapreciosXsucursal]
GO
CREATE PROCEDURE [sel_ter_sucursal_listapreciosXsucursal]
	@p_id_sucursal INT
AS
BEGIN
	SELECT * 
	FROM [view_ter_sucursal_listaprecio]
	WHERE ([id_sucursal]= @p_id_sucursal)
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_ter_sucursal_listapreciosXlistaprecio]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_sucursal_listapreciosXlistaprecio]
GO
CREATE PROCEDURE [sel_ter_sucursal_listapreciosXlistaprecio]
	@p_id_listaprecio INT
AS
BEGIN
	SELECT * 
	FROM [view_ter_sucursal_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_ter_sucursal_listaprecios]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_sucursal_listaprecios]
GO
CREATE PROCEDURE [sel_ter_sucursal_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_ter_sucursal_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_ter_sucursal_listaprecios]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_ter_sucursal_listaprecios]
GO
CREATE PROCEDURE [lis_ter_sucursal_listaprecios]
	AS
BEGIN
	SELECT id AS id,
[sucursal_listaprecio] AS [ter_sucursal_listaprecio]
	FROM [ter_sucursal_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_ter_sucursal_listaprecio]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_ter_sucursal_listaprecio]
GO
CREATE PROCEDURE [upd_ter_sucursal_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_sucursal_listaprecio varchar(300) = null,
		@p_id_sucursal int = null,
		@p_id_listaprecio int = null
AS
BEGIN
		UPDATE [ter_sucursal_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(),
			[sucursal_listaprecio] = isnull(@p_sucursal_listaprecio,[sucursal_listaprecio]),
			[id_sucursal] = isnull(@p_id_sucursal,[id_sucursal]),
			[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_ter_sucursal_listaprecio]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_ter_sucursal_listaprecio]
GO
CREATE PROCEDURE [ins_ter_sucursal_listaprecio]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_sucursal_listaprecio varchar(300),
		@p_id_sucursal int,
		@p_id_listaprecio int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [ter_sucursal_listaprecio]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[sucursal_listaprecio],
			[id_sucursal],
			[id_listaprecio])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_sucursal_listaprecio,
			@p_id_sucursal,
			@p_id_listaprecio)
		SET @v_id = scope_identity()
UPDATE [ter_sucursal_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_ter_sucursal_listaprecio]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_ter_sucursal_listaprecio]
GO
CREATE PROCEDURE [act_ter_sucursal_listaprecio]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [ter_sucursal_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_ter_sucursal_listaprecio_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_ter_sucursal_listaprecio_m]
GO
CREATE PROCEDURE [reg_ter_sucursal_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],
		[sucursal_listaprecio] AS [sucursal_listaprecio],
		[id_sucursal] AS [id_sucursal],
		[id_listaprecio] AS [id_listaprecio]
	FROM [ter_sucursal_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_ter_sucursal_listapreciosXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_sucursal_listapreciosXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_ter_sucursal_listapreciosXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ter_sucursal_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_ter_sucursal_listapreciosXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_sucursal_listapreciosXusuario_creo_m]
GO
CREATE PROCEDURE [sel_ter_sucursal_listapreciosXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ter_sucursal_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_ter_sucursal_listapreciosXsucursal_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_sucursal_listapreciosXsucursal_m]
GO
CREATE PROCEDURE [sel_ter_sucursal_listapreciosXsucursal_m]
	@p_id_sucursal INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ter_sucursal_listaprecio]
	WHERE ([id_sucursal]= @p_id_sucursal)
	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_ter_sucursal_listapreciosXlistaprecio_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_sucursal_listapreciosXlistaprecio_m]
GO
CREATE PROCEDURE [sel_ter_sucursal_listapreciosXlistaprecio_m]
	@p_id_listaprecio INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ter_sucursal_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_ter_sucursal_listaprecios_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_sucursal_listaprecios_m]
GO
CREATE PROCEDURE [sel_ter_sucursal_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_ter_sucursal_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_ter_sucursal_listaprecios_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_ter_sucursal_listaprecios_m]
GO
CREATE PROCEDURE [lis_ter_sucursal_listaprecios_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[sucursal_listaprecio] AS [ter_sucursal_listaprecio]
	FROM [ter_sucursal_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_ter_sucursal_listaprecio]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_ter_sucursal_listaprecio]
GO
CREATE PROCEDURE [aud_ter_sucursal_listaprecio]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [ter_sucursal_listaprecio].id as id_tabla,
	[ter_sucursal_listaprecio].id_usuario_modifico as id_usuario_modifico,
	[ter_sucursal_listaprecio].id_usuario_creo as id_usuario_creo,
	[ter_sucursal_listaprecio].fechaModificacion as fechaModificacion,
	[ter_sucursal_listaprecio].fechaCreacion as fechaCreacion,
	[ter_sucursal_listaprecio].active as active,
	[ter_sucursal_listaprecio].[sucursal_listaprecio] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

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