IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[_actividadesReporteUnificado]') AND type in (N'P', N'PC'))
BEGIN
execute('
	create PROCEDURE [dbo].[_actividadesReporteUnificado]
		-- Add the parameters for the stored procedure here
		@p_id_OT as int,
		@p_eid as varchar(500),
		@p_id_usuario as int
	AS
	BEGIN
		-- SET NOCOUNT ON added to prevent extra result sets from
		-- interfering with SELECT statements.
		SET NOCOUNT ON;

		-- Insert statements for procedure here
		declare @comillaDoble char
		set @comillaDoble = ''"''

		declare @comillaSimple char
		set @comillaSimple = ''''''''

		-- @tablaCatalogo es la tabla que se retorna del SP
		declare @tablaCatalogo table (idCatalogo int, textoDescripcion varchar(500), codigoInventario varchar(45));
		
		--Si hay actividades
		insert into @tablaCatalogo
			select id as idCatalogo, replace(replace([catalogo.actividad],@comillaDoble,''''),@comillaSimple,'''') as textoDescripcion, cat_catalogo_actividad_codigoInventario as codigoInventario
				from view_cat_catalogo_actividad
				where active = 1 and 
					  id in (select id_catalogo from doc_itemDocumento where id_documento = @p_id_OT and (totalPlaneado > 0 or cantidadPlaneado > 0) and active = 1)

		declare @cantRegistros int 
		select @cantRegistros = count(*) from @tablaCatalogo
 
		if @cantRegistros = 0 --Si no hay activiadades planeadas en la OT, entonces revisa si por familia hay actividades
		begin 
			insert into @tablaCatalogo
				select id as idCatalogo, replace(replace([catalogo.actividad],@comillaDoble,''''),@comillaSimple,'''') as textoDescripcion, cat_catalogo_actividad_codigoInventario as codigoInventario
					 from view_cat_catalogo_actividad 
					 where active = 1 and 
						   cat_catalogo_actividad_id_familia = (select id_familia from cat_catalogo 
																	where active = 1 and
																		  id = (select [id_catalogo.equipo] 
																					from equ_equipo 
																					where id = (select id_equipo from [doc_documento.ot] where id = @p_id_OT)))
			select @cantRegistros = count(*) from @tablaCatalogo

			if @cantRegistros = 0 -- Si no hay actividades por familia, entonces se revisa si hay actividades por tempario.
			begin
				insert into @tablaCatalogo
					select id as idCatalogo, replace(replace([catalogo.actividad],@comillaDoble,''''),@comillaSimple,'''') as textoDescripcion, cat_catalogo_actividad_codigoInventario as codigoInventario
						from view_cat_catalogo_actividad
						where active = 1 and 
							  id in (select id_catalogo from cat_detalleTempario where active = 1 and 
										[id_catalogo.tempario] in (select [id_catalogo.tempario] from [dbo].[cat_catalogo.equipo_catalogo.tempario] where active = 1 and
												[id_catalogo.equipo] = (select [id_catalogo.equipo] from equ_equipo where 
																			id = (select id_equipo from [doc_documento.ot] where id = @p_id_OT))))
			end
		end

		-- @tablaCatalogo es la tabla que se retorna del SP
		select * from @tablaCatalogo

	END
')

END



