CREATE   procedure [C6MartPeriodico].[PL_S185UCaratteristicheESG_bkp_20250228_Pre_ESG2024] --'F','BRGLCU52B09F839W'
 -- 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 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,
	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
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.ORDINAMENTO = 10 then 10
	when AREA.ORDINAMENTO = 5 then 5
	when AREA.ORDINAMENTO = 4 then 4
	WHEN AREA.ORDINAMENTO = 8 or area.ORDINAMENTO = 9 then 3 
	when AREA.ORDINAMENTO = 3 then 3
	when AREA.ORDINAMENTO = 2 then 2
	when AREA.ORDINAMENTO = 1 then 1
	ELSE  3
END , nome_progetto, PATRBF.CTV DESC
END