--exec C6MartPeriodico.SP_PreTotaliReportGenerazioneZip 960, 1
CREATE procedure [C6MartPeriodico].[SP_PreTotaliReportGenerazioneZip]
	@reportPerZip int,
	@isLotto1 int = 0
AS
	set nocount on
			create table #result (
				totReportGenerati int,
				isUltimoZip int
			)
			declare @ce int=0
			declare @totReportGenerati int
			declare @isUltimoZip int
			declare @KO int
			set @KO = 0
			declare @err varchar(500)
			set @err = ''
			--exec [C6MartPeriodico].[SP_TotaliReportGenerazioneZip] 960, 1
			insert into #result exec [C6MartPeriodico].[SP_TotaliReportGenerazioneZip]  @reportPerZip, @isLotto1
			select @totReportGenerati = totReportGenerati, @isUltimoZip = isUltimoZip from #result
			if @isLotto1 = 1
				begin
					if @isUltimoZip = 2
						begin
							if (select count(*) from  [C6MartPeriodico].WHITELIST_ZIP WHERE Pacchetto_Zip is null) = 0
							begin
								set @KO = 1
								set @err = 'Nel primo lotto non c''è da fare il file Approval, ma la procedura ''SP_TotaliReportGenerazioneZip'' ha restituito lo stato 2'
							end
						end
					if @isUltimoZip = 1
						begin
							if (select count(*) from  [C6MartPeriodico].WHITELIST_ZIP WHERE Pacchetto_Zip is null) > 0
							begin
								set @KO = 1
								set @err = 'Nel primo lotto c''è da fare il file Approval, ma la procedura ''SP_TotaliReportGenerazioneZip'' ha restituito lo stato 1'
							end
						end
					if @isUltimoZip not in (1, 2)
						begin
							set @KO = 1
							set @err = 'Nel primo lotto, la procedura ''SP_TotaliReportGenerazioneZip'' ha restituito lo stato inconsistente ' + convert(varchar(10), @isUltimoZip)
						end
				end
			else
				begin
					if @isUltimoZip <> 1
						begin
							set @KO = 1
							set @err = 'Per il lotto successivo, la procedura ''SP_TotaliReportGenerazioneZip'' ha restituito lo stato inconsistente ' + convert(varchar(10), @isUltimoZip)
						end
				end
			print '@isLotto1 = ' + convert(varchar(10), @isLotto1)
			print '@isUltimoZip = ' + convert(varchar(10), @isUltimoZip)
			print '@KO = ' + convert(varchar(10), @KO)
			print '@err = ''' +  @err + ''''
			if @KO = 0
				begin
					if @isUltimoZip in (1, 2)
						begin
							declare @num_stato_report_1 int
							declare @num_elab_4 int
							select @num_stato_report_1 = COUNT(*) 
							from C6MartPeriodico.controllo_trimestrale
							where stato_report in (1)  --1 
							select @num_elab_4 = COUNT(*) 
							from C6MartPeriodico.CRUSCOTTO_CONTRATTISEI 
							where elab in(4)  
							if @num_stato_report_1 <> @num_elab_4
								begin
									set @KO = 1
									set @err = 'Controllare gli stati: Controllo_Trimestrale ha ' + convert(varchar(100), @num_stato_report_1) + ' report con stato_report = 1, Cruscotto_ContrattiSEI ha ' + convert(varchar(100), @num_elab_4) + ' con elab = 4'
								end
						end
				end
				if @KO = 0
					begin
						print 'Controllo su tabella C6MartPeriodico.Gestione_Zip_FTP'
						declare @maxIdElaborazione int
						select @maxIdElaborazione = max(FkIdElaborazioneZip) from [C6MartPeriodico].[gestione_zip_ftp]
						if( select count(*)
							from (
									SELECT TOP (1) [id]
										,[nomefile]
										,[dataInvio]
										,[flagInvio]
										,[nomeCartella]
										,[ID_ELAB]
										,[DateCopyToSend]
										,[FKIDElaborazioneZIP]
									FROM [C6MartPeriodico].[gestione_zip_ftp]
									where
										FkIdElaborazioneZip = @maxIdElaborazione
										and
										DateCopyToSend is null
									order by 1 desc, 3 desc) a
									) > 0
						begin
							set @KO = 1
							set @err = 'Situazione incongruente sulla tabella ''[C6MartPeriodico].[gestione_zip_ftp]'''
						end
					end
				if @KO = 0
					begin
						print 'Controllo su tabella C6MartPeriodico.Elaborazioni_ZIP'
						--modifica del 17/10/2018 per gestire il caso in cui il file di approval è stato inviato ma l'intero lotto ancora no e quindi c'è
						--  una riga con datafine= null
						if not (@isLotto1 = 1 and  @isUltimoZip = 1)
							begin
								if( select count(*)
									from (
										SELECT TOP (1) [IDElaborazioneZIP]
										,[DataInizio]
										,[DataFine]
										FROM [C6MartPeriodico].[elaborazioni_zip]
										where DataFine is null
										order by 1 desc, 2 desc) a
									) > 0
									begin
										set @KO = 1
										set @err = 'Situazione incongruente sulla tabella ''[C6MartPeriodico].[elaborazioni_zip]'''
									end
							end
					end
				if @KO = 0
					begin
						print 'COPIA TABELLE'
						begin try
						set @ce=10
							declare @query nvarchar(max)
							declare @today datetime
							declare @sToday varchar(10)
							declare @nometabella nvarchar(max)
							declare @sql nvarchar(max)
							declare @res table (tablename varchar(100))
							--SET FMTONLY ON
							set @today = getdate()
							set @ce=20
							set @sToday = convert(varchar(4), datepart(yyyy, @today)) + right('0' + convert(varchar(2), datepart(mm, @today)), 2) + right('0' + convert(varchar(2), datepart(dd, @today)), 2)
							set @ce=30
							set @nometabella='C6MartPeriodico.controllo_trimestrale_' + @sToday + '_preZip';
							set @sql='DROP TABLE '+QUOTENAME(@nometabella)+'';
							--if EXISTS (select * from sys.objects where object_id= OBJECT_ID(@nometabella) and type in (N'U'))
							--	exec sp_executesql @sql;
							set @query = 'select * into C6MartPeriodico.controllo_trimestrale_' + @sToday + '_preZip from C6MartPeriodico.controllo_trimestrale'
							print @query
							insert @res
							exec sys.sp_executesql @query
							set @nometabella='C6MartPeriodico.cruscotto_contrattisei_' + @sToday + '_preZip';
							set @sql='DROP TABLE '+QUOTENAME(@nometabella)+'';
							--if EXISTS (select * from sys.objects where object_id= OBJECT_ID(@nometabella) and type in (N'U'))
							--	exec sp_executesql @sql;
							set @ce=40
							set @query = 'select * into C6MartPeriodico.cruscotto_contrattisei_' + @sToday + '_preZip from C6MartPeriodico.cruscotto_contrattisei'
							print @query
							insert @res
							EXEC sys.sp_executesql @query
							set @nometabella='C6MartPeriodico.elaborazioni_zip_' + @sToday + '_preZip';
							set @sql='DROP TABLE '+QUOTENAME(@nometabella)+'';
							--if EXISTS (select * from sys.objects where object_id= OBJECT_ID(@nometabella) and type in (N'U'))
							--	exec sp_executesql @sql;
							set @ce=50
							set @query = 'select * into C6MartPeriodico.elaborazioni_zip_' + @sToday + '_preZip from C6MartPeriodico.elaborazioni_zip'
							print @query
							insert @res
							EXEC sys.sp_executesql @query
							set @nometabella='C6MartPeriodico.gestione_zip_ftp_' + @sToday + '_preZip';
							set @sql='DROP TABLE '+QUOTENAME(@nometabella)+'';
							--if EXISTS (select * from sys.objects where object_id= OBJECT_ID(@nometabella) and type in (N'U'))
							--	exec sp_executesql @sql;
							set @ce=60
							set @query = 'select * into C6MartPeriodico.gestione_zip_ftp_' + @sToday + '_preZip from C6MartPeriodico.gestione_zip_ftp'
							print @query
							insert @res
							EXEC sys.sp_executesql @query
							set @nometabella='C6MartPeriodico.gestione_pdf_ftp_' + @sToday + '_preZip';
							set @sql='DROP TABLE '+QUOTENAME(@nometabella)+'';
							--if EXISTS (select * from sys.objects where object_id= OBJECT_ID(@nometabella) and type in (N'U'))
							--	exec sp_executesql @sql;
							set @ce=70
							set @query = 'select * into C6MartPeriodico.gestione_pdf_ftp_' + @sToday + '_preZip from C6MartPeriodico.gestione_pdf_ftp'
							print @query
							insert @res
							EXEC sys.sp_executesql @query
							--SET FMTONLY OFF
							--exec C6MartPeriodico.CreaTabelleCopia
						end try
						begin catch
							set @KO = 1
							set @err = ERROR_MESSAGE()
							select 0 as Esito, @err as Err, ERROR_LINE() as rigaerr,@ce as riga
							--SET FMTONLY OFF
							return
						end catch
					end
				select
					case
						when @KO = 0 then 1
						when @KO = 1 then 0
						else 0
					end as Esito,
					@err as Err
			drop table #result