-- Stored procedure
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
--  [C6Mart].[PL_S6PatrimonioFinanziario] 'S','LCRGPR67H19Z326U'
CREATE procedure [C6Mart].[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 = 'Banca Fideuram'
	ELSE IF @RETE = 'S'
		SET @NOMERETE = 'Sanpaolo Invest'
		ELSE SET @NOMERETE = 'IW Private Investments'
	SELECT 
		Intermediario,
		Ordine,
		Banca,
		Controvalore,
		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 C6Mart.PATRIMONIO_BF patrBF
			/*LEFT JOIN C6Mart.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.CTV > = 0
				and patrBF.id_area  <> 'cc' --essendo in liquidità ci sono solo i negativi
			UNION ALL
			SELECT 
				Intermediario, 
				SUM(ROUND(CTV,2)) AS ctv,
				2 AS ordine
			FROM C6Mart.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 [C6Mart].[PL_S6PatrimonioFinanziario]    Script Date: 08/02/2022 16:56:07 ******/
--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO
---- Stored procedure
---- =============================================
---- Author:		<Author,,Name>
---- Create date: <Create Date,,>
---- Description:	<Description,,>
---- =============================================
----  [C6Mart].[PL_S6PatrimonioFinanziario] 'S','LCRGPR67H19Z326U'
--ALTER procedure [C6Mart].[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 = 'Banca Fideuram'
--	ELSE
--		SET @NOMERETE = 'Sanpaolo Invest'
--	SELECT 
--		Intermediario,
--		Ordine,
--		Banca,
--		Controvalore,
--		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 C6Mart.PATRIMONIO_BF patrBF
--			/*LEFT JOIN C6Mart.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.CTV > = 0
--				and patrBF.id_area  <> 'cc' --essendo in liquidità ci sono solo i negativi
--			UNION ALL
--			SELECT 
--				Intermediario, 
--				SUM(ROUND(CTV,2)) AS ctv,
--				2 AS ordine
--			FROM C6Mart.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