-- Schema: C6Mart
-- Stored Procedure: PL_D_S160DettaglioProdottiBF



-- =============================================
-- Author:		Gentilezza Marco
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
-- cast(C6Mart.ANAG_CC.SALDO as decimal(15,2)) as Controvalore
-- [C6Mart].[PL_S43DettaglioProdottiBF] 'F', 'GFFSLL50T44F205Z'
--SELECT * FROM PATR1

CREATE procedure [C6Mart].[PL_D_S160DettaglioProdottiBF]
 -- Add the parameters for the stored procedure here
 @Rete char(1), 
 @CodiceFiscale varchar(16)

AS
BEGIN

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 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,
	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
		ELSE NULL
	END	AS somma_copertura_need_prog,
	cast(RISCHIO_AGGR_AREA.VAR_PERC_PTF as decimal(15,2)) AS var_needarea,
	--COALESCE(cast(RISCHIO_AGGR_AREA.VAR_PERC_PTF as decimal(15,2)),'n.c.
