-- Stored procedure
-- Stored procedure
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
--[C6MartPeriodico].[PL_MP_S129Patrimonio] 'S','VNDMRZ60L23G702B'
CREATE procedure [C6MartPeriodico].[PL_MP_S129Patrimonio_bkp_fc]
	-- Add the parameters for the stored procedure here
	@Rete char(1), 
	@CodiceFiscale varchar(16)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	-- Recupero data congelamento SEI
	DECLARE @DataCongelamento DATETIME
    DECLARE @MONIT_ATTIV INT
	DECLARE @MONIT_INIT INT
	DECLARE @DettMovPrec money
	DECLARE @DettMovCorr money
	DECLARE @AppoDettMovPrec varchar(50)
	DECLARE @AppoDettMovCorr varchar(50)
	DECLARE @NumAsterischiPrec int
	DECLARE @NumAsterischiCorr int
	DECLARE @DATA_FOTO datetime
	DECLARE @UltimoInvio int
	DECLARE @SommaProventi money
	SET @NumAsterischiPrec = 0
	SET @NumAsterischiCorr = 0
	SET @DataCongelamento = C6MartPeriodico.getDataCongelamento()
    SET  @MONIT_ATTIV=0
	SET  @MONIT_INIT = 0
   -- INSERIMENTO BLOCCO PER MONITORAGGIO CHIUSO O NON ESISTENTE
    SELECT @MONIT_ATTIV=COUNT(*)
    FROM 
		C6MARTPERIODICO.MONITORAGGIO_ATTUALE
    WHERE
		RETE = @Rete AND
		COD_FISCALE = @CodiceFiscale
	SELECT @MONIT_INIT = COUNT(*)
	FROM
	C6MARTPERIODICO.MONITORAGGIO_INIZIALE
	WHERE 
    		RETE = @Rete AND
		    COD_FISCALE = @CodiceFiscale    
    -- Inserimento Dati Scirocco
    --SELECT @DettMovPrec = (sum(APPORTI) - sum(PRELIEVI))
	--FROM [C6StampeCentralizzate].[C6StagingPeriodico].[W6APTR]
	--WHERE DTTRIM < '20111231'
	SELECT @DettMovPrec = DETT_MOV_PREC, @DettMovCorr = DETT_MOV_TRIMESTRE_PREC 
	--modifica rleboffe 20130617
	--@DettMovCorr = DETT_MOV_CORR
	FROM C6MartPeriodico.DettaglioMovimenti
	WHERE
		RETE = @Rete
	AND
		COD_FIS = @CodiceFiscale
    --SELECT @DettMovCorr = (sum(APPORTI) - sum(PRELIEVI))
	--FROM [C6StampeCentralizzate].[C6StagingPeriodico].[W6APTR]
	--WHERE DTTRIM = '20111231'
	IF @DettMovPrec is null
		SET @AppoDettMovPrec = 'n.d.'
	ELSE
		SET @AppoDettMovPrec = REPLACE(CAST(@DettMovPrec as varchar(50)), '.', ',')
	IF @DettMovCorr is null
		SET @AppoDettMovCorr = 'n.d.'
	ELSE
		SET @AppoDettMovCorr = REPLACE(CAST(@DettMovCorr as varchar(50)), '.', ',')
	--query per asterischi situazione precedente
	IF EXISTS(SELECT * FROM [C6StampeCentralizzate].[C6StagingPeriodico].[WSEICS]
		WHERE CODPROD IN ('37', 'GP') AND DTTRIM = c6martperiodico.getprevioustrimestre(getdate(),0)
		AND RETE = @Rete AND CODFIS = @CodiceFiscale)
		SET @NumAsterischiPrec = 1
	IF EXISTS(SELECT * FROM C6StagingPeriodico.ST_MON
		WHERE RETE = @Rete AND CODFIS = @CodiceFiscale)
		SET @NumAsterischiPrec = 1
	-----------------------------------------
	--query per asterischi situazione corrente
	IF EXISTS(SELECT * FROM C6MartPeriodico.PATRIMONIO_BF
		WHERE COD_INTERNO IN ('37', 'GP')
		AND RETE = @Rete AND COD_FISCALE = @CodiceFiscale)
		SET @NumAsterischiCorr = 1
	-----------------------------------------
	SELECT @DATA_FOTO = DATA_FOTO FROM C6MartPeriodico.MONITORAGGIO_INIZIALE a
	WHERE a.Rete = @Rete
	AND a.COD_FISCALE = @CodiceFiscale
	SELECT @UltimoInvio = Count(*) FROM C6MartPeriodico.MONITORAGGIO_REPORT a
	WHERE a.Rete = @Rete
	AND a.COD_FISCALE = @CodiceFiscale
    IF (@MONIT_ATTIV > 0 AND @MONIT_INIT > 0)
    BEGIN
	-- Attivazione monitoraggio
