
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_bodega_usuario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_gen_bodega_usuario]
GO
CREATE PROCEDURE [reg_gen_bodega_usuario]
	@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],
		[bodega_usuario] AS [bodega_usuario],
		[id_usuario] AS [id_usuario],
		[id_bodega] AS [id_bodega],
		[sugerir] AS [sugerir]
	FROM [gen_bodega_usuario]
	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_bodega_usuario]')) DROP VIEW [dbo].[view_gen_bodega_usuario]
GO
CREATE  VIEW [view_gen_bodega_usuario] AS
SELECT 	[gen_bodega_usuario].[id] AS [id],
		[gen_bodega_usuario].[uid] AS [uid],
		[gen_bodega_usuario].[eid] AS [eid],
		[gen_bodega_usuario].[id_usuario_modifico] AS [id_usuario_modifico],
		[gen_bodega_usuario].[id_usuario_creo] AS [id_usuario_creo],
		[gen_bodega_usuario].[fechaModificacion] AS [fechaModificacion],
		[gen_bodega_usuario].[fechaCreacion] AS [fechaCreacion],
		[gen_bodega_usuario].[active] AS [active],
		[gen_bodega_usuario].[bodega_usuario] AS [bodega_usuario],
		[gen_bodega_usuario].[id_usuario] AS [id_usuario],
[seg_usuario].[usuario] as [seg_usuario_usuario],
[seg_usuario].[usuario_codigo] as [seg_usuario_usuario_codigo],
[seg_usuario].[intentosFallidos] as [seg_usuario_intentosFallidos],
[seg_usuario].[numeroIngresos] as [seg_usuario_numeroIngresos],
[seg_usuario].[fechaUltimoIngreso_fh] as [seg_usuario_fechaUltimoIngreso_fh],
[seg_usuario].[fechaIntentoFallido_fh] as [seg_usuario_fechaIntentoFallido_fh],
[seg_usuario].[clave] as [seg_usuario_clave],
[seg_usuario].[nombre] as [seg_usuario_nombre],
[seg_usuario].[profesion] as [seg_usuario_profesion],
[seg_usuario].[email] as [seg_usuario_email],
[seg_usuario].[direccion] as [seg_usuario_direccion],
[seg_usuario].[telefono] as [seg_usuario_telefono],
[seg_usuario].[costoHora] as [seg_usuario_costoHora],
[seg_usuario].[porcentajeComision] as [seg_usuario_porcentajeComision],
[seg_usuario].[accesoActivo] as [seg_usuario_accesoActivo],
[seg_usuario].[cambiarClave] as [seg_usuario_cambiarClave],
[seg_usuario].[fechaUltimoCambioClave_fh] as [seg_usuario_fechaUltimoCambioClave_fh],
[seg_usuario].[aplicaRecargo] as [seg_usuario_aplicaRecargo],
[seg_usuario].[sexo] as [seg_usuario_sexo],
[seg_usuario].[id_perfil] as [seg_usuario_id_perfil],
[seg_usuario].[id_cargo] as [seg_usuario_id_cargo],
[seg_usuario].[id_grupo] as [seg_usuario_id_grupo],
[seg_usuario].[id_zona] as [seg_usuario_id_zona],
[seg_usuario].[identificacion] as [seg_usuario_identificacion],
[seg_usuario].[id_usuario] as [seg_usuario_id_usuario],
[seg_usuario].[preferencias] as [seg_usuario_preferencias],
		[gen_bodega_usuario].[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_usuario].[sugerir] AS [sugerir],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [gen_bodega_usuario]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[gen_bodega_usuario].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[gen_bodega_usuario].[id_usuario_creo]
	INNER JOIN [seg_usuario] ON [seg_usuario].id=[gen_bodega_usuario].[id_usuario]
	INNER JOIN [gen_bodega] ON [gen_bodega].id=[gen_bodega_usuario].[id_bodega]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [gen_bodega_usuario].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_gen_bodega_usuariosXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_bodega_usuariosXusuario_modifico]
GO
CREATE PROCEDURE [sel_gen_bodega_usuariosXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_gen_bodega_usuario]
	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_bodega_usuariosXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_bodega_usuariosXusuario_creo]
GO
CREATE PROCEDURE [sel_gen_bodega_usuariosXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_gen_bodega_usuario]
	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_bodega_usuariosXusuario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_bodega_usuariosXusuario]
GO
CREATE PROCEDURE [sel_gen_bodega_usuariosXusuario]
	@p_id_usuario INT
AS
BEGIN
	SELECT * 
	FROM [view_gen_bodega_usuario]
	WHERE ([id_usuario]= @p_id_usuario)
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_bodega_usuariosXbodega]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_bodega_usuariosXbodega]
GO
CREATE PROCEDURE [sel_gen_bodega_usuariosXbodega]
	@p_id_bodega INT
