CREATE   procedure [C6Mart].[RP_DM_W6CatESG]
as
begin 
	-- Log esecuzione
	INSERT INTO LOG_ESECUZIONE
		(Nome, Inizio, Nota)
	VALUES (
		'RP_DM_SPB_W6CatESG',
		GETDATE(),
		'Elaborazione giornaliera'
	)
--drop table c6staging.RP_CatalogoProdotti
	truncate table [C6StampeCentralizzate].[C6Mart].[SPB_W6CatESG]
	truncate table [C6StampeCentralizzate].[C6Mart].[W6CatESG]
INSERT INTO [C6Mart].[SPB_W6CatESG]
           ([Rete]
           ,[Codice_Agente]
           ,[Codice_Fiscale]
           ,[Piva_Fiduciaria]
           ,[CodiceContratto]
           ,[FamigliaProdotto]
           ,[CodiceProdotto]
           ,[DescrizioneProdotto]
           ,[CodiceTitolo]
           ,[Conto]
           ,[Custgar]
           ,[Asset]
           ,[Pct]
           ,[CodiceSottoprodotto]
           ,[DescrizioneSottoprodotto]
           ,[Isin]
           ,[IsinProxy]
           ,[TipoFondo]
           ,[Controvalore]
           ,[Importo]
           ,[QuantitaValoreNominale]
           ,[VarComparto]
           ,[CodiceLinea]
           ,[Sost_E]
           ,[Sost_S]
           ,[Sost_G]
           ,[ClasseComplessita]
           ,[DescrizioneClasseComplessita]
           ,[OrizzonteTemporale]
           ,[FlagRiserva])
SELECT [Rete]
      ,[Codice_Agente]
      ,[Codice_Fiscale]
      ,[Piva_Fiduciaria]
      ,[CodiceContratto]
      ,case when [FamigliaProdotto]='AS' then 'ASU1' 
			when [FamigliaProdotto]='OF' then 'GE'
	  else [FamigliaProdotto] end as [FamigliaProdotto]
      ,[CodiceProdotto]
      ,[DescrizioneProdotto]
      ,[CodiceTitolo]
      ,[Conto]
      ,[Custgar]
      ,[Asset]
      ,[Pct]
      ,case when [CodiceLinea]='FP' then 'FP' else [CodiceSottoprodotto] end as [CodiceSottoprodotto]
      ,[DescrizioneSottoprodotto]
      ,[Isin]
      ,[IsinProxy]
      ,[TipoFondo]
      ,[Controvalore]
      ,[Importo]
      ,[QuantitaValoreNominale]
      ,[VarComparto]
      ,[CodiceLinea]
      ,[Sost_E]
      ,[Sost_S]
      ,[Sost_G]
      ,[ClasseComplessita]
      ,[DescrizioneClasseComplessita]
      ,[OrizzonteTemporale]
      ,[FlagRiserva]
  FROM [C6StampeCentralizzate].[C6Staging].[SPB_W6CatESG]
  update b
  set  b.positionID=a.POSITIONID --, b.catalogueID=a.catalogueID
 from [C6Mart].[SPB_W6CatESG] b
  left join C6Staging.SPB_CONTR_SINTESI a
  on a.RETE=b.rete
  and a.CODICEFISCALE=b.codice_fiscale
  and a.CODICECONTRATTO=b.codicecontratto
 and a.CTV = b.controvalore 
   update b
  set  b.positionID=a.POSITIONID --, b.catalogueID=a.catalogueID
 from [C6Mart].[SPB_W6CatESG] b
  left join C6Staging.SPB_CONTR_SINTESI a
  on a.RETE=b.rete
  and a.CODICEFISCALE=b.codice_fiscale
  and a.CODICECONTRATTO=b.codicecontratto
 and a.CODICEINTERNO = b.CodiceProdotto
 and a.CODICESOTTOPRODOTTO=b.CodiceSottoprodotto
 where b.PositionID is null
 insert into [C6Mart].[W6CATESG]
  select GETDATE() as dttrim,
  '03296' as Banca, 
  Rete ,
  Codice_Fiscale as codfis,
  '' as codman, 
  case when Sost_E='Y' then 'Y' else 'N' end as esg_e_prod_cli,
  case when Sost_S='Y' then 'Y' else 'N' end as esg_s_prod_cli,
  case when Sost_G='Y' then 'Y' else 'N' end as esg_g_prod_cli,
  SUBSTRING(POSITIONID,1,57) as prodotto_id,
  PositionID as position_id
  from C6Mart.SPB_W6CatESG
-- select * from C6Staging.SPB_CONTR_SINTESI --where CODICEFISCALE='BCCSHR68B66H433Z'
-- select * from [C6Mart].[SPB_W6CatESG] --where Codice_Fiscale='BCCSHR68B66H433Z'
	--- Log esecuzione
	UPDATE
		LOG_ESECUZIONE
	SET
		Fine = GETDATE(),
		Tipo = 'INSERT',
		Righe = @@ROWCOUNT
	WHERE
		Nome = 'RP_DM_SPB_W6CatESG' AND
		Inizio = (
			SELECT
				MAX(Inizio)
			FROM
				LOG_ESECUZIONE
			WHERE
				Nome = 'RP_DM_SPB_W6CatESG'
		)
end