﻿CREATE    procedure [C6Mart].[rp_aggiornaDataValidazione]
AS 
begin
/*
delete from [CONSUNI].ConsulenzaUnicaFL.[dbo].CBS_STAMPAREPORT_TEST
insert into [CONSUNI].ConsulenzaUnicaFL.[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 [CONSUNI].ConsulenzaUnicaFL.[dbo].CBS_STAMPAREPORT
--insert into [CONSUNI].ConsulenzaUnicaFL.[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  [CONSUNI].ConsulenzaUnicaFL.[dbo].CBS_STAMPAREPORT
		INSERT INTO [CONSUNI].ConsulenzaUnicaFL.[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()
					------where datagenerazione<='2016-08-19 12:44:37.497'  --en 20160922 temporaneo
					) pdf2
		) l
		WHERE    ( ordine = 1 and upgrade = 0  and InvioBenvenuto='S' and DataFineAvanzato > getdate() )
				  OR
				 ( ordine = 2 and upgrade = 1 )
-- EN20160405-start: Soluzione per Ticket T-1396128-M1Y0 
				  OR
				 ( ordine = 1 and upgrade = 1 and SUBSTRING(NomeFile, 1, 11)='013614CO205' )
				 --forzatura per CRVRRT74P02L675V
				 --union all
				 --select '2013-10-18 13:54:50.883' as data,'002249CB000' as codiceContratto,'1' as definitivo,getDate() AS dataAggiornamento
				 --fine forzatura per CRVRRT74P02L675V
-- EN20160405-end:
--	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