-- Schema: C6MartPeriodico
-- Stored Procedure: PL_D3_S151ProfiloDiRischio








-- =============================================
-- Author:		<Colaneri>
-- Create date: <05/04/2018>
-- Description:	<Modifiche scheda S151 per MIFID 2>
-- =============================================
--[C6MartPeriodico].[PL_D3_S151ProfiloDiRischio] 'F', 'DVTMRA58B09F839W'
CREATE   procedure [C6MartPeriodico].[PL_D3_S151ProfiloDiRischio]
	-- 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
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	DECLARE @varBF DECIMAL(12,2)
	DECLARE @coperturaBF DECIMAL(12,2)
	DECLARE @codiceProfilo SMALLINT
	DECLARE @dataProfilo DATETIME
	DECLARE @dataScadenzaProfilo DATETIME
	DECLARE @varProfilo DECIMAL(5,2)
	DECLARE @nomeProfilo VARCHAR(50)
	DECLARE @descrizioneProfilo VARCHAR(500)
    DECLARE @riskclass VARCHAR(50)
    DECLARE @experience SMALLINT
	declare @descrizioneEsperienza VARCHAR(100)
	DECLARE @riserva decimal(13,2)
	DECLARE @inv_l_p decimal(5,2)


	SELECT
        @codiceProfilo = M.PROFILO_ASS,
		@dataProfilo = M.DATA_INIZIO_VAL,
		--DA DEFINIRE:
		@dataScadenzaProfilo = M.DATA_INIZIO_VAL,
		@varProfilo = CR.MAX_VAR,
		@nomeProfilo = ProfiliDiRischio.NOMEPROFILO,
		@descrizioneProfilo = ProfiliDiRischio.DescrizioneProfilo,
        @riskclass =  m.riskclass,
        @experience = m.experience,
		@descrizioneEsperienza =	
		case m.experience
				when 0 then 'Minimo'
				when 1 then 'Basso'
				when 2 then 'Medio basso'
				when 3 then 'Medio alto'
				when 4 then 'Alto'
			else '-'
		end
	FROM
		C6MartPERIODICO.MIFID AS M
		INNER JOIN C6MartPERIODICO.CODIFICA_RISCHIO AS CR
		ON M.PROFILO_ASS = CR.PROFILO
		INNER JOIN DBO.ProfiliDiRischio AS ProfiliDiRischio
		ON M.PROFILO_ASS = ProfiliDiRischio.CODICEPROFILO
	WHERE
		M.RETE = @Rete AND
		M.COD_FISCALE = @CodiceFiscale

		-- RICERCA DATA FINE VALIDITA'		
	declare @dtfinval decimal(9,0)

	declare @codman varchar(6)
	set @codman = ''

	if upper(left(@CodiceFiscale,3)) = 'FF@'
	    begin
			set @codman = substring(@CodiceFiscale,4,len(@CodiceFiscale)-3)
			select @dtfinval = dtfinval,@riserva=IMP_RISERVA,@inv_l_p=PERC_ILP from C6StagingPeriodico.WSEIAN2
			where codman = @codman and  DTTRIM = (SELECT MAX(DTTRIM) FROM C6StagingPeriodico.WSEIAN2) 
			print 'codice mandato'
		end
	else
		begin
			select @dtfinval = dtfinval,@riserva=IMP_RISERVA,@inv_l_p=PERC_ILP  from C6StagingPeriodico.WSEIAN2
			where codfis = @CodiceFiscale and  DTTRIM = (SELECT MAX(DTTRIM) FROM C6StagingPeriodico.WSEIAN2) 
			print 'codice fiscale'
		end
		--
		
		if @dtfinval <> 0 and @dtfinval is not null and ltrim(rtrim(@dtfinval)) <> ''
			set @dataScadenzaProfilo = CONVERT(datetime, CONVERT(varchar,CONVERT(int,@dtfinval)), 0)
		else
			set @dataScadenzaProfilo = null

	SELECT
        @codiceProfilo as codiceProfilo,
		DBO.TOSHORTDATESTRING (@dataProfilo) as dataInizioValidita,
		DBO.TOSHORTDATESTRING (convert(datetime, @dataScadenzaProfilo)) as dataScadenzaProfilo,
		@varProfilo as varMassimo,
		ISNULL(@varBF,0) as varBF,
		ISNULL(@coperturaBF,0) as coperturaBF,
		@nomeProfilo as nomeProfilo,
		@descrizioneProfilo as descrizioneProfilo,
        @riskclass as riskclass,
        @experience as experince,
		@descrizioneEsperienza as descrizioneEsperienza,
		@riserva as Riserva,
		@inv_l_p as Inv_L_P

END