﻿CREATE         procedure [C6MartPeriodico].[PL_FD185UCaratteristicheESG]-- 'F','BBLCMS66M11F960M'
 -- Add the parameters for the stored procedure here
 @Rete char(1), 
 @CodiceFiscale varchar(16)
AS
BEGIN
-------------------------------------------------------------------------------------
--INIZIO INTERVENTI OMNIA QUIII
	DECLARE @TOTSELFLIQ AS INT
	SELECT @TOTSELFLIQ = COUNT(*)
	FROM C6MartPeriodico.PATRIMONIO_BF
	WHERE PATRIMONIO_BF.RETE = @Rete
		AND PATRIMONIO_BF.COD_FISCALE = @CodiceFiscale
		AND ISIN in('EURO00000009', 'EURO10000007' )
		AND ID_AREA = 'LIQ'
	DECLARE @TOTLIQ AS INT
	SELECT @TOTLIQ = COUNT(*)
	FROM C6MartPeriodico.PATRIMONIO_BF
	WHERE PATRIMONIO_BF.RETE = @Rete
		AND PATRIMONIO_BF.COD_FISCALE = @CodiceFiscale
		AND ID_AREA = 'LIQ'
--FINE INTERVENTI OMNIA QUIII
--------------------------------------------------------------------------------------
SELECT 
	CASE 
		when ISNULL(PATRBF.NOME_PROGETTO,'') like '%Direct EVO%' then 'Rob'--when robo.isProgettoRobo='S' then 'Rob'
		else 'ASF'
	END AS needarea,
	CASE 
		when ISNULL(PATRBF.NOME_PROGETTO,'') like '%Direct EVO%' then 'Portafoglio Direct Evo' --when robo.isProgettoRobo='S' then 'Portafoglio Direct Evo'
		else 'Portafoglio Base'
		--WHEN AREA.ID_AREA = 'Pre1' OR AREA.ID_AREA = 'Pre2' THEN 'Previdenza'
		--WHEN AREA.ID_AREA = 'Na'  THEN 'Non allocate'
		--ELSE AREA.NOME_AREA 
	END AS needareades,
CASE 
	--WHEN AREA.ID_AREA = 'Pre1' OR AREA.ID_AREA = 'Pre2' THEN  3 
	--ELSE AREA.ORDINAMENTO 
	--	WHEN robo.isProgettoRobo='S' THEN  1
	--ELSE 2
	WHEN ISNULL(PATRBF.NOME_PROGETTO,'') like '%Direct EVO%' then 1
	ELSE 2
END 
 AS areasortorder,
CASE 
	WHEN AREA.ID_AREA = 'Inv' AND PATRBF.NOME_PROGETTO IS NULL THEN 'Prodotti non associati a progetti'
	when isnull(trim(PATRBF.NOME_PROGETTO),'') not like '%Direct EVO%' then 'Default'
	ELSE ISNULL(PATRBF.NOME_PROGETTO,'')
END AS nome_progetto,
SUM(PATRBF.CTV ) OVER (PARTITION BY CASE 
		when ISNULL(PATRBF.NOME_PROGETTO,'') like '%Direct EVO%' then 'Portafoglio Direct Evo' --when robo.isProgettoRobo='S' then 'Portafoglio Direct Evo'
		else 'Portafoglio Base' end)
		AS somma_controval_need_prog,
	CASE WHEN PATRBF.TIPO_PRODOTTO <> 'CC' THEN ANAGPROD.DESCR_PRODOTTO
		 ELSE ANAGPROD.DESCR_PRODOTTO+' '+SUBSTRING(COD_CC,4,2) + '/' + SUBSTRING(COD_CC,6,LEN(ANAG_CC.COD_CC))
	END AS Descrizione,
--CASE 
--   WHEN isnull(esg.PERC_INVESTIMENTO_SFDR,0.00) = 0.00 
--      THEN '-' 
--   ELSE 
--      REPLACE(CONVERT(VARCHAR(10), esg.PERC_INVESTIMENTO_SFDR), '.', ',')-- + '%'
--END AS PERC_INVESTIMENTO_SFDR,
--CASE 
--   WHEN isnull(esg.PERC_INVESTIMENTO_SFDR,0.00) = 0.00 											  ----decommenta per ESG 2024
--      THEN '-' 																					  ----decommenta per ESG 2024
--   ELSE 																							  ----decommenta per ESG 2024
--      REPLACE(CONVERT(VARCHAR(10), esg.PERC_INVESTIMENTO_SFDR), '.', ',')-- + '%'					  ----decommenta per ESG 2024
--END AS PERC_INVESTIMENTO_SFDR_AMB,																  ----decommenta per ESG 2024
--																									  --decommenta per ESG 2024
--																									  --decommenta per ESG 2024
--CASE 																								  ----decommenta per ESG 2024
--   WHEN isnull(esg.PERC_INVESTIMENTO_SFDR,0.00) = 0.00 											  ----decommenta per ESG 2024
--      THEN '-' 																					  ----decommenta per ESG 2024
--   ELSE 																							  ----decommenta per ESG 2024
--      REPLACE(CONVERT(VARCHAR(10), esg.PERC_INVESTIMENTO_SFDR), '.', ',')-- + '%'					  ----decommenta per ESG 2024
--END AS PERC_INVESTIMENTO_SFDR_SOC,																  ----decommenta per ESG 2024
CASE 
   WHEN isnull(esg.PERC_INVESTIMENTO_SFDR,0.00) = 0.00 
      THEN '-' 
   ELSE 
      REPLACE(CONVERT(VARCHAR(10), esg.PERC_INVESTIMENTO_SFDR), '.', ',')-- + '%'
