
			
			with UslugaPid (
				EvnUsluga_id
			) as (
				select EvnUsluga_id from v_EvnUsluga EU
				where  '450101249317210' = EU.EvnUsluga_pid
				and (EU.EvnClass_SysNick in ('EvnUslugaCommon', 'EvnUslugaOper', 'EvnUslugaStom' /*, 'EvnUslugaOnkoChem', 'EvnUslugaOnkoBeam', 'EvnUslugaOnkoGormun', 'EvnUslugaOnkoChemBeam', 'EvnUslugaOnkoSurg'*/ ) OR (EU.EvnClass_SysNick ilike 'EvnUslugaPar' and EU.EvnUsluga_setDate is not null))
				and COALESCE(EU.EvnUsluga_IsVizitCode, 1) = 1
			)
			
			select
			  case when 1=1 and COALESCE(EV.EvnVizit_IsPaid, 1) = 1 and COALESCE(ES.EvnSection_IsPaid, 1) = 1 
			and case
				when EU.Lpu_id = '60' then 1
				
				when cast(0 as boolean) = TRUE then 1
				else 0
			end = 1
		 and (COALESCE(EV.MedPersonal_id, ES.MedPersonal_id) in (450101000011920)
		or exists(
					select WG.WorkGraph_id
						from v_WorkGraph WG
						inner join v_MedStaffFact WG_MSF on WG_MSF.MedStaffFact_id = WG.MedStaffFact_id
						where (
							CAST(WG.WorkGraph_begDT as date) <= CAST(dbo.tzGetDate() as date)
							and CAST(WG.WorkGraph_endDT as date) >= CAST(dbo.tzGetDate() as date)
						)
						and WG_MSF.MedPersonal_id in (450101000011920)
			 	)
		or (ES.MedPersonal_id is null and EV.MedPersonal_id is null)
			-- то же отделение в движении или посещении, что и у текущего врача
			or EV.LpuSection_id = '450101000003856' or ES.LpuSection_id = '450101000003856'
		) then 'edit' else 'view' end as "accessType",
			  
			 EU.EvnUsluga_id as "EvnUsluga_id"
			,EU.EvnUsluga_pid as "EvnUsluga_pid"
			,RTRIM(EU.EvnClass_SysNick) as "EvnClass_SysNick"
			,to_char (EU.EvnUsluga_setDate, 'dd.mm.yyyy') as "EvnUsluga_setDate"
			,COALESCE(to_char(EU.EvnUsluga_setTime, 'HH24:mi'),'') as "EvnUsluga_setTime"
			,COALESCE(Usluga.Usluga_Code, UC.UslugaComplex_Code) as "Usluga_Code"
			,COALESCE(Usluga.Usluga_Name, UC.UslugaComplex_Name) as "Usluga_Name"
			,ROUND(cast(EU.EvnUsluga_Kolvo as numeric), 2) as "EvnUsluga_Kolvo"
			,ROUND(cast(EU.EvnUsluga_Price as numeric), 2) as "EvnUsluga_Price"
			,ROUND(cast(EU.EvnUsluga_Summa as numeric), 2) as "EvnUsluga_Summa"
			,PT.PayType_id as "PayType_id"
			,COALESCE(PT.PayType_SysNick, '') as "PayType_SysNick"
			,UC.UslugaComplex_id as "UslugaComplex_id"
			,UCA.Usluga_DI_count as "Usluga_DI_count"
			,UCAA.Usluga_PP_count as "Usluga_PP_count"
		
			  
			from
			  
			UslugaPid
			inner join v_EvnUsluga EU on UslugaPid.EvnUsluga_id = EU.Evnusluga_id
			left join lateral (select * from  v_Evn EvnParent where EvnParent.Evn_id = EU.EvnUsluga_pid limit 1) EvnParent on true
			left join lateral (select * from  v_EvnPrescr EP where EP.EvnPrescr_id = EU.EvnPrescr_id limit 1) EP on true
			left join lateral (select * from  v_Usluga Usluga where Usluga.Usluga_id = EU.Usluga_id limit 1) Usluga on true
			left join lateral (select * from  v_UslugaComplex UC where UC.UslugaComplex_id = EU.UslugaComplex_id limit 1) UC on true
			left join lateral (select * from  v_EvnVizit EV where EV.EvnVizit_id = EU.EvnUsluga_pid limit 1) EV on true
			left join lateral (select * from  v_EvnSection ES where ES.EvnSection_id = EU.EvnUsluga_pid limit 1) ES on true
			left join v_PayType PT on PT.PayType_id = EU.PayType_id
			left join lateral (
				select count(*) as Usluga_DI_count from v_UslugaComplexAttribute UCA
					left join v_UslugaComplexAttributeType UCAT on UCAT.uslugacomplexattributetype_id = UCA.uslugacomplexattributetype_id
				where
					UCA.UslugaComplex_id = UC.UslugaComplex_id
					and UCAT.UslugaComplexAttributeType_code = 290
				group by
					UCA.UslugaComplex_id
			) UCA on true
			left join lateral (
				select count(*) as Usluga_PP_count from v_UslugaComplexAttribute UCAA
				left join v_UslugaComplexAttributeType UCAT on UCAT.uslugacomplexattributetype_id = UCAA.uslugacomplexattributetype_id
				where
					UCAA.UslugaComplex_id = UC.UslugaComplex_id
					and UCAT.UslugaComplexAttributeType_Code = 291
				group by
					UCAA.UslugaComplex_id
			) UCAA on true
		
			where
			  (1=1)
				and COALESCE(EU.EvnUsluga_IsVizitCode, 1) = 1
			 and (EvnParent.EvnClass_SysNick <> 'EvnUslugaPar' or EvnParent.EvnClass_SysNick is null)

			
		
