

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]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_gen_bodega]
GO
CREATE PROCEDURE [reg_gen_bodega]
	@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] AS [bodega],
		[bodega_codigo] AS [bodega_codigo],
		[salidasEnRojo] AS [salidasEnRojo],
		[usaLocalizacion] AS [usaLocalizacion]
	FROM [gen_bodega]
	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]')) DROP VIEW [dbo].[view_gen_bodega]
GO
CREATE  VIEW [view_gen_bodega] AS
SELECT 	[gen_bodega].[id] AS [id],
		[gen_bodega].[uid] AS [uid],
		[gen_bodega].[eid] AS [eid],
		[gen_bodega].[id_usuario_modifico] AS [id_usuario_modifico],
		[gen_bodega].[id_usuario_creo] AS [id_usuario_creo],
		[gen_bodega].[fechaModificacion] AS [fechaModificacion],
		[gen_bodega].[fechaCreacion] AS [fechaCreacion],
		[gen_bodega].[active] AS [active],
		[gen_bodega].[bodega] AS [bodega],
		[gen_bodega].[bodega_codigo] AS [bodega_codigo],
		[gen_bodega].[salidasEnRojo] AS [salidasEnRojo],
		[gen_bodega].[usaLocalizacion] AS [usaLocalizacion],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [gen_bodega]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[gen_bodega].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[gen_bodega].[id_usuario_creo]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [gen_bodega].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [gen_bodega].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_bodegasXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_bodegasXusuario_modifico]
GO
CREATE PROCEDURE [sel_gen_bodegasXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_gen_bodega]
	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_bodegasXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_bodegasXusuario_creo]
GO
CREATE PROCEDURE [sel_gen_bodegasXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_gen_bodega]
	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_bodegas]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_bodegas]
GO
CREATE PROCEDURE [sel_gen_bodegas]
	
	@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]
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_bodegas]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_gen_bodegas]
GO
CREATE PROCEDURE [lis_gen_bodegas]
	AS
BEGIN
	SELECT id AS id,
[bodega] AS [gen_bodega]
	FROM [gen_bodega]
	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]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_gen_bodega]
GO
CREATE PROCEDURE [upd_gen_bodega]
	@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 varchar(300) = null,
		@p_bodega_codigo varchar(100) = null,
		@p_salidasEnRojo bit = null,
		@p_usaLocalizacion bit = null
AS
BEGIN
		UPDATE [gen_bodega]
		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] = isnull(@p_bodega,[bodega]),
			[bodega_codigo] = isnull(@p_bodega_codigo,[bodega_codigo]),
			[salidasEnRojo] = isnull(@p_salidasEnRojo,[salidasEnRojo]),
			[usaLocalizacion] = isnull(@p_usaLocalizacion,[usaLocalizacion])
		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]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_gen_bodega]
GO
CREATE PROCEDURE [ins_gen_bodega]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_bodega varchar(300),
		@p_bodega_codigo varchar(100),
		@p_salidasEnRojo bit,
		@p_usaLocalizacion bit
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [gen_bodega]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[bodega],
			[bodega_codigo],
			[salidasEnRojo],
			[usaLocalizacion])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_bodega,
			@p_bodega_codigo,
			@p_salidasEnRojo,
			@p_usaLocalizacion)
		SET @v_id = scope_identity()
UPDATE [gen_bodega] 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]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_gen_bodega]
GO
CREATE PROCEDURE [act_gen_bodega]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [gen_bodega]
	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_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_gen_bodega_m]
GO
CREATE PROCEDURE [reg_gen_bodega_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] AS [bodega],
		[bodega_codigo] AS [bodega_codigo],
		[salidasEnRojo] AS [salidasEnRojo],
		[usaLocalizacion] AS [usaLocalizacion]
	FROM [gen_bodega]
	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_bodegasXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_bodegasXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_gen_bodegasXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_gen_bodega]
	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_bodegasXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_bodegasXusuario_creo_m]
GO
CREATE PROCEDURE [sel_gen_bodegasXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_gen_bodega]
	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_bodegas_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_gen_bodegas_m]
GO
CREATE PROCEDURE [sel_gen_bodegas_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]
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_bodegas_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_gen_bodegas_m]
GO
CREATE PROCEDURE [lis_gen_bodegas_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[bodega] AS [gen_bodega]
	FROM [gen_bodega]
	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]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_gen_bodega]
