

IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[view_cnt_contrato_sucursal]')) DROP VIEW [dbo].[view_cnt_contrato_sucursal]
GO
CREATE  VIEW [view_cnt_contrato_sucursal] AS
SELECT 	[cnt_contrato_sucursal].[id] AS [id],
		[cnt_contrato_sucursal].[uid] AS [uid],
		[cnt_contrato_sucursal].[eid] AS [eid],
		[cnt_contrato_sucursal].[id_usuario_modifico] AS [id_usuario_modifico],
		[cnt_contrato_sucursal].[id_usuario_creo] AS [id_usuario_creo],
		[cnt_contrato_sucursal].[fechaModificacion] AS [fechaModificacion],
		[cnt_contrato_sucursal].[fechaCreacion] AS [fechaCreacion],
		[cnt_contrato_sucursal].[active] AS [active],
		[cnt_contrato_sucursal].[contrato_sucursal] AS [contrato_sucursal],
		[cnt_contrato_sucursal].[precioVisita] AS [precioVisita],
		[cnt_contrato_sucursal].[precioServicio] AS [precioServicio],
		[cnt_contrato_sucursal].[id_contrato] AS [id_contrato],
[cnt_contrato].[contrato] as [cnt_contrato_contrato],
[cnt_contrato].[contrato_codigo] as [cnt_contrato_contrato_codigo],
[cnt_contrato].[numero] as [cnt_contrato_numero],
[cnt_contrato].[totalContrato] as [cnt_contrato_totalContrato],
[cnt_contrato].[presupuesto] as [cnt_contrato_presupuesto],
[cnt_contrato].[fechaInicio_ff] as [cnt_contrato_fechaInicio_ff],
[cnt_contrato].[fechaFin_ff] as [cnt_contrato_fechaFin_ff],
[cnt_contrato].[diasCumplimiento] as [cnt_contrato_diasCumplimiento],
[cnt_contrato].[condiciones] as [cnt_contrato_condiciones],
[cnt_contrato].[id_periodoContrato] as [cnt_contrato_id_periodoContrato],
[cnt_contrato].[id_tercero] as [cnt_contrato_id_tercero],
[cnt_contrato].[id_tipoServicio] as [cnt_contrato_id_tipoServicio],
[cnt_contrato].[id_tipoContrato] as [cnt_contrato_id_tipoContrato],
[cnt_contrato].[id_tercero_servicio] as [cnt_contrato_id_tercero_servicio],
[cnt_contrato].[id_subtipoDocumento] as [cnt_contrato_id_subtipoDocumento],
[cnt_contrato].[id_departamentoSolicitud] as [cnt_contrato_id_departamentoSolicitud],
[cnt_contrato].[id_usuario_coordinador] as [cnt_contrato_id_usuario_coordinador],
[cnt_contrato].[id_documento.alquiler] as [cnt_contrato_id_documento.alquiler],
		[cnt_contrato_sucursal].[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],
		[cnt_contrato_sucursal].[presupuesto] AS [presupuesto],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [cnt_contrato_sucursal]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[cnt_contrato_sucursal].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[cnt_contrato_sucursal].[id_usuario_creo]
	INNER JOIN [cnt_contrato] ON [cnt_contrato].id=[cnt_contrato_sucursal].[id_contrato]
	INNER JOIN [ter_sucursal] ON [ter_sucursal].id=[cnt_contrato_sucursal].[id_sucursal]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [cnt_contrato_sucursal].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
