-- Schema: C6MartPeriodico
-- Stored Procedure: PL_D2_S170RischioDiversificazione










-- [C6MartPeriodico].[PL_D2_S170RischioDiversificazione] 'W','MLAMRA61P03G337F'
CREATE   procedure [C6MartPeriodico].[PL_D2_S170RischioDiversificazione] --'S','BCCNDR69P21A462P'
 -- Add the parameters for the stored procedure here
 @Rete char(1), 
 @CodiceFiscale varchar(16)

AS
BEGIN

 
	declare @contaCC as int
	select @contaCC = count(*)
	from c6martperiodico.patrimonio_Bf
	where tipo_prodotto = 'CC' and cod_fiscale = @codiceFiscale and rete = @rete

	
	declare @contaPOS as int
	select @contaPOS = count(*)
	from c6martperiodico.patrimonio_Bf
	where cod_fiscale = @codiceFiscale and rete = @rete
    
    declare @ctvbf as decimal(12,2)  
	SELECT @ctvbf = isnull(sum(ctv),0) FROM  c6martperiodico.patrimonio_bf  			
			where cod_fiscale = @CodiceFiscale
				  and rete= @Rete 
				  and CTV > 0
				  and id_area not in ('LIGHT', 'VOID')
				  -- modifica del 5/2/2019 per escludere i record del patr non rappresentabile (commentata ed eventualmente utilizzata ad hoc)
				  --and (rend_non_rappr = 0  or (id_area = 'Liq' and rend_non_rappr = 1))
				  --fine modifica del 5/2/2019
				  
    declare @ctvterzi as decimal (12,2)  
	SELECT @ctvterzi = isnull(sum(ctv),0) FROM  c6martperiodico.patrimonio_terzi  			
			where cod_fiscale = @CodiceFiscale
				  and rete= @Rete 
				  and CTV > 0

	------------------------    MIFID2 - MAGGIO 2018

	DECLARE @DataFineTrim VARCHAR(8)  
    SET @DataFineTrim = c6martperiodico.getTrimestre(getDate(),0) 
    declare @maxComplessita as int
	
	
	 if @CodiceFiscale like '%FF@%'  
	   
		   select @maxComplessita  = maxcompl from [C6StagingPeriodico].[WSEIAN2]
			where 
			      codman = substring(@CodiceFiscale, 4,len(@CodiceFiscale)-3)		
				  and rete= @Rete 
				  and DTTRIM =  @DataFineTrim
				  -- 20180703 bisogna escludere l'eventuale compl_base = 10, che non verrebbe decodificata
				--  and compl_base <> 10; commentata per MUA
	else
			select @maxComplessita  = maxcompl from [C6StagingPeriodico].[WSEIAN2]
			where 
			      codfis= @CodiceFiscale	
				  and rete= @Rete 
				  and DTTRIM =  @DataFineTrim
				  -- 20180703 bisogna escludere l'eventuale compl_base = 10, che non verrebbe decodificata
				--  and compl_base <> 10; commentata per MUA
			

SELECT 
K.COD_FISCALE,
K.RETE,
CASE 
	when cod_Aggreg = 'COMPLESSIVO|BF' then @ctvbf
	when cod_Aggreg = 'COMPLESSIVO|TERZI' then @ctvterzi
	when cod_Aggreg = 'COMPLESSIVO' then @ctvbf + @ctvterzi
	else 0
END AS CTV_AGGREG,	
K.CREDITRISK,

CASE 
	WHEN (@contaPOS - @contacc = 0) THEN 0
	ELSE VAR_PERC_PTF 
END AS VAR,
CASE WHEN ISNULL(COPERTURA,0.00) = 0.00 AND @contaPOS - @contacc <> 0 THEN 'n.c.' ELSE NULL END as varString,
	
CASE 
	WHEN (ISNULL(copertura,100) < 100) AND @contaPOS - @contacc <> 0
		--THEN 'Grado di copertura: ' + REPLACE(CAST(CAST(COPERTURA AS DECIMAL (5,2)) AS VARCHAR),'.',',') + '%'  
		THEN REPLACE(CAST(CAST(COPERTURA AS DECIMAL (6,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 (@contaPOS - @contacc = 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' when k.rete = 'S' then 'Sanpaolo Invest' else 'IW Private Investments' 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,
isnull(b.max_var, 0) as var_profilo,

------------------------    MIFID2 - MAGGIO 2018
case @