
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_cnt_contrato]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_cnt_contrato]
GO
CREATE PROCEDURE [reg_cnt_contrato]
	@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],
		[contrato] AS [contrato],
		[contrato_codigo] AS [contrato_codigo],
		[numero] AS [numero],
		[totalContrato] AS [totalContrato],
		[presupuesto] AS [presupuesto],
		[fechaInicio_ff] AS [fechaInicio_ff],
		[fechaFin_ff] AS [fechaFin_ff],
		[diasCumplimiento] AS [diasCumplimiento],
		[condiciones] AS [condiciones],
		[id_periodoContrato] AS [id_periodoContrato],
		[id_tercero] AS [id_tercero],
		[id_tipoServicio] AS [id_tipoServicio],
		[id_tipoContrato] AS [id_tipoContrato],
		[id_tercero_servicio] AS [id_tercero_servicio],
		[id_subtipoDocumento] AS [id_subtipoDocumento],
		[id_departamentoSolicitud] AS [id_departamentoSolicitud],
		[id_usuario_coordinador] AS [id_usuario_coordinador],
		[id_documento.alquiler] AS [id_documento.alquiler],
		[id_estadoTipoDocumento_inicial] AS [id_estadoTipoDocumento_inicial],
		[id_subtipoDocumento_solicitud] AS [id_subtipoDocumento_solicitud]
	FROM [cnt_contrato]
	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_cnt_contrato]')) DROP VIEW [dbo].[view_cnt_contrato]
GO
CREATE  VIEW [view_cnt_contrato] AS
SELECT 	[cnt_contrato].[id] AS [id],
		[cnt_contrato].[uid] AS [uid],
		[cnt_contrato].[eid] AS [eid],
		[cnt_contrato].[id_usuario_modifico] AS [id_usuario_modifico],
		[cnt_contrato].[id_usuario_creo] AS [id_usuario_creo],
		[cnt_contrato].[fechaModificacion] AS [fechaModificacion],
		[cnt_contrato].[fechaCreacion] AS [fechaCreacion],
		[cnt_contrato].[active] AS [active],
		[cnt_contrato].[contrato] AS [contrato],
		[cnt_contrato].[contrato_codigo] AS [contrato_codigo],
		[cnt_contrato].[numero] AS [numero],
		[cnt_contrato].[totalContrato] AS [totalContrato],
		[cnt_contrato].[presupuesto] AS [presupuesto],
		[cnt_contrato].[fechaInicio_ff] AS [fechaInicio_ff],
		[cnt_contrato].[fechaFin_ff] AS [fechaFin_ff],
		[cnt_contrato].[diasCumplimiento] AS [diasCumplimiento],
		[cnt_contrato].[condiciones] AS [condiciones],
		[cnt_contrato].[id_periodoContrato] AS [id_periodoContrato],
[cnt_periodoContrato].[periodoContrato] as [cnt_periodoContrato_periodoContrato],
[cnt_periodoContrato].[periodoContrato_codigo] as [cnt_periodoContrato_periodoContrato_codigo],
		[cnt_contrato].[id_tercero] AS [id_tercero],
[ter_tercero].[tercero] as [ter_tercero_tercero],
[ter_tercero].[tercero_codigo] as [ter_tercero_tercero_codigo],
[ter_tercero].[tercero_nit] as [ter_tercero_tercero_nit],
[ter_tercero].[direccion] as [ter_tercero_direccion],
[ter_tercero].[contacto] as [ter_tercero_contacto],
[ter_tercero].[telefono] as [ter_tercero_telefono],
[ter_tercero].[fax] as [ter_tercero_fax],
[ter_tercero].[email] as [ter_tercero_email],
[ter_tercero].[website] as [ter_tercero_website],
[ter_tercero].[representante] as [ter_tercero_representante],
[ter_tercero].[notas] as [ter_tercero_notas],
[ter_tercero].[tercero_nombreCorto] as [ter_tercero_tercero_nombreCorto],
[ter_tercero].[esCliente] as [ter_tercero_esCliente],
[ter_tercero].[esProveedor] as [ter_tercero_esProveedor],
[ter_tercero].[esTransportador] as [ter_tercero_esTransportador],
[ter_tercero].[esFabricante] as [ter_tercero_esFabricante],
[ter_tercero].[esEmpresaPropia] as [ter_tercero_esEmpresaPropia],
[ter_tercero].[clasificacion] as [ter_tercero_clasificacion],
[ter_tercero].[id_zona] as [ter_tercero_id_zona],
[ter_tercero].[id_estadoTercero] as [ter_tercero_id_estadoTercero],
[ter_tercero].[id_naturalezaTercero] as [ter_tercero_id_naturalezaTercero],
[ter_tercero].[esProspecto] as [ter_tercero_esProspecto],
		[cnt_contrato].[id_tipoServicio] AS [id_tipoServicio],
