2017-09-15 14:44:40 +03:00
|
|
|
<?php defined('BASEPATH') OR exit('No direct script access allowed');
|
2015-10-05 01:32:47 +03:00
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Easy!Appointments - Open Source Web Scheduler
|
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2017-01-31 09:35:34 +03:00
|
|
|
* @copyright Copyright (c) 2013 - 2017, Alex Tselegidis
|
2015-10-05 01:32:47 +03:00
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link http://easyappointments.org
|
|
|
|
* @since v1.0.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
2016-11-09 21:56:24 +03:00
|
|
|
use \EA\Engine\Types\Text;
|
2016-07-21 23:36:17 +03:00
|
|
|
use \EA\Engine\Types\Email;
|
|
|
|
use \EA\Engine\Types\Url;
|
2016-07-16 17:56:02 +03:00
|
|
|
|
2015-10-05 01:32:47 +03:00
|
|
|
/**
|
|
|
|
* Appointments Controller
|
|
|
|
*
|
|
|
|
* @package Controllers
|
|
|
|
*/
|
|
|
|
class Appointments extends CI_Controller {
|
2015-10-18 21:00:01 +03:00
|
|
|
/**
|
|
|
|
* Class Constructor
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
2015-10-18 21:00:01 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$this->load->library('session');
|
2015-10-18 21:00:01 +03:00
|
|
|
$this->load->helper('installation');
|
|
|
|
|
|
|
|
// Set user's selected language.
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($this->session->userdata('language'))
|
|
|
|
{
|
|
|
|
$this->config->set_item('language', $this->session->userdata('language'));
|
|
|
|
$this->lang->load('translations', $this->session->userdata('language'));
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
$this->lang->load('translations', $this->config->item('language')); // default
|
|
|
|
}
|
2016-01-17 14:10:48 +02:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
// Common helpers
|
|
|
|
$this->load->helper('google_analytics');
|
|
|
|
}
|
2015-10-05 01:32:47 +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.
|
|
|
|
*
|
2017-09-06 16:22:11 +03:00
|
|
|
* @param string $appointment_hash DB appointment hash of an existing record (default '').
|
2015-10-05 01:32:47 +03:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function index($appointment_hash = '')
|
|
|
|
{
|
|
|
|
if ( ! is_ea_installed())
|
|
|
|
{
|
2015-10-18 20:18:02 +03:00
|
|
|
redirect('installation/index');
|
|
|
|
return;
|
|
|
|
}
|
2015-10-05 01:32:47 +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');
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
try
|
|
|
|
{
|
|
|
|
$available_services = $this->services_model->get_available_services();
|
2015-10-18 21:00:01 +03:00
|
|
|
$available_providers = $this->providers_model->get_available_providers();
|
2017-09-15 14:36:37 +03:00
|
|
|
$company_name = $this->settings_model->get_setting('company_name');
|
|
|
|
$date_format = $this->settings_model->get_setting('date_format');
|
|
|
|
|
|
|
|
// Remove the data that are not needed inside the $available_providers array.
|
|
|
|
foreach ($available_providers as $index => $provider)
|
|
|
|
{
|
|
|
|
$stripped_data = [
|
|
|
|
'id' => $provider['id'],
|
|
|
|
'first_name' => $provider['first_name'],
|
|
|
|
'last_name' => $provider['last_name'],
|
|
|
|
'services' => $provider['services']
|
|
|
|
];
|
|
|
|
$available_providers[$index] = $stripped_data;
|
|
|
|
}
|
2016-01-01 16:52:43 +02:00
|
|
|
|
2015-10-18 21:00:01 +03:00
|
|
|
// If an appointment hash is provided then it means that the customer
|
|
|
|
// is trying to edit a registered appointment record.
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($appointment_hash !== '')
|
|
|
|
{
|
2015-10-18 21:00:01 +03:00
|
|
|
// Load the appointments data and enable the manage mode of the page.
|
|
|
|
$manage_mode = TRUE;
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$results = $this->appointments_model->get_batch(['hash' => $appointment_hash]);
|
2015-10-18 21:00:01 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if (count($results) === 0)
|
|
|
|
{
|
2015-10-18 21:00:01 +03:00
|
|
|
// The requested appointment doesn't exist in the database. Display
|
|
|
|
// a message to the customer.
|
2017-09-15 14:36:37 +03:00
|
|
|
$view = [
|
2015-10-18 21:00:01 +03:00
|
|
|
'message_title' => $this->lang->line('appointment_not_found'),
|
2017-09-15 14:36:37 +03:00
|
|
|
'message_text' => $this->lang->line('appointment_does_not_exist_in_db'),
|
|
|
|
'message_icon' => base_url('assets/img/error.png')
|
|
|
|
];
|
2015-10-18 21:00:01 +03:00
|
|
|
$this->load->view('appointments/message', $view);
|
|
|
|
return;
|
2015-10-05 01:32:47 +03:00
|
|
|
}
|
|
|
|
|
2015-10-18 21:00:01 +03:00
|
|
|
$appointment = $results[0];
|
2015-10-05 01:32:47 +03:00
|
|
|
$provider = $this->providers_model->get_row($appointment['id_users_provider']);
|
2015-10-18 21:00:01 +03:00
|
|
|
$customer = $this->customers_model->get_row($appointment['id_users_customer']);
|
2015-10-05 01:32:47 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
} else
|
|
|
|
{
|
2015-10-18 21:00:01 +03:00
|
|
|
// The customer is going to book a new appointment so there is no
|
|
|
|
// need for the manage functionality to be initialized.
|
2016-01-31 14:01:37 +02:00
|
|
|
$manage_mode = FALSE;
|
2017-09-15 14:36:37 +03:00
|
|
|
$appointment = [];
|
|
|
|
$provider = [];
|
|
|
|
$customer = [];
|
2015-10-05 01:32:47 +03:00
|
|
|
}
|
2015-10-07 23:51:12 +03:00
|
|
|
|
2015-10-18 21:00:01 +03:00
|
|
|
// Load the book appointment view.
|
2017-09-15 14:36:37 +03:00
|
|
|
$view = [
|
|
|
|
'available_services' => $available_services,
|
|
|
|
'available_providers' => $available_providers,
|
|
|
|
'company_name' => $company_name,
|
|
|
|
'manage_mode' => $manage_mode,
|
|
|
|
'date_format' => $date_format,
|
|
|
|
'appointment_data' => $appointment,
|
|
|
|
'provider_data' => $provider,
|
|
|
|
'customer_data' => $customer
|
|
|
|
];
|
|
|
|
} catch (Exception $exc)
|
|
|
|
{
|
2015-10-18 21:00:01 +03:00
|
|
|
$view['exceptions'][] = $exc;
|
2015-10-05 01:32:47 +03:00
|
|
|
}
|
2015-10-18 21:00:01 +03:00
|
|
|
|
|
|
|
$this->load->view('appointments/book', $view);
|
2015-10-05 01:32:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancel an existing appointment.
|
|
|
|
*
|
2017-09-06 16:22:11 +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. Provide the $_POST['cancel_reason'] parameter to describe the cancellation reason.
|
2015-10-05 01:32:47 +03:00
|
|
|
*
|
2017-09-06 16:22:11 +03:00
|
|
|
* @param string $appointment_hash This is used to distinguish the appointment record.
|
2015-10-05 01:32:47 +03:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function cancel($appointment_hash)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2015-10-05 01:32:47 +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');
|
|
|
|
|
|
|
|
// Check whether the appointment hash exists in the database.
|
2017-09-15 14:36:37 +03:00
|
|
|
$records = $this->appointments_model->get_batch(['hash' => $appointment_hash]);
|
|
|
|
if (count($records) == 0)
|
|
|
|
{
|
2015-10-05 01:32:47 +03:00
|
|
|
throw new Exception('No record matches the provided hash.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$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']);
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$company_settings = [
|
2015-10-05 01:32:47 +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')
|
2017-09-15 14:36:37 +03:00
|
|
|
];
|
2015-10-05 01:32:47 +03:00
|
|
|
|
|
|
|
// :: DELETE APPOINTMENT RECORD FROM THE DATABASE.
|
2017-09-15 14:36:37 +03:00
|
|
|
if ( ! $this->appointments_model->delete($appointment['id']))
|
|
|
|
{
|
2015-10-05 01:32:47 +03:00
|
|
|
throw new Exception('Appointment could not be deleted from the database.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// :: SYNC APPOINTMENT REMOVAL WITH GOOGLE CALENDAR
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($appointment['id_google_calendar'] != NULL)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2016-02-06 02:05:11 +02:00
|
|
|
$google_sync = filter_var($this->providers_model
|
2017-09-15 14:36:37 +03:00
|
|
|
->get_setting('google_sync', $appointment['id_users_provider']), FILTER_VALIDATE_BOOLEAN);
|
2015-10-05 01:32:47 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($google_sync == TRUE)
|
|
|
|
{
|
2015-10-05 01:32:47 +03:00
|
|
|
$google_token = json_decode($this->providers_model
|
2017-09-15 14:36:37 +03:00
|
|
|
->get_setting('google_token', $provider['id']));
|
2016-07-20 23:33:24 +03:00
|
|
|
$this->load->library('Google_sync');
|
2015-10-05 01:32:47 +03:00
|
|
|
$this->google_sync->refresh_token($google_token->refresh_token);
|
|
|
|
$this->google_sync->delete_appointment($provider, $appointment['id_google_calendar']);
|
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
} catch (Exception $exc)
|
|
|
|
{
|
2015-10-05 01:32:47 +03:00
|
|
|
$exceptions[] = $exc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// :: SEND NOTIFICATION EMAILS TO CUSTOMER AND PROVIDER
|
2017-09-15 14:36:37 +03:00
|
|
|
try
|
|
|
|
{
|
2016-07-21 23:36:17 +03:00
|
|
|
$this->config->load('email');
|
|
|
|
$email = new \EA\Engine\Notifications\Email($this, $this->config->config);
|
2015-10-05 01:32:47 +03:00
|
|
|
|
2016-02-06 02:05:11 +02:00
|
|
|
$send_provider = filter_var($this->providers_model
|
2017-09-15 14:36:37 +03:00
|
|
|
->get_setting('notifications', $provider['id']), FILTER_VALIDATE_BOOLEAN);
|
2015-10-05 01:32:47 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($send_provider === TRUE)
|
|
|
|
{
|
2016-07-16 17:56:02 +03:00
|
|
|
$email->sendDeleteAppointment($appointment, $provider,
|
2017-09-15 14:36:37 +03:00
|
|
|
$service, $customer, $company_settings, new Email($provider['email']),
|
|
|
|
new Text($_POST['cancel_reason']));
|
2015-10-05 01:32:47 +03:00
|
|
|
}
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$send_customer = filter_var($this->settings_model->get_setting('customer_notifications'),
|
|
|
|
FILTER_VALIDATE_BOOLEAN);
|
2015-11-25 00:12:37 +02:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($send_customer === TRUE)
|
|
|
|
{
|
|
|
|
$email->sendDeleteAppointment($appointment, $provider,
|
|
|
|
$service, $customer, $company_settings, new Email($customer['email']),
|
|
|
|
new Text($_POST['cancel_reason']));
|
|
|
|
}
|
2015-11-25 00:12:37 +02:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
} catch (Exception $exc)
|
|
|
|
{
|
2015-10-05 01:32:47 +03:00
|
|
|
$exceptions[] = $exc;
|
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
} catch (Exception $exc)
|
|
|
|
{
|
2015-10-05 01:32:47 +03:00
|
|
|
// Display the error message to the customer.
|
|
|
|
$exceptions[] = $exc;
|
|
|
|
}
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$view = [
|
2015-10-05 01:32:47 +03:00
|
|
|
'message_title' => $this->lang->line('appointment_cancelled_title'),
|
|
|
|
'message_text' => $this->lang->line('appointment_cancelled'),
|
2016-03-07 22:19:55 +02:00
|
|
|
'message_icon' => base_url('assets/img/success.png')
|
2017-09-15 14:36:37 +03:00
|
|
|
];
|
2015-10-05 01:32:47 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if (isset($exceptions))
|
|
|
|
{
|
2015-10-05 01:32:47 +03:00
|
|
|
$view['exceptions'] = $exceptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->load->view('appointments/message', $view);
|
|
|
|
}
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
/**
|
2015-11-23 23:58:32 +02:00
|
|
|
* GET an specific appointment book and redirect to the success screen.
|
|
|
|
*
|
2017-09-06 16:22:11 +03:00
|
|
|
* @param int $appointment_id Contains the ID of the appointment to retrieve.
|
2015-11-23 23:58:32 +02:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function book_success($appointment_id)
|
|
|
|
{
|
2015-11-23 23:58:32 +02:00
|
|
|
//if the appointment id doesn't exist or zero redirect to index
|
2017-09-15 14:36:37 +03:00
|
|
|
if ( ! $appointment_id)
|
|
|
|
{
|
2015-11-23 23:58:32 +02:00
|
|
|
redirect('appointments');
|
|
|
|
}
|
|
|
|
$this->load->model('appointments_model');
|
|
|
|
$this->load->model('providers_model');
|
|
|
|
$this->load->model('services_model');
|
|
|
|
$this->load->model('settings_model');
|
|
|
|
//retrieve the data needed in the view
|
2017-09-15 14:36:37 +03:00
|
|
|
$appointment = $this->appointments_model->get_row($appointment_id);
|
2015-11-23 23:58:32 +02:00
|
|
|
$provider = $this->providers_model->get_row($appointment['id_users_provider']);
|
|
|
|
$service = $this->services_model->get_row($appointment['id_services']);
|
|
|
|
$company_name = $this->settings_model->get_setting('company_name');
|
|
|
|
//get the exceptions
|
|
|
|
$exceptions = $this->session->flashdata('book_success');
|
2017-09-15 14:36:37 +03:00
|
|
|
// :: LOAD THE BOOK SUCCESS VIEW
|
|
|
|
$view = [
|
|
|
|
'appointment_data' => $appointment,
|
|
|
|
'provider_data' => $provider,
|
|
|
|
'service_data' => $service,
|
|
|
|
'company_name' => $company_name,
|
|
|
|
];
|
|
|
|
if ($exceptions)
|
|
|
|
{
|
2015-11-23 23:58:32 +02:00
|
|
|
$view['exceptions'] = $exceptions;
|
|
|
|
}
|
|
|
|
$this->load->view('appointments/book_success', $view);
|
|
|
|
}
|
|
|
|
|
2015-10-05 01:32:47 +03:00
|
|
|
/**
|
|
|
|
* [AJAX] Get the available appointment hours for the given date.
|
|
|
|
*
|
2017-09-06 16:22:11 +03:00
|
|
|
* This method answers to an AJAX request. It calculates the available hours for the given service, provider and
|
|
|
|
* date.
|
2015-10-05 01:32:47 +03:00
|
|
|
*
|
2017-09-06 16:22:11 +03:00
|
|
|
* Required POST parameters:
|
|
|
|
*
|
|
|
|
* - int $_POST['service_id'] Selected service record ID.
|
|
|
|
* - int|string $_POST['provider_id'] Selected provider record id, can also be 'any-provider'.
|
|
|
|
* - string $_POST['selected_date'] Selected date for availabilities.
|
|
|
|
* - int $_POST['service_duration'] Selected service duration in minutes.
|
|
|
|
* - string $_POST['manage_mode'] Contains either 'true' or 'false' and determines the if current user
|
2015-12-30 12:44:42 +02:00
|
|
|
* is managing an already booked appointment or not.
|
2016-04-27 09:21:40 +03:00
|
|
|
*
|
2017-09-06 16:22:11 +03:00
|
|
|
* Outputs a JSON string with the availabilities.
|
2015-10-05 01:32:47 +03:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function ajax_get_available_hours()
|
|
|
|
{
|
2015-10-05 01:32:47 +03:00
|
|
|
$this->load->model('providers_model');
|
|
|
|
$this->load->model('appointments_model');
|
|
|
|
$this->load->model('settings_model');
|
2016-07-20 23:06:19 +03:00
|
|
|
$this->load->model('services_model');
|
2015-10-05 01:32:47 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
try
|
|
|
|
{
|
|
|
|
// Do not continue if there was no provider selected (more likely there is no provider in the system).
|
|
|
|
if (empty($_POST['provider_id']))
|
|
|
|
{
|
|
|
|
echo json_encode([]);
|
|
|
|
return;
|
|
|
|
}
|
2015-12-11 00:29:15 +02:00
|
|
|
|
2015-10-05 01:32:47 +03:00
|
|
|
// 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')
|
2017-09-15 14:36:37 +03:00
|
|
|
? [$_POST['appointment_id']]
|
|
|
|
: [];
|
|
|
|
|
|
|
|
// If the user has selected the "any-provider" option then we will need to search
|
|
|
|
// for an available provider that will provide the requested service.
|
|
|
|
if ($_POST['provider_id'] === ANY_PROVIDER)
|
|
|
|
{
|
|
|
|
$_POST['provider_id'] = $this->_search_any_provider($_POST['service_id'], $_POST['selected_date']);
|
|
|
|
if ($_POST['provider_id'] === NULL)
|
|
|
|
{
|
|
|
|
echo json_encode([]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-12-01 01:10:16 +02:00
|
|
|
|
2016-07-20 23:06:19 +03:00
|
|
|
$availabilities_type = $this->services_model->get_value('availabilities_type', $_POST['service_id']);
|
2016-07-21 23:36:17 +03:00
|
|
|
$attendants_number = $this->services_model->get_value('attendants_number', $_POST['service_id']);
|
2016-07-20 23:06:19 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$empty_periods = $this->_get_provider_available_time_periods($_POST['provider_id'],
|
|
|
|
$_POST['selected_date'], $exclude_appointments);
|
2015-10-05 01:32:47 +03:00
|
|
|
|
2016-04-27 09:21:40 +03:00
|
|
|
$available_hours = $this->_calculate_available_hours($empty_periods, $_POST['selected_date'],
|
2017-09-15 14:36:37 +03:00
|
|
|
$_POST['service_duration'], filter_var($_POST['manage_mode'], FILTER_VALIDATE_BOOLEAN),
|
|
|
|
$availabilities_type);
|
2015-10-05 01:32:47 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($attendants_number > 1)
|
|
|
|
{
|
2016-07-21 23:36:17 +03:00
|
|
|
$this->_get_multiple_attendants_hours($available_hours, $attendants_number, $_POST['service_id'],
|
|
|
|
$_POST['selected_date']);
|
|
|
|
}
|
|
|
|
|
2015-10-05 01:32:47 +03:00
|
|
|
echo json_encode($available_hours);
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
} catch (Exception $exc)
|
|
|
|
{
|
|
|
|
echo json_encode([
|
|
|
|
'exceptions' => [exceptionToJavaScript($exc)]
|
|
|
|
]);
|
2015-10-05 01:32:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-18 21:00:01 +03:00
|
|
|
/**
|
|
|
|
* [AJAX] Register the appointment to the database.
|
2015-12-30 12:44:42 +02:00
|
|
|
*
|
2017-09-06 16:22:11 +03:00
|
|
|
* Outputs a JSON string with the appointment ID.
|
2015-10-18 21:00:01 +03:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function ajax_register_appointment()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2015-10-22 00:29:20 +03:00
|
|
|
$post_data = $_POST['post_data']; // alias
|
2017-09-15 14:36:37 +03:00
|
|
|
$post_data['manage_mode'] = filter_var($post_data['manage_mode'], FILTER_VALIDATE_BOOLEAN);
|
2015-10-22 00:29:20 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$this->load->model('appointments_model');
|
2015-12-30 13:54:33 +02:00
|
|
|
$this->load->model('providers_model');
|
|
|
|
$this->load->model('services_model');
|
|
|
|
$this->load->model('customers_model');
|
|
|
|
$this->load->model('settings_model');
|
|
|
|
|
2015-10-22 00:29:20 +03:00
|
|
|
// Validate the CAPTCHA string.
|
2015-12-30 13:54:33 +02:00
|
|
|
if ($this->settings_model->get_setting('require_captcha') === '1'
|
2017-09-15 14:36:37 +03:00
|
|
|
&& $this->session->userdata('captcha_phrase') !== $_POST['captcha'])
|
|
|
|
{
|
|
|
|
echo json_encode([
|
|
|
|
'captcha_verification' => FALSE,
|
|
|
|
'expected_phrase' => $this->session->userdata('captcha_phrase')
|
|
|
|
]);
|
|
|
|
return;
|
2015-10-22 00:29:20 +03:00
|
|
|
}
|
|
|
|
|
2015-11-23 23:58:32 +02:00
|
|
|
// Check appointment availability.
|
2017-09-15 14:36:37 +03:00
|
|
|
if ( ! $this->_check_datetime_availability())
|
|
|
|
{
|
2015-11-23 23:58:32 +02:00
|
|
|
throw new Exception($this->lang->line('requested_hour_is_unavailable'));
|
|
|
|
}
|
|
|
|
|
2015-12-01 01:10:16 +02:00
|
|
|
$appointment = $_POST['post_data']['appointment'];
|
|
|
|
$customer = $_POST['post_data']['customer'];
|
2015-10-18 21:00:01 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($this->customers_model->exists($customer))
|
|
|
|
{
|
2015-12-01 01:10:16 +02:00
|
|
|
$customer['id'] = $this->customers_model->find_record_id($customer);
|
2017-09-15 14:36:37 +03:00
|
|
|
}
|
2015-10-18 21:00:01 +03:00
|
|
|
|
|
|
|
$customer_id = $this->customers_model->add($customer);
|
|
|
|
$appointment['id_users_customer'] = $customer_id;
|
2017-09-15 14:36:37 +03:00
|
|
|
$appointment['is_unavailable'] = (int)$appointment['is_unavailable']; // needs to be type casted
|
2015-10-18 21:00:01 +03:00
|
|
|
$appointment['id'] = $this->appointments_model->add($appointment);
|
|
|
|
$appointment['hash'] = $this->appointments_model->get_value('hash', $appointment['id']);
|
|
|
|
|
|
|
|
$provider = $this->providers_model->get_row($appointment['id_users_provider']);
|
|
|
|
$service = $this->services_model->get_row($appointment['id_services']);
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$company_settings = [
|
2017-09-14 16:18:51 +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'),
|
|
|
|
'date_format' => $this->settings_model->get_setting('date_format')
|
2017-09-15 14:36:37 +03:00
|
|
|
];
|
2015-10-18 21:00:01 +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.
|
2017-09-15 14:36:37 +03:00
|
|
|
try
|
|
|
|
{
|
2016-02-06 02:05:11 +02:00
|
|
|
$google_sync = filter_var($this->providers_model->get_setting('google_sync',
|
2017-09-15 14:36:37 +03:00
|
|
|
$appointment['id_users_provider']), FILTER_VALIDATE_BOOLEAN);
|
2015-10-18 21:00:01 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($google_sync == TRUE)
|
|
|
|
{
|
2015-10-18 21:00:01 +03:00
|
|
|
$google_token = json_decode($this->providers_model
|
2017-09-15 14:36:37 +03:00
|
|
|
->get_setting('google_token', $appointment['id_users_provider']));
|
2015-10-18 21:00:01 +03:00
|
|
|
|
|
|
|
$this->load->library('google_sync');
|
|
|
|
$this->google_sync->refresh_token($google_token->refresh_token);
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($post_data['manage_mode'] === FALSE)
|
|
|
|
{
|
2015-10-18 21:00:01 +03:00
|
|
|
// Add appointment to Google Calendar.
|
|
|
|
$google_event = $this->google_sync->add_appointment($appointment, $provider,
|
2017-09-15 14:36:37 +03:00
|
|
|
$service, $customer, $company_settings);
|
2015-10-18 21:00:01 +03:00
|
|
|
$appointment['id_google_calendar'] = $google_event->id;
|
|
|
|
$this->appointments_model->add($appointment);
|
2017-09-15 14:36:37 +03:00
|
|
|
} else
|
|
|
|
{
|
2015-10-18 21:00:01 +03:00
|
|
|
// Update appointment to Google Calendar.
|
|
|
|
$appointment['id_google_calendar'] = $this->appointments_model
|
2017-09-15 14:36:37 +03:00
|
|
|
->get_value('id_google_calendar', $appointment['id']);
|
2015-10-18 21:00:01 +03:00
|
|
|
|
|
|
|
$this->google_sync->update_appointment($appointment, $provider,
|
2017-09-15 14:36:37 +03:00
|
|
|
$service, $customer, $company_settings);
|
2015-10-18 21:00:01 +03:00
|
|
|
}
|
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
} catch (Exception $exc)
|
|
|
|
{
|
2015-10-18 21:00:01 +03:00
|
|
|
log_message('error', $exc->getMessage());
|
|
|
|
log_message('error', $exc->getTraceAsString());
|
|
|
|
}
|
|
|
|
|
|
|
|
// :: SEND NOTIFICATION EMAILS TO BOTH CUSTOMER AND PROVIDER
|
2017-09-15 14:36:37 +03:00
|
|
|
try
|
|
|
|
{
|
2016-07-16 17:56:02 +03:00
|
|
|
$this->config->load('email');
|
2016-07-21 23:36:17 +03:00
|
|
|
$email = new \EA\Engine\Notifications\Email($this, $this->config->config);
|
2015-10-18 21:00:01 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($post_data['manage_mode'] == FALSE)
|
|
|
|
{
|
2016-11-09 21:56:24 +03:00
|
|
|
$customer_title = new Text($this->lang->line('appointment_booked'));
|
|
|
|
$customer_message = new Text($this->lang->line('thank_you_for_appointment'));
|
|
|
|
$provider_title = new Text($this->lang->line('appointment_added_to_your_plan'));
|
|
|
|
$provider_message = new Text($this->lang->line('appointment_link_description'));
|
2016-03-07 21:59:04 +02:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
} else
|
|
|
|
{
|
2016-11-09 21:56:24 +03:00
|
|
|
$customer_title = new Text($this->lang->line('appointment_changes_saved'));
|
|
|
|
$customer_message = new Text('');
|
|
|
|
$provider_title = new Text($this->lang->line('appointment_details_changed'));
|
|
|
|
$provider_message = new Text('');
|
2015-10-18 21:00:01 +03:00
|
|
|
}
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$customer_link = new Url(site_url('appointments/index/' . $appointment['hash']));
|
|
|
|
$provider_link = new Url(site_url('backend/index/' . $appointment['hash']));
|
2016-03-07 21:59:04 +02:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$send_customer = filter_var($this->settings_model->get_setting('customer_notifications'),
|
|
|
|
FILTER_VALIDATE_BOOLEAN);
|
2015-11-25 00:12:37 +02:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($send_customer === TRUE)
|
|
|
|
{
|
|
|
|
$email->sendAppointmentDetails($appointment, $provider,
|
|
|
|
$service, $customer, $company_settings, $customer_title,
|
|
|
|
$customer_message, $customer_link, new Email($customer['email']));
|
|
|
|
}
|
2015-10-18 21:00:01 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$send_provider = filter_var($this->providers_model->get_setting('notifications', $provider['id']),
|
|
|
|
FILTER_VALIDATE_BOOLEAN);
|
2016-02-06 02:05:11 +02:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($send_provider === TRUE)
|
|
|
|
{
|
2016-07-16 17:56:02 +03:00
|
|
|
$email->sendAppointmentDetails($appointment, $provider,
|
2017-09-15 14:36:37 +03:00
|
|
|
$service, $customer, $company_settings, $provider_title,
|
|
|
|
$provider_message, $provider_link, new Email($provider['email']));
|
2015-10-18 21:00:01 +03:00
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
} catch (Exception $exc)
|
|
|
|
{
|
2015-10-18 21:00:01 +03:00
|
|
|
log_message('error', $exc->getMessage());
|
|
|
|
log_message('error', $exc->getTraceAsString());
|
|
|
|
}
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
echo json_encode([
|
|
|
|
'appointment_id' => $appointment['id']
|
|
|
|
]);
|
2015-10-18 21:00:01 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
} catch (Exception $exc)
|
|
|
|
{
|
|
|
|
echo json_encode([
|
|
|
|
'exceptions' => [exceptionToJavaScript($exc)]
|
|
|
|
]);
|
2015-10-18 21:00:01 +03:00
|
|
|
}
|
|
|
|
}
|
2015-11-23 23:58:32 +02:00
|
|
|
|
2016-04-27 09:21:40 +03:00
|
|
|
/**
|
2017-09-15 14:36:37 +03:00
|
|
|
* [AJAX] Get Unavailable Dates
|
|
|
|
*
|
|
|
|
* Get an array with the available dates of a specific provider, service and month of the year. Provide the
|
2017-09-06 16:22:11 +03:00
|
|
|
* "provider_id", "service_id" and "selected_date" as GET parameters to the request. The "selected_date" parameter
|
|
|
|
* must have the Y-m-d format.
|
2017-09-15 14:36:37 +03:00
|
|
|
*
|
2017-09-06 16:22:11 +03:00
|
|
|
* Outputs a JSON string with the unavailable dates. that are unavailable.
|
2017-09-15 14:36:37 +03:00
|
|
|
*/
|
|
|
|
public function ajax_get_unavailable_dates()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$provider_id = $this->input->get('provider_id');
|
|
|
|
$service_id = $this->input->get('service_id');
|
|
|
|
$selected_date = new DateTime($this->input->get('selected_date'));
|
|
|
|
$number_of_days = (int)$selected_date->format('t');
|
|
|
|
$unavailable_dates = [];
|
|
|
|
|
|
|
|
// Handle the "Any Provider" case.
|
|
|
|
if ($provider_id === ANY_PROVIDER)
|
|
|
|
{
|
|
|
|
$provider_id = $this->_search_any_provider($service_id, $this->input->get('selected_date'));
|
|
|
|
if ($provider_id === NULL)
|
|
|
|
{ // No provider is available in the selected date.
|
|
|
|
for ($i = 1; $i <= $number_of_days; $i++)
|
|
|
|
{
|
|
|
|
$current_date = new DateTime($selected_date->format('Y-m') . '-' . $i);
|
|
|
|
$unavailable_dates[] = $current_date->format('Y-m-d');
|
|
|
|
}
|
|
|
|
echo json_encode($unavailable_dates);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the available time periods for every day of this month.
|
|
|
|
$this->load->model('services_model');
|
2016-07-20 23:06:19 +03:00
|
|
|
$service_duration = (int)$this->services_model->get_value('duration', $service_id);
|
2017-09-15 14:36:37 +03:00
|
|
|
$availabilities_type = (int)$this->services_model->get_value('availabilities_type', $service_id);
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
for ($i = 1; $i <= $number_of_days; $i++)
|
|
|
|
{
|
|
|
|
$current_date = new DateTime($selected_date->format('Y-m') . '-' . $i);
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($current_date < new DateTime(date('Y-m-d 00:00:00')))
|
|
|
|
{ // Past dates become immediately unavailable.
|
|
|
|
$unavailable_dates[] = $current_date->format('Y-m-d');
|
|
|
|
continue;
|
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$empty_periods = $this->_get_provider_available_time_periods($provider_id,
|
|
|
|
$current_date->format('Y-m-d'));
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$available_hours = $this->_calculate_available_hours($empty_periods, $current_date->format('Y-m-d'),
|
|
|
|
$service_duration, FALSE, $availabilities_type);
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if (empty($available_hours))
|
|
|
|
{
|
|
|
|
$unavailable_dates[] = $current_date->format('Y-m-d');
|
|
|
|
}
|
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
echo json_encode($unavailable_dates);
|
|
|
|
} catch (Exception $exc)
|
|
|
|
{
|
|
|
|
echo json_encode([
|
|
|
|
'exceptions' => [exceptionToJavaScript($exc)]
|
|
|
|
]);
|
2016-04-27 09:21:40 +03:00
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2017-09-15 14:36:37 +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
|
2017-09-06 16:22:11 +03:00
|
|
|
* allowed to happen, so one of the two customers will eventually get the preferred 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.
|
|
|
|
*
|
2017-09-15 14:36:37 +03:00
|
|
|
* @return bool Returns whether the selected datetime is still available.
|
|
|
|
*/
|
|
|
|
protected function _check_datetime_availability()
|
|
|
|
{
|
|
|
|
$this->load->model('services_model');
|
|
|
|
$this->load->model('appointments_model');
|
2015-11-23 23:58:32 +02:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$appointment = $_POST['post_data']['appointment'];
|
2015-11-23 23:58:32 +02:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$service_duration = $this->services_model->get_value('duration', $appointment['id_services']);
|
2015-11-23 23:58:32 +02:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$exclude_appointments = (isset($appointment['id'])) ? [$appointment['id']] : [];
|
2015-11-23 23:58:32 +02:00
|
|
|
|
2016-07-21 23:36:17 +03:00
|
|
|
$attendants_number = $this->services_model->get_value('attendants_number', $appointment['id_services']);
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($attendants_number > 1)
|
|
|
|
{
|
2016-07-21 23:36:17 +03:00
|
|
|
// 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']
|
|
|
|
]);
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ( ! empty($exclude) && count($exclude) < $attendants_number)
|
|
|
|
{
|
|
|
|
foreach ($exclude as $entry)
|
|
|
|
{
|
2016-07-21 23:36:17 +03:00
|
|
|
$exclude_appointments[] = $entry['id'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
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'])));
|
|
|
|
$_POST['post_data']['appointment']['id_users_provider'] = $appointment['id_users_provider'];
|
|
|
|
return TRUE; // The selected provider is always available.
|
|
|
|
}
|
2015-12-01 01:10:16 +02:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$available_periods = $this->_get_provider_available_time_periods(
|
|
|
|
$appointment['id_users_provider'], date('Y-m-d', strtotime($appointment['start_datetime'])),
|
|
|
|
$exclude_appointments);
|
2015-11-23 23:58:32 +02:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$is_still_available = FALSE;
|
2015-11-23 23:58:32 +02:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
foreach ($available_periods as $period)
|
|
|
|
{
|
|
|
|
$appt_start = new DateTime($appointment['start_datetime']);
|
|
|
|
$appt_start = $appt_start->format('H:i');
|
2015-11-23 23:58:32 +02:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$appt_end = new DateTime($appointment['start_datetime']);
|
|
|
|
$appt_end->add(new DateInterval('PT' . $service_duration . 'M'));
|
|
|
|
$appt_end = $appt_end->format('H:i');
|
2015-11-23 23:58:32 +02:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$period_start = date('H:i', strtotime($period['start']));
|
|
|
|
$period_end = date('H:i', strtotime($period['end']));
|
2015-11-23 23:58:32 +02:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($period_start <= $appt_start && $period_end >= $appt_end)
|
|
|
|
{
|
|
|
|
$is_still_available = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-11-23 23:58:32 +02:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
return $is_still_available;
|
|
|
|
}
|
2015-11-23 23:58:32 +02:00
|
|
|
|
2017-09-15 14:36:37 +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
|
2017-09-06 16:22:11 +03:00
|
|
|
* available 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.
|
2017-09-15 14:36:37 +03:00
|
|
|
*
|
|
|
|
* @param int $provider_id Provider record ID.
|
|
|
|
* @param string $selected_date Date to be checked (MySQL formatted string).
|
|
|
|
* @param array $exclude_appointments Array containing the IDs of the appointments that will not be taken into
|
2017-09-06 16:22:11 +03:00
|
|
|
* consideration when the available time periods are calculated.
|
2017-09-15 14:36:37 +03:00
|
|
|
*
|
|
|
|
* @return array Returns an array with the available time periods of the provider.
|
|
|
|
*/
|
|
|
|
protected function _get_provider_available_time_periods(
|
|
|
|
$provider_id,
|
|
|
|
$selected_date,
|
|
|
|
$exclude_appointments = []
|
|
|
|
) {
|
|
|
|
$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);
|
|
|
|
|
|
|
|
$where_clause = [
|
|
|
|
'id_users_provider' => $provider_id
|
|
|
|
];
|
|
|
|
|
|
|
|
$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.
|
|
|
|
foreach ($exclude_appointments as $excluded_id)
|
|
|
|
{
|
|
|
|
foreach ($reserved_appointments as $index => $reserved)
|
|
|
|
{
|
|
|
|
if ($reserved['id'] == $excluded_id)
|
|
|
|
{
|
|
|
|
unset($reserved_appointments[$index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 = [];
|
|
|
|
|
|
|
|
if (isset($selected_date_working_plan['breaks']))
|
|
|
|
{
|
|
|
|
$start = new DateTime($selected_date_working_plan['start']);
|
|
|
|
$end = new DateTime($selected_date_working_plan['end']);
|
|
|
|
$available_periods_with_breaks[] = [
|
|
|
|
'start' => $selected_date_working_plan['start'],
|
|
|
|
'end' => $selected_date_working_plan['end']
|
|
|
|
];
|
|
|
|
|
|
|
|
// Split the working plan to available time periods that do not contain the breaks in them.
|
|
|
|
foreach ($selected_date_working_plan['breaks'] as $index => $break)
|
|
|
|
{
|
|
|
|
$break_start = new DateTime($break['start']);
|
|
|
|
$break_end = new DateTime($break['end']);
|
|
|
|
|
|
|
|
if ($break_start < $start)
|
|
|
|
{
|
|
|
|
$break_start = $start;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($break_end > $end)
|
|
|
|
{
|
|
|
|
$break_end = $end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($break_start >= $break_end)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($available_periods_with_breaks as $key => $open_period)
|
|
|
|
{
|
|
|
|
$s = new DateTime($open_period['start']);
|
|
|
|
$e = new DateTime($open_period['end']);
|
|
|
|
|
|
|
|
if ($s < $break_end && $break_start < $e)
|
|
|
|
{ // check for overlap
|
|
|
|
$changed = FALSE;
|
|
|
|
if ($s < $break_start)
|
|
|
|
{
|
|
|
|
$open_start = $s;
|
|
|
|
$open_end = $break_start;
|
|
|
|
$available_periods_with_breaks[] = [
|
|
|
|
'start' => $open_start->format("H:i"),
|
|
|
|
'end' => $open_end->format("H:i")
|
|
|
|
];
|
|
|
|
$changed = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($break_end < $e)
|
|
|
|
{
|
|
|
|
$open_start = $break_end;
|
|
|
|
$open_end = $e;
|
|
|
|
$available_periods_with_breaks[] = [
|
|
|
|
'start' => $open_start->format("H:i"),
|
|
|
|
'end' => $open_end->format("H:i")
|
|
|
|
];
|
|
|
|
$changed = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($changed)
|
|
|
|
{
|
|
|
|
unset($available_periods_with_breaks[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Break the empty periods with the reserved appointments.
|
|
|
|
$available_periods_with_appointments = $available_periods_with_breaks;
|
|
|
|
|
|
|
|
foreach ($reserved_appointments as $appointment)
|
|
|
|
{
|
|
|
|
foreach ($available_periods_with_appointments as $index => &$period)
|
|
|
|
{
|
|
|
|
$a_start = strtotime($appointment['start_datetime']);
|
|
|
|
$a_end = strtotime($appointment['end_datetime']);
|
|
|
|
$p_start = strtotime($selected_date . ' ' . $period['start']);
|
|
|
|
$p_end = strtotime($selected_date . ' ' . $period['end']);
|
|
|
|
|
|
|
|
if ($a_start <= $p_start && $a_end <= $p_end && $a_end <= $p_start)
|
|
|
|
{
|
|
|
|
// The appointment does not belong in this time period, so we
|
|
|
|
// will not change anything.
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
];
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
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.
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_values($available_periods_with_appointments);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search for any provider that can handle the requested service.
|
|
|
|
*
|
|
|
|
* This method will return the database ID of the provider with the most available periods.
|
|
|
|
*
|
|
|
|
* @param int $service_id The requested service ID.
|
|
|
|
* @param string $selected_date The date to be searched.
|
|
|
|
*
|
|
|
|
* @return int Returns the ID of the provider that can provide the service at the selected date.
|
|
|
|
*/
|
|
|
|
protected function _search_any_provider($service_id, $selected_date)
|
|
|
|
{
|
|
|
|
$this->load->model('providers_model');
|
|
|
|
$this->load->model('services_model');
|
|
|
|
$available_providers = $this->providers_model->get_available_providers();
|
|
|
|
$service = $this->services_model->get_row($service_id);
|
|
|
|
$provider_id = NULL;
|
|
|
|
$max_hours_count = 0;
|
|
|
|
|
|
|
|
foreach ($available_providers as $provider)
|
|
|
|
{
|
|
|
|
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'], FALSE, $service['availabilities_type']);
|
|
|
|
if (count($available_hours) > $max_hours_count)
|
|
|
|
{
|
|
|
|
$provider_id = $provider['id'];
|
|
|
|
$max_hours_count = count($available_hours);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $provider_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the available appointment hours.
|
|
|
|
*
|
|
|
|
* 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 "_get_provider_available_time_periods"
|
2017-09-06 16:22:11 +03:00
|
|
|
* method.
|
2017-09-15 14:36:37 +03:00
|
|
|
* @param string $selected_date The selected date to be search (format )
|
|
|
|
* @param int $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).
|
2016-10-10 19:29:48 +03:00
|
|
|
* @param string $availabilities_type Optional ('flexible'), the service availabilities type.
|
2017-09-15 14:36:37 +03:00
|
|
|
*
|
|
|
|
* @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,
|
|
|
|
$availabilities_type = 'flexible'
|
|
|
|
) {
|
|
|
|
$this->load->model('settings_model');
|
|
|
|
|
|
|
|
$available_hours = [];
|
|
|
|
|
|
|
|
foreach ($empty_periods as $period)
|
|
|
|
{
|
|
|
|
$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;
|
2015-12-30 12:44:42 +02:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$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('PT' . $interval . 'M'));
|
|
|
|
$diff = $current_hour->diff($end_hour);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
if (date('m/d/Y', strtotime($selected_date)) === date('m/d/Y'))
|
|
|
|
{
|
|
|
|
$book_advance_timeout = $this->settings_model->get_setting('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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$available_hours = array_values($available_hours);
|
|
|
|
sort($available_hours, SORT_STRING);
|
|
|
|
$available_hours = array_values($available_hours);
|
|
|
|
|
|
|
|
return $available_hours;
|
|
|
|
}
|
2016-07-21 23:36:17 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
protected function _get_multiple_attendants_hours(
|
|
|
|
&$available_hours,
|
|
|
|
$attendants_number,
|
|
|
|
$service_id,
|
|
|
|
$selected_date
|
|
|
|
) {
|
2016-07-21 23:36:17 +03:00
|
|
|
$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))) . ')');
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
foreach ($appointments as $appointment)
|
|
|
|
{
|
2016-07-21 23:36:17 +03:00
|
|
|
$hour = date('H:i', strtotime($appointment['start_datetime']));
|
|
|
|
$current_attendants_number = $this->appointments_model->appointment_count_for_hour($service_id,
|
2017-09-15 14:36:37 +03:00
|
|
|
$selected_date, $hour);
|
|
|
|
if ($current_attendants_number < $attendants_number && ! in_array($hour, $available_hours))
|
|
|
|
{
|
2016-07-21 23:36:17 +03:00
|
|
|
$available_hours[] = $hour;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$available_hours = array_values($available_hours);
|
2017-09-15 14:36:37 +03:00
|
|
|
sort($available_hours, SORT_STRING);
|
|
|
|
$available_hours = array_values($available_hours);
|
2016-07-21 23:36:17 +03:00
|
|
|
}
|
2015-10-05 01:32:47 +03:00
|
|
|
}
|