﻿-- Stored procedure
-- =============================================
-- Author:		<Luca De Lisio,,[PL_DT_S159PatrimonioFinanziario]>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
--[C6Mart].[PL_D2_S159PatrimonioFinanziario] 'F','02477630582'
 CREATE procedure [C6Mart].[PL_D2_S159PatrimonioFinanziario]
	-- Add the parameters for the stored procedure here
	@Rete char(1), 
	@CodiceFiscale varchar(16)
AS
BEGIN
SELECT 
	A.BANCA AS Banca,
	A.ORDINAMENTO AS Ordinamento,
	A.AssetClassId AS AssetClassId, 
	A.AssetClassName AS AssetClassName, 
	cast(A.Controvalore as decimal(19,5)) AS Controvalore,
	cast(A.Percentuale  as decimal(19,5)) AS 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' 
			WHEN 'F' THEN  'Fideuram' 
			ELSE 'IW Private Investments'
		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) /
            --16.07.2011 COLAIANNI CONSOLI GESTIONE DIVISIONE PER ZERO
			CASE WHEN ( SUM(VALORIASSET.CONTROVALORE) OVER (PARTITION BY 'TOTALE') ) = 0 
				 THEN 1 
                 ELSE ( SUM(VALORIASSET.CONTROVALORE) OVER (PARTITION BY 'TOTALE') ) 
            END  
		) * 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.id_area not in ('CC','Self')
			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'
--			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 
				ASSETPERC.COD_PRODOTTO = ASUL.COD_PRODOTTO
				---ELIMINO GLI SKANDIA
				AND ASUL.COD_ISIN_SOTT = ''
				AND ASSETPERC.LIVELLO = 1
		WHERE
			ASUL.RETE = @Rete
			AND ASUL.COD_FISCALE = @CodiceFiscale
		UNION ALL
		SELECT ASSETPERC.ID_ASSETCLASS,
				ASUL.CTV*ASSETPERC.PERC as Controvalore
		FROM C6MART.DETTAGLIO_ASUL ASUL
		INNER JOIN C6MART.ASSET_PERC ASSETPERC
			--PRENDO SOLO GLI SKANDIA
			ON ASSETPERC.COD_ISIN = ASUL.COD_ISIN_SOTT
			AND ASSETPERC.COD_MAF = ASUL.COD_MAF_SOTT
			AND ASSETPERC.COD_INTERNO = ASUL.COD_INTERNO
            AND rtrim(ASSETPERC.COD_SOTTOPRODOTTO) = ASUL.COD_SOTTOPRODOTTO
			AND ASSETPERC.LIVELLO = 1
		WHERE
			ASUL.RETE = @Rete
			AND ASUL.COD_FISCALE = @CodiceFiscale
	) VALORIASSET
	INNER JOIN 
		C6MART.ASSETCLASS ASSETCLASS
		ON 
			ASSETCLASS.ID_ASSETCLASS = VALORIASSET.ID_ASSETCLASS
) A
ORDER BY A.ORDINAMENTO
END
--USE [C6StampeCentralizzate]
--GO
--/****** Object:  StoredProcedure [C6Mart].[PL_D2_S159PatrimonioFinanziario]    Script Date: 08/02/2022 16:37:35 ******/
--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO
---- Stored procedure
---- =============================================
---- Author:		<Luca De Lisio,,[PL_DT_S159PatrimonioFinanziario]>
---- Create date: <Create Date,,>
---- Description:	<Description,,>
---- =============================================
----[C6Mart].[PL_D2_S159PatrimonioFinanziario] 'F','02477630582'
-- ALTER procedure [C6Mart].[PL_D2_S159PatrimonioFinanziario]
--	-- Add the parameters for the stored procedure here
--	@Rete char(1), 
--	@CodiceFiscale varchar(16)
--AS
--BEGIN
--SELECT 
--	A.BANCA AS Banca,
--	A.ORDINAMENTO AS Ordinamento,
--	A.AssetClassId AS AssetClassId, 
--	A.AssetClassName AS AssetClassName, 
--	cast(A.Controvalore as decimal(19,5)) AS Controvalore,
--	cast(A.Percentuale  as decimal(19,5)) AS 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' 
--			ELSE '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) /
--            --16.07.2011 COLAIANNI CONSOLI GESTIONE DIVISIONE PER ZERO
--			CASE WHEN ( SUM(VALORIASSET.CONTROVALORE) OVER (PARTITION BY 'TOTALE') ) = 0 
--				 THEN 1 
--                 ELSE ( SUM(VALORIASSET.CONTROVALORE) OVER (PARTITION BY 'TOTALE') ) 
--            END  
--		) * 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.id_area not in ('CC','Self')
--			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'
----			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 
--				ASSETPERC.COD_PRODOTTO = ASUL.COD_PRODOTTO
--				---ELIMINO GLI SKANDIA
--				AND ASUL.COD_ISIN_SOTT = ''
--				AND ASSETPERC.LIVELLO = 1
--		WHERE
--			ASUL.RETE = @Rete
--			AND ASUL.COD_FISCALE = @CodiceFiscale
--		UNION ALL
--		SELECT ASSETPERC.ID_ASSETCLASS,
--				ASUL.CTV*ASSETPERC.PERC as Controvalore
--		FROM C6MART.DETTAGLIO_ASUL ASUL
--		INNER JOIN C6MART.ASSET_PERC ASSETPERC
--			--PRENDO SOLO GLI SKANDIA
--			ON ASSETPERC.COD_ISIN = ASUL.COD_ISIN_SOTT
--			AND ASSETPERC.COD_MAF = ASUL.COD_MAF_SOTT
--			AND ASSETPERC.COD_INTERNO = ASUL.COD_INTERNO
--            AND rtrim(ASSETPERC.COD_SOTTOPRODOTTO) = ASUL.COD_SOTTOPRODOTTO
--			AND ASSETPERC.LIVELLO = 1
--		WHERE
--			ASUL.RETE = @Rete
--			AND ASUL.COD_FISCALE = @CodiceFiscale
--	) VALORIASSET
--	INNER JOIN 
--		C6MART.ASSETCLASS ASSETCLASS
--		ON 
--			ASSETCLASS.ID_ASSETCLASS = VALORIASSET.ID_ASSETCLASS
--) A
--ORDER BY A.ORDINAMENTO
--END