[gen_tipoServicio].[tipoServicio] as [gen_tipoServicio_tipoServicio],
[gen_tipoServicio].[tipoServicio_codigo] as [gen_tipoServicio_tipoServicio_codigo],
[gen_tipoServicio].[tieneGarantia] as [gen_tipoServicio_tieneGarantia],
[gen_tipoServicio].[diasGarantia] as [gen_tipoServicio_diasGarantia],
		[cnt_contrato].[id_tipoContrato] AS [id_tipoContrato],
[cnt_tipoContrato].[tipoContrato] as [cnt_tipoContrato_tipoContrato],
[cnt_tipoContrato].[tipoContrato_codigo] as [cnt_tipoContrato_tipoContrato_codigo],
		[cnt_contrato].[id_tercero_servicio] AS [id_tercero_servicio],
[ter_tercero_servicio].[tercero] as [ter_tercero_servicio_tercero],
[ter_tercero_servicio].[tercero_codigo] as [ter_tercero_servicio_tercero_codigo],
[ter_tercero_servicio].[tercero_nit] as [ter_tercero_servicio_tercero_nit],
[ter_tercero_servicio].[direccion] as [ter_tercero_servicio_direccion],
[ter_tercero_servicio].[contacto] as [ter_tercero_servicio_contacto],
[ter_tercero_servicio].[telefono] as [ter_tercero_servicio_telefono],
[ter_tercero_servicio].[fax] as [ter_tercero_servicio_fax],
[ter_tercero_servicio].[email] as [ter_tercero_servicio_email],
[ter_tercero_servicio].[website] as [ter_tercero_servicio_website],
[ter_tercero_servicio].[representante] as [ter_tercero_servicio_representante],
[ter_tercero_servicio].[notas] as [ter_tercero_servicio_notas],
[ter_tercero_servicio].[tercero_nombreCorto] as [ter_tercero_servicio_tercero_nombreCorto],
[ter_tercero_servicio].[esCliente] as [ter_tercero_servicio_esCliente],
[ter_tercero_servicio].[esProveedor] as [ter_tercero_servicio_esProveedor],
[ter_tercero_servicio].[esTransportador] as [ter_tercero_servicio_esTransportador],
[ter_tercero_servicio].[esFabricante] as [ter_tercero_servicio_esFabricante],
[ter_tercero_servicio].[esEmpresaPropia] as [ter_tercero_servicio_esEmpresaPropia],
[ter_tercero_servicio].[clasificacion] as [ter_tercero_servicio_clasificacion],
[ter_tercero_servicio].[id_zona] as [ter_tercero_servicio_id_zona],
[ter_tercero_servicio].[id_estadoTercero] as [ter_tercero_servicio_id_estadoTercero],
[ter_tercero_servicio].[id_naturalezaTercero] as [ter_tercero_servicio_id_naturalezaTercero],
[ter_tercero_servicio].[esProspecto] as [ter_tercero_servicio_esProspecto],
		[cnt_contrato].[id_subtipoDocumento] AS [id_subtipoDocumento],
[doc_subtipoDocumento].[subtipoDocumento] as [doc_subtipoDocumento_subtipoDocumento],
[doc_subtipoDocumento].[subtipoDocumento_codigo] as [doc_subtipoDocumento_subtipoDocumento_codigo],
[doc_subtipoDocumento].[prefijo] as [doc_subtipoDocumento_prefijo],
[doc_subtipoDocumento].[consecutivo] as [doc_subtipoDocumento_consecutivo],
[doc_subtipoDocumento].[id_tipoDocumento] as [doc_subtipoDocumento_id_tipoDocumento],
[doc_subtipoDocumento].[programarPlaneadas] as [doc_subtipoDocumento_programarPlaneadas],
[doc_subtipoDocumento].[cargarSolicitante] as [doc_subtipoDocumento_cargarSolicitante],
[doc_subtipoDocumento].[verCentroCosto] as [doc_subtipoDocumento_verCentroCosto],
[doc_subtipoDocumento].[verIncoterm] as [doc_subtipoDocumento_verIncoterm],
[doc_subtipoDocumento].[valUrgente] as [doc_subtipoDocumento_valUrgente],
[doc_subtipoDocumento].[bloquearDespacho] as [doc_subtipoDocumento_bloquearDespacho],
[doc_subtipoDocumento].[cerrarSol] as [doc_subtipoDocumento_cerrarSol],
[doc_subtipoDocumento].[ejecutaDevolucion] as [doc_subtipoDocumento_ejecutaDevolucion],
		[cnt_contrato].[id_departamentoSolicitud] AS [id_departamentoSolicitud],
[ort_departamentoSolicitud].[departamentoSolicitud] as [ort_departamentoSolicitud_departamentoSolicitud],
[ort_departamentoSolicitud].[departamentoSolicitud_codigo] as [ort_departamentoSolicitud_departamentoSolicitud_codigo],
[ort_departamentoSolicitud].[recibeSolicitudes] as [ort_departamentoSolicitud_recibeSolicitudes],
		[cnt_contrato].[id_usuario_coordinador] AS [id_usuario_coordinador],
