-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [C6Mart].[Popolamento_RP_W6DAILY]
AS
BEGIN
	SET NOCOUNT ON;
	--dichiaro variabile data di trimestrale
	DECLARE @dttrim int
	--valorizzo la data di trimestrale al giorno del congelamento
	set @dttrim  = CAST(CONVERT(varchar, CAST(getdate() as DATETIME),112) as int)
	--print @dttrim
	--Tronchiamo la tabella RP_vTuttiIPortafogli_Aladdin_tab e la ripopoliamo
	--a partire dalla vista RP_vTuttiIPortafogli_Aladdin
	Truncate table C6StampeCentralizzate.C6Mart.w6daily
	--
	insert into C6StampeCentralizzate.C6Mart.w6daily
		select 
		'G' as FLGCLI ,
		'M' as FLGBL ,
		'A' as TIPOELA,
		@dttrim as DTTRIM,
		rete,
		codfis,
		piva,
		codman,
		'' as CODFID,
		'' as CODAGE,
		CASE  
			--WHEN cod_interno <> 'SINT3MEUR' and famprodt = 'DT' THEN isnull(cod_interno,'')   
			WHEN ((cod_interno <> 'SINT3MEUR' and cod_interno <> 'SINT6MEUR') and famprodt = 'DT') THEN isnull(cod_interno,'')
			ELSE '' 
		END  as CODABI, 
		ISNULL(termid,00000000), 
		cast(annos as int) as anno ,
		cast(progs as int) as prog ,
		0 as quote,
		cast(ctvreale as float) as CTV,
		codconf, --17
		rubr,
		custgar,
		ISNULL(cod_interno,''),
		'' as codana,
		CASE  
			WHEN    cast(annos as int) <> 0 and cast(progs as int) <> 0 then 'S' 
			ELSE 'N' 
		END as FLAGPCT,
		--CASE  
		--	WHEN famprodt <> 'DT' and famprodt <> 'CC' THEN isnull(cod_interno,'')
		--	ELSE ''   
		--END 
		'' as codprod,
		ISNULL(cod_sottoprodotto,''),
		ISNULL(cod_isin,''),
		'' as codlinea,
		tipoptf,
		area,
		progetto,
		isnull(codprj,'') as codprj,
		ISNULL(chiave_prodotto_terzi,''),
		ISNULL(assetterzi,''),
		ISNULL(famprodt,''),
		ISNULL(intermediario,''),
		ISNULL(codiceadeguatezza,''),
		ISNULL(cod_maf,''),
		conto,
		numpolizza,
		position_id,
		'' as PREFSOST,
        '' as FLAGAMB,
        '' as FLAGSOC,
        '' as FLAGGOV
		from [C6StampeCentralizzate].[C6Mart].[RP_vTuttiIPortafogli_Aladdin]
	-- Aggiungiamo le informazioni mancanti per il codman piva_fiduciaria
	update C6StampeCentralizzate.C6Mart.w6daily
	set codprj = ''
	where codprj = 0
	select substring(b.codicefiscale,4,len(b.codicefiscale)) as codman,b.rete,b.pivafiduciaria into
	#tab
	from C6StampeCentralizzate.C6Mart.w6daily a
	inner join (select distinct rete,codicefiscale,pivafiduciaria from [C6StampeCentralizzate].[C6Staging].[SPB_CONTR_SINTESI] where pivafiduciaria is not null ) b
	on a.rete=b.rete and isnull(a.codman,'')=isnull(substring(b.codicefiscale,4,len(b.codicefiscale)),'')
	--select b.cod_mandato,b.rete,b.piva_fiduciaria into 
	--	#tab
	--	FROM 
	--		[C6StampeCentralizzate].[C6MartPeriodico].[w6clirep] a
	--		inner join (select distinct rete,cod_mandato,piva_fiduciaria from [C6StampeCentralizzate].[C6StagingPeriodico].[APPO_CONTRATTI_V] where cod_mandato<>'') b
	--		on a.rete= b.rete and isnull(a.codman,'')=isnull(b.cod_mandato,'')
	update C6StampeCentralizzate.C6Mart.w6daily
	set piva = pivafiduciaria
	from  #tab tab
	inner join C6StampeCentralizzate.C6Mart.w6daily as a
	on a.rete= tab.rete and isnull(a.codman,'')=isnull(tab.codman,'')
	--UPDATE [C6StampeCentralizzate].[C6MartPeriodico].[w6clirep]
	--	set piva = piva_fiduciaria
	--	from #tab tab
	--	inner join [C6StampeCentralizzate].[C6MartPeriodico].[w6clirep] as a 
	--	on a.rete= tab.rete and isnull(a.codman,'')=isnull(tab.cod_mandato,'')
	update C6StampeCentralizzate.C6Mart.w6daily
	set codage = isnull(appo.CODAGENTE,'')
	from C6StampeCentralizzate.C6Mart.w6daily w
	left join (select distinct [RETE],[CODAGENTE],[CODICEFISCALE],[PIVAFIDUCIARIA]  from [C6StampeCentralizzate].[C6Staging].[SPB_CONTR_SINTESI] ) appo
	on w.rete= appo.rete and isnull(w.codfis,'') = isnull(appo.CODICEFISCALE,'') 
	----1 update per CF
	--update [C6StampeCentralizzate].[C6MartPeriodico].[w6clirep] 
	--	set codage = appo.cod_agente
	--	from c6stagingperiodico.appo_contratti appo
	--	left join [C6StampeCentralizzate].[C6MartPeriodico].[w6clirep] w
	--	on w.rete= appo.rete and isnull(w.codfis,'') = isnull(appo.cod_fiscale,'')
	update C6StampeCentralizzate.C6Mart.w6daily
	set codage = isnull(appo.CODAGENTE,'')
	from C6StampeCentralizzate.C6Mart.w6daily w
	left join (select distinct [RETE],[CODAGENTE],[CODICEFISCALE],[PIVAFIDUCIARIA]  from [C6StampeCentralizzate].[C6Staging].[SPB_CONTR_SINTESI] ) appo
	on w.rete= appo.rete and isnull(w.piva,'') = isnull(appo.CODICEFISCALE,'')
	where w.piva <> ''
	----2 update per Piva
	--update [C6StampeCentralizzate].[C6MartPeriodico].[w6clirep] 
	--	set codage = appo.cod_agente
	--	from c6stagingperiodico.appo_contratti appo
	--	left join [C6StampeCentralizzate].[C6MartPeriodico].[w6clirep] w
	--	on w.rete= appo.rete and isnull(w.piva,'') = isnull(appo.cod_fiscale,'')
	--	where w.piva <> ''
	update C6StampeCentralizzate.C6Mart.w6daily
	set codage = isnull(appo.CODAGENTE,'')
	from C6StampeCentralizzate.C6Mart.w6daily w
	left join (select distinct [RETE],[CODAGENTE],[CODICEFISCALE],[PIVAFIDUCIARIA]  from [C6StampeCentralizzate].[C6Staging].[SPB_CONTR_SINTESI] ) appo
	on w.rete= appo.rete and 'FF@'+(isnull(w.codman,'')) = isnull(appo.CODICEFISCALE,'')
	where w.codman <> ''
	----3 update per Fiduciaria
	--update [C6StampeCentralizzate].[C6MartPeriodico].[w6clirep] 
	--	set codage = appo.cod_agente
	--	from c6stagingperiodico.appo_contratti appo
	--	left join [C6StampeCentralizzate].[C6MartPeriodico].[w6clirep] w
	--	on w.rete= appo.rete and 'FF@'+(isnull(w.codman,'')) = isnull(appo.cod_fiscale,'')
	--	where w.codman <> ''
	--eliminazione righe duplicate per API ma le teniamo in una tb di Recupero per eventuali analisi
	--truncate table [C6MartPeriodico].[W6CLI_REK]
	--insert into [C6MartPeriodico].[W6CLI_REK]
	--select  * 
	--from [C6StampeCentralizzate].[C6MartPeriodico].[w6clirep] where tipoptf = 'TIPOPRODTERZI|TERZI'
	delete from 
	[C6StampeCentralizzate].[C6Mart].[w6daily] where tipoptf = 'TIPOPRODTERZI|TERZI'
	update C6StampeCentralizzate.C6Mart.w6daily
		set PREFSOST =  case when esg.Profsost = '1' then '1' else '0' end,
		FLAGAMB = case when esg.sost_E = 'Y' then 'Y' else 'N' end ,
		FLAGSOC = case when esg.sost_S = 'Y' then 'Y' else 'N' end ,
		FLAGGOV = case when esg.sost_G = 'Y' then 'Y' else 'N' end  
		from C6StampeCentralizzate.C6Mart.w6daily w
		left join [C6Staging].[VAR_ADEGUATEZZA_INDICATORI] esg
		on w.rete= esg.rete and isnull(w.codfis,'') = isnull(esg.CODICE_FISCALE,'') 
	update C6StampeCentralizzate.C6Mart.w6daily
		set PREFSOST =  case when esg.Profsost = '1' then '1' else '0' end,
		FLAGAMB = case when esg.sost_E = 'Y' then 'Y' else 'N' end ,
		FLAGSOC = case when esg.sost_S = 'Y' then 'Y' else 'N' end ,
		FLAGGOV = case when esg.sost_G = 'Y' then 'Y' else 'N' end 
		from C6StampeCentralizzate.C6Mart.w6daily w
		left join [C6Staging].[VAR_ADEGUATEZZA_INDICATORI] esg
		on w.rete= esg.rete and isnull(w.piva,'') = isnull(esg.CODICE_FISCALE,'') 
		where piva <> ''
	update C6StampeCentralizzate.C6Mart.w6daily
		set PREFSOST =  case when esg.Profsost = '1' then '1' else '0' end ,
		FLAGAMB = case when esg.sost_E = 'Y' then 'Y' else 'N' end ,
		FLAGSOC = case when esg.sost_S = 'Y' then 'Y' else 'N' end ,
		FLAGGOV = case when esg.sost_G = 'Y' then 'Y' else 'N' end 
		from C6StampeCentralizzate.C6Mart.w6daily w
		left join [C6Staging].[VAR_ADEGUATEZZA_INDICATORI] esg
		on w.rete= esg.rete and isnull('FF@'+ w.codman,'') = isnull(esg.CODICE_FISCALE,'') 
		where codman <> ''
END