﻿/* Nel giro di email di revoca non si devono recuperare i dati dal SEI (Fabrizio) --
AND (Cod_Fiscale = 'TBNMSM52C01E938I')*/
CREATE VIEW [C6MartPeriodico].[vW6CKADEG]
AS
select wsei.rete as Rete,
wsei.codfis as Chiave_Acn,
case when wsei.CODFIS like '%@%' then wsei.piva else wsei.CODFIS end as Codfis,
wsei.CODMAN as Codman,
wsei.DTTRIM as Dttrim,
case when ris.Ind_Rischio='S' then 'S'
	 when ris.Ind_Rischio='N' then 'N' else 'NA' end as Adeg_Rischio,
	 case when wsei.RISERVA='S' then 'S'
		  when wsei.RISERVA='N' then 'N'
		 else 'NA' end as Adeg_Riserva,
case when wsei.INVL_PERIOD='S' then 'S'
	 when wsei.INVL_PERIOD='N' then 'N'
	 else 'NA' end as Adeg_Invl_Period,
	 case
	when wsei.flgprof = 'S' then 'NR'
	when wsei.flagpg = 1 and wsei.flgprof = 'N' and  wsei.flagnqp = 'S' and wsei.flgprlrde = 'N' then 'NA' --Massimiliano dice che va messo prima di tutti senza testare altri flag 17/5/2018
	when upper(wsei.clcompl) = 'N' and wsei.nocompl = 'S' then 'N'
	when upper(wsei.clcompl) = 'N' and wsei.flagpg = 0 then 'N'
		  when upper(wsei.clcompl) = 'N' and wsei.flagpg = 1 and wsei.flgprlrde = 'S' then 'N'
	when upper(wsei.clcompl) = 'S' then 'S'
	else 'NA' end as Adeg_Compl,
	case
    when wsei.flgprof = 'S' then 'NR'
	when upper(wsei.flgconc) = 'S' then 'S'
	when upper(wsei.flgconc) in ('N') then 'N'
	else 'NA' end as Adeg_Conc_ProdCompl,
	case
    when wsei.flgprof = 'S' then 'NR'
	when upper(wsei.freq) = 'S' then 'S'
	when upper(wsei.freq) = 'N' then 'N'
	else 'NA'	
end as Adeg_Freq_Oper,
case
	when wsei.flgprof = 'S' then 'NR'
	when upper(wsei.concemi) = 'S' then 'S'
	when upper(wsei.concemi) = 'N' then 'N'
	else 'NA' end as Adeg_Conc_Emitt,
	case
	when wsei.flgprof = 'S' then 'NR'
	when upper(wsei.ADEGCONC_VALUT) = 'S' then 'S'
	when upper(wsei.ADEGCONC_VALUT) = 'N' then 'N'
	else 'NA'end as Adeg_Conc_Valut,
	isnull(vbf.ctv,0.00) + isnull(ppvv.IMPORTO,0.00) as Rfa_tot,
	nob.CTV as Rfa_sei,
	wsei.IMP_RISERVA as Riserva,
	wsei.CODPROF as Profilo,
	wsei.COD_PROFEC AS Profilo_EC,
	wsei.CLCOMPL as Indicatore_ChiamataR4,
	wsei.FLGPRLRDE as Flg_Legali_Rappresentanti
		 from C6MartPeriodico.WSEIAN2 wsei
left join C6MartPeriodico.Indicatore_Rischio ris on RIS.rete=wsei.RETE and RIS.cod_fiscale=wsei.CODFIS
left join [C6MartPeriodico].[vPatrimonioBFAggregato] vbf on vbf.RETE=wsei.RETE and wsei.CODFIS=vbf.COD_FISCALE
left join [C6MartPeriodico].[vPatrimonioBFAggregato_SenzaNobPrez] nob on nob.RETE=wsei.RETE and wsei.CODFIS=nob.COD_FISCALE
left join  [C6MartPeriodico].[PARTITE_VIAGGIANTI] ppvv on wsei.RETE=ppvv.RETE and wsei.CODFIS=ppvv.COD_FISCALE
--and c6.cod_fiscale = 'BRZGPP39H08C352F'