[seg_usuario_coordinador].[usuario] as [seg_usuario_coordinador_usuario],
[seg_usuario_coordinador].[usuario_codigo] as [seg_usuario_coordinador_usuario_codigo],
[seg_usuario_coordinador].[intentosFallidos] as [seg_usuario_coordinador_intentosFallidos],
[seg_usuario_coordinador].[numeroIngresos] as [seg_usuario_coordinador_numeroIngresos],
[seg_usuario_coordinador].[fechaUltimoIngreso_fh] as [seg_usuario_coordinador_fechaUltimoIngreso_fh],
[seg_usuario_coordinador].[fechaIntentoFallido_fh] as [seg_usuario_coordinador_fechaIntentoFallido_fh],
[seg_usuario_coordinador].[clave] as [seg_usuario_coordinador_clave],
[seg_usuario_coordinador].[nombre] as [seg_usuario_coordinador_nombre],
[seg_usuario_coordinador].[profesion] as [seg_usuario_coordinador_profesion],
[seg_usuario_coordinador].[email] as [seg_usuario_coordinador_email],
[seg_usuario_coordinador].[direccion] as [seg_usuario_coordinador_direccion],
[seg_usuario_coordinador].[telefono] as [seg_usuario_coordinador_telefono],
[seg_usuario_coordinador].[costoHora] as [seg_usuario_coordinador_costoHora],
[seg_usuario_coordinador].[porcentajeComision] as [seg_usuario_coordinador_porcentajeComision],
[seg_usuario_coordinador].[accesoActivo] as [seg_usuario_coordinador_accesoActivo],
[seg_usuario_coordinador].[cambiarClave] as [seg_usuario_coordinador_cambiarClave],
[seg_usuario_coordinador].[fechaUltimoCambioClave_fh] as [seg_usuario_coordinador_fechaUltimoCambioClave_fh],
[seg_usuario_coordinador].[aplicaRecargo] as [seg_usuario_coordinador_aplicaRecargo],
[seg_usuario_coordinador].[sexo] as [seg_usuario_coordinador_sexo],
[seg_usuario_coordinador].[id_perfil] as [seg_usuario_coordinador_id_perfil],
[seg_usuario_coordinador].[id_cargo] as [seg_usuario_coordinador_id_cargo],
[seg_usuario_coordinador].[id_grupo] as [seg_usuario_coordinador_id_grupo],
[seg_usuario_coordinador].[id_zona] as [seg_usuario_coordinador_id_zona],
[seg_usuario_coordinador].[identificacion] as [seg_usuario_coordinador_identificacion],
[seg_usuario_coordinador].[id_usuario] as [seg_usuario_coordinador_id_usuario],
[seg_usuario_coordinador].[preferencias] as [seg_usuario_coordinador_preferencias],
[seg_usuario_coordinador].[habilitado] as [seg_usuario_coordinador_habilitado],
		[cnt_contrato].[id_documento.alquiler] AS [id_documento.alquiler],
