-- Stored procedure
-- =============================================
-- Author:		Simone D'Elia
-- Create date: Aprile 2022
-- Description: 
-- =============================================
-- [C6MartPeriodico].[PL_S172PreferenzeESG] 'S', 'BCCNDR69P21A462P'
CREATE    procedure [C6MartPeriodico].[PL_FD181PreferenzeESG_bkp_20240911]--'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
   FROM [C6StampeCentralizzate].[C6MartPeriodico].[BrsClienteESG] esg
   left join C6MartPeriodico.wseian2 ind
  on ind.rete=esg.rete and ind.CODFIS=esg.cod_fiscale
WHERE 1 = 1
	AND esg.RETE = @Rete
	AND esg.COD_FISCALE = @CodiceFiscale
	AND ISNULL(@SCADUTO_PARZIALE,'') <>'P'
END