﻿--select * from wh.Maledetti
CREATE   procedure [C6Mart].[PL_S187SfdrTaxESG]-- 'F','FRDGGN27C43L194Y'
 -- 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 C6Mart.BrsClienteESG where rete=@Rete and cod_fiscale=@CodiceFiscale
declare @U_PREFSOST varchar(2)
select @U_PREFSOST=U_PREFSOST from [C6Mart].[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(isnull(cli.SFDR_PERCT,0.00) as varchar(16)),'.',',') as Peso,
	'SFDR' as Tabella
		,'N' as isNA
  from  [C6Mart].[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(isnull(cli.SFDR_PERC_OK,000) as varchar(16)),'.',',') as Peso,
	'SFDR' as Tabella
		,case when cli.FLAGSFDR='N' then 'Y' 
		else 'N' end as isNA
	from  [C6Mart].[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-isnull(cli.SFDR_PERCT,0.00))as varchar(16)),'.',',') as Peso,
	'SFDR' as Tabella
		,'N' as isNA
	  from  [C6Mart].[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  [C6Mart].[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(isnull(cli.TAXO_PERCT,0.00) as varchar(16)),'.',',') as Peso,
	'Taxonomy' as Tabella
		,'N' as isNA
	 from  [C6Mart].[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 (isnull(cli.TAXO_PERC_OK,0.00) as varchar(16)),'.',',') as Peso,
	'Taxonomy' as Tabella
		,case when cli.FLAGTAXO='N' then 'Y' 
		else 'N' end as isNA
	 from  [C6Mart].[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-isnull(cli.TAXO_PERCT,0.00)) as varchar(16)),'.',',') as Peso,
	'Taxonomy' as Tabella
		,'N' as isNA
	 from  [C6Mart].[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  [C6Mart].[W6CliESG] cli
 WHERE 1 = 1
 AND cli.RETE =  @Rete
 AND cli.CODFIS =@CodiceFiscale
--	select * from [C6Mart].[W6CliESG] cli
end															 
END