[doc_documento.alquiler].[documento.alquiler] as [doc_documento.alquiler_documento.alquiler],
[doc_documento.alquiler].[fechaFin_ff] as [doc_documento.alquiler_fechaFin_ff],
[doc_documento.alquiler].[mesVencido] as [doc_documento.alquiler_mesVencido],
[doc_documento.alquiler].[observaciones] as [doc_documento.alquiler_observaciones],
[doc_documento.alquiler].[ejecutaDevolucion] as [doc_documento.alquiler_ejecutaDevolucion],
		[doc_documento_alquiler].[documento_codigo] AS [doc_documento_alquiler_documento_codigo],
		[doc_documento_alquiler].[documento_numero] AS [doc_documento_alquiler_documento_numero],
		[doc_documento_alquiler].[prefijo] AS [doc_documento_alquiler_prefijo],
		[doc_documento_alquiler].[subtotal] AS [doc_documento_alquiler_subtotal],
		[doc_documento_alquiler].[descuento] AS [doc_documento_alquiler_descuento],
		[doc_documento_alquiler].[iva] AS [doc_documento_alquiler_iva],
		[doc_documento_alquiler].[total] AS [doc_documento_alquiler_total],
		[doc_documento_alquiler].[costo] AS [doc_documento_alquiler_costo],
		[doc_documento_alquiler].[rentabilidad] AS [doc_documento_alquiler_rentabilidad],
		[doc_documento_alquiler].[trm] AS [doc_documento_alquiler_trm],
		[doc_documento_alquiler].[fecha_fh] AS [doc_documento_alquiler_fecha_fh],
		[doc_documento_alquiler].[fechaSugerida_fh] AS [doc_documento_alquiler_fechaSugerida_fh],
		[doc_documento_alquiler].[fechaCierre_ff] AS [doc_documento_alquiler_fechaCierre_ff],
		[doc_documento_alquiler].[fechaLimite_fh] AS [doc_documento_alquiler_fechaLimite_fh],
		[doc_documento_alquiler].[id_subtipoDocumento] AS [doc_documento_alquiler_id_subtipoDocumento],
		[doc_documento_alquiler].[id_tercero_cliente] AS [doc_documento_alquiler_id_tercero_cliente],
		[doc_documento_alquiler].[id_documento] AS [doc_documento_alquiler_id_documento],
		[doc_documento_alquiler].[id_tercero_proveedor] AS [doc_documento_alquiler_id_tercero_proveedor],
		[doc_documento_alquiler].[id_prioridadDocumento] AS [doc_documento_alquiler_id_prioridadDocumento],
		[doc_documento_alquiler].[id_moneda] AS [doc_documento_alquiler_id_moneda],
		[doc_documento_alquiler].[id_estadoTipoDocumento] AS [doc_documento_alquiler_id_estadoTipoDocumento],
		[doc_documento_alquiler].[id_motivoestado] AS [doc_documento_alquiler_id_motivoestado],
		[doc_documento_alquiler].[subtotalPlaneado] AS [doc_documento_alquiler_subtotalPlaneado],
		[doc_documento_alquiler].[descuentoPlaneado] AS [doc_documento_alquiler_descuentoPlaneado],
		[doc_documento_alquiler].[ivaPlaneado] AS [doc_documento_alquiler_ivaPlaneado],
		[doc_documento_alquiler].[totalPlaneado] AS [doc_documento_alquiler_totalPlaneado],
		[doc_documento_alquiler].[costoPlaneado] AS [doc_documento_alquiler_costoPlaneado],
		[doc_documento_alquiler].[id_usuario_asignado] AS [doc_documento_alquiler_id_usuario_asignado],
		[doc_documento_alquiler].[id_usuario_enUso] AS [doc_documento_alquiler_id_usuario_enUso],
		[cnt_contrato].[id_estadoTipoDocumento_inicial] AS [id_estadoTipoDocumento_inicial],
[doc_estadoTipoDocumento_inicial].[estadoTipoDocumento] as [doc_estadoTipoDocumento_inicial_estadoTipoDocumento],
[doc_estadoTipoDocumento_inicial].[estadoTipoDocumento_codigo] as [doc_estadoTipoDocumento_inicial_estadoTipoDocumento_codigo],
[doc_estadoTipoDocumento_inicial].[afectaIndicador] as [doc_estadoTipoDocumento_inicial_afectaIndicador],
[doc_estadoTipoDocumento_inicial].[editaDocumento] as [doc_estadoTipoDocumento_inicial_editaDocumento],
[doc_estadoTipoDocumento_inicial].[orden] as [doc_estadoTipoDocumento_inicial_orden],
[doc_estadoTipoDocumento_inicial].[puedeRegresar] as [doc_estadoTipoDocumento_inicial_puedeRegresar],
[doc_estadoTipoDocumento_inicial].[color] as [doc_estadoTipoDocumento_inicial_color],
[doc_estadoTipoDocumento_inicial].[afectaInventario] as [doc_estadoTipoDocumento_inicial_afectaInventario],
[doc_estadoTipoDocumento_inicial].[id_tipoDocumento] as [doc_estadoTipoDocumento_inicial_id_tipoDocumento],
[doc_estadoTipoDocumento_inicial].[descripcion] as [doc_estadoTipoDocumento_inicial_descripcion],
		[cnt_contrato].[id_subtipoDocumento_solicitud] AS [id_subtipoDocumento_solicitud],
