-- Schema: C6MartPeriodico
-- Stored Procedure: PL_S184CoerenzaESG
















--Inv	Investimento	021233XY004	BLACK ROCK EQT X 20230215	99999999	300000.000	0.000	300000.000

-- Stored procedure

-- =============================================
-- Author:		Simone D'Elia
-- Create date: Aprile 2022
-- Description: 
-- =============================================

CREATE       procedure [C6MartPeriodico].[PL_S184CoerenzaESG] --'F','RSSLRA62M47C573M'

 -- Add the parameters for the stored procedure here
 @Rete char(1), 
 @CodiceFiscale varchar(16)

AS
BEGIN
select w.Rete,
	   w.CODFIS,
	   case when w.COERENZA='Z' then '-' 
			when w.SOGLIAESG=25.00 then 'Almeno pari al 25%'
	        when w.SOGLIAESG=50.00 then 'Almeno pari al 50%'
			when w.SOGLIAESG=75.00 then 'Almeno pari al 75%'
			else '-' end as percESG,
			CAST(ROUND(ISNULL(b.obiettivo_ESG_minimo, 0.00), 2) AS decimal(10, 2)) AS pesoSostESG,
	 --  ROUND(isnull(b.obiettivo_ESG_minimo,0.00),2) as pesoSostESG,
	   case when w.COERENZA='S' then 'S'
	   when w.COERENZA='X' and ISNULL(b.obiettivo_ESG_minimo, 0.00)>w.SOGLIAESG then 'S' 
	   when w.COERENZA='Z' then 'Z'
	   else 'N' end as Coerenza
	 --  case when RISERVA='N' or invl_period='N' Or clcompl='N' Or flgconc='N' or freq='N' Or concemi='N' or ADEGCONC_VALUT='N' or COERENZA='N'  then 'N' else 'S' end as Nota_Adeguatezza
 from C6MartPeriodico.WSEIAN2 w left join 
 C6MartPeriodico.BrsClienteESG b on w.rete=b.rete and w.CODFIS=b.cod_fiscale
where w.Rete=@Rete and w.CODFIS=@CodiceFiscale
and w.PREFSOST = 'S' -- questa tabella deve apparire solo per chi ha espresso la preferenza di sostenibilit�
--and b.Preferenza_esg_cliente=1 
--and w.COERENZA<>'X' -- questa condizione serve per i clienti professional che non hanno questo indicatore
--and w.FLGPROF='N'



--COERENZA = 'S' SI
--COERENZA = 'N' NO
--COERENZA = 'X' Errore di R4
--COERENZA = 'Z' con ESG senza soglia di sostenibilita ( deve apparire n.a.)

END