
			select
				case when EvnVizitPL.Lpu_id = '13101856' AND coalesce(EvnVizitPL.EvnVizitPL_IsSigned,1) = 1 then 'edit' else 'view' end as "accessType",
			    case when ((RPL.EvnReanimatPeriod_id is not null and 'polka' = 'stac6') or (RPL.EvnReanimatPeriod_id is null and 'polka' = 'reanimation6') and es.EvnSection_IsPriem <> 2) then 'view' else 'edit' end as "calendarAccessType",
				EP.EvnPrescr_id as "EvnPrescr_id",
				'EvnCourseProc' as "object",
				EP.EvnPrescr_pid as "EvnPrescr_pid",
				EP.EvnPrescr_rid as "EvnPrescr_rid",
				to_char(EP.EvnPrescr_setDT, 'dd.mm.yyyy') as "EvnPrescr_setDate",
			    to_char(EP.EvnPrescr_begRecomDate, 'dd.mm.yyyy hh24:mi') as "EvnPrescr_begRecomDate",
			    to_char(EP.EvnPrescr_endRecomDate, 'dd.mm.yyyy hh24:mi') as "EvnPrescr_endRecomDate",
				null as "EvnPrescr_setTime",
				coalesce(EP.EvnPrescr_IsExec,1) as "EvnPrescr_IsExec",
				coalesce(EP.EvnPrescr_IsExec,1) as "EvnPrescrIsExecPrescription",
				case when EU.EvnUsluga_id is null then 1 else 2 end as "EvnPrescr_IsHasEvn",
				case when 2 = EP.EvnPrescr_IsExec then to_char(EP.EvnPrescr_updDT, 'dd.mm.yyyy HH24:MI:SS') else null end as "EvnPrescr_execDT",
				EP.PrescriptionStatusType_id as "PrescriptionStatusType_id",
				EP.PrescriptionType_id as "PrescriptionType_id",
				EP.PrescriptionType_id as "PrescriptionType_Code",
				coalesce(EP.EvnPrescr_IsCito,1) as "EvnPrescr_IsCito",
				coalesce(EP.EvnPrescr_Descr,'') as "EvnPrescr_Descr",
				CUC.UslugaComplex_id as "CourseUslugaComplex_id",
				CUC.UslugaComplex_2011id as "CourseUslugaComplex_2011id",
				CUC.UslugaComplex_Name as "CourseUslugaComplex_Name",
				CUC.UslugaComplex_Code as "CourseUslugaComplex_Code",
				MS.MedService_id as "MedService_id",
				MS.MedService_Name as "MedService_Name",
				EPPR.EvnCourse_id as "EvnCourse_id",
				coalesce(CAST(ECPR.EvnCourseProc_MaxCountDay AS varchar), '') as "MaxCountInDay",
                coalesce(CAST(ECPR.EvnCourseProc_MinCountDay AS varchar), '') as "MinCountInDay",
                coalesce(CAST(ECPR.EvnCourseProc_Duration AS varchar), '') as "Duration",
                coalesce(CAST(ECPR.EvnCourseProc_ContReception AS varchar), '') as "ContReception",
                coalesce(CAST(ECPR.EvnCourseProc_Interval AS varchar), '') as "Interval",
				DTP.DurationType_Nick as "DurationType_Nick",
				DTI.DurationType_Nick as "DurationType_IntNick",
				DTN.DurationType_Nick as "DurationType_RecNick",
				UC.UslugaComplex_id as "UslugaComplex_id",
				UC.UslugaComplex_2011id as "UslugaComplex_2011id",
				UC.UslugaComplex_Code as "UslugaComplex_Code",
				UC.UslugaComplex_Name as "UslugaComplex_Name",
				null as "TableUsluga_id",
				case when ED.EvnDirection_id is null OR coalesce(ED.EvnStatus_id, 16) in (12,13) then 1 else 2 end as "EvnPrescr_IsDir",
				case when ED.EvnStatus_id is null and (ED.DirFailType_id > 0 OR EQ.QueueFailCause_id > 0 ) then 12 else ED.EvnStatus_id end as "EvnStatus_id",
				case when EvnStatus.EvnStatus_Name is null and (ED.DirFailType_id > 0 OR EQ.QueueFailCause_id > 0 ) then 'Отменено' else EvnStatus.EvnStatus_Name end as "EvnStatus_Name",
				EvnStatus.EvnStatus_SysNick as "EvnStatus_SysNick",
				coalesce(EvnStatusCause.EvnStatusCause_Name, DFT.DirFailType_Name, QFC.QueueFailCause_Name) as "EvnStatusCause_Name",
				to_char(coalesce(ED.EvnDirection_statusDate, ED.EvnDirection_failDT, EQ.EvnQueue_failDT), 'dd.mm.yyyy') as "EvnDirection_statusDate",
				ESH.EvnStatusCause_id as "EvnStatusCause_id",
				ED.DirFailType_id as "DirFailType_id",
				EQ.QueueFailCause_id  as "QueueFailCause_id",
				ESH.EvnStatusHistory_Cause as "EvnStatusHistory_Cause",
				ED.EvnDirection_id as "EvnDirection_id",
				ED.EvnDirection_IsSigned as "EvnDirection_IsSigned",
				ED.EvnDirection_IsSigned as "IsSigned",
				0 as "EvnDirection_SignCount",
				0 as "EvnDirection_MinSignCount",
				EQ.EvnQueue_id as "EvnQueue_id",
				EP2.CountIsExec as "CountIsExec",
				EP2.CountIsUnExec as "CountIsUnExec",
				case when ED.EvnDirection_Num is null /*or coalesce(ED.EvnDirection_IsAuto,1) = 2*/ then '' else cast(ED.EvnDirection_Num as varchar) end as "EvnDirection_Num",
				case
					when TTMS.TimetableMedService_id is not null then
						case
							when MS.MedService_id is not null and  MS.LpuSection_id is null and MS.LpuUnit_id is null
							then coalesce(MS.MedService_Name,'')
							when MS.MedService_id is not null and  MS.LpuSection_id is null and MS.LpuUnit_id is not null
							then coalesce(MS.MedService_Name,'') ||' / '|| coalesce(LU.LpuUnit_Name,'')
							when MS.MedService_id is not null and  MS.LpuSection_id is not null and MS.LpuUnit_id is not null
							then coalesce(MS.MedService_Name,'') ||' / '|| coalesce(LSPD.LpuSectionProfile_Name,'') ||' / '|| coalesce(LU.LpuUnit_Name,'')
							else coalesce(LSPD.LpuSectionProfile_Name,'') ||' / '|| coalesce(LU.LpuUnit_Name,'')
						end ||' / '|| coalesce(Lpu.Lpu_Nick,'')
					when EQ.EvnQueue_id is not null then
						case
							when MS.MedService_id is not null and  MS.LpuSection_id is null and MS.LpuUnit_id is null
							then coalesce(MS.MedService_Name,'')
							when MS.MedService_id is not null and  MS.LpuSection_id is null and MS.LpuUnit_id is not null
							then coalesce(MS.MedService_Name,'') ||' / '|| coalesce(LU.LpuUnit_Name,'')
							when MS.MedService_id is not null and  MS.LpuSection_id is not null and MS.LpuUnit_id is not null
							then coalesce(MS.MedService_Name,'') ||' / '|| coalesce(LSPD.LpuSectionProfile_Name,'') ||' / '|| coalesce(LU.LpuUnit_Name,'')
							else coalesce(LSPD.LpuSectionProfile_Name,'') ||' / '|| coalesce(LU.LpuUnit_Name,'')
						end ||' / '|| coalesce(Lpu.Lpu_Nick,'')
				else '' end as "RecTo",
				case
					when TTMS.TimetableMedService_id is not null then coalesce(to_char(TTMS.TimetableMedService_begTime, 'dd.mm.yyyy'),'')||' '||coalesce(to_char(TTMS.TimetableMedService_begTime, 'HH24:MI:SS'),'')
					when EQ.EvnQueue_id is not null then 'В очереди с '|| coalesce(to_char(EQ.EvnQueue_setDate, 'dd.mm.yyyy'),'')
				else '' end as "RecDate",
				case
					when TTMS.TimetableMedService_id is not null then 'TimetableMedService'
					when EQ.EvnQueue_id is not null then 'EvnQueue'
				else '' end as "timetable",
				case
					when TTMS.TimetableMedService_id is not null then CAST(TTMS.TimetableMedService_id AS varchar)
                    when EQ.EvnQueue_id is not null then CAST(EQ.EvnQueue_id AS varchar)
				else '' end as "timetable_id",
				coalesce(to_char(TTMS.TimetableMedService_begTime, 'dd.mm.yyyy HH24:MI:SS'),'') as "TimetableMedService_begTime",
				EP.EvnPrescr_pid as "timetable_pid",
				LU.LpuUnitType_SysNick as "LpuUnitType_SysNick",
				DT.DirType_Code as "DirType_Code",
				EC.EvnCourse_IsManual as "EvnCourse_IsManual",
				EUP.EvnUslugaPar_id as "EvnUslugaPar_id",
				CASE 
					when ((ED.Lpu_did is not null and ED.Lpu_did <> LpuSession.Lpu_id) or (Lpu.Lpu_id is not null and Lpu.Lpu_id <> LpuSession.Lpu_id) or (ECPR.Lpu_id is not null and ECPR.Lpu_id <> LpuSession.Lpu_id)) then 2 else 1
				end as "otherMO",
				coalesce(to_char(EQ.EvnQueue_setDate, 'dd.mm.yyyy'),'') as "EvnQueue_setDate"
			from v_EvnPrescr EP
				inner join v_EvnPrescrProc EPPR on EPPR.EvnPrescrProc_id = EP.EvnPrescr_id
				left join v_EvnCourseProc ECPR on ECPR.EvnCourseProc_id = EPPR.EvnCourse_id
				left join v_EvnCourse EC on EC.EvnCourse_id = EPPR.EvnCourse_id
				left join DurationType DTP on ECPR.DurationType_id = DTP.DurationType_id
				left join DurationType DTN on ECPR.DurationType_recid = DTN.DurationType_id
				left join DurationType DTI on ECPR.DurationType_intid = DTI.DurationType_id
				left join v_UslugaComplex UC on UC.UslugaComplex_id = EPPR.UslugaComplex_id
				left join v_UslugaComplex CUC on CUC.UslugaComplex_id = ECPR.UslugaComplex_id
				left join lateral(
					Select ED.EvnDirection_id
						,coalesce(ED.Lpu_sid, ED.Lpu_id) Lpu_id
						,ED.EvnQueue_id
						,ED.EvnDirection_Num
						,ED.EvnDirection_IsAuto
						,ED.LpuSection_did
						,ED.LpuUnit_did
						,ED.Lpu_did
						,ED.MedService_id
						,ED.LpuSectionProfile_id
						,ED.DirType_id
						,ED.EvnStatus_id
						,ED.EvnDirection_statusDate
						,ED.DirFailType_id
						,ED.EvnDirection_failDT
						,ED.EvnDirection_IsSigned
					from v_EvnPrescrDirection epd
					inner join v_EvnDirection_all ED on epd.EvnDirection_id = ED.EvnDirection_id
					where epd.EvnPrescr_id = EP.EvnPrescr_id
					order by 
						case when coalesce(ED.EvnStatus_id, 16) in (12,13) then 2 else 1 end /* первым неотмененное/неотклоненное направление */
						,epd.EvnPrescrDirection_insDT desc
					limit 1
				) ED on true
				-- службы и параклиника
				left join lateral(
					Select TimetableMedService_id, TimetableMedService_begTime from v_TimetableMedService_lite TTMS where TTMS.EvnDirection_id = ED.EvnDirection_id limit 1
				) TTMS on true
				-- очередь
				left join lateral(
					(Select EQ.EvnQueue_id, EQ.LpuUnit_did, EQ.LpuSectionProfile_did, Lpu_id, EQ.EvnQueue_setDate, EQ.EvnQueue_failDT, EQ.QueueFailCause_id
					from v_EvnQueue EQ
					where EQ.EvnDirection_id = ED.EvnDirection_id
					and EQ.EvnQueue_recDT is null
					limit 1)
					-- это костыль для того, чтобы направления у которых есть связь по EQ.EvnQueue_id = ED.EvnQueue_id, но нет реальной записи в TimetableMedService можно было отменить
					union
					(Select EQ.EvnQueue_id, EQ.LpuUnit_did, EQ.LpuSectionProfile_did, Lpu_id, EQ.EvnQueue_setDate, EQ.EvnQueue_failDT, EQ.QueueFailCause_id 
					from v_EvnQueue EQ
					where (EQ.EvnQueue_id = ED.EvnQueue_id)
					and (EQ.EvnQueue_recDT is null or TTMS.TimetableMedService_id is null)
					and EQ.EvnQueue_failDT is null
					limit 1)
				) EQ on true
				left join lateral(
					select ESH.EvnStatus_id, ESH.EvnStatusCause_id, ESH.pmUser_insID, ESH.EvnStatusHistory_Cause
					from EvnStatusHistory ESH
					where ESH.Evn_id = ED.EvnDirection_id
						and ESH.EvnStatus_id = ED.EvnStatus_id
					order by ESH.EvnStatusHistory_begDate desc
					limit 1
				) ESH on true
				left join EvnStatus on EvnStatus.EvnStatus_id = ESH.EvnStatus_id
				left join EvnStatusCause on EvnStatusCause.EvnStatusCause_id = ESH.EvnStatusCause_id
				left join v_DirFailType DFT on DFT.DirFailType_id = ED.DirFailType_id
				left join v_QueueFailCause QFC on QFC.QueueFailCause_id = EQ.QueueFailCause_id
				-- сама служба (todo: надо ли оно)
				left join v_MedService MS on MS.MedService_id = coalesce(ED.MedService_id, EP.MedService_id) -- ED.MedService_did должно быть
				-- отделение для полки и стаца и для очереди
				left join v_LpuSection LS on LS.LpuSection_id = coalesce(ED.LpuSection_did, MS.LpuSection_id)
				-- заказанная услуга для параклиники
				left join v_EvnUslugaPar EUP on EUP.EvnDirection_id = ED.EvnDirection_id
				-- подразделение для очереди и служб
				left join v_LpuUnit LU on coalesce(ED.LpuUnit_did,EQ.LpuUnit_did,MS.LpuUnit_id) = LU.LpuUnit_id -- todo: в ED.LpuUnit_did пусто, чего не должно быть
				-- профиль для очереди
				left join v_LpuSectionProfile LSPD on coalesce(ED.LpuSectionProfile_id,EQ.LpuSectionProfile_did,LS.LpuSectionProfile_id) = LSPD.LpuSectionProfile_id -- todo: тут на примере оказалось что почему то ED.LpuSectionProfile_id != EQ.LpuSectionProfile_did, чего не должно быть
				-- тип направления
				left join v_DirType DT on ED.DirType_id = DT.DirType_id
				-- ЛПУ
				left join v_Lpu Lpu on Lpu.Lpu_id = coalesce(ED.Lpu_did, LS.Lpu_id, MS.Lpu_id, EQ.Lpu_id)
				left join v_Lpu LpuSession on LpuSession.Lpu_id = '13101856'

				left join lateral(
					select EvnUsluga_id, EvnUsluga_setDT from v_EvnUsluga
					where EP.EvnPrescr_IsExec = 2 and UC.UslugaComplex_id is not null and EvnPrescr_id = EP.EvnPrescr_id
					limit 1
				) EU on true
         		left join lateral (
					select coalesce(SUM(count.execEvn),0) as CountIsExec,
					coalesce(SUM(count.unExecEvn),0) as CountIsUnExec
						from (
							select case when epp.EvnPrescr_IsExec = 2 then 1 else 0 end as execEvn,
							case when epp.EvnPrescr_IsExec = 1 then 1 else 0 end as unExecEvn
							from v_EvnPrescrProc EPT2
								left join v_EvnPrescr epp on EPT2.EvnPrescrProc_id = epp.EvnPrescr_id
						where EPPR.evncourse_id = EPT2.evncourse_id
						) as count
				) EP2 ON true
				left join v_ReanimatPeriodPrescrLink RPL on RPL.EvnPrescr_id = EP.EvnPrescr_id
				left join v_EvnSection es on es.EvnSection_id = '890101013847807'				    
				left join v_EvnVizitPL EvnVizitPL on EvnVizitPL.EvnVizitPL_id = EP.EvnPrescr_pid
			where
				EP.EvnPrescr_pid  = '890101013847807'
				and EP.PrescriptionType_id = 6
				and EP.PrescriptionStatusType_id != 3
				
			order by
				EPPR.EvnCourse_id,
				EP.EvnPrescr_setDT
		
