CREATE procedure [C6Mart].[rp_aggiornaDataValidazione_20160405]
AS 
begin
/*
delete from [CONSEVO].ConsulenzaBaseFL.[dbo].CBS_STAMPAREPORT_TEST
insert into [CONSEVO].ConsulenzaBaseFL.[dbo].CBS_STAMPAREPORT_TEST
select 
	DataGenerazione as data,
	SUBSTRING(NomeFile, 1, 11) as codiceContratto, --SUBSTRING(NomeFile, 0, 12)
	case 
		when (id_fk_zip = -1) then 0
		else 1
	end as definitivo,
	getDate() as dataAggiornamento
from 
(
		select 
		datagenerazione, nomefile,id_fk_zip, 
		row_number() over (partition by SUBSTRING(NomeFile, 1, 11) order by datagenerazione desc )  pippo from --SUBSTRING(NomeFile, 0, 12)
		c6mart.gestione_pdf_ftp
		--where SUBSTRING(NomeFile, 1, 11) in ('001771CO205') --SUBSTRING(NomeFile, 0, 12)
)l where pippo =1
*/
--delete from [CONSEVO].ConsulenzaBaseFL.[dbo].CBS_STAMPAREPORT
--insert into [CONSEVO].ConsulenzaBaseFL.[dbo].CBS_STAMPAREPORT
--select 
--	DataGenerazione as data,
--	SUBSTRING(NomeFile, 1, 11) as codiceContratto, --SUBSTRING(NomeFile, 0, 12)
--	case 
--		when (id_fk_zip = -1) then 0
--		else 1
--	end as definitivo,
--	getDate() as dataAggiornamento
--from 
--(
--	select 
--		datagenerazione, 
--		nomefile,
--		id_fk_zip, 
--		row_number() over (partition by SUBSTRING(NomeFile, 1, 11) order by datagenerazione desc )  as ordine, --SUBSTRING(NomeFile, 0, 12)
--		upgrade
--	from  (select pdf.*,case when md.TIP_riattivazione  = 2 then 1 else 0 end upgrade 
--			from  c6mart.gestione_pdf_ftp pdf 
--			left join c6mart.vcontratti  vc 
--			on SUBSTRING(pdf.NomeFile, 1, 11) = vc.codicecontratto --SUBSTRING(pdf.NomeFile, 0, 12)
--			left  join c6mart.contrattosei_metadati md
--			on md.chiaveclientepb = vc.chiaveclientepb) pdf2 	   
--) l 
--where    ( ordine = 1 and upgrade = 0 ) or (ordine = 2 and upgrade = 1)
--BEGIN TRANSACTION
--	BEGIN TRY
		DELETE FROM [CONSEVO].ConsulenzaBaseFL.[dbo].CBS_STAMPAREPORT
		INSERT INTO [CONSEVO].ConsulenzaBaseFL.[dbo].CBS_STAMPAREPORT
		SELECT 
			DataGenerazione AS data,
			SUBSTRING(NomeFile, 1, 11) AS codiceContratto, --SUBSTRING(NomeFile, 0, 12)
			CASE 
				WHEN (id_fk_zip = -1) THEN 0
				ELSE 1
			END AS definitivo,
			getDate() AS dataAggiornamento
		FROM 
		(
			SELECT 
				datagenerazione, 
				nomefile,
				id_fk_zip, 
				row_number() over (partition by SUBSTRING(NomeFile, 1, 11) order by datagenerazione desc )  as ordine, --SUBSTRING(NomeFile, 0, 12)
				upgrade,
				InvioBenvenuto,
				DataFineAvanzato
			FROM  (
					SELECT pdf.*,CASE WHEN md.TIP_riattivazione  = 2 THEN 1 ELSE 0 END upgrade ,md.inviobenvenuto AS InvioBenvenuto,data_fineavanzato as DataFineAvanzato
					FROM  c6mart.gestione_pdf_ftp pdf 
					left join c6mart.vcontratti  vc 
					ON SUBSTRING(pdf.NomeFile, 1, 11) = vc.codicecontratto  
					left  join c6mart.contrattosei_metadati md
					ON md.chiaveclientepb = vc.chiaveclientepb
					--WHERE md.inviobenvenuto = 'S' and data_fineavanzato > getdate()
					) pdf2
		) l
		WHERE    ( ordine = 1 and upgrade = 0  and InvioBenvenuto='S' and DataFineAvanzato > getdate() )
				  OR
				 ( ordine = 2 and upgrade = 1 )
--	COMMIT TRANSACTION
--	
--END TRY
--
--BEGIN CATCH
--	ROLLBACK TRANSACTION
--	DECLARE @Msg NVARCHAR(MAX)  
--	SELECT @Msg=ERROR_MESSAGE() 
--	RAISERROR('Descrizione errore: %s', 20, 101,@msg) WITH LOG
--END CATCH
end