
            SELECT
                et.ElectronicTalon_id as "ElectronicTalon_id",
                et.ElectronicTalon_Num as "ElectronicTalon_Num",
                et.Person_id as "Person_id",
                et.ElectronicTalonStatus_id as "ElectronicTalonStatus_id",
                ets.ElectronicTalonStatus_Name as "ElectronicTalonStatus_Name",
                et.ElectronicService_id as "ElectronicService_id",
                ed.EvnQueue_id as "EvnQueue_id",
                ed.EvnStatus_id as "EvnStatus_id",
                ed.EvnDirection_Num as "EvnDirection_Num",
                ed.EvnDirection_id as "EvnDirection_id",
                et.ElectronicTalon_insDT as "ElectronicTalon_insDT",
                eth.ElectronicTalon_ProcessedTime as "ElectronicTalon_ProcessedTime",
                et.ElectronicTreatment_id as "ElectronicTreatment_id",
                etre.ElectronicTreatment_Name as "ElectronicTreatment_Name",
                to_char(ElectronicTalon_insDT, 'HH24:MI:SS') as "ElectronicTalon_Time",
                to_char(ElectronicTalon_insDT, 'dd.mm.yyyy') as "ElectronicTalon_Date",
                case when et.Person_id is not null then
                    case
                        when pu.pmUser_id is not null then rtrim(pu.pmUser_Name)
                        else 'Запись через интернет'
                    end
                end as "pmUser_Name",
                et.pmUser_updId as "pmUser_updId",
                et.pmUser_insId as "pmUser_insId",
                rtrim(rtrim(p.Person_Surname) || ' ' || coalesce(rtrim(p.Person_Firname),'') || ' ' || coalesce(rtrim(p.Person_Secname),'')) as "Person_FIO",
                case when ed.EvnDirection_isAuto != 2 then 'true' else 'false' end as "IsEvnDirection"
            FROM
                v_ElectronicTalon et
                inner join lateral (
                    SELECT
                        EvnDirection_id, EvnQueue_id, EvnStatus_id, EvnDirection_Num, EvnDirection_isAuto
                    FROM
                        v_EvnDirection_all
                    WHERE
                        et.EvnDirection_id = EvnDirection_id AND DirType_id = 24

                    UNION

                    SELECT
                        EvnDirection_id, EvnQueue_id, EvnStatus_id, EvnDirection_Num, EvnDirection_isAuto
                    FROM
                        v_EvnDirection_all
                    WHERE
                        et.EvnDirection_uid = EvnDirection_id AND DirType_id = 24
                ) ed on true
                inner join v_EvnQueue evnq on (evnq.EvnDirection_id = ed.EvnDirection_id)
                inner join v_ElectronicQueueInfo eqi on eqi.MedService_id = evnq.MedService_did
                inner join v_ElectronicService es on es.ElectronicQueueInfo_id = eqi.ElectronicQueueInfo_id
                left join v_ElectronicTalonStatus ets on ets.ElectronicTalonStatus_id = et.ElectronicTalonStatus_id
                left join lateral (
                    select
                        pmUser_id,
                        pmUser_Name
                    from v_pmUser
                    where pmUser_id = et.pmUser_updId
                    limit 1
                ) pu on true
                left join v_PersonState_all p on p.Person_id = et.Person_id
                left join v_ElectronicTreatment etre on etre.ElectronicTreatment_id = et.ElectronicTreatment_id
                left join lateral (
                    select
                        to_char(etho.ElectronicTalonHist_insDT, 'HH24:MI:SS') as ElectronicTalon_ProcessedTime
                    from
                        v_ElectronicTalonHist etho
                    where
                        (1=1)
                        and etho.ElectronicTalon_id = et.ElectronicTalon_id
                        and etho.ElectronicTalonStatus_id = et.ElectronicTalonStatus_id
                    order by ElectronicTalonHist_id desc
                    limit 1
                ) as eth on true
            WHERE
                (1 = 1) and ElectronicTalon_insDT between '2022-03-21 00:00:00' and '2022-03-21 23:59:59'
                and es.ElectronicService_id = '890101000000017'
                -- показываем талоны которые еще никем не обработаны, либо только талоны своего пункта обслуживания
                and (et.ElectronicService_id is null or et.ElectronicService_id = '890101000000017')
            
