﻿--[C6MartPeriodico].[SP_TotaliReportGenerazioneZip]  960,1
CREATE PROCEDURE [C6MartPeriodico].[SP_TotaliReportGenerazioneZip] 
@reportPerZip int, @isLotto1 int = 0
AS
BEGIN
IF @isLotto1 = 1 and 0 < (select count(*) from  [C6MartPeriodico].WHITELIST_ZIP WHERE Pacchetto_Zip is null)
		BEGIN
		select  count(*) totReportGenerati,
				2 as   isUltimoZip 
		from  [C6MartPeriodico].WHITELIST_ZIP WHERE Pacchetto_Zip is null
		END
ELSE
		BEGIN
		Declare @totReportDaGenerareCT int
		Declare @totReportGenerati int 
		declare @totReportDaNonGenerareCCS int
		 if @reportPerZip = 0 
			set @reportPerZip = 960
		select @totReportGenerati =COUNT(*) 
		from C6MartPeriodico.controllo_trimestrale
		where stato_report in (4)  --1 
		select @totReportDaGenerareCT =COUNT(*) 
		from C6MartPeriodico.controllo_trimestrale
		where stato_report in (1)  --1 
		select  @totReportDaNonGenerareCCS=COUNT(*) 
		from C6MartPeriodico.CRUSCOTTO_CONTRATTISEI 
		where elab in(4)  
		--select @totReportGenerati,@totReportDaGenerareCT,@totReportDaNonGenerareCCS
		select  @totReportGenerati totReportGenerati,
				case when(@totReportDaGenerareCT - @totReportDaNonGenerareCCS) = 0 then 
						   1 
					  else
						   0
				End	   as   isUltimoZip 
				END
End