
if (not exists(select * FROM INFORMATION_SCHEMA.COLUMNS AS c1 where c1.column_name = 'incluirMenores' and c1.table_name = 'cat_catalogo.tempario'))
begin 
	alter table [cat_catalogo.tempario] add [incluirMenores] bit NOT NULL DEFAULT 0
end
go

if (not exists(select * FROM INFORMATION_SCHEMA.COLUMNS AS c1 where c1.column_name = 'modoAjuste' and c1.table_name = 'cat_catalogo.tempario'))
begin 
	alter table [cat_catalogo.tempario] add [modoAjuste] int NOT NULL DEFAULT 0
end
go

--insercion en tabla de tablas
if not exists (select columna from _columnas where tabla='cat_catalogo.tempario' and columna='incluirMenores')
begin
	insert into _columnas
	(tabla,columna,mostrarEnGrilla,ordenGrilla,requerido,tipo,defecto,tamano)
	values
	('cat_catalogo.tempario','incluirMenores',1,1,1,'bit','((0))',NULL)
end

if not exists (select columna from _columnas where tabla='cat_catalogo.tempario' and columna='modoAjuste')
begin
	insert into _columnas
	(tabla,columna,mostrarEnGrilla,ordenGrilla,requerido,tipo,defecto,tamano)
	values
	('cat_catalogo.tempario','modoAjuste',1,1,1,'int','((0))',NULL)
end


--asigna el bit de incluir menores según como se haya usado en las afiliaciones anteriores
update [cat_catalogo.tempario] 
set incluirmenores=CDV.incluirmenores
from [cat_catalogo.tempario]
inner join cnt_visitafija VF on VF.[id_catalogo.tempario]=[cat_catalogo.tempario].id
inner join cnt_contratoEquipo CE on VF.id_contratoequipo=CE.id
inner join cnt_contratodetallevisita CDV on CE.id_contratodetallevisita=CDV.id


--**************     FALTA     ++**********************
-- actualizar campos según info de contratos actuales

---07/11/2010 12:13:57 p.m.
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_cat_catalogo_equipo_catalogo_tempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_cat_catalogo_equipo_catalogo_tempario]
GO
CREATE PROCEDURE [reg_cat_catalogo_equipo_catalogo_tempario]
	@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],
		[catalogo.equipo_catalogo.tempario] AS [catalogo.equipo_catalogo.tempario],
		[id_catalogo.equipo] AS [id_catalogo.equipo],
		[id_catalogo.tempario] AS [id_catalogo.tempario]
	FROM [cat_catalogo.equipo_catalogo.tempario]
	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_cat_catalogo_equipo_catalogo_tempario]')) DROP VIEW [dbo].[view_cat_catalogo_equipo_catalogo_tempario]
GO
CREATE  VIEW [view_cat_catalogo_equipo_catalogo_tempario] AS
SELECT 	[cat_catalogo.equipo_catalogo.tempario].[id] AS [id],
		[cat_catalogo.equipo_catalogo.tempario].[uid] AS [uid],
		[cat_catalogo.equipo_catalogo.tempario].[eid] AS [eid],
		[cat_catalogo.equipo_catalogo.tempario].[id_usuario_modifico] AS [id_usuario_modifico],
		[cat_catalogo.equipo_catalogo.tempario].[id_usuario_creo] AS [id_usuario_creo],
		[cat_catalogo.equipo_catalogo.tempario].[fechaModificacion] AS [fechaModificacion],
		[cat_catalogo.equipo_catalogo.tempario].[fechaCreacion] AS [fechaCreacion],
		[cat_catalogo.equipo_catalogo.tempario].[active] AS [active],
		[cat_catalogo.equipo_catalogo.tempario].[catalogo.equipo_catalogo.tempario] AS [catalogo.equipo_catalogo.tempario],
		[cat_catalogo.equipo_catalogo.tempario].[id_catalogo.equipo] AS [id_catalogo.equipo],
[cat_catalogo.equipo].[catalogo.equipo] as [cat_catalogo.equipo_catalogo.equipo],
[cat_catalogo.equipo].[garantiaMeses] as [cat_catalogo.equipo_garantiaMeses],
[cat_catalogo.equipo].[garantiaHoras] as [cat_catalogo.equipo_garantiaHoras],
[cat_catalogo.equipo].[manejaHorometro] as [cat_catalogo.equipo_manejaHorometro],
[cat_catalogo.equipo].[id_marca] as [cat_catalogo.equipo_id_marca],
		[cat_catalogo_equipo].[catalogo_codigo] AS [cat_catalogo_equipo_catalogo_codigo],
		[cat_catalogo_equipo].[codigoInventario] AS [cat_catalogo_equipo_codigoInventario],
		[cat_catalogo_equipo].[precioVenta] AS [cat_catalogo_equipo_precioVenta],
		[cat_catalogo_equipo].[tiempoConsecucion] AS [cat_catalogo_equipo_tiempoConsecucion],
		[cat_catalogo_equipo].[esNacional] AS [cat_catalogo_equipo_esNacional],
		[cat_catalogo_equipo].[id_familia] AS [cat_catalogo_equipo_id_familia],
		[cat_catalogo_equipo].[id_subTipoCatalogo] AS [cat_catalogo_equipo_id_subTipoCatalogo],
		[cat_catalogo_equipo].[id_unidad] AS [cat_catalogo_equipo_id_unidad],
		[cat_catalogo_equipo].[id_impuesto] AS [cat_catalogo_equipo_id_impuesto],
		[cat_catalogo_equipo].[descripcion] AS [cat_catalogo_equipo_descripcion],
		[cat_catalogo_equipo].[bloquearCostear] AS [cat_catalogo_equipo_bloquearCostear],
		[cat_catalogo.equipo_catalogo.tempario].[id_catalogo.tempario] AS [id_catalogo.tempario],
[cat_catalogo.tempario].[catalogo.tempario] as [cat_catalogo.tempario_catalogo.tempario],
[cat_catalogo.tempario].[duracionEstimada] as [cat_catalogo.tempario_duracionEstimada],
[cat_catalogo.tempario].[enTaller] as [cat_catalogo.tempario_enTaller],
[cat_catalogo.tempario].[esCiclico] as [cat_catalogo.tempario_esCiclico],
[cat_catalogo.tempario].[detieneEquipos] as [cat_catalogo.tempario_detieneEquipos],
[cat_catalogo.tempario].[horasDetencion] as [cat_catalogo.tempario_horasDetencion],
[cat_catalogo.tempario].[periodoHoras] as [cat_catalogo.tempario_periodoHoras],
[cat_catalogo.tempario].[facturaTodo] as [cat_catalogo.tempario_facturaTodo],
[cat_catalogo.tempario].[periodoDias] as [cat_catalogo.tempario_periodoDias],
[cat_catalogo.tempario].[porcGarantia] as [cat_catalogo.tempario_porcGarantia],
[cat_catalogo.tempario].[porcTropicalizacion] as [cat_catalogo.tempario_porcTropicalizacion],
[cat_catalogo.tempario].[id_tipoServicio] as [cat_catalogo.tempario_id_tipoServicio],
[cat_catalogo.tempario].[proyectar] as [cat_catalogo.tempario_proyectar],
[cat_catalogo.tempario].[incluirMenores] as [cat_catalogo.tempario_incluirMenores],
[cat_catalogo.tempario].[modoAjuste] as [cat_catalogo.tempario_modoAjuste],
		[cat_catalogo_tempario].[catalogo_codigo] AS [cat_catalogo_tempario_catalogo_codigo],
		[cat_catalogo_tempario].[codigoInventario] AS [cat_catalogo_tempario_codigoInventario],
		[cat_catalogo_tempario].[precioVenta] AS [cat_catalogo_tempario_precioVenta],
		[cat_catalogo_tempario].[tiempoConsecucion] AS [cat_catalogo_tempario_tiempoConsecucion],
		[cat_catalogo_tempario].[esNacional] AS [cat_catalogo_tempario_esNacional],
		[cat_catalogo_tempario].[id_familia] AS [cat_catalogo_tempario_id_familia],
		[cat_catalogo_tempario].[id_subTipoCatalogo] AS [cat_catalogo_tempario_id_subTipoCatalogo],
		[cat_catalogo_tempario].[id_unidad] AS [cat_catalogo_tempario_id_unidad],
		[cat_catalogo_tempario].[id_impuesto] AS [cat_catalogo_tempario_id_impuesto],
		[cat_catalogo_tempario].[descripcion] AS [cat_catalogo_tempario_descripcion],
		[cat_catalogo_tempario].[bloquearCostear] AS [cat_catalogo_tempario_bloquearCostear],
		cat_subtipocatalogo.subtipocatalogo as cat_subtipocatalogo_subtipocatalogo,
		[empresaCodigo].[empresa] As [multiempresa]
FROM [cat_catalogo.equipo_catalogo.tempario]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[cat_catalogo.equipo_catalogo.tempario].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[cat_catalogo.equipo_catalogo.tempario].[id_usuario_creo]
	INNER JOIN [cat_catalogo.equipo] ON [cat_catalogo.equipo].id=[cat_catalogo.equipo_catalogo.tempario].[id_catalogo.equipo]
	INNER JOIN [cat_catalogo] aS [cat_catalogo_equipo] ON [cat_catalogo.equipo].id = [cat_catalogo_equipo].id
	INNER JOIN [cat_catalogo.tempario] ON [cat_catalogo.tempario].id=[cat_catalogo.equipo_catalogo.tempario].[id_catalogo.tempario]
	INNER JOIN [cat_catalogo] aS [cat_catalogo_tempario] ON [cat_catalogo.tempario].id = [cat_catalogo_tempario].id
	inner join cat_subtipocatalogo on cat_subtipocatalogo.id = [cat_catalogo_equipo].id_subtipocatalogo
	INNER JOIN [gen_empresa] AS empresaCodigo ON [cat_catalogo.equipo_catalogo.tempario].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_cat_catalogo_equipo_catalogo_tempariosXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_catalogo_equipo_catalogo_tempariosXusuario_modifico]
GO
CREATE PROCEDURE [sel_cat_catalogo_equipo_catalogo_tempariosXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_cat_catalogo.equipo_catalogo.tempario]
	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_cat_catalogo_equipo_catalogo_tempariosXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_catalogo_equipo_catalogo_tempariosXusuario_creo]
GO
CREATE PROCEDURE [sel_cat_catalogo_equipo_catalogo_tempariosXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_cat_catalogo.equipo_catalogo.tempario]
	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_cat_catalogo_equipo_catalogo_tempariosXcatalogo_equipo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_catalogo_equipo_catalogo_tempariosXcatalogo_equipo]
GO
CREATE PROCEDURE [sel_cat_catalogo_equipo_catalogo_tempariosXcatalogo_equipo]
	@p_id_catalogo_equipo INT
AS
BEGIN
	SELECT * 
	FROM [view_cat_catalogo.equipo_catalogo.tempario]
	WHERE ([id_catalogo.equipo]= @p_id_catalogo_equipo)
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_catalogo_equipo_catalogo_tempariosXcatalogo_tempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_catalogo_equipo_catalogo_tempariosXcatalogo_tempario]
GO
CREATE PROCEDURE [sel_cat_catalogo_equipo_catalogo_tempariosXcatalogo_tempario]
	@p_id_catalogo_tempario INT
AS
BEGIN
	SELECT * 
	FROM [view_cat_catalogo.equipo_catalogo.tempario]
	WHERE ([id_catalogo.tempario]= @p_id_catalogo_tempario)
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_catalogo_equipo_catalogo_temparios]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_catalogo_equipo_catalogo_temparios]
GO
CREATE PROCEDURE [sel_cat_catalogo_equipo_catalogo_temparios]
	
	@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_cat_catalogo_equipo_catalogo_tempario]
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_cat_catalogo_equipo_catalogo_temparios]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_cat_catalogo_equipo_catalogo_temparios]
GO
CREATE PROCEDURE [lis_cat_catalogo_equipo_catalogo_temparios]
	AS
BEGIN
	SELECT id AS id,
[catalogo.equipo_catalogo.tempario] AS [cat_catalogo.equipo_catalogo.tempario]
	FROM [cat_catalogo.equipo_catalogo.tempario]
	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_cat_catalogo_equipo_catalogo_tempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_cat_catalogo_equipo_catalogo_tempario]
GO
CREATE PROCEDURE [upd_cat_catalogo_equipo_catalogo_tempario]
	@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_equipo_catalogo_tempario varchar(300) = null,
		@p_id_catalogo_equipo int = null,
		@p_id_catalogo_tempario int = null
AS
BEGIN
		UPDATE [cat_catalogo.equipo_catalogo.tempario]
		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.equipo_catalogo.tempario] = isnull(@p_catalogo_equipo_catalogo_tempario,[catalogo.equipo_catalogo.tempario]),
			[id_catalogo.equipo] = isnull(@p_id_catalogo_equipo,[id_catalogo.equipo]),
			[id_catalogo.tempario] = isnull(@p_id_catalogo_tempario,[id_catalogo.tempario])
		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_cat_catalogo_equipo_catalogo_tempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_cat_catalogo_equipo_catalogo_tempario]
GO
CREATE PROCEDURE [ins_cat_catalogo_equipo_catalogo_tempario]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_catalogo_equipo_catalogo_tempario varchar(300),
		@p_id_catalogo_equipo int,
		@p_id_catalogo_tempario int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [cat_catalogo.equipo_catalogo.tempario]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[catalogo.equipo_catalogo.tempario],
			[id_catalogo.equipo],
			[id_catalogo.tempario])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_catalogo_equipo_catalogo_tempario,
			@p_id_catalogo_equipo,
			@p_id_catalogo_tempario)
		SET @v_id = scope_identity()
UPDATE [cat_catalogo.equipo_catalogo.tempario] 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_cat_catalogo_equipo_catalogo_tempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_cat_catalogo_equipo_catalogo_tempario]
GO
CREATE PROCEDURE [act_cat_catalogo_equipo_catalogo_tempario]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [cat_catalogo.equipo_catalogo.tempario]
	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_cat_catalogo_equipo_catalogo_tempario_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_cat_catalogo_equipo_catalogo_tempario_m]
GO
CREATE PROCEDURE [reg_cat_catalogo_equipo_catalogo_tempario_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],
		[catalogo.equipo_catalogo.tempario] AS [catalogo.equipo_catalogo.tempario],
		[id_catalogo.equipo] AS [id_catalogo.equipo],
		[id_catalogo.tempario] AS [id_catalogo.tempario]
	FROM [cat_catalogo.equipo_catalogo.tempario]
	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_cat_catalogo_equipo_catalogo_tempariosXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_catalogo_equipo_catalogo_tempariosXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_cat_catalogo_equipo_catalogo_tempariosXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cat_catalogo.equipo_catalogo.tempario]
	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_cat_catalogo_equipo_catalogo_tempariosXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_catalogo_equipo_catalogo_tempariosXusuario_creo_m]