[doc_subtipoDocumento_solicitud].[subtipoDocumento] as [doc_subtipoDocumento_solicitud_subtipoDocumento],
[doc_subtipoDocumento_solicitud].[subtipoDocumento_codigo] as [doc_subtipoDocumento_solicitud_subtipoDocumento_codigo],
[doc_subtipoDocumento_solicitud].[prefijo] as [doc_subtipoDocumento_solicitud_prefijo],
[doc_subtipoDocumento_solicitud].[consecutivo] as [doc_subtipoDocumento_solicitud_consecutivo],
[doc_subtipoDocumento_solicitud].[id_tipoDocumento] as [doc_subtipoDocumento_solicitud_id_tipoDocumento],
[doc_subtipoDocumento_solicitud].[programarPlaneadas] as [doc_subtipoDocumento_solicitud_programarPlaneadas],
[doc_subtipoDocumento_solicitud].[cargarSolicitante] as [doc_subtipoDocumento_solicitud_cargarSolicitante],
[doc_subtipoDocumento_solicitud].[verCentroCosto] as [doc_subtipoDocumento_solicitud_verCentroCosto],
[doc_subtipoDocumento_solicitud].[verIncoterm] as [doc_subtipoDocumento_solicitud_verIncoterm],
[doc_subtipoDocumento_solicitud].[valUrgente] as [doc_subtipoDocumento_solicitud_valUrgente],
[doc_subtipoDocumento_solicitud].[bloquearDespacho] as [doc_subtipoDocumento_solicitud_bloquearDespacho],
[doc_subtipoDocumento_solicitud].[cerrarSol] as [doc_subtipoDocumento_solicitud_cerrarSol],
[doc_subtipoDocumento_solicitud].[ejecutaDevolucion] as [doc_subtipoDocumento_solicitud_ejecutaDevolucion],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [cnt_contrato]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[cnt_contrato].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[cnt_contrato].[id_usuario_creo]
	INNER JOIN [cnt_periodoContrato] ON [cnt_periodoContrato].id=[cnt_contrato].[id_periodoContrato]
	INNER JOIN [ter_tercero] ON [ter_tercero].id=[cnt_contrato].[id_tercero]
	INNER JOIN [gen_tipoServicio] ON [gen_tipoServicio].id=[cnt_contrato].[id_tipoServicio]
	INNER JOIN [cnt_tipoContrato] ON [cnt_tipoContrato].id=[cnt_contrato].[id_tipoContrato]
	INNER JOIN [ter_tercero] AS [ter_tercero_servicio] ON [ter_tercero_servicio].id=[cnt_contrato].[id_tercero_servicio]
	INNER JOIN [doc_subtipoDocumento] ON [doc_subtipoDocumento].id=[cnt_contrato].[id_subtipoDocumento]
	INNER JOIN [ort_departamentoSolicitud] ON [ort_departamentoSolicitud].id=[cnt_contrato].[id_departamentoSolicitud]
	INNER JOIN [seg_usuario] AS [seg_usuario_coordinador] ON [seg_usuario_coordinador].id=[cnt_contrato].[id_usuario_coordinador]
	INNER JOIN [doc_documento.alquiler] ON [doc_documento.alquiler].id=[cnt_contrato].[id_documento.alquiler]
	INNER JOIN [doc_documento] aS [doc_documento_alquiler] ON [doc_documento.alquiler].id = [doc_documento_alquiler].id
	INNER JOIN [doc_estadoTipoDocumento] AS [doc_estadoTipoDocumento_inicial] ON [doc_estadoTipoDocumento_inicial].id=[cnt_contrato].[id_estadoTipoDocumento_inicial]
	INNER JOIN [doc_subtipoDocumento] AS [doc_subtipoDocumento_solicitud] ON [doc_subtipoDocumento_solicitud].id=[cnt_contrato].[id_subtipoDocumento_solicitud]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [cnt_contrato].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [cnt_contrato].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_cnt_contratosXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_contratosXusuario_modifico]
GO
CREATE PROCEDURE [sel_cnt_contratosXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_cnt_contrato]
	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_cnt_contratosXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_contratosXusuario_creo]
GO
CREATE PROCEDURE [sel_cnt_contratosXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_cnt_contrato]
	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_cnt_contratosXperiodoContrato]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_contratosXperiodoContrato]
GO
CREATE PROCEDURE [sel_cnt_contratosXperiodoContrato]
	@p_id_periodoContrato INT
AS
BEGIN
	SELECT * 
	FROM [view_cnt_contrato]
	WHERE ([id_periodoContrato]= @p_id_periodoContrato)
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_cnt_contratosXtercero]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_contratosXtercero]
GO
CREATE PROCEDURE [sel_cnt_contratosXtercero]
	@p_id_tercero INT
AS
BEGIN
	SELECT * 
	FROM [view_cnt_contrato]
	WHERE ([id_tercero]= @p_id_tercero)
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_cnt_contratosXtipoServicio]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_contratosXtipoServicio]
GO
CREATE PROCEDURE [sel_cnt_contratosXtipoServicio]
	@p_id_tipoServicio INT
AS
BEGIN
	SELECT * 
	FROM [view_cnt_contrato]
	WHERE ([id_tipoServicio]= @p_id_tipoServicio)
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_cnt_contratosXtipoContrato]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_contratosXtipoContrato]
GO
CREATE PROCEDURE [sel_cnt_contratosXtipoContrato]
	@p_id_tipoContrato INT
AS
BEGIN
	SELECT * 
	FROM [view_cnt_contrato]
	WHERE ([id_tipoContrato]= @p_id_tipoContrato)
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_cnt_contratosXtercero_servicio]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_contratosXtercero_servicio]
GO
CREATE PROCEDURE [sel_cnt_contratosXtercero_servicio]
	@p_id_tercero_servicio INT
AS
BEGIN
	SELECT * 
	FROM [view_cnt_contrato]
	WHERE ([id_tercero_servicio]= @p_id_tercero_servicio)
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_cnt_contratosXsubtipoDocumento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_contratosXsubtipoDocumento]
GO
CREATE PROCEDURE [sel_cnt_contratosXsubtipoDocumento]
	@p_id_subtipoDocumento INT
