
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_gen_localizacion]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_gen_localizacion]
GO
CREATE PROCEDURE [reg_gen_localizacion]
	@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],
		[localizacion] AS [localizacion],
		[localizacion_codigo] AS [localizacion_codigo],
		[id_catalogo_bodega] AS [id_catalogo_bodega],
		[existencia] AS [existencia],
		[existenciaMaxima] AS [existenciaMaxima]
	FROM [gen_localizacion]
	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_gen_localizacion]')) DROP VIEW [dbo].[view_gen_localizacion]
GO
CREATE  VIEW [view_gen_localizacion] AS
SELECT 	[gen_localizacion].[id] AS [id],
		[gen_localizacion].[uid] AS [uid],
		[gen_localizacion].[eid] AS [eid],
		[gen_localizacion].[id_usuario_modifico] AS [id_usuario_modifico],
		[gen_localizacion].[id_usuario_creo] AS [id_usuario_creo],
		[gen_localizacion].[fechaModificacion] AS [fechaModificacion],
		[gen_localizacion].[fechaCreacion] AS [fechaCreacion],
		[gen_localizacion].[active] AS [active],
		[gen_localizacion].[localizacion] AS [localizacion],
		[gen_localizacion].[localizacion_codigo] AS [localizacion_codigo],
		[gen_localizacion].[id_catalogo_bodega] AS [id_catalogo_bodega],
		[cat_catalogo_bodega].[disponible],
		[cat_catalogo_bodega].[reservado],
		[cat_catalogo_bodega].[enOrdenes],
		[cat_catalogo_bodega].[costo],
		[cat_catalogo_bodega].[existenciaMinima],
		[cat_catalogo_bodega].[id_bodega],
[cat_catalogo].[catalogo] as [cat_catalogo_bodega_catalogo],
[cat_catalogo].[catalogo_codigo] as [cat_catalogo_bodega_catalogo_codigo],
[cat_catalogo].[codigoInventario] as [cat_catalogo_bodega_codigoInventario],
[cat_catalogo].[precioVenta] as [cat_catalogo_bodega_precioVenta],
[cat_catalogo].[tiempoConsecucion] as [cat_catalogo_bodega_tiempoConsecucion],
[cat_catalogo].[esNacional] as [cat_catalogo_bodega_esNacional],
[cat_catalogo].[id_familia] as [cat_catalogo_bodega_id_familia],
[cat_catalogo].[id_subTipoCatalogo] as [cat_catalogo_bodega_id_subTipoCatalogo],
[cat_catalogo].[id_unidad] as [cat_catalogo_bodega_id_unidad],
[cat_catalogo].[id_impuesto] as [cat_catalogo_bodega_id_impuesto],
[cat_catalogo].[descripcion] as [cat_catalogo_bodega_descripcion],
[cat_catalogo].[bloquearCostear] as [cat_catalogo_bodega_bloquearCostear],
		[gen_localizacion].[existencia] AS [existencia],
		[gen_localizacion].[existenciaMaxima] AS [existenciaMaxima],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [gen_localizacion]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[gen_localizacion].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[gen_localizacion].[id_usuario_creo]
	INNER JOIN [cat_catalogo_bodega] AS [cat_catalogo_bodega] ON [cat_catalogo_bodega].id=[gen_localizacion].[id_catalogo_bodega]
	INNER JOIN [cat_catalogo] AS [cat_catalogo] ON [cat_catalogo_bodega].id_catalogo=[cat_catalogo].[id]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [gen_localizacion].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [gen_localizacion].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_gen_localizacionesXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_localizacionesXusuario_modifico]
GO
CREATE PROCEDURE [sel_gen_localizacionesXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_gen_localizacion]
	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_gen_localizacionesXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_localizacionesXusuario_creo]
GO
CREATE PROCEDURE [sel_gen_localizacionesXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_gen_localizacion]
	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_gen_localizacionesXcatalogo_bodega]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_localizacionesXcatalogo_bodega]
GO
CREATE PROCEDURE [sel_gen_localizacionesXcatalogo_bodega]
	@p_id_catalogo_bodega INT
AS
BEGIN
	SELECT * 
	FROM [view_gen_localizacion]
	WHERE ([id_catalogo_bodega]= @p_id_catalogo_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_gen_localizaciones]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_localizaciones]