GO
CREATE PROCEDURE [sel_cat_catalogo_equipo_catalogo_tempariosXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cat_catalogo.equipo_catalogo.tempario]
	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_cat_catalogo_equipo_catalogo_tempariosXcatalogo_equipo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_catalogo_equipo_catalogo_tempariosXcatalogo_equipo_m]
GO
CREATE PROCEDURE [sel_cat_catalogo_equipo_catalogo_tempariosXcatalogo_equipo_m]
	@p_id_catalogo_equipo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cat_catalogo.equipo_catalogo.tempario]
	WHERE ([id_catalogo.equipo]= @p_id_catalogo_equipo)
	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_cat_catalogo_equipo_catalogo_tempariosXcatalogo_tempario_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_catalogo_equipo_catalogo_tempariosXcatalogo_tempario_m]
GO
CREATE PROCEDURE [sel_cat_catalogo_equipo_catalogo_tempariosXcatalogo_tempario_m]
	@p_id_catalogo_tempario INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cat_catalogo.equipo_catalogo.tempario]
	WHERE ([id_catalogo.tempario]= @p_id_catalogo_tempario)
	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_cat_catalogo_equipo_catalogo_temparios_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_catalogo_equipo_catalogo_temparios_m]
GO
CREATE PROCEDURE [sel_cat_catalogo_equipo_catalogo_temparios_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_cat_catalogo_equipo_catalogo_tempario]
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_cat_catalogo_equipo_catalogo_temparios_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_cat_catalogo_equipo_catalogo_temparios_m]
GO
CREATE PROCEDURE [lis_cat_catalogo_equipo_catalogo_temparios_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[catalogo.equipo_catalogo.tempario] AS [cat_catalogo.equipo_catalogo.tempario]
	FROM [cat_catalogo.equipo_catalogo.tempario]
	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_cat_catalogo_equipo_catalogo_tempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_cat_catalogo_equipo_catalogo_tempario]
GO
CREATE PROCEDURE [aud_cat_catalogo_equipo_catalogo_tempario]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [cat_catalogo.equipo_catalogo.tempario].id as id_tabla,
	[cat_catalogo.equipo_catalogo.tempario].id_usuario_modifico as id_usuario_modifico,
	[cat_catalogo.equipo_catalogo.tempario].id_usuario_creo as id_usuario_creo,
	[cat_catalogo.equipo_catalogo.tempario].fechaModificacion as fechaModificacion,
	[cat_catalogo.equipo_catalogo.tempario].fechaCreacion as fechaCreacion,
	[cat_catalogo.equipo_catalogo.tempario].active as active,
	[cat_catalogo.equipo_catalogo.tempario].[catalogo.equipo_catalogo.tempario] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [cat_catalogo.equipo_catalogo.tempario]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[cat_catalogo.equipo_catalogo.tempario].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[cat_catalogo.equipo_catalogo.tempario].[id_usuario_creo]
	where ' + @p_filtro)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'cat_catalogo.equipo_catalogo.tempario'
--------------------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_cat_catalogo_tempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_cat_catalogo_tempario]
GO
CREATE PROCEDURE [reg_cat_catalogo_tempario]
	@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],
		[catalogo.tempario] AS [catalogo.tempario],
		[duracionEstimada] AS [duracionEstimada],
		[enTaller] AS [enTaller],
		[esCiclico] AS [esCiclico],
		[detieneEquipos] AS [detieneEquipos],
		[horasDetencion] AS [horasDetencion],
		[periodoHoras] AS [periodoHoras],
		[facturaTodo] AS [facturaTodo],
		[periodoDias] AS [periodoDias],
		[porcGarantia] AS [porcGarantia],
		[porcTropicalizacion] AS [porcTropicalizacion],
		[id_tipoServicio] AS [id_tipoServicio],
		[proyectar] AS [proyectar],
		[incluirMenores] AS [incluirMenores],
		[modoAjuste] AS [modoAjuste]
	FROM [cat_catalogo.tempario]
	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_cat_catalogo_tempario]')) DROP VIEW [dbo].[view_cat_catalogo_tempario]
GO
CREATE  VIEW [view_cat_catalogo_tempario] AS
SELECT 	[cat_catalogo.tempario].[id] AS [id],
		[cat_catalogo.tempario].[uid] AS [uid],
		[cat_catalogo.tempario].[eid] AS [eid],
		[cat_catalogo.tempario].[id_usuario_modifico] AS [id_usuario_modifico],
		[cat_catalogo.tempario].[id_usuario_creo] AS [id_usuario_creo],
		[cat_catalogo.tempario].[fechaModificacion] AS [fechaModificacion],
		[cat_catalogo.tempario].[fechaCreacion] AS [fechaCreacion],
		[cat_catalogo.tempario].[active] AS [active],
		[cat_catalogo.tempario].[catalogo.tempario] AS [catalogo.tempario],
		[cat_catalogo.tempario].[duracionEstimada] AS [duracionEstimada],
		[cat_catalogo.tempario].[enTaller] AS [enTaller],
		[cat_catalogo.tempario].[esCiclico] AS [esCiclico],
		[cat_catalogo.tempario].[detieneEquipos] AS [detieneEquipos],
		[cat_catalogo.tempario].[horasDetencion] AS [horasDetencion],
		[cat_catalogo.tempario].[periodoHoras] AS [periodoHoras],
		[cat_catalogo.tempario].[facturaTodo] AS [facturaTodo],
		[cat_catalogo.tempario].[periodoDias] AS [periodoDias],
		[cat_catalogo.tempario].[porcGarantia] AS [porcGarantia],
		[cat_catalogo.tempario].[porcTropicalizacion] AS [porcTropicalizacion],
		[cat_catalogo.tempario].[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],
		[cat_catalogo.tempario].[proyectar] AS [proyectar],
		[cat_catalogo.tempario].[incluirMenores] AS [incluirMenores],
		[cat_catalogo.tempario].[modoAjuste] AS [modoAjuste],
[cat_catalogo_tempario].[catalogo_codigo] AS [cat_catalogo_tempario_catalogo_codigo],
[cat_catalogo_tempario].[codigoInventario] AS [cat_catalogo_tempario_codigoInventario],
[cat_catalogo_tempario].[precioVenta] AS [cat_catalogo_tempario_precioVenta],
[cat_catalogo_tempario].[tiempoConsecucion] AS [cat_catalogo_tempario_tiempoConsecucion],
[cat_catalogo_tempario].[esNacional] AS [cat_catalogo_tempario_esNacional],
[cat_catalogo_tempario].[id_familia] AS [cat_catalogo_tempario_id_familia],
[cat_catalogo_tempario_gen_familia].[familia] as [cat_catalogo_tempario_gen_familia_familia],
[cat_catalogo_tempario_gen_familia].[familia_codigo] as [cat_catalogo_tempario_gen_familia_familia_codigo],
[cat_catalogo_tempario_gen_familia].[id_familia] as [cat_catalogo_tempario_gen_familia_id_familia],
[cat_catalogo_tempario].[id_subTipoCatalogo] AS [cat_catalogo_tempario_id_subTipoCatalogo],
[cat_catalogo_tempario_cat_subtipoCatalogo].[subtipoCatalogo] as [cat_catalogo_tempario_cat_subtipoCatalogo_subtipoCatalogo],
[cat_catalogo_tempario_cat_subtipoCatalogo].[subtipoCatalogo_codigo] as [cat_catalogo_tempario_cat_subtipoCatalogo_subtipoCatalogo_codigo],
[cat_catalogo_tempario_cat_subtipoCatalogo].[id_tipoCatalogo] as [cat_catalogo_tempario_cat_subtipoCatalogo_id_tipoCatalogo],
[cat_catalogo_tempario].[id_unidad] AS [cat_catalogo_tempario_id_unidad],
[cat_catalogo_tempario_gen_unidad].[unidad] as [cat_catalogo_tempario_gen_unidad_unidad],
[cat_catalogo_tempario_gen_unidad].[unidad_codigo] as [cat_catalogo_tempario_gen_unidad_unidad_codigo],
[cat_catalogo_tempario].[id_impuesto] AS [cat_catalogo_tempario_id_impuesto],
[cat_catalogo_tempario_gen_impuesto].[impuesto] as [cat_catalogo_tempario_gen_impuesto_impuesto],
[cat_catalogo_tempario_gen_impuesto].[impuesto_codigo] as [cat_catalogo_tempario_gen_impuesto_impuesto_codigo],
[cat_catalogo_tempario_gen_impuesto].[valor] as [cat_catalogo_tempario_gen_impuesto_valor],
[cat_catalogo_tempario].[descripcion] AS [cat_catalogo_tempario_descripcion],
[cat_catalogo_tempario].[bloquearCostear] AS [cat_catalogo_tempario_bloquearCostear],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [cat_catalogo.tempario]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[cat_catalogo.tempario].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[cat_catalogo.tempario].[id_usuario_creo]
	INNER JOIN [gen_tipoServicio] ON [gen_tipoServicio].id=[cat_catalogo.tempario].[id_tipoServicio]
	INNER JOIN [cat_catalogo] aS [cat_catalogo_tempario] ON [cat_catalogo.tempario].id = [cat_catalogo_tempario].id
	INNER JOIN [gen_familia]as [cat_catalogo_tempario_gen_familia] ON [cat_catalogo_tempario_gen_familia].id=[cat_catalogo_tempario].[id_familia]
	INNER JOIN [cat_subtipoCatalogo]as [cat_catalogo_tempario_cat_subtipoCatalogo] ON [cat_catalogo_tempario_cat_subtipoCatalogo].id=[cat_catalogo_tempario].[id_subTipoCatalogo]
	INNER JOIN [gen_unidad]as [cat_catalogo_tempario_gen_unidad] ON [cat_catalogo_tempario_gen_unidad].id=[cat_catalogo_tempario].[id_unidad]
	INNER JOIN [gen_impuesto]as [cat_catalogo_tempario_gen_impuesto] ON [cat_catalogo_tempario_gen_impuesto].id=[cat_catalogo_tempario].[id_impuesto]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [cat_catalogo.tempario].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [cat_catalogo.tempario].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_cat_catalogo_tempariosXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_catalogo_tempariosXusuario_modifico]
GO
CREATE PROCEDURE [sel_cat_catalogo_tempariosXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_cat_catalogo.tempario]
	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_cat_catalogo_tempariosXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_catalogo_tempariosXusuario_creo]
GO
CREATE PROCEDURE [sel_cat_catalogo_tempariosXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_cat_catalogo.tempario]
	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_cat_catalogo_tempariosXtipoServicio]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_catalogo_tempariosXtipoServicio]
GO
CREATE PROCEDURE [sel_cat_catalogo_tempariosXtipoServicio]
	@p_id_tipoServicio INT
AS
BEGIN
	SELECT * 
	FROM [view_cat_catalogo.tempario]
	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_cat_catalogo_temparios]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_catalogo_temparios]
GO
CREATE PROCEDURE [sel_cat_catalogo_temparios]
	
	@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_cat_catalogo_tempario]
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_cat_catalogo_temparios]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_cat_catalogo_temparios]
GO
CREATE PROCEDURE [lis_cat_catalogo_temparios]
	AS
BEGIN
	SELECT id AS id,
[catalogo.tempario] AS [cat_catalogo.tempario]
	FROM [cat_catalogo.tempario]
	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_cat_catalogo_tempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_cat_catalogo_tempario]
GO
CREATE PROCEDURE [upd_cat_catalogo_tempario]
	@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_active bit = null,
		@p_catalogo_tempario varchar(300) = null,
		@p_duracionEstimada float = null,
		@p_enTaller bit = null,
		@p_esCiclico bit = null,
		@p_detieneEquipos bit = null,
		@p_horasDetencion float = null,
		@p_periodoHoras int = null,
		@p_facturaTodo bit = null,
		@p_periodoDias int = null,
		@p_porcGarantia float = null,
		@p_porcTropicalizacion float = null,
		@p_id_tipoServicio int = null,
		@p_proyectar bit = null,
		@p_incluirMenores bit = null,
		@p_modoAjuste int = null,
		@p_catalogo_codigo varchar(100),
		@p_codigoInventario varchar(50),
		@p_precioVenta money,
		@p_tiempoConsecucion int,
		@p_esNacional bit,
		@p_id_familia int,
		@p_id_subTipoCatalogo int,
		@p_id_unidad int,
		@p_id_impuesto int,
		@p_descripcion varchar(8000),
		@p_bloquearCostear bit
AS
BEGIN
exec [upd_cat_catalogo]
			@p_id,
			@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			@p_fechaCreacion,
			@p_catalogo_tempario,
			@p_catalogo_codigo,
			@p_codigoInventario,
			@p_precioVenta,
			@p_tiempoConsecucion,
			@p_esNacional,
			@p_id_familia,
			@p_id_subTipoCatalogo,
			@p_id_unidad,
			@p_id_impuesto,
			@p_descripcion,
			@p_bloquearCostear
	UPDATE [cat_catalogo.tempario]
		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(),
			[fechaCreacion] = isnull(@p_fechaCreacion,[fechaCreacion]),
			[catalogo.tempario] = isnull(@p_catalogo_tempario,[catalogo.tempario]),
			[duracionEstimada] = isnull(@p_duracionEstimada,[duracionEstimada]),
			[enTaller] = isnull(@p_enTaller,[enTaller]),
			[esCiclico] = isnull(@p_esCiclico,[esCiclico]),
			[detieneEquipos] = isnull(@p_detieneEquipos,[detieneEquipos]),
			[horasDetencion] = isnull(@p_horasDetencion,[horasDetencion]),
			[periodoHoras] = isnull(@p_periodoHoras,[periodoHoras]),
			[facturaTodo] = isnull(@p_facturaTodo,[facturaTodo]),
			[periodoDias] = isnull(@p_periodoDias,[periodoDias]),
			[porcGarantia] = isnull(@p_porcGarantia,[porcGarantia]),
			[porcTropicalizacion] = isnull(@p_porcTropicalizacion,[porcTropicalizacion]),
			[id_tipoServicio] = isnull(@p_id_tipoServicio,[id_tipoServicio]),
			[proyectar] = isnull(@p_proyectar,[proyectar]),
			[incluirMenores] = isnull(@p_incluirMenores,[incluirMenores]),
			[modoAjuste] = isnull(@p_modoAjuste,[modoAjuste])
		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_cat_catalogo_tempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_cat_catalogo_tempario]
