2020-04-22 22:48:56 +03:00
|
|
|
<?php defined('BASEPATH') or exit('No direct script access allowed');
|
2016-07-19 10:52:20 +03:00
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Easy!Appointments - Open Source Web Scheduler
|
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2020-03-11 12:10:59 +03:00
|
|
|
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
|
2020-11-14 22:36:25 +03:00
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link https://easyappointments.org
|
2016-07-19 10:52:20 +03:00
|
|
|
* @since v1.2.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
require_once __DIR__ . '/API_V1_Controller.php';
|
|
|
|
require_once __DIR__ . '/../../Appointments.php';
|
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
use EA\Engine\Types\UnsignedInteger;
|
2016-07-19 10:52:20 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Availabilities Controller
|
|
|
|
*
|
2020-09-23 12:47:18 +03:00
|
|
|
* @property CI_Session $session
|
|
|
|
* @property CI_Loader $load
|
|
|
|
* @property CI_Input $input
|
|
|
|
* @property CI_Output $output
|
|
|
|
* @property CI_Config $config
|
|
|
|
* @property CI_Lang $lang
|
|
|
|
* @property CI_Cache $cache
|
|
|
|
* @property CI_DB_query_builder $db
|
|
|
|
* @property CI_Security $security
|
|
|
|
* @property Google_Sync $google_sync
|
|
|
|
* @property Ics_file $ics_file
|
2020-11-13 12:00:57 +03:00
|
|
|
* @property Appointments_model $appointments_model
|
|
|
|
* @property Providers_model $providers_model
|
|
|
|
* @property Services_model $services_model
|
|
|
|
* @property Customers_model $customers_model
|
|
|
|
* @property Settings_model $settings_model
|
2020-09-23 12:47:18 +03:00
|
|
|
* @property Timezones $timezones
|
2020-11-13 12:00:57 +03:00
|
|
|
* @property Roles_model $roles_model
|
|
|
|
* @property Secretaries_model $secretaries_model
|
|
|
|
* @property Admins_model $admins_model
|
|
|
|
* @property User_model $user_model
|
2020-04-22 22:48:56 +03:00
|
|
|
*
|
2016-07-19 10:52:20 +03:00
|
|
|
* @package Controllers
|
|
|
|
*/
|
|
|
|
class Availabilities extends API_V1_Controller {
|
|
|
|
/**
|
|
|
|
* Class Constructor
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function __construct()
|
|
|
|
{
|
2016-07-19 10:52:20 +03:00
|
|
|
parent::__construct();
|
|
|
|
$this->load->model('appointments_model');
|
|
|
|
$this->load->model('providers_model');
|
2016-07-21 23:36:17 +03:00
|
|
|
$this->load->model('services_model');
|
2016-07-19 10:52:20 +03:00
|
|
|
$this->load->model('settings_model');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET API Method
|
|
|
|
*
|
|
|
|
* Provide the "providerId", "serviceId" and "date" GET parameters to get the availabilities for a specific date.
|
|
|
|
* If no "date" was provided then the current date will be used.
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function get()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-10-21 21:49:05 +03:00
|
|
|
$provider_id = new UnsignedInteger($this->input->get('providerId'));
|
|
|
|
$service_id = new UnsignedInteger($this->input->get('serviceId'));
|
2016-07-19 10:52:20 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($this->input->get('date'))
|
|
|
|
{
|
2016-07-21 23:36:17 +03:00
|
|
|
$date = new DateTime($this->input->get('date'));
|
2018-01-23 12:08:37 +03:00
|
|
|
}
|
|
|
|
else
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2016-07-19 10:52:20 +03:00
|
|
|
$date = new DateTime();
|
|
|
|
}
|
|
|
|
|
2020-10-21 21:49:05 +03:00
|
|
|
$provider = $this->providers_model->get_row($provider_id->get());
|
|
|
|
$service = $this->services_model->get_row($service_id->get());
|
2016-07-19 10:52:20 +03:00
|
|
|
|
2020-10-21 21:49:05 +03:00
|
|
|
$empty_periods = $this->get_provider_available_time_periods($provider_id->get(),
|
2017-09-15 14:36:37 +03:00
|
|
|
$date->format('Y-m-d'), []);
|
2016-07-21 23:36:17 +03:00
|
|
|
|
2020-10-21 21:49:05 +03:00
|
|
|
$available_hours = $this->calculate_available_hours($empty_periods,
|
2017-09-15 14:36:37 +03:00
|
|
|
$date->format('Y-m-d'), $service['duration'], FALSE, $service['availabilities_type']);
|
2016-07-19 10:52:20 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($service['attendants_number'] > 1)
|
|
|
|
{
|
2020-10-21 21:49:05 +03:00
|
|
|
$available_hours = $this->get_multiple_attendants_hours($date->format('Y-m-d'), $service, $provider);
|
2016-07-21 23:36:17 +03:00
|
|
|
}
|
|
|
|
|
2020-10-21 21:49:05 +03:00
|
|
|
// If the selected date is today, remove past hours. It is important include the timeout before booking
|
|
|
|
// that is set in the back-office the system. Normally we might want the customer to book an appointment
|
|
|
|
// that is at least half or one hour from now. The setting is stored in minutes.
|
2018-01-02 14:55:52 +03:00
|
|
|
if ($date->format('Y-m-d') === date('Y-m-d'))
|
|
|
|
{
|
2020-10-21 21:49:05 +03:00
|
|
|
$book_advance_timeout = $this->settings_model->get_setting('book_advance_timeout');
|
2018-01-02 14:55:52 +03:00
|
|
|
|
2020-10-21 21:49:05 +03:00
|
|
|
foreach ($available_hours as $index => $value)
|
2018-01-02 14:55:52 +03:00
|
|
|
{
|
2020-10-21 21:49:05 +03:00
|
|
|
$available_hour = strtotime($value);
|
|
|
|
$currentHour = strtotime('+' . $book_advance_timeout . ' minutes', strtotime('now'));
|
|
|
|
if ($available_hour <= $currentHour)
|
2018-01-02 14:55:52 +03:00
|
|
|
{
|
2020-10-21 21:49:05 +03:00
|
|
|
unset($available_hours[$index]);
|
2018-01-02 14:55:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 21:49:05 +03:00
|
|
|
$available_hours = array_values($available_hours);
|
|
|
|
sort($available_hours, SORT_STRING);
|
|
|
|
$available_hours = array_values($available_hours);
|
2018-01-02 14:55:52 +03:00
|
|
|
|
2016-07-19 10:52:20 +03:00
|
|
|
$this->output
|
2016-07-21 23:36:17 +03:00
|
|
|
->set_content_type('application/json')
|
2020-10-21 21:49:05 +03:00
|
|
|
->set_output(json_encode($available_hours));
|
2017-09-23 02:30:22 +03:00
|
|
|
}
|
2020-04-22 22:48:56 +03:00
|
|
|
catch (Exception $exception)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-10-21 21:49:05 +03:00
|
|
|
exit($this->handle_exception($exception));
|
2016-07-21 23:36:17 +03:00
|
|
|
}
|
2016-07-19 10:52:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an array containing the free time periods (start - end) of a selected date.
|
|
|
|
*
|
|
|
|
* This method is very important because there are many cases where the system needs to
|
2016-10-10 19:29:48 +03:00
|
|
|
* know when a provider is available for an appointment. This method will return an array
|
2016-07-19 10:52:20 +03:00
|
|
|
* that belongs to the selected date and contains values that have the start and the end
|
|
|
|
* time of an available time period.
|
|
|
|
*
|
2017-09-06 16:22:11 +03:00
|
|
|
* @param int $provider_id The provider's record id.
|
2016-07-19 10:52:20 +03:00
|
|
|
* @param string $selected_date The date to be checked (MySQL formatted string).
|
|
|
|
* @param array $exclude_appointments This array contains the ids of the appointments that
|
|
|
|
* will not be taken into consideration when the available time periods are calculated.
|
|
|
|
*
|
|
|
|
* @return array Returns an array with the available time periods of the provider.
|
|
|
|
*/
|
2020-10-21 21:49:05 +03:00
|
|
|
protected function get_provider_available_time_periods(
|
2017-09-15 14:36:37 +03:00
|
|
|
$provider_id,
|
|
|
|
$selected_date,
|
|
|
|
$exclude_appointments = []
|
2020-04-22 22:48:56 +03:00
|
|
|
)
|
|
|
|
{
|
2016-07-19 10:52:20 +03:00
|
|
|
$this->load->model('appointments_model');
|
|
|
|
$this->load->model('providers_model');
|
|
|
|
|
|
|
|
// Get the provider's working plan and reserved appointments.
|
|
|
|
$working_plan = json_decode($this->providers_model->get_setting('working_plan', $provider_id), TRUE);
|
|
|
|
|
2020-10-20 16:03:48 +03:00
|
|
|
// Get the provider's working plan exceptions.
|
|
|
|
$working_plan_exceptions = json_decode($this->providers_model->get_setting('working_plan_exceptions', $provider_id), TRUE);
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$where_clause = [
|
2016-07-19 10:52:20 +03:00
|
|
|
'id_users_provider' => $provider_id
|
2017-09-15 14:36:37 +03:00
|
|
|
];
|
2016-07-19 10:52:20 +03:00
|
|
|
|
|
|
|
$reserved_appointments = $this->appointments_model->get_batch($where_clause);
|
|
|
|
|
|
|
|
// 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.
|
2017-09-15 14:36:37 +03:00
|
|
|
foreach ($exclude_appointments as $excluded_id)
|
|
|
|
{
|
|
|
|
foreach ($reserved_appointments as $index => $reserved)
|
|
|
|
{
|
|
|
|
if ($reserved['id'] == $excluded_id)
|
|
|
|
{
|
2016-07-19 10:52:20 +03:00
|
|
|
unset($reserved_appointments[$index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 16:03:48 +03:00
|
|
|
// 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 taken space in the plan.
|
2016-07-19 10:52:20 +03:00
|
|
|
$selected_date_working_plan = $working_plan[strtolower(date('l', strtotime($selected_date)))];
|
2020-10-20 16:03:48 +03:00
|
|
|
|
|
|
|
if (isset($working_plan_exceptions[$selected_date]))
|
|
|
|
{
|
|
|
|
$selected_date_working_plan = $working_plan_exceptions[$selected_date];
|
|
|
|
}
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$available_periods_with_breaks = [];
|
2016-07-19 10:52:20 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if (isset($selected_date_working_plan['breaks']))
|
|
|
|
{
|
2016-07-19 10:52:20 +03:00
|
|
|
$start = new DateTime($selected_date_working_plan['start']);
|
|
|
|
$end = new DateTime($selected_date_working_plan['end']);
|
2017-09-15 14:36:37 +03:00
|
|
|
$available_periods_with_breaks[] = [
|
2016-07-19 10:52:20 +03:00
|
|
|
'start' => $selected_date_working_plan['start'],
|
|
|
|
'end' => $selected_date_working_plan['end']
|
2017-09-15 14:36:37 +03:00
|
|
|
];
|
2016-07-19 10:52:20 +03:00
|
|
|
|
|
|
|
// Split the working plan to available time periods that do not contain the breaks in them.
|
2017-09-15 14:36:37 +03:00
|
|
|
foreach ($selected_date_working_plan['breaks'] as $index => $break)
|
|
|
|
{
|
2016-07-19 10:52:20 +03:00
|
|
|
$break_start = new DateTime($break['start']);
|
|
|
|
$break_end = new DateTime($break['end']);
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($break_start < $start)
|
|
|
|
{
|
2016-07-19 10:52:20 +03:00
|
|
|
$break_start = $start;
|
|
|
|
}
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($break_end > $end)
|
|
|
|
{
|
2016-07-19 10:52:20 +03:00
|
|
|
$break_end = $end;
|
|
|
|
}
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($break_start >= $break_end)
|
|
|
|
{
|
2016-07-19 10:52:20 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
foreach ($available_periods_with_breaks as $key => $open_period)
|
|
|
|
{
|
2016-07-19 10:52:20 +03:00
|
|
|
$s = new DateTime($open_period['start']);
|
|
|
|
$e = new DateTime($open_period['end']);
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($s < $break_end && $break_start < $e)
|
|
|
|
{ // check for overlap
|
2016-07-19 10:52:20 +03:00
|
|
|
$changed = FALSE;
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($s < $break_start)
|
|
|
|
{
|
2016-07-19 10:52:20 +03:00
|
|
|
$open_start = $s;
|
|
|
|
$open_end = $break_start;
|
2017-09-15 14:36:37 +03:00
|
|
|
$available_periods_with_breaks[] = [
|
2020-10-20 16:03:48 +03:00
|
|
|
'start' => $open_start->format('H:i'),
|
|
|
|
'end' => $open_end->format('H:i')
|
2017-09-15 14:36:37 +03:00
|
|
|
];
|
2016-07-19 10:52:20 +03:00
|
|
|
$changed = TRUE;
|
|
|
|
}
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($break_end < $e)
|
|
|
|
{
|
2016-07-19 10:52:20 +03:00
|
|
|
$open_start = $break_end;
|
|
|
|
$open_end = $e;
|
2017-09-15 14:36:37 +03:00
|
|
|
$available_periods_with_breaks[] = [
|
2020-10-20 16:03:48 +03:00
|
|
|
'start' => $open_start->format('H:i'),
|
|
|
|
'end' => $open_end->format('H:i')
|
2017-09-15 14:36:37 +03:00
|
|
|
];
|
2016-07-19 10:52:20 +03:00
|
|
|
$changed = TRUE;
|
|
|
|
}
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($changed)
|
|
|
|
{
|
2016-07-19 10:52:20 +03:00
|
|
|
unset($available_periods_with_breaks[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Break the empty periods with the reserved appointments.
|
|
|
|
$available_periods_with_appointments = $available_periods_with_breaks;
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
foreach ($reserved_appointments as $appointment)
|
|
|
|
{
|
|
|
|
foreach ($available_periods_with_appointments as $index => &$period)
|
|
|
|
{
|
2016-07-19 10:52:20 +03:00
|
|
|
$a_start = strtotime($appointment['start_datetime']);
|
2017-09-15 14:36:37 +03:00
|
|
|
$a_end = strtotime($appointment['end_datetime']);
|
|
|
|
$p_start = strtotime($selected_date . ' ' . $period['start']);
|
|
|
|
$p_end = strtotime($selected_date . ' ' . $period['end']);
|
2016-07-19 10:52:20 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($a_start <= $p_start && $a_end <= $p_end && $a_end <= $p_start)
|
|
|
|
{
|
2016-07-19 10:52:20 +03:00
|
|
|
// The appointment does not belong in this time period, so we
|
|
|
|
// will not change anything.
|
2018-01-23 12:08:37 +03:00
|
|
|
}
|
|
|
|
else
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
|
|
|
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);
|
2018-01-23 12:08:37 +03:00
|
|
|
}
|
|
|
|
else
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
|
|
|
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[] = [
|
|
|
|
'start' => date('H:i', $p_start),
|
|
|
|
'end' => date('H:i', $a_start)
|
|
|
|
];
|
|
|
|
$available_periods_with_appointments[] = [
|
|
|
|
'start' => date('H:i', $a_end),
|
|
|
|
'end' => date('H:i', $p_end)
|
|
|
|
];
|
2018-01-23 12:08:37 +03:00
|
|
|
}
|
|
|
|
else
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
|
|
|
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);
|
2018-01-23 12:08:37 +03:00
|
|
|
}
|
|
|
|
else
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
|
|
|
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.
|
2018-01-23 12:08:37 +03:00
|
|
|
}
|
|
|
|
else
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-19 10:52:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_values($available_periods_with_appointments);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-10-10 19:29:48 +03:00
|
|
|
* Calculate the available appointment hours.
|
2016-07-19 10:52:20 +03:00
|
|
|
*
|
|
|
|
* Calculate the available appointment hours for the given date. The empty spaces
|
|
|
|
* are broken down to 15 min and if the service fit in each quarter then a new
|
|
|
|
* available hour is added to the "$available_hours" array.
|
|
|
|
*
|
|
|
|
* @param array $empty_periods Contains the empty periods as generated by the
|
2016-07-19 10:54:48 +03:00
|
|
|
* "_getProviderAvailableTimePeriods" method.
|
2016-07-19 10:52:20 +03:00
|
|
|
* @param string $selected_date The selected date to be search (format )
|
2017-09-06 16:22:11 +03:00
|
|
|
* @param int $service_duration The service duration is required for the hour calculation.
|
2016-07-19 10:52:20 +03:00
|
|
|
* @param bool $manage_mode (optional) Whether we are currently on manage mode (editing an existing appointment).
|
2016-10-10 19:29:48 +03:00
|
|
|
* @param string $availabilities_type Optional ('flexible'), the service availabilities type.
|
2016-07-19 10:52:20 +03:00
|
|
|
*
|
|
|
|
* @return array Returns an array with the available hours for the appointment.
|
|
|
|
*/
|
2020-10-21 21:49:05 +03:00
|
|
|
protected function calculate_available_hours(
|
2017-09-15 14:36:37 +03:00
|
|
|
array $empty_periods,
|
|
|
|
$selected_date,
|
|
|
|
$service_duration,
|
|
|
|
$manage_mode = FALSE,
|
|
|
|
$availabilities_type = 'flexible'
|
2020-04-22 22:48:56 +03:00
|
|
|
)
|
|
|
|
{
|
2016-07-19 10:52:20 +03:00
|
|
|
$this->load->model('settings_model');
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$available_hours = [];
|
2016-07-19 10:52:20 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
foreach ($empty_periods as $period)
|
|
|
|
{
|
2016-07-19 10:52:20 +03:00
|
|
|
$start_hour = new DateTime($selected_date . ' ' . $period['start']);
|
|
|
|
$end_hour = new DateTime($selected_date . ' ' . $period['end']);
|
2016-07-21 23:36:17 +03:00
|
|
|
$interval = $availabilities_type === AVAILABILITIES_TYPE_FIXED ? (int)$service_duration : 15;
|
2016-07-19 10:52:20 +03:00
|
|
|
|
|
|
|
$current_hour = $start_hour;
|
|
|
|
$diff = $current_hour->diff($end_hour);
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
while (($diff->h * 60 + $diff->i) >= intval($service_duration))
|
|
|
|
{
|
2016-07-19 10:52:20 +03:00
|
|
|
$available_hours[] = $current_hour->format('H:i');
|
2016-07-20 23:06:19 +03:00
|
|
|
$current_hour->add(new DateInterval('PT' . $interval . 'M'));
|
2016-07-19 10:52:20 +03:00
|
|
|
$diff = $current_hour->diff($end_hour);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-02 14:55:52 +03:00
|
|
|
return $available_hours;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get multiple attendants hours.
|
|
|
|
*
|
2020-09-23 12:24:42 +03:00
|
|
|
* This method will add the additional appointment hours whenever a service accepts multiple attendants.
|
2018-01-02 14:55:52 +03:00
|
|
|
*
|
|
|
|
* @param string $selected_date The selected appointment date.
|
|
|
|
* @param array $service Selected service data.
|
|
|
|
* @param array $provider Selected provider data.
|
|
|
|
*
|
2020-09-23 12:24:42 +03:00
|
|
|
* @return array Returns the available hours array.
|
2018-01-02 14:55:52 +03:00
|
|
|
*/
|
2020-10-21 21:49:05 +03:00
|
|
|
protected function get_multiple_attendants_hours(
|
2018-01-02 14:55:52 +03:00
|
|
|
$selected_date,
|
|
|
|
$service,
|
|
|
|
$provider
|
2020-04-22 22:48:56 +03:00
|
|
|
)
|
|
|
|
{
|
2018-01-02 14:55:52 +03:00
|
|
|
$this->load->model('appointments_model');
|
|
|
|
$this->load->model('services_model');
|
|
|
|
$this->load->model('providers_model');
|
|
|
|
|
|
|
|
$unavailabilities = $this->appointments_model->get_batch([
|
|
|
|
'is_unavailable' => TRUE,
|
|
|
|
'DATE(start_datetime)' => $selected_date,
|
|
|
|
'id_users_provider' => $provider['id']
|
|
|
|
]);
|
|
|
|
|
|
|
|
$working_plan = json_decode($provider['settings']['working_plan'], TRUE);
|
|
|
|
$working_day = strtolower(date('l', strtotime($selected_date)));
|
|
|
|
$working_hours = $working_plan[$working_day];
|
|
|
|
|
|
|
|
$periods = [
|
|
|
|
[
|
|
|
|
'start' => new DateTime($selected_date . ' ' . $working_hours['start']),
|
|
|
|
'end' => new DateTime($selected_date . ' ' . $working_hours['end'])
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
2020-10-21 21:49:05 +03:00
|
|
|
$periods = $this->remove_breaks($selected_date, $periods, $working_hours['breaks']);
|
|
|
|
$periods = $this->remove_unavailabilities($periods, $unavailabilities);
|
2018-01-02 14:55:52 +03:00
|
|
|
|
|
|
|
$hours = [];
|
|
|
|
|
|
|
|
$interval_value = $service['availabilities_type'] == AVAILABILITIES_TYPE_FIXED ? $service['duration'] : '15';
|
|
|
|
$interval = new DateInterval('PT' . (int)$interval_value . 'M');
|
|
|
|
$duration = new DateInterval('PT' . (int)$service['duration'] . 'M');
|
|
|
|
|
|
|
|
foreach ($periods as $period)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2018-01-02 14:55:52 +03:00
|
|
|
$slot_start = clone $period['start'];
|
|
|
|
$slot_end = clone $slot_start;
|
|
|
|
$slot_end->add($duration);
|
2016-07-19 10:52:20 +03:00
|
|
|
|
2018-01-02 14:55:52 +03:00
|
|
|
while ($slot_end <= $period['end'])
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2018-01-02 14:55:52 +03:00
|
|
|
// Check reserved attendants for this time slot and see if current attendants fit.
|
|
|
|
$appointment_attendants_number = $this->appointments_model->get_attendants_number_for_period($slot_start,
|
|
|
|
$slot_end, $service['id']);
|
|
|
|
|
|
|
|
if ($appointment_attendants_number < $service['attendants_number'])
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2018-01-02 14:55:52 +03:00
|
|
|
$hours[] = $slot_start->format('H:i');
|
2016-07-19 10:52:20 +03:00
|
|
|
}
|
2018-01-02 14:55:52 +03:00
|
|
|
|
|
|
|
$slot_start->add($interval);
|
|
|
|
$slot_end->add($interval);
|
2016-07-19 10:52:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-02 14:55:52 +03:00
|
|
|
return $hours;
|
2016-07-19 10:52:20 +03:00
|
|
|
}
|
2016-07-21 23:36:17 +03:00
|
|
|
|
|
|
|
/**
|
2018-01-02 14:55:52 +03:00
|
|
|
* Remove breaks from available time periods.
|
2016-07-21 23:36:17 +03:00
|
|
|
*
|
2018-01-02 14:55:52 +03:00
|
|
|
* @param string $selected_date Selected data (Y-m-d format).
|
|
|
|
* @param array $periods Time periods of the current date.
|
|
|
|
* @param array $breaks Breaks array for the current date.
|
2016-07-21 23:36:17 +03:00
|
|
|
*
|
2018-01-02 14:55:52 +03:00
|
|
|
* @return array Returns the available time periods without the breaks.
|
2016-07-21 23:36:17 +03:00
|
|
|
*/
|
2020-10-21 21:49:05 +03:00
|
|
|
public function remove_breaks($selected_date, $periods, $breaks)
|
2018-01-02 14:55:52 +03:00
|
|
|
{
|
|
|
|
if ( ! $breaks)
|
|
|
|
{
|
|
|
|
return $periods;
|
|
|
|
}
|
2016-07-21 23:36:17 +03:00
|
|
|
|
2018-01-02 14:55:52 +03:00
|
|
|
foreach ($breaks as $break)
|
|
|
|
{
|
|
|
|
$break_start = new DateTime($selected_date . ' ' . $break['start']);
|
|
|
|
$break_end = new DateTime($selected_date . ' ' . $break['end']);
|
|
|
|
|
|
|
|
foreach ($periods as &$period)
|
|
|
|
{
|
|
|
|
$period_start = $period['start'];
|
|
|
|
$period_end = $period['end'];
|
|
|
|
|
|
|
|
if ($break_start <= $period_start && $break_end >= $period_start && $break_end <= $period_end)
|
|
|
|
{
|
|
|
|
// left
|
|
|
|
$period['start'] = $break_end;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($break_start >= $period_start && $break_start <= $period_end && $break_end >= $period_start && $break_end <= $period_end)
|
|
|
|
{
|
|
|
|
// middle
|
|
|
|
$period['end'] = $break_start;
|
|
|
|
$periods[] = [
|
|
|
|
'start' => $break_end,
|
|
|
|
'end' => $period_end
|
|
|
|
];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($break_start >= $period_start && $break_start <= $period_end && $break_end >= $period_end)
|
|
|
|
{
|
|
|
|
// right
|
|
|
|
$period['end'] = $break_start;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($break_start <= $period_start && $break_end >= $period_end)
|
|
|
|
{
|
|
|
|
// break contains period
|
|
|
|
$period['start'] = $break_end;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $periods;
|
|
|
|
}
|
2016-07-21 23:36:17 +03:00
|
|
|
|
2018-01-02 14:55:52 +03:00
|
|
|
/**
|
|
|
|
* Remove the unavailabilities from the available time periods of the selected date.
|
|
|
|
*
|
|
|
|
* @param array $periods Available time periods.
|
|
|
|
* @param array $unavailabilities Unavailabilities of the current date.
|
|
|
|
*
|
|
|
|
* @return array Returns the available time periods without the unavailabilities.
|
|
|
|
*/
|
2020-10-21 21:49:05 +03:00
|
|
|
public function remove_unavailabilities($periods, $unavailabilities)
|
2018-01-02 14:55:52 +03:00
|
|
|
{
|
|
|
|
foreach ($unavailabilities as $unavailability)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2018-01-02 14:55:52 +03:00
|
|
|
$unavailability_start = new DateTime($unavailability['start_datetime']);
|
|
|
|
$unavailability_end = new DateTime($unavailability['end_datetime']);
|
|
|
|
|
|
|
|
foreach ($periods as &$period)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2018-01-02 14:55:52 +03:00
|
|
|
$period_start = $period['start'];
|
|
|
|
$period_end = $period['end'];
|
|
|
|
|
|
|
|
if ($unavailability_start <= $period_start && $unavailability_end >= $period_start && $unavailability_end <= $period_end)
|
|
|
|
{
|
|
|
|
// left
|
|
|
|
$period['start'] = $unavailability_end;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($unavailability_start >= $period_start && $unavailability_start <= $period_end && $unavailability_end >= $period_start && $unavailability_end <= $period_end)
|
|
|
|
{
|
|
|
|
// middle
|
|
|
|
$period['end'] = $unavailability_start;
|
|
|
|
$periods[] = [
|
|
|
|
'start' => $unavailability_end,
|
|
|
|
'end' => $period_end
|
|
|
|
];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($unavailability_start >= $period_start && $unavailability_start <= $period_end && $unavailability_end >= $period_end)
|
|
|
|
{
|
|
|
|
// right
|
|
|
|
$period['end'] = $unavailability_start;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($unavailability_start <= $period_start && $unavailability_end >= $period_end)
|
|
|
|
{
|
|
|
|
// Unavaibility contains period
|
|
|
|
$period['start'] = $unavailability_end;
|
|
|
|
continue;
|
|
|
|
}
|
2016-07-21 23:36:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-02 14:55:52 +03:00
|
|
|
return $periods;
|
2016-07-21 23:36:17 +03:00
|
|
|
}
|
2016-07-19 10:52:20 +03:00
|
|
|
}
|