AS
BEGIN
	SELECT * 
	FROM [view_gen_bodega_usuario]
	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_gen_bodega_usuarios]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_bodega_usuarios]
GO
CREATE PROCEDURE [sel_gen_bodega_usuarios]
	
	@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_bodega_usuario]
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_bodega_usuarios]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_gen_bodega_usuarios]
GO
CREATE PROCEDURE [lis_gen_bodega_usuarios]
	AS
BEGIN
	SELECT id AS id,
[bodega_usuario] AS [gen_bodega_usuario]
	FROM [gen_bodega_usuario]
	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_bodega_usuario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_gen_bodega_usuario]
GO
CREATE PROCEDURE [upd_gen_bodega_usuario]
	@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_bodega_usuario varchar(300) = null,
		@p_id_usuario int = null,
		@p_id_bodega int = null,
		@p_sugerir bit = null
AS
BEGIN
		UPDATE [gen_bodega_usuario]
		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(),
			[bodega_usuario] = isnull(@p_bodega_usuario,[bodega_usuario]),
			[id_usuario] = isnull(@p_id_usuario,[id_usuario]),
			[id_bodega] = isnull(@p_id_bodega,[id_bodega]),
			[sugerir] = isnull(@p_sugerir,[sugerir])
		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_bodega_usuario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_gen_bodega_usuario]
GO
CREATE PROCEDURE [ins_gen_bodega_usuario]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_bodega_usuario varchar(300),
		@p_id_usuario int,
		@p_id_bodega int,
		@p_sugerir bit
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [gen_bodega_usuario]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[bodega_usuario],
			[id_usuario],
			[id_bodega],
			[sugerir])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_bodega_usuario,
			@p_id_usuario,
			@p_id_bodega,
			@p_sugerir)
		SET @v_id = scope_identity()
UPDATE [gen_bodega_usuario] 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_bodega_usuario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_gen_bodega_usuario]
GO
CREATE PROCEDURE [act_gen_bodega_usuario]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [gen_bodega_usuario]
	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_bodega_usuario_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_gen_bodega_usuario_m]
GO
CREATE PROCEDURE [reg_gen_bodega_usuario_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],
		[bodega_usuario] AS [bodega_usuario],
		[id_usuario] AS [id_usuario],
		[id_bodega] AS [id_bodega],
		[sugerir] AS [sugerir]
	FROM [gen_bodega_usuario]
	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_bodega_usuariosXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_bodega_usuariosXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_gen_bodega_usuariosXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_gen_bodega_usuario]
	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_bodega_usuariosXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_bodega_usuariosXusuario_creo_m]
GO
CREATE PROCEDURE [sel_gen_bodega_usuariosXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_gen_bodega_usuario]
	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_bodega_usuariosXusuario_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_bodega_usuariosXusuario_m]
GO
CREATE PROCEDURE [sel_gen_bodega_usuariosXusuario_m]
	@p_id_usuario INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_gen_bodega_usuario]
	WHERE ([id_usuario]= @p_id_usuario)
	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_bodega_usuariosXbodega_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_bodega_usuariosXbodega_m]
GO
CREATE PROCEDURE [sel_gen_bodega_usuariosXbodega_m]
	@p_id_bodega INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_gen_bodega_usuario]
	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_gen_bodega_usuarios_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_bodega_usuarios_m]
GO
CREATE PROCEDURE [sel_gen_bodega_usuarios_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_bodega_usuario]
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_bodega_usuarios_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_gen_bodega_usuarios_m]
GO
CREATE PROCEDURE [lis_gen_bodega_usuarios_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[bodega_usuario] AS [gen_bodega_usuario]
	FROM [gen_bodega_usuario]
	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_bodega_usuario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_gen_bodega_usuario]
GO
CREATE PROCEDURE [aud_gen_bodega_usuario]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [gen_bodega_usuario].id as id_tabla,
	[gen_bodega_usuario].id_usuario_modifico as id_usuario_modifico,
	[gen_bodega_usuario].id_usuario_creo as id_usuario_creo,
	[gen_bodega_usuario].fechaModificacion as fechaModificacion,
	[gen_bodega_usuario].fechaCreacion as fechaCreacion,
	[gen_bodega_usuario].active as active,
	[gen_bodega_usuario].[bodega_usuario] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [gen_bodega_usuario]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[gen_bodega_usuario].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[gen_bodega_usuario].[id_usuario_creo]
	where ' + @p_filtro)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'gen_bodega_usuario'
-----------------------------------18/10/2012 10:22:57 a.m.