﻿-- Stored procedure
-- =============================================
-- Author:		Simone D'Elia
-- Create date: Aprile 2022
-- Description: 
-- =============================================
-- [C6MartPeriodico].[PL_S172PreferenzeESG] 'S', 'BCCNDR69P21A462P'
CREATE    procedure [C6MartPeriodico].[PL_FD181PreferenzeESG_prova]--'S','BCCNDR69P21A462P'
 -- 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
			,'Y' as U_PREFSOST --Ulteriori Preferenze di Sostenibilità							   ----decommenta per ESG 2024
	--,'20,00%' as SFDR_PERC																   ----decommenta per ESG 2024
	--,'50,00%' as TAXO_PERC	
	,cli.SFDR_PERC as SFDR_PERC																   
	,cli.TAXO_PERC as TAXO_PERC	----decommenta per ESG 2024
	--,'Emissioni gas serra, Biodiversità, Acqua, Settore immobiliare' as PAI_A				   ----decommenta per ESG 2024
	--,'Questioni Sociali e dei Dipendenti' as PAI_S	----decommenta per ESG 2024
	,cli.PAI_A
	,cli.PAI_S
	,case when ESG_E='Y' then 24.21 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 21.32 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 44.32 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				   ----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 cli
  on cli.rete=esg.rete and cli.CODFIS=esg.cod_fiscale
WHERE 1 = 1
	AND esg.RETE = @Rete
	AND esg.COD_FISCALE = @CodiceFiscale
	--AND ISNULL(@SCADUTO_PARZIALE,'') <>'P' non valida x i direct 
END