GO
CREATE PROCEDURE [ins_cat_catalogo_tempario]
		@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_catalogo_tempario varchar(300),
		@p_duracionEstimada float,
		@p_enTaller bit,
		@p_esCiclico bit,
		@p_detieneEquipos bit,
		@p_horasDetencion float,
		@p_periodoHoras int,
		@p_facturaTodo bit,
		@p_periodoDias int,
		@p_porcGarantia float,
		@p_porcTropicalizacion float,
		@p_id_tipoServicio int,
		@p_proyectar bit,
		@p_incluirMenores bit,
		@p_modoAjuste int,
		@p_catalogo_codigo varchar(100),
		@p_codigoInventario varchar(50),
		@p_precioVenta money,
		@p_tiempoConsecucion int,
		@p_esNacional bit,
		@p_id_familia int,
		@p_id_subTipoCatalogo int,
		@p_id_unidad int,
		@p_id_impuesto int,
		@p_descripcion varchar(8000),
		@p_bloquearCostear bit
AS
BEGIN
	DECLARE @v_id int
exec @v_id = [ins_cat_catalogo]
			@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			@p_catalogo_tempario,
			@p_catalogo_codigo,
			@p_codigoInventario,
			@p_precioVenta,
			@p_tiempoConsecucion,
			@p_esNacional,
			@p_id_familia,
			@p_id_subTipoCatalogo,
			@p_id_unidad,
			@p_id_impuesto,
			@p_descripcion,
			@p_bloquearCostear		
INSERT INTO [cat_catalogo.tempario]
			([id],
			[uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[catalogo.tempario],
			[duracionEstimada],
			[enTaller],
			[esCiclico],
			[detieneEquipos],
			[horasDetencion],
			[periodoHoras],
			[facturaTodo],
			[periodoDias],
			[porcGarantia],
			[porcTropicalizacion],
			[id_tipoServicio],
			[proyectar],
			[incluirMenores],
			[modoAjuste])
		VALUES( @v_id,
			@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_catalogo_tempario,
			@p_duracionEstimada,
			@p_enTaller,
			@p_esCiclico,
			@p_detieneEquipos,
			@p_horasDetencion,
			@p_periodoHoras,
			@p_facturaTodo,
			@p_periodoDias,
			@p_porcGarantia,
			@p_porcTropicalizacion,
			@p_id_tipoServicio,
			@p_proyectar,
			@p_incluirMenores,
			@p_modoAjuste)
UPDATE [cat_catalogo.tempario] set [uid] = [uid] + '_' + convert(varchar(20),@v_id) where [id]=@v_id
SELECT @v_id as 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_cat_catalogo_tempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_cat_catalogo_tempario]
GO
CREATE PROCEDURE [act_cat_catalogo_tempario]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
EXEC [act_cat_catalogo]
	@p_active,
	@p_id,
@p_id_usuario_modifico
	UPDATE [cat_catalogo.tempario]
	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_cat_catalogo_tempario_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_cat_catalogo_tempario_m]
GO
CREATE PROCEDURE [reg_cat_catalogo_tempario_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],
		[catalogo.tempario] AS [catalogo.tempario],
		[duracionEstimada] AS [duracionEstimada],
		[enTaller] AS [enTaller],
		[esCiclico] AS [esCiclico],
		[detieneEquipos] AS [detieneEquipos],
		[horasDetencion] AS [horasDetencion],
		[periodoHoras] AS [periodoHoras],
		[facturaTodo] AS [facturaTodo],
		[periodoDias] AS [periodoDias],
		[porcGarantia] AS [porcGarantia],
		[porcTropicalizacion] AS [porcTropicalizacion],
		[id_tipoServicio] AS [id_tipoServicio],
		[proyectar] AS [proyectar],
		[incluirMenores] AS [incluirMenores],
		[modoAjuste] AS [modoAjuste]
	FROM [cat_catalogo.tempario]
	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_cat_catalogo_tempariosXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_catalogo_tempariosXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_cat_catalogo_tempariosXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cat_catalogo.tempario]
	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_cat_catalogo_tempariosXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_catalogo_tempariosXusuario_creo_m]
GO
CREATE PROCEDURE [sel_cat_catalogo_tempariosXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cat_catalogo.tempario]
	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_cat_catalogo_tempariosXtipoServicio_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_catalogo_tempariosXtipoServicio_m]
GO
CREATE PROCEDURE [sel_cat_catalogo_tempariosXtipoServicio_m]
	@p_id_tipoServicio INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cat_catalogo.tempario]
	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_cat_catalogo_temparios_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_catalogo_temparios_m]
GO
CREATE PROCEDURE [sel_cat_catalogo_temparios_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_cat_catalogo_tempario]
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_cat_catalogo_temparios_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_cat_catalogo_temparios_m]
GO
CREATE PROCEDURE [lis_cat_catalogo_temparios_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[catalogo.tempario] AS [cat_catalogo.tempario]
	FROM [cat_catalogo.tempario]
	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_cat_catalogo_tempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_cat_catalogo_tempario]
GO
CREATE PROCEDURE [aud_cat_catalogo_tempario]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [cat_catalogo.tempario].id as id_tabla,
	[cat_catalogo.tempario].id_usuario_modifico as id_usuario_modifico,
	[cat_catalogo.tempario].id_usuario_creo as id_usuario_creo,
	[cat_catalogo.tempario].fechaModificacion as fechaModificacion,
	[cat_catalogo.tempario].fechaCreacion as fechaCreacion,
	[cat_catalogo.tempario].active as active,
	[cat_catalogo.tempario].[catalogo.tempario] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [cat_catalogo.tempario]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[cat_catalogo.tempario].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[cat_catalogo.tempario].[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_catalogo_equiposXcatalogo_tempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_catalogo_equiposXcatalogo_tempario]
GO
CREATE PROCEDURE [sel_cat_catalogo_equiposXcatalogo_tempario]
	@p_id_cat_catalogo_tempario INT
AS
BEGIN
	SELECT 	[cat_catalogo.equipo_catalogo.tempario].[id] AS [I_id],
		[cat_catalogo.equipo_catalogo.tempario].[uid] AS [I_uid],
		[cat_catalogo.equipo_catalogo.tempario].[eid] AS [I_eid],
		[cat_catalogo.equipo_catalogo.tempario].[fechaModificacion] AS [I_fechaModificacion],
		[cat_catalogo.equipo_catalogo.tempario].[fechaCreacion] AS [I_fechaCreacion],
		[cat_catalogo.equipo_catalogo.tempario].[active] AS [I_active],
		[cat_catalogo.equipo_catalogo.tempario].[catalogo.equipo_catalogo.tempario] AS [I_catalogo.equipo_catalogo.tempario]
	FROM cat_catalogo.equipo_catalogo.tempario INNER JOIN cat_catalogo.equipo
	 ON [cat_catalogo.equipo_catalogo.tempario].[id_catalogo.equipo] = [cat_catalogo.equipo].id
	WHERE	([cat_catalogo.equipo_catalogo.tempario].[id_catalogo.tempario]= @p_id_cat_catalogo_tempario)
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_equipo_catalogo_tempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_cat_catalogo_equipo_catalogo_tempario]
GO
CREATE PROCEDURE [ins_cat_catalogo_equipo_catalogo_tempario]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_catalogo_equipo_catalogo_tempario varchar(300),
		@p_id_catalogo_equipo int,
		@p_id_catalogo_tempario int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [cat_catalogo.equipo_catalogo.tempario]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[catalogo.equipo_catalogo.tempario],
			[id_catalogo.equipo],
			[id_catalogo.tempario])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_catalogo_equipo_catalogo_tempario,
			@p_id_catalogo_equipo,
			@p_id_catalogo_tempario)
		SET @v_id = scope_identity()
UPDATE [cat_catalogo.equipo_catalogo.tempario] 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_equipo_catalogo_tempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_cat_catalogo_equipo_catalogo_tempario]
GO
CREATE PROCEDURE [upd_cat_catalogo_equipo_catalogo_tempario]
	@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_equipo_catalogo_tempario varchar(300) = null,
		@p_id_catalogo_equipo int = null,
		@p_id_catalogo_tempario int = null
AS
BEGIN
		UPDATE [cat_catalogo.equipo_catalogo.tempario]
		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.equipo_catalogo.tempario] = isnull(@p_catalogo_equipo_catalogo_tempario,[catalogo.equipo_catalogo.tempario]),
			[id_catalogo.equipo] = isnull(@p_id_catalogo_equipo,[id_catalogo.equipo]),
			[id_catalogo.tempario] = isnull(@p_id_catalogo_tempario,[id_catalogo.tempario])
		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].[sel_doc_documentosXcatalogo_tempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_documentosXcatalogo_tempario]
GO
CREATE PROCEDURE [sel_doc_documentosXcatalogo_tempario]
	@p_id_cat_catalogo_tempario INT
AS
BEGIN
	SELECT 	[doc_documento_catalogo.tempario].[id] AS [I_id],
		[doc_documento_catalogo.tempario].[uid] AS [I_uid],
		[doc_documento_catalogo.tempario].[eid] AS [I_eid],
		[doc_documento_catalogo.tempario].[fechaModificacion] AS [I_fechaModificacion],
		[doc_documento_catalogo.tempario].[fechaCreacion] AS [I_fechaCreacion],
		[doc_documento_catalogo.tempario].[active] AS [I_active],
		[doc_documento_catalogo.tempario].[documento_catalogo.tempario] AS [I_documento_catalogo.tempario]
	FROM doc_documento_catalogo.tempario INNER JOIN doc_documento
	 ON [doc_documento_catalogo.tempario].[id_documento] = [doc_documento].id
	WHERE	([doc_documento_catalogo.tempario].[id_catalogo.tempario]= @p_id_cat_catalogo_tempario)
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_doc_documento_catalogo_tempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_doc_documento_catalogo_tempario]
GO
CREATE PROCEDURE [ins_doc_documento_catalogo_tempario]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_documento_catalogo_tempario varchar(300),
		@p_id_catalogo_tempario int,
		@p_id_documento int,
		@p_id_itemDocumento int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [doc_documento_catalogo.tempario]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[documento_catalogo.tempario],
			[id_catalogo.tempario],
			[id_documento],
			[id_itemDocumento])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_documento_catalogo_tempario,
			@p_id_catalogo_tempario,
			@p_id_documento,
			@p_id_itemDocumento)
		SET @v_id = scope_identity()
UPDATE [doc_documento_catalogo.tempario] 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_doc_documento_catalogo_tempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_doc_documento_catalogo_tempario]
GO
CREATE PROCEDURE [upd_doc_documento_catalogo_tempario]
	@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_documento_catalogo_tempario varchar(300) = null,
		@p_id_catalogo_tempario int = null,
		@p_id_documento int = null,
		@p_id_itemDocumento int = null
AS
BEGIN
		UPDATE [doc_documento_catalogo.tempario]
		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(),
			[documento_catalogo.tempario] = isnull(@p_documento_catalogo_tempario,[documento_catalogo.tempario]),
			[id_catalogo.tempario] = isnull(@p_id_catalogo_tempario,[id_catalogo.tempario]),
			[id_documento] = isnull(@p_id_documento,[id_documento]),
			[id_itemDocumento] = isnull(@p_id_itemDocumento,[id_itemDocumento])
		WHERE (id = @p_id)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'cat_catalogo.tempario'
--------------------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_cat_detalleTempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_cat_detalleTempario]
GO
CREATE PROCEDURE [reg_cat_detalleTempario]
	@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],
		[detalleTempario] AS [detalleTempario],
		[detalleTempario_codigo] AS [detalleTempario_codigo],
		[cantidadEstandar] AS [cantidadEstandar],
		[orden] AS [orden],
		[cantidadTropicalizada] AS [cantidadTropicalizada],
		[id_catalogo] AS [id_catalogo],
		[id_catalogo.tempario] AS [id_catalogo.tempario],
		[id_pruebaCheckList] AS [id_pruebaCheckList],
		[id_Sistema] AS [id_Sistema],
		[id_detalleTempario_padre] AS [id_detalleTempario_padre]
	FROM [cat_detalleTempario]
	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_cat_detalleTempario]')) DROP VIEW [dbo].[view_cat_detalleTempario]
GO
CREATE  VIEW [view_cat_detalleTempario] AS
SELECT 	[cat_detalleTempario].[id] AS [id],
		[cat_detalleTempario].[uid] AS [uid],
		[cat_detalleTempario].[eid] AS [eid],
		[cat_detalleTempario].[id_usuario_modifico] AS [id_usuario_modifico],
		[cat_detalleTempario].[id_usuario_creo] AS [id_usuario_creo],
		[cat_detalleTempario].[fechaModificacion] AS [fechaModificacion],
		[cat_detalleTempario].[fechaCreacion] AS [fechaCreacion],
		[cat_detalleTempario].[active] AS [active],
		[cat_detalleTempario].[detalleTempario] AS [detalleTempario],
		[cat_detalleTempario].[detalleTempario_codigo] AS [detalleTempario_codigo],
		[cat_detalleTempario].[cantidadEstandar] AS [cantidadEstandar],
		[cat_detalleTempario].[orden] AS [orden],
		[cat_detalleTempario].[cantidadTropicalizada] AS [cantidadTropicalizada],
		[cat_detalleTempario].[id_catalogo] AS [id_catalogo],
[cat_catalogo].[catalogo] as [cat_catalogo_catalogo],
[cat_catalogo].[catalogo_codigo] as [cat_catalogo_catalogo_codigo],
[cat_catalogo].[codigoInventario] as [cat_catalogo_codigoInventario],
[cat_catalogo].[precioVenta] as [cat_catalogo_precioVenta],
[cat_catalogo].[tiempoConsecucion] as [cat_catalogo_tiempoConsecucion],
[cat_catalogo].[esNacional] as [cat_catalogo_esNacional],
[cat_catalogo].[id_familia] as [cat_catalogo_id_familia],
[cat_catalogo].[id_subTipoCatalogo] as [cat_catalogo_id_subTipoCatalogo],
[cat_catalogo].[id_unidad] as [cat_catalogo_id_unidad],
[cat_catalogo].[id_impuesto] as [cat_catalogo_id_impuesto],
[cat_catalogo].[descripcion] as [cat_catalogo_descripcion],
[cat_catalogo].[bloquearCostear] as [cat_catalogo_bloquearCostear],
		[cat_detalleTempario].[id_catalogo.tempario] AS [id_catalogo.tempario],
