-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
--exec readLogPdfRM @idImage=0,@pdfC6=2
CREATE PROCEDURE [dbo].[readLogPdfRM]
@idImage as int,	
@pdfC6 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
--	SELECT 
--		IDLog,
--		e.Testo as Evento,
--		Data_inserimento,
--		(Nome + ' ' + Cognome) as Utente
--	FROM
--		LogFilePDF l
--		INNER JOIN EventoLog e ON l.tipo = e.codice 
--
--	WHERE	((@idImage  is null) or (l.IDImage = @idImage))	
--	and ((@pdfC6=0 and l.applicativo='RM')
if @pdfC6 = 2
begin
	select 
		0 as IdLog,
		'Direct Marketing' As Evento,
		DataGenerazione as Data_inserimento,
		rete+cod_agente  as Utente
	from
		repositoryMetadati--dbo.C6ReportFisico_CAMPAGNA
	where 
		idInRepository = @idImage and repository = 2
end
else if(@pdfC6 = 5)
	begin
		select 0 as IdLog,
		'Generazione automatica Report di Proposta Validata' As Evento,
		dataGenerazione as Data_inserimento,
		rete+cod_agente as Utente
		from repositoryMetadati
		where repository = 5 and idInRepository = @idImage
	end
else --(@pdfC6 <> 2,5)
begin
SELECT 
		IDLog,
		e.Testo as Evento,
		Data_inserimento,
        rete+pb  AS Utente
	FROM (
		  Select   
			idInRepository as identificativo,
			case when(flagStoricizzato = 0) then (case when(repository = 1) then 'C6' else 'RM' end)
			else (case when(repositoryPreStorico = 1) then 'C6' else 'RM' end)
			end as applicativo
		  from RepositoryMetadati--C6ReportFisico
		  where 
		       (flagStoricizzato = 1 and repository=@pdfC6 and id = @idImage)
				OR (flagStoricizzato = 0 and repository=@pdfC6 and idInRepository = @idImage)
		 ) A
	inner join LogFilePDF l on A.identificativo = l.IDImage and a.applicativo = l.applicativo
	INNER JOIN EventoLog e ON l.tipo = e.codice 
end
/* VECCHIA GESTIONE
else
begin
SELECT 
		IDLog,
		e.Testo as Evento,
		Data_inserimento,
--		CASE 
--			WHEN CodiceFiscale LIKE '%@%' THEN Cognome + ' ' + REPLACE(Nome,'$','') + ' - ' + SUBSTRING(CodiceFiscale, CHARINDEX('@',CodiceFiscale) + 1 , LEN(CodiceFiscale) - CHARINDEX('@',CodiceFiscale) + 1)
--			ELSE (Nome + ' ' + Cognome) end
        rete+pb  AS Utente
	FROM (Select   identificativo
					from C6ReportFisico
					where @idImage is null or idpadre = @idImage or identificativo = @idImage
			union
			Select	identificativo 
					from reportFisico
					where identificativo = @idImage
					) A
	inner join LogFilePDF l on A.identificativo = l.IDImage
	INNER JOIN EventoLog e ON l.tipo = e.codice 
end
*/
END