-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE procedure [C6Mart].[DM_TEST_QUALITY_20181003]
AS
BEGIN
	SET NOCOUNT ON;
update c6mart.email_pb set email = 'fabio.consoli@hp.com' where codice = '015312'
update c6mart.anag_promotori set e_mail = 'fabio.consoli@hp.com' where id_promotore = '015312'
--***********************************************************************************************************
 --***********************************************************************************************************
--***********************************************************************************************************
--***********************************************************************************************************
 INSERT INTO C6MART.TEST_QUALITY
 SELECT DISTINCT VC.RETE,VC.COD_FISCALE,'CONTRATTOSEI_METADATI NON AGG.2' FROM [C6Mart].[vContrattiSei] VC
				LEFT JOIN c6mart.gestione_pdf_ftp  PDF
				ON SUBSTRING(PDF.NomeFile, 1, 11) = VC.CODICECONTRATTO --SUBSTRING(PDF.NomeFile, 0, 12)
			WHERE    VC.INVIOBENVENUTO = 'S' AND PDF.CODICEFISCALE IS NULL
--***********************************************************************************************************
 --***********************************************************************************************************
	INSERT INTO C6MART.TEST_QUALITY
	SELECT
		 RETE,
		 COD_FISCALE,		 
		 CASE WHEN PMOD.PRC > 100 
			THEN 'SCARTO PER PIRAMIDE MODELLO SUPERIORE A 100'
			ELSE 'SCARTO PER PIRAMIDE MODELLO INFERIORE A 100'
		 END AS MOTIVO
     --FROM C6MART.VCONTRATTISEI VC
      FROM C6MART.VCONTRATTI VC
		INNER JOIN	
			(SELECT chiaveclientepb, sum(percentualeprogetto) AS PRC FROM c6staging.rp_progettipiramidemodello 
				WHERE 1=1
				AND ((area = 'Inv' and tipoProgetto = 1) OR (area <> 'Inv' and tipoProgetto = 0))
				AND DtFineValidita > getdate() 
				group by chiaveclientepb
				having sum(percentualeprogetto) <> 0 AND sum(percentualeprogetto) <> 100 
				) AS PMOD
		 ON VC.chiaveclientepb = PMOD.chiaveclientepb	
	 WHERE	PMOD.PRC <> 0 AND PMOD.PRC <> 100  
			--AND ISNULL(VC.INVIOBENVENUTO,'N') = 'N'
INSERT INTO C6MART.TEST_QUALITY 
SELECT  DISTINCT RETE,COD_FISCALE,'DATO ERRATO C6MART.ASSET_PERC' FROM C6MART.PATRIMONIO_BF WHERE COD_PRODOTTO IN
	 (select COD_PRODOTTO  from C6MART.ASSET_PERC WHERE PERC < 0 OR PERC > 100)
END