select ed.evndirection_id as direction_id, ed.evndirection_num as direction_num, tmh.timetablemedservice_id as booking_id,
                  tmh.person_id as person_id, tmh.timetablemedservice_begtime as booking_date,
                      tmh.medservice_id as medservice_id, tmh.uslugacomplexmedservice_id as usluga_complex_id,
                        vesh.evnstatuscause_id as update_id, vesc.evnstatuscause_name as update_reason, ed.server_id as server_id,
                        ed.lpu_id as mo_lpu_id,ed.recmethodtype_id as source_id
        from v_evndirection_all ed
            inner join v_evnstatus ves on (ves.evnstatus_id = ed.evnstatus_id and ves.evnstatus_sysnick in ('Canceled','Declined') )
                inner join v_evnstatushistory vesh on ( vesh.evn_id = ed.evndirection_id and vesh.evnstatus_id = ed.evnstatus_id )
                    inner join v_evnstatuscause vesc  on vesc.evnstatuscause_id = vesh.evnstatuscause_id
                        inner join timetablemedservicehist tmh on  tmh.evndirection_id = ed.evndirection_id
          where ed.evndirection_fersenddt is null and
          ( ed.evndirection_statusdate >= $1 and ed.evndirection_statusdate <= $2 )
          and
          ( tmh.timetablemedservice_upddt >= $3 and tmh.timetablemedservice_upddt <= $4 )
              and ed.recmethodtype_id is not null
