-- Schema: C6MartPeriodico
-- Stored Procedure: GESTIONE_SELECT_PDF_GETDATATHREADS_PartiteViaggianti_CAMPIONE



CREATE   procedure [C6MartPeriodico].[GESTIONE_SELECT_PDF_GETDATATHREADS_PartiteViaggianti_CAMPIONE]
(@volThreads int = 10000)
AS
BEGIN SET TRANSACTION ISOLATION LEVEL READ COMMITTED 
	 
BEGIN TRANSACTION  
  
declare @dataTrimestre as datetime
SELECT @dataTrimestre = c6martperiodico.gettrimestre2(getdate(),0)	

----EN20170712: begin
--		;with WSEIAN_1 as
--		(
--		select codfis, rete, flagnqp, flgprlrde, FLAGPG, dttrim, row_number()  over( partition by codfis, rete order by dttrim desc)  as rank  
--		from [C6StagingPeriodico].[WSEIAN]
--		WHERE codman = ''
--		UNION ALL 
--		select 'FF@'+ codman as codfis, rete, 'N' as flagnqp, null as flgprlrde, null asFLAGPG, dttrim, row_number()  over( partition by codman, rete order by dttrim desc)  as rank
--		from [C6StagingPeriodico].[WSEIAN]
--		WHERE codman <> ''
--		) 
----EN20170712: end
--EN20171024: begin
	;with WSEIAN_1 as
	(
	select codfis, rete, flagnqp, flgprlrde, FLAGPG, FLGPROF, dttrim, row_number()  over( partition by codfis, rete order by dttrim desc)  as rank  
	from [C6StagingPeriodico].[WSEIAN2]
	WHERE codman = ''
	UNION ALL  
	select 'FF@'+ codman as codfis, rete, flagnqp, flgprlrde, FLAGPG, FLGPROF, dttrim, row_number()  over( partition by codman, rete order by dttrim desc)  as rank
	from [C6StagingPeriodico].[WSEIAN2]
	WHERE codman <> ''
	) 
--EN20171024: end
SELECT DISTINCT TOP(@volThreads)
			--DATI COMUNI
			--RETE
			contratti.Rete
			,CASE WHEN UPPER(contratti.Rete) = 'F' THEN 'Fideuram' ELSE 'Sanpaolo Invest' END NomeRete
			--AGENTE	
			,contratti.Cod_Agente Agente
			,(CASE UPPER(ISNULL(promotori.STATO,'')) WHEN 'ATTIVO' THEN isnull(promotori.Cognome,'') ELSE '' END) CognomeAgente
			,(CASE UPPER(ISNULL(promotori.STATO,'')) WHEN 'ATTIVO' THEN isnull(promotori.Nome,'') ELSE '' END) NomeAgente
			,isnull(promotori.Indirizzo,'') IndirizzoAgente
			,isnull(promotori.Cap,'') CapAgente
			,isnull(promotori.Localita,'') CittaAgente
			,isnull(promotori.TEL_Recapito,'') TelefonoAgente
			--CLIENTE
			,contratti.Cod_Fiscale CodiceFiscale
			,clienti.Cognome CognomeCliente
			,clienti.nome NomeCliente
			--***********************************************************************************************
			--PATRIMONI CLIENTE
			,CASE WHEN isnull( MIFID.SCADUTO,1) = 1 THEN 0 ELSE 1 END ProfiloIsAct			
 			,isnull(PatrNonRapprAsset.Controvalore,0) as PatrimonioNonRappresentabile
			,isnull(patrimonioBF.PartiteViaggiantiDisinv,0) as PartiteViaggiantiDisinv
			,isnull(partiteViaggianti.Importo,0) as PartiteViaggiantiInv
			,isnull(patrimonioBF.ContoCorrente,0) as  ContoCorrente
			,isnull(patrimonioBF.PatrimonioSenzaCC,0) as PatrimonioBancaFideuramCtvAlNettoContoCorrente 
			,isnull(patrimonioBF.PatrimonioNonAssociato,0) as  RisorseNonAssociate
			,isnull(patrimonioAltro.Patrimonioaltro,0) as Patrimonioaltroctv
			,isnull(patrimonioImmobiliare.PatrimonioImmob,0) as Patrimonioimmobiliarectv
			,isnull(patrimonioTerzi.ctv,0) as PatrimonioTerzictv
			,isnull(patrimonioTerzi.ControvaloreSenzaCC,0) as PatrimonioTerziCtvAlNettoContoCorrente 
			,isnull(patrimonioTerzi.ControvaloreCC,0) as PatrimonioTerziContoCorrente
			,isnull(patrimonioBF.ctv,0) as Patrimoniobancafideuramctv
			,(isnull(patrimonioBF.ctv,0) + isnull(patrimonioTerzi.ctv,0)) as Patrimoniocomplessivototalectv
			--*****************************************************************************************************
			,isnull(numIntermediari.NumeroIntermediari,0) as NumeroIntermediari
			--CONTRATTO
			,contratti.CodiceContratto CodiceContratto
			,contratti.tip_contratto AS tip_contratto 
			,0 AS contrattoold
			--*************************************************
			,0 AS NoMonitoraggio			
			,'Diagnosi' as descrizioneDiagnosi
			,-1 idModelloDiagnosi
			,-1 AS idReportDiagnosi
			,'Monitoraggio' as descrizioneMonitoraggio
			,4 as idReportMonitoraggio 
			,14 AS idModelloMonitoraggio
			--****************************************
