mirror of
https://github.com/alextselegidis/easyappointments.git
synced 2024-11-25 17:33:19 +03:00
Fixes #66 thanks to bikimike solution.
This commit is contained in:
parent
561fc8d859
commit
1e31c11341
1 changed files with 116 additions and 101 deletions
|
@ -547,117 +547,132 @@ class Appointments extends CI_Controller {
|
||||||
private function get_provider_available_time_periods($provider_id, $selected_date,
|
private function get_provider_available_time_periods($provider_id, $selected_date,
|
||||||
$exclude_appointments = array()) {
|
$exclude_appointments = array()) {
|
||||||
$this->load->model('appointments_model');
|
$this->load->model('appointments_model');
|
||||||
$this->load->model('providers_model');
|
$this->load->model('providers_model');
|
||||||
|
|
||||||
// Get the provider's working plan and reserved appointments.
|
// Get the provider's working plan and reserved appointments.
|
||||||
$working_plan = json_decode($this->providers_model
|
$working_plan = json_decode($this->providers_model->get_setting('working_plan', $provider_id), true);
|
||||||
->get_setting('working_plan', $provider_id), true);
|
|
||||||
|
|
||||||
$where_clause = array(
|
$where_clause = array(
|
||||||
'DATE(start_datetime)' => date('Y-m-d', strtotime($selected_date)),
|
//'DATE(start_datetime)' => date('Y-m-d', strtotime($selected_date)),
|
||||||
'id_users_provider' => $provider_id
|
'id_users_provider' => $provider_id
|
||||||
);
|
);
|
||||||
|
|
||||||
$reserved_appointments = $this->appointments_model->get_batch($where_clause);
|
$reserved_appointments = $this->appointments_model->get_batch($where_clause);
|
||||||
|
|
||||||
// Sometimes it might be necessary to not take into account some appointment records
|
// Sometimes it might be necessary to not take into account some appointment records
|
||||||
// in order to display what the providers' available time periods would be without them.
|
// in order to display what the providers' available time periods would be without them.
|
||||||
foreach ($exclude_appointments as $excluded_id) {
|
foreach ($exclude_appointments as $excluded_id) {
|
||||||
foreach ($reserved_appointments as $index => $reserved) {
|
foreach ($reserved_appointments as $index => $reserved) {
|
||||||
if ($reserved['id'] == $excluded_id) {
|
if ($reserved['id'] == $excluded_id) {
|
||||||
unset($reserved_appointments[$index]);
|
unset($reserved_appointments[$index]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find the empty spaces on the plan. The first split between the plan is due to
|
// Find the empty spaces on the plan. The first split between the plan is due to
|
||||||
// a break (if exist). After that every reserved appointment is considered to be
|
// a break (if exist). After that every reserved appointment is considered to be
|
||||||
// a taken space in the plan.
|
// a taken space in the plan.
|
||||||
$selected_date_working_plan = $working_plan[strtolower(date('l', strtotime($selected_date)))];
|
$selected_date_working_plan = $working_plan[strtolower(date('l', strtotime($selected_date)))];
|
||||||
$available_periods_with_breaks = array();
|
$available_periods_with_breaks = array();
|
||||||
|
|
||||||
if (isset($selected_date_working_plan['breaks'])) {
|
if (isset($selected_date_working_plan['breaks'])) {
|
||||||
if (count($selected_date_working_plan['breaks'])) {
|
$start = new DateTime($selected_date_working_plan['start']);
|
||||||
foreach($selected_date_working_plan['breaks'] as $index=>$break) {
|
$end = new DateTime($selected_date_working_plan['end']);
|
||||||
// Split the working plan to available time periods that do not
|
$available_periods_with_breaks[] = array(
|
||||||
// contain the breaks in them.
|
'start' => $selected_date_working_plan['start'],
|
||||||
$last_break_index = $index - 1;
|
'end' => $selected_date_working_plan['end']
|
||||||
|
);
|
||||||
|
// Split the working plan to available time periods that do not
|
||||||
|
// contain the breaks in them.
|
||||||
|
foreach($selected_date_working_plan['breaks'] as $index=>$break) {
|
||||||
|
$break_start = new DateTime($break['start']);
|
||||||
|
$break_end = new DateTime($break['end']);
|
||||||
|
if ($break_start < $start)
|
||||||
|
$break_start = $start;
|
||||||
|
if ($break_end > $end)
|
||||||
|
$break_end = $end;
|
||||||
|
if ($break_start >= $break_end)
|
||||||
|
continue;
|
||||||
|
foreach ($available_periods_with_breaks as $key => $open_period)
|
||||||
|
{
|
||||||
|
$s = new DateTime($open_period['start']);
|
||||||
|
$e = new DateTime($open_period['end']);
|
||||||
|
if ($s < $break_end && $break_start < $e) // check for overlap
|
||||||
|
{
|
||||||
|
$changed = FALSE;
|
||||||
|
if ($s < $break_start)
|
||||||
|
{
|
||||||
|
$open_start = $s;
|
||||||
|
$open_end = $break_start;
|
||||||
|
$available_periods_with_breaks[] = array(
|
||||||
|
'start' => $open_start->format("H:i"),
|
||||||
|
'end' => $open_end->format("H:i")
|
||||||
|
);
|
||||||
|
$changed = TRUE;
|
||||||
|
}
|
||||||
|
if ($break_end < $e)
|
||||||
|
{
|
||||||
|
$open_start = $break_end;
|
||||||
|
$open_end = $e;
|
||||||
|
$available_periods_with_breaks[] = array(
|
||||||
|
'start' => $open_start->format("H:i"),
|
||||||
|
'end' => $open_end->format("H:i")
|
||||||
|
);
|
||||||
|
$changed = TRUE;
|
||||||
|
}
|
||||||
|
if ($changed)
|
||||||
|
{
|
||||||
|
unset($available_periods_with_breaks[$key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (count($available_periods_with_breaks) === 0) {
|
// Break the empty periods with the reserved appointments.
|
||||||
$start_hour = $selected_date_working_plan['start'];
|
$available_periods_with_appointments = $available_periods_with_breaks;
|
||||||
$end_hour = $break['start'];
|
|
||||||
} else {
|
|
||||||
$start_hour = $selected_date_working_plan['breaks'][$last_break_index]['end'];
|
|
||||||
$end_hour = $break['start'];
|
|
||||||
}
|
|
||||||
|
|
||||||
$available_periods_with_breaks[] = array(
|
foreach($reserved_appointments as $appointment) {
|
||||||
'start' => $start_hour,
|
foreach($available_periods_with_appointments as $index => &$period) {
|
||||||
'end' => $end_hour
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add the period from the last break to the end of the day.
|
$a_start = strtotime($appointment['start_datetime']);
|
||||||
$available_periods_with_breaks[] = array(
|
$a_end = strtotime($appointment['end_datetime']);
|
||||||
'start' => $selected_date_working_plan['breaks'][$index]['end'],
|
$p_start = strtotime($selected_date . ' ' . $period['start']);
|
||||||
'end' => $selected_date_working_plan['end']
|
$p_end = strtotime($selected_date . ' ' .$period['end']);
|
||||||
);
|
if ($a_start <= $p_start && $a_end <= $p_end && $a_end <= $p_start) {
|
||||||
} else {
|
// The appointment does not belong in this time period, so we
|
||||||
$available_periods_with_breaks[] = array(
|
// will not change anything.
|
||||||
'start' => $selected_date_working_plan['start'],
|
} else if ($a_start <= $p_start && $a_end <= $p_end && $a_end >= $p_start) {
|
||||||
'end' => $selected_date_working_plan['end']
|
// The appointment starts before the period and finishes somewhere inside.
|
||||||
);
|
// We will need to break this period and leave the available part.
|
||||||
}
|
$period['start'] = date('H:i', $a_end);
|
||||||
}
|
} else if ($a_start >= $p_start && $a_end <= $p_end) {
|
||||||
|
// The appointment is inside the time period, so we will split the period
|
||||||
|
// into two new others.
|
||||||
|
unset($available_periods_with_appointments[$index]);
|
||||||
|
$available_periods_with_appointments[] = array(
|
||||||
|
'start' => date('H:i', $p_start),
|
||||||
|
'end' => date('H:i', $a_start)
|
||||||
|
);
|
||||||
|
$available_periods_with_appointments[] = array(
|
||||||
|
'start' => date('H:i', $a_end),
|
||||||
|
'end' => date('H:i', $p_end)
|
||||||
|
);
|
||||||
|
} else if ($a_start >= $p_start && $a_end >= $p_start && $a_start <= $p_end) {
|
||||||
|
// The appointment starts in the period and finishes out of it. We will
|
||||||
|
// need to remove the time that is taken from the appointment.
|
||||||
|
$period['end'] = date('H:i', $a_start);
|
||||||
|
} else if ($a_start >= $p_start && $a_end >= $p_end && $a_start >= $p_end) {
|
||||||
|
// The appointment does not belong in the period so do not change anything.
|
||||||
|
} else if ($a_start <= $p_start && $a_end >= $p_end && $a_start <= $p_end) {
|
||||||
|
// The appointment is bigger than the period, so this period needs to be
|
||||||
|
// removed.
|
||||||
|
unset($available_periods_with_appointments[$index]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Break the empty periods with the reserved appointments.
|
return array_values($available_periods_with_appointments);
|
||||||
$available_periods_with_appointments = $available_periods_with_breaks;
|
|
||||||
|
|
||||||
foreach($reserved_appointments as $appointment) {
|
|
||||||
foreach($available_periods_with_appointments as $index => &$period) {
|
|
||||||
|
|
||||||
$a_start = strtotime($appointment['start_datetime']);
|
|
||||||
$a_end = strtotime($appointment['end_datetime']);
|
|
||||||
$p_start = strtotime($selected_date . ' ' . $period['start']);
|
|
||||||
$p_end = strtotime($selected_date . ' ' .$period['end']);
|
|
||||||
|
|
||||||
if ($a_start <= $p_start && $a_end <= $p_end && $a_end <= $p_start) {
|
|
||||||
// The appointment does not belong in this time period, so we
|
|
||||||
// will not change anything.
|
|
||||||
} else if ($a_start <= $p_start && $a_end <= $p_end && $a_end >= $p_start) {
|
|
||||||
// The appointment starts before the period and finishes somewhere inside.
|
|
||||||
// We will need to break this period and leave the available part.
|
|
||||||
$period['start'] = date('H:i', $a_end);
|
|
||||||
|
|
||||||
} else if ($a_start >= $p_start && $a_end <= $p_end) {
|
|
||||||
// The appointment is inside the time period, so we will split the period
|
|
||||||
// into two new others.
|
|
||||||
unset($available_periods_with_appointments[$index]);
|
|
||||||
$available_periods_with_appointments[] = array(
|
|
||||||
'start' => date('H:i', $p_start),
|
|
||||||
'end' => date('H:i', $a_start)
|
|
||||||
);
|
|
||||||
$available_periods_with_appointments[] = array(
|
|
||||||
'start' => date('H:i', $a_end),
|
|
||||||
'end' => date('H:i', $p_end)
|
|
||||||
);
|
|
||||||
|
|
||||||
} else if ($a_start >= $p_start && $a_end >= $p_start && $a_start <= $p_end) {
|
|
||||||
// The appointment starts in the period and finishes out of it. We will
|
|
||||||
// need to remove the time that is taken from the appointment.
|
|
||||||
$period['end'] = date('H:i', $a_start);
|
|
||||||
|
|
||||||
} else if ($a_start >= $p_start && $a_end >= $p_end && $a_start >= $p_end) {
|
|
||||||
// The appointment does not belong in the period so do not change anything.
|
|
||||||
} else if ($a_start <= $p_start && $a_end >= $p_end && $a_start <= $p_end) {
|
|
||||||
// The appointment is bigger than the period, so this period needs to be
|
|
||||||
// removed.
|
|
||||||
unset($available_periods_with_appointments[$index]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return array_values($available_periods_with_appointments);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue