
            Select
                EvnDirection.Lpu_id as "Lpu_id",
                EvnDirection.Diag_id as "Diag_id",
                EvnDirection.EvnDirection_pid as "EvnDirection_pid",
                EvnDirection.EvnDirection_id as "EvnDirection_id",
                EvnDirection.EvnDirection_Num as "EvnDirection_Num",
                DirType.DirType_id as "DirType_id",
                Rtrim(DirType.DirType_Name) as "DirType_Name",
                RTrim(COALESCE(to_char(cast(EvnDirection.EvnDirection_setDate as timestamp(3)),'dd.mm.yyyy'),'')) as "EvnDirection_setDT",
                RTrim(Lpu.Lpu_Nick) as "Lpu_Nick",
                LpuUnit.LpuUnit_id as "LpuUnit_id",
                Rtrim(LpuUnit.LpuUnit_Name) as "LpuUnit_Name",
                LpuSectionProfile.LpuSectionProfile_id as "LpuSectionProfile_id",
                RTrim(LpuSectionProfile.LpuSectionProfile_Code) ||'.'|| RTrim(LpuSectionProfile.LpuSectionProfile_Name) as "LpuSectionProfile_Name",
                case
                    when ttms.TimetableMedService_id is not null
                        then to_char (ttms.TimetableMedService_begTime, 'dd.mm.yyyy')||' '||to_char (ttms.TimetableMedService_begTime, 'HH24:MM')
                    when ttg.TimetableGraf_id is not null
                        then to_char (ttg.TimetableGraf_begTime, 'dd.mm.yyyy')||' '||to_char (ttg.TimetableGraf_begTime, 'HH24:MM')
                    when tts.TimetableStac_id is not null and ttp.TimetablePar_id is null
                        then to_char (tts.TimetableStac_setDate, 'dd.mm.yyyy')
                    when (ttp.TimetablePar_id is not null and tts.TimetableStac_id is null)
                        then to_char (ttp.TimetablePar_begTime, 'dd.mm.yyyy')||' '||to_char (ttp.TimetablePar_begTime, 'HH24:MM')
                    when (ttp.TimetablePar_id is not null and tts.TimetableStac_id is not null)
                        then to_char (ttp.TimetablePar_begTime, 'dd.mm.yyyy')||' '||to_char (ttp.TimetablePar_begTime, 'HH24:MM') ||'На койку:'||to_char (tts.TimetableStac_setDate, 'dd.mm.yyyy')
                    when EVK.EvnVK_id is not null
                        then to_char (EVK.EvnVK_setDate, 'dd.mm.yyyy')
                    when EQ.EvnQueue_id is not null
                        then case when EUP.EvnUslugaPar_setDT is null then 'В очереди с '|| coalesce(to_char (EQ.EvnQueue_setDate, 'dd.mm.yyyy'),'') else to_char (EUP.EvnUslugaPar_setDT, 'dd.mm.yyyy')||' '||to_char (EUP.EvnUslugaPar_setDT, 'HH24:MM') end
                    when coalesce(TTG.TimetableGraf_id, TTMS.TimetableMedService_id, TTS.TimetableStac_id, EQ.EvnQueue_id) is null
                        then 'Направление выписано ' || to_char (EvnDirection.EvnDirection_setDT, 'dd.mm.yyyy')
                    else ''
                end as "RecDate",
                to_char (EvnDirection.EvnDirection_statusDate, 'dd.mm.yyyy') as "EvnDirection_statusDate",
                EvnDirection.EvnStatus_id as "EvnStatus_id",
                EvnStatus.EvnStatus_Name as "EvnStatus_Name",
                PrescriptionType.PrescriptionType_Code as "PrescriptionType_Code"
                
            from v_EvnDirection_all EvnDirection
            left join v_Lpu Lpu on Lpu.Lpu_id=EvnDirection.Lpu_did
            left join v_Diag Diag on Diag.Diag_id = EvnDirection.Diag_id
            --left join v_Lpu Lpu on Lpu.Lpu_id=EvnDirection.Lpu_did
            left join v_AccessRightsOrg ARO on Lpu.Org_id = ARO.Org_id
            left join v_LpuUnit LpuUnit on LpuUnit.LpuUnit_id=EvnDirection.LpuUnit_did
            left join v_DirType DirType on DirType.DirType_id=EvnDirection.DirType_id
            left join v_LpuSectionProfile LpuSectionProfile on LpuSectionProfile.LpuSectionProfile_id=EvnDirection.LpuSectionProfile_id
            left join v_EvnPrescrDirection EvnPr on EvnPr.EvnDirection_id=EvnDirection.EvnDirection_id
            left join v_EvnPrescr EvnPrescr on EvnPr.EvnPrescr_id=EvnPrescr.EvnPrescr_id
            left join v_PrescriptionType PrescriptionType on EvnPrescr.PrescriptionType_id=PrescriptionType.PrescriptionType_id
            LEFT JOIN LATERAL (
                Select TimetableGraf_id as TimetableGraf_id, MedStaffFact_id as MedStaffFact_id, TimetableGraf_begTime as TimetableGraf_begTime from v_TimetableGraf_lite TTG where TTG.EvnDirection_id = EvnDirection.EvnDirection_id LIMIT 1
            ) TTG ON TRUE
             -- службы и параклиника
            LEFT JOIN LATERAL (
                Select TimetableMedService_id as TimetableMedService_id, TimetableMedService_begTime as TimetableMedService_begTime from v_TimetableMedService_lite TTMS where TTMS.EvnDirection_id = EvnDirection.EvnDirection_id LIMIT 1
            ) TTMS ON TRUE
             -- стац
            LEFT JOIN LATERAL (
                Select TimetableStac_id as TimetableStac_id, LpuSection_id as LpuSection_id, TimetableStac_setDate as TimetableStac_setDate from v_TimetableStac_lite TTS where TTS.EvnDirection_id = EvnDirection.EvnDirection_id LIMIT 1
            ) TTS ON TRUE
            left join TimetablePar ttp on EvnDirection.TimetablePar_id = ttp.TimetablePar_id
             -- очередь
            LEFT JOIN LATERAL (
                Select
                    EQ.EvnQueue_id as EvnQueue_id,
                    EQ.EvnQueue_failDT as EvnQueue_failDT,
                    EQ.EvnQueue_setDate as EvnQueue_setDate
                from
                    v_EvnQueue EQ
                where EQ.EvnDirection_id = EvnDirection.EvnDirection_id
                    and EQ.EvnQueue_recDT is null
                LIMIT 1
            ) EQ ON TRUE
            -- заказанная услуга для параклиники
            LEFT JOIN LATERAL(
                select
                    EvnUslugaPar_setDT as EvnUslugaPar_setDT
                from
                    v_EvnUslugaPar EUP
                where
                    EvnDirection_id = EvnDirection.EvnDirection_id
                LIMIT 1
            ) EUP ON TRUE
            -- назначение на ВК в очереди
            LEFT JOIN LATERAL(
                select
                    EVK.Evn_id as EvnVK_id,
                    EVK.Evn_setDT as EvnVK_setDate
                from
                    EvnVK EVK
                    inner join Evn on EVK.Evn_id = Evn.Evn_id and Evn.Evn_deleted = 1
                    inner join v_EvnPrescrVK EPVK on EPVK.EvnPrescrVK_id = EVK.EvnPrescrVK_id
                where
                    EPVK.EvnQueue_id = EQ.EvnQueue_id
                LIMIT 1
            ) EVK ON TRUE
            left join EvnStatus on EvnStatus.EvnStatus_id = EvnDirection.EvnStatus_id
            where
                (1=1)  and EvnDirection.Person_id = '890101000724247' and EvnDirection.EvnDirection_pid is null and (coalesce(EvnDirection.EvnDirection_IsAuto, 1) = 1 and 
        (
            coalesce(EvnDirection.EvnStatus_id,0) not in (15)
            OR exists (
                select epd.EvnDirection_id as EvnDirection_id from v_EvnPrescrDirection epd
                inner join v_EvnPrescr EP on epd.EvnPrescr_id = EP.EvnPrescr_id
                where EvnDirection.EvnDirection_id = epd.EvnDirection_id
                and coalesce(EP.EvnPrescr_IsExec, 1) = 1 LIMIT 1
            )
        )) and EvnDirection.EvnDirection_failDT is null
        and EvnDirection.DirFailType_id is null
        and coalesce(EvnDirection.EvnStatus_id,0) not in (12,13)
        and EQ.EvnQueue_failDT is null
         and coalesce(Diag.Diag_Code, '') not in('Z11.3') and coalesce(Diag.Diag_Code, '') not between 'A50.0' and 'A64'
                and (
                    ARO.AccessRightsOrg_id is null or (
                        ARO.AccessRightsOrg_id is not null and (
                            exists(
                                select
                                    AccessRightsLimit_id as "AccessRightsLimit_id"
                                from v_AccessRightsLimit ARL
                                left join v_AccessRightsName ARN on arn.AccessRightsName_id = arl.AccessRightsName_id
                                where
                                    ARO.AccessRightsName_id = ARL.AccessRightsName_id and
                                    ARN.AccessRightsType_id = 2 and (
                                        ARL.Org_id = 13102587 or
                                        ARL.AccessRightsType_User = '372591147258' or
                                        ARL.AccessRightsType_UserGroups in ('LpuUser','TubRegistry','HIVRegistry','NarkoRegistry','HepatitisRegistry')  or
                                        ARL.Post_id in (
                                            select
                                                Post_id
                                            from MedPersonalCache MPC
                                                left join PersonState PS on PS.person_id = MPC.person_id
                                            where
                                                MPC.MedPersonal_id = '890101000002038'
                                        )
                                    )
                                limit 1
                            ) or
                            EvnDirection.Lpu_did = '13101856' or
                            EvnDirection.MedPersonal_id = '890101000002038'
                        )
                    )
                )

            --order by EvnDirection.EvnDirection_setDate DESC
        
