-- Stored procedure
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
--  [C6MartPeriodico].[PL_S6PatrimonioFinanziario] 'F','GZZSLD52E20G869L'
CREATE procedure [C6MartPeriodico].[PL_S6PatrimonioFinanziario]
	-- Add the parameters for the stored procedure here
	@Rete char(1), 
	@CodiceFiscale varchar(16)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	DECLARE @NOMERETE VARCHAR(20)
	IF @Rete = 'F'
		SET @NOMERETE = 'Fideuram'
	ELSE IF @Rete = 'S'
		SET @NOMERETE = 'Sanpaolo Invest'
		ELSE SET @NOMERETE = 'IW Private Investments'
	SELECT 
		Intermediario,
		Ordine,
		Banca,
		Controvalore,
		round(Percentuale,2) as Percentuale
	FROM 
	(
		SELECT DISTINCT
			Intermediario,
			Ordine,
			@NOMERETE AS Banca,
			-- if(BancaFideuram) -> Sommare al controvalore le partiteViaggiantiInv
			SUM(ctv) over (partition by Intermediario,Ordine) AS CONTROVALORE,
			(
				(SUM(ctv) over (partition by Intermediario,Ordine)) / 
				(SUM(ctv) over (partition by 'Totale'))
			) * 100 AS PERCENTUALE
		FROM 
		(
			SELECT 
				@NOMERETE AS Intermediario,
				SUM(ROUND(CTV,2)) AS ctv,
				1 AS ordine
			FROM C6MartPeriodico.PATRIMONIO_BF patrBF
--			LEFT JOIN C6MartPeriodico.ClientiConCCNegativo CCNegativo
--				ON PATRBF.RETE = CCNegativo.Rete
--				AND PATRBF.COD_FISCALE = CCNegativo.Cod_Fiscale
--				AND PATRBF.TIPO_PRODOTTO = 'CC'
			WHERE 1 = 1
				AND patrBF.Rete = @Rete
				AND patrBF.Cod_Fiscale = @CodiceFiscale
				--AND CCNegativo.Cod_Fiscale IS NULL		-- ELIMINO I CC CON SALDO NEGATIVO
				and patrBF.id_area  <> 'cc'
			UNION ALL
			SELECT 
				Intermediario, 
				SUM(ROUND(CTV,2)) AS ctv,
				2 AS ordine
			FROM C6MartPeriodico.PATRIMONIO_TERZI patrTerzi
			WHERE 1 = 1
				AND patrTerzi.Rete = @Rete
				AND patrTerzi.Cod_Fiscale = @CodiceFiscale
			GROUP BY patrTerzi.Intermediario
		) PATR
	) A
	WHERE CONTROVALORE IS NOT NULL
	ORDER BY Ordine
END
--USE [C6StampeCentralizzate]
--GO
--/****** Object:  StoredProcedure [C6MartPeriodico].[PL_S6PatrimonioFinanziario]    Script Date: 10/02/2022 15:58:38 ******/
--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO
---- Stored procedure
---- =============================================
---- Author:		<Author,,Name>
---- Create date: <Create Date,,>
---- Description:	<Description,,>
---- =============================================
----  [C6MartPeriodico].[PL_S6PatrimonioFinanziario] 'F','GZZSLD52E20G869L'
--ALTER procedure [C6MartPeriodico].[PL_S6PatrimonioFinanziario]
--	-- Add the parameters for the stored procedure here
--	@Rete char(1), 
--	@CodiceFiscale varchar(16)
--AS
--BEGIN
--	-- SET NOCOUNT ON added to prevent extra result sets from
--	DECLARE @NOMERETE VARCHAR(20)
--	IF @Rete = 'F'
--		SET @NOMERETE = 'Fideuram'
--	ELSE
--		SET @NOMERETE = 'Sanpaolo Invest'
--	SELECT 
--		Intermediario,
--		Ordine,
--		Banca,
--		Controvalore,
--		round(Percentuale,2) as Percentuale
--	FROM 
--	(
--		SELECT DISTINCT
--			Intermediario,
--			Ordine,
--			@NOMERETE AS Banca,
--			-- if(BancaFideuram) -> Sommare al controvalore le partiteViaggiantiInv
--			SUM(ctv) over (partition by Intermediario,Ordine) AS CONTROVALORE,
--			(
--				(SUM(ctv) over (partition by Intermediario,Ordine)) / 
--				(SUM(ctv) over (partition by 'Totale'))
--			) * 100 AS PERCENTUALE
--		FROM 
--		(
--			SELECT 
--				@NOMERETE AS Intermediario,
--				SUM(ROUND(CTV,2)) AS ctv,
--				1 AS ordine
--			FROM C6MartPeriodico.PATRIMONIO_BF patrBF
----			LEFT JOIN C6MartPeriodico.ClientiConCCNegativo CCNegativo
----				ON PATRBF.RETE = CCNegativo.Rete
----				AND PATRBF.COD_FISCALE = CCNegativo.Cod_Fiscale
----				AND PATRBF.TIPO_PRODOTTO = 'CC'
--			WHERE 1 = 1
--				AND patrBF.Rete = @Rete
--				AND patrBF.Cod_Fiscale = @CodiceFiscale
--				--AND CCNegativo.Cod_Fiscale IS NULL		-- ELIMINO I CC CON SALDO NEGATIVO
--				and patrBF.id_area  <> 'cc'
--			UNION ALL
--			SELECT 
--				Intermediario, 
--				SUM(ROUND(CTV,2)) AS ctv,
--				2 AS ordine
--			FROM C6MartPeriodico.PATRIMONIO_TERZI patrTerzi
--			WHERE 1 = 1
--				AND patrTerzi.Rete = @Rete
--				AND patrTerzi.Cod_Fiscale = @CodiceFiscale
--			GROUP BY patrTerzi.Intermediario
--		) PATR
--	) A
--	WHERE CONTROVALORE IS NOT NULL
--	ORDER BY Ordine
--END