Integrated the availabilities type in the booking wizard and API controller (#185).

This commit is contained in:
Alex Tselegidis 2016-07-20 22:06:19 +02:00
parent 4d5b3ecad2
commit d5544d0044
2 changed files with 19 additions and 41 deletions

View file

@ -289,6 +289,7 @@ class Appointments extends CI_Controller {
$this->load->model('providers_model'); $this->load->model('providers_model');
$this->load->model('appointments_model'); $this->load->model('appointments_model');
$this->load->model('settings_model'); $this->load->model('settings_model');
$this->load->model('services_model');
try { try {
// Do not continue if there was no provider selected (more likely there is no provider in the system). // Do not continue if there was no provider selected (more likely there is no provider in the system).
@ -313,11 +314,14 @@ class Appointments extends CI_Controller {
} }
} }
$availabilities_type = $this->services_model->get_value('availabilities_type', $_POST['service_id']);
$empty_periods = $this->_get_provider_available_time_periods($_POST['provider_id'], $empty_periods = $this->_get_provider_available_time_periods($_POST['provider_id'],
$_POST['selected_date'], $exclude_appointments); $_POST['selected_date'], $exclude_appointments);
$available_hours = $this->_calculate_available_hours($empty_periods, $_POST['selected_date'], $available_hours = $this->_calculate_available_hours($empty_periods, $_POST['selected_date'],
$_POST['service_duration'], filter_var($_POST['manage_mode'], FILTER_VALIDATE_BOOLEAN)); $_POST['service_duration'], filter_var($_POST['manage_mode'], FILTER_VALIDATE_BOOLEAN),
$availabilities_type);
echo json_encode($available_hours); echo json_encode($available_hours);
@ -502,6 +506,7 @@ class Appointments extends CI_Controller {
// Get the available time periods for every day of this month. // Get the available time periods for every day of this month.
$this->load->model('services_model'); $this->load->model('services_model');
$service_duration = (int)$this->services_model->get_value('duration', $service_id); $service_duration = (int)$this->services_model->get_value('duration', $service_id);
$availabilities_type = (int)$this->services_model->get_value('availabilities_type', $service_id);
for ($i=1; $i<=$number_of_days; $i++) { for ($i=1; $i<=$number_of_days; $i++) {
$current_date = new DateTime($selected_date->format('Y-m') . '-' . $i); $current_date = new DateTime($selected_date->format('Y-m') . '-' . $i);
@ -515,7 +520,7 @@ class Appointments extends CI_Controller {
$current_date->format('Y-m-d')); $current_date->format('Y-m-d'));
$available_hours = $this->_calculate_available_hours($empty_periods, $current_date->format('Y-m-d'), $available_hours = $this->_calculate_available_hours($empty_periods, $current_date->format('Y-m-d'),
$service_duration); $service_duration, false, $availabilities_type);
if (empty($available_hours)) { if (empty($available_hours)) {
$unavailable_dates[] = $current_date->format('Y-m-d'); $unavailable_dates[] = $current_date->format('Y-m-d');
@ -754,7 +759,8 @@ class Appointments extends CI_Controller {
foreach($provider['services'] as $provider_service_id) { foreach($provider['services'] as $provider_service_id) {
if ($provider_service_id == $service_id) { // Check if the provider is available for the requested date. if ($provider_service_id == $service_id) { // Check if the provider is available for the requested date.
$empty_periods = $this->_get_provider_available_time_periods($provider['id'], $selected_date); $empty_periods = $this->_get_provider_available_time_periods($provider['id'], $selected_date);
$available_hours = $this->_calculate_available_hours($empty_periods, $selected_date, $service['duration']); $available_hours = $this->_calculate_available_hours($empty_periods, $selected_date,
$service['duration'], false, $service['availabilities_type']);
if (count($available_hours) > $max_hours_count) { if (count($available_hours) > $max_hours_count) {
$provider_id = $provider['id']; $provider_id = $provider['id'];
$max_hours_count = count($available_hours); $max_hours_count = count($available_hours);
@ -778,11 +784,12 @@ class Appointments extends CI_Controller {
* @param string $selected_date The selected date to be search (format ) * @param string $selected_date The selected date to be search (format )
* @param numeric $service_duration The service duration is required for the hour calculation. * @param numeric $service_duration The service duration is required for the hour calculation.
* @param bool $manage_mode (optional) Whether we are currently on manage mode (editing an existing appointment). * @param bool $manage_mode (optional) Whether we are currently on manage mode (editing an existing appointment).
* @param string $availlabilities_type Optional ('flexible'), the service availabilities type.
* *
* @return array Returns an array with the available hours for the appointment. * @return array Returns an array with the available hours for the appointment.
*/ */
protected function _calculate_available_hours(array $empty_periods, $selected_date, $service_duration, protected function _calculate_available_hours(array $empty_periods, $selected_date, $service_duration,
$manage_mode = FALSE) { $manage_mode = FALSE, $availabilities_type = 'flexible') {
$this->load->model('settings_model'); $this->load->model('settings_model');
$available_hours = array(); $available_hours = array();
@ -790,29 +797,14 @@ class Appointments extends CI_Controller {
foreach ($empty_periods as $period) { foreach ($empty_periods as $period) {
$start_hour = new DateTime($selected_date . ' ' . $period['start']); $start_hour = new DateTime($selected_date . ' ' . $period['start']);
$end_hour = new DateTime($selected_date . ' ' . $period['end']); $end_hour = new DateTime($selected_date . ' ' . $period['end']);
$interval = $availabilities_type === AVAILABILITIES_TYPE_FIXED ? (int)$service_duration : 15;
$minutes = $start_hour->format('i');
if ($minutes % 15 != 0) {
// Change the start hour of the current space in order to be
// on of the following: 00, 15, 30, 45.
if ($minutes < 15) {
$start_hour->setTime($start_hour->format('H'), 15);
} else if ($minutes < 30) {
$start_hour->setTime($start_hour->format('H'), 30);
} else if ($minutes < 45) {
$start_hour->setTime($start_hour->format('H'), 45);
} else {
$start_hour->setTime($start_hour->format('H') + 1, 00);
}
}
$current_hour = $start_hour; $current_hour = $start_hour;
$diff = $current_hour->diff($end_hour); $diff = $current_hour->diff($end_hour);
while (($diff->h * 60 + $diff->i) >= intval($service_duration)) { while (($diff->h * 60 + $diff->i) >= intval($service_duration)) {
$available_hours[] = $current_hour->format('H:i'); $available_hours[] = $current_hour->format('H:i');
$current_hour->add(new DateInterval("PT15M")); $current_hour->add(new DateInterval('PT' . $interval . 'M'));
$diff = $current_hour->diff($end_hour); $diff = $current_hour->diff($end_hour);
} }
} }

View file

@ -59,7 +59,7 @@ class Availabilities extends API_V1_Controller {
$date->format('Y-m-d'), []); $date->format('Y-m-d'), []);
$availableHours = $this->_calculateAvailableHours($emptyPeriods, $availableHours = $this->_calculateAvailableHours($emptyPeriods,
$date->format('Y-m-d'), $service['duration'], false); $date->format('Y-m-d'), $service['duration'], false, $service['availabilities_type']);
$this->output $this->output
->set_content_type('application/json') ->set_content_type('application/json')
@ -231,11 +231,12 @@ class Availabilities extends API_V1_Controller {
* @param string $selected_date The selected date to be search (format ) * @param string $selected_date The selected date to be search (format )
* @param numeric $service_duration The service duration is required for the hour calculation. * @param numeric $service_duration The service duration is required for the hour calculation.
* @param bool $manage_mode (optional) Whether we are currently on manage mode (editing an existing appointment). * @param bool $manage_mode (optional) Whether we are currently on manage mode (editing an existing appointment).
* @param string $availlabilities_type Optional ('flexible'), the service availabilities type.
* *
* @return array Returns an array with the available hours for the appointment. * @return array Returns an array with the available hours for the appointment.
*/ */
protected function _calculateAvailableHours(array $empty_periods, $selected_date, $service_duration, protected function _calculateAvailableHours(array $empty_periods, $selected_date, $service_duration,
$manage_mode = FALSE) { $manage_mode = FALSE, $availabilities_type = 'flexible') {
$this->load->model('settings_model'); $this->load->model('settings_model');
$available_hours = array(); $available_hours = array();
@ -243,29 +244,14 @@ class Availabilities extends API_V1_Controller {
foreach ($empty_periods as $period) { foreach ($empty_periods as $period) {
$start_hour = new DateTime($selected_date . ' ' . $period['start']); $start_hour = new DateTime($selected_date . ' ' . $period['start']);
$end_hour = new DateTime($selected_date . ' ' . $period['end']); $end_hour = new DateTime($selected_date . ' ' . $period['end']);
$interval = $availabilities_type === AVAILABILITIES_TYPE_FIXED ? (int)$service_duration : 15;
$minutes = $start_hour->format('i');
if ($minutes % 15 != 0) {
// Change the start hour of the current space in order to be
// on of the following: 00, 15, 30, 45.
if ($minutes < 15) {
$start_hour->setTime($start_hour->format('H'), 15);
} else if ($minutes < 30) {
$start_hour->setTime($start_hour->format('H'), 30);
} else if ($minutes < 45) {
$start_hour->setTime($start_hour->format('H'), 45);
} else {
$start_hour->setTime($start_hour->format('H') + 1, 00);
}
}
$current_hour = $start_hour; $current_hour = $start_hour;
$diff = $current_hour->diff($end_hour); $diff = $current_hour->diff($end_hour);
while (($diff->h * 60 + $diff->i) >= intval($service_duration)) { while (($diff->h * 60 + $diff->i) >= intval($service_duration)) {
$available_hours[] = $current_hour->format('H:i'); $available_hours[] = $current_hour->format('H:i');
$current_hour->add(new DateInterval("PT15M")); $current_hour->add(new DateInterval('PT' . $interval . 'M'));
$diff = $current_hour->diff($end_hour); $diff = $current_hour->diff($end_hour);
} }
} }