[cat_catalogo.tempario].[catalogo.tempario] as [cat_catalogo.tempario_catalogo.tempario],
[cat_catalogo.tempario].[duracionEstimada] as [cat_catalogo.tempario_duracionEstimada],
[cat_catalogo.tempario].[enTaller] as [cat_catalogo.tempario_enTaller],
[cat_catalogo.tempario].[esCiclico] as [cat_catalogo.tempario_esCiclico],
[cat_catalogo.tempario].[detieneEquipos] as [cat_catalogo.tempario_detieneEquipos],
[cat_catalogo.tempario].[horasDetencion] as [cat_catalogo.tempario_horasDetencion],
[cat_catalogo.tempario].[periodoHoras] as [cat_catalogo.tempario_periodoHoras],
[cat_catalogo.tempario].[facturaTodo] as [cat_catalogo.tempario_facturaTodo],
[cat_catalogo.tempario].[periodoDias] as [cat_catalogo.tempario_periodoDias],
[cat_catalogo.tempario].[porcGarantia] as [cat_catalogo.tempario_porcGarantia],
[cat_catalogo.tempario].[porcTropicalizacion] as [cat_catalogo.tempario_porcTropicalizacion],
[cat_catalogo.tempario].[id_tipoServicio] as [cat_catalogo.tempario_id_tipoServicio],
[cat_catalogo.tempario].[proyectar] as [cat_catalogo.tempario_proyectar],
[cat_catalogo.tempario].[incluirMenores] as [cat_catalogo.tempario_incluirMenores],
[cat_catalogo.tempario].[modoAjuste] as [cat_catalogo.tempario_modoAjuste],
		[cat_catalogo_tempario].[catalogo_codigo] AS [cat_catalogo_tempario_catalogo_codigo],
		[cat_catalogo_tempario].[codigoInventario] AS [cat_catalogo_tempario_codigoInventario],
		[cat_catalogo_tempario].[precioVenta] AS [cat_catalogo_tempario_precioVenta],
		[cat_catalogo_tempario].[tiempoConsecucion] AS [cat_catalogo_tempario_tiempoConsecucion],
		[cat_catalogo_tempario].[esNacional] AS [cat_catalogo_tempario_esNacional],
		[cat_catalogo_tempario].[id_familia] AS [cat_catalogo_tempario_id_familia],
		[cat_catalogo_tempario].[id_subTipoCatalogo] AS [cat_catalogo_tempario_id_subTipoCatalogo],
		[cat_catalogo_tempario].[id_unidad] AS [cat_catalogo_tempario_id_unidad],
		[cat_catalogo_tempario].[id_impuesto] AS [cat_catalogo_tempario_id_impuesto],
		[cat_catalogo_tempario].[descripcion] AS [cat_catalogo_tempario_descripcion],
		[cat_catalogo_tempario].[bloquearCostear] AS [cat_catalogo_tempario_bloquearCostear],
		[cat_detalleTempario].[id_pruebaCheckList] AS [id_pruebaCheckList],
[cat_pruebaCheckList].[pruebaCheckList] as [cat_pruebaCheckList_pruebaCheckList],
[cat_pruebaCheckList].[pruebaCheckList_codigo] as [cat_pruebaCheckList_pruebaCheckList_codigo],
		[cat_detalleTempario].[id_Sistema] AS [id_Sistema],
[cat_sistema].[sistema] as [cat_sistema_sistema],
[cat_sistema].[sistema_codigo] as [cat_sistema_sistema_codigo],
		[cat_detalleTempario].[id_detalleTempario_padre] AS [id_detalleTempario_padre],
[cat_detalleTempario_padre].[detalleTempario] as [cat_detalleTempario_padre_detalleTempario],
[cat_detalleTempario_padre].[detalleTempario_codigo] as [cat_detalleTempario_padre_detalleTempario_codigo],
[cat_detalleTempario_padre].[cantidadEstandar] as [cat_detalleTempario_padre_cantidadEstandar],
[cat_detalleTempario_padre].[orden] as [cat_detalleTempario_padre_orden],
[cat_detalleTempario_padre].[cantidadTropicalizada] as [cat_detalleTempario_padre_cantidadTropicalizada],
[cat_detalleTempario_padre].[id_catalogo] as [cat_detalleTempario_padre_id_catalogo],
[cat_detalleTempario_padre].[id_catalogo.tempario] as [cat_detalleTempario_padre_id_catalogo.tempario],
[cat_detalleTempario_padre].[id_pruebaCheckList] as [cat_detalleTempario_padre_id_pruebaCheckList],
[cat_detalleTempario_padre].[id_Sistema] as [cat_detalleTempario_padre_id_Sistema],
[cat_detalleTempario_padre].[id_detalleTempario_padre] as [cat_detalleTempario_padre_id_detalleTempario_padre],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [cat_detalleTempario]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[cat_detalleTempario].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[cat_detalleTempario].[id_usuario_creo]
	INNER JOIN [cat_catalogo] ON [cat_catalogo].id=[cat_detalleTempario].[id_catalogo]
	INNER JOIN [cat_catalogo.tempario] ON [cat_catalogo.tempario].id=[cat_detalleTempario].[id_catalogo.tempario]
	INNER JOIN [cat_catalogo] aS [cat_catalogo_tempario] ON [cat_catalogo.tempario].id = [cat_catalogo_tempario].id
	INNER JOIN [cat_pruebaCheckList] ON [cat_pruebaCheckList].id=[cat_detalleTempario].[id_pruebaCheckList]
	INNER JOIN [cat_sistema] ON [cat_sistema].id=[cat_detalleTempario].[id_Sistema]
	INNER JOIN [cat_detalleTempario] AS [cat_detalleTempario_padre] ON [cat_detalleTempario_padre].id=[cat_detalleTempario].[id_detalleTempario_padre]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [cat_detalleTempario].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [cat_detalleTempario].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_cat_detalleTempariosXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_detalleTempariosXusuario_modifico]
GO
CREATE PROCEDURE [sel_cat_detalleTempariosXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_cat_detalleTempario]
	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_cat_detalleTempariosXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_detalleTempariosXusuario_creo]
GO
CREATE PROCEDURE [sel_cat_detalleTempariosXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_cat_detalleTempario]
	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_cat_detalleTempariosXcatalogo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_detalleTempariosXcatalogo]
GO
CREATE PROCEDURE [sel_cat_detalleTempariosXcatalogo]
	@p_id_catalogo INT
AS
BEGIN
	SELECT * 
	FROM [view_cat_detalleTempario]
	WHERE ([id_catalogo]= @p_id_catalogo)
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_detalleTempariosXcatalogo_tempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_detalleTempariosXcatalogo_tempario]
GO
CREATE PROCEDURE [sel_cat_detalleTempariosXcatalogo_tempario]
	@p_id_catalogo_tempario INT
AS
BEGIN
	SELECT * 
	FROM [view_cat_detalleTempario]
	WHERE ([id_catalogo.tempario]= @p_id_catalogo_tempario)
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_detalleTempariosXpruebaCheckList]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_detalleTempariosXpruebaCheckList]
GO
CREATE PROCEDURE [sel_cat_detalleTempariosXpruebaCheckList]
	@p_id_pruebaCheckList INT
AS
BEGIN
	SELECT * 
	FROM [view_cat_detalleTempario]
	WHERE ([id_pruebaCheckList]= @p_id_pruebaCheckList)
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_detalleTempariosXSistema]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_detalleTempariosXSistema]
GO
CREATE PROCEDURE [sel_cat_detalleTempariosXSistema]
	@p_id_Sistema INT
AS
BEGIN
	SELECT * 
	FROM [view_cat_detalleTempario]
	WHERE ([id_Sistema]= @p_id_Sistema)
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_detalleTempariosXdetalleTempario_padre]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_detalleTempariosXdetalleTempario_padre]
GO
CREATE PROCEDURE [sel_cat_detalleTempariosXdetalleTempario_padre]
	@p_id_detalleTempario_padre INT
AS
BEGIN
	SELECT * 
	FROM [view_cat_detalleTempario]
	WHERE ([id_detalleTempario_padre]= @p_id_detalleTempario_padre)
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_detalleTemparios]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_detalleTemparios]
GO
CREATE PROCEDURE [sel_cat_detalleTemparios]
	
	@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_cat_detalleTempario]
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_cat_detalleTemparios]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_cat_detalleTemparios]
GO
CREATE PROCEDURE [lis_cat_detalleTemparios]
	AS
BEGIN
	SELECT id AS id,
[detalleTempario] AS [cat_detalleTempario]
	FROM [cat_detalleTempario]
	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_cat_detalleTempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_cat_detalleTempario]
GO
CREATE PROCEDURE [upd_cat_detalleTempario]
	@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_detalleTempario varchar(300) = null,
		@p_detalleTempario_codigo varchar(100) = null,
		@p_cantidadEstandar float = null,
		@p_orden int = null,
		@p_cantidadTropicalizada float = null,
		@p_id_catalogo int = null,
		@p_id_catalogo_tempario int = null,
		@p_id_pruebaCheckList int = null,
		@p_id_Sistema int = null,
		@p_id_detalleTempario_padre int = null
AS
BEGIN
		UPDATE [cat_detalleTempario]
		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(),
			[detalleTempario] = isnull(@p_detalleTempario,[detalleTempario]),
			[detalleTempario_codigo] = isnull(@p_detalleTempario_codigo,[detalleTempario_codigo]),
			[cantidadEstandar] = isnull(@p_cantidadEstandar,[cantidadEstandar]),
			[orden] = isnull(@p_orden,[orden]),
			[cantidadTropicalizada] = isnull(@p_cantidadTropicalizada,[cantidadTropicalizada]),
			[id_catalogo] = isnull(@p_id_catalogo,[id_catalogo]),
			[id_catalogo.tempario] = isnull(@p_id_catalogo_tempario,[id_catalogo.tempario]),
			[id_pruebaCheckList] = isnull(@p_id_pruebaCheckList,[id_pruebaCheckList]),
			[id_Sistema] = isnull(@p_id_Sistema,[id_Sistema]),
			[id_detalleTempario_padre] = isnull(@p_id_detalleTempario_padre,[id_detalleTempario_padre])
		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_cat_detalleTempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_cat_detalleTempario]
GO
CREATE PROCEDURE [ins_cat_detalleTempario]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_detalleTempario varchar(300),
		@p_detalleTempario_codigo varchar(100),
		@p_cantidadEstandar float,
		@p_orden int,
		@p_cantidadTropicalizada float,
		@p_id_catalogo int,
		@p_id_catalogo_tempario int,
		@p_id_pruebaCheckList int,
		@p_id_Sistema int,
		@p_id_detalleTempario_padre int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [cat_detalleTempario]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[detalleTempario],
			[detalleTempario_codigo],
			[cantidadEstandar],
			[orden],
			[cantidadTropicalizada],
			[id_catalogo],
			[id_catalogo.tempario],
			[id_pruebaCheckList],
			[id_Sistema],
			[id_detalleTempario_padre])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_detalleTempario,
			@p_detalleTempario_codigo,
			@p_cantidadEstandar,
			@p_orden,
			@p_cantidadTropicalizada,
			@p_id_catalogo,
			@p_id_catalogo_tempario,
			@p_id_pruebaCheckList,
			@p_id_Sistema,
			@p_id_detalleTempario_padre)
		SET @v_id = scope_identity()
UPDATE [cat_detalleTempario] 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_cat_detalleTempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_cat_detalleTempario]
GO
CREATE PROCEDURE [act_cat_detalleTempario]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [cat_detalleTempario]
	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_cat_detalleTempario_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_cat_detalleTempario_m]
GO
CREATE PROCEDURE [reg_cat_detalleTempario_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],
		[detalleTempario] AS [detalleTempario],
		[detalleTempario_codigo] AS [detalleTempario_codigo],
		[cantidadEstandar] AS [cantidadEstandar],
		[orden] AS [orden],
		[cantidadTropicalizada] AS [cantidadTropicalizada],
		[id_catalogo] AS [id_catalogo],
		[id_catalogo.tempario] AS [id_catalogo.tempario],
		[id_pruebaCheckList] AS [id_pruebaCheckList],
		[id_Sistema] AS [id_Sistema],
		[id_detalleTempario_padre] AS [id_detalleTempario_padre]
	FROM [cat_detalleTempario]
	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_cat_detalleTempariosXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_detalleTempariosXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_cat_detalleTempariosXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cat_detalleTempario]
	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_cat_detalleTempariosXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_detalleTempariosXusuario_creo_m]
GO
CREATE PROCEDURE [sel_cat_detalleTempariosXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cat_detalleTempario]
	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_cat_detalleTempariosXcatalogo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_detalleTempariosXcatalogo_m]
GO
CREATE PROCEDURE [sel_cat_detalleTempariosXcatalogo_m]
	@p_id_catalogo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cat_detalleTempario]
	WHERE ([id_catalogo]= @p_id_catalogo)
	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_cat_detalleTempariosXcatalogo_tempario_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_detalleTempariosXcatalogo_tempario_m]
GO
CREATE PROCEDURE [sel_cat_detalleTempariosXcatalogo_tempario_m]
	@p_id_catalogo_tempario INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cat_detalleTempario]
	WHERE ([id_catalogo.tempario]= @p_id_catalogo_tempario)
	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_cat_detalleTempariosXpruebaCheckList_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_detalleTempariosXpruebaCheckList_m]
GO
CREATE PROCEDURE [sel_cat_detalleTempariosXpruebaCheckList_m]
	@p_id_pruebaCheckList INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cat_detalleTempario]
	WHERE ([id_pruebaCheckList]= @p_id_pruebaCheckList)
	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_cat_detalleTempariosXSistema_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_detalleTempariosXSistema_m]
GO
CREATE PROCEDURE [sel_cat_detalleTempariosXSistema_m]
	@p_id_Sistema INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cat_detalleTempario]
	WHERE ([id_Sistema]= @p_id_Sistema)
	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_cat_detalleTempariosXdetalleTempario_padre_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_detalleTempariosXdetalleTempario_padre_m]
GO
CREATE PROCEDURE [sel_cat_detalleTempariosXdetalleTempario_padre_m]
	@p_id_detalleTempario_padre INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cat_detalleTempario]
	WHERE ([id_detalleTempario_padre]= @p_id_detalleTempario_padre)
	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_cat_detalleTemparios_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_detalleTemparios_m]
GO
CREATE PROCEDURE [sel_cat_detalleTemparios_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_cat_detalleTempario]
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_cat_detalleTemparios_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_cat_detalleTemparios_m]
GO
CREATE PROCEDURE [lis_cat_detalleTemparios_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[detalleTempario] AS [cat_detalleTempario]
	FROM [cat_detalleTempario]
	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_cat_detalleTempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_cat_detalleTempario]
