

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],
		[gen_bodega].[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_listaPrecio] as [ter_sucursal_id_listaPrecio],
[ter_sucursal].[id_estadoTercero] as [ter_sucursal_id_estadoTercero],
[ter_sucursal].[codigorecursiva] as [ter_sucursal_codigorecursiva],
		[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 [ter_sucursal] ON [ter_sucursal].id=[gen_bodega].[id_sucursal]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [gen_bodega].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [gen_bodega].active=1