--versione nuova da testare 
--select @SommaProventi= sum(IMPORTO) 
--from [C6StagingPeriodico].[WSEIPR]
--WHERE Rete = @Rete
--	AND codfis = @CodiceFiscale
SELECT @SommaProventi= sum(IMPORTO)
	FROM [C6MartPeriodico].proventi 
	WHERE 
    	  RETE = @Rete AND
		  COD_FISCALE = @CodiceFiscale 
	SELECT
		1 AS Fase,
		INI.DATA_FOTO AS DataFase,
		ISNULL(INI.PARTITA_VIAGGIANTE,0) AS Partita_viaggiante,
		INI.CTV_EXT,
		INI.CTV_INV,
		INI.CTV_PRE,
		INI.CTV_RIS,
		INI.CTV_LIQ,
		INI.CTV_EXT + INI.CTV_INV + INI.CTV_PRE + INI.CTV_RIS + INI.CTV_LIQ AS RisorseAllocate,
		INI.CTV_NA AS RisorseNonAllocate,
		INI.CTV_EXT + INI.CTV_INV + INI.CTV_PRE + INI.CTV_RIS + INI.CTV_LIQ + INI.CTV_NA AS RisorseFinanziarie,
		INI.CTV_CC AS Contocorrente,
--INIZIO INTERVENTI OMNIA		
		INI.ctvself AS ctvself,
		--Contocorrente = CASE WHEN CAST(INI.CTV_CC AS NUMERIC(20,4)) < 0 THEN INI.CTV_CC ELSE 0 END,
--		INI.CTV_EXT + INI.CTV_INV + INI.CTV_PRE + INI.CTV_RIS + INI.CTV_LIQ + INI.CTV_NA + INI.CTV_CC + ISNULL(INI.PARTITA_VIAGGIANTE,0) AS TotalePatrimonio		
		INI.CTV_EXT + INI.CTV_INV + INI.CTV_PRE + INI.CTV_RIS + INI.CTV_LIQ + INI.CTV_NA + INI.CTV_CC + ISNULL(INI.ctvself,0) + ISNULL(INI.PARTITA_VIAGGIANTE,0) AS TotalePatrimonio		
--FINE INTERVENTI OMNIA		
		--INI.CTV_EXT + INI.CTV_INV + INI.CTV_PRE + INI.CTV_RIS + INI.CTV_LIQ + INI.CTV_NA +	(CASE WHEN CAST(INI.CTV_CC AS NUMERIC(20,4)) < 0 THEN INI.CTV_CC ELSE 0 END) AS TotalePatrimonio
		--INI.CTV_TOTALE AS ControvaloreComplessivo
		,null as DettMov
		,0	as NumeroAsterischiNota
		,@SommaProventi as SommaProventi
	FROM
		C6MartPeriodico.MONITORAGGIO_INIZIALE INI
        left outer join
        C6MartPeriodico.MONITORAGGIO_REPORT rep
        on 
        REP.RETE=INI.RETE
        AND 
        REP.COD_FISCALE=INI.COD_FISCALE
	WHERE
		INI.RETE = @Rete AND
		INI.COD_FISCALE = @CodiceFiscale
       --AND INI.DATA_FOTO < ISNULL(REP.DATA_INVIO,'2999-12-31')
    UNION ALL
	-- Precedente invio report
 	SELECT
		2 AS Fase,
		rep.DATA_INVIO AS DataFase,
		ISNULL(rep.PARTITA_VIAGGIANTE,0) AS Partita_viaggiante,
		rep.CTV_EXT,
		rep.CTV_INV,
		rep.CTV_PRE,
		rep.CTV_RIS,
		rep.CTV_LIQ,
		rep.CTV_EXT + rep.CTV_INV + rep.CTV_PRE + rep.CTV_RIS + rep.CTV_LIQ AS RisorseAllocate,		
		rep.CTV_NA AS RisorseNonAllocate,
		rep.CTV_EXT + rep.CTV_INV + rep.CTV_PRE + rep.CTV_RIS + rep.CTV_LIQ + rep.CTV_NA AS RisorseFinanziarie,		
		rep.CTV_CC AS Contocorrente,
