-- [C6MartPeriodico].[PL_D2_S170RischioDiversificazione] 'W','MLAMRA61P03G337F'
CREATE procedure [C6MartPeriodico].[PL_D2_FD170RischioDiversificazione]
 -- 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.getTrimestre2(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 @maxComplessita
			   when 0 then 'Non complesso'
	when 1 then 'Bassa'
	when 2 then 'Medio bassa'
	when 3 then 'Medio alta'
	when 4 then 'Alta'
	else 'n.c.'
		end
		as complessita,
-------------------------  FINE MIFID2 - MAGGIO 2018
--V l'ordine serve per la corretta visualizzazione del report: Patrimonio casa, patrimonio terzi, patrimonio complessivo
--V Monitoraggio prende soltanto la prima riga, diagnosi anche nel caso non ci siano i terzi.
case 
when cod_Aggreg = 'COMPLESSIVO' then 3
when cod_Aggreg = 'COMPLESSIVO|BF' then 1
when cod_Aggreg = 'COMPLESSIVO|TERZI' then 2
ELSE ''
END AS Ordine
FROM
C6MARTperiodico.RISCHIO_AGGREGATO K left outer JOIN c6martperiodico.mifid a
	ON k.cod_fiscale = a.cod_fiscale and
		k.rete = a.rete
	left outer join
C6Martperiodico.CODIFICA_RISCHIO b on
A.PROFILO_ASS=B.PROFILO
WHERE
--A.PROFILO_ASS=B.PROFILO
--AND K.RETE=A.RETE
--AND K.COD_FISCALE= A.COD_FISCALE
COD_AGGREG IN ('COMPLESSIVO|BF', 'COMPLESSIVO', 'COMPLESSIVO|TERZI')
AND k.COD_FISCALE = @COdiceFISCALE
AND k.RETE= @RETE
ORDER BY ordine
END 
--USE [C6StampeCentralizzate]
--GO
--/****** Object:  StoredProcedure [C6MartPeriodico].[PL_D2_S170RischioDiversificazione]    Script Date: 09/02/2022 11:37:02 ******/
--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO
---- [C6MartPeriodico].[PL_D2_S170RischioDiversificazione] 's','BRNGDI72S69L219Y'
--ALTER procedure [C6MartPeriodico].[PL_D2_S170RischioDiversificazione]
-- -- 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.getTrimestre2(getDate(),0) 
--    declare @maxComplessita as int
--	if @CodiceFiscale like '%FF@%'  
--			select @maxComplessita = max(compl_base) from [C6StagingPeriodico].[WSEIAD2]
--			where 
--			  --   codfis like 
--					--   CASE when @CodiceFiscale like '%FF@%'THEN '%%'
--					--   ELSE @CodiceFiscale
--					--   END
--				 --and codman = 
--					--   CASE when @CodiceFiscale like '%FF@%' THEN substring(@CodiceFiscale, 4,len(@CodiceFiscale)-3)
--					--   ELSE ''
--					--   END
--				 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;
--	 else 
--			select @maxComplessita = max(compl_base) from [C6StagingPeriodico].[WSEIAD2]
--			where 
--			  --   codfis like 
--					--   CASE when @CodiceFiscale like '%FF@%'THEN '%%'
--					--   ELSE @CodiceFiscale
--					--   END
--				 --and codman = 
--					--   CASE when @CodiceFiscale like '%FF@%' THEN substring(@CodiceFiscale, 4,len(@CodiceFiscale)-3)
--					--   ELSE ''
--					--   END
--				 codfis= @CodiceFiscale
--				 and rete= @Rete 
--				 and DTTRIM =  @DataFineTrim
--				  -- 20180703 bisogna escludere l'eventuale compl_base = 10, che non verrebbe decodificata
--				 and compl_base <> 10;
--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 (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 (@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' 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,
--isnull(b.max_var, 0) as var_profilo,
--------------------------    MIFID2 - MAGGIO 2018
--case @maxComplessita
--        	when 0 then '-'
--			when 1 then 'Minima'
--			when 2 then 'Bassa'
--			when 3 then 'Media'
--			when 4 then 'Alta'
--			when 5 then 'Molto alta'
--			else '-'
--		end
--		as complessita,
---------------------------  FINE MIFID2 - MAGGIO 2018
----V l'ordine serve per la corretta visualizzazione del report: Patrimonio casa, patrimonio terzi, patrimonio complessivo
----V Monitoraggio prende soltanto la prima riga, diagnosi anche nel caso non ci siano i terzi.
--case 
--when cod_Aggreg = 'COMPLESSIVO' then 3
--when cod_Aggreg = 'COMPLESSIVO|BF' then 1
--when cod_Aggreg = 'COMPLESSIVO|TERZI' then 2
--ELSE ''
--END AS Ordine
--FROM
--C6MARTperiodico.RISCHIO_AGGREGATO K left outer JOIN c6martperiodico.mifid a
--	ON k.cod_fiscale = a.cod_fiscale and
--		k.rete = a.rete
--	left outer join
--C6Martperiodico.CODIFICA_RISCHIO b on
--A.PROFILO_ASS=B.PROFILO
--WHERE
----A.PROFILO_ASS=B.PROFILO
----AND K.RETE=A.RETE
----AND K.COD_FISCALE= A.COD_FISCALE
--COD_AGGREG IN ('COMPLESSIVO|BF', 'COMPLESSIVO', 'COMPLESSIVO|TERZI')
--AND k.COD_FISCALE = @COdiceFISCALE
--AND k.RETE= @RETE
--ORDER BY ordine
--END