--select distinct u_prefsost  from [C6MartPeriodico].[W6CliESG] cli
--select * from wh.Maledetti
CREATE  procedure [C6MartPeriodico].[PL_S188PaiESG] --'F','BNDDDM46C12B967J'
 -- 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)
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 che considerano almeno un PAI ambientale' as Descrizione,
	replace(cast(cli.PAIA_PERCT as varchar(16)),'.',',') as Peso,
	'PAI Ambientali' as Tabella
		,'N' as isNA
	  from [C6MartPeriodico].[W6CliESG] cli
 WHERE 1 = 1
 AND cli.RETE =  @Rete
 AND cli.CODFIS =@CodiceFiscale
AND ISNULL(@SCADUTO_PARZIALE,'') <>'P'
	union
select 2 as ID,
	'di cui in linea con le Preferenze di Sostenibilità' as Descrizione,
	replace(cast(cli.PAIA_PERC_OK as varchar(16)),'.',',') as Peso,
	'PAI Ambientali' as Tabella
		,case when isnull(cli.PAI_A,'')='' then 'Y' 
		else 'N' end as isNA 
	  from [C6MartPeriodico].[W6CliESG] cli
 WHERE 1 = 1
 AND cli.RETE =  @Rete
 AND cli.CODFIS =@CodiceFiscale
AND ISNULL(@SCADUTO_PARZIALE,'') <>'P'
	union
select 3 as ID,
	'Prodotti che non considerano PAI ambientali' as Descrizione,
 	replace(cast((100-cli.PAIA_PERCT) as varchar(16)),'.',',') as Peso,
	'PAI Ambientali' as Tabella
		,'N' as isNA
	  from [C6MartPeriodico].[W6CliESG] cli
 WHERE 1 = 1
 AND cli.RETE =  @Rete
 AND cli.CODFIS =@CodiceFiscale
AND ISNULL(@SCADUTO_PARZIALE,'') <>'P'
	union
select 4 as ID,
	'TOTALE' as Descrizione,
	'100,00' as Peso,
	'PAI Ambientali' as Tabella
		,'N' as isNA
	union
select 5 as ID,
	'Prodotti che considerano almeno un PAI sociale' as Descrizione,
	replace(cast(cli.PAIS_PERCT as varchar(16)),'.',',') as Peso,
	'PAI Sociali' as Tabella
		,'N' as isNA
from [C6MartPeriodico].[W6CliESG] cli
 WHERE 1 = 1
 AND cli.RETE =  @Rete
 AND cli.CODFIS =@CodiceFiscale
AND ISNULL(@SCADUTO_PARZIALE,'') <>'P'
	union
select 6 as ID,
	'di cui in linea con le Preferenze di Sostenibilità' as Descrizione,
	replace(cast(cli.PAIS_PERC_OK as varchar(16)),'.',',') as Peso,
	'PAI Sociali' as Tabella
		,case when isnull(cli.PAI_S,'')='' then 'Y' 
		else 'N' end as isNA 
from [C6MartPeriodico].[W6CliESG] cli
 WHERE 1 = 1
 AND cli.RETE =  @Rete
 AND cli.CODFIS =@CodiceFiscale
AND ISNULL(@SCADUTO_PARZIALE,'') <>'P'
	union
select 7 as ID,
	'Prodotti che non considerano PAI sociali' as Descrizione,
	replace(cast((100-cli.PAIS_PERCT) as varchar(16)),'.',',') as Peso,
	'PAI Sociali' as Tabella
		,'N' as isNA
from [C6MartPeriodico].[W6CliESG] cli
 WHERE 1 = 1
 AND cli.RETE =  @Rete
 AND cli.CODFIS =@CodiceFiscale
AND ISNULL(@SCADUTO_PARZIALE,'') <>'P'
	union
select 8 as ID,
	'TOTALE' as Descrizione,
	'100,00' as Peso,
	'PAI Sociali' as Tabella
		,'N' as isNA
end															 
END