--INIZIO INTERVENTI OMNIA
		rep.CTV_SELF AS ctvself,
		--Contocorrente = CASE WHEN CAST(CTV_CC AS NUMERIC(20,4)) < 0 THEN CTV_CC ELSE 0 END,
--		rep.CTV_EXT + rep.CTV_INV + rep.CTV_PRE + rep.CTV_RIS + rep.CTV_LIQ + rep.CTV_NA + rep.CTV_CC + ISNULL(rep.PARTITA_VIAGGIANTE,0) AS TotalePatrimonio
		rep.CTV_EXT + rep.CTV_INV + rep.CTV_PRE + rep.CTV_RIS + rep.CTV_LIQ + rep.CTV_NA + rep.CTV_CC + ISNULL(rep.CTV_SELF,0) + ISNULL(rep.PARTITA_VIAGGIANTE,0) AS TotalePatrimonio
--FINE INTERVENTI OMNIA				
		--CTV_EXT + CTV_INV + CTV_PRE + CTV_RIS + CTV_LIQ + CTV_NA + (CASE WHEN CAST(CTV_CC AS NUMERIC(20,4)) < 0 THEN CTV_CC ELSE 0 END) AS TotalePatrimonio
		--CTV_TOTALE AS ControvaloreComplessivo
		,(CASE WHEN @DATA_FOTO < '20111001' THEN 'n.d.' ELSE @AppoDettMovPrec END) as DettMov
		--,(CASE WHEN @DATA_FOTO < '20111001' THEN 0 ELSE @NumAsterischiPrec END) as NumeroAsterischiNota
		,0 as NumeroAsterischiNota
		,@SommaProventi as SommaProventi
	FROM
		C6MartPeriodico.MONITORAGGIO_INIZIALE INI
        left outer join
        C6MartPeriodico.MONITORAGGIO_REPORT rep
        on 
        REP.RETE=INI.RETE
        AND 
        REP.COD_FISCALE=INI.COD_FISCALE
	WHERE
		rep.RETE = @Rete AND
		rep.COD_FISCALE = @CodiceFiscale
		AND INI.DATA_FOTO < ISNULL(REP.DATA_INVIO,cast('29991231' as datetime))
		and ISNULL(DATA_INVIO,cast('29991231' as datetime))<@datacongelamento
	UNION ALL
	-- Situazione corrente
	--All values will be read from DB and passed to code, no value will be calculated on code
	SELECT 
			3 AS Fase,
			CASE
				WHEN @DataCongelamento IS NULL THEN GETDATE()
				ELSE @DataCongelamento
			END AS DataFase,
			--Get the sum Operazioni in corso	
			--sum((isnull(IMPORTO,0))) as Partita_viaggiante,
			0 as Partita_viaggiante,
			sum(case Upper(id_area) when 'EXT' then ctv else 0 end ) as CTV_EXT,
			sum(case Upper(id_area) when 'INV' then ctv else 0 end ) as CTV_INV,
			sum(case when Upper(id_area) like 'PRE%' then ctv else 0 end ) as CTV_PRE,
			--sum(case Upper(id_area) when 'PRE' then ctv else 0 end ) as CTV_PRE,
			sum(case Upper(id_area) when 'RIS' then ctv else 0 end ) as CTV_RIS,			
			sum(case Upper(id_area) when 'LIQ' then ctv else 0 end ) as CTV_LIQ,
			sum(case Upper(id_area) when 'EXT' then ctv else 0 end )+ 
			sum(case Upper(id_area) when 'INV' then ctv else 0 end )+ 
			sum(case when Upper(id_area) like 'PRE%' then ctv else 0 end ) +
			sum(case Upper(id_area) when 'RIS' then ctv else 0 end )+
			sum(case Upper(id_area) when 'LIQ' then ctv else 0 end ) AS CTV_RA,
			sum(case Upper(id_area) when 'NA' then ctv else 0 end ) as CTV_NA,
			sum(case Upper(id_area) when 'EXT' then ctv else 0 end )+ 
			sum(case Upper(id_area) when 'INV' then ctv else 0 end )+ 
			--sum(case Upper(id_area) when 'PRE' then ctv else 0 end )+ 
			sum(case when Upper(id_area) like 'PRE%' then ctv else 0 end ) +
			sum(case Upper(id_area) when 'RIS' then ctv else 0 end )+
			sum(case Upper(id_area) when 'LIQ' then ctv else 0 end )+ 
			sum(case Upper(id_area) when 'NA' then ctv else 0 end )	AS CTV_RF,				
			--CTV_CC = (Case WHEN sum(case Upper(id_area) when 'CC' then ctv  else 0 end ) < 0 THEN sum(case Upper(id_area) when 'CC' then ctv  else 0 end) ELSE 0 END),
			--sum(Case When (Upper(id_area) = 'CC' and cast(ctv As Numeric)<0) Then ctv else 0 end) AS CTV_CC,
			sum(Case Upper(id_area) when 'CC' then ctv else 0 end) AS CTV_CC,			
