-- =============================================
-- Author:		<Luca,,Name>
-- Create date: <Create Date,,>
-- Description:	<Popola la struttura piatta dei promotori,,>
-- =============================================
CREATE procedure [C6Mart].[DM_STRUTTURA_RETE]
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	--SET NOCOUNT ON;
DECLARE @righe INT
SET @righe = 0
-- Log esecuzione
INSERT INTO LOG_ESECUZIONE
	(Nome, Inizio, Nota)
VALUES (
	'DM_STRUTTURA_RETE',
	GETDATE(),
	'Elaborazione giornaliera'
)
--codice per forzare l'exit in caso di delay delle risposte dal SIMPB
--if (select count(*) from log_esecuzione where nome='SIM_PB' and abs(datediff(minute,getdate(),inizio))>350)>=1
--	begin RAISERROR ('Risposte in ritardo dai WSDL del SIMPB',16,10);
--	select * from blablabla;
--   end
--V Utilizzato per mandare avanti la procedura, al momento non ho clienti associati al pb
delete from  C6Staging.SPB_NAC
where
codiceRete = 'S'
and codiceAgente = '008521'
and codiceManager = 'I03128'
--EN20160427: Eliminare agenti con codici maggiore delal capienza della tabella destinaria (varchr(6), per mandare avanti la procedura
--            per ora riguarda solo l'agente 'AM Fittizio di Agente Speciale Firenze'
delete from  C6Staging.SPB_NAC
where LEN(codiceagente) > 6
and codicemanager = ''
--SVUOTAMENTO STRUTTURA
TRUNCATE TABLE C6Mart.STRUTTURA_RETE
--INSERIMENTO PB
INSERT INTO C6Mart.STRUTTURA_RETE 
		   ([Rete]
		   ,[PB]
		   ,[CognomeNomePB]
		   ,[SPV]
		   ,[CognomeNomeSPV]
		   ,[RM]
		   ,[CognomeNomeRM]
		   ,[DM]
		   ,[CognomeNomeDM]
		   ,[AM]
		   ,[CognomeNomeAM]
		   ,[Livello])
SELECT 
		AM.codicerete as Rete,
		dbo.CodiceAgenteDirettiDi(PB.codiceagente) as PB, PB.cognomeNome as cognomeNomePB,
		dbo.CodiceAgenteDirettiDi(SPV.codiceagente) as SPV, SPV.cognomeNome as cognomeNomeSPV,
		dbo.CodiceAgenteDirettiDi(RM.codiceagente) as RM, RM.cognomeNome as cognomeNomeRM,
		dbo.CodiceAgenteDirettiDi(DM.codiceagente) as DM, DM.cognomeNome as cognomeNomeDM,
		AM.codiceagente as AM, AM.cognomeNome as cognomeNomeAM,
		PB.LivelloNellaGerarchia as Livello
FROM C6Staging.SPB_NAC AM
INNER JOIN C6Staging.SPB_NAC DM
ON
	DM.codicerete = AM.codicerete
	AND 
	DM.codicemanager = AM.codiceagente 
	AND
	DM.LivelloNellaGerarchia = 'DM'
INNER JOIN C6Staging.SPB_NAC RM
ON
	RM.codicerete = DM.codicerete
	AND 
	RM.codicemanager = DM.codiceagente 
	AND
	RM.LivelloNellaGerarchia = 'RM'
INNER JOIN C6Staging.SPB_NAC SPV
ON
	SPV.codicerete = RM.codicerete
	AND 
	SPV.codicemanager = RM.codiceagente 
	AND
	SPV.LivelloNellaGerarchia like 'SPV%'
INNER JOIN C6Staging.SPB_NAC PB
ON
	PB.codicerete = SPV.codicerete
	AND 
	PB.codicemanager = SPV.codiceagente 
	AND
	PB.LivelloNellaGerarchia = 'PB'
	AND 
	PB.cognomeNome not LIKE 'I FITTIZI DI%'
WHERE AM.codicemanager = ''
AND AM.LivelloNellaGerarchia = 'AM'
ORDER BY 1,2,4,6,8,10
SET @righe = @righe + @@ROWCOUNT
--INSERIMENTO AM COME PB
INSERT INTO C6Mart.STRUTTURA_RETE
		   ([Rete]
		   ,[PB]
		   ,[CognomeNomePB]
		   ,[SPV]
		   ,[CognomeNomeSPV]
		   ,[RM]
		   ,[CognomeNomeRM]
		   ,[DM]
		   ,[CognomeNomeDM]
		   ,[AM]
		   ,[CognomeNomeAM]
		   ,[Livello])
SELECT	
AM.codicerete as Rete,
		AM.codiceagente as PB, AM.cognomeNome as cognomeNomePB,
		AM.codiceagente as SPV, AM.cognomeNome as cognomeNomeSPV,
		AM.codiceagente as RM, AM.cognomeNome as cognomeNomeRM,
		AM.codiceagente as DM, AM.cognomeNome as cognomeNomeDM,
		AM.codiceagente as AM, AM.cognomeNome as cognomeNomeAM,
		AM.LivelloNellaGerarchia as Livello
FROM C6Staging.SPB_NAC AM
WHERE AM.codicemanager = ''
	AND AM.LivelloNellaGerarchia = 'AM'
ORDER BY 1,2
SET @righe = @righe + @@ROWCOUNT
--INSERIMENTO DM COME PB
INSERT INTO C6Mart.STRUTTURA_RETE
		   ([Rete]
		   ,[PB]
		   ,[CognomeNomePB]
		   ,[SPV]
		   ,[CognomeNomeSPV]
		   ,[RM]
		   ,[CognomeNomeRM]
		   ,[DM]
		   ,[CognomeNomeDM]
		   ,[AM]
		   ,[CognomeNomeAM]
		   ,[Livello])
SELECT	
AM.codicerete as Rete,
		DM.codiceagente as PB, DM.cognomeNome as cognomeNomePB,
		DM.codiceagente as SPV, DM.cognomeNome as cognomeNomeSPV,
		DM.codiceagente as RM, DM.cognomeNome as cognomeNomeRM,
		DM.codiceagente as DM, DM.cognomeNome as cognomeNomeDM,
		AM.codiceagente as AM, AM.cognomeNome as cognomeNomeAM,
		DM.LivelloNellaGerarchia as Livello
FROM C6Staging.SPB_NAC AM
INNER JOIN C6Staging.SPB_NAC DM
ON
	DM.codicerete = AM.codicerete
	AND 
	DM.codicemanager = AM.codiceagente 
	AND
	DM.LivelloNellaGerarchia = 'DM'
	AND left(DM.codiceagente,1) NOT  in ('I','S')
WHERE AM.codicemanager = ''
	AND AM.LivelloNellaGerarchia = 'AM'
ORDER BY 1,2,4
SET @righe = @righe + @@ROWCOUNT
--INSERIMENTO RM COME PB
INSERT INTO C6Mart.STRUTTURA_RETE
		   ([Rete]
		   ,[PB]
		   ,[CognomeNomePB]
		   ,[SPV]
		   ,[CognomeNomeSPV]
		   ,[RM]
		   ,[CognomeNomeRM]
		   ,[DM]
		   ,[CognomeNomeDM]
		   ,[AM]
		   ,[CognomeNomeAM]
		   ,[Livello])
SELECT	
AM.codicerete as Rete,
		RM.codiceagente as PB, RM.cognomeNome as cognomeNomePB,
		RM.codiceagente as SPV, RM.cognomeNome as cognomeNomeSPV,
		RM.codiceagente as RM, RM.cognomeNome as cognomeNomeRM,
		dbo.CodiceAgenteDirettiDi(DM.codiceagente) as DM, DM.cognomeNome as cognomeNomeDM,
		AM.codiceagente as AM, AM.cognomeNome as cognomeNomeAM,
		RM.LivelloNellaGerarchia as Livello
from C6Staging.SPB_NAC AM
INNER JOIN C6Staging.SPB_NAC DM
ON
	DM.codicerete = AM.codicerete
	AND 
	DM.codicemanager = AM.codiceagente 
	AND
	DM.LivelloNellaGerarchia = 'DM'
--	AND left(DM.codiceagente,1) <> 'I'
INNER JOIN C6Staging.SPB_NAC RM
ON
	RM.codicerete = DM.codicerete
	AND 
	RM.codicemanager = DM.codiceagente 
	AND
	RM.LivelloNellaGerarchia = 'RM'
	AND left(RM.codiceagente,1) NOT IN ('I','S')
where AM.codicemanager = ''
	AND AM.LivelloNellaGerarchia = 'AM'
ORDER BY 1,2,4,6
SET @righe = @righe + @@ROWCOUNT
--INSERIMENTO SPV COME PB
INSERT INTO C6Mart.STRUTTURA_RETE
		   ([Rete]
		   ,[PB]
		   ,[CognomeNomePB]
		   ,[SPV]
		   ,[CognomeNomeSPV]
		   ,[RM]
		   ,[CognomeNomeRM]
		   ,[DM]
		   ,[CognomeNomeDM]
		   ,[AM]
		   ,[CognomeNomeAM]
		   ,[Livello])
SELECT	
AM.codicerete as Rete,
		SPV.codiceagente as PB, SPV.cognomeNome as cognomeNomePB,
		SPV.codiceagente as SPV, SPV.cognomeNome as cognomeNomeSPV,
		dbo.CodiceAgenteDirettiDi(RM.codiceagente) as RM, RM.cognomeNome as cognomeNomeRM,
		dbo.CodiceAgenteDirettiDi(DM.codiceagente) as DM, DM.cognomeNome as cognomeNomeDM,
		AM.codiceagente as AM, AM.cognomeNome as cognomeNomeAM,
		SPV.LivelloNellaGerarchia as Livello
FROM C6Staging.SPB_NAC AM
INNER JOIN C6Staging.SPB_NAC DM
ON
	DM.codicerete = AM.codicerete
	AND 
	DM.codicemanager = AM.codiceagente 
	AND
	DM.LivelloNellaGerarchia = 'DM'
--	AND left(DM.codiceagente,1) <> 'I'
INNER JOIN C6Staging.SPB_NAC RM
ON
	RM.codicerete = DM.codicerete
	AND 
	RM.codicemanager = DM.codiceagente 
	AND
	RM.LivelloNellaGerarchia = 'RM'
--	AND left(RM.codiceagente,1) NOT IN ('I','S')
INNER join C6Staging.SPB_NAC SPV
ON
	SPV.codicerete = RM.codicerete
	AND 
	SPV.codicemanager = RM.codiceagente 
	AND
	SPV.LivelloNellaGerarchia like 'SPV%'
	AND left(SPV.codiceagente,1) NOT IN ('I','S')
WHERE AM.codicemanager = ''
	AND AM.LivelloNellaGerarchia = 'AM'
ORDER BY 1,2,4,6,8
SET @righe = @righe + @@ROWCOUNT
-- Log esecuzione
UPDATE
	LOG_ESECUZIONE
SET
	Fine = GETDATE(),
	Tipo = 'INSERT',
	Righe = @righe
WHERE
	Nome = 'DM_STRUTTURA_RETE' AND
	Inizio = (
		SELECT
			MAX(Inizio)
		FROM
			LOG_ESECUZIONE
		WHERE
			Nome = 'DM_STRUTTURA_RETE'
	)
-- 14 GENNAIO 2009 MODIFICA PAOLO GIOVANETTI PER AGGIUNTA ID_ELAB
--ESTRAZIONE ID_ELAB
DECLARE @ID_ELAB INT
SET @ID_ELAB = C6MART.GETIDELAB()
UPDATE C6Mart.STRUTTURA_RETE 
SET ID_ELAB = @ID_ELAB
WHERE ID_ELAB IS NULL
-- 14 GENNAIO 2009 FINE MODIFICA PAOLO GIOVANETTI PER AGGIUNTA ID_ELAB
END