-- Stored procedure
-- =============================================
-- Author:		Simone D'Elia
-- Create date: Aprile 2022
-- Description: 
-- =============================================
-- [C6MartPeriodico].[PL_S172PreferenzeESG] 'S', 'BCCNDR69P21A462P'
CREATE     procedure [C6MartPeriodico].[PL_S181PreferenzeESG] --'F','MRTDRJ66P03G283W'
 -- Add the parameters for the stored procedure here
 @Rete char(1), 
 @CodiceFiscale varchar(16)
AS
BEGIN
DECLARE @SCADUTO_PARZIALE varchar(16)
SET @SCADUTO_PARZIALE= (select distinct FLGPROF from C6MartPeriodico.wseirpf where rete=@rete and CODFIS=@CodiceFiscale)
SELECT esg.[rete]
      ,[cod_fiscale]
      --,case 
		--when @SCADUTO_PARZIALE='P' then 0 else [Preferenza_esg_cliente]
		--end as
	  ,[Preferenza_esg_cliente]
      ,[ESG_E]
      ,[ESG_S]
      ,[ESG_G]
      ,CAST(ROUND(ISNULL(obiettivo_ESG_minimo, 0.00), 2) AS decimal(6, 2)) AS obiettivo_ESG_minimo
      ,[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 coerenza<>'Z' and isnull(ind.SOGLIAESG,'0.00')>=25 and isnull(ind.SOGLIAESG,'0.00')<50 then 'Almeno pari al 25%'
			when coerenza<>'Z' and isnull(ind.SOGLIAESG,'0.00')>=50 and isnull(ind.SOGLIAESG,'0.00')<75 then 'Almeno pari al 50%'
			when coerenza<>'Z' and isnull(ind.SOGLIAESG,'0.00')>=75 then 'Almeno pari al 75%' else '-' end as Pref_Sost
	,case when cle.U_PREFSOST='S' then 'Y' 
		when cle.U_PREFSOST='N' then 'N' 
		else '-' end as U_PREFSOST --Ulteriori Preferenze di Sostenibilità modifica 10122024 Scapellato						   ----decommenta per ESG 2024
	--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
	--,'Emissioni gas serra, Biodiversità, Acqua, Rifiuti, Governativi di tipo Ambientale, Settore immobiliare' as PAI_A				   ----decommenta per ESG 2024
	--,'Questioni Sociali e dei Dipendenti, Governativi di tipo Sociale' as PAI_S										   ----decommenta per ESG 2024
	,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].[C6MartPeriodico].[BrsClienteESG] esg
   left join C6MartPeriodico.wseian2 ind
  on ind.rete=esg.rete and ind.CODFIS=esg.cod_fiscale
  left join C6MartPeriodico.W6CLIESG cle
  on ind.rete=cle.rete and ind.CODFIS=cle.CODFIS
WHERE 1 = 1
	AND esg.RETE = @Rete
	AND esg.COD_FISCALE = @CodiceFiscale
	AND ISNULL(@SCADUTO_PARZIALE,'') <>'P'
END