﻿--select * from wh.Maledetti
CREATE procedure [C6MartPeriodico].[PL_FD187SfdrTaxESG] --'F','00545400483'
 -- Add the parameters for the stored procedure here
 @Rete char(1), 
 @CodiceFiscale varchar(16)
AS
BEGIN
declare @pref_esg varchar(2)
select @pref_esg=Preferenza_esg_cliente from C6MartPeriodico.BrsClienteESG where rete=@Rete and cod_fiscale=@CodiceFiscale
declare @U_PREFSOST varchar(2)
select @U_PREFSOST=U_PREFSOST from [C6MartPeriodico].[W6CliESG] where rete=@Rete and codfis=@CodiceFiscale
if(@pref_esg='1' AND @U_PREFSOST='S')
begin
 select
 1 as ID,
	'Prodotti sostenibili SFDR' as Descrizione,
	replace(cast(cli.SFDR_PERCT as varchar(16)),'.',',') as Peso,
	'SFDR' as Tabella
		,'N' as isNA
  from [C6MartPeriodico].[W6CliESG] cli
 WHERE 1 = 1
 AND cli.RETE =  @Rete
 AND cli.CODFIS =@CodiceFiscale
	union
 select 2 as ID,
	'di cui in linea con le Preferenze di Sostenibilità' as Descrizione,
	 replace(cast(cli.SFDR_PERC_OK as varchar(16)),'.',',') as Peso,
	'SFDR' as Tabella
		,case when cli.FLAGSFDR='N' then 'Y' 
		else 'N' end as isNA
	from [C6MartPeriodico].[W6CliESG] cli
	WHERE 1 = 1
 AND cli.RETE =  @Rete
 AND cli.CODFIS =@CodiceFiscale
	union
select 3 as ID,
	'Prodotti non sostenibili' as Descrizione,
 	replace(cast((100-cli.SFDR_PERCT)as varchar(16)),'.',',') as Peso,
	'SFDR' as Tabella
		,'N' as isNA
	  from [C6MartPeriodico].[W6CliESG] cli
 WHERE 1 = 1
 AND cli.RETE =  @Rete
 AND cli.CODFIS =@CodiceFiscale
	union
select 4 as ID,
	'TOTALE' as Descrizione,
	'100,00' as Peso,
	'SFDR' as Tabella
		,'N' as isNA
     from [C6MartPeriodico].[W6CliESG] cli
 WHERE 1 = 1
 AND cli.RETE =  @Rete
 AND cli.CODFIS =@CodiceFiscale
	union
select 5 as ID,
	'Prodotti ecosostenibili Taxonomy' as Descrizione,
	replace(cast(cli.TAXO_PERCT as varchar(16)),'.',',') as Peso,
	'Taxonomy' as Tabella
		,'N' as isNA
	 from [C6MartPeriodico].[W6CliESG] cli
 WHERE 1 = 1
 AND cli.RETE =  @Rete
 AND cli.CODFIS =@CodiceFiscale
	union
select 6 as ID,
	'di cui in linea con le Preferenze di Sostenibilità' as Descrizione,
	 replace(cast (cli.TAXO_PERC_OK as varchar(16)),'.',',') as Peso,
	'Taxonomy' as Tabella
		,case when cli.FLAGTAXO='N' then 'Y' 
		else 'N' end as isNA
	 from [C6MartPeriodico].[W6CliESG] cli
 WHERE 1 = 1
 AND cli.RETE =  @Rete
 AND cli.CODFIS =@CodiceFiscale
	union
select 7 as ID,
	'Prodotti non ecosostenibili' as Descrizione,
	replace(cast((100-cli.TAXO_PERCT) as varchar(16)),'.',',') as Peso,
	'Taxonomy' as Tabella
		,'N' as isNA
	 from [C6MartPeriodico].[W6CliESG] cli
 WHERE 1 = 1
 AND cli.RETE =  @Rete
 AND cli.CODFIS =@CodiceFiscale
	union
select 8 as ID,
	'TOTALE' as Descrizione,
	'100,00' as Peso,
	'Taxonomy' as Tabella
		,'N' as isNA
	 from C6MartPeriodico.[W6CliESG] cli
 WHERE 1 = 1
 AND cli.RETE =  @Rete
 AND cli.CODFIS =@CodiceFiscale
end
END