--INIZIO INTERVENTI OMNIA
			sum(Case Upper(id_area) when 'self' then ctv else 0 end) AS ctvself,			
--FINE INTERVENTI OMNIA
			sum(case Upper(id_area) when 'EXT' then ctv else 0 end )+ 
			sum(case Upper(id_area) when 'INV' then ctv else 0 end )+ 
			--sum(case Upper(id_area) when 'PRE' then ctv else 0 end )+ 
			sum(case when Upper(id_area) like 'PRE%' then ctv else 0 end )+
			sum(case Upper(id_area) when 'RIS' then ctv else 0 end )+
			sum(case Upper(id_area) when 'LIQ' then ctv else 0 end )+ 
			sum(case Upper(id_area) when 'NA' then ctv else 0 end )	+
--INIZIO INTERVENTI OMNIA
--			sum(Case Upper(id_area) when 'CC' then ctv else 0 end) AS TotalePatrimonio
			sum(Case Upper(id_area) when 'CC' then ctv else 0 end) +
			sum(Case Upper(id_area) when 'self' then ctv else 0 end) AS TotalePatrimonio
--FINE INTERVENTI OMNIA
			--sum((isnull(IMPORTO,0))) AS TotalePatrimonio
			--sum(Case When (Upper(id_area) = 'CC' and cast(ctv As Numeric)<0) Then ctv else 0 end) AS TotalePatrimonio			
			--SUM(CTV) as ControvaloreComplessivo
			--,(CASE WHEN @UltimoInvio = 0 AND @DATA_FOTO < '20111001' THEN 'n.d.' ELSE @AppoDettMovCorr END) as DettMov			
			,(CASE WHEN @UltimoInvio = 0 AND @DATA_FOTO < '20111001' THEN 'n.d.' ELSE @AppoDettMovCorr END) as DettMov
			--,(CASE WHEN @UltimoInvio = 0 AND @DATA_FOTO < '20111001' THEN 0 ELSE @NumAsterischiCorr END) as NumeroAsterischiNota
			,0 as NumeroAsterischiNota
			,@SommaProventi as SommaProventi
			--,'1' as NumeroAsterischiNota
	FROM 
		C6MartPeriodico.PATRIMONIO_BF 
--		AS BF Left Join C6MartPeriodico.partite_viaggianti AS PV
--		ON 	BF.RETE = PV.RETE
--		AND BF.COD_FISCALE = PV.COD_FISCALE
	WHERE	
		RETE = @Rete AND
		COD_FISCALE = @CodiceFiscale
END
ELSE
BEGIN
	SELECT
		1 AS Fase,
		DATA_FOTO AS DataFase,
		ISNULL(PARTITA_VIAGGIANTE,0) AS Partita_viaggiante,
		CTV_EXT,
		CTV_INV,
		CTV_PRE,
		CTV_RIS,
		CTV_LIQ,
		CTV_EXT + CTV_INV + CTV_PRE + CTV_RIS + CTV_LIQ AS RisorseAllocate,		
		CTV_NA AS RisorseNonAllocate,
		CTV_EXT + CTV_INV + CTV_PRE + CTV_RIS + CTV_LIQ + CTV_NA AS RisorseFinanziarie,		
		CTV_CC AS Contocorrente,
--INIZIO INTERVENTI OMNIA
		CTVself AS ctvself,
--		CTV_EXT + CTV_INV + CTV_PRE + CTV_RIS + CTV_LIQ + CTV_NA + CTV_CC AS TotalePatrimonio			
		CTV_EXT + CTV_INV + CTV_PRE + CTV_RIS + CTV_LIQ + CTV_NA + CTV_CC + ctvself AS TotalePatrimonio			
--FINE INTERVENTI OMNIA
		--CTV_TOTALE AS ControvaloreComplessivo
	FROM
		MONITORAGGIO_INIZIALE
	WHERE
		1=2
END
END