-- Schema: C6Mart
-- Stored Procedure: PL_FD182BISDettaglioProdottiESG






								
																		  
CREATE procedure [C6Mart].[PL_FD182BISDettaglioProdottiESG] --'S','SNTCHR80A61D612V'
 -- 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 
	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 PATRBF.NOME_PROGETTO
	END AS nome_progetto,
											   
	SUM(PATRBF.CTV) OVER (PARTITION BY AREA.ID_AREA) AS somma_controval_needarea,
0 as ORDINAMENTO_PROGETTO,
	SUM(PATRBF.VERSATO_NETTO) OVER (PARTITION BY AREA.ID_AREA) 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 AREA.ID_AREA) 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 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, 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' AND COUNT(CASE WHEN (PATRBF.CTV - 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 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
--	
