-- Schema: C6MartPeriodico
-- Stored Procedure: PL_D_S160DettaglioProdottiBF


-- Stored procedure

-- =============================================
-- Author:		Paolo Giovanetti
-- Create date: 17 febbraio 2009
-- Description: 
-- =============================================
--select * from c6martperiodico.patrimonio_bf where cod_fiscale = 'SLLLMN44E29A176G'
-- [C6MartPeriodico].[PL_DT_S160DettaglioProdottiBF] 'S', 'GSTMGS42D51L833Z'
CREATE procedure [C6MartPeriodico].[PL_D_S160DettaglioProdottiBF]
 -- Add the parameters for the stored procedure here
 @Rete char(1), 
 @CodiceFiscale varchar(16)

AS
BEGIN

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 ltrim(rtrim(PATRBF.NOME_PROGETTO))
END AS nome_progetto,
PATRBF.CHIAVE_PROGETTO as ORDINAMENTO_PROGETTO,
SUM(PATRBF.CTV) OVER (PARTITION BY AREA.ID_AREA) AS somma_controval_needarea,
SUM(PATRBF.CTV - MONIT.MINUS_PLUSVALENZA) OVER (PARTITION BY AREA.ID_AREA) AS versato_needarea,
CASE 
	WHEN COUNT(CASE WHEN (PATRBF.CTV - MONIT.MINUS_PLUSVALENZA) 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.MINUS_PLUSVALENZA) OVER (PARTITION BY AREA.ID_AREA) AS MinusPlusValenza_needarea,
CASE 
	WHEN COUNT( CASE 
					WHEN MONIT.MINUS_PLUSVALENZA 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.CHIAVE_progetto) 
	ELSE NULL	
END	AS somma_controval_need_prog,
CASE 
	WHEN AREA.ID_AREA = 'Inv' THEN SUM(PATRBF.CTV - MONIT.MINUS_PLUSVALENZA) OVER (PARTITION BY AREA.ID_AREA, PATRBF.NOME_PROGETTO, PATRBF.CHIAVE_progetto) 
	ELSE NULL
END	AS versato_need_prog,
CASE 
	WHEN AREA.ID_AREA = 'Inv' AND COUNT(CASE WHEN (PATRBF.CTV - MONIT.MINUS_PLUSVALENZA) IS NULL THEN 1 ELSE NULL END) OVER (PARTITION BY AREA.ID_AREA, PATRBF.NOME_PROGETTO, PATRBF.CHIAVE_progetto) > 0 
		THEN 'n.d.'
	ELSE NULL
END	AS versato_need_progString,
CASE 
	WHEN AREA.ID_AREA = 'Inv' THEN SUM(MONIT.MINUS_PLUSVALENZA) OVER (PARTITION BY AREA.ID_AREA, PATRBF.NOME_PROGETTO, PATRBF.CHIAVE_progetto) 
	ELSE NULL
END	AS MinusPlusValenza_need_prog,
CASE 
	WHEN AREA.ID_AREA = 'Inv' AND COUNT(CASE WHEN MONIT.MINUS_PLUSVALENZA IS NULL THEN 1 ELSE NULL END) OVER (PARTITION BY AREA.ID_AREA, PATRBF.NOME_PROGETTO, PATRBF.CHIAVE_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.' 
	ELSE NULL 
END AS var_needareaString,
CAST(ISNULL(RISCHIO_AGGR_AREA.COPERTURA,100) AS DECIMAL (5,2)) AS copertura_needarea,
	CASE 
	WHEN (ISNULL(RISCHIO_AGGR_AREA.copertura,100) < 100) 
		--THEN '(*) Grado di copertura: ' + REPLACE(CAST(CAST(RISCHIO_AGGR_AREA.COPERTURA AS DECIMAL (5,2)) AS VARCHAR),'.',',') + '%'  
		THEN 'Grado di copertura: ' + REPLACE(CAST(CAST(RISCHIO_AGGR_AREA.COPERTURA AS DECIMAL (5,2)) AS VARCHAR),'.',',') + '%'  
	ELSE '' 
END AS copertura_needareaString,
RISCHIO_AGGR_PROG.VAR_PERC_PTF AS var_need_prog,
CASE 
	WHEN ISNULL(RISCHIO_AGGR_PROG.COPERTURA,0.00) = 0.00 THEN 'n.c.' 
	ELSE NULL 
E
