Integrated the multiple attendants in the availabilities generation (#109).

This commit is contained in:
Alex Tselegidis 2016-07-21 22:36:17 +02:00
parent 6d081ad471
commit 8f12b459d0
3 changed files with 135 additions and 23 deletions

View file

@ -315,6 +315,7 @@ class Appointments extends CI_Controller {
}
$availabilities_type = $this->services_model->get_value('availabilities_type', $_POST['service_id']);
$attendants_number = $this->services_model->get_value('attendants_number', $_POST['service_id']);
$empty_periods = $this->_get_provider_available_time_periods($_POST['provider_id'],
$_POST['selected_date'], $exclude_appointments);
@ -323,6 +324,11 @@ class Appointments extends CI_Controller {
$_POST['service_duration'], filter_var($_POST['manage_mode'], FILTER_VALIDATE_BOOLEAN),
$availabilities_type);
if ($attendants_number > 1) {
$this->_get_multiple_attendants_hours($available_hours, $attendants_number, $_POST['service_id'],
$_POST['selected_date']);
}
echo json_encode($available_hours);
} catch(Exception $exc) {
@ -548,6 +554,7 @@ class Appointments extends CI_Controller {
*/
protected function _check_datetime_availability() {
$this->load->model('services_model');
$this->load->model('appointments_model');
$appointment = $_POST['post_data']['appointment'];
@ -555,6 +562,22 @@ class Appointments extends CI_Controller {
$exclude_appointments = (isset($appointment['id'])) ? array($appointment['id']) : array();
$attendants_number = $this->services_model->get_value('attendants_number', $appointment['id_services']);
if ($attendants_number > 1) {
// Exclude all the appointments that will are currently registered.
$exclude = $this->appointments_model->get_batch([
'id_services' => $appointment['id_services'],
'start_datetime' => $appointment['start_datetime']
]);
if (!empty($exclude) && count($exclude) < $attendants_number) {
foreach ($exclude as $entry) {
$exclude_appointments[] = $entry['id'];
}
}
}
if ($appointment['id_users_provider'] === ANY_PROVIDER) {
$appointment['id_users_provider'] = $this->_search_any_provider($appointment['id_services'],
date('Y-m-d', strtotime($appointment['start_datetime'])));
@ -830,6 +853,40 @@ class Appointments extends CI_Controller {
return $available_hours;
}
/**
* Get multiple attendants hours.
*
* This method will add the extra appointment hours whenever a service accepts multiple attendants.
*
* @param array $available_hours The previously calculated appointment hours.
* @param int $attendants_number Service attendants number.
* @param int $service_id Selected service ID.
* @param string $selected_date The selected appointment date.
*/
protected function _get_multiple_attendants_hours(&$available_hours, $attendants_number, $service_id,
$selected_date) {
$this->load->model('appointments_model');
$appointments = $this->appointments_model->get_batch(
'id_services = ' . $this->db->escape($service_id) . ' AND DATE(start_datetime) = DATE('
. $this->db->escape(date('Y-m-d', strtotime($selected_date))) . ')');
$hours = [];
foreach($appointments as $appointment) {
$hour = date('H:i', strtotime($appointment['start_datetime']));
$current_attendants_number = $this->appointments_model->appointment_count_for_hour($service_id,
$selected_date, $hour);
if ($current_attendants_number < $attendants_number && !in_array($hour, $available_hours)) {
$available_hours[] = $hour;
}
}
$available_hours = array_values($available_hours);
sort($available_hours, SORT_STRING );
$available_hours = array_values($available_hours);
}
}
/* End of file appointments.php */

View file

@ -61,6 +61,11 @@ class Availabilities extends API_V1_Controller {
$availableHours = $this->_calculateAvailableHours($emptyPeriods,
$date->format('Y-m-d'), $service['duration'], false, $service['availabilities_type']);
if ($service['attendants_number'] > 1) {
$this->_getMultipleAttendantsHours($availableHours, $service['attendants_number'], $service['id'],
$date->format('Y-m-d'));
}
$this->output
->set_content_type('application/json')
->set_output(json_encode($availableHours));
@ -277,7 +282,41 @@ class Availabilities extends API_V1_Controller {
return $available_hours;
}
/**
* Get multiple attendants hours.
*
* This method will add the extra appointment hours whenever a service accepts multiple attendants.
*
* @param array $available_hours The previously calculated appointment hours.
* @param int $attendants_number Service attendants number.
* @param int $service_id Selected service ID.
* @param string $selected_date The selected appointment date.
*/
protected function _getMultipleAttendantsHours(&$available_hours, $attendants_number, $service_id,
$selected_date) {
$this->load->model('appointments_model');
$appointments = $this->appointments_model->get_batch(
'id_services = ' . $this->db->escape($service_id) . ' AND DATE(start_datetime) = DATE('
. $this->db->escape(date('Y-m-d', strtotime($selected_date))) . ')');
$hours = [];
foreach($appointments as $appointment) {
$hour = date('H:i', strtotime($appointment['start_datetime']));
$current_attendants_number = $this->appointments_model->appointment_count_for_hour($service_id,
$selected_date, $hour);
if ($current_attendants_number < $attendants_number && !in_array($hour, $available_hours)) {
$available_hours[] = $hour;
}
}
$available_hours = array_values($available_hours);
sort($available_hours, SORT_STRING );
$available_hours = array_values($available_hours);
}
}
/* End of file Appointments.php */
/* Location: ./application/controllers/api/v1/Appointments.php */
/* End of file Availabilities.php */
/* Location: ./application/controllers/api/v1/Availabilities.php */

View file

@ -393,6 +393,22 @@ class Appointments_Model extends CI_Model {
$this->db->update('ea_appointments', array('id_google_calendar' => NULL),
array('id_users_provider' => $provider_id));
}
/**
* Get appointment count for the provided start datetime.
*
* @param int $service_id Selected service ID.
* @param string $selected_date Selected date string.
* @param string $hour Selected hour string.
*
* @return int Returns the appointment number at the selected start time.
*/
public function appointment_count_for_hour($service_id, $selected_date, $hour) {
return $this->db->get_where('ea_appointments', [
'id_services' => $service_id,
'start_datetime' => date('Y-m-d H:i:s', strtotime($selected_date . ' ' . $hour . ':00'))
])->num_rows();
}
}
/* End of file appointments_model.php */