AS
BEGIN
	SELECT * 
	FROM [view_cnt_contrato]
	WHERE ([id_subtipoDocumento]= @p_id_subtipoDocumento)
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_cnt_contratosXdepartamentoSolicitud]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_contratosXdepartamentoSolicitud]
GO
CREATE PROCEDURE [sel_cnt_contratosXdepartamentoSolicitud]
	@p_id_departamentoSolicitud INT
AS
BEGIN
	SELECT * 
	FROM [view_cnt_contrato]
	WHERE ([id_departamentoSolicitud]= @p_id_departamentoSolicitud)
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_cnt_contratosXusuario_coordinador]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_contratosXusuario_coordinador]
GO
CREATE PROCEDURE [sel_cnt_contratosXusuario_coordinador]
	@p_id_usuario_coordinador INT
AS
BEGIN
	SELECT * 
	FROM [view_cnt_contrato]
	WHERE ([id_usuario_coordinador]= @p_id_usuario_coordinador)
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_cnt_contratosXdocumento_alquiler]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_contratosXdocumento_alquiler]
GO
CREATE PROCEDURE [sel_cnt_contratosXdocumento_alquiler]
	@p_id_documento_alquiler INT
AS
BEGIN
	SELECT * 
	FROM [view_cnt_contrato]
	WHERE ([id_documento.alquiler]= @p_id_documento_alquiler)
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_cnt_contratosXestadoTipoDocumento_inicial]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_contratosXestadoTipoDocumento_inicial]
GO
CREATE PROCEDURE [sel_cnt_contratosXestadoTipoDocumento_inicial]
	@p_id_estadoTipoDocumento_inicial INT
AS
BEGIN
	SELECT * 
	FROM [view_cnt_contrato]
	WHERE ([id_estadoTipoDocumento_inicial]= @p_id_estadoTipoDocumento_inicial)
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_cnt_contratosXsubtipoDocumento_solicitud]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_contratosXsubtipoDocumento_solicitud]
GO
CREATE PROCEDURE [sel_cnt_contratosXsubtipoDocumento_solicitud]
	@p_id_subtipoDocumento_solicitud INT
AS
BEGIN
	SELECT * 
	FROM [view_cnt_contrato]
	WHERE ([id_subtipoDocumento_solicitud]= @p_id_subtipoDocumento_solicitud)
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_cnt_contratos]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_contratos]
GO
CREATE PROCEDURE [sel_cnt_contratos]
	
	@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_cnt_contrato]
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_cnt_contratos]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_cnt_contratos]
GO
CREATE PROCEDURE [lis_cnt_contratos]
	AS
BEGIN
	SELECT id AS id,
[contrato] AS [cnt_contrato]
	FROM [cnt_contrato]
	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_cnt_contrato]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_cnt_contrato]
GO
CREATE PROCEDURE [upd_cnt_contrato]
	@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_contrato varchar(300) = null,
		@p_contrato_codigo varchar(100) = null,
		@p_numero varchar(50) = null,
		@p_totalContrato money = null,
		@p_presupuesto money = null,
		@p_fechaInicio_ff smalldatetime = null,
		@p_fechaFin_ff smalldatetime = null,
		@p_diasCumplimiento int = null,
		@p_condiciones varchar(8000) = null,
		@p_id_periodoContrato int = null,
		@p_id_tercero int = null,
		@p_id_tipoServicio int = null,
		@p_id_tipoContrato int = null,
		@p_id_tercero_servicio int = null,
		@p_id_subtipoDocumento int = null,
		@p_id_departamentoSolicitud int = null,
		@p_id_usuario_coordinador int = null,
		@p_id_documento_alquiler int = null,
		@p_id_estadoTipoDocumento_inicial int = null,
		@p_id_subtipoDocumento_solicitud int = null
AS
BEGIN
		UPDATE [cnt_contrato]
		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(),
			[contrato] = isnull(@p_contrato,[contrato]),
			[contrato_codigo] = isnull(@p_contrato_codigo,[contrato_codigo]),
			[numero] = isnull(@p_numero,[numero]),
			[totalContrato] = isnull(@p_totalContrato,[totalContrato]),
			[presupuesto] = isnull(@p_presupuesto,[presupuesto]),
			[fechaInicio_ff] = isnull(@p_fechaInicio_ff,[fechaInicio_ff]),
			[fechaFin_ff] = isnull(@p_fechaFin_ff,[fechaFin_ff]),
			[diasCumplimiento] = isnull(@p_diasCumplimiento,[diasCumplimiento]),
			[condiciones] = isnull(@p_condiciones,[condiciones]),
			[id_periodoContrato] = isnull(@p_id_periodoContrato,[id_periodoContrato]),
			[id_tercero] = isnull(@p_id_tercero,[id_tercero]),
			[id_tipoServicio] = isnull(@p_id_tipoServicio,[id_tipoServicio]),
			[id_tipoContrato] = isnull(@p_id_tipoContrato,[id_tipoContrato]),
			[id_tercero_servicio] = isnull(@p_id_tercero_servicio,[id_tercero_servicio]),
			[id_subtipoDocumento] = isnull(@p_id_subtipoDocumento,[id_subtipoDocumento]),
			[id_departamentoSolicitud] = isnull(@p_id_departamentoSolicitud,[id_departamentoSolicitud]),
			[id_usuario_coordinador] = isnull(@p_id_usuario_coordinador,[id_usuario_coordinador]),
			[id_documento.alquiler] = isnull(@p_id_documento_alquiler,[id_documento.alquiler]),
			[id_estadoTipoDocumento_inicial] = isnull(@p_id_estadoTipoDocumento_inicial,[id_estadoTipoDocumento_inicial]),
			[id_subtipoDocumento_solicitud] = isnull(@p_id_subtipoDocumento_solicitud,[id_subtipoDocumento_solicitud])
		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_cnt_contrato]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_cnt_contrato]
