-- Schema: wh
-- Stored Procedure: MERGED_PROCEDURE_S170






CREATE procedure [wh].[MERGED_PROCEDURE_S170]
AS
BEGIN
	SET NOCOUNT ON;

	-- Appoggio in tabelle temporanee
	select rete, cod_fiscale, count(*) as contaCC
	into #tContaCC
	from c6martperiodico.patrimonio_Bf
	where tipo_prodotto = 'CC'
	group by rete, cod_fiscale

	select rete, cod_fiscale, count(*) as contaPos
	into #tContaPos
	from c6martperiodico.patrimonio_Bf
	group by rete, cod_fiscale
    
	SELECT rete, cod_fiscale, isnull(sum(ctv),0) as CTVBF
	into #tCTVBF
	FROM  c6martperiodico.patrimonio_bf  			
	where CTV > 0
	group by rete, cod_fiscale
				  
	SELECT rete, cod_fiscale, isnull(sum(ctv),0) as CTVTerzi
	into #tCTVTerzi
	FROM  c6martperiodico.patrimonio_terzi  			
	where CTV > 0
	group by rete, cod_fiscale

	-- Calcolo Complessità Massima
	DECLARE @DataFineTrim VARCHAR(8)  
    SET @DataFineTrim = c6martperiodico.getTrimestre2(getDate(),0)

	create table #tMaxComplessita (Rete varchar(1), CodiceFiscale varchar(20), MaxComplessita int)

	insert into #tMaxComplessita (Rete, CodiceFiscale, MaxComplessita)

		select rete, 'FF@' + CODMAN as CodiceFiscale, MAXCOMPL  as maxComplessita
		from [C6StagingPeriodico].[WSEIAN2]
		where DTTRIM =  @DataFineTrim
		--	and compl_base <> 10
			and isnull(codman, '') <> ''
		group by rete, 'FF@' + CODMAN,MAXCOMPL

		union

		select rete, CodFis as CodiceFiscale, MAXCOMPL  as maxComplessita
		from [C6StagingPeriodico].[WSEIAN2]
		where DTTRIM =  @DataFineTrim
			--and compl_base <> 10
			and isnull(codman, '') = ''
		group by rete, CodFis,MAXCOMPL

		--select * from #tContaCC where rete = 'F' and cod_Fiscale = 'RSCFNC43M09A965T'
		--select * from #tContaPos where rete = 'F' and cod_Fiscale = 'RSCFNC43M09A965T'
		--select * from #tCTVBF where rete = 'F' and cod_Fiscale = 'RSCFNC43M09A965T'
		--select * from #tCTVTerzi where rete = 'F' and cod_Fiscale = 'RSCFNC43M09A965T'

	insert into wh.PL_D2_S170RischioDiversificazione
		SELECT 'C6MartPeriodico.PL_D2_S170RischioDiversificazione' as ProcedureName, K.Rete as i_Rete, K.Cod_Fiscale as i_CodiceFiscale,
			K.COD_FISCALE,
			K.RETE,
			CASE 
				when cod_Aggreg = 'COMPLESSIVO|BF' then isnull(#tCTVBF.CTVBF, 0)
				when cod_Aggreg = 'COMPLESSIVO|TERZI' then isnull(#tCTVTerzi.CTVTerzi, 0)
				when cod_Aggreg = 'COMPLESSIVO' then isnull(#tCTVBF.CTVBF, 0) + isnull(#tCTVTerzi.CTVTerzi, 0)
				else 0
			END AS CTV_AGGREG,	
			K.CREDITRISK,
			CASE 
				WHEN (isnull(#tContaPOS.contaPOS, 0) - isnull(#tContaCC.ContaCC, 0) = 0) THEN 0
				ELSE VAR_PERC_PTF 
			END AS VAR,
			CASE WHEN ISNULL(COPERTURA,0.00) = 0.00 AND isnull(#tContaPOS.contaPOS, 0) - isnull(#tContaCC.ContaCC, 0) <> 0 THEN 'n.c.' ELSE NULL END as varString,
	
			CASE 
				WHEN (ISNULL(copertura,100) < 100) AND isnull(#tContaPOS.contaPOS, 0) - isnull(#tContaCC.ContaCC, 0) <> 0
					--THEN 'Grado di copertura: ' + REPLACE(CAST(CAST(COPERTURA AS DECIMAL (5,2)) AS VARCHAR),'.',',') + '%'  
					THEN REPLACE(CAST(CAST(COPERTURA AS DECIMAL (5,2)) AS VARCHAR),'.',',') --+ '%'  
				ELSE null 
			END AS coperturaString,

			-- modifica del 19/6/2018 - gestione indicatore per gli altri ptf del Diagnosi
			beneficio as diversificazione,
			--case 
			--when cod_Aggreg = 'COMPLESSIVO' then null
			--when cod_Aggreg = 'COMPLESSIVO|BF' then beneficio
			--when cod_Aggreg = 'COMPLESSIVO|TERZI' then null
			--END AS diversificazione,

			-- fine modifica del 19/6/2018 


			CASE 
				WHEN (isnull(#tContaPOS.contaPOS, 0) - isnull(#tContaCC.ContaCC, 0) = 0) THEN 100
				ELSE COPERTURA
			END AS COPERTURA,
			case 
			when cod_Aggreg = 'COMPLESSIVO' then 'Patrimonio Complessivo'
			when cod_Aggreg = 'COMPLESSIVO|BF' then 'Patrimonio ' + (case when k.rete ='F' then 'Fideuram' else 'Sanpaolo Invest' end)
			when cod_Aggreg = 'COMPLESSIVO|TERZI' then 'Patrimonio altri Istituti'
			ELSE ''
			END AS PATRIMONIO,

			case 
				when b.profilo is null then -1 
				else b.profilo
			end as profiloCode,

