-- Schema: C6MartPeriodico
-- Stored Procedure: PL_S43BISDettaglioProdottiBF_20160118





--Aggiunta GESTIONE EligoTitoli L192 (FCianfa, 26/11/2014)

-- [C6MartPeriodico].[PL_S43BISDettaglioProdottiBF] 'S', 'CLCLNE41M47H434D'
CREATE PROCEDURE [C6MartPeriodico].[PL_S43BISDettaglioProdottiBF_20160118]
 -- Add the parameters for the stored procedure here
 @Rete char(1), 
 @CodiceFiscale varchar(16)

AS
BEGIN


-------------------------------------------------------------------------------------
--INIZIO INTERVENTI OMNIA QUIII
	DECLARE @TOTSELFLIQ AS INT
	SELECT @TOTSELFLIQ = COUNT(*)
	FROM C6MARTPERIODICO.PATRIMONIO_BF
	WHERE PATRIMONIO_BF.RETE = @Rete
		AND PATRIMONIO_BF.COD_FISCALE = @CodiceFiscale
		AND ISIN in('EURO00000009', 'EURO10000007')
		AND ID_AREA = 'LIQ'

	DECLARE @TOTLIQ AS INT
	SELECT @TOTLIQ = COUNT(*)
	FROM C6MARTPERIODICO.PATRIMONIO_BF
	WHERE PATRIMONIO_BF.RETE = @Rete
		AND PATRIMONIO_BF.COD_FISCALE = @CodiceFiscale
		AND ID_AREA = 'LIQ'
--FINE INTERVENTI OMNIA QUIII
--------------------------------------------------------------------------------------


SELECT

CASE 
	WHEN AREA.ID_AREA = 'Pre1' OR AREA.ID_AREA = 'Pre2' THEN 'Pre' 
	ELSE AREA.ID_AREA 
END AS needarea,
CASE 
	WHEN AREA.ID_AREA = 'Pre1' OR AREA.ID_AREA = 'Pre2' THEN 'Previdenza'
	WHEN AREA.ID_AREA = 'Na'  THEN 'Non allocate'
	ELSE AREA.NOME_AREA 
END AS needareades,
CASE 
	WHEN AREA.ID_AREA = 'Pre1' OR AREA.ID_AREA = 'Pre2' THEN  3 
	ELSE AREA.ORDINAMENTO 
END AS areasortorder,
CASE 
	WHEN AREA.ID_AREA = 'Inv' AND PATRBF.NOME_PROGETTO IS NULL THEN 'Prodotti non associati a progetti'
	ELSE ISNULL(PATRBF.NOME_PROGETTO,'')
END AS nome_progetto,
PATRBF.ORDINAMENTO_PROGETTO,


--Bido
SUM(PATRBF.CTV ) OVER (PARTITION BY AREA.ID_AREA) AS somma_controval_needarea,


SUM(PATRBF.CTV - MONIT.UTILE_PERDITA_DA_ASSOC) OVER (PARTITION BY AREA.ID_AREA) AS versato_needarea,
CASE 
	WHEN COUNT(CASE WHEN (PATRBF.CTV - MONIT.UTILE_PERDITA_DA_ASSOC) IS NULL THEN 1 ELSE NULL END) OVER (PARTITION BY AREA.ID_AREA) > 0 THEN 'n.d.' 
	ELSE NULL 
END as versato_needareaString,
SUM(MONIT.UTILE_PERDITA_DA_ASSOC) OVER (PARTITION BY AREA.ID_AREA) AS MinusPlusValenza_needarea,
CASE 
	WHEN COUNT( CASE 
					WHEN MONIT.UTILE_PERDITA_DA_ASSOC IS NULL THEN 1 
					ELSE NULL 
				END ) OVER (PARTITION BY AREA.ID_AREA) > 0 THEN 'n.d.' 
	ELSE NULL 
END AS MinusPlusValenza_needareaString,
CASE 
	WHEN AREA.ID_AREA = 'Inv' THEN SUM(PATRBF.CTV) OVER (PARTITION BY AREA.ID_AREA, PATRBF.NOME_PROGETTO, PATRBF.ordinamento_progetto) 
	ELSE NULL	
END	AS somma_controval_need_prog,
CASE 
	WHEN AREA.ID_AREA = 'Inv' THEN SUM(PATRBF.CTV - MONIT.UTILE_PERDITA_DA_ASSOC) OVER (PARTITION BY AREA.ID_AREA, PATRBF.NOME_PROGETTO, PATRBF.ordinamento_progetto) 
	ELSE NULL
END	AS versato_need_prog,
CASE 
	WHEN AREA.ID_AREA = 'Inv' AND COUNT(CASE WHEN (PATRBF.CTV - MONIT.UTILE_PERDITA_DA_ASSOC) IS NULL THEN 1 ELSE NULL END) OVER (PARTITION BY AREA.ID_AREA, PATRBF.NOME_PROGETTO, PATRBF.ordinamento_progetto) > 0 
		THEN 'n.d.'
	ELSE NULL
END	AS versato_need_progString,
CASE 
	WHEN AREA.ID_AREA = 'Inv' THEN SUM(MONIT.UTILE_PERDITA_DA_ASSOC) OVER (PARTITION BY AREA.ID_AREA, PATRBF.NOME_PROGETTO, PATRBF.ordinamento_progetto) 
	ELSE NULL
END	AS MinusPlusValenza_need_prog,
CASE 
	WHEN AREA.ID_AREA = 'Inv' AND COUNT(CASE WHEN MONIT.UTILE_PERDITA_DA_ASSOC IS NULL THEN 1 ELSE NULL END) OVER (PARTITION BY AREA.ID_AREA, PATRBF.NOME_PROGETTO, PATRBF.ordinamento_progetto) > 0	
		THEN 'n.d.'
	ELSE NULL
END	AS MinusPlusValenza_need_progString,
CASE 
	WHEN AREA.ID_AREA = 'Inv' THEN RISCHIO_AGGR_PROG.VAR_PERC_PTF
	ELSE NULL
END	AS somma_var_need_prog,
CASE 
	WHEN AREA.ID_AREA = 'Inv' THEN RISCHIO_AGGR_PROG.COPERTURA
	ELSE NULL
END	AS somma_copertura_need_prog,
RISCHIO_AGGR_AREA.VAR_PERC_PTF AS var_needarea,
CASE 
	WHEN ISNULL(SUM(RISCHIO_AGGR_AREA.COPERTURA) OVER (PARTITION BY AREA.ID_AREA),0.00) = 0.00 
		THEN 'n.c.' 
--------------------------------------------------------------
--INIZIO INTERVENTI OMNIA Q