GO
CREATE PROCEDURE [aud_cat_detalleTempario]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [cat_detalleTempario].id as id_tabla,
	[cat_detalleTempario].id_usuario_modifico as id_usuario_modifico,
	[cat_detalleTempario].id_usuario_creo as id_usuario_creo,
	[cat_detalleTempario].fechaModificacion as fechaModificacion,
	[cat_detalleTempario].fechaCreacion as fechaCreacion,
	[cat_detalleTempario].active as active,
	[cat_detalleTempario].[detalleTempario] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [cat_detalleTempario]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[cat_detalleTempario].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[cat_detalleTempario].[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_versionEquiposXdetalleTempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_versionEquiposXdetalleTempario]
GO
CREATE PROCEDURE [sel_cat_versionEquiposXdetalleTempario]
	@p_id_cat_detalleTempario INT
AS
BEGIN
	SELECT 	[cat_versionEquipo_detalleTempario].[id] AS [I_id],
		[cat_versionEquipo_detalleTempario].[uid] AS [I_uid],
		[cat_versionEquipo_detalleTempario].[eid] AS [I_eid],
		[cat_versionEquipo_detalleTempario].[fechaModificacion] AS [I_fechaModificacion],
		[cat_versionEquipo_detalleTempario].[fechaCreacion] AS [I_fechaCreacion],
		[cat_versionEquipo_detalleTempario].[active] AS [I_active],
		[cat_versionEquipo_detalleTempario].[versionEquipo_detalleTempario] AS [I_versionEquipo_detalleTempario]
	FROM cat_versionEquipo_detalleTempario INNER JOIN cat_versionEquipo
	 ON [cat_versionEquipo_detalleTempario].[id_versionEquipo] = [cat_versionEquipo].id
	WHERE	([cat_versionEquipo_detalleTempario].[id_detalleTempario]= @p_id_cat_detalleTempario)
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_versionEquipo_detalleTempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_cat_versionEquipo_detalleTempario]
GO
CREATE PROCEDURE [ins_cat_versionEquipo_detalleTempario]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_versionEquipo_detalleTempario varchar(300),
		@p_id_detalleTempario int,
		@p_id_versionEquipo int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [cat_versionEquipo_detalleTempario]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[versionEquipo_detalleTempario],
			[id_detalleTempario],
			[id_versionEquipo])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_versionEquipo_detalleTempario,
			@p_id_detalleTempario,
			@p_id_versionEquipo)
		SET @v_id = scope_identity()
UPDATE [cat_versionEquipo_detalleTempario] 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_versionEquipo_detalleTempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_cat_versionEquipo_detalleTempario]
GO
CREATE PROCEDURE [upd_cat_versionEquipo_detalleTempario]
	@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_versionEquipo_detalleTempario varchar(300) = null,
		@p_id_detalleTempario int = null,
		@p_id_versionEquipo int = null
AS
BEGIN
		UPDATE [cat_versionEquipo_detalleTempario]
		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(),
			[versionEquipo_detalleTempario] = isnull(@p_versionEquipo_detalleTempario,[versionEquipo_detalleTempario]),
			[id_detalleTempario] = isnull(@p_id_detalleTempario,[id_detalleTempario]),
			[id_versionEquipo] = isnull(@p_id_versionEquipo,[id_versionEquipo])
		WHERE (id = @p_id)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'cat_detalleTempario'
--------------------------------
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_visitaFija]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_cnt_visitaFija]
GO
CREATE PROCEDURE [reg_cnt_visitaFija]
	@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],
		[visitaFija] AS [visitaFija],
		[visitaFija_codigo] AS [visitaFija_codigo],
		[fecha_ff] AS [fecha_ff],
		[fechaCreada_ff] AS [fechaCreada_ff],
		[horometro] AS [horometro],
		[id_documento.ot] AS [id_documento.ot],
		[id_contratoEquipo] AS [id_contratoEquipo],
		[id_contratoDetalleVisita] AS [id_contratoDetalleVisita],
		[id_catalogo.tempario] AS [id_catalogo.tempario],
		[esPorTrabajo] AS [esPorTrabajo]
	FROM [cnt_visitaFija]
	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_visitaFija]')) DROP VIEW [dbo].[view_cnt_visitaFija]
GO
CREATE  VIEW [view_cnt_visitaFija] AS
SELECT 	[cnt_visitaFija].[id] AS [id],
		[cnt_visitaFija].[uid] AS [uid],
		[cnt_visitaFija].[eid] AS [eid],
		[cnt_visitaFija].[id_usuario_modifico] AS [id_usuario_modifico],
		[cnt_visitaFija].[id_usuario_creo] AS [id_usuario_creo],
		[cnt_visitaFija].[fechaModificacion] AS [fechaModificacion],
		[cnt_visitaFija].[fechaCreacion] AS [fechaCreacion],
		[cnt_visitaFija].[active] AS [active],
		[cnt_visitaFija].[visitaFija] AS [visitaFija],
		[cnt_visitaFija].[visitaFija_codigo] AS [visitaFija_codigo],
		[cnt_visitaFija].[fecha_ff] AS [fecha_ff],
		[cnt_visitaFija].[fechaCreada_ff] AS [fechaCreada_ff],
		[cnt_visitaFija].[horometro] AS [horometro],
		[cnt_visitaFija].[id_documento.ot] AS [id_documento.ot],
[doc_documento.ot].[documento.ot] as [doc_documento.ot_documento.ot],
[doc_documento.ot].[motivoServicio] as [doc_documento.ot_motivoServicio],
[doc_documento.ot].[enTaller] as [doc_documento.ot_enTaller],
[doc_documento.ot].[diagnosticoInicial] as [doc_documento.ot_diagnosticoInicial],
[doc_documento.ot].[contacto] as [doc_documento.ot_contacto],
[doc_documento.ot].[cargo] as [doc_documento.ot_cargo],
[doc_documento.ot].[telefono] as [doc_documento.ot_telefono],
[doc_documento.ot].[fax] as [doc_documento.ot_fax],
[doc_documento.ot].[email] as [doc_documento.ot_email],
[doc_documento.ot].[direccionUbicacion] as [doc_documento.ot_direccionUbicacion],
[doc_documento.ot].[duracionEstimada] as [doc_documento.ot_duracionEstimada],
[doc_documento.ot].[fechaGarantia_ff] as [doc_documento.ot_fechaGarantia_ff],
[doc_documento.ot].[fechaCompromiso_fh] as [doc_documento.ot_fechaCompromiso_fh],
[doc_documento.ot].[trabajos] as [doc_documento.ot_trabajos],
[doc_documento.ot].[recomendaciones] as [doc_documento.ot_recomendaciones],
[doc_documento.ot].[compromisos] as [doc_documento.ot_compromisos],
[doc_documento.ot].[reclamacionFabrica] as [doc_documento.ot_reclamacionFabrica],
[doc_documento.ot].[porcentajeCliente] as [doc_documento.ot_porcentajeCliente],
[doc_documento.ot].[porcentajeDistribuidor] as [doc_documento.ot_porcentajeDistribuidor],
[doc_documento.ot].[porcentajeFabricante] as [doc_documento.ot_porcentajeFabricante],
[doc_documento.ot].[segunTempario] as [doc_documento.ot_segunTempario],
[doc_documento.ot].[distribuyeCentros] as [doc_documento.ot_distribuyeCentros],
[doc_documento.ot].[id_tipoServicio] as [doc_documento.ot_id_tipoServicio],
[doc_documento.ot].[id_equipo] as [doc_documento.ot_id_equipo],
[doc_documento.ot].[id_usuario_coordinador] as [doc_documento.ot_id_usuario_coordinador],
[doc_documento.ot].[id_motivoCancelacion] as [doc_documento.ot_id_motivoCancelacion],
[doc_documento.ot].[id_sucursal] as [doc_documento.ot_id_sucursal],
[doc_documento.ot].[id_sistema] as [doc_documento.ot_id_sistema],
[doc_documento.ot].[id_departamentoSolicitud] as [doc_documento.ot_id_departamentoSolicitud],
[doc_documento.ot].[id_contrato] as [doc_documento.ot_id_contrato],
[doc_documento.ot].[id_zona] as [doc_documento.ot_id_zona],
		[doc_documento_ot].[documento_codigo] AS [doc_documento_ot_documento_codigo],
		[doc_documento_ot].[documento_numero] AS [doc_documento_ot_documento_numero],
		[doc_documento_ot].[prefijo] AS [doc_documento_ot_prefijo],
		[doc_documento_ot].[subtotal] AS [doc_documento_ot_subtotal],
		[doc_documento_ot].[descuento] AS [doc_documento_ot_descuento],
		[doc_documento_ot].[iva] AS [doc_documento_ot_iva],
		[doc_documento_ot].[total] AS [doc_documento_ot_total],
		[doc_documento_ot].[costo] AS [doc_documento_ot_costo],
		[doc_documento_ot].[rentabilidad] AS [doc_documento_ot_rentabilidad],
		[doc_documento_ot].[trm] AS [doc_documento_ot_trm],
		[doc_documento_ot].[fecha_fh] AS [doc_documento_ot_fecha_fh],
		[doc_documento_ot].[fechaSugerida_fh] AS [doc_documento_ot_fechaSugerida_fh],
		[doc_documento_ot].[fechaCierre_ff] AS [doc_documento_ot_fechaCierre_ff],
		[doc_documento_ot].[fechaLimite_fh] AS [doc_documento_ot_fechaLimite_fh],
		[doc_documento_ot].[id_subtipoDocumento] AS [doc_documento_ot_id_subtipoDocumento],
		[doc_documento_ot].[id_tercero_cliente] AS [doc_documento_ot_id_tercero_cliente],
		[doc_documento_ot].[id_documento] AS [doc_documento_ot_id_documento],
		[doc_documento_ot].[id_tercero_proveedor] AS [doc_documento_ot_id_tercero_proveedor],
		[doc_documento_ot].[id_prioridadDocumento] AS [doc_documento_ot_id_prioridadDocumento],
		[doc_documento_ot].[id_moneda] AS [doc_documento_ot_id_moneda],
		[doc_documento_ot].[id_estadoTipoDocumento] AS [doc_documento_ot_id_estadoTipoDocumento],
		[doc_documento_ot].[id_motivoestado] AS [doc_documento_ot_id_motivoestado],
		[doc_documento_ot].[subtotalPlaneado] AS [doc_documento_ot_subtotalPlaneado],
		[doc_documento_ot].[descuentoPlaneado] AS [doc_documento_ot_descuentoPlaneado],
		[doc_documento_ot].[ivaPlaneado] AS [doc_documento_ot_ivaPlaneado],
		[doc_documento_ot].[totalPlaneado] AS [doc_documento_ot_totalPlaneado],
		[doc_documento_ot].[costoPlaneado] AS [doc_documento_ot_costoPlaneado],
		[cnt_visitaFija].[id_contratoEquipo] AS [id_contratoEquipo],
[cnt_contratoEquipo].[contratoEquipo] as [cnt_contratoEquipo_contratoEquipo],
[cnt_contratoEquipo].[contratoEquipo_codigo] as [cnt_contratoEquipo_contratoEquipo_codigo],
[cnt_contratoEquipo].[precioVisita] as [cnt_contratoEquipo_precioVisita],
[cnt_contratoEquipo].[precioServicio] as [cnt_contratoEquipo_precioServicio],
[cnt_contratoEquipo].[fecha_ff] as [cnt_contratoEquipo_fecha_ff],
[cnt_contratoEquipo].[promedio] as [cnt_contratoEquipo_promedio],
[cnt_contratoEquipo].[id_equipo] as [cnt_contratoEquipo_id_equipo],
[cnt_contratoEquipo].[id_contrato] as [cnt_contratoEquipo_id_contrato],
[cnt_contratoEquipo].[id_contratoDetalleVisita] as [cnt_contratoEquipo_id_contratoDetalleVisita],
		[cnt_visitaFija].[id_contratoDetalleVisita] AS [id_contratoDetalleVisita],
[cnt_contratoDetalleVisita].[contratoDetalleVisita] as [cnt_contratoDetalleVisita_contratoDetalleVisita],
[cnt_contratoDetalleVisita].[contratoDetalleVisita_codigo] as [cnt_contratoDetalleVisita_contratoDetalleVisita_codigo],
[cnt_contratoDetalleVisita].[frecuencia] as [cnt_contratoDetalleVisita_frecuencia],
[cnt_contratoDetalleVisita].[cadaN] as [cnt_contratoDetalleVisita_cadaN],
[cnt_contratoDetalleVisita].[dia] as [cnt_contratoDetalleVisita_dia],
[cnt_contratoDetalleVisita].[estrategia] as [cnt_contratoDetalleVisita_estrategia],
[cnt_contratoDetalleVisita].[porTrabajo] as [cnt_contratoDetalleVisita_porTrabajo],
[cnt_contratoDetalleVisita].[incluirMenores] as [cnt_contratoDetalleVisita_incluirMenores],
[cnt_contratoDetalleVisita].[aproximarA] as [cnt_contratoDetalleVisita_aproximarA],
[cnt_contratoDetalleVisita].[fecha_ff] as [cnt_contratoDetalleVisita_fecha_ff],
[cnt_contratoDetalleVisita].[id_contrato] as [cnt_contratoDetalleVisita_id_contrato],
		[cnt_visitaFija].[id_catalogo.tempario] AS [id_catalogo.tempario],
