2013-04-12 19:36:09 +03:00
|
|
|
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
|
|
|
|
|
|
|
class Appointments extends CI_Controller {
|
2013-06-03 17:42:19 +03:00
|
|
|
/**
|
|
|
|
* Default callback method of the application.
|
|
|
|
*
|
|
|
|
* This method creates the appointment book wizard. If an appointment hash
|
|
|
|
* is provided then it means that the customer followed the appointment
|
|
|
|
* manage link that was send with the book success email.
|
|
|
|
*
|
|
|
|
* @param string $appointment_hash The db appointment hash of an existing
|
|
|
|
* record.
|
|
|
|
*/
|
|
|
|
public function index($appointment_hash = '') {
|
2013-07-06 03:00:04 +03:00
|
|
|
$this->load->model('appointments_model');
|
|
|
|
$this->load->model('providers_model');
|
|
|
|
$this->load->model('services_model');
|
|
|
|
$this->load->model('customers_model');
|
|
|
|
$this->load->model('settings_model');
|
2013-06-26 12:31:57 +03:00
|
|
|
|
2013-06-03 17:42:19 +03:00
|
|
|
if (strtoupper($_SERVER['REQUEST_METHOD']) !== 'POST') {
|
2013-07-02 20:18:19 +03:00
|
|
|
try {
|
2013-07-06 03:00:04 +03:00
|
|
|
$available_services = $this->services_model->get_available_services();
|
|
|
|
$available_providers = $this->providers_model->get_available_providers();
|
|
|
|
$company_name = $this->settings_model->get_setting('company_name');
|
2013-07-02 20:18:19 +03:00
|
|
|
|
|
|
|
// If an appointment hash is provided then it means that the customer
|
|
|
|
// is trying to edit a registered appointment record.
|
|
|
|
if ($appointment_hash !== ''){
|
|
|
|
// Load the appointments data and enable the manage mode of the page.
|
|
|
|
$manage_mode = TRUE;
|
|
|
|
|
2013-07-06 03:00:04 +03:00
|
|
|
$appointments_results = $this->appointments_model
|
2013-07-02 20:18:19 +03:00
|
|
|
->get_batch(array('hash' => $appointment_hash));
|
2013-06-26 12:31:57 +03:00
|
|
|
|
2013-07-02 20:18:19 +03:00
|
|
|
if (count($appointments_results) === 0) {
|
|
|
|
// The requested appointment doesn't exist in the database. Display
|
|
|
|
// a message to the customer.
|
|
|
|
$view_data = array(
|
|
|
|
'message_title' => 'Appointment Not Found!',
|
|
|
|
'message_text' => 'The appointment you requested does not exist in '
|
|
|
|
. 'the system database anymore.',
|
|
|
|
'message_icon' => $this->config->item('base_url')
|
|
|
|
. 'assets/images/error.png'
|
|
|
|
);
|
|
|
|
$this->load->view('appointments/message', $view_data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-09 17:46:48 +03:00
|
|
|
$appointment = $appointments_results[0];
|
2013-07-02 20:18:19 +03:00
|
|
|
|
2013-07-09 17:46:48 +03:00
|
|
|
$provider = $this->providers_model
|
|
|
|
->get_row($appointment['id_users_provider']);
|
|
|
|
$customer = $this->customers_model
|
|
|
|
->get_row($appointment['id_users_customer']);
|
2013-07-02 20:18:19 +03:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// The customer is going to book a new appointment so there is no
|
|
|
|
// need for the manage functionality to be initialized.
|
|
|
|
$manage_mode = FALSE;
|
2013-07-09 17:46:48 +03:00
|
|
|
$appointment = array();
|
|
|
|
$provider = array();
|
|
|
|
$customer = array();
|
2013-06-26 12:31:57 +03:00
|
|
|
}
|
2013-07-02 20:18:19 +03:00
|
|
|
|
|
|
|
// Load the book appointment view.
|
|
|
|
$view_data = array (
|
|
|
|
'available_services' => $available_services,
|
|
|
|
'available_providers' => $available_providers,
|
|
|
|
'company_name' => $company_name,
|
|
|
|
'manage_mode' => $manage_mode,
|
2013-07-09 17:46:48 +03:00
|
|
|
'appointment_data' => $appointment,
|
|
|
|
'provider_data' => $provider,
|
|
|
|
'customer_data' => $customer
|
2013-07-02 20:18:19 +03:00
|
|
|
);
|
2013-06-26 12:31:57 +03:00
|
|
|
|
2013-07-02 20:18:19 +03:00
|
|
|
} catch(Exception $exc) {
|
|
|
|
$view_data['exceptions'][] = $exc;
|
2013-06-03 17:42:19 +03:00
|
|
|
}
|
2013-06-26 12:31:57 +03:00
|
|
|
|
2013-05-04 00:26:04 +03:00
|
|
|
$this->load->view('appointments/book', $view_data);
|
2013-06-26 12:31:57 +03:00
|
|
|
|
2013-04-20 20:20:16 +03:00
|
|
|
} else {
|
2013-06-26 12:31:57 +03:00
|
|
|
// The page is a post-back. Register the appointment and send notification emails
|
|
|
|
// to the provider and the customer that are related to the appointment. If google
|
|
|
|
// sync is enabled then add the appointment to the provider's account.
|
2013-07-02 20:18:19 +03:00
|
|
|
|
2013-06-26 12:31:57 +03:00
|
|
|
try {
|
2013-07-09 17:46:48 +03:00
|
|
|
$post_data = json_decode($_POST['post_data'], true);
|
|
|
|
$appointment = $post_data['appointment'];
|
|
|
|
$customer = $post_data['customer'];
|
2013-06-03 17:42:19 +03:00
|
|
|
|
2013-07-09 17:46:48 +03:00
|
|
|
$customer_id = $this->customers_model->add($customer);
|
|
|
|
$appointment['id_users_customer'] = $customer_id;
|
2013-06-26 12:31:57 +03:00
|
|
|
|
2013-07-09 17:46:48 +03:00
|
|
|
$appointment['id'] = $this->appointments_model->add($appointment);
|
|
|
|
$appointment['hash'] = $this->appointments_model->get_value('hash', $appointment['id']);
|
2013-06-26 12:31:57 +03:00
|
|
|
|
2013-07-09 17:46:48 +03:00
|
|
|
$provider = $this->providers_model->get_row($appointment['id_users_provider']);
|
|
|
|
$service = $this->services_model->get_row($appointment['id_services']);
|
2013-06-28 17:23:17 +03:00
|
|
|
|
2013-06-26 12:31:57 +03:00
|
|
|
$company_settings = array(
|
2013-07-06 03:00:04 +03:00
|
|
|
'company_name' => $this->settings_model->get_setting('company_name'),
|
|
|
|
'company_link' => $this->settings_model->get_setting('company_link'),
|
|
|
|
'company_email' => $this->settings_model->get_setting('company_email')
|
2013-06-26 12:31:57 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
// :: SYNCHRONIZE APPOINTMENT WITH PROVIDER'S GOOGLE CALENDAR
|
|
|
|
// The provider must have previously granted access to his google calendar account
|
|
|
|
// in order to sync the appointment.
|
2013-07-02 20:18:19 +03:00
|
|
|
try {
|
2013-07-06 03:00:04 +03:00
|
|
|
$google_sync = $this->providers_model->get_setting('google_sync',
|
2013-07-09 17:46:48 +03:00
|
|
|
$appointment['id_users_provider']);
|
2013-06-26 12:31:57 +03:00
|
|
|
|
2013-07-02 20:18:19 +03:00
|
|
|
if ($google_sync == TRUE) {
|
2013-07-06 03:00:04 +03:00
|
|
|
$google_token = json_decode($this->providers_model
|
2013-07-09 17:46:48 +03:00
|
|
|
->get_setting('google_token', $appointment['id_users_provider']));
|
2013-06-26 12:31:57 +03:00
|
|
|
|
2013-07-02 20:18:19 +03:00
|
|
|
$this->load->library('google_sync');
|
|
|
|
$this->google_sync->refresh_token($google_token->refresh_token);
|
|
|
|
|
|
|
|
if ($post_data['manage_mode'] === FALSE) {
|
|
|
|
// Add appointment to Google Calendar.
|
2013-07-09 17:46:48 +03:00
|
|
|
$google_event = $this->google_sync->add_appointment($appointment, $provider,
|
|
|
|
$service, $customer, $company_settings);
|
|
|
|
$appointment['id_google_calendar'] = $google_event->id;
|
|
|
|
$this->appointments_model->add($appointment);
|
2013-07-02 20:18:19 +03:00
|
|
|
} else {
|
|
|
|
// Update appointment to Google Calendar.
|
2013-07-09 17:46:48 +03:00
|
|
|
$appointment['id_google_calendar'] = $this->appointments_model
|
|
|
|
->get_value('id_google_calendar', $appointment['id']);
|
2013-07-02 20:18:19 +03:00
|
|
|
|
2013-07-09 17:46:48 +03:00
|
|
|
$this->google_sync->update_appointment($appointment, $provider,
|
|
|
|
$service, $customer, $company_settings);
|
2013-07-02 20:18:19 +03:00
|
|
|
}
|
|
|
|
}
|
2013-07-03 20:27:00 +03:00
|
|
|
} catch(Exception $exc) {
|
|
|
|
$view_data['exceptions'][] = $exc;
|
2013-07-02 20:18:19 +03:00
|
|
|
}
|
2013-06-28 17:23:17 +03:00
|
|
|
|
|
|
|
// :: SEND NOTIFICATION EMAILS TO BOTH CUSTOMER AND PROVIDER
|
2013-07-02 20:18:19 +03:00
|
|
|
try {
|
|
|
|
$this->load->library('Notifications');
|
|
|
|
|
|
|
|
if (!$post_data['manage_mode']) {
|
2013-07-03 20:27:00 +03:00
|
|
|
$customer_title = 'Your appointment has been successfully booked!';
|
2013-07-02 20:18:19 +03:00
|
|
|
$customer_message = 'Thank you for arranging an appointment with us. '
|
2013-07-03 20:27:00 +03:00
|
|
|
. 'Below you can see the appointment details. Make changes '
|
|
|
|
. 'by clicking the appointment link.';
|
|
|
|
$customer_link = $this->config->item('base_url') . 'appointments/index/'
|
2013-07-09 17:46:48 +03:00
|
|
|
. $appointment['hash'];
|
2013-07-02 20:18:19 +03:00
|
|
|
|
2013-07-03 20:27:00 +03:00
|
|
|
$provider_title = 'A new appointment has been added to your plan.';
|
2013-07-02 20:18:19 +03:00
|
|
|
$provider_message = 'You can make changes by clicking the appointment '
|
2013-07-03 20:27:00 +03:00
|
|
|
. 'link below';
|
|
|
|
$provider_link = $this->config->item('base_url') . 'backend/'
|
2013-07-09 17:46:48 +03:00
|
|
|
. $appointment['hash'];
|
2013-07-02 20:18:19 +03:00
|
|
|
} else {
|
2013-07-03 20:27:00 +03:00
|
|
|
$customer_title = 'Appointment changes have been successfully saved!';
|
2013-07-02 20:18:19 +03:00
|
|
|
$customer_message = '';
|
2013-07-03 20:27:00 +03:00
|
|
|
$customer_link = $this->config->item('base_url') . 'appointments/index/'
|
2013-07-09 17:46:48 +03:00
|
|
|
. $appointment['hash'];
|
2013-07-02 20:18:19 +03:00
|
|
|
|
2013-07-03 20:27:00 +03:00
|
|
|
$provider_title = 'Appointment details have changed.';
|
2013-07-02 20:18:19 +03:00
|
|
|
$provider_message = '';
|
2013-07-03 20:27:00 +03:00
|
|
|
$provider_link = $this->config->item('base_url') . 'backend/'
|
2013-07-09 17:46:48 +03:00
|
|
|
. $appointment['hash'];
|
2013-07-02 20:18:19 +03:00
|
|
|
}
|
2013-06-28 17:23:17 +03:00
|
|
|
|
2013-07-09 17:46:48 +03:00
|
|
|
$this->notifications->send_appointment_details($appointment, $provider,
|
|
|
|
$service, $customer,$company_settings, $customer_title,
|
|
|
|
$customer_message, $customer_link, $customer['email']);
|
2013-06-26 12:31:57 +03:00
|
|
|
|
2013-07-09 17:46:48 +03:00
|
|
|
$this->notifications->send_appointment_details($appointment, $provider,
|
|
|
|
$service, $customer, $company_settings, $provider_title,
|
|
|
|
$provider_message, $provider_link, $provider['email']);
|
2013-07-03 20:27:00 +03:00
|
|
|
} catch(Exception $exc) {
|
|
|
|
$view_data['exceptions'][] = $exc;
|
2013-07-02 20:18:19 +03:00
|
|
|
}
|
|
|
|
|
2013-06-26 12:31:57 +03:00
|
|
|
// :: LOAD THE BOOK SUCCESS VIEW
|
|
|
|
$view_data = array(
|
2013-07-09 17:46:48 +03:00
|
|
|
'appointment_data' => $appointment,
|
|
|
|
'provider_data' => $provider,
|
|
|
|
'service_data' => $service,
|
2013-06-26 12:31:57 +03:00
|
|
|
'company_name' => $company_settings['company_name']
|
|
|
|
);
|
|
|
|
|
|
|
|
} catch(Exception $exc) {
|
2013-07-02 20:18:19 +03:00
|
|
|
$view_data['exceptions'][] = $exc;
|
2013-06-26 12:31:57 +03:00
|
|
|
}
|
2013-06-28 17:23:17 +03:00
|
|
|
|
2013-05-04 00:26:04 +03:00
|
|
|
$this->load->view('appointments/book_success', $view_data);
|
2013-06-03 17:42:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-08 12:54:45 +03:00
|
|
|
* Cancel an existing appointment.
|
2013-06-03 17:42:19 +03:00
|
|
|
*
|
2013-06-08 12:54:45 +03:00
|
|
|
* This method removes an appointment from the company's schedule.
|
|
|
|
* In order for the appointment to be deleted, the hash string must
|
|
|
|
* be provided. The customer can only cancel the appointment if the
|
|
|
|
* edit time period is not over yet.
|
2013-06-03 17:42:19 +03:00
|
|
|
*
|
2013-06-08 12:54:45 +03:00
|
|
|
* @param string $appointment_hash This is used to distinguish the
|
|
|
|
* appointment record.
|
2013-07-03 20:27:00 +03:00
|
|
|
* @param string $_POST['cancel_reason'] The text that describes why
|
|
|
|
* the customer cancelled the appointment.
|
2013-06-03 17:42:19 +03:00
|
|
|
*/
|
2013-06-08 12:54:45 +03:00
|
|
|
public function cancel($appointment_hash) {
|
|
|
|
try {
|
2013-07-06 03:00:04 +03:00
|
|
|
$this->load->model('appointments_model');
|
|
|
|
$this->load->model('providers_model');
|
|
|
|
$this->load->model('customers_model');
|
|
|
|
$this->load->model('services_model');
|
|
|
|
$this->load->model('settings_model');
|
2013-06-03 17:42:19 +03:00
|
|
|
|
2013-06-08 12:54:45 +03:00
|
|
|
// Check whether the appointment hash exists in the database.
|
2013-07-06 03:00:04 +03:00
|
|
|
$records = $this->appointments_model->get_batch(array('hash' => $appointment_hash));
|
2013-06-08 12:54:45 +03:00
|
|
|
if (count($records) == 0) {
|
|
|
|
throw new Exception('No record matches the provided hash.');
|
|
|
|
}
|
2013-06-03 17:42:19 +03:00
|
|
|
|
2013-07-09 17:46:48 +03:00
|
|
|
$appointment = $records[0];
|
|
|
|
$provider = $this->providers_model->get_row($appointment['id_users_provider']);
|
|
|
|
$customer = $this->customers_model->get_row($appointment['id_users_customer']);
|
|
|
|
$service = $this->services_model->get_row($appointment['id_services']);
|
2013-06-28 17:23:17 +03:00
|
|
|
|
2013-06-26 12:31:57 +03:00
|
|
|
$company_settings = array(
|
2013-07-06 03:00:04 +03:00
|
|
|
'company_name' => $this->settings_model->get_setting('company_name'),
|
|
|
|
'company_email' => $this->settings_model->get_setting('company_email'),
|
|
|
|
'company_link' => $this->settings_model->get_setting('company_link')
|
2013-06-24 09:04:30 +03:00
|
|
|
);
|
2013-06-03 17:42:19 +03:00
|
|
|
|
2013-06-24 09:04:30 +03:00
|
|
|
// :: DELETE APPOINTMENT RECORD FROM THE DATABASE.
|
2013-07-09 17:46:48 +03:00
|
|
|
if (!$this->appointments_model->delete($appointment['id'])) {
|
2013-06-08 12:54:45 +03:00
|
|
|
throw new Exception('Appointment could not be deleted from the database.');
|
|
|
|
}
|
|
|
|
|
2013-06-24 09:04:30 +03:00
|
|
|
// :: SYNC APPOINTMENT REMOVAL WITH GOOGLE CALENDAR
|
2013-07-09 17:46:48 +03:00
|
|
|
if ($appointment['id_google_calendar'] != NULL) {
|
2013-07-03 20:27:00 +03:00
|
|
|
try {
|
2013-07-06 03:00:04 +03:00
|
|
|
$google_sync = $this->providers_model->get_setting('google_sync',
|
2013-07-09 17:46:48 +03:00
|
|
|
$appointment['id_users_provider']);
|
2013-07-03 20:27:00 +03:00
|
|
|
|
|
|
|
if ($google_sync == TRUE) {
|
2013-07-06 03:00:04 +03:00
|
|
|
$google_token = json_decode($this->providers_model
|
2013-07-09 17:46:48 +03:00
|
|
|
->get_setting('google_token', $provider['id']));
|
2013-07-03 20:27:00 +03:00
|
|
|
$this->load->library('Google_Sync');
|
|
|
|
$this->google_sync->refresh_token($google_token->refresh_token);
|
2013-07-09 17:46:48 +03:00
|
|
|
$this->google_sync->delete_appointment($appointment['id_google_calendar']);
|
2013-07-03 20:27:00 +03:00
|
|
|
}
|
|
|
|
} catch(Exception $exc) {
|
|
|
|
$exceptions[] = $exc;
|
2013-06-12 18:31:16 +03:00
|
|
|
}
|
2013-06-24 09:04:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// :: SEND NOTIFICATION EMAILS TO CUSTOMER AND PROVIDER
|
2013-07-03 20:27:00 +03:00
|
|
|
try {
|
|
|
|
$this->load->library('Notifications');
|
2013-07-09 17:46:48 +03:00
|
|
|
$this->notifications->send_delete_appointment($appointment, $provider,
|
|
|
|
$service, $customer, $company_settings, $provider['email'],
|
2013-07-03 20:27:00 +03:00
|
|
|
$_POST['cancel_reason']);
|
2013-07-09 17:46:48 +03:00
|
|
|
$this->notifications->send_delete_appointment($appointment, $provider,
|
|
|
|
$service, $customer, $company_settings, $customer['email'],
|
2013-07-03 20:27:00 +03:00
|
|
|
$_POST['cancel_reason']);
|
|
|
|
} catch(Exception $exc) {
|
|
|
|
$exceptions[] = $exc;
|
|
|
|
}
|
2013-06-08 12:54:45 +03:00
|
|
|
} catch(Exception $exc) {
|
|
|
|
// Display the error message to the customer.
|
2013-07-03 20:27:00 +03:00
|
|
|
$exceptions[] = $exc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($exceptions)) {
|
|
|
|
$view_data['exceptions'] = $exceptions;
|
2013-04-20 20:20:16 +03:00
|
|
|
}
|
2013-06-08 12:54:45 +03:00
|
|
|
|
2013-06-28 17:23:17 +03:00
|
|
|
$this->load->view('appointments/cancel', $view_data);
|
2013-06-03 17:42:19 +03:00
|
|
|
}
|
2013-04-17 00:37:36 +03:00
|
|
|
|
|
|
|
/**
|
2013-05-20 20:21:58 +03:00
|
|
|
* [AJAX] Get the available appointment hours for the given date.
|
|
|
|
*
|
2013-07-02 20:18:19 +03:00
|
|
|
* This method answers to an AJAX request. It calculates the available hours
|
|
|
|
* for thegiven service, provider and date.
|
2013-04-17 00:37:36 +03:00
|
|
|
*
|
2013-06-28 17:23:17 +03:00
|
|
|
* @param numeric $_POST['service_id'] The selected service's record id.
|
|
|
|
* @param numeric $_POST['provider_id'] The selected provider's record id.
|
2013-07-02 20:18:19 +03:00
|
|
|
* @param string $_POST['selected_date'] The selected date of which the
|
|
|
|
* available hours we want to see.
|
|
|
|
* @param numeric $_POST['service_duration'] The selected service duration in
|
|
|
|
* minutes.
|
2013-05-20 20:21:58 +03:00
|
|
|
* @return Returns a json object with the available hours.
|
2013-04-17 00:37:36 +03:00
|
|
|
*/
|
2013-05-04 00:26:04 +03:00
|
|
|
public function ajax_get_available_hours() {
|
2013-07-06 03:00:04 +03:00
|
|
|
$this->load->model('providers_model');
|
|
|
|
$this->load->model('appointments_model');
|
|
|
|
$this->load->model('settings_model');
|
2013-05-20 20:21:58 +03:00
|
|
|
|
2013-07-02 20:18:19 +03:00
|
|
|
try {
|
|
|
|
// If manage mode is TRUE then the following we should not consider the selected
|
|
|
|
// appointment when calculating the available time periods of the provider.
|
|
|
|
$exclude_appointments = ($_POST['manage_mode'] === 'true')
|
|
|
|
? array($_POST['appointment_id'])
|
|
|
|
: array();
|
|
|
|
|
|
|
|
$empty_periods = $this->get_provider_available_time_periods($_POST['provider_id'],
|
|
|
|
$_POST['selected_date'], $exclude_appointments);
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
$available_hours = array();
|
|
|
|
|
|
|
|
foreach($empty_periods as $period) {
|
|
|
|
$start_hour = new DateTime($_POST['selected_date'] . ' ' . $period['start']);
|
|
|
|
$end_hour = new DateTime($_POST['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);
|
|
|
|
}
|
2013-06-28 17:23:17 +03:00
|
|
|
}
|
2013-07-02 20:18:19 +03:00
|
|
|
|
|
|
|
$curr_hour = $start_hour;
|
2013-06-28 17:23:17 +03:00
|
|
|
$diff = $curr_hour->diff($end_hour);
|
2013-07-02 20:18:19 +03:00
|
|
|
|
|
|
|
while(($diff->h * 60 + $diff->i) > intval($_POST['service_duration'])) {
|
|
|
|
$available_hours[] = $curr_hour->format('H:i');
|
|
|
|
$curr_hour->add(new DateInterval("PT15M"));
|
|
|
|
$diff = $curr_hour->diff($end_hour);
|
|
|
|
}
|
2013-06-28 17:23:17 +03:00
|
|
|
}
|
2013-07-02 20:18:19 +03:00
|
|
|
|
|
|
|
// If the selected date is today, remove past hours. It is important
|
|
|
|
// include the timeout before booking that is set in the backoffice
|
|
|
|
// 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.
|
|
|
|
if (date('m/d/Y', strtotime($_POST['selected_date'])) == date('m/d/Y')) {
|
|
|
|
if ($_POST['manage_mode'] === 'true') {
|
|
|
|
$book_advance_timeout = 0;
|
|
|
|
} else {
|
2013-07-06 03:00:04 +03:00
|
|
|
$book_advance_timeout = $this->settings_model->get_setting(
|
2013-07-02 20:18:19 +03:00
|
|
|
'book_advance_timeout');
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($available_hours as $index=>$value) {
|
|
|
|
$available_hour = strtotime($value);
|
|
|
|
$current_hour = strtotime('+' . $book_advance_timeout . ' minutes',
|
|
|
|
strtotime('now'));
|
|
|
|
|
|
|
|
if ($available_hour <= $current_hour) {
|
|
|
|
unset($available_hours[$index]);
|
|
|
|
}
|
2013-06-28 17:23:17 +03:00
|
|
|
}
|
|
|
|
}
|
2013-07-02 20:18:19 +03:00
|
|
|
|
|
|
|
$available_hours = array_values($available_hours);
|
|
|
|
|
|
|
|
echo json_encode($available_hours);
|
|
|
|
|
|
|
|
} catch(Exception $exc) {
|
|
|
|
echo json_encode(array(
|
|
|
|
'exceptions' => array( exceptionToJavascript($exc) )
|
|
|
|
));
|
2013-06-28 17:23:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the provider is still available in the selected appointment date.
|
|
|
|
*
|
|
|
|
* It might be times where two or more customers select the same appointment date and time.
|
|
|
|
* This shouldn't be allowed to happen, so one of the two customers will eventually get the
|
|
|
|
* prefered date and the other one will have to choose for another date. Use this method
|
|
|
|
* just before the customer confirms the appointment details. If the selected date was taken
|
|
|
|
* in the mean time, the customer must be prompted to select another time for his appointment.
|
|
|
|
*
|
|
|
|
* @param int $_POST['id_users_provider'] The selected provider's record id.
|
|
|
|
* @param int $_POST['id_services'] The selected service's record id.
|
|
|
|
* @param string $_POST['start_datetime'] This is a mysql formed string.
|
|
|
|
* @return bool Returns whether the selected datetime is still available.
|
|
|
|
*/
|
|
|
|
public function ajax_check_datetime_availability() {
|
|
|
|
try {
|
2013-07-06 03:00:04 +03:00
|
|
|
$this->load->model('services_model');
|
2013-06-28 17:23:17 +03:00
|
|
|
|
2013-07-06 03:00:04 +03:00
|
|
|
$service_duration = $this->services_model->get_value('duration', $_POST['id_services']);
|
2013-06-28 17:23:17 +03:00
|
|
|
|
|
|
|
$available_periods = $this->get_provider_available_time_periods(
|
|
|
|
$_POST['id_users_provider'], $_POST['start_datetime']);
|
|
|
|
|
|
|
|
$is_still_available = FALSE;
|
|
|
|
|
|
|
|
foreach($available_periods as $period) {
|
|
|
|
$appt_start = new DateTime($_POST['start_datetime']);
|
|
|
|
$appt_start = $appt_start->format('H:i');
|
|
|
|
|
2013-07-03 20:27:00 +03:00
|
|
|
$appt_end = new DateTime($_POST['start_datetime']);
|
2013-06-28 17:23:17 +03:00
|
|
|
$appt_end->add(new DateInterval('PT' . $service_duration . 'M'));
|
2013-07-03 20:27:00 +03:00
|
|
|
$appt_end = $appt_end->format('H:i');
|
2013-06-28 17:23:17 +03:00
|
|
|
|
|
|
|
$period_start = date('H:i', strtotime($period['start']));
|
2013-07-03 20:27:00 +03:00
|
|
|
$period_end = date('H:i', strtotime($period['end']));
|
2013-06-28 17:23:17 +03:00
|
|
|
|
2013-07-03 20:27:00 +03:00
|
|
|
if ($period_start <= $appt_start && $period_end >= $appt_end) {
|
2013-06-28 17:23:17 +03:00
|
|
|
$is_still_available = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
echo json_encode($is_still_available);
|
|
|
|
|
|
|
|
} catch(Exception $exc) {
|
|
|
|
echo json_encode(array(
|
2013-07-03 20:27:00 +03:00
|
|
|
'exceptions' => array(exceptionToJavascript($exc))
|
2013-06-28 17:23:17 +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
|
|
|
|
* know when a provider is avaible for an appointment. This method will return an array
|
|
|
|
* that belongs to the selected date and contains values that have the start and the end
|
|
|
|
* time of an available time period.
|
|
|
|
*
|
|
|
|
* @param numeric $provider_id The provider's record id.
|
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
private function get_provider_available_time_periods($provider_id, $selected_date,
|
|
|
|
$exclude_appointments = array()) {
|
2013-07-06 03:00:04 +03:00
|
|
|
$this->load->model('appointments_model');
|
|
|
|
$this->load->model('providers_model');
|
2013-06-28 17:23:17 +03:00
|
|
|
|
2013-05-20 20:21:58 +03:00
|
|
|
// Get the provider's working plan and reserved appointments.
|
2013-07-06 03:00:04 +03:00
|
|
|
$working_plan = json_decode($this->providers_model
|
2013-06-28 17:23:17 +03:00
|
|
|
->get_setting('working_plan', $provider_id), true);
|
2013-04-17 00:37:36 +03:00
|
|
|
|
2013-06-03 17:42:19 +03:00
|
|
|
$where_clause = array(
|
2013-06-28 17:23:17 +03:00
|
|
|
'DATE(start_datetime)' => date('Y-m-d', strtotime($selected_date)),
|
|
|
|
'id_users_provider' => $provider_id
|
|
|
|
);
|
|
|
|
|
2013-07-06 03:00:04 +03:00
|
|
|
$reserved_appointments = $this->appointments_model->get_batch($where_clause);
|
2013-06-03 17:42:19 +03:00
|
|
|
|
2013-06-28 17:23:17 +03:00
|
|
|
// 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.
|
|
|
|
foreach($exclude_appointments as $excluded_appointment) {
|
2013-06-08 12:54:45 +03:00
|
|
|
foreach($reserved_appointments as $index=>$appointment) {
|
2013-06-28 17:23:17 +03:00
|
|
|
if ($appointment['id'] == $excluded_appointment['id']) {
|
2013-06-08 12:54:45 +03:00
|
|
|
unset($reserved_appointments[$index]);
|
|
|
|
}
|
|
|
|
}
|
2013-06-03 17:42:19 +03:00
|
|
|
}
|
2013-04-17 00:37:36 +03:00
|
|
|
|
2013-06-28 17:23:17 +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.
|
|
|
|
$selected_date_working_plan = $working_plan[strtolower(date('l',
|
|
|
|
strtotime($selected_date)))];
|
|
|
|
$available_periods_with_breaks = array();
|
|
|
|
|
|
|
|
if (isset($selected_date_working_plan['breaks'])) {
|
|
|
|
foreach($selected_date_working_plan['breaks'] as $index=>$break) {
|
2013-05-20 20:21:58 +03:00
|
|
|
// Split the working plan to available time periods that do not
|
|
|
|
// contain the breaks in them.
|
|
|
|
$last_break_index = $index - 1;
|
|
|
|
|
2013-06-28 17:23:17 +03:00
|
|
|
if (count($available_periods_with_breaks) === 0) {
|
|
|
|
$start_hour = $selected_date_working_plan['start'];
|
|
|
|
$end_hour = $break['start'];
|
2013-05-20 20:21:58 +03:00
|
|
|
} else {
|
2013-06-28 17:23:17 +03:00
|
|
|
$start_hour = $selected_date_working_plan['breaks'][$last_break_index]['end'];
|
|
|
|
$end_hour = $break['start'];
|
2013-05-20 20:21:58 +03:00
|
|
|
}
|
|
|
|
|
2013-06-28 17:23:17 +03:00
|
|
|
$available_periods_with_breaks[] = array(
|
|
|
|
'start' => $start_hour,
|
|
|
|
'end' => $end_hour
|
|
|
|
);
|
2013-05-20 20:21:58 +03:00
|
|
|
}
|
|
|
|
|
2013-06-28 17:23:17 +03:00
|
|
|
// Add the period from the last break to the end of the day.
|
|
|
|
$available_periods_with_breaks[] = array(
|
|
|
|
'start' => $selected_date_working_plan['breaks'][$index]['end'],
|
|
|
|
'end' => $selected_date_working_plan['end']
|
2013-05-20 20:21:58 +03:00
|
|
|
);
|
|
|
|
}
|
2013-06-26 12:31:57 +03:00
|
|
|
|
2013-06-28 17:23:17 +03:00
|
|
|
// Break the empty periods with the reserved appointments.
|
|
|
|
$available_periods_with_appointments = array();
|
|
|
|
|
2013-05-20 20:21:58 +03:00
|
|
|
if (count($reserved_appointments) > 0) {
|
2013-06-28 17:23:17 +03:00
|
|
|
|
|
|
|
foreach($available_periods_with_breaks as $period) {
|
|
|
|
|
|
|
|
foreach($reserved_appointments as $index=>$excluded_appointment) {
|
|
|
|
$appointment_start = date('H:i', strtotime($excluded_appointment['start_datetime']));
|
|
|
|
$appointment_end = date('H:i', strtotime($excluded_appointment['end_datetime']));
|
|
|
|
$period_start = date('H:i', strtotime($period['start']));
|
|
|
|
$period_end = date('H:i', strtotime($period['end']));
|
2013-05-20 20:21:58 +03:00
|
|
|
|
2013-06-28 17:23:17 +03:00
|
|
|
if ($period_start < $appointment_start && $period_end > $appointment_end) {
|
2013-06-26 12:31:57 +03:00
|
|
|
// We need to check whether another appointment fits in the current
|
2013-06-28 17:23:17 +03:00
|
|
|
// time period. If this happens, then we need to consider the whole
|
|
|
|
// appointment time as one, because the provider will not be available.
|
|
|
|
foreach ($reserved_appointments as $tmp_appointment) {
|
|
|
|
$appt_start = date('H:i', strtotime($tmp_appointment['start_datetime']));
|
|
|
|
$appt_end = date('H:i', strtotime($tmp_appointment['end_datetime']));
|
2013-06-26 12:31:57 +03:00
|
|
|
|
2013-06-28 17:23:17 +03:00
|
|
|
if ($period_start < $appt_start && $period_end > $appt_end) {
|
2013-06-26 12:31:57 +03:00
|
|
|
if ($appointment_start > $appt_start) {
|
|
|
|
$appointment_start = $appt_start;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($appointment_end < $appt_end) {
|
|
|
|
$appointment_end = $appt_end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-20 20:21:58 +03:00
|
|
|
// Current appointment is within the current empty space. So
|
|
|
|
// we need to break the empty space into two other spaces that
|
|
|
|
// don't include the appointment.
|
2013-06-28 17:23:17 +03:00
|
|
|
$new_period = array(
|
|
|
|
'start' => $period_start,
|
2013-07-03 20:27:00 +03:00
|
|
|
'end' => $appointment_start
|
2013-06-26 12:31:57 +03:00
|
|
|
);
|
|
|
|
|
2013-06-28 17:23:17 +03:00
|
|
|
if (!in_array($new_period, $available_periods_with_appointments)) {
|
|
|
|
$available_periods_with_appointments[] = $new_period;
|
2013-06-26 12:31:57 +03:00
|
|
|
}
|
|
|
|
|
2013-06-28 17:23:17 +03:00
|
|
|
$new_period = array(
|
2013-06-26 12:31:57 +03:00
|
|
|
'start' => $appointment_end,
|
2013-07-03 20:27:00 +03:00
|
|
|
'end' => $period_end
|
2013-06-26 12:31:57 +03:00
|
|
|
);
|
2013-06-28 17:23:17 +03:00
|
|
|
|
|
|
|
if (!in_array($new_period, $available_periods_with_appointments)) {
|
|
|
|
$available_periods_with_appointments[] = $new_period;
|
2013-06-26 12:31:57 +03:00
|
|
|
}
|
|
|
|
|
2013-05-20 20:21:58 +03:00
|
|
|
} else {
|
2013-06-03 17:42:19 +03:00
|
|
|
// Check if there are any other appointments between this
|
|
|
|
// time space. If not, it is going to be added as it is.
|
|
|
|
$found = FALSE;
|
2013-06-28 17:23:17 +03:00
|
|
|
|
|
|
|
foreach ($reserved_appointments as $tmp_appointment) {
|
2013-07-03 20:27:00 +03:00
|
|
|
$appt_start = date('H:i', strtotime($tmp_appointment['start_datetime']));
|
|
|
|
$appt_end = date('H:i', strtotime($tmp_appointment['end_datetime']));
|
2013-06-28 17:23:17 +03:00
|
|
|
|
|
|
|
if ($period_start < $appt_start && $period_end > $appt_end) {
|
2013-06-03 17:42:19 +03:00
|
|
|
$found = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// It is also necessary to check that this time period doesn't
|
|
|
|
// already exist in the "$empty_spaces_with_appointments" array.
|
2013-06-28 17:23:17 +03:00
|
|
|
$empty_period = array(
|
|
|
|
'start' => $period_start,
|
2013-07-03 20:27:00 +03:00
|
|
|
'end' => $period_end
|
2013-06-28 17:23:17 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
$already_exist = in_array($empty_period, $available_periods_with_appointments);
|
|
|
|
|
2013-06-03 17:42:19 +03:00
|
|
|
if ($found === FALSE && $already_exist === FALSE) {
|
2013-06-28 17:23:17 +03:00
|
|
|
$available_periods_with_appointments[] = $empty_period;
|
2013-06-03 17:42:19 +03:00
|
|
|
}
|
2013-05-20 20:21:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2013-06-28 17:23:17 +03:00
|
|
|
$available_periods_with_appointments = $available_periods_with_breaks;
|
2013-05-20 20:21:58 +03:00
|
|
|
}
|
|
|
|
|
2013-06-28 17:23:17 +03:00
|
|
|
return $available_periods_with_appointments;
|
2013-05-04 00:26:04 +03:00
|
|
|
}
|
2013-04-12 19:36:09 +03:00
|
|
|
}
|
2013-05-04 00:26:04 +03:00
|
|
|
|
|
|
|
/* End of file appointments.php */
|
|
|
|
/* Location: ./application/controllers/appointments.php */
|