-- Schema: C6Mart
-- Stored Procedure: PL_S183iCapital













--Inv	Investimento	021233XY004	BLACK ROCK EQT X 20230215	99999999	300000.000	0.000	300000.000

-- Stored procedure

-- =============================================
-- Author:		Simone D'Elia
-- Create date: Aprile 2022
-- Description: 
-- =============================================

CREATE procedure [C6Mart].[PL_S183iCapital] --'S','DGSMHL70H08Z404F'
 -- Add the parameters for the stored procedure here
 @Rete char(1), 
 @CodiceFiscale varchar(16)

AS
BEGIN

--SELECT 
--		'Inv' as CodArea,
--		'Investimento' as NomeArea,
--		'954572CO210' as CodiceContratto,
--		'BlackRock' as ContrattoSGR,
--		'Prodotto di Test' as NomeProdotto,
--		cast('05/04/2022' as varchar) as DataInizio,
--		cast('15/01/2023' as varchar) as DataFine,
--		cast('23468.230' as varchar) as Totale,
--		cast('632.234' as varchar) as ImportoVersato,
--		cast('326.263' as varchar) as ImportoDaVersare

--		union all

--		SELECT 
--		'Inv' as CodArea,
--		'Investimento' as NomeArea,
--		'954572CO210' as CodiceContratto,
--		'Fideuram' as ContrattoSGR,
--		'Test con stesso NomeProdotto' as NomeProdotto,
--		cast('05/03/2022' as varchar) as DataInizio,
--		cast('15/02/2023' as varchar) as DataFine,
--		cast('238.230' as varchar) as Totale,
--		cast('2.234' as varchar) as ImportoVersato,
--		cast('36.263' as varchar) as ImportoDaVersare

--		union all

--		SELECT 
--		'Ris' as CodArea,
--		'Riserva' as NomeArea,
--		'000000CO111' as CodiceContratto,
--		'Fideuram' as ContrattoSGR,
--		'Prodotto con codice diverso' as NomeProdotto,
--		cast('05/12/2020' as varchar) as DataInizio,
--		cast('15/01/2012' as varchar) as DataFine,
--		cast('23328.230' as varchar) as Totale,
--		cast('2123.234' as varchar) as ImportoVersato,
--		cast('362.263' as varchar) as ImportoDaVersare


	SELECT
distinct
CASE 
	WHEN AREA.ID_AREA = 'Pre1' OR AREA.ID_AREA = 'Pre2' THEN 'Pre' 
	ELSE AREA.ID_AREA 
END AS Codarea,
CASE 
	WHEN AREA.ID_AREA = 'Pre1' OR AREA.ID_AREA = 'Pre2' THEN 'Previdenza'
	WHEN AREA.ID_AREA = 'Na'  THEN 'Non allocate'
	ELSE AREA.NOME_AREA 
END AS NomeArea,
--CASE 
	--WHEN AREA.ID_AREA = 'Pre1' OR AREA.ID_AREA = 'Pre2' THEN  3 
	--ELSE AREA.ORDINAMENTO 
--END AS OrdineArea,
iCap.codconf as CodiceContratto,
iCap.sgr as ContrattoSGR,
iCap.descprod as NomeProdotto,
iCap.dtiniz as DataInizio,
iCap.dtfine as DataFine,
iCap.CTV_COMMIT as Totale,
iCap.impvers as ImportoVersato,
iCap.impres as ImportoDaVersare
from  C6Mart.PATRIMONIO_BF PATRBF
INNER JOIN 
C6Mart.AREA_BISOGNO AREA 
ON LEFT(PATRBF.ID_AREA,3) = AREA.ID_AREA 
inner join C6Mart.WSEICAP iCap 
on icap.codconf=PATRBF.ID_CONTRATTO
and icap.subprod=PATRBF.COD_SOTTOPRODOTTO
and icap.isin=patrbf.ISIN
where patrbf.RETE=@Rete 
and patrbf.COD_FISCALE=@CodiceFiscale 

--select top(1) Codarea,NomeArea,CodiceContratto,ContrattoSGR,NomeProdotto,DataInizio,DataFine,
--cast(Totale as decimal(15,2))as Totale,
--cast(ImportoVersato as decimal(15,2))as ImportoVersato ,
--cast(ImportoDaVersare as decimal(15,2)) as ImportoDaVersare 
--from C6MartPeriodico.test_icapital 
--where rete=@rete and CODiceFISCALE=@CodiceFiscale



END


