--[C6MartPeriodico].[PL_S172ProtezioneCapitaleLineeProdotti] 's','VCCGRL39E57E356Q'
CREATE procedure [C6MartPeriodico].[PL_S172ProtezioneCapitaleLineeProdotti] 
	-- Add the parameters for the stored procedure here
	-- Add the parameters for the stored procedure here
	@Rete char(1), 
	@CodiceFiscale varchar(16)
AS
declare @isinInterfund80 varchar(20)
set @isinInterfund80 = 'LU1106988527'
BEGIN
---------------------- LINEA PROTETTA (RF/MP)
select   Linea
		,Contratto
        ,VersatoNetto
        ,Controvalore
        ,ComponentePerformance
        ,ComponenteProtezione
        ,ControvaloreProtetto
        ,DataRiferimento
        ,PercentualePerformance
        ,PercentualeProtezione
		,ordine
	from
		(SELECT
		a.des_prod_tit as Linea
		,a.cod_conf as	  Contratto
        ,0 as      VersatoNetto
        ,a.controvalore_riferimento as      Controvalore
        ,0 as      ComponentePerformance
        ,a.componente_protezione as      ComponenteProtezione
        ,a.controvalore_prot as      ControvaloreProtetto
        ,convert(varchar(4), year(datarif)) + convert(varchar(2), month(datarif)) + convert(varchar(2), day(datarif)) as      DataRiferimento
        ,0 as      PercentualePerformance
        ,0 as      PercentualeProtezione
		,1 as ordine
		from [C6MartPeriodico].[CAPPROT_FVI] a
		where SUBPROD in ('MP')
		and CODFIS = @CodiceFiscale
		and rete = @Rete
union
		---------------------- MY BLUE/MY WHITE
		SELECT
		a.des_prod_tit as Linea
		,a.cod_conf as	  Contratto
        ,0 as      VersatoNetto
        ,a.controvalore_riferimento as      Controvalore
        ,0 as      ComponentePerformance
        ,a.componente_protezione as      ComponenteProtezione
        ,a.controvalore_prot as      ControvaloreProtetto
        ,convert(varchar(4), year(datarif)) + convert(varchar(2), month(datarif)) + convert(varchar(2), day(datarif)) as      DataRiferimento
        ,0 as      PercentualePerformance
        ,0 as      PercentualeProtezione
		,2 as ordine
		from [C6MartPeriodico].[CAPPROT_FVI] a
		where SUBPROD in ('CS80', 'CT80')
		and CODFIS = @CodiceFiscale
		and rete = @Rete
union
-- INTERFUND 80
	SELECT
		a.des_prod_tit as Linea
		,a.Cod_Conf as	  Contratto
        ,0 as      VersatoNetto
        ,a.Controvalore as      Controvalore
        ,0 as      ComponentePerformance
        ,0 as      ComponenteProtezione
        ,a.Controvalore_prot as      ControvaloreProtetto
        --,datarif as      DataRiferimento
		,convert(varchar(4), year(datarif)) + convert(varchar(2), month(datarif)) + convert(varchar(2), day(datarif)) as      DataRiferimento
        ,0 as      PercentualePerformance
        ,0 as      PercentualeProtezione
		,3 as ordine
	--into #tInterfund80
	FROM C6MartPeriodico.capprot a
	WHERE 1=1
	AND a.RETE = @Rete 
	AND a.CODFIS = @CodiceFiscale
	--and dttrim
	union
		---------------------- FONDITALIA 80
		SELECT
		a.desprodtit as Linea
		,a.codconf as	  Contratto
        ,0 as      VersatoNetto
        ,a.ctv as      Controvalore
        ,0 as      ComponentePerformance
        ,a.prezzoprot as      ComponenteProtezione
        ,a.ctrvimpprot as      ControvaloreProtetto
        ,dtrife as      DataRiferimento
        ,0 as      PercentualePerformance
        ,0 as      PercentualeProtezione
	    ,4 as ordine
	FROM c6martperiodico.foi80 a
	WHERE 1=1
	AND a.RETE = @Rete 
	AND a.CODFIS = @CodiceFiscale) as int
	 order by ordine asc,controvalore desc
END