
			select
				ED.EvnDirection_id as "EvnDirection_id",
				ED.LpuSection_id as "LpuSection_id",
				ED.MedPersonal_id as "MedPersonal_id",
				ED.Diag_id as "Diag_id",
				DT.DirType_id as "DirType_id",
				ED.EvnDirection_IsAuto as "EvnDirection_IsAuto",
				ED.EvnDirection_IsReceive as "EvnDirection_IsReceive",
				ED.Lpu_sid as "Lpu_sid",
				ED.Lpu_id as "Lpu_id",
				COALESCE(ED.Org_sid,Lpu.Org_id) as "Org_id",

				uc.UslugaComplex_id as "UslugaComplex_id",
				uc.UslugaComplex_Name as "UslugaComplex_Name",
				RTRIM(Org.Org_Nick) as "Lpu_Name",
				RTRIM(LSP.LpuSectionProfile_Name) as "LpuSectionProfile_Name",
				ED.EvnDirection_Num as "EvnDirection_Num",
				to_char(COALESCE(COALESCE(TTG.TimetableGraf_begTime, TTP.TimetablePar_begTime), TTS.TimetableStac_setDate), 'DD.MM.YYYY') as "Timetable_begTime",

				RTRIM(DT.DirType_Name) as "DirType_Name",
				RTRIM(Diag.Diag_Code) || '. ' || RTRIM(Diag.Diag_Name) as "Diag_Name",
				to_char(ED.EvnDirection_setDate, 'DD.MM.YYYY') as "EvnDirection_setDate",

				LpuD.Lpu_Nick as "Lpu_dNick",
				case when (EvnUslugaPar.EvnUslugaPar_id is null and EvnPL.EvnPL_id is null and EvnPS.EvnPS_id is null) then 2 else 0 end as enabled,
				LB.LpuBuildingType_id as "LpuBuildingType_id"
				
			from v_EvnDirection_all ED
				left join v_Lpu Lpu  on Lpu.Lpu_id = ED.Lpu_sid
				left join v_Lpu LpuD  on LpuD.Lpu_id = ED.Lpu_did
				left join v_Org Org  on Org.Org_id = COALESCE(ED.Org_sid,Lpu.Org_id)
				left join DirType DT  on DT.DirType_id = ED.DirType_id
				left join LpuSectionProfile LSP  on LSP.LpuSectionProfile_id = ED.LpuSectionProfile_id
				left join v_LpuSection LS  on LS.LpuSection_id = ED.LpuSection_id
				left join v_LpuUnit LU  on LU.LpuUnit_id = LS.LpuUnit_id
				left join v_MedService MS  on MS.MedService_id = ED.MedService_id
				left join v_LpuBuilding LB  on LB.LpuBuilding_id = COALESCE(LU.LpuBuilding_id,MS.LpuBuilding_id)
				left join Diag  on Diag.Diag_id = ED.Diag_id
				LEFT JOIN LATERAL (
					select
						COALESCE(EPLD.UslugaComplex_id,EPFDU.UslugaComplex_id) as UslugaComplex_id
					from
						v_EvnPrescrDirection EPD
						left join EvnPrescrLabDiag EPLD  on EPLD.Evn_id = EPD.EvnPrescr_id
						left join EvnPrescrFuncDiag EPFD  on EPFD.Evn_id = EPD.EvnPrescr_id
						left join EvnPrescrFuncDiagUsluga EPFDU  on EPFDU.EvnPrescrFuncDiag_id = EPFD.Evn_id
					where
						EPD.EvnDirection_id = ed.EvnDirection_id
						limit 1
				) UCOMPL on true
				left join v_UslugaComplex UC  on UC.UslugaComplex_id = UCOMPL.UslugaComplex_id
				LEFT JOIN LATERAL (
					Select TimetableGraf_begTime, MedStaffFact_id from v_TimeTableGraf_lite TTG
					where TTG.EvnDirection_id = ED.EvnDirection_id limit 1
				) TTG on true
				left join TimetablePar TTP  on TTP.TimetablePar_id = ED.TimetablePar_id

				LEFT JOIN LATERAL (
					Select TimetableStac_setDate, LpuSection_id from v_TimetableStac_lite TTS
					where TTS.EvnDirection_id = ED.EvnDirection_id limit 1
				) TTS on true
				LEFT JOIN LATERAL (
					Select EvnPL_id from v_EvnPL EvnPL
					where EvnPL.EvnDirection_id = ED.EvnDirection_id limit 1
				) EvnPL on true
				LEFT JOIN LATERAL (
					Select EvnPS_id from v_EvnPS EvnPS
					where EvnPS.EvnDirection_id = ED.EvnDirection_id limit 1
				) EvnPS on true
				LEFT JOIN LATERAL (
					Select EvnUslugaPar_id from v_EvnUslugaPar EvnUslugaPar
					where EvnUslugaPar.EvnDirection_id = ED.EvnDirection_id limit 1
				) EvnUslugaPar on true

			
			where ED.Person_id = '4176448'
				 and COALESCE(ED.EvnStatus_id, 16) not in (12,13)
				--and EvnPL.EvnPL_id is null and EvnPS.EvnPS_id is null
		
