-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE procedure [C6Mart].[GESTIONE_SELECT_HOST] 
	AS
BEGIN
    SET NOCOUNT ON;
    SELECT
	(C6Mart.GESTIONE_PDF_FTP.Rete + C6Mart.GESTIONE_PDF_FTP.CodiceFiscale + right('00000000000' + C6Mart.VCONTRATTI.CodiceContratto,11) + 'C' 
     + (Case TipoReport when 'D' then 'LD' when 'LR' then 'LR' else TipoReport end) 
     + (Case TipoReport when 'D' then CONVERT(VARCHAR(10), C6Mart.GESTIONE_PDF_FTP.DataInvio, 112) 
     when 'LR' then CONVERT(VARCHAR(10), C6Mart.GESTIONE_PDF_FTP.DataInvioRistampa, 112) else null end)) as riga
	FROM        C6Mart.GESTIONE_PDF_FTP
	INNER JOIN C6Mart.VCONTRATTI
	ON C6Mart.VCONTRATTI.Rete = C6Mart.GESTIONE_PDF_FTP.Rete
	and C6Mart.VCONTRATTI.COD_FISCALE = C6Mart.GESTIONE_PDF_FTP.CodiceFiscale
	INNER JOIN  C6Mart.ANAG_CLIENTI 
	ON C6Mart.ANAG_CLIENTI.Rete = C6Mart.GESTIONE_PDF_FTP.Rete
	and C6Mart.ANAG_CLIENTI.COD_FISCALE = C6Mart.GESTIONE_PDF_FTP.CodiceFiscale
    WHERE DATEDIFF(DAY,isnull(C6Mart.GESTIONE_PDF_FTP.DataInvio,C6Mart.GESTIONE_PDF_FTP.DataInvioRistampa),GETDATE()) = 0
	AND C6Mart.VCONTRATTI.Data_FineAvanzato > getDate()
	AND C6Mart.VCONTRATTI.Cod_Fiscale +  C6Mart.VCONTRATTI.Rete NOT IN (SELECT COD_FISCALE + RETE FROM C6MART.TEST_QUALITY)
    AND C6Mart.VCONTRATTI.COD_AGENTE <> '015312'
END