select vepld.evndirection_aid as direction_id, veda.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, veda.server_id as server_id,
                          999 as update_id, 'SUCCESS' as update_reason, veda.lpu_id as mo_lpu_id, veda.recmethodtype_id as source_id
          from  v_evnpldispdop13 vepld
            inner join v_evndirection_all veda on
                (vepld.evndirection_aid = veda.evndirection_id  and veda.evndirection_fersenddt is null)
            inner join timetablemedservicehist tmh on  tmh.evndirection_id = veda.evndirection_id
                where vepld.evnpldispdop13_isendstage = 2
                    and ( vepld.evnpldispdop13_upddt >= $1 and vepld.evnpldispdop13_upddt <= $2 )
                    and ( tmh.timetablemedservice_upddt >= $3 and tmh.timetablemedservice_upddt <= $4 )
