-- Schema: C6Mart
-- Stored Procedure: PL_D2_FD160DettaglioProdottiBF














create     procedure [C6Mart].[PL_D2_FD160DettaglioProdottiBF] --'F','CHRMNL86P18H657Z'
 -- 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 C6MART.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 C6MART.PATRIMONIO_BF
	WHERE PATRIMONIO_BF.RETE = @Rete
		AND PATRIMONIO_BF.COD_FISCALE = @CodiceFiscale
		AND ID_AREA = 'LIQ'
--FINE INTERVENTI OMNIA QUIII
--------------------------------------------------------------------------------------

SELECT 
PATRBF.NOME_PROGETTO AS EE, PATRBF.ID_AREA,
CASE 
					WHEN (PATRBF.NOME_PROGETTO IS NOT NULL)
						THEN 'RISFIN|PIRAMIDE|' + LEFT(PATRBF.ID_AREA,3) + '|' + PATRBF.NOME_PROGETTO+ '|'
					when LEFT(PATRBF.ID_AREA,3)='Na' then 'RISFIN|NA|'
					ELSE 'RISFIN|PIRAMIDE|' + LEFT(PATRBF.ID_AREA,3) + '|' 
				END
--		END + PATRBF.POSITION_ID 
		+ PATRBF.POSITION_ID AS TMP,
	CASE 
		when robo.isProgettoRobo='S' then 'Rob'
		else 'ASF'
	END AS needarea,
	CASE 
		when robo.isProgettoRobo='S' then 'Portafoglio Direct Evo'
		else 'Portafoglio Base'
		--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 
		WHEN patrbf.tipo_prodotto='CC' THEN 10 -- lo setto ad un valore alto in modo che si trova in basso come ordinamento
		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 PATRBF.NOME_PROGETTO
	END AS nome_progetto,
	SUM(PATRBF.CTV) OVER (PARTITION BY robo.isProgettoRobo) AS somma_controval_needarea,
	SUM(PATRBF.VERSATO_NETTO) OVER (PARTITION BY robo.isProgettoRobo) AS versato_needarea,
--	CASE WHEN SUM(PATRBF.VERSATO_NETTO) OVER (PARTITION BY AREA.ID_AREA) IS NULL THEN 'n.d.' ELSE NULL END as versato_needareaString,
	CASE WHEN COUNT(CASE WHEN PATRBF.VERSATO_NETTO IS NULL THEN 1 ELSE NULL END) OVER (PARTITION BY AREA.ID_AREA) > 0 THEN 'n.d.' ELSE NULL END as versato_needareaString,
	SUM(PATRBF.CTV - PATRBF.VERSATO_NETTO) OVER (PARTITION BY robo.isProgettoRobo) AS MinusPlusValenza_needarea,
--	CASE WHEN SUM(PATRBF.CTV - PATRBF.VERSATO_NETTO) OVER (PARTITION BY AREA.ID_AREA) IS NULL THEN 'n.d.' ELSE NULL END as MinusPlusValenza_needareaString,
	CASE WHEN COUNT(CASE WHEN (PATRBF.CTV - PATRBF.VERSATO_NETTO) IS NULL THEN 1 ELSE NULL END) OVER (PARTITION BY robo.isProgettoRobo) > 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, NOME_PROGETTO) 
		ELSE NULL	
	END	AS somma_controval_need_prog,
	CASE 
		WHEN AREA.ID_AREA = 'Inv' THEN SUM(PATRBF.VERSATO_NETTO) OVER (PARTITION BY AREA.ID_AREA, NOME_PROGETTO) 
		ELSE NULL
	END	AS versato_need_prog,
	CASE 
--	WHEN AREA.ID_AREA = 'Inv' AND SUM(PATRBF.VERSATO_NETTO) OVER (PARTITION BY AREA.ID_AREA, NOME_PROGETTO) IS NULL
	WHEN AREA.ID_AREA = 'Inv' AND COUNT(CASE WHEN PATRBF.VERSATO_NETTO IS NULL THEN 1 ELSE NULL END) OVER (PARTITION BY AREA.ID_AREA, NOME_PROGETTO) > 0
		THEN 'n.d.'
		ELSE NULL
	END	AS versato_need_progString,
	CASE 
	WHEN AREA.ID_AREA = 'Inv' 
		THEN SUM(PATRBF.CTV - PATRBF.VERSATO_NETTO) OVER (PARTITION BY AREA.ID_AREA, NOME_PROGETTO) 
		ELSE NULL
	END	AS MinusPlusValenza_need_prog,
	CASE 
--	WHEN AREA.ID_AREA = 'Inv' AND SUM(PATRBF.CTV - PATRBF.VERSATO_NETTO) OVER (PARTITION BY AREA.ID_AREA, NOME_PROGETTO) IS NULL
	WHEN AREA.ID_AREA = 'Inv'
