
			SELECT
				case when EvnPS.Lpu_id = '9990010252' AND LU.LpuUnitType_SysNick in ('stac','dstac','hstac','pstac','priem') then 'edit' else 'view' end as "accessType",
				case when EvnPS.Lpu_id = '9990010252' AND LU.LpuUnitType_SysNick in ('stac','dstac','hstac','pstac','priem') then 1 else 0 end as "allowUnsign",
				EvnPS.EvnPS_id as "EvnPS_id",
				EvnPS.Diag_id as "Diag_id",
				EvnPS.EvnPS_IsSigned as "EvnPS_IsSigned",
				COALESCE(D.Diag_Code,'') as "Diag_Code", -- основной диагноз последнего движения или приемного отделения
				COALESCE(D.Diag_Name,'') as "Diag_Name",
				EvnPS.Diag_did as "Diag_did",
				COALESCE(DD.Diag_Code,'') as "Diag_d_Code",
				COALESCE(DD.Diag_Name,'') as "Diag_d_Name",
				FM.CureStandart_Count as "CureStandart_Count",
				DFM.DiagFedMes_FileName as "DiagFedMes_FileName",
				COALESCE(to_char(EvnPS.EvnPS_setDT, 'DD.MM.YYYY'),'') as "EvnPS_setDate",
				COALESCE(to_char(EvnPS.EvnPS_setDT, 'HH24:MI'),'') as "EvnPS_setTime",
				to_char(COALESCE(EL.EvnLeave_setDT,COALESCE(EvnPS.EvnPS_disDT,dbo.tzGetDate())), 'DD.MM.YYYY') as "EvnPS_disDate",--дата и время  выписки из стационара, или текущие  дата и время, если данных о выписке нет
				to_char(COALESCE(EL.EvnLeave_setDT,COALESCE(EvnPS.EvnPS_disDT,dbo.tzGetDate())), 'HH24:MI') as "EvnPS_disTime",
				RTRIM(EvnPS.EvnPS_NumCard) as "EvnPS_NumCard",
				EvnPS.EvnPS_CodeConv as "EvnPS_CodeConv",
				EvnPS.EvnPS_NumConv as "EvnPS_NumConv",
				EvnPS.EvnPS_IsCont as "EvnPS_IsCont",-- переведен
				coalesce(OP.Org_name,LSP.LpuSection_name,'') as "Lpu_p_Name",-- откуда переведен
				EvnPS.EvnPS_IsDiagMismatch as "EvnPS_IsDiagMismatch",
				EvnPS.EvnPS_IsWrongCure as "EvnPS_IsWrongCure",
				EvnPS.EvnPS_IsShortVolume as "EvnPS_IsShortVolume",
				EvnPS.EvnPS_IsImperHosp as "EvnPS_IsImperHosp",
				COALESCE(CAST(EvnPS.EvnDirection_Num AS varchar),'') as "EvnDirection_Num",
				COALESCE(to_char(EvnPS.EvnDirection_setDT, 'DD.MM.YYYY'),'') as "EvnDirection_setDate",
				coalesce(OD.Org_name,LSD.LpuSection_name,'') as "Lpu_d_Name", --кем выдано:  ЛПУ, Отделение Организация
				LT.LeaveType_Code as "LeaveType_Code",
				LT.LeaveType_Name as "LeaveType_Name",
				PT.PrehospType_Name as "PrehospType_Name",
				PT.PrehospType_SysNick as "PrehospType_SysNick",
				PA.PrehospArrive_id as "PrehospArrive_id",
				PA.PrehospArrive_Name as "PrehospArrive_Name",
				PA.PrehospArrive_SysNick as "PrehospArrive_SysNick",
				--EvnPS.LpuSection_id,--последнее отделение
				EvnPS.Person_id as "Person_id",
				EvnPS.PersonEvn_id as "PersonEvn_id",
				EvnPS.Server_id as "Server_id",
				BSS.PregnancyResult_id as "PregnancyResult_id",
				BSS.AbortMethod_id as "AbortMethod_id",
				PReg.PersonRegister_id as "PersonRegister_id",
				DM.DeathMotherType_id as "DeathMotherStatus",
				LUT.LpuUnitType_SysNick as "LpuUnitType_SysNick",
				Child.ChildEvn_id as "ChildEvn_id",
				to_char(ecp.EvnCostPrint_setDT, 'DD.MM.YYYY') as "EvnCostPrint_setDT",
				ecp.EvnCostPrint_IsNoPrint as "EvnCostPrint_IsNoPrint",
				(ecp.EvnCostPrint_Cost::numeric(10,2))::text as "CostPrint",
				ESLAST.EvnSection_id as "EvnSection_id",
				case
					when dp.Diag_Code IN ('U07.1', 'U07.2') then 3
					when dd.Diag_Code IN ('U07.1', 'U07.2') then 3
					when exists(
						select
							edps.EvnDiagPS_id
						from
							v_EvnDiagPS edps
							inner join v_Diag d on d.Diag_id = edps.Diag_id 
						where
							edps.EvnDiagPS_rid = EvnPS.EvnPS_id
							and edps.DiagSetType_id in (1, 2, 3)
							and d.Diag_Code IN ('U07.1', 'U07.2')
						limit 1
					) then 3
					else RepositoryObserv.CovidType_id
				end as "CovidType_id",
				case when coalesce(EvnPS.EvnPS_IsArchive, 1) = 1 then 0 else 1 end as "archiveRecord",
				EvnPS.EvnPS_IsActive as "EvnPS_IsActive"
			       
			FROM
				v_EvnPS EvnPS 
				left join v_EvnCostPrint ecp  on ecp.Evn_id = EvnPS.EvnPS_id
				left join v_LpuSection LS  on LS.LpuSection_id = EvnPS.LpuSection_id
				left join v_LpuUnit LUT  on LUT.LpuUnit_id = LS.LpuUnit_id
				left join v_Diag DD  on EvnPS.Diag_did = DD.Diag_id
				left join v_Diag DP  on EvnPS.Diag_pid = DP.Diag_id
				left join v_LeaveType LT  on EvnPS.LeaveType_id = LT.LeaveType_id
				left join v_PrehospType PT  on EvnPS.PrehospType_id = PT.PrehospType_id
				left join v_PrehospArrive PA  on EvnPS.PrehospArrive_id = PA.PrehospArrive_id
				left join v_EvnLeave EL  on EvnPS.EvnPS_id = EL.EvnLeave_pid
				left join v_EvnSection ESLAST  on EvnPS.EvnPS_id = ESLAST.EvnSection_pid and EvnPS.LpuSection_id = ESLAST.LpuSection_id
				left join v_Diag D  on COALESCE(ESLAST.Diag_id,EvnPS.Diag_pid) = D.Diag_id
				left join v_Org OP  on EvnPS.EvnPS_IsCont = 2 AND EvnPS.Org_did = OP.Org_id
				left join v_LpuSection LSP  on EvnPS.EvnPS_IsCont = 2 AND EvnPS.LpuSection_did = LSP.LpuSection_id
				left join Org OD  on EvnPS.Org_did = OD.Org_id
				left join v_LpuSection LSD  on EvnPS.EvnDirection_id is not null AND EvnPS.LpuSection_did = LSD.LpuSection_id
				left join v_PersonState PS  on EvnPS.Person_id = PS.Person_id
				LEFT JOIN LATERAL (
					select
						PersonRegister_id,
						PersonPregnancy_id,
						date_part('day', dbo.tzGetDate() - PersonPregnancy_begMensDate)/7 as PersonPregnancy_begMensDate
					from 
						v_PersonPregnancy 
					where Person_id = PS.Person_id  
					and date_part('day', dbo.tzGetDate() - PersonPregnancy_begMensDate) / 7 < 60
					order by 
						PersonPregnancy_id
					desc 
					limit 1
				) PReg on true
				LEFT JOIN LATERAL (
					select
						PersonRegister_id
					from 
						v_PersonRegister 
					where Person_id = PS.Person_id and PersonRegisterType_id = 2
					order by 
						PersonRegister_id
					desc 
					limit 1
				) PersonRegister on true
				LEFT JOIN LATERAL(
					select
						DeathMotherType_id
					from v_DeathMother
					where PersonRegister_id = coalesce(PReg.PersonRegister_id,PersonRegister.PersonRegister_id)
					ORDER BY DeathMother_id DESC
					LIMIT 1 
				) DM on true
				left join v_BirthSpecStac BSS on BSS.PersonRegister_id = coalesce(PReg.PersonRegister_id,PersonRegister.PersonRegister_id)
				LEFT JOIN LATERAL (
					
		select
			COUNT(cs.CureStandart_id) as CureStandart_Count
		from v_CureStandartDiag csd
			inner join v_CureStandart cs on csd.CureStandart_id = cs.CureStandart_id
		where
			(csd.Diag_id = D.Diag_pid or csd.Diag_id = D.Diag_id)
			and cs.CureStandartAgeGroupType_id in (case when dbo.Age2(PS.Person_BirthDay, COALESCE(EvnPS.EvnPS_setDT,dbo.tzGetDate())) < 18 then 2 else 1 end,3)
			and cast(cs.CureStandart_begDate as date) <= cast(COALESCE(EvnPS.EvnPS_setDT,dbo.tzGetDate()) as date)
			and (cs.CureStandart_endDate is null or cast(cs.CureStandart_endDate as date) > cast(COALESCE(EvnPS.EvnPS_setDT,dbo.tzGetDate()) as date))
		
				) FM ON true
				LEFT JOIN LATERAL (
					
				select null as DiagFedMes_FileName
			
				) DFM ON true
				LEFT JOIN LATERAL(
					select
						Evn_id as ChildEvn_id
					from
						v_Evn E 
						inner join v_EvnSection ES  on E.Evn_pid = ES.EvnSection_id
					where
						ES.EvnSection_pid = '500801005614131'
					limit 1
				) Child ON true
				LEFT JOIN LATERAL (
					select CovidType_id
					from v_RepositoryObserv
					where Evn_id = '500801005614131'
					order by RepositoryObserv_updDT DESC
					limit 1
				) RepositoryObserv ON true
				left join v_MedStaffFact MSF  on MSF.MedStaffFact_id = '62782'
				left join v_LpuUnit LU  on MSF.LpuUnit_id = LU.LpuUnit_id
			
			WHERE
				EvnPS.EvnPS_id = '500801005614131'
			limit 1
		
