-- Stored procedure
-- =============================================
-- Author:		Simone D'Elia
-- Create date: Aprile 2022
-- Description: 
-- =============================================
-- [C6MartPeriodico].[PL_S172PreferenzeESG] 'S', 'VNZGPL53M04L781P'
CREATE procedure [C6Mart].[PL_FD181PreferenzeESG] --'W','MLDDND66M21B967S'
 -- Add the parameters for the stored procedure here
 @Rete char(1), 
 @CodiceFiscale varchar(16)
AS
BEGIN
SELECT esg.[rete]
      ,esg.[cod_fiscale]
      ,esg.[Preferenza_esg_cliente]
      ,esg.[ESG_E]
      ,esg.[ESG_S]
      ,esg.[ESG_G]
      ,CAST(ROUND(ISNULL(obiettivo_ESG_minimo, 0.00), 2) AS decimal(6, 2)) AS obiettivo_ESG_minimo
      ,esg.[ESG_Rating]
      ,CAST(ROUND(ISNULL(ESG_E_perc, 0.00), 2) AS decimal(6, 2)) AS ESG_E_perc
      ,CAST(ROUND(ISNULL(ESG_S_perc, 0.00), 2) AS decimal(6, 2)) AS ESG_S_perc
      ,CAST(ROUND(ISNULL(ESG_G_perc, 0.00), 2) AS decimal(6, 2)) AS ESG_G_perc
	  ,case when isnull(ind.percESG,'0.00')>=25 and isnull(ind.percESG,'0.00')<50 then 'Almeno pari al 25%'
			when isnull(ind.percESG,'0.00')>=50 and isnull(ind.percESG,'0.00')<75 then 'Almeno pari al 50%'
			when isnull(ind.percESG,'0.00')>=75 then 'Almeno pari al 75%' else 'n.d.' end as Pref_Sost
	,case when cle.U_PREFSOST='S' then 'Y' 
		when cle.U_PREFSOST='N' then 'N' else 'N' end as U_PREFSOST --Ulteriori Preferenze di Sostenibilità							   ----decommenta per ESG 2024
	,case when cle.FLAGSFDR='S' then replace(cast(cle.SFDR_PERC as varchar(12)),'.00','')+'%' else '-' end as SFDR_PERC																   ----decommenta per ESG 2024
	,case when cle.FLAGTAXO='S' then replace(cast(cle.TAXO_PERC as varchar(12)),'.00','')+'%' else '-' end as TAXO_PERC																   ----decommenta per ESG 2024
	,case when cle.PAI_NO_DETT = 'S' then 'Preferisco non dettagliare' --modifica 10122024 Scapellato	
		  when isnull(cle.PAI_A,'')='' then '-' else cle.PAI_A end as PAI_A
	,case when cle.PAI_NO_DETT = 'S' then 'Preferisco non dettagliare' --modifica 10122024 Scapellato	
		  when isnull(cle.PAI_S,'')='' then '-' else cle.PAI_S end as PAI_S
	,case when ESG_E='Y' then cle.PERCAMB_OK ELSE NULL END AS E_PERC_OK--,CAST(ROUND(ISNULL(ESG_E_perc, 0.00), 2) AS decimal(6, 2)) AS E_PERC_OK				   ----decommenta per ESG 2024
    ,case when ESG_S='Y' then cle.PERCSOC_OK ELSE NULL END AS S_PERC_OK--,CAST(ROUND(ISNULL(ESG_S_perc, 0.00), 2) AS decimal(6, 2)) AS S_PERC_OK				   ----decommenta per ESG 2024
    ,case when ESG_G='Y' then cle.PERCGOV_OK ELSE NULL END AS G_PERC_OK--,CAST(ROUND(ISNULL(ESG_G_perc, 0.00), 2) AS decimal(6, 2)) AS G_PERC_OK				   ----decommenta per ESG 2024
  FROM [C6StampeCentralizzate].[C6Mart].[BrsClienteESG] esg
  left join c6staging.VAR_ADEGUATEZZA_INDICATORI ind
  on ind.rete=esg.rete and ind.CODICE_FISCALE=esg.cod_fiscale
    left join C6Mart.W6CLIESG cle
  on ind.rete=cle.rete and ind.CODICE_FISCALE=cle.CODFIS
WHERE 1 = 1
	AND esg.RETE = @Rete
	AND esg.COD_FISCALE = @CodiceFiscale
END