GO
CREATE PROCEDURE [ins_cnt_contrato]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_contrato varchar(300),
		@p_contrato_codigo varchar(100),
		@p_numero varchar(50),
		@p_totalContrato money,
		@p_presupuesto money,
		@p_fechaInicio_ff smalldatetime,
		@p_fechaFin_ff smalldatetime,
		@p_diasCumplimiento int,
		@p_condiciones varchar(8000),
		@p_id_periodoContrato int,
		@p_id_tercero int,
		@p_id_tipoServicio int,
		@p_id_tipoContrato int,
		@p_id_tercero_servicio int,
		@p_id_subtipoDocumento int,
		@p_id_departamentoSolicitud int,
		@p_id_usuario_coordinador int,
		@p_id_documento_alquiler int,
		@p_id_estadoTipoDocumento_inicial int,
		@p_id_subtipoDocumento_solicitud int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [cnt_contrato]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[contrato],
			[contrato_codigo],
			[numero],
			[totalContrato],
			[presupuesto],
			[fechaInicio_ff],
			[fechaFin_ff],
			[diasCumplimiento],
			[condiciones],
			[id_periodoContrato],
			[id_tercero],
			[id_tipoServicio],
			[id_tipoContrato],
			[id_tercero_servicio],
			[id_subtipoDocumento],
			[id_departamentoSolicitud],
			[id_usuario_coordinador],
			[id_documento.alquiler],
			[id_estadoTipoDocumento_inicial],
			[id_subtipoDocumento_solicitud])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_contrato,
			@p_contrato_codigo,
			@p_numero,
			@p_totalContrato,
			@p_presupuesto,
			@p_fechaInicio_ff,
			@p_fechaFin_ff,
			@p_diasCumplimiento,
			@p_condiciones,
			@p_id_periodoContrato,
			@p_id_tercero,
			@p_id_tipoServicio,
			@p_id_tipoContrato,
			@p_id_tercero_servicio,
			@p_id_subtipoDocumento,
			@p_id_departamentoSolicitud,
			@p_id_usuario_coordinador,
			@p_id_documento_alquiler,
			@p_id_estadoTipoDocumento_inicial,
			@p_id_subtipoDocumento_solicitud)
		SET @v_id = scope_identity()
UPDATE [cnt_contrato] 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_cnt_contrato]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_cnt_contrato]
GO
CREATE PROCEDURE [act_cnt_contrato]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [cnt_contrato]
	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_cnt_contrato_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_cnt_contrato_m]
GO
CREATE PROCEDURE [reg_cnt_contrato_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],
		[contrato] AS [contrato],
		[contrato_codigo] AS [contrato_codigo],
		[numero] AS [numero],
		[totalContrato] AS [totalContrato],
		[presupuesto] AS [presupuesto],
		[fechaInicio_ff] AS [fechaInicio_ff],
		[fechaFin_ff] AS [fechaFin_ff],
		[diasCumplimiento] AS [diasCumplimiento],
		[condiciones] AS [condiciones],
		[id_periodoContrato] AS [id_periodoContrato],
		[id_tercero] AS [id_tercero],
		[id_tipoServicio] AS [id_tipoServicio],
		[id_tipoContrato] AS [id_tipoContrato],
		[id_tercero_servicio] AS [id_tercero_servicio],
		[id_subtipoDocumento] AS [id_subtipoDocumento],
		[id_departamentoSolicitud] AS [id_departamentoSolicitud],
		[id_usuario_coordinador] AS [id_usuario_coordinador],
		[id_documento.alquiler] AS [id_documento.alquiler],
		[id_estadoTipoDocumento_inicial] AS [id_estadoTipoDocumento_inicial],
		[id_subtipoDocumento_solicitud] AS [id_subtipoDocumento_solicitud]
	FROM [cnt_contrato]
	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_cnt_contratosXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_contratosXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_cnt_contratosXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cnt_contrato]
	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_cnt_contratosXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_contratosXusuario_creo_m]
