-- Schema: dbo
-- Stored Procedure: TEST_MERGED_PROCEDURE


CREATE procedure [dbo].[TEST_MERGED_PROCEDURE]
@Rete char(1), 
@CodiceFiscale varchar(16)

AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

----------------------------------------------- C6MartPeriodico.PL_S43LineaSelfBF
SELECT  '' as 'C6MartPeriodico.PL_S43LineaSelfBF', @Rete as i_rete ,@CodiceFiscale as i_codiceFiscale,	 	 
		 codconf as NumContratto,
		 isnull(ctv,0) as Controvalore,
--INIZIO Intervento Eligo Titoli		 
		 NomeProdotto as NomeProdotto,
--FINE   Intervento Eligo Titoli		 		 
		 dbo.ToShortDateString(cast(datasott as varchar))  as DataSott
	FROM       
		C6MartPeriodico.ANAG_SELF LS	
	WHERE 
		LS.rete= @Rete
		AND LS.codfis= @CodiceFiscale
		AND isnull(ctv,0) < 0
--------------------------------------------------------------------

----------------------------------------------- C6MartPeriodico.PL_D_S333Fonditalia80
Select  
	'' as 'C6MartPeriodico.PL_D_S333Fonditalia80', @Rete as i_rete ,@CodiceFiscale as i_codiceFiscale
	,[DES_PROD]
	,[COD_CONF]
	,[QUOTA]
	,[NUMEROQUOTE]
	,[CONTROVALORE]
	,[DATARIF]
	,[DES_MAX]
	,[QUOTA_MAX]
	,[QUOTA_MAX_PERC]
	,[DATARIF_MAX]
	,[DES_PROT]
	,[QUOTA_PROT]
	,[NUMEROQUOTE_PROT]
	,[CONTROVALORE_PROT]
from [C6martperiodico].[capprot]
 WHERE	[Rete] 		= @Rete 
  AND	[Codfis] = @CodiceFiscale
--------------------------------------------------------------------

----------------------------------------------- C6MartPeriodico.PL_S10PatrimonioFinanziario
SELECT '' as 'C6MartPeriodico.PL_S10PatrimonioFinanziario' , @Rete as i_rete ,@CodiceFiscale as i_codiceFiscale,
	A.BANCA,
	A.ORDINAMENTO,
	A.AssetClassId, 
	A.AssetClassName, 
	cast(A.Controvalore as decimal(19,5)) Controvalore,
	cast(A.Percentuale  as decimal(19,5)) Percentuale,
	cast(ROUND(SUM(A.Controvalore) OVER (PARTITION BY 'TOTALE'),2)  as decimal(19,5))  AS Totale
FROM 
(		
	SELECT DISTINCT
		CASE @Rete
			WHEN 'S' THEN 'Sanpaolo Invest'
			ELSE 'Banca Fideuram' 
		END AS Banca,
		ASSETCLASS.ORDINAMENTO,
		VALORIASSET.ID_ASSETCLASS AS AssetClassId, 
		ASSETCLASS.DESCRIZIONE AS AssetClassName, 
		ROUND(SUM(VALORIASSET.CONTROVALORE) OVER (PARTITION BY VALORIASSET.ID_ASSETCLASS),2) as Controvalore,
		(
			SUM(VALORIASSET.CONTROVALORE) OVER (PARTITION BY VALORIASSET.ID_ASSETCLASS) /
			SUM(VALORIASSET.CONTROVALORE) OVER (PARTITION BY 'TOTALE') 
		) * 100 AS Percentuale
	FROM (

		SELECT 	
			ASSETPERC.ID_ASSETCLASS,
			PATRBF.CTV*ASSETPERC.PERC as Controvalore
		FROM C6MART.PATRIMONIO_BF PATRBF
		INNER JOIN C6MART.ASSET_PERC ASSETPERC 
			ON PATRBF.COD_PRODOTTO = ASSETPERC.COD_PRODOTTO
				AND ASSETPERC.LIVELLO = 1
		WHERE 
			--ELIMINO I CONTRATTI ASUL PERCHE' UTILIZZIAMO IL DETTGALIO CHE PRENDIAMO CON LA SECONDA QUERY
			(PATRBF.TIPO_PRODOTTO NOT IN ('ASUL'))--,'CC'))
			and patrbf.id_area <> 'CC'
			AND PATRBF.RETE = @Rete
			AND PATRBF.COD_FISCALE = @CodiceFiscale

		UNION ALL
		
		-- MODIFICA PER AVERE ASSET PERCENTUALI SUI CC
--		SELECT 	ASSETPERC.ID_ASSETCLASS,
--				PATRBF.CTV*ASSETPERC.PERC as Controvalore
--		FROM C6MART.PATRIMONIO_BF PATRBF
--		INNER JOIN C6MART.ASSET_PERC ASSETPERC 
--			ON PATRBF.COD_SOTTOPRODOTTO = ASSETPERC.COD_SOTTOPRODOTTO
--			AND PATRBF.COD_INTERNO = ASSETPERC.COD_INTERNO 
--			AND ASSETPERC.LIVELLO = 1
----		LEFT OUTER JOIN C6Mart.ClientiConCCNegativo CCNegativo
----			ON PATRBF.RETE = CCNegativo.Rete
----			AND PATRBF.COD_FISCALE = CCNegativo.Cod_Fiscale
--		WHERE 
--			--ELIMINO I CC CON SALDO NEGATIVO
--			--PATRBF.TIPO_PRODOTTO = 'CC'
--			PATRBF.ID_AREA = 'CC'
--			AND PATRBF.RETE = @Rete
--			AND PATRBF.COD_FISCALE = @CodiceFiscale
--			--AND CCNegativo.Cod_Fiscale IS NULL

--		UNION ALL

		SELECT ASSETPERC.ID_ASSETCLASS,
				ASUL.CTV*ASSETPERC.PERC as Controvalore
		FROM C6MART.DETTAGLIO_ASUL ASUL
		INNER JOIN C6MART.ASSET_PERC ASSETPERC
			ON ASS