GO
CREATE PROCEDURE [sel_gen_localizaciones]
	
	@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_gen_localizacion]
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_gen_localizaciones]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_gen_localizaciones]
GO
CREATE PROCEDURE [lis_gen_localizaciones]
	AS
BEGIN
	SELECT id AS id,
[localizacion] AS [gen_localizacion]
	FROM [gen_localizacion]
	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_gen_localizacion]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_gen_localizacion]
GO
CREATE PROCEDURE [upd_gen_localizacion]
	@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 varchar(300) = null,
		@p_localizacion_codigo varchar(100) = null,
		@p_id_catalogo_bodega int = null,
		@p_existencia int = null,
		@p_existenciaMaxima int = null
AS
BEGIN
		UPDATE [gen_localizacion]
		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] = isnull(@p_localizacion,[localizacion]),
			[localizacion_codigo] = isnull(@p_localizacion_codigo,[localizacion_codigo]),
			[id_catalogo_bodega] = isnull(@p_id_catalogo_bodega,[id_catalogo_bodega]),
			[existencia] = isnull(@p_existencia,[existencia]),
			[existenciaMaxima] = isnull(@p_existenciaMaxima,[existenciaMaxima])
		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_gen_localizacion]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_gen_localizacion]
GO
CREATE PROCEDURE [ins_gen_localizacion]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_localizacion varchar(300),
		@p_localizacion_codigo varchar(100),
		@p_id_catalogo_bodega int,
		@p_existencia int,
		@p_existenciaMaxima int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [gen_localizacion]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[localizacion],
			[localizacion_codigo],
			[id_catalogo_bodega],
			[existencia],
			[existenciaMaxima])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_localizacion,
			@p_localizacion_codigo,
			@p_id_catalogo_bodega,
			@p_existencia,
			@p_existenciaMaxima)
		SET @v_id = scope_identity()
UPDATE [gen_localizacion] 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_gen_localizacion]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_gen_localizacion]
GO
CREATE PROCEDURE [act_gen_localizacion]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [gen_localizacion]
	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_gen_localizacion_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_gen_localizacion_m]
GO
CREATE PROCEDURE [reg_gen_localizacion_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],
		[localizacion] AS [localizacion],
		[localizacion_codigo] AS [localizacion_codigo],
		[id_catalogo_bodega] AS [id_catalogo_bodega],
		[existencia] AS [existencia],
		[existenciaMaxima] AS [existenciaMaxima]
	FROM [gen_localizacion]
	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_gen_localizacionesXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_localizacionesXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_gen_localizacionesXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_gen_localizacion]
	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_gen_localizacionesXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_localizacionesXusuario_creo_m]
GO
CREATE PROCEDURE [sel_gen_localizacionesXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_gen_localizacion]
	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_gen_localizacionesXcatalogo_bodega_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_localizacionesXcatalogo_bodega_m]
GO
CREATE PROCEDURE [sel_gen_localizacionesXcatalogo_bodega_m]
	@p_id_catalogo_bodega INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_gen_localizacion]
	WHERE ([id_catalogo_bodega]= @p_id_catalogo_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_gen_localizaciones_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_localizaciones_m]
GO
CREATE PROCEDURE [sel_gen_localizaciones_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_gen_localizacion]
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_gen_localizaciones_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_gen_localizaciones_m]
GO
CREATE PROCEDURE [lis_gen_localizaciones_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[localizacion] AS [gen_localizacion]
	FROM [gen_localizacion]
	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_gen_localizacion]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_gen_localizacion]
GO
CREATE PROCEDURE [aud_gen_localizacion]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [gen_localizacion].id as id_tabla,
	[gen_localizacion].id_usuario_modifico as id_usuario_modifico,
	[gen_localizacion].id_usuario_creo as id_usuario_creo,
	[gen_localizacion].fechaModificacion as fechaModificacion,
	[gen_localizacion].fechaCreacion as fechaCreacion,
	[gen_localizacion].active as active,
	[gen_localizacion].[localizacion] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [gen_localizacion]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[gen_localizacion].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[gen_localizacion].[id_usuario_creo]
	where ' + @p_filtro)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'gen_localizacion'
-----------------------------------18/06/2013 02:14:03 p.m.