﻿CREATE PROCEDURE [C6MartPeriodico].[DM_TEST_QUALITY]
AS
BEGIN
declare @date datetime
declare @trimestre datetime
set @date = getdate()
set @trimestre = [C6MartPeriodico].[getTrimestre] (@date, 0)
SELECT 'TESTING FOR TRIMESTRALE = ' + cast( @trimestre as NVARCHAR(20))
--set @trimestre = '2017-06-30'
SELECT 'TESTING DATA_MONIT'
;with dd as
(
SELECT
	CONO.ID_AREA AS NEED_AREA,
	CASE
		WHEN CONO.NOME_PROGETTO = 'XXX' THEN ''
		ELSE CONO.NOME_PROGETTO
	END AS NOME_PROGETTO,
	CONO.RETE,
	CONO.COD_FISCALE,
	CONO.DATA_CONO,
	MONIT.DATA_CTV as DATA_MONIT
FROM
	C6MartPeriodico.CONO_PIANIFICAZIONE AS CONO
	INNER JOIN C6MartPeriodico.MONITORAGGIO_ATTUALE AS MONIT
		ON CONO.RETE = MONIT.RETE
	AND CONO.COD_FISCALE = MONIT.COD_FISCALE
	AND CONO.ID_AREA = MONIT.ID_AREA
	AND CONO.NOME_PROGETTO = MONIT.NOME_PROGETTO
	AND cono.ordinamento_progetto=monit.ordinamento_progetto 
),reduced_data as
(
select distinct d.* 
from dd d 
	inner join (select * from dd where data_cono = @trimestre) x on d.need_area = x.need_area and d.nome_progetto = x.nome_progetto 
	and x.rete = d.rete and x.cod_fiscale = d.cod_fiscale 
),
correct_data as
(
select * from reduced_data d where data_cono = data_monit
)
select  d.RETE, d.COD_FISCALE, 'missing data for DATA_MONIT for NOME_PROGETTO = ' + d.nome_progetto + ' NEED_AREA = '+ d.need_area as MOTIVO
from reduced_data d 
 left join correct_data x on d.need_area = x.need_area and d.nome_progetto = x.nome_progetto 
	and x.rete = d.rete and x.cod_fiscale = d.cod_fiscale 
where x.need_area is null
group by d.rete, d.cod_fiscale, d.need_area, d.nome_progetto
SELECT 'TESTING data_perf in c6mart.vcontratti'
DECLARE @dtrim VARCHAR(8)
		set @dtrim = c6martperiodico.getPreviousTrimestre(getDate(),0) 
		SELECT codfis, rete, flagnqp, flgprlrde, FLAGPG, FLGPROF, dttrim,rank INTO #WSEIAN_1
		FROM
		(
		select codfis, rete, flagnqp, flgprlrde, FLAGPG, FLGPROF, dttrim, 1  as rank  
		from [C6StagingPeriodico].[WSEIAN2]
		WHERE codman = '' and dttrim = @dtrim
		UNION ALL 
		select 'FF@'+ codman as codfis, rete, flagnqp, flgprlrde, FLAGPG, FLGPROF, dttrim, 1 as rank  
		from [C6StagingPeriodico].[WSEIAN2]
		WHERE codman <> '' and dttrim = @dtrim
		)X
select * from wh.Maledetti m 	 inner join #WSEIAN_1 wse 
			 on wse.codfis = m.i_codiceFiscale and wse.rete = m.i_rete	left join   [C6MartPeriodico].[RP_vContrattiPerGenerazioneReport]  VC
on vc.cod_fiscale = i_codiceFiscale and vc.rete = i_rete
where data_perf is null and wse.FLGPROF = 'S'
SELECT 'TESTING SCARTO PER PIRAMIDE MODELLO'
SELECT RETE, COD_FISCALE,		 
		 CASE 
			WHEN PMOD.PRC > 100 THEN 'SCARTO PER PIRAMIDE MODELLO SUPERIORE A 100'
			WHEN PMOD.PRC < 1  THEN 'SCARTO PER PIRAMIDE MODELLO BELOW 1'
			ELSE 'SCARTO PER PIRAMIDE MODELLO INFERIORE A 100'
END AS MOTIVO
FROM C6MARTPeriodico.VCONTRATTI VC
	INNER JOIN	
		(SELECT chiaveclientepb, sum(percentualeprogetto) AS PRC FROM c6stagingperiodico.rp_progettipiramidemodello 
		WHERE 1=1
			AND ((area = 'Inv' and tipoProgetto = 1) OR (area <> 'Inv' and tipoProgetto = 0))
			AND DtFineValidita > getdate() 
		GROUP BY chiaveclientepb
		) AS PMOD
	ON VC.chiaveclientepb = PMOD.chiaveclientepb	
WHERE	PMOD.PRC < 1 or  PMOD.PRC > 100
DECLARE @d nvarchar(8)
set @d = c6martperiodico.getTrimestre(getdate(), 1)
;with alldata as 
(
select * from [C6StagingPeriodico].[WSEIAN2] WHERE codfis not like 'FF%' and  DTTRIM=@d
), moddata as 
(	SELECT an.*
	FROM [C6StagingPeriodico].[WSEIAN2] an
	INNER JOIN [C6StagingPeriodico].[CASISTICHE_ADEGUATEZZA_PG_QPROF] adeg
	ON [CLCOMPL]=[COMPLESSITA] and [FLGCONC]=[CONCENTRAZIONE]
	and an.[FLAGPG]=adeg.[FLAGPG] and an.[FLAGNQP]=adeg.[FLAGNQP] and isNULL(an.[FLGPRLRDE],'')=isNULL(adeg.[FLGPRLRDE],'') 
	WHERE codfis not like 'FF%' and  DTTRIM=@d
)
select * from alldata d left join moddata m on m.rete = d.rete and m.codfis = d.codfis and m.dttrim = d.dttrim
where m.codfis is null
;with alldata as 
(
select * from [C6StagingPeriodico].[WSEIAN2] WHERE codfis  like 'FF%' and  DTTRIM=@d
), moddata as 
(	SELECT an.* 
	FROM [C6StagingPeriodico].[WSEIAN2] an
	INNER JOIN [C6StagingPeriodico].[CASISTICHE_ADEGUATEZZA_PG_QPROF] adeg
	ON [CLCOMPL]=[COMPLESSITA] and [FLGCONC]=[CONCENTRAZIONE] 
	and adeg.[FLAGPG]=0 and adeg.[FLAGNQP]='N' and isNULL(an.[FLGPRLRDE],'')=isNULL(adeg.[FLGPRLRDE],'') 
	WHERE   codfis  like 'FF%'
	and DTTRIM=@d
)
select * from alldata d left join moddata m on m.rete = d.rete and m.codfis = d.codfis and m.dttrim = d.dttrim
where m.codfis is null
END