-- Schema: C6Staging
-- Stored Procedure: SRV_STORICIZZAZIONE


-- [C6Staging].[SRV_STORICIZZAZIONE] 'ACBDENOTH'
CREATE procedure [C6Staging].[SRV_STORICIZZAZIONE]
	@NomeTabella VARCHAR(50) = NULL
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	DECLARE @ID_ELAB INT			-- Elaborazione da storicizzare
	DECLARE @MIN_ID_ELAB AS INT		-- Elaborazione vecchia da eliminare dallo storico
	DECLARE @TempoStoricoC6 AS INT	-- Numero di giorni di vita dello storico

	SET @TempoStoricoC6 = dbo.getparametro('TempoStoricoC6') 

	SELECT @MIN_ID_ELAB = ISNULL(MAX(ID_ELAB),0)
	FROM C6MART.ELABORAZIONI
	WHERE DATEDIFF(DAY,DATA_OPERAZIONE,GETDATE()) > @TempoStoricoC6

	SET @ID_ELAB = C6MART.GETIDELAB()

-- XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX TABELLE XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

	--XXXXXXXXXXXXXXXXXXXXXXXX ACBDENOTH XXXXXXXXXXXXXXXXXXXXXXXX--
	IF(@NomeTabella IS NULL OR @NomeTabella = 'ACBDENOTH')
		BEGIN
			DELETE FROM C6Storico.ST_ACBDENOTH WHERE ID_ELAB >= @ID_ELAB OR ID_ELAB < @MIN_ID_ELAB
			INSERT INTO C6Storico.ST_ACBDENOTH
			(OID_, VERSION_, BDPERCENTAGE, BDBETA, F_OID__PASSE_CDF1A, F_PFINANCIAL_815C0, ID_ELAB)
			SELECT ACBDENOTH.OID_, ACBDENOTH.VERSION_, ACBDENOTH.BDPERCENTAGE, ACBDENOTH.BDBETA, ACBDENOTH.F_OID__PASSE_CDF1A, ACBDENOTH.F_PFINANCIAL_815C0, @ID_ELAB 
			FROM C6Staging.ACBDENOTH
			LEFT OUTER JOIN C6Storico.ST_ACBDENOTH
				ON ACBDENOTH.OID_ = ST_ACBDENOTH.OID_
			WHERE ST_ACBDENOTH.OID_ IS NULL
		END

	--XXXXXXXXXXXXXXXXXXXXXXXX ACBDENTRY XXXXXXXXXXXXXXXXXXXXXXXX--
	IF(@NomeTabella IS NULL OR @NomeTabella = 'ACBDENTRY')
		BEGIN
			DELETE FROM C6Storico.ST_ACBDENTRY WHERE ID_ELAB >= @ID_ELAB OR ID_ELAB < @MIN_ID_ELAB
			INSERT INTO C6Storico.ST_ACBDENTRY
			(OID_, VERSION_, F_PINDEXBREA_08079, F_PINSTRUMEN_FBCDE, BDPERCENTAGE, BDBETA, F_OID__PASSE_B8A1E, F_PFINANCIAL_65EBB, F_PFINANCIAL_C2FD3, ID_ELAB)
			SELECT ACBDENTRY.OID_, ACBDENTRY.VERSION_, ACBDENTRY.F_PINDEXBREA_08079, ACBDENTRY.F_PINSTRUMEN_FBCDE, ACBDENTRY.BDPERCENTAGE, ACBDENTRY.BDBETA, ACBDENTRY.F_OID__PASSE_B8A1E, ACBDENTRY.F_PFINANCIAL_65EBB, ACBDENTRY.F_PFINANCIAL_C2FD3, @ID_ELAB 
			FROM C6Staging.ACBDENTRY
			LEFT OUTER JOIN C6Storico.ST_ACBDENTRY
				ON ACBDENTRY.OID_ = ST_ACBDENTRY.OID_
			WHERE ST_ACBDENTRY.OID_ IS NULL
		END
 
	--XXXXXXXXXXXXXXXXXXXXXXXX ADBKRELAT XXXXXXXXXXXXXXXXXXXXXXXX--
	IF(@NomeTabella IS NULL OR @NomeTabella = 'ADBKRELAT')
		BEGIN	
			DELETE FROM C6Storico.ST_ADBKRELAT WHERE ID_ELAB >= @ID_ELAB OR ID_ELAB < @MIN_ID_ELAB
			INSERT INTO C6Storico.ST_ADBKRELAT
			(OID_, VERSION_, TYPE__TYPE_CAB4A, ADBKRELDESCR, ADBKRELNOTE, DEPENDANT, PHONEDECIMAL, F_OID__ADBKR_CBA9A, F_PADDRESSBO_24DA1, ID_ELAB)
			SELECT ADBKRELAT.OID_, ADBKRELAT.VERSION_, ADBKRELAT.TYPE__TYPE_CAB4A, ADBKRELAT.ADBKRELDESCR, ADBKRELAT.ADBKRELNOTE, ADBKRELAT.DEPENDANT, ADBKRELAT.PHONEDECIMAL, ADBKRELAT.F_OID__ADBKR_CBA9A, ADBKRELAT.F_PADDRESSBO_24DA1, @ID_ELAB 
			FROM C6Staging.ADBKRELAT
			LEFT OUTER JOIN C6Storico.ST_ADBKRELAT
				ON ADBKRELAT.OID_ = ST_ADBKRELAT.OID_
			WHERE ST_ADBKRELAT.OID_ IS NULL
 		END

	--XXXXXXXXXXXXXXXXXXXXXXXX ADS4WS_PROMOTORI XXXXXXXXXXXXXXXXXXXXXXXX--
	IF(@NomeTabella IS NULL OR @NomeTabella = 'ADS4WS_PROMOTORI')
		BEGIN
			DELETE FROM C6Storico.ST_ADS4WS_PROMOTORI WHERE ID_ELAB >= @ID_ELAB OR ID_ELAB < @MIN_ID_ELAB
			INSERT INTO C6Storico.ST_ADS4WS_PROMOTORI
			(Rete, Codice, Cognome, Nome, Prefisso, Firma, DataInizioCollaborazione, DataFineCollaborazione, DataUltimaAggiornamento, CAP, Citta, CittaNascita, DataNascita, Indirizzo, Provincia, PrefissoTelefono, TelefonoAgenzia1, TelefonoAgenzia2, ID_ELAB)
			SELECT Rete, Codice, Cognome, Nome, Prefisso, Firma, DataInizioCollaborazione, DataFineCollaborazione, DataUltimaAggiornamento, CAP, Citta, CittaNascita, DataNascita, Indirizzo, Provincia, PrefissoTelefono, TelefonoAgenzia1, TelefonoAgenzia2, @ID_ELAB 
			FROM C6Staging.AD
