﻿CREATE   procedure [C6MartPeriodico].[RP_DM_W6CKADEG]
as 
begin
drop table if exists #tot
truncate table C6MartPeriodico.W6CKADEG
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 'S'
	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,
	vbf.ctv 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
	into #tot
		 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
--select	Rete,
--		Chiave_Acn,
--		Codfis,
--		Codman,
--		Dttrim,
--		Adeg_Rischio,
--		Adeg_Riserva,
--		Adeg_Invl_Period,
--		Adeg_Compl,
--		Adeg_Conc_ProdCompl,
--		Adeg_Freq_Oper,
--		Adeg_Conc_Emitt,
--		Adeg_Conc_Valut
--   from #tot
insert into C6MartPeriodico.W6CKADEG
SELECT
    Rete,
    Chiave_Acn,
    Codfis,
    Codman,
    Dttrim,
		CASE
        WHEN 'N' IN (Adeg_Rischio, Adeg_Riserva, Adeg_Invl_Period, Adeg_Compl, Adeg_Conc_ProdCompl, Adeg_Freq_Oper, Adeg_Conc_Emitt, Adeg_Conc_Valut) THEN 'N'
        ELSE 'S'
    END AS Adeg_Check,
    Adeg_Rischio,
    Adeg_Riserva,
    Adeg_Invl_Period,
    Adeg_Compl,
    Adeg_Conc_ProdCompl,
    Adeg_Freq_Oper,
    Adeg_Conc_Emitt,
    Adeg_Conc_Valut,
	Rfa_tot,
	Rfa_sei,
	Riserva,
	Profilo,
	Profilo_EC,
	Indicatore_ChiamataR4
	--into C6MartPeriodico.W6CKADEG
FROM #tot;
--casi particolari
update C6MartPeriodico.W6CKADEG
set Adeg_Check='N'
where Indicatore_ChiamataR4='Z' --clcompl=Z
and Profilo=0 -- profilo Scaduto
--and isnull(Rfa_sei,0.00)=0.00 -- Rfa_sei = 0 e >0 adeg_check deve essere comunque = N
update C6MartPeriodico.W6CKADEG
set Adeg_Check='S'
where Indicatore_ChiamataR4='Z' --clcompl=Z
and Profilo>0 -- profilo non scaduto 
and isnull(Rfa_tot,0.00)=0.00 --rfa totale =0 
and isnull(Riserva,0.00)=0.00 -- riserva =0
update C6MartPeriodico.W6CKADEG
set Adeg_Check='N'
where Indicatore_ChiamataR4='Z' --clcompl=Z
and Profilo>0 -- profilo non scaduto 
and isnull(Rfa_tot,0.00)=0.00 --rfa totale =0 
and isnull(Riserva,0.00)>0.00 -- riserva >0
--update C6MartPeriodico.W6CKADEG
--set Adeg_Check='N'
--where Indicatore_ChiamataR4='Z' --clcompl=Z
--and Profilo>0 -- profilo non scaduto 
--and isnull(Rfa_tot,0.00)>0.00 --rfa totale =0 
--and isnull(Riserva,0.00)>0.00 -- riserva >0
--SI
--NO
--NA Non Applicabile
--NR Non restituito, Professionale
END