
			select
				case when case
				when EPL.Lpu_id = '46' and exists (select EV.EvnVizitPL_id
					from v_EvnVizitPL EV 
					where EV.EvnVizitPL_pid = epl.EvnPL_id
					and (EV.MedStaffFact_id = '450101000080148' OR EV.LpuSection_id = '450101000004063')
					) then 1
				else 0
			end = 1 then 'edit' else 'view' end as "accessType",
				case when EPL.Lpu_id = '46' and COALESCE(epl.EvnPL_IsFinish, 1) != 2 and ec.EvnClass_SysNick = 'EvnPL' then 1 else 0 end as "canCreateVizit",
				epl.EvnPL_id as "EvnPL_id",
				epl.EvnPL_IsSigned as "EvnPL_IsSigned",
				epl.Person_id as "Person_id",
				epl.PersonEvn_id as "PersonEvn_id",
				epl.Server_id as "Server_id",
				epl.EvnPL_NumCard as "EvnPL_NumCard",
				epl.EvnPL_IsFinish as "EvnPL_IsFinish",
				(case when epl.EvnPL_isMseDirected = 2 then 'Да' else 'Нет' end) as "EvnPL_isMseDirected",
				(case when epl.EvnPL_IsFirstDisable = 0 then 'Нет' else (case when epl.EvnPL_IsFirstDisable = 1 then 'Да' else null end) end) as "EvnPL_IsFirstDisable",
				(cast(prt.PrivilegeType_Code as varchar(1))||'. '||prt.PrivilegeType_Name) as "PrivilegeType_id",
				d.Diag_id as "Diag_id",
				d.Diag_Code as "Diag_Code",
				d.Diag_Name as "Diag_Name",
				ifin.YesNo_Name as "EvnPL_IsFinishName",
				epl.EvnPL_UKL as "EvnPL_UKL",
				isr.YesNo_Name as "EvnPL_IsSurveyRefuseName",
				rc.ResultClass_Name as "ResultClass_Name",
				rc.ResultClass_id as "ResultClass_id",
				ilt.InterruptLeaveType_Name as "InterruptLeaveType_Name",
				rdt.ResultDeseaseType_Name as "ResultDeseaseType_Name",
				dt.DirectType_Name as "DirectType_Name",
				dt.DirectType_id as "DirectType_id",
				dc.DirectClass_Name as "DirectClass_Name",
				dc.DirectClass_id as "DirectClass_id",
				ls.LpuSection_FullName as "LpuSection_FullName",
				lpu.Lpu_Name as "Lpu_Name",
				dl.Diag_Code || '. ' || dl.Diag_Name as "Diag_lName",
				dco.Diag_Code || '. ' || dco.Diag_Name as "Diag_concName",
				pt.PrehospTrauma_Name as "PrehospTrauma_Name",
				pt.PrehospTrauma_id as "PrehospTrauma_id",
				iu.YesNo_Name as "EvnPL_IsUnportName",
				lt.LeaveType_Name as "LeaveType_fedName",
				lt.LeaveType_id as "LeaveType_id",
				rdtf.ResultDeseaseType_Name as "ResultDeseaseType_fedName",
				rdtf.ResultDeseaseType_id as "ResultDeseaseType_id",
				EUP.cnt as "EvnUslugaParCount",
				BSS.PregnancyResult_id as "PregnancyResult_id",
				BSS.AbortMethod_id as "AbortMethod_id",
				ES.cnt + ES2.cnt as "EvnStickCount",
				ER.cnt as "EvnReceptCount",
				EMD.cnt as "EvnMediaDataCount",
				to_char(epl.EvnPL_setDate, 'dd.mm.yyyy') as "EvnPL_setDate"
			from
				v_EvnPL epl
				left join v_Lpu lpu on epl.Lpu_oid = lpu.Lpu_id
				left join v_evnclass ec on ec.EvnClass_id = epl.EvnClass_id
				left join v_LpuSection ls on epl.LpuSection_oid = ls.LpuSection_id
				left join v_PrivilegeType prt on epl.PrivilegeType_id = prt.PrivilegeType_id
				left join v_YesNo ifin on ifin.YesNo_id = epl.EvnPL_IsFinish
				left join v_ResultClass rc on rc.ResultClass_id = epl.ResultClass_id
				left join v_InterruptLeaveType ilt on ilt.InterruptLeaveType_id = epl.InterruptLeaveType_id
				left join v_DirectType dt on dt.DirectType_id = epl.DirectType_id
				left join v_DirectClass dc on dc.DirectClass_id = epl.DirectClass_id
				left join v_Diag dl on dl.Diag_id = epl.Diag_lid
				left join v_Diag dco on dco.Diag_id = epl.Diag_concid
				left join v_PrehospTrauma pt on pt.PrehospTrauma_id = epl.PrehospTrauma_id
				left join fed.v_LeaveType lt on lt.LeaveType_id = epl.LeaveType_fedid
				left join v_ResultDeseaseType rdt on rdt.ResultDeseaseType_id = epl.ResultDeseaseType_id
				left join fed.v_ResultDeseaseType rdtf on rdtf.ResultDeseaseType_id = epl.ResultDeseaseType_fedid
				left join v_YesNo iu on iu.YesNo_id = epl.EvnPL_IsUnport
				left join v_YesNo isr on isr.YesNo_id = epl.EvnPL_IsSurveyRefuse
				left join v_Diag d on d.Diag_id = epl.Diag_id
				LEFT JOIN LATERAL (
					select
						PersonRegister_id,
						PersonPregnancy_id
					from 
						v_PersonPregnancy 
					where Person_id = epl.Person_id
					order by 
						PersonPregnancy_id
					desc 
					limit 1
				) PReg on true
				LEFT JOIN LATERAL (
					select
						PersonRegister_id
					from 
						v_PersonRegister 
					where Person_id = epl.Person_id and PersonRegisterType_id = 2
					order by 
						PersonRegister_id
					desc 
					limit 1
				) PersonRegister on true
				left join v_BirthSpecStac BSS on BSS.PersonRegister_id = coalesce(PReg.PersonRegister_id,PersonRegister.PersonRegister_id)
				left join lateral (
					select
						count(EUP.EvnUslugaPar_id) as cnt
					from
						v_EvnUslugaPar EUP
						left join v_Evn EvnUP on EvnUP.Evn_id = eup.EvnUslugaPar_pid
					where
						EUP.EvnUslugaPar_rid = EPL.EvnPL_id
						and EUP.EvnUslugaPar_setDT is not null
						and COALESCE(EvnUP.EvnClass_SysNick, '') != 'EvnUslugaPar'
				) EUP on true
				left join lateral (
					select
						count(ER.EvnRecept_id) as cnt
					from
						v_EvnRecept er
					where
						er.EvnRecept_pid = EPL.EvnPL_id
						and er.Lpu_id = EPL.Lpu_id
				) ER on true
				left join lateral (
					select
						count(ES.EvnStickBase_id) as cnt
					from
						v_EvnStickBase es
					where
						es.EvnStickBase_mid = EPL.EvnPL_id
				) ES on true
				left join lateral (
					select
						count(ES.EvnStickBase_id) as cnt
					from
						v_EvnStickBase es
					where
						exists (select EvnLink_id as "EvnLink_id"  from v_EvnLink where Evn_id = EPL.EvnPL_id and Evn_lid = es.EvnStickBase_id)
				) ES2 on true
				left join lateral (
					select
						count(EMD.EvnMediaData_id) as cnt
					from
						v_EvnMediaData EMD
					where
						EMD.Evn_id = epl.EvnPL_id
				) EMD on true
			where
				EvnPL_id = '450101245026831'
				limit 1
		
