<?php

if ($doctorId > 0 && $visit->detectionid != 999 && $visit->detectionid != 9999 && $visit->detectionid != -88) {
    $departs = $this->getOrderDepts();
    if (in_array($visit->for_department, $departs)) {
        $data = R::getRow('SELECT MAX(visit_order) as last_order
                 FROM visits  where visitdate = ? and for_doctor = ?   ', [$visitDate, $doctorId]);
        $last_order = $data['last_order'];
        $visit->visit_order = $last_order + 1;
        R::store($visit);
    }
}