-- Stored procedure
-- =============================================
-- Author:		Simone D'Elia
-- Create date: Aprile 2022
-- Description: 
-- =============================================
CREATE procedure [C6MartPeriodico].[PL_S183iCapital] --'F','GSSGLI67A52F205P'
 -- Add the parameters for the stored procedure here
 @Rete char(1), 
 @CodiceFiscale varchar(16)
AS
BEGIN
	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  C6MartPeriodico.PATRIMONIO_BF PATRBF
INNER JOIN 
C6MartPeriodico.AREA_BISOGNO AREA 
ON LEFT(PATRBF.ID_AREA,3) = AREA.ID_AREA 
inner join C6MartPeriodico.WSEICAP iCap 
on icap.codconf=PATRBF.ID_CONTRATTO
and icap.subprod=PATRBF.COD_SOTTOPRODOTTO
--and icap.isin=patrbf.ISIN
where icap.RETE=@Rete 
and icap.COD_FISCALE=@CodiceFiscale 
--select 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
	--SELECT 
	--	'Inv' as CodArea,
	--	'Investimento' as NomeArea,
	--	'954572CO210' as CodiceContratto,
	--	'BlackRock' as ContrattoSGR,
	--	'Prodotto di Test' as NomeProdotto,
	--	'05/04/2022' as DataInizio,
	--	'15/01/2023' as DataFine,
	--	'23468.230' as Totale,
	--	'632.234' as ImportoVersato,
	--	'326.263' as ImportoDaVersare
END