﻿CREATE procedure [C6Mart].[sp_getDatiReport]
AS
BEGIN
select
	Contratti.codicecontratto as codiceContratto ,
	pdf.rete as rete ,
	pdf.codicefiscale as codiceFiscale ,
	clienti.nome as nomeCliente  ,
	clienti.cognome as cognomeCliente, 
	pdf.TipoReport as TipoReport,
	pdf.idReport as IdReport ,
	contratti.cod_agente as codAgente ,
	isnull(promot.nome,'') as nomeAgente ,
	isnull(promot.Cognome,'') as cognomeAgente ,
	pdf.numeropagine as numeroPagine ,
	pdf.NomeFile as NomeFile,
	contratti.tip_contratto tipoContratto,
	id_fk_zip,
	pdf.IDReport as identityReport
from [C6Mart].[GESTIONE_PDF_FTP]  PDF
	inner join [C6Mart].[ANAG_CLIENTI] Clienti
	on pdf.rete=clienti.rete and pdf.codicefiscale=clienti.COD_FISCALE 
		inner join c6mart.vContratti Contratti
		on contratti.rete=pdf.rete and contratti.cod_fiscale= pdf.codicefiscale 
			left join [C6Mart].[ANAG_Promotori] promot
			on contratti.rete=promot.rete and contratti.cod_agente= promot.id_promotore
				inner join dbo.reportDainserire_recupero rep
				on pdf.IDReport = rep.identificativo
where
	pdf.DataGenerazione = rep.Data
--order by IdentityReport
End