[cat_catalogo.tempario].[catalogo.tempario] as [cat_catalogo.tempario_catalogo.tempario],
[cat_catalogo.tempario].[duracionEstimada] as [cat_catalogo.tempario_duracionEstimada],
[cat_catalogo.tempario].[enTaller] as [cat_catalogo.tempario_enTaller],
[cat_catalogo.tempario].[esCiclico] as [cat_catalogo.tempario_esCiclico],
[cat_catalogo.tempario].[detieneEquipos] as [cat_catalogo.tempario_detieneEquipos],
[cat_catalogo.tempario].[horasDetencion] as [cat_catalogo.tempario_horasDetencion],
[cat_catalogo.tempario].[periodoHoras] as [cat_catalogo.tempario_periodoHoras],
[cat_catalogo.tempario].[facturaTodo] as [cat_catalogo.tempario_facturaTodo],
[cat_catalogo.tempario].[periodoDias] as [cat_catalogo.tempario_periodoDias],
[cat_catalogo.tempario].[porcGarantia] as [cat_catalogo.tempario_porcGarantia],
[cat_catalogo.tempario].[porcTropicalizacion] as [cat_catalogo.tempario_porcTropicalizacion],
[cat_catalogo.tempario].[id_tipoServicio] as [cat_catalogo.tempario_id_tipoServicio],
[cat_catalogo.tempario].[proyectar] as [cat_catalogo.tempario_proyectar],
[cat_catalogo.tempario].[incluirMenores] as [cat_catalogo.tempario_incluirMenores],
[cat_catalogo.tempario].[modoAjuste] as [cat_catalogo.tempario_modoAjuste],
		[cat_catalogo_tempario].[catalogo_codigo] AS [cat_catalogo_tempario_catalogo_codigo],
		[cat_catalogo_tempario].[codigoInventario] AS [cat_catalogo_tempario_codigoInventario],
		[cat_catalogo_tempario].[precioVenta] AS [cat_catalogo_tempario_precioVenta],
		[cat_catalogo_tempario].[tiempoConsecucion] AS [cat_catalogo_tempario_tiempoConsecucion],
		[cat_catalogo_tempario].[esNacional] AS [cat_catalogo_tempario_esNacional],
		[cat_catalogo_tempario].[id_familia] AS [cat_catalogo_tempario_id_familia],
		[cat_catalogo_tempario].[id_subTipoCatalogo] AS [cat_catalogo_tempario_id_subTipoCatalogo],
		[cat_catalogo_tempario].[id_unidad] AS [cat_catalogo_tempario_id_unidad],
		[cat_catalogo_tempario].[id_impuesto] AS [cat_catalogo_tempario_id_impuesto],
		[cat_catalogo_tempario].[descripcion] AS [cat_catalogo_tempario_descripcion],
		[cat_catalogo_tempario].[bloquearCostear] AS [cat_catalogo_tempario_bloquearCostear],
		[cnt_visitaFija].[esPorTrabajo] AS [esPorTrabajo],
		[empresaCodigo].[empresa] As [multiempresa],
		cnt_contrato.fechafin_ff as fechavencimiento,
		cnt_tipocontrato.tipocontrato as tipocontrato,
		cnt_tipocontrato.id as cnt_tipocontrato_id ,
		equ_equipo.id as equ_equipo_id,		
		equ_equipo.equipo as equ_equipo_equipo,
		equ_equipo.equipo_serial as equ_equipo_equipo_serial,
		cnt_contrato.contrato as cnt_contrato_contrato,
		[gen_zona].id as gen_zona_id

FROM [cnt_visitaFija]
		INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[cnt_visitaFija].[id_usuario_modifico]
		INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[cnt_visitaFija].[id_usuario_creo]
		INNER JOIN [cnt_contratoEquipo] ON [cnt_contratoEquipo].id=[cnt_visitaFija].[id_contratoEquipo]
		INNER JOIN equ_equipo on equ_equipo.id = [cnt_contratoEquipo].id_equipo
		INNER JOIN [gen_zona] on [gen_zona].id = equ_equipo.id_zona
		INNER JOIN [cnt_contrato] on [cnt_contrato].id=[cnt_contratoEquipo].id_Contrato
		INNER JOIN cnt_tipocontrato on cnt_tipocontrato.id=[cnt_contrato].id_tipocontrato
		INNER JOIN [cnt_contratoDetalleVisita] ON [cnt_contratoDetalleVisita].id=[cnt_visitaFija].[id_contratoDetalleVisita]
		INNER JOIN [cat_catalogo.tempario] ON [cat_catalogo.tempario].id=[cnt_visitaFija].[id_catalogo.tempario]
		INNER JOIN [cat_catalogo] aS [cat_catalogo_tempario] ON [cat_catalogo.tempario].id = [cat_catalogo_tempario].id
		INNER JOIN [doc_documento.ot] ON [doc_documento.ot].id=[cnt_visitaFija].[id_documento.ot]
		INNER JOIN [doc_documento] aS [doc_documento_ot] ON [doc_documento.ot].id = [doc_documento_ot].id
		INNER JOIN [gen_empresa] AS empresaCodigo ON [cnt_visitaFija].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
		
WHERE [cnt_visitaFija].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_visitaFijasXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_visitaFijasXusuario_modifico]
GO
CREATE PROCEDURE [sel_cnt_visitaFijasXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_cnt_visitaFija]
	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_visitaFijasXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_visitaFijasXusuario_creo]
GO
CREATE PROCEDURE [sel_cnt_visitaFijasXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_cnt_visitaFija]
	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_visitaFijasXdocumento_ot]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_visitaFijasXdocumento_ot]
GO
CREATE PROCEDURE [sel_cnt_visitaFijasXdocumento_ot]
	@p_id_documento_ot INT
AS
BEGIN
	SELECT * 
	FROM [view_cnt_visitaFija]
	WHERE ([id_documento.ot]= @p_id_documento_ot)
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_visitaFijasXcontratoEquipo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_visitaFijasXcontratoEquipo]
GO
CREATE PROCEDURE [sel_cnt_visitaFijasXcontratoEquipo]
	@p_id_contratoEquipo INT
AS
BEGIN
	SELECT * 
	FROM [view_cnt_visitaFija]
	WHERE ([id_contratoEquipo]= @p_id_contratoEquipo)
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_visitaFijasXcontratoDetalleVisita]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_visitaFijasXcontratoDetalleVisita]
GO
CREATE PROCEDURE [sel_cnt_visitaFijasXcontratoDetalleVisita]
	@p_id_contratoDetalleVisita INT
AS
BEGIN
	SELECT * 
	FROM [view_cnt_visitaFija]
	WHERE ([id_contratoDetalleVisita]= @p_id_contratoDetalleVisita)
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_visitaFijasXcatalogo_tempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_visitaFijasXcatalogo_tempario]
GO
CREATE PROCEDURE [sel_cnt_visitaFijasXcatalogo_tempario]
	@p_id_catalogo_tempario INT
AS
BEGIN
	SELECT * 
	FROM [view_cnt_visitaFija]
	WHERE ([id_catalogo.tempario]= @p_id_catalogo_tempario)
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_visitaFijas]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_visitaFijas]
GO
CREATE PROCEDURE [sel_cnt_visitaFijas]
	
	@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_visitaFija]
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_visitaFijas]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_cnt_visitaFijas]
GO
CREATE PROCEDURE [lis_cnt_visitaFijas]
	AS
BEGIN
	SELECT id AS id,
[visitaFija] AS [cnt_visitaFija]
	FROM [cnt_visitaFija]
	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_visitaFija]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_cnt_visitaFija]
GO
CREATE PROCEDURE [upd_cnt_visitaFija]
	@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_visitaFija varchar(300) = null,
		@p_visitaFija_codigo varchar(100) = null,
		@p_fecha_ff smalldatetime = null,
		@p_fechaCreada_ff smalldatetime = null,
		@p_horometro float = null,
		@p_id_documento_ot int = null,
		@p_id_contratoEquipo int = null,
		@p_id_contratoDetalleVisita int = null,
		@p_id_catalogo_tempario int = null,
		@p_esPorTrabajo bit = null
AS
BEGIN
		UPDATE [cnt_visitaFija]
		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(),
			[visitaFija] = isnull(@p_visitaFija,[visitaFija]),
			[visitaFija_codigo] = isnull(@p_visitaFija_codigo,[visitaFija_codigo]),
			[fecha_ff] = isnull(@p_fecha_ff,[fecha_ff]),
			[fechaCreada_ff] = isnull(@p_fechaCreada_ff,[fechaCreada_ff]),
			[horometro] = isnull(@p_horometro,[horometro]),
			[id_documento.ot] = isnull(@p_id_documento_ot,[id_documento.ot]),
			[id_contratoEquipo] = isnull(@p_id_contratoEquipo,[id_contratoEquipo]),
			[id_contratoDetalleVisita] = isnull(@p_id_contratoDetalleVisita,[id_contratoDetalleVisita]),
			[id_catalogo.tempario] = isnull(@p_id_catalogo_tempario,[id_catalogo.tempario]),
			[esPorTrabajo] = isnull(@p_esPorTrabajo,[esPorTrabajo])
		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_visitaFija]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_cnt_visitaFija]
GO
CREATE PROCEDURE [ins_cnt_visitaFija]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_visitaFija varchar(300),
		@p_visitaFija_codigo varchar(100),
		@p_fecha_ff smalldatetime,
		@p_fechaCreada_ff smalldatetime,
		@p_horometro float,
		@p_id_documento_ot int,
		@p_id_contratoEquipo int,
		@p_id_contratoDetalleVisita int,
		@p_id_catalogo_tempario int,
		@p_esPorTrabajo bit
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [cnt_visitaFija]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[visitaFija],
			[visitaFija_codigo],
			[fecha_ff],
			[fechaCreada_ff],
			[horometro],
			[id_documento.ot],
			[id_contratoEquipo],
			[id_contratoDetalleVisita],
			[id_catalogo.tempario],
			[esPorTrabajo])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_visitaFija,
			@p_visitaFija_codigo,
			@p_fecha_ff,
			@p_fechaCreada_ff,
			@p_horometro,
			@p_id_documento_ot,
			@p_id_contratoEquipo,
			@p_id_contratoDetalleVisita,
			@p_id_catalogo_tempario,
			@p_esPorTrabajo)
		SET @v_id = scope_identity()
UPDATE [cnt_visitaFija] 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_visitaFija]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_cnt_visitaFija]
GO
CREATE PROCEDURE [act_cnt_visitaFija]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [cnt_visitaFija]
	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_visitaFija_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_cnt_visitaFija_m]
GO
CREATE PROCEDURE [reg_cnt_visitaFija_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],
		[visitaFija] AS [visitaFija],
		[visitaFija_codigo] AS [visitaFija_codigo],
		[fecha_ff] AS [fecha_ff],
		[fechaCreada_ff] AS [fechaCreada_ff],
		[horometro] AS [horometro],
		[id_documento.ot] AS [id_documento.ot],
		[id_contratoEquipo] AS [id_contratoEquipo],
		[id_contratoDetalleVisita] AS [id_contratoDetalleVisita],
		[id_catalogo.tempario] AS [id_catalogo.tempario],
		[esPorTrabajo] AS [esPorTrabajo]
	FROM [cnt_visitaFija]
	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_visitaFijasXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_visitaFijasXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_cnt_visitaFijasXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cnt_visitaFija]
	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_visitaFijasXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_visitaFijasXusuario_creo_m]
GO
CREATE PROCEDURE [sel_cnt_visitaFijasXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cnt_visitaFija]
	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_visitaFijasXdocumento_ot_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_visitaFijasXdocumento_ot_m]
GO
CREATE PROCEDURE [sel_cnt_visitaFijasXdocumento_ot_m]
	@p_id_documento_ot INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cnt_visitaFija]
	WHERE ([id_documento.ot]= @p_id_documento_ot)
	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_visitaFijasXcontratoEquipo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_visitaFijasXcontratoEquipo_m]
GO
CREATE PROCEDURE [sel_cnt_visitaFijasXcontratoEquipo_m]
	@p_id_contratoEquipo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cnt_visitaFija]
	WHERE ([id_contratoEquipo]= @p_id_contratoEquipo)
	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_visitaFijasXcontratoDetalleVisita_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_visitaFijasXcontratoDetalleVisita_m]
GO
CREATE PROCEDURE [sel_cnt_visitaFijasXcontratoDetalleVisita_m]
	@p_id_contratoDetalleVisita INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cnt_visitaFija]
	WHERE ([id_contratoDetalleVisita]= @p_id_contratoDetalleVisita)
	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_visitaFijasXcatalogo_tempario_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_visitaFijasXcatalogo_tempario_m]
GO
CREATE PROCEDURE [sel_cnt_visitaFijasXcatalogo_tempario_m]
	@p_id_catalogo_tempario INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cnt_visitaFija]
	WHERE ([id_catalogo.tempario]= @p_id_catalogo_tempario)
	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_visitaFijas_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cnt_visitaFijas_m]
GO
CREATE PROCEDURE [sel_cnt_visitaFijas_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_visitaFija]
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_visitaFijas_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_cnt_visitaFijas_m]
GO
CREATE PROCEDURE [lis_cnt_visitaFijas_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[visitaFija] AS [cnt_visitaFija]
	FROM [cnt_visitaFija]
	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_visitaFija]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_cnt_visitaFija]
GO
CREATE PROCEDURE [aud_cnt_visitaFija]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [cnt_visitaFija].id as id_tabla,
	[cnt_visitaFija].id_usuario_modifico as id_usuario_modifico,
	[cnt_visitaFija].id_usuario_creo as id_usuario_creo,
	[cnt_visitaFija].fechaModificacion as fechaModificacion,
	[cnt_visitaFija].fechaCreacion as fechaCreacion,
	[cnt_visitaFija].active as active,
	[cnt_visitaFija].[visitaFija] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [cnt_visitaFija]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[cnt_visitaFija].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[cnt_visitaFija].[id_usuario_creo]
	where ' + @p_filtro)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'cnt_visitaFija'
--------------------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_doc_documento_catalogo_tempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_doc_documento_catalogo_tempario]
GO
CREATE PROCEDURE [reg_doc_documento_catalogo_tempario]
	@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],
		[documento_catalogo.tempario] AS [documento_catalogo.tempario],
		[id_catalogo.tempario] AS [id_catalogo.tempario],
		[id_documento] AS [id_documento],
		[id_itemDocumento] AS [id_itemDocumento]
	FROM [doc_documento_catalogo.tempario]
	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_doc_documento_catalogo_tempario]')) DROP VIEW [dbo].[view_doc_documento_catalogo_tempario]
GO
CREATE  VIEW [view_doc_documento_catalogo_tempario] AS
SELECT 	[doc_documento_catalogo.tempario].[id] AS [id],
		[doc_documento_catalogo.tempario].[uid] AS [uid],
		[doc_documento_catalogo.tempario].[eid] AS [eid],
		[doc_documento_catalogo.tempario].[id_usuario_modifico] AS [id_usuario_modifico],
		[doc_documento_catalogo.tempario].[id_usuario_creo] AS [id_usuario_creo],
		[doc_documento_catalogo.tempario].[fechaModificacion] AS [fechaModificacion],
		[doc_documento_catalogo.tempario].[fechaCreacion] AS [fechaCreacion],
		[doc_documento_catalogo.tempario].[active] AS [active],
		[doc_documento_catalogo.tempario].[documento_catalogo.tempario] AS [documento_catalogo.tempario],
		[doc_documento_catalogo.tempario].[id_catalogo.tempario] AS [id_catalogo.tempario],