END AS PERC_INVESTIMENTO_SFDR,
CASE 
   WHEN isnull(esg.PERC_INVEST_SFDR_MIN_SUST_E,0.00) = 0.00 											  ----decommenta per ESG 2024
      THEN '-' 																					  ----decommenta per ESG 2024
   ELSE 																							  ----decommenta per ESG 2024
      REPLACE(CONVERT(VARCHAR(10), esg.PERC_INVEST_SFDR_MIN_SUST_E), '.', ',')-- + '%'					  ----decommenta per ESG 2024
END AS PERC_INVESTIMENTO_SFDR_AMB,																  ----decommenta per ESG 2024
																								  --decommenta per ESG 202																					  --decommenta per ESG 2024
CASE 																								  ----decommenta per ESG 2024
   WHEN isnull(esg.PERC_INVEST_SFDR_MIN_SUST_S,0.00) = 0.00 											  ----decommenta per ESG 2024
      THEN '-' 																					  ----decommenta per ESG 2024
   ELSE 																							  ----decommenta per ESG 2024
      REPLACE(CONVERT(VARCHAR(10), esg.PERC_INVEST_SFDR_MIN_SUST_S), '.', ',')-- + '%'					  ----decommenta per ESG 2024
END AS PERC_INVESTIMENTO_SFDR_SOC,		
	CASE 
   WHEN ISNULL(esg.PERC_INVESTIMENTO_TAXONOMY, 0.00) = 0.00 
      THEN '-' 
   ELSE 
      REPLACE(CONVERT(VARCHAR(10), esg.PERC_INVESTIMENTO_TAXONOMY), '.', ',')-- + '%'
END AS PERC_INVESTIMENTO_TAXONOMY,
	isnull(esg.label_PAI_A,'-') as label_PAI_A,
	isnull(esg.Label_PAI_S,'-') as label_PAI_S,
	isnull(esg.Label_U_INFO_ESG,'-') as label_U_INFO_ESG,
	PATRBF.CTV AS Controvalore,
	SUM(PATRBF.CTV ) OVER (PARTITION BY CASE 
		when ISNULL(PATRBF.NOME_PROGETTO,'') like '%Direct EVO%' then 'Portafoglio Direct Evo' --when robo.isProgettoRobo='S' then 'Portafoglio Direct Evo'
		else 'Portafoglio Base' end)
	 	AS somma_controval_needarea
	--SUM(PATRBF.CTV) OVER (PARTITION BY AREA.ID_AREA) AS somma_controval_needarea
FROM 
C6MartPeriodico.PATRIMONIO_BF PATRBF
INNER JOIN 
C6MartPeriodico.ANAG_PRODOTTI ANAGPROD 
	ON 
	ANAGPROD.COD_PRODOTTO = PATRBF.COD_PRODOTTO
INNER JOIN 
C6MartPeriodico.AREA_BISOGNO AREA 
	ON LEFT(PATRBF.ID_AREA,3) = AREA.ID_AREA 
left join [C6StagingPeriodico].[ESG_ANAG_METRICHE] esg
on esg.cod_prodotto = ANAGPROD.COD_PRODOTTO
LEFT OUTER JOIN C6MartPeriodico.ANAG_CC								   
ON 
	ANAG_CC.COD_FISCALE = PATRBF.COD_FISCALE
	AND ANAG_CC.RETE = PATRBF.RETE
	AND cast(ANAG_CC.cod_cc as integer ) = substring(PATRBF.position_id,72,9)
WHERE 1 = 1
	AND PATRBF.RETE = @Rete
	AND PATRBF.COD_FISCALE = @CodiceFiscale
	--V  
	AND PATRBF.CTV > 0
	--AND AREA.ORDINAMENTO in (1,2,3,4,5,8,9)  --aggiunto 10/10/2022 per non far uscire le risorse non allocate e c/c negativi
ORDER BY 
	CASE 
	--WHEN AREA.ID_AREA = 'Pre1' OR AREA.ID_AREA = 'Pre2' THEN  3 
	--ELSE AREA.ORDINAMENTO 
	--	WHEN robo.isProgettoRobo='S' THEN  1
	--ELSE 2
	WHEN ISNULL(PATRBF.NOME_PROGETTO,'') like '%Direct EVO%' then 1
	ELSE 2
END ,PATRBF.CTV DESC				 
END