with cte AS (
select
	EvnPS.EvnPS_id,
	EvnSection.EvnSection_id,
	EvnSection.mes_sid,
	EvnSection.mes_tid,
	COALESCE(sm.MesOld_Num, tm.MesOld_Num) AS mes_code,
	EvnSection.evnsection_iswillpaid,
	EvnSection.mestariff_id, 
	EvnSection.mesolduslugacomplex_id,
	EvnSection.diag_id,
jsonb_build_object (
	'evnsection_pid', evnsection_pid,
	'evnsection_id', evnsection_id,
	'evnsection_setdt', evnsection_setdate,
	'evnsection_disdt', evnsection_disdate,
	'person_id', EvnSection.person_id,
	'diag_id', EvnSection.diag_id,
	'paytype_id', EvnSection.paytype_id,
	'lpusection_id', EvnSection.lpusection_id,
	'lpusectionprofile_id', LpuSection.lpusectionprofile_id,
	'lpusectionbedprofile_id', LpuSection.lpusectionbedprofile_id,
	'lpuunittype_id', LpuUnit.lpuunittype_id,
	'rehabscale_id', EvnSection.rehabscale_id,
	'evnsection_sofascalepoints', EvnSection.evnsection_sofascalepoints,
	'drugtherapyscheme_ids', array(select drugtherapyscheme_id from evnsectiondrugtherapyscheme where evnsectiondrugtherapyscheme.evnsection_id = EvnSection.evnsection_id),
	'mesdop_ids', array(select mesdop_id from mesdoplink where mesdoplink.evnsection_id = EvnSection.evnsection_id)
	) AS i	
from v_EvnPS EvnPS
 join v_EvnSection as EvnSection on EvnSection.EvnSection_pid = EvnPS.EvnPS_id and EvnPS.Lpu_id = EvnSection.Lpu_id
	join dbo.v_LpuSection AS LpuSection on LpuSection.lpusection_id = EvnSection.lpusection_id
	JOIN dbo.LpuUnit AS LpuUnit ON LpuSection.lpuunit_id = LpuUnit.lpuunit_id 
 join v_EvnLeaveBase as EvnLeaveBase on EvnLeaveBase.EvnLeaveBase_pid = EvnSection.EvnSection_id and EvnLeaveBase_index = EvnLeaveBase_count - 1
 
 left JOIN dbo.v_mesold AS sm ON sm.mes_id = EvnSection.mes_sid 
 left JOIN dbo.v_mesold AS tm ON tm.mes_id = EvnSection.mes_tid 
	left JOIN dbo.mestariff AS mt ON mt.mestariff_id = EvnSection.mestariff_id
	left join dbo.mesolduslugacomplex AS moc ON EvnSection.mesolduslugacomplex_id = moc.mesolduslugacomplex_id
 left join LeaveType on EvnPS.leavetype_id = LeaveType.leavetype_id
where
     EvnPS.EvnPS_disDate between '2022-01-01' and '2022-01-31'
		-- and EvnPS.Lpu_id = 2832
     --and EvnSection.paytype_id = 520101000000009
     and EvnSection.LeaveType_id is not null
     AND NULLIF(EvnSection.EvnSection_IsPriem, 1) IS NULL 
		 and (
		 COALESCE(EvnPS_disdate, evnsection_disdate) > COALESCE(sm.mes_enddt, 'INFINITY')  
		 or COALESCE(EvnPS_disdate, evnsection_disdate) > COALESCE(tm.mes_enddt, 'INFINITY') 
		 or COALESCE(EvnPS_disdate, evnsection_disdate) > COALESCE(mt.mestariff_enddt, 'INFINITY') 
		 or COALESCE(EvnPS_disdate, evnsection_disdate) > COALESCE(moc.mesolduslugacomplex_enddt, 'INFINITY') 
		 )
order by 2 desc

	)  
	SELECT 
KSG.Mes_tid as "Mes_tid",
				KSG.Mes_sid as "Mes_sid",
				KSG.Mes_kid as "Mes_kid",
				KSG.MesTariff_id as "MesTariff_id",
				KSG.MesOldUslugaComplex_id as "MesOldUslugaComplex_id",
				MO.Mes_Code as "Mes_Code",
				MO.MesOld_Num as "MesOld_Num",
				MO.Mes_Name as "Mes_Name",
				MT.MesTariff_Value as "MesTariff_Value",
				coalesce(MOKPG.Mes_Code || coalesce('. ' || MOKPG.Mes_Name, ''),'') as "KPG",
				KSG.Error_Message as "Error_Msg",
				cte.*				
from cte
left join r52.evnps_ksg_calc(cte.i) KSG on true
	left join v_MesOld MO on MO.Mes_id = coalesce(KSG.Mes_tid, KSG.Mes_sid, KSG.Mes_kid)
				left join v_MesTariff MT on MT.MesTariff_id = KSG.MesTariff_id
				left join v_MesOld MOKPG on MOKPG.Mes_id = KSG.Mes_kid