[cat_catalogo.tempario].[catalogo.tempario] as [cat_catalogo.tempario_catalogo.tempario],
[cat_catalogo.tempario].[duracionEstimada] as [cat_catalogo.tempario_duracionEstimada],
[cat_catalogo.tempario].[enTaller] as [cat_catalogo.tempario_enTaller],
[cat_catalogo.tempario].[esCiclico] as [cat_catalogo.tempario_esCiclico],
[cat_catalogo.tempario].[detieneEquipos] as [cat_catalogo.tempario_detieneEquipos],
[cat_catalogo.tempario].[horasDetencion] as [cat_catalogo.tempario_horasDetencion],
[cat_catalogo.tempario].[periodoHoras] as [cat_catalogo.tempario_periodoHoras],
[cat_catalogo.tempario].[facturaTodo] as [cat_catalogo.tempario_facturaTodo],
[cat_catalogo.tempario].[periodoDias] as [cat_catalogo.tempario_periodoDias],
[cat_catalogo.tempario].[porcGarantia] as [cat_catalogo.tempario_porcGarantia],
[cat_catalogo.tempario].[porcTropicalizacion] as [cat_catalogo.tempario_porcTropicalizacion],
[cat_catalogo.tempario].[id_tipoServicio] as [cat_catalogo.tempario_id_tipoServicio],
[cat_catalogo.tempario].[proyectar] as [cat_catalogo.tempario_proyectar],
[cat_catalogo.tempario].[incluirMenores] as [cat_catalogo.tempario_incluirMenores],
[cat_catalogo.tempario].[modoAjuste] as [cat_catalogo.tempario_modoAjuste],
		[cat_catalogo_tempario].[catalogo_codigo] AS [cat_catalogo_tempario_catalogo_codigo],
		[cat_catalogo_tempario].[codigoInventario] AS [cat_catalogo_tempario_codigoInventario],
		[cat_catalogo_tempario].[precioVenta] AS [cat_catalogo_tempario_precioVenta],
		[cat_catalogo_tempario].[tiempoConsecucion] AS [cat_catalogo_tempario_tiempoConsecucion],
		[cat_catalogo_tempario].[esNacional] AS [cat_catalogo_tempario_esNacional],
		[cat_catalogo_tempario].[id_familia] AS [cat_catalogo_tempario_id_familia],
		[cat_catalogo_tempario].[id_subTipoCatalogo] AS [cat_catalogo_tempario_id_subTipoCatalogo],
		[cat_catalogo_tempario].[id_unidad] AS [cat_catalogo_tempario_id_unidad],
		[cat_catalogo_tempario].[id_impuesto] AS [cat_catalogo_tempario_id_impuesto],
		[cat_catalogo_tempario].[descripcion] AS [cat_catalogo_tempario_descripcion],
		[cat_catalogo_tempario].[bloquearCostear] AS [cat_catalogo_tempario_bloquearCostear],
		[doc_documento_catalogo.tempario].[id_documento] AS [id_documento],
[doc_documento].[documento] as [doc_documento_documento],
[doc_documento].[documento_codigo] as [doc_documento_documento_codigo],
[doc_documento].[documento_numero] as [doc_documento_documento_numero],
[doc_documento].[prefijo] as [doc_documento_prefijo],
[doc_documento].[subtotal] as [doc_documento_subtotal],
[doc_documento].[descuento] as [doc_documento_descuento],
[doc_documento].[iva] as [doc_documento_iva],
[doc_documento].[total] as [doc_documento_total],
[doc_documento].[costo] as [doc_documento_costo],
[doc_documento].[rentabilidad] as [doc_documento_rentabilidad],
[doc_documento].[trm] as [doc_documento_trm],
[doc_documento].[fecha_fh] as [doc_documento_fecha_fh],
[doc_documento].[fechaSugerida_fh] as [doc_documento_fechaSugerida_fh],
[doc_documento].[fechaCierre_ff] as [doc_documento_fechaCierre_ff],
[doc_documento].[fechaLimite_fh] as [doc_documento_fechaLimite_fh],
[doc_documento].[id_subtipoDocumento] as [doc_documento_id_subtipoDocumento],
[doc_documento].[id_tercero_cliente] as [doc_documento_id_tercero_cliente],
[doc_documento].[id_documento] as [doc_documento_id_documento],
[doc_documento].[id_tercero_proveedor] as [doc_documento_id_tercero_proveedor],
[doc_documento].[id_prioridadDocumento] as [doc_documento_id_prioridadDocumento],
[doc_documento].[id_moneda] as [doc_documento_id_moneda],
[doc_documento].[id_estadoTipoDocumento] as [doc_documento_id_estadoTipoDocumento],
[doc_documento].[id_motivoestado] as [doc_documento_id_motivoestado],
[doc_documento].[subtotalPlaneado] as [doc_documento_subtotalPlaneado],
[doc_documento].[descuentoPlaneado] as [doc_documento_descuentoPlaneado],
[doc_documento].[ivaPlaneado] as [doc_documento_ivaPlaneado],
[doc_documento].[totalPlaneado] as [doc_documento_totalPlaneado],
[doc_documento].[costoPlaneado] as [doc_documento_costoPlaneado],
		[doc_documento_catalogo.tempario].[id_itemDocumento] AS [id_itemDocumento],
[doc_itemDocumento].[itemDocumento] as [doc_itemDocumento_itemDocumento],
[doc_itemDocumento].[itemDocumento_codigo] as [doc_itemDocumento_itemDocumento_codigo],
[doc_itemDocumento].[detalle] as [doc_itemDocumento_detalle],
[doc_itemDocumento].[horasOrdinales] as [doc_itemDocumento_horasOrdinales],
[doc_itemDocumento].[horasFestivas] as [doc_itemDocumento_horasFestivas],
[doc_itemDocumento].[horasNocturnas] as [doc_itemDocumento_horasNocturnas],
[doc_itemDocumento].[cantidadPlaneado] as [doc_itemDocumento_cantidadPlaneado],
[doc_itemDocumento].[cantidadEjecutado] as [doc_itemDocumento_cantidadEjecutado],
[doc_itemDocumento].[valorUnitarioPlaneado] as [doc_itemDocumento_valorUnitarioPlaneado],
[doc_itemDocumento].[valorUnitarioEjecutado] as [doc_itemDocumento_valorUnitarioEjecutado],
[doc_itemDocumento].[subtotalPlaneado] as [doc_itemDocumento_subtotalPlaneado],
[doc_itemDocumento].[subtotalEjecutado] as [doc_itemDocumento_subtotalEjecutado],
[doc_itemDocumento].[totalPlaneado] as [doc_itemDocumento_totalPlaneado],
[doc_itemDocumento].[totalEjecutado] as [doc_itemDocumento_totalEjecutado],
[doc_itemDocumento].[porciva] as [doc_itemDocumento_porciva],
[doc_itemDocumento].[ivaPlaneado] as [doc_itemDocumento_ivaPlaneado],
[doc_itemDocumento].[ivaEjecutado] as [doc_itemDocumento_ivaEjecutado],
[doc_itemDocumento].[porcdto] as [doc_itemDocumento_porcdto],
[doc_itemDocumento].[dtoPlaneado] as [doc_itemDocumento_dtoPlaneado],
[doc_itemDocumento].[dtoEjecutado] as [doc_itemDocumento_dtoEjecutado],
[doc_itemDocumento].[costear] as [doc_itemDocumento_costear],
[doc_itemDocumento].[esUrgente] as [doc_itemDocumento_esUrgente],
[doc_itemDocumento].[vencimientoGarantia_ff] as [doc_itemDocumento_vencimientoGarantia_ff],
[doc_itemDocumento].[horasGarantia] as [doc_itemDocumento_horasGarantia],
[doc_itemDocumento].[porcentajeCliente] as [doc_itemDocumento_porcentajeCliente],
[doc_itemDocumento].[porcentajeDistribuidor] as [doc_itemDocumento_porcentajeDistribuidor],
[doc_itemDocumento].[porcentajeFabricante] as [doc_itemDocumento_porcentajeFabricante],
[doc_itemDocumento].[afectoInventario] as [doc_itemDocumento_afectoInventario],
[doc_itemDocumento].[id_documento] as [doc_itemDocumento_id_documento],
[doc_itemDocumento].[id_itemDocumento] as [doc_itemDocumento_id_itemDocumento],
[doc_itemDocumento].[id_centroCosto] as [doc_itemDocumento_id_centroCosto],
[doc_itemDocumento].[id_catalogo] as [doc_itemDocumento_id_catalogo],
[doc_itemDocumento].[id_bodega] as [doc_itemDocumento_id_bodega],
[doc_itemDocumento].[id_incoterm] as [doc_itemDocumento_id_incoterm],
[doc_itemDocumento].[id_itemDocumento_superior] as [doc_itemDocumento_id_itemDocumento_superior],
[doc_itemDocumento].[costoPlaneado] as [doc_itemDocumento_costoPlaneado],
[doc_itemDocumento].[costoEjecutado] as [doc_itemDocumento_costoEjecutado],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [doc_documento_catalogo.tempario]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[doc_documento_catalogo.tempario].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[doc_documento_catalogo.tempario].[id_usuario_creo]
	INNER JOIN [cat_catalogo.tempario] ON [cat_catalogo.tempario].id=[doc_documento_catalogo.tempario].[id_catalogo.tempario]
	INNER JOIN [cat_catalogo] aS [cat_catalogo_tempario] ON [cat_catalogo.tempario].id = [cat_catalogo_tempario].id
	INNER JOIN [doc_documento] ON [doc_documento].id=[doc_documento_catalogo.tempario].[id_documento]
	INNER JOIN [doc_itemDocumento] ON [doc_itemDocumento].id=[doc_documento_catalogo.tempario].[id_itemDocumento]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [doc_documento_catalogo.tempario].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_doc_documento_catalogo_tempariosXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_documento_catalogo_tempariosXusuario_modifico]
GO
CREATE PROCEDURE [sel_doc_documento_catalogo_tempariosXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_doc_documento_catalogo.tempario]
	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_doc_documento_catalogo_tempariosXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_documento_catalogo_tempariosXusuario_creo]
GO
CREATE PROCEDURE [sel_doc_documento_catalogo_tempariosXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_doc_documento_catalogo.tempario]
	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_doc_documento_catalogo_tempariosXcatalogo_tempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_documento_catalogo_tempariosXcatalogo_tempario]
GO
CREATE PROCEDURE [sel_doc_documento_catalogo_tempariosXcatalogo_tempario]
	@p_id_catalogo_tempario INT
AS
BEGIN
	SELECT * 
	FROM [view_doc_documento_catalogo.tempario]
	WHERE ([id_catalogo.tempario]= @p_id_catalogo_tempario)
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_doc_documento_catalogo_tempariosXdocumento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_documento_catalogo_tempariosXdocumento]
GO
CREATE PROCEDURE [sel_doc_documento_catalogo_tempariosXdocumento]
	@p_id_documento INT
AS
BEGIN
	SELECT * 
	FROM [view_doc_documento_catalogo.tempario]
	WHERE ([id_documento]= @p_id_documento)
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_doc_documento_catalogo_tempariosXitemDocumento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_documento_catalogo_tempariosXitemDocumento]
GO
CREATE PROCEDURE [sel_doc_documento_catalogo_tempariosXitemDocumento]
	@p_id_itemDocumento INT
AS
BEGIN
	SELECT * 
	FROM [view_doc_documento_catalogo.tempario]
	WHERE ([id_itemDocumento]= @p_id_itemDocumento)
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_doc_documento_catalogo_temparios]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_documento_catalogo_temparios]
GO
CREATE PROCEDURE [sel_doc_documento_catalogo_temparios]
	
	@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_doc_documento_catalogo_tempario]
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_doc_documento_catalogo_temparios]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_doc_documento_catalogo_temparios]
GO
CREATE PROCEDURE [lis_doc_documento_catalogo_temparios]
	AS
BEGIN
	SELECT id AS id,
[documento_catalogo.tempario] AS [doc_documento_catalogo.tempario]
	FROM [doc_documento_catalogo.tempario]
	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_doc_documento_catalogo_tempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_doc_documento_catalogo_tempario]
GO
CREATE PROCEDURE [upd_doc_documento_catalogo_tempario]
	@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_documento_catalogo_tempario varchar(300) = null,
		@p_id_catalogo_tempario int = null,
		@p_id_documento int = null,
		@p_id_itemDocumento int = null
AS
BEGIN
		UPDATE [doc_documento_catalogo.tempario]
		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(),
			[documento_catalogo.tempario] = isnull(@p_documento_catalogo_tempario,[documento_catalogo.tempario]),
			[id_catalogo.tempario] = isnull(@p_id_catalogo_tempario,[id_catalogo.tempario]),
			[id_documento] = isnull(@p_id_documento,[id_documento]),
			[id_itemDocumento] = isnull(@p_id_itemDocumento,[id_itemDocumento])
		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_doc_documento_catalogo_tempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_doc_documento_catalogo_tempario]
GO
CREATE PROCEDURE [ins_doc_documento_catalogo_tempario]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_documento_catalogo_tempario varchar(300),
		@p_id_catalogo_tempario int,
		@p_id_documento int,
		@p_id_itemDocumento int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [doc_documento_catalogo.tempario]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[documento_catalogo.tempario],
			[id_catalogo.tempario],
			[id_documento],
			[id_itemDocumento])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_documento_catalogo_tempario,
			@p_id_catalogo_tempario,
			@p_id_documento,
			@p_id_itemDocumento)
		SET @v_id = scope_identity()
UPDATE [doc_documento_catalogo.tempario] 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_doc_documento_catalogo_tempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_doc_documento_catalogo_tempario]
GO
CREATE PROCEDURE [act_doc_documento_catalogo_tempario]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [doc_documento_catalogo.tempario]
	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_doc_documento_catalogo_tempario_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_doc_documento_catalogo_tempario_m]
GO
CREATE PROCEDURE [reg_doc_documento_catalogo_tempario_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],
		[documento_catalogo.tempario] AS [documento_catalogo.tempario],
		[id_catalogo.tempario] AS [id_catalogo.tempario],
		[id_documento] AS [id_documento],
		[id_itemDocumento] AS [id_itemDocumento]
	FROM [doc_documento_catalogo.tempario]
	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_doc_documento_catalogo_tempariosXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_documento_catalogo_tempariosXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_doc_documento_catalogo_tempariosXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_doc_documento_catalogo.tempario]
	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_doc_documento_catalogo_tempariosXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_documento_catalogo_tempariosXusuario_creo_m]
