
            with MSL as (
                select MSL.MedService_id from v_MedServiceLink MSL
                where  MSL.MedService_lid = '450101000000360'
                       AND MSL.MedServiceLinkType_id =
                ( select MSLT.MedServiceLinkType_id from v_MedServiceLinkType MSLT
                 where MSLT.MedServiceLinkType_Code = '3' limit 1 )
            )
        
            select
                TTMS.TimetableMedService_id as "TimetableMedService_id",
                null as "EvnDirection_id",
                null as "EvnQueue_id",
                null as "Person_Phone",
                null as "EvnFuncRequest_id",
                to_char(TTMS.TimetableMedService_begTime, 'DD.MM.YYYY') as "group_name",
                case when TTMS.TimetableMedService_begTime is not null
                    then to_char(cast(TTMS.TimetableMedService_begTime as timestamp), 'DD.MM.YYYY')
                    else null
                end as "TimetableMedService_begDate",
                COALESCE(to_char(TTMS.TimetableMedService_begTime, 'HH24:MI:SS'),'б/з') as "TimetableMedService_begTime",
                to_char(TTMS.TimetableMedService_begTime, 'DD.MM.YYYY') as "TimetableMedService_Type",
                'false' as "EvnDirection_IsCito",
                null as "EvnDirection_setDT",
                null as "EvnPrescr_setDate",
                null as "EvnDirection_Num",
                null as "Person_id",
                null as "EvnUslugaPar_id",
                (select MSL.MedService_id from MSL limit 1) as "RCC_MedService_id",
                null as "RemoteConsultCenterResearch_id",
                null as "RemoteConsultCenterResearch_status",
                null as "Person_FIO",
                null as "Person_BirthDay",
                'false' as "FuncRequestState",
                '' as "Operator",
                case when UCMS.UslugaComplexMedService_id IS NULL then 'Общее' else UC.UslugaComplex_Name end as "TimetableMedServiceType",
                '' as "EvnFuncRequest_UslugaCache",
                '' as "Lpu_Name",
                '' as "LpuSection_Name",
                '' as "EvnCostPrint_PrintStatus",
                '' as "PersonQuarantine_IsOn",
                null as "PersonQuarantine_begDT"
            FROM v_TimetableMedService_lite TTMS
                LEFT JOIN v_UslugaComplexMedService UCMS  on UCMS.UslugaComplexMedService_id = TTMS.UslugaComplexMedService_id
                left join v_UslugaComplex UC  on UC.UslugaComplex_id = UCMS.UslugaComplex_id
                LEFT JOIN v_MedService MS  ON MS.MedService_id = COALESCE(TTMS.MedService_id, UCMS.MedService_id)
                --LEFT JOIN v_pmUserCache PUC  on PUC.PMUser_id = TTMS.pmUser_insID
            WHERE
                (COALESCE(TTMS.MedService_id, UCMS.MedService_id) = '450101000000360') and (
                (TTMS.TimetableMedService_Day is not null and TTMS.TimetableMedService_Day between 44602 and 44602)
            )
                and TTMS.EvnDirection_id is null
        
            union all
            select
                ttms.TimetableMedService_id as "TimetableMedService_id",
                ED.EvnDirection_id as "EvnDirection_id",
                ED.EvnQueue_id as "EvnQueue_id",
                dbo.getPersonPhones(ed.Person_id, '<br />') as "Person_Phone",
                EFR.EvnFuncRequest_id as "EvnFuncRequest_id",
                null as "group_name",
                case
                    when ES.EvnStatus_SysNick = 'FuncDonerec' then to_char(cast(efr.EvnFuncRequest_statusDate as timestamp), 'DD.MM.YYYY')
                    else null
                end as "TimetableMedService_begDate",
                'б/з' as "TimetableMedService_begTime",
                null as "TimetableMedService_Type",
                case when 2 = COALESCE(ED.EvnDirection_IsCito, 1) then 'true' else 'false' end as "EvnDirection_IsCito",
                to_char(ED.EvnDirection_setDT, 'DD.MM.YYYY') as "EvnDirection_setDT",
                to_char(EPP.EvnPrescrProc_setDT, 'DD.MM.YYYY') as "EvnPrescr_setDate",
                ED.EvnDirection_Num as "EvnDirection_Num",
                ED.Person_id as "Person_id",
                EvnUslugaPar.EvnUslugaPar_id as "EvnUslugaPar_id",
                (select MSL.MedService_id from MSL limit 1) as "RCC_MedService_id",
                RCCR.RemoteConsultCenterResearch_id as "RemoteConsultCenterResearch_id",
                RCCR.RemoteConsultCenterResearch_status as "RemoteConsultCenterResearch_status",
                (PS.Person_SurName || ' ' || COALESCE(PS.Person_FirName, '') || ' ' || COALESCE(PS.Person_SecName,'')) as "Person_FIO",
                to_char(PS.Person_BirthDay, 'DD.MM.YYYY') as "Person_BirthDay",
                case when ES.EvnStatus_SysNick = 'FuncDonerec' and EPP.EvnPrescrProc_didDT is not null then 'true' else 'false' end as "FuncRequestState",
                case
                    when PUC.PMUser_surName is null then ''
                    else PUC.PMUser_surName||' '||COALESCE(left(PUC.PMUser_firName,1),'')||' '||COALESCE(left(PUC.PMUser_secName,1),'')
                end as "Operator",
                'Общее' as "TimetableMedServiceType",
                efr.EvnFuncRequest_UslugaCache as "EvnFuncRequest_UslugaCache",
                LpuFrom.Lpu_Nick as "Lpu_Name",
                LpuSectionFrom.LpuSection_Name as "LpuSection_Name",
                --'' as EvnCostPrint_PrintStatus,
                case when NoPrintCount.NPC > 0 then 'true' else 'false' end as "EvnCostPrint_PrintStatus",
                CASE WHEN PQ.PersonQuarantine_id is not null THEN 'true' ELSE 'false' END as "PersonQuarantine_IsOn",
                to_char(PQ.PersonQuarantine_begDT, 'DD.MM.YYYY') as "PersonQuarantine_begDT"
            FROM v_EvnFuncRequest efr
                inner join v_EvnDirection_all ed  on ed.EvnDirection_id = efr.EvnFuncRequest_pid and ed.EvnDirection_failDT is null
                inner join v_EvnQueue eq  on eq.EvnDirection_id = ed.EvnDirection_id and eq.EvnQueue_failDT is null and eq.EvnQueue_recDT is null
                left join v_TimetableMedService_lite ttms  on ttms.EvnDirection_id = ed.EvnDirection_id
                left join v_Lpu LpuFrom  on LpuFrom.Lpu_id = ED.Lpu_sid
                left join v_LpuSection LpuSectionFrom  on LpuSectionFrom.LpuSection_id = ED.LpuSection_id
                left join v_PersonState PS  on PS.Person_id = ED.Person_id
                left join lateral (
                    select PQ.*
                    from v_PersonQuarantine PQ
                    where PQ.Person_id = PS.Person_id
                    and PQ.PersonQuarantine_endDT is null
                    limit 1
                ) PQ on true
                --left join v_EvnCostPrint ECP  on ECP.Evn_id = efr.EvnFuncRequest_id
                LEFT JOIN LATERAL (
                    select ECP.EvnCostPrint_id as NPC
                    from v_EvnUslugaPar EUP
                    left join v_EvnCostPrint ECP on ECP.Evn_id = EUP.EvnUslugaPar_id
                    where EUP.EvnDirection_id = efr.EvnFuncRequest_pid
                    and COALESCE(ECP.EvnCostPrint_IsNoPrint,0) <> 2
                    limit 1
                ) NoPrintCount ON true
                left join v_EvnStatus ES  on ES.EvnStatus_id = EFR.EvnStatus_id
                LEFT JOIN v_EvnPrescrDirection EPD  on EPD.EvnDirection_id = ED.EvnDirection_id
                LEFT JOIN v_EvnPrescrProc EPP on EPP.EvnPrescrProc_id = EPD.EvnPrescr_id
                LEFT JOIN v_EvnPrescrFuncDiag EPFD  on EPFD.EvnPrescrFuncDiag_id = EPD.EvnPrescr_id
                LEFT JOIN LATERAL(
                    select EvnUslugaPar_id from v_EvnUslugaPar  where EvnDirection_id = ED.EvnDirection_id
                    limit 1
                ) EvnUslugaPar ON true
                LEFT JOIN v_RemoteConsultCenterResearch RCCR  on RCCR.EvnUslugaPar_id = EvnUslugaPar.EvnUslugaPar_id
                LEFT JOIN v_pmUserCache PUC  on PUC.PMUser_id = ed.pmUser_insID
            WHERE
                efr.MedService_id = '450101000000360'
                and (
                    (ES.EvnStatus_SysNick <> 'FuncDonerec')
                    OR
                    (ES.EvnStatus_SysNick = 'FuncDonerec' and CAST(efr.EvnFuncRequest_statusDate as date) between '2022-02-11' and '2022-02-11')
                ) and (
                    TTMS.TimetableMedService_begTime is null
                ) and PS.Person_SurName iLIKE ('Фрицлер'||'%')
        
            union all
            select
                ttms.TimetableMedService_id as "TimetableMedService_id",
                ED.EvnDirection_id as "EvnDirection_id",
                ED.EvnQueue_id as "EvnQueue_id",
                dbo.getPersonPhones(ed.Person_id, '<br />') as "Person_Phone",
                EFR.EvnFuncRequest_id as "EvnFuncRequest_id",
                to_char(TTMS.TimetableMedService_begTime, 'DD.MM.YYYY') as "group_name",
                case
                    when TTMS.TimetableMedService_begTime is not null then to_char(cast(TTMS.TimetableMedService_begTime as timestamp), 'DD.MM.YYYY')
                    when ES.EvnStatus_SysNick = 'FuncDonerec' then to_char(cast(efr.EvnFuncRequest_statusDate as timestamp), 'DD.MM.YYYY')
                    else null
                end as "TimetableMedService_begDate",
                COALESCE(to_char(TTMS.TimetableMedService_begTime, 'HH24:MI:SS'),'б/з') as "TimetableMedService_begTime",
                to_char(TTMS.TimetableMedService_begTime, 'DD.MM.YYYY') as "TimetableMedService_Type",
                case when 2 = COALESCE(ED.EvnDirection_IsCito, 1)
                    or COALESCE(EPP.EvnPrescrProc_IsCito, 1) = 2
                    then 'true'
                    else 'false'
                end as "EvnDirection_IsCito",
                to_char(ED.EvnDirection_setDT, 'DD.MM.YYYY') as "EvnDirection_setDT",
                to_char(EPP.EvnPrescrProc_setDT, 'DD.MM.YYYY') as "EvnPrescr_setDate",
                ED.EvnDirection_Num as "EvnDirection_Num",
                ED.Person_id as "Person_id",
                EvnUslugaPar.EvnUslugaPar_id as "EvnUslugaPar_id",
                (select MSL.MedService_id from MSL limit 1) as "RCC_MedService_id",
                RCCR.RemoteConsultCenterResearch_id as "RemoteConsultCenterResearch_id",
                RCCR.RemoteConsultCenterResearch_status as "RemoteConsultCenterResearch_status",
                (PS.Person_SurName || ' ' || COALESCE(PS.Person_FirName, '') || ' ' || COALESCE(PS.Person_SecName,'')) as "Person_FIO",
                to_char(PS.Person_BirthDay, 'DD.MM.YYYY') as "Person_BirthDay",
                case when ES.EvnStatus_SysNick = 'FuncDonerec' then 'true' else 'false' end as "FuncRequestState",
                case
                    when PUC.PMUser_surName is null then ''
                    else PUC.PMUser_surName||' '||COALESCE(left(PUC.PMUser_firName,1),'')||' '||COALESCE(left(PUC.PMUser_secName,1),'')
                end as "Operator",
                case when UCMS.UslugaComplexMedService_id IS NULL then 'Общее' else UC.UslugaComplex_Name end as "TimetableMedServiceType",
                efr.EvnFuncRequest_UslugaCache as "EvnFuncRequest_UslugaCache",
                LpuFrom.Lpu_Nick as "Lpu_Name",
                LpuSectionFrom.LpuSection_Name as "LpuSection_Name",
                --'' as "EvnCostPrint_PrintStatus",
                case when NoPrintCount.NPC > 0 then 'true' else 'false' end as "EvnCostPrint_PrintStatus",
                CASE WHEN PQ.PersonQuarantine_id is not null THEN 'true' ELSE 'false' END as "PersonQuarantine_IsOn",
                to_char(PQ.PersonQuarantine_begDT, 'DD.MM.YYYY') as "PersonQuarantine_begDT"
            FROM v_EvnFuncRequest efr
                inner join v_EvnDirection_all ed  on ed.EvnDirection_id = efr.EvnFuncRequest_pid and ed.EvnDirection_failDT is null
                inner join v_TimetableMedService_lite ttms  on ttms.EvnDirection_id = ed.EvnDirection_id and ttms.TimetableMedService_begTime is not null
                left join v_Lpu LpuFrom  on LpuFrom.Lpu_id = ED.Lpu_sid
                left join v_LpuSection LpuSectionFrom  on LpuSectionFrom.LpuSection_id = ED.LpuSection_id
                left join v_PersonState PS  on PS.Person_id = ED.Person_id
                left join lateral (
                    select PQ.*
                    from v_PersonQuarantine PQ
                    where PQ.Person_id = PS.Person_id
                    and PQ.PersonQuarantine_endDT is null
                    limit 1
                ) PQ on true
                --left join v_EvnCostPrint ECP  on ECP.Evn_id = efr.EvnFuncRequest_id
                LEFT JOIN LATERAL (
                    select ECP.EvnCostPrint_id as NPC
                    from v_EvnUslugaPar EUP
                    left join v_EvnCostPrint ECP on ECP.Evn_id = EUP.EvnUslugaPar_id
                    where EUP.EvnDirection_id = efr.EvnFuncRequest_pid
                    and COALESCE(ECP.EvnCostPrint_IsNoPrint,0) <> 2
                    limit 1
                ) NoPrintCount ON true
                LEFT JOIN v_UslugaComplexMedService UCMS  on UCMS.UslugaComplexMedService_id = TTMS.UslugaComplexMedService_id
                left join v_UslugaComplex UC  on UC.UslugaComplex_id = UCMS.UslugaComplex_id
                left join v_EvnStatus ES  on ES.EvnStatus_id = EFR.EvnStatus_id
                LEFT JOIN v_EvnPrescrDirection EPD  on EPD.EvnDirection_id = ED.EvnDirection_id
                left join v_EvnPrescrProc EPP on EPP.EvnPrescrProc_id = EPD.EvnPrescr_id
                LEFT JOIN v_EvnPrescrFuncDiag EPFD  on EPFD.EvnPrescrFuncDiag_id = EPD.EvnPrescr_id
                LEFT JOIN LATERAL(
                    select EvnUslugaPar_id from v_EvnUslugaPar  where EvnDirection_id = ED.EvnDirection_id
                    limit 1
                ) EvnUslugaPar ON true
                LEFT JOIN v_RemoteConsultCenterResearch RCCR  on RCCR.EvnUslugaPar_id = EvnUslugaPar.EvnUslugaPar_id
                LEFT JOIN v_pmUserCache PUC on PUC.PMUser_id = ed.pmUser_insID
            WHERE
                efr.MedService_id = '450101000000360'
                and (
                    (TTMS.TimetableMedService_Day between 44602 and 44602)
                ) and PS.Person_SurName iLIKE ('Фрицлер'||'%')
        
            union all
            -- без направления (без EvnDirection, связь с услугой по EvnFuncRequest_pid) по #23048.
            select
                NULL as "TimetableMedService_id",
                null as "EvnDirection_id",
                null as "EvnQueue_id",
                dbo.getPersonPhones(EFR.Person_id, '<br />') as "Person_Phone",
                EFR.EvnFuncRequest_id as "EvnFuncRequest_id",
                null as "group_name",
                case when EvnUslugaPar.EvnUslugaPar_setDT is not null
                    then to_char(cast(EvnUslugaPar.EvnUslugaPar_setDT as timestamp), 'DD.MM.YYYY')
                    else null
                end as "TimetableMedService_begDate",
                'б/н' as "TimetableMedService_begTime",
                null as "TimetableMedService_Type",
                'false' as "EvnDirection_IsCito",
                null as "EvnDirection_setDT",
                null as "EvnPrescr_setDate",
                null as "EvnDirection_Num",
                EFR.Person_id as "Person_id",
                EvnUslugaPar.EvnUslugaPar_id as "EvnUslugaPar_id",
                (select MSL.MedService_id from MSL limit 1) as "RCC_MedService_id",
                RCCR.RemoteConsultCenterResearch_id as "RemoteConsultCenterResearch_id",
                RCCR.RemoteConsultCenterResearch_status as "RemoteConsultCenterResearch_status",
                (PS.Person_SurName || ' ' || COALESCE(PS.Person_FirName, '') || ' ' || COALESCE(PS.Person_SecName,'')) as "Person_FIO",
                to_char(PS.Person_BirthDay, 'DD.MM.YYYY') as "Person_BirthDay",
                case when EvnUslugaPar.EvnUslugaPar_setDT is not null then 'true' else 'false' end as "FuncRequestState",
                case
                    when UC.PMUser_surName is null then ''
                    else UC.PMUser_surName||' '||COALESCE(left(UC.PMUser_firName,1),'')||' '||COALESCE(left(UC.PMUser_secName,1),'')
                end as "Operator",
                '' as "TimetableMedServiceType",
                efr.EvnFuncRequest_UslugaCache as "EvnFuncRequest_UslugaCache",
                '' as "Lpu_Name",
                '' as "LpuSection_Name",
                '' as "EvnCostPrint_PrintStatus",
                CASE WHEN PQ.PersonQuarantine_id is not null THEN 'true' ELSE 'false' END as "PersonQuarantine_IsOn",
                to_char(PQ.PersonQuarantine_begDT, 'DD.MM.YYYY') as "PersonQuarantine_begDT"
            FROM v_EvnFuncRequest EFR
                LEFT JOIN v_EvnStatus ES on ES.EvnStatus_id = EFR.EvnStatus_id
                inner join v_EvnUslugaPar EvnUslugaPar on EvnUslugaPar.EvnUslugaPar_id = EFR.EvnFuncRequest_pid
                LEFT JOIN v_PersonState PS on PS.Person_id = EFR.Person_id
                left join lateral (
                    select
                        PQ.PersonQuarantine_id,
                        PQ.PersonQuarantine_begDT
                    from v_PersonQuarantine PQ
                    where PQ.Person_id = PS.Person_id
                    and PQ.PersonQuarantine_endDT is null
                    limit 1
                ) PQ on true
                LEFT JOIN v_RemoteConsultCenterResearch RCCR on RCCR.EvnUslugaPar_id = EvnUslugaPar.EvnUslugaPar_id
                LEFT JOIN v_pmUserCache UC on UC.PMUser_id = EFR.pmUser_insID
            WHERE
                EFR.Lpu_id = '69'
                and EFR.MedService_id = '450101000000360'
                and PS.Person_SurName iLIKE ('Фрицлер'||'%') and (EFR.EvnFuncRequest_setDT between '2022-02-11' and '2022-02-11'::date + interval '1 day')
            order by 8, 21
        
