From d5544d00449c92169e5a3209b63c486ce6c861dd Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Wed, 20 Jul 2016 22:06:19 +0200 Subject: [PATCH] Integrated the availabilities type in the booking wizard and API controller (#185). --- src/application/controllers/Appointments.php | 36 ++++++++----------- .../controllers/api/v1/Availabilities.php | 24 +++---------- 2 files changed, 19 insertions(+), 41 deletions(-) diff --git a/src/application/controllers/Appointments.php b/src/application/controllers/Appointments.php index d6de49b9..007e39d1 100755 --- a/src/application/controllers/Appointments.php +++ b/src/application/controllers/Appointments.php @@ -289,6 +289,7 @@ class Appointments extends CI_Controller { $this->load->model('providers_model'); $this->load->model('appointments_model'); $this->load->model('settings_model'); + $this->load->model('services_model'); try { // 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'], $_POST['selected_date'], $exclude_appointments); $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); @@ -501,7 +505,8 @@ class Appointments extends CI_Controller { // Get the available time periods for every day of this month. $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++) { $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')); $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)) { $unavailable_dates[] = $current_date->format('Y-m-d'); @@ -754,7 +759,8 @@ class Appointments extends CI_Controller { foreach($provider['services'] as $provider_service_id) { 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); - $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) { $provider_id = $provider['id']; $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 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 string $availlabilities_type Optional ('flexible'), the service availabilities type. * * @return array Returns an array with the available hours for the appointment. */ 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'); $available_hours = array(); @@ -790,29 +797,14 @@ class Appointments extends CI_Controller { foreach ($empty_periods as $period) { $start_hour = new DateTime($selected_date . ' ' . $period['start']); $end_hour = new DateTime($selected_date . ' ' . $period['end']); - - $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); - } - } + $interval = $availabilities_type === AVAILABILITIES_TYPE_FIXED ? (int)$service_duration : 15; $current_hour = $start_hour; $diff = $current_hour->diff($end_hour); while (($diff->h * 60 + $diff->i) >= intval($service_duration)) { $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 --git a/src/application/controllers/api/v1/Availabilities.php b/src/application/controllers/api/v1/Availabilities.php index 3a01c349..8b3e983b 100644 --- a/src/application/controllers/api/v1/Availabilities.php +++ b/src/application/controllers/api/v1/Availabilities.php @@ -59,7 +59,7 @@ class Availabilities extends API_V1_Controller { $date->format('Y-m-d'), []); $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 ->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 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 string $availlabilities_type Optional ('flexible'), the service availabilities type. * * @return array Returns an array with the available hours for the appointment. */ protected function _calculateAvailableHours(array $empty_periods, $selected_date, $service_duration, - $manage_mode = FALSE) { + $manage_mode = FALSE, $availabilities_type = 'flexible') { $this->load->model('settings_model'); $available_hours = array(); @@ -243,29 +244,14 @@ class Availabilities extends API_V1_Controller { foreach ($empty_periods as $period) { $start_hour = new DateTime($selected_date . ' ' . $period['start']); $end_hour = new DateTime($selected_date . ' ' . $period['end']); - - $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); - } - } + $interval = $availabilities_type === AVAILABILITIES_TYPE_FIXED ? (int)$service_duration : 15; $current_hour = $start_hour; $diff = $current_hour->diff($end_hour); while (($diff->h * 60 + $diff->i) >= intval($service_duration)) { $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); } }