-- Schema: C6Mart
-- Stored Procedure: PL_S185UCaratteristicheESG











																	  
CREATE procedure [C6Mart].[PL_S185UCaratteristicheESG] --'F','DCRCRL47S11D612N'
 -- 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 C6MART.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 C6MART.PATRIMONIO_BF
	WHERE PATRIMONIO_BF.RETE = @Rete
		AND PATRIMONIO_BF.COD_FISCALE = @CodiceFiscale
		AND ID_AREA = 'LIQ'
--FINE INTERVENTI OMNIA QUIII
--------------------------------------------------------------------------------------

SELECT 
    patrbf.cod_prodotto as COD_PRODOTTO,
	CASE 
	WHEN AREA.ID_AREA = 'Pre1' OR AREA.ID_AREA = 'Pre2' THEN 'Pre' 
	ELSE AREA.ID_AREA 
END AS needarea,   					   
CASE 
		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 
	END AS areasortorder,
	CASE 
		WHEN AREA.ID_AREA = 'Inv' AND PATRBF.NOME_PROGETTO IS NULL THEN 'Prodotti non associati a progetti'
		ELSE PATRBF.NOME_PROGETTO
	END AS nome_progetto,
	CASE 
		WHEN AREA.ID_AREA = 'Inv' THEN SUM(PATRBF.CTV) OVER (PARTITION BY AREA.ID_AREA, NOME_PROGETTO) 
		ELSE NULL	
	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,

--aggiunta ESG 2024
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_INVEST_SFDR_MIN_SUST_E), '.', ',')-- + '%'					  ----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_INVEST_SFDR_MIN_SUST_S), '.', ',')-- + '%'					  ----decommenta per ESG 2024
END AS PERC_INVESTIMENTO_SFDR_SOC,																  ----decommenta per ESG 2024


	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 AREA.ID_AREA) AS somma_controval_needarea



											   



	--V
											



FROM 
C6MART.PATRIMONIO_BF PATRBF
INNER JOIN 
C6MART.ANAG_PRODOTTI ANAGPROD 
	ON 
	ANAGPROD.COD_PRODOTTO = PATRBF.COD_PRODOTTO
INNER JOIN 
C6MART.AREA_BISOGNO AREA 
	ON LEFT(PATRBF.ID_AREA,3) = AREA.ID_AREA 
left join [C6Staging].[ESG_ANAG_METRICHE] esg
on esg.cod_prodotto =
