-- Schema: C6MartPeriodico
-- Stored Procedure: GC_CONVALIDAREPORTSELECT


-- =============================================
-- Author:		Berni Francesco
-- Create date: 24 Aprile 2009
-- Description: 
-- =============================================
-- [C6MartPeriodico].[GC_CONVALIDAREPORTSELECT] 
CREATE procedure [C6MartPeriodico].[GC_CONVALIDAREPORTSELECT]
 
AS
BEGIN
Declare @DiagnosiVerificati as int
Declare @DiagnosiNonVerificati as int
Declare @DiagnosiOK as int
Declare @DiagnosiKO as int
Declare @MonitoraggioVerificati as int
Declare @MonitoraggioNonVerificati as int
Declare @MonitoraggioOK as int
Declare @MonitoraggioKO as int
-- Totali
Declare @TotaleDiagnosi as int
Declare @TotaleMonitoraggio as int

Declare @TotaleVerificati as int
Declare @TotaleNonVerificati as int
Declare @TotaleOK as int
Declare @TotaleKO as int


-- Diagnosi
			
			
-- Diagnosi Verificati
SELECT @DiagnosiVerificati = count(distinct B.IDREPORT) 
	FROM
		C6Mart.CONTRATTOSEI  A INNER JOIN
        C6MartPeriodico.GESTIONE_PDF_FTP B ON 
		A.Rete = B.Rete AND 
        A.Cod_Fiscale = B.CodiceFiscale 
	 
	Left outer JOIN C6MartPeriodico.VERIFICHECAMPIONE C
	ON B.IDREPORT  = C.IDREPORT
	WHERE  
	B.[TipoReport] = 'D' 
	AND ISNULL(B.FlagInvio,'N') = 'N'
	AND ISNULL(B.DEFINITIVO, 0) = 0
	AND C.datafine is  null 
	AND ISNULL(C.Verifica, 0) = 1


-- Diagnosi Non Verificati
SELECT @DiagnosiNonVerificati = count(distinct B.IDREPORT) 
	FROM
		C6Mart.CONTRATTOSEI  A INNER JOIN
        C6MartPeriodico.GESTIONE_PDF_FTP B ON 
		A.Rete = B.Rete AND 
        A.Cod_Fiscale = B.CodiceFiscale 
	Left outer JOIN C6MartPeriodico.VERIFICHECAMPIONE C
	ON B.IDREPORT  = C.IDREPORT
	WHERE  
	B.[TipoReport] = 'D' 
	AND ISNULL(B.FlagInvio,'N') = 'N'
	AND ISNULL(B.DEFINITIVO, 0) = 0
	AND C.datafine is  null 
	AND ISNULL(C.Verifica, 0) = 0

-- Diagnosi OK
SELECT @DiagnosiOK = count(distinct B.IDREPORT)
	FROM
		C6Mart.CONTRATTOSEI  A INNER JOIN
        C6MartPeriodico.GESTIONE_PDF_FTP B ON 
		A.Rete = B.Rete AND 
        A.Cod_Fiscale = B.CodiceFiscale 
	Left outer JOIN C6MartPeriodico.VERIFICHECAMPIONE C
	ON B.IDREPORT  = C.IDREPORT
	WHERE  
	B.[TipoReport] = 'D' 
	AND ISNULL(B.FlagInvio,'N') = 'N'
	AND ISNULL(B.DEFINITIVO, 0) = 0
	AND C.datafine is  null 
	and ISNULL(C.Esito,'-') = 'OK' 


-- Diagnosi KO
SELECT @DiagnosiKO = count(distinct B.IDREPORT) 
	FROM
		C6Mart.CONTRATTOSEI  A INNER JOIN
        C6MartPeriodico.GESTIONE_PDF_FTP B ON 
		A.Rete = B.Rete AND 
        A.Cod_Fiscale = B.CodiceFiscale 
	Left outer JOIN C6MartPeriodico.VERIFICHECAMPIONE C
	ON B.IDREPORT  = C.IDREPORT
	WHERE  
	B.[TipoReport] = 'D' 
	AND ISNULL(B.FlagInvio,'N') = 'N'
	AND ISNULL(B.DEFINITIVO, 0) = 0
	AND C.datafine is  null 
	and ISNULL(C.Esito,'-') = 'KO' 

-- Monitoraggio


-- Monitoraggio Verificati
SELECT @MonitoraggioVerificati = count(distinct B.IDREPORT) 
		FROM
		C6Mart.CONTRATTOSEI  A INNER JOIN
        C6MartPeriodico.GESTIONE_PDF_FTP B ON 
		A.Rete = B.Rete AND 
        A.Cod_Fiscale = B.CodiceFiscale 
	 
	Left outer JOIN C6MartPeriodico.VERIFICHECAMPIONE C
	ON B.IDREPORT  = C.IDREPORT
	WHERE  
	B.[TipoReport] = 'M' 
	AND ISNULL(B.FlagInvio,'N') = 'N'
	AND ISNULL(B.DEFINITIVO, 0) = 0
	AND C.datafine is  null 
	AND ISNULL(C.Verifica, 0) = 1


-- Monitoraggio Non Verificati
SELECT @MonitoraggioNonVerificati = count(distinct B.IDREPORT) 
	FROM
		C6Mart.CONTRATTOSEI  A INNER JOIN
        C6MartPeriodico.GESTIONE_PDF_FTP B ON 
		A.Rete = B.Rete AND 
        A.Cod_Fiscale = B.CodiceFiscale 
	 
	Left outer JOIN C6MartPeriodico.VERIFICHECAMPIONE C
	ON B.IDREPORT  = C.IDREPORT
	WHERE  
	B.[TipoReport] = 'M' 
	AND ISNULL(B.FlagInvio,'N') = 'N'
	AND ISNULL(B.DEFINITIVO, 0) = 0
	AND C.datafine is  null 
	AND ISNULL(C.Verifica, 0) = 0

-- Monitoraggio OK
SELECT @MonitoraggioOK = count(distinct B.IDREPORT) 
	FROM
		C6Mart.CONTRATTOSEI  A INNER JOIN
        C6MartPeriodico.GESTIONE_PDF_FTP B ON 
		A.Rete = B.Rete AND 
        A.Cod_Fisc