GO
CREATE PROCEDURE [sel_cnt_contratosXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cnt_contrato]
	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_cnt_contratosXperiodoContrato_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_contratosXperiodoContrato_m]
GO
CREATE PROCEDURE [sel_cnt_contratosXperiodoContrato_m]
	@p_id_periodoContrato INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cnt_contrato]
	WHERE ([id_periodoContrato]= @p_id_periodoContrato)
	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_cnt_contratosXtercero_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_contratosXtercero_m]
GO
CREATE PROCEDURE [sel_cnt_contratosXtercero_m]
	@p_id_tercero INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cnt_contrato]
	WHERE ([id_tercero]= @p_id_tercero)
	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_cnt_contratosXtipoServicio_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_contratosXtipoServicio_m]
GO
CREATE PROCEDURE [sel_cnt_contratosXtipoServicio_m]
	@p_id_tipoServicio INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cnt_contrato]
	WHERE ([id_tipoServicio]= @p_id_tipoServicio)
	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_cnt_contratosXtipoContrato_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_contratosXtipoContrato_m]
GO
CREATE PROCEDURE [sel_cnt_contratosXtipoContrato_m]
	@p_id_tipoContrato INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cnt_contrato]
	WHERE ([id_tipoContrato]= @p_id_tipoContrato)
	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_cnt_contratosXtercero_servicio_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_contratosXtercero_servicio_m]
GO
CREATE PROCEDURE [sel_cnt_contratosXtercero_servicio_m]
	@p_id_tercero_servicio INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cnt_contrato]
	WHERE ([id_tercero_servicio]= @p_id_tercero_servicio)
	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_cnt_contratosXsubtipoDocumento_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_contratosXsubtipoDocumento_m]
GO
CREATE PROCEDURE [sel_cnt_contratosXsubtipoDocumento_m]
	@p_id_subtipoDocumento INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cnt_contrato]
	WHERE ([id_subtipoDocumento]= @p_id_subtipoDocumento)
	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_cnt_contratosXdepartamentoSolicitud_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_contratosXdepartamentoSolicitud_m]
GO
CREATE PROCEDURE [sel_cnt_contratosXdepartamentoSolicitud_m]
	@p_id_departamentoSolicitud INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cnt_contrato]
	WHERE ([id_departamentoSolicitud]= @p_id_departamentoSolicitud)
	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_cnt_contratosXusuario_coordinador_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_contratosXusuario_coordinador_m]
GO
CREATE PROCEDURE [sel_cnt_contratosXusuario_coordinador_m]
	@p_id_usuario_coordinador INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cnt_contrato]
	WHERE ([id_usuario_coordinador]= @p_id_usuario_coordinador)
	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_cnt_contratosXdocumento_alquiler_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_contratosXdocumento_alquiler_m]
GO
CREATE PROCEDURE [sel_cnt_contratosXdocumento_alquiler_m]
	@p_id_documento_alquiler INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cnt_contrato]
	WHERE ([id_documento.alquiler]= @p_id_documento_alquiler)
	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_cnt_contratosXestadoTipoDocumento_inicial_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_contratosXestadoTipoDocumento_inicial_m]
GO
CREATE PROCEDURE [sel_cnt_contratosXestadoTipoDocumento_inicial_m]
	@p_id_estadoTipoDocumento_inicial INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cnt_contrato]
	WHERE ([id_estadoTipoDocumento_inicial]= @p_id_estadoTipoDocumento_inicial)
	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_cnt_contratosXsubtipoDocumento_solicitud_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_contratosXsubtipoDocumento_solicitud_m]
GO
CREATE PROCEDURE [sel_cnt_contratosXsubtipoDocumento_solicitud_m]
	@p_id_subtipoDocumento_solicitud INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cnt_contrato]
	WHERE ([id_subtipoDocumento_solicitud]= @p_id_subtipoDocumento_solicitud)
	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_cnt_contratos_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_contratos_m]
GO
CREATE PROCEDURE [sel_cnt_contratos_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_cnt_contrato]
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_cnt_contratos_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_cnt_contratos_m]
GO
CREATE PROCEDURE [lis_cnt_contratos_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[contrato] AS [cnt_contrato]
	FROM [cnt_contrato]
	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_cnt_contrato]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_cnt_contrato]
GO
CREATE PROCEDURE [aud_cnt_contrato]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [cnt_contrato].id as id_tabla,
	[cnt_contrato].id_usuario_modifico as id_usuario_modifico,
	[cnt_contrato].id_usuario_creo as id_usuario_creo,
	[cnt_contrato].fechaModificacion as fechaModificacion,
	[cnt_contrato].fechaCreacion as fechaCreacion,
	[cnt_contrato].active as active,
	[cnt_contrato].[contrato] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [cnt_contrato]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[cnt_contrato].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[cnt_contrato].[id_usuario_creo]
	where ' + @p_filtro)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'cnt_contrato'
-----------------------------------27/09/2018 15:20:10