GO
CREATE PROCEDURE [aud_gen_bodega]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [gen_bodega].id as id_tabla,
	[gen_bodega].id_usuario_modifico as id_usuario_modifico,
	[gen_bodega].id_usuario_creo as id_usuario_creo,
	[gen_bodega].fechaModificacion as fechaModificacion,
	[gen_bodega].fechaCreacion as fechaCreacion,
	[gen_bodega].active as active,
	[gen_bodega].[bodega] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [gen_bodega]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[gen_bodega].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[gen_bodega].[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_cat_catalogosXbodega]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_catalogosXbodega]
GO
CREATE PROCEDURE [sel_cat_catalogosXbodega]
	@p_id_gen_bodega INT
AS
BEGIN
	SELECT 	[cat_catalogo_bodega].[id] AS [I_id],
		[cat_catalogo_bodega].[uid] AS [I_uid],
		[cat_catalogo_bodega].[eid] AS [I_eid],
		[cat_catalogo_bodega].[fechaModificacion] AS [I_fechaModificacion],
		[cat_catalogo_bodega].[fechaCreacion] AS [I_fechaCreacion],
		[cat_catalogo_bodega].[active] AS [I_active],
		[cat_catalogo_bodega].[catalogo_bodega] AS [I_catalogo_bodega],
		[cat_catalogo_bodega].[disponible] AS [I_disponible],
		[cat_catalogo_bodega].[existencia] AS [I_existencia],
		[cat_catalogo_bodega].[reservado] AS [I_reservado],
		[cat_catalogo_bodega].[existenciaMinima] AS [I_existenciaMinima],
		[cat_catalogo_bodega].[enOrdenes] AS [I_enOrdenes],
		[cat_catalogo_bodega].[localizacion] AS [I_localizacion],
		[cat_catalogo_bodega].[costo] AS [I_costo],
		[cat_catalogo_bodega].[existenciaMaxima] AS [I_existenciaMaxima]
	FROM cat_catalogo_bodega INNER JOIN cat_catalogo
	 ON [cat_catalogo_bodega].[id_catalogo] = [cat_catalogo].id
	WHERE	([cat_catalogo_bodega].[id_bodega]= @p_id_gen_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].[ins_cat_catalogo_bodega]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_cat_catalogo_bodega]
GO
CREATE PROCEDURE [ins_cat_catalogo_bodega]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_catalogo_bodega varchar(300),
		@p_disponible float,
		@p_existencia float,
		@p_reservado float,
		@p_existenciaMinima float,
		@p_enOrdenes float,
		@p_localizacion varchar(50),
		@p_costo money,
		@p_existenciaMaxima float,
		@p_id_catalogo int,
		@p_id_bodega int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [cat_catalogo_bodega]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[catalogo_bodega],
			[disponible],
			[existencia],
			[reservado],
			[existenciaMinima],
			[enOrdenes],
			[localizacion],
			[costo],
			[existenciaMaxima],
			[id_catalogo],
			[id_bodega])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_catalogo_bodega,
			@p_disponible,
			@p_existencia,
			@p_reservado,
			@p_existenciaMinima,
			@p_enOrdenes,
			@p_localizacion,
			@p_costo,
			@p_existenciaMaxima,
			@p_id_catalogo,
			@p_id_bodega)
		SET @v_id = scope_identity()
UPDATE [cat_catalogo_bodega] 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_cat_catalogo_bodega]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_cat_catalogo_bodega]
GO
CREATE PROCEDURE [upd_cat_catalogo_bodega]
	@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_bodega varchar(300) = null,
		@p_disponible float = null,
		@p_existencia float = null,
		@p_reservado float = null,
		@p_existenciaMinima float = null,
		@p_enOrdenes float = null,
		@p_localizacion varchar(50) = null,
		@p_costo money = null,
		@p_existenciaMaxima float = null,
		@p_id_catalogo int = null,
		@p_id_bodega int = null
AS
BEGIN
		UPDATE [cat_catalogo_bodega]
		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_bodega] = isnull(@p_catalogo_bodega,[catalogo_bodega]),
			[disponible] = isnull(@p_disponible,[disponible]),
			[existencia] = isnull(@p_existencia,[existencia]),
			[reservado] = isnull(@p_reservado,[reservado]),
			[existenciaMinima] = isnull(@p_existenciaMinima,[existenciaMinima]),
			[enOrdenes] = isnull(@p_enOrdenes,[enOrdenes]),
			[localizacion] = isnull(@p_localizacion,[localizacion]),
			[costo] = isnull(@p_costo,[costo]),
			[existenciaMaxima] = isnull(@p_existenciaMaxima,[existenciaMaxima]),
			[id_catalogo] = isnull(@p_id_catalogo,[id_catalogo]),
			[id_bodega] = isnull(@p_id_bodega,[id_bodega])
		WHERE (id = @p_id)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'gen_bodega'
-----------------------------------18/06/2013 02:09:48 p.m.