GO
CREATE PROCEDURE [sel_doc_documento_catalogo_tempariosXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_doc_documento_catalogo.tempario]
	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_doc_documento_catalogo_tempariosXcatalogo_tempario_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_documento_catalogo_tempariosXcatalogo_tempario_m]
GO
CREATE PROCEDURE [sel_doc_documento_catalogo_tempariosXcatalogo_tempario_m]
	@p_id_catalogo_tempario INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_doc_documento_catalogo.tempario]
	WHERE ([id_catalogo.tempario]= @p_id_catalogo_tempario)
	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_doc_documento_catalogo_tempariosXdocumento_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_documento_catalogo_tempariosXdocumento_m]
GO
CREATE PROCEDURE [sel_doc_documento_catalogo_tempariosXdocumento_m]
	@p_id_documento INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_doc_documento_catalogo.tempario]
	WHERE ([id_documento]= @p_id_documento)
	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_doc_documento_catalogo_tempariosXitemDocumento_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_documento_catalogo_tempariosXitemDocumento_m]
GO
CREATE PROCEDURE [sel_doc_documento_catalogo_tempariosXitemDocumento_m]
	@p_id_itemDocumento INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_doc_documento_catalogo.tempario]
	WHERE ([id_itemDocumento]= @p_id_itemDocumento)
	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_doc_documento_catalogo_temparios_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_documento_catalogo_temparios_m]
GO
CREATE PROCEDURE [sel_doc_documento_catalogo_temparios_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_doc_documento_catalogo_tempario]
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_doc_documento_catalogo_temparios_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_doc_documento_catalogo_temparios_m]
GO
CREATE PROCEDURE [lis_doc_documento_catalogo_temparios_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[documento_catalogo.tempario] AS [doc_documento_catalogo.tempario]
	FROM [doc_documento_catalogo.tempario]
	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_doc_documento_catalogo_tempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_doc_documento_catalogo_tempario]
GO
CREATE PROCEDURE [aud_doc_documento_catalogo_tempario]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [doc_documento_catalogo.tempario].id as id_tabla,
	[doc_documento_catalogo.tempario].id_usuario_modifico as id_usuario_modifico,
	[doc_documento_catalogo.tempario].id_usuario_creo as id_usuario_creo,
	[doc_documento_catalogo.tempario].fechaModificacion as fechaModificacion,
	[doc_documento_catalogo.tempario].fechaCreacion as fechaCreacion,
	[doc_documento_catalogo.tempario].active as active,
	[doc_documento_catalogo.tempario].[documento_catalogo.tempario] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [doc_documento_catalogo.tempario]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[doc_documento_catalogo.tempario].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[doc_documento_catalogo.tempario].[id_usuario_creo]
	where ' + @p_filtro)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'doc_documento_catalogo.tempario'
--------------------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_equ_campana]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_equ_campana]
GO
CREATE PROCEDURE [reg_equ_campana]
	@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],
		[campana] AS [campana],
		[campana_codigo] AS [campana_codigo],
		[detalleCampana] AS [detalleCampana],
		[fechaInicio_ff] AS [fechaInicio_ff],
		[fechafin_ff] AS [fechafin_ff],
		[activo] AS [activo],
		[id_tercero] AS [id_tercero],
		[id_catalogo.tempario] AS [id_catalogo.tempario]
	FROM [equ_campana]
	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_equ_campana]')) DROP VIEW [dbo].[view_equ_campana]
GO
CREATE  VIEW [view_equ_campana] AS
SELECT 	[equ_campana].[id] AS [id],
		[equ_campana].[uid] AS [uid],
		[equ_campana].[eid] AS [eid],
		[equ_campana].[id_usuario_modifico] AS [id_usuario_modifico],
		[equ_campana].[id_usuario_creo] AS [id_usuario_creo],
		[equ_campana].[fechaModificacion] AS [fechaModificacion],
		[equ_campana].[fechaCreacion] AS [fechaCreacion],
		[equ_campana].[active] AS [active],
		[equ_campana].[campana] AS [campana],
		[equ_campana].[campana_codigo] AS [campana_codigo],
		[equ_campana].[detalleCampana] AS [detalleCampana],
		[equ_campana].[fechaInicio_ff] AS [fechaInicio_ff],
		[equ_campana].[fechafin_ff] AS [fechafin_ff],
		[equ_campana].[activo] AS [activo],
		[equ_campana].[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_listaPrecio] as [ter_tercero_id_listaPrecio],
[ter_tercero].[id_naturalezaTercero] as [ter_tercero_id_naturalezaTercero],
		[equ_campana].[id_catalogo.tempario] AS [id_catalogo.tempario],
[cat_catalogo.tempario].[catalogo.tempario] as [cat_catalogo.tempario_catalogo.tempario],
[cat_catalogo.tempario].[duracionEstimada] as [cat_catalogo.tempario_duracionEstimada],
[cat_catalogo.tempario].[enTaller] as [cat_catalogo.tempario_enTaller],
[cat_catalogo.tempario].[esCiclico] as [cat_catalogo.tempario_esCiclico],
[cat_catalogo.tempario].[detieneEquipos] as [cat_catalogo.tempario_detieneEquipos],
[cat_catalogo.tempario].[horasDetencion] as [cat_catalogo.tempario_horasDetencion],
[cat_catalogo.tempario].[periodoHoras] as [cat_catalogo.tempario_periodoHoras],
[cat_catalogo.tempario].[facturaTodo] as [cat_catalogo.tempario_facturaTodo],
[cat_catalogo.tempario].[periodoDias] as [cat_catalogo.tempario_periodoDias],
[cat_catalogo.tempario].[porcGarantia] as [cat_catalogo.tempario_porcGarantia],
[cat_catalogo.tempario].[porcTropicalizacion] as [cat_catalogo.tempario_porcTropicalizacion],
[cat_catalogo.tempario].[id_tipoServicio] as [cat_catalogo.tempario_id_tipoServicio],
[cat_catalogo.tempario].[proyectar] as [cat_catalogo.tempario_proyectar],
[cat_catalogo.tempario].[incluirMenores] as [cat_catalogo.tempario_incluirMenores],
[cat_catalogo.tempario].[modoAjuste] as [cat_catalogo.tempario_modoAjuste],
		[cat_catalogo_tempario].[catalogo_codigo] AS [cat_catalogo_tempario_catalogo_codigo],
		[cat_catalogo_tempario].[codigoInventario] AS [cat_catalogo_tempario_codigoInventario],
		[cat_catalogo_tempario].[precioVenta] AS [cat_catalogo_tempario_precioVenta],
		[cat_catalogo_tempario].[tiempoConsecucion] AS [cat_catalogo_tempario_tiempoConsecucion],
		[cat_catalogo_tempario].[esNacional] AS [cat_catalogo_tempario_esNacional],
		[cat_catalogo_tempario].[id_familia] AS [cat_catalogo_tempario_id_familia],
		[cat_catalogo_tempario].[id_subTipoCatalogo] AS [cat_catalogo_tempario_id_subTipoCatalogo],
		[cat_catalogo_tempario].[id_unidad] AS [cat_catalogo_tempario_id_unidad],
		[cat_catalogo_tempario].[id_impuesto] AS [cat_catalogo_tempario_id_impuesto],
		[cat_catalogo_tempario].[descripcion] AS [cat_catalogo_tempario_descripcion],
		[cat_catalogo_tempario].[bloquearCostear] AS [cat_catalogo_tempario_bloquearCostear],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [equ_campana]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[equ_campana].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[equ_campana].[id_usuario_creo]
	INNER JOIN [ter_tercero] ON [ter_tercero].id=[equ_campana].[id_tercero]
	INNER JOIN [cat_catalogo.tempario] ON [cat_catalogo.tempario].id=[equ_campana].[id_catalogo.tempario]
	INNER JOIN [cat_catalogo] aS [cat_catalogo_tempario] ON [cat_catalogo.tempario].id = [cat_catalogo_tempario].id
	INNER JOIN [gen_empresa] AS empresaCodigo ON [equ_campana].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [equ_campana].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_equ_campanasXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_campanasXusuario_modifico]
GO
CREATE PROCEDURE [sel_equ_campanasXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_campana]
	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_equ_campanasXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_campanasXusuario_creo]
GO
CREATE PROCEDURE [sel_equ_campanasXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_campana]
	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_equ_campanasXtercero]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_campanasXtercero]
GO
CREATE PROCEDURE [sel_equ_campanasXtercero]
	@p_id_tercero INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_campana]
	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_equ_campanasXcatalogo_tempario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_campanasXcatalogo_tempario]
GO
CREATE PROCEDURE [sel_equ_campanasXcatalogo_tempario]
	@p_id_catalogo_tempario INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_campana]
	WHERE ([id_catalogo.tempario]= @p_id_catalogo_tempario)
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_equ_campanas]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_campanas]
GO
CREATE PROCEDURE [sel_equ_campanas]
	
	@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_equ_campana]
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_equ_campanas]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_equ_campanas]
GO
CREATE PROCEDURE [lis_equ_campanas]
	AS
BEGIN
	SELECT id AS id,
[campana] AS [equ_campana]
	FROM [equ_campana]
	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_equ_campana]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_equ_campana]
GO
CREATE PROCEDURE [upd_equ_campana]
	@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_campana varchar(300) = null,
		@p_campana_codigo varchar(100) = null,
		@p_detalleCampana varchar(8000) = null,
		@p_fechaInicio_ff smalldatetime = null,
		@p_fechafin_ff smalldatetime = null,
		@p_activo bit = null,
		@p_id_tercero int = null,
		@p_id_catalogo_tempario int = null
AS
BEGIN
		UPDATE [equ_campana]
		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(),
			[campana] = isnull(@p_campana,[campana]),
			[campana_codigo] = isnull(@p_campana_codigo,[campana_codigo]),
			[detalleCampana] = isnull(@p_detalleCampana,[detalleCampana]),
			[fechaInicio_ff] = isnull(@p_fechaInicio_ff,[fechaInicio_ff]),
			[fechafin_ff] = isnull(@p_fechafin_ff,[fechafin_ff]),
			[activo] = isnull(@p_activo,[activo]),
			[id_tercero] = isnull(@p_id_tercero,[id_tercero]),
			[id_catalogo.tempario] = isnull(@p_id_catalogo_tempario,[id_catalogo.tempario])
		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_equ_campana]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_equ_campana]
GO
CREATE PROCEDURE [ins_equ_campana]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_campana varchar(300),
		@p_campana_codigo varchar(100),
		@p_detalleCampana varchar(8000),
		@p_fechaInicio_ff smalldatetime,
		@p_fechafin_ff smalldatetime,
		@p_activo bit,
		@p_id_tercero int,
		@p_id_catalogo_tempario int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [equ_campana]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[campana],
			[campana_codigo],
			[detalleCampana],
			[fechaInicio_ff],
			[fechafin_ff],
			[activo],
			[id_tercero],
			[id_catalogo.tempario])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_campana,
			@p_campana_codigo,
			@p_detalleCampana,
			@p_fechaInicio_ff,
			@p_fechafin_ff,
			@p_activo,
			@p_id_tercero,
			@p_id_catalogo_tempario)
		SET @v_id = scope_identity()
UPDATE [equ_campana] 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_equ_campana]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_equ_campana]
GO
CREATE PROCEDURE [act_equ_campana]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [equ_campana]
	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_equ_campana_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_equ_campana_m]
GO
CREATE PROCEDURE [reg_equ_campana_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],
		[campana] AS [campana],
		[campana_codigo] AS [campana_codigo],
		[detalleCampana] AS [detalleCampana],
		[fechaInicio_ff] AS [fechaInicio_ff],
		[fechafin_ff] AS [fechafin_ff],
		[activo] AS [activo],
		[id_tercero] AS [id_tercero],
		[id_catalogo.tempario] AS [id_catalogo.tempario]
	FROM [equ_campana]
	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_equ_campanasXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_campanasXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_equ_campanasXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_campana]
	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_equ_campanasXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_campanasXusuario_creo_m]
GO
CREATE PROCEDURE [sel_equ_campanasXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_campana]
	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_equ_campanasXtercero_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_campanasXtercero_m]
GO
CREATE PROCEDURE [sel_equ_campanasXtercero_m]
	@p_id_tercero INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_campana]
	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_equ_campanasXcatalogo_tempario_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_campanasXcatalogo_tempario_m]
GO
CREATE PROCEDURE [sel_equ_campanasXcatalogo_tempario_m]
	@p_id_catalogo_tempario INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_campana]
	WHERE ([id_catalogo.tempario]= @p_id_catalogo_tempario)
	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_equ_campanas_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_campanas_m]
GO
CREATE PROCEDURE [sel_equ_campanas_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_equ_campana]
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_equ_campanas_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_equ_campanas_m]
GO
CREATE PROCEDURE [lis_equ_campanas_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[campana] AS [equ_campana]
	FROM [equ_campana]
	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_equ_campana]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_equ_campana]
GO
CREATE PROCEDURE [aud_equ_campana]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [equ_campana].id as id_tabla,
	[equ_campana].id_usuario_modifico as id_usuario_modifico,
	[equ_campana].id_usuario_creo as id_usuario_creo,
	[equ_campana].fechaModificacion as fechaModificacion,
	[equ_campana].fechaCreacion as fechaCreacion,
	[equ_campana].active as active,
	[equ_campana].[campana] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [equ_campana]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[equ_campana].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[equ_campana].[id_usuario_creo]
	where ' + @p_filtro)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'equ_campana'
--------------------------------

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[a_sel_datosAplicacionTemparioXEquipo]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[a_sel_datosAplicacionTemparioXEquipo]

GO

CREATE procedure [dbo].[a_sel_datosAplicacionTemparioXEquipo]
	@p_eid as varchar(100) = '',
	@p_id_equipo int
	
as
begin
	
	select TRZ.horometro
	, TRZ.fecha_ff
	, TRZ.id_documento
	, TMP.periodoHoras
	, DT.[id_catalogo.tempario]
	from equ_trazabilidad TRZ
	    inner join doc_documento DOC on DOC.id = TRZ.id_documento
	    inner join [doc_documento_catalogo.tempario] DT on DT.id_documento=DOC.id
	    inner join [cat_catalogo.tempario] TMP on TMP.id = DT.[id_catalogo.tempario]
	    inner join doc_estadotipodocumento EST on EST.id=DOC.id_estadotipodocumento 
	where TRZ.active=1
	    and DT.active=1
	    and TRZ.id_equipo=@p_id_equipo
	    and TRZ.eid like @p_eid + '%'
	order by TRZ.fecha_ff 
	
end
GO

