2020-04-22 22:48:56 +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>
|
2020-03-11 12:10:59 +03:00
|
|
|
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
|
2020-11-14 22:36:25 +03:00
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link https://easyappointments.org
|
2015-10-05 01:32:47 +03:00
|
|
|
* @since v1.0.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Appointments Controller
|
|
|
|
*
|
|
|
|
* @package Controllers
|
|
|
|
*/
|
2020-11-16 11:29:36 +03:00
|
|
|
class Appointments extends EA_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
|
|
|
|
|
|
|
$this->load->helper('installation');
|
2020-04-22 22:48:56 +03:00
|
|
|
$this->load->helper('google_analytics');
|
2020-11-14 22:36:25 +03:00
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
$this->load->model('appointments_model');
|
|
|
|
$this->load->model('providers_model');
|
2020-09-23 12:42:18 +03:00
|
|
|
$this->load->model('admins_model');
|
|
|
|
$this->load->model('secretaries_model');
|
2020-04-22 22:48:56 +03:00
|
|
|
$this->load->model('services_model');
|
|
|
|
$this->load->model('customers_model');
|
|
|
|
$this->load->model('settings_model');
|
2020-11-14 22:36:25 +03:00
|
|
|
|
2020-05-12 21:50:55 +03:00
|
|
|
$this->load->library('timezones');
|
2020-10-21 21:37:47 +03:00
|
|
|
$this->load->library('synchronization');
|
|
|
|
$this->load->library('notifications');
|
|
|
|
$this->load->library('availability');
|
2015-10-18 21:00:01 +03:00
|
|
|
|
2020-11-14 22:36:25 +03:00
|
|
|
$this->load->driver('cache', ['adapter' => 'file']);
|
2017-09-15 14:36:37 +03:00
|
|
|
}
|
2015-10-05 01:32:47 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Default callback method of the application.
|
|
|
|
*
|
2020-04-22 22:48:56 +03:00
|
|
|
* 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.
|
2015-10-05 01:32:47 +03:00
|
|
|
*
|
2020-04-22 22:48:56 +03:00
|
|
|
* @param string $appointment_hash The appointment hash identifier.
|
2015-10-05 01:32:47 +03:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function index($appointment_hash = '')
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-09-07 11:15:58 +03:00
|
|
|
if ( ! is_app_installed())
|
2020-04-22 22:48:56 +03:00
|
|
|
{
|
|
|
|
redirect('installation/index');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$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');
|
2020-06-05 19:24:05 +03:00
|
|
|
$book_advance_timeout = $this->settings_model->get_setting('book_advance_timeout');
|
2017-09-15 14:36:37 +03:00
|
|
|
$date_format = $this->settings_model->get_setting('date_format');
|
2018-03-06 14:20:47 +03:00
|
|
|
$time_format = $this->settings_model->get_setting('time_format');
|
2018-06-29 01:41:17 +03:00
|
|
|
$first_weekday = $this->settings_model->get_setting('first_weekday');
|
2019-10-18 05:37:52 +03:00
|
|
|
$require_phone_number = $this->settings_model->get_setting('require_phone_number');
|
2018-06-24 17:58:03 +03:00
|
|
|
$display_cookie_notice = $this->settings_model->get_setting('display_cookie_notice');
|
|
|
|
$cookie_notice_content = $this->settings_model->get_setting('cookie_notice_content');
|
|
|
|
$display_terms_and_conditions = $this->settings_model->get_setting('display_terms_and_conditions');
|
|
|
|
$terms_and_conditions_content = $this->settings_model->get_setting('terms_and_conditions_content');
|
|
|
|
$display_privacy_policy = $this->settings_model->get_setting('display_privacy_policy');
|
|
|
|
$privacy_policy_content = $this->settings_model->get_setting('privacy_policy_content');
|
2020-04-06 21:36:22 +03:00
|
|
|
$display_any_provider = $this->settings_model->get_setting('display_any_provider');
|
2020-05-12 21:50:55 +03:00
|
|
|
$timezones = $this->timezones->to_array();
|
2017-09-15 14:36:37 +03:00
|
|
|
|
|
|
|
// 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'],
|
2020-03-29 17:20:30 +03:00
|
|
|
'services' => $provider['services'],
|
|
|
|
'timezone' => $provider['timezone']
|
2017-09-15 14:36:37 +03:00
|
|
|
];
|
|
|
|
$available_providers[$index] = $stripped_data;
|
|
|
|
}
|
2016-01-01 16:52:43 +02:00
|
|
|
|
2020-04-22 22:48:56 +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
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
if (empty($results))
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-04-22 22:48:56 +03:00
|
|
|
// The requested appointment doesn't exist in the database. Display a message to the customer.
|
|
|
|
$variables = [
|
|
|
|
'message_title' => lang('appointment_not_found'),
|
|
|
|
'message_text' => lang('appointment_does_not_exist_in_db'),
|
2017-09-15 14:36:37 +03:00
|
|
|
'message_icon' => base_url('assets/img/error.png')
|
|
|
|
];
|
2020-04-22 22:48:56 +03:00
|
|
|
|
|
|
|
$this->load->view('appointments/message', $variables);
|
|
|
|
|
2015-10-18 21:00:01 +03:00
|
|
|
return;
|
2015-10-05 01:32:47 +03:00
|
|
|
}
|
|
|
|
|
2020-10-21 21:37:47 +03:00
|
|
|
// If the requested appointment begin date is lower than book_advance_timeout. Display a message to the
|
|
|
|
// customer.
|
2020-06-05 19:24:05 +03:00
|
|
|
$startDate = strtotime($results[0]['start_datetime']);
|
|
|
|
$limit = strtotime('+' . $book_advance_timeout . ' minutes', strtotime('now'));
|
|
|
|
|
|
|
|
if ($startDate < $limit)
|
|
|
|
{
|
|
|
|
$hours = floor($book_advance_timeout / 60);
|
|
|
|
$minutes = ($book_advance_timeout % 60);
|
2020-10-20 16:03:48 +03:00
|
|
|
|
2020-06-05 19:24:05 +03:00
|
|
|
$view = [
|
2020-12-02 23:11:07 +03:00
|
|
|
'message_title' => lang('appointment_locked'),
|
|
|
|
'message_text' => strtr(lang('appointment_locked_message'), [
|
2020-06-05 19:24:05 +03:00
|
|
|
'{$limit}' => sprintf('%02d:%02d', $hours, $minutes)
|
|
|
|
]),
|
|
|
|
'message_icon' => base_url('assets/img/error.png')
|
|
|
|
];
|
|
|
|
$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
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
$customer_token = md5(uniqid(mt_rand(), TRUE));
|
2018-06-24 20:08:45 +03:00
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
// Save the token for 10 minutes.
|
|
|
|
$this->cache->save('customer-token-' . $customer_token, $customer['id'], 600);
|
2017-12-01 11:12:09 +03:00
|
|
|
}
|
|
|
|
else
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-04-22 22:48:56 +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;
|
2018-06-24 20:08:45 +03:00
|
|
|
$customer_token = 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.
|
2020-04-22 22:48:56 +03:00
|
|
|
$variables = [
|
2017-09-15 14:36:37 +03:00
|
|
|
'available_services' => $available_services,
|
|
|
|
'available_providers' => $available_providers,
|
|
|
|
'company_name' => $company_name,
|
|
|
|
'manage_mode' => $manage_mode,
|
2018-06-24 20:08:45 +03:00
|
|
|
'customer_token' => $customer_token,
|
2017-09-15 14:36:37 +03:00
|
|
|
'date_format' => $date_format,
|
2018-03-06 14:20:47 +03:00
|
|
|
'time_format' => $time_format,
|
2018-06-29 01:41:17 +03:00
|
|
|
'first_weekday' => $first_weekday,
|
2019-10-18 05:37:52 +03:00
|
|
|
'require_phone_number' => $require_phone_number,
|
2017-09-15 14:36:37 +03:00
|
|
|
'appointment_data' => $appointment,
|
|
|
|
'provider_data' => $provider,
|
2018-06-24 17:58:03 +03:00
|
|
|
'customer_data' => $customer,
|
|
|
|
'display_cookie_notice' => $display_cookie_notice,
|
|
|
|
'cookie_notice_content' => $cookie_notice_content,
|
|
|
|
'display_terms_and_conditions' => $display_terms_and_conditions,
|
|
|
|
'terms_and_conditions_content' => $terms_and_conditions_content,
|
|
|
|
'display_privacy_policy' => $display_privacy_policy,
|
|
|
|
'privacy_policy_content' => $privacy_policy_content,
|
2020-03-29 17:20:30 +03:00
|
|
|
'timezones' => $timezones,
|
2020-04-06 21:36:22 +03:00
|
|
|
'display_any_provider' => $display_any_provider,
|
2017-09-15 14:36:37 +03:00
|
|
|
];
|
2017-09-23 02:30:22 +03:00
|
|
|
}
|
2020-04-22 22:48:56 +03:00
|
|
|
catch (Exception $exception)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-04-22 22:48:56 +03:00
|
|
|
$variables['exceptions'][] = $exception;
|
2015-10-05 01:32:47 +03:00
|
|
|
}
|
2015-10-18 21:00:01 +03:00
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
$this->load->view('appointments/book', $variables);
|
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
|
2020-04-22 22:48:56 +03:00
|
|
|
* yet.
|
2015-10-05 01:32:47 +03:00
|
|
|
*
|
2020-04-22 22:48:56 +03:00
|
|
|
* @param string $appointment_hash This appointment hash identifier.
|
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
|
|
|
// Check whether the appointment hash exists in the database.
|
2020-04-22 22:48:56 +03:00
|
|
|
$appointments = $this->appointments_model->get_batch(['hash' => $appointment_hash]);
|
|
|
|
|
|
|
|
if (empty($appointments))
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2015-10-05 01:32:47 +03:00
|
|
|
throw new Exception('No record matches the provided hash.');
|
|
|
|
}
|
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
$appointment = $appointments[0];
|
2015-10-05 01:32:47 +03:00
|
|
|
$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']);
|
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
$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'),
|
2017-10-31 13:42:53 +03:00
|
|
|
'company_link' => $this->settings_model->get_setting('company_link'),
|
2018-03-06 14:20:47 +03:00
|
|
|
'date_format' => $this->settings_model->get_setting('date_format'),
|
|
|
|
'time_format' => $this->settings_model->get_setting('time_format')
|
2017-09-15 14:36:37 +03:00
|
|
|
];
|
2015-10-05 01:32:47 +03:00
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
// Remove the appointment record from the data.
|
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.');
|
|
|
|
}
|
|
|
|
|
2020-10-21 21:37:47 +03:00
|
|
|
$this->synchronization->sync_appointment_deleted($appointment, $provider);
|
|
|
|
$this->notifications->notify_appointment_deleted($appointment, $service, $provider, $customer, $settings);
|
2017-09-23 02:30:22 +03:00
|
|
|
}
|
2020-04-22 22:48:56 +03:00
|
|
|
catch (Exception $exception)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2015-10-05 01:32:47 +03:00
|
|
|
// Display the error message to the customer.
|
2020-04-22 22:48:56 +03:00
|
|
|
$exceptions[] = $exception;
|
2015-10-05 01:32:47 +03:00
|
|
|
}
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$view = [
|
2020-04-22 22:48:56 +03:00
|
|
|
'message_title' => lang('appointment_cancelled_title'),
|
|
|
|
'message_text' => lang('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.
|
|
|
|
*
|
2020-04-22 22:48:56 +03:00
|
|
|
* @param string $appointment_hash The appointment hash identifier.
|
|
|
|
*
|
|
|
|
* @throws Exception
|
2015-11-23 23:58:32 +02:00
|
|
|
*/
|
2020-04-22 22:48:56 +03:00
|
|
|
public function book_success($appointment_hash)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-04-22 22:48:56 +03:00
|
|
|
$appointments = $this->appointments_model->get_batch(['hash' => $appointment_hash]);
|
|
|
|
|
|
|
|
if (empty($appointments))
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-04-22 22:48:56 +03:00
|
|
|
redirect('appointments'); // The appointment does not exist.
|
|
|
|
return;
|
2015-11-23 23:58:32 +02:00
|
|
|
}
|
2020-04-22 22:48:56 +03:00
|
|
|
|
|
|
|
$appointment = $appointments[0];
|
2020-04-08 11:30:27 +03:00
|
|
|
unset($appointment['notes']);
|
|
|
|
|
2015-11-23 23:58:32 +02:00
|
|
|
$provider = $this->providers_model->get_row($appointment['id_users_provider']);
|
2020-04-08 11:30:27 +03:00
|
|
|
|
2015-11-23 23:58:32 +02:00
|
|
|
$service = $this->services_model->get_row($appointment['id_services']);
|
2020-04-08 11:30:27 +03:00
|
|
|
|
2015-11-23 23:58:32 +02:00
|
|
|
$company_name = $this->settings_model->get_setting('company_name');
|
2019-12-04 11:48:57 +03:00
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
// Get any pending exceptions.
|
|
|
|
$exceptions = $this->session->flashdata('book_success');
|
2019-12-04 11:48:57 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$view = [
|
|
|
|
'appointment_data' => $appointment,
|
2020-11-21 23:02:55 +03:00
|
|
|
'provider_data' => [
|
|
|
|
'id' => $provider['id'],
|
|
|
|
'first_name' => $provider['first_name'],
|
|
|
|
'last_name' => $provider['last_name'],
|
|
|
|
'email' => $provider['email'],
|
|
|
|
'timezone' => $provider['timezone'],
|
|
|
|
],
|
2017-09-15 14:36:37 +03:00
|
|
|
'service_data' => $service,
|
|
|
|
'company_name' => $company_name,
|
|
|
|
];
|
2020-04-22 22:48:56 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($exceptions)
|
|
|
|
{
|
2015-11-23 23:58:32 +02:00
|
|
|
$view['exceptions'] = $exceptions;
|
|
|
|
}
|
2020-04-22 22:48:56 +03:00
|
|
|
|
2015-11-23 23:58:32 +02:00
|
|
|
$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
|
|
|
* 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()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-04-22 22:48:56 +03:00
|
|
|
$provider_id = $this->input->post('provider_id');
|
|
|
|
$service_id = $this->input->post('service_id');
|
|
|
|
$selected_date = $this->input->post('selected_date');
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
// Do not continue if there was no provider selected (more likely there is no provider in the system).
|
2020-04-22 22:48:56 +03:00
|
|
|
if (empty($provider_id))
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2017-09-20 17:09:01 +03:00
|
|
|
$this->output
|
|
|
|
->set_content_type('application/json')
|
|
|
|
->set_output(json_encode([]));
|
2020-04-22 22:48:56 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
return;
|
|
|
|
}
|
2015-12-11 00:29:15 +02:00
|
|
|
|
2020-04-22 22:48:56 +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.
|
2020-10-21 21:37:47 +03:00
|
|
|
$exclude_appointment_id = $this->input->post('manage_mode') === 'true' ? $this->input->post('appointment_id') : NULL;
|
2020-04-22 22:48:56 +03:00
|
|
|
|
|
|
|
// 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 ($provider_id === ANY_PROVIDER)
|
|
|
|
{
|
2020-11-12 15:15:38 +03:00
|
|
|
$provider_id = $this->search_any_provider($selected_date, $service_id);
|
2020-04-22 22:48:56 +03:00
|
|
|
|
|
|
|
if ($provider_id === NULL)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2017-09-20 17:09:01 +03:00
|
|
|
$this->output
|
|
|
|
->set_content_type('application/json')
|
|
|
|
->set_output(json_encode([]));
|
2020-04-22 22:48:56 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-12-01 01:10:16 +02:00
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
$service = $this->services_model->get_row($service_id);
|
2015-10-05 01:32:47 +03:00
|
|
|
|
2020-10-21 21:37:47 +03:00
|
|
|
$provider = $this->providers_model->get_row($provider_id);
|
2016-07-21 23:36:17 +03:00
|
|
|
|
2020-10-21 21:37:47 +03:00
|
|
|
$response = $this->availability->get_available_hours($selected_date, $service, $provider, $exclude_appointment_id);
|
2017-09-23 02:30:22 +03:00
|
|
|
}
|
2020-04-22 22:48:56 +03:00
|
|
|
catch (Exception $exception)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-04-22 22:48:56 +03:00
|
|
|
$this->output->set_status_header(500);
|
|
|
|
|
|
|
|
$response = [
|
|
|
|
'message' => $exception->getMessage(),
|
|
|
|
'trace' => config('debug') ? $exception->getTrace() : []
|
|
|
|
];
|
2015-10-05 01:32:47 +03:00
|
|
|
}
|
2020-04-22 22:48:56 +03:00
|
|
|
|
|
|
|
$this->output
|
|
|
|
->set_content_type('application/json')
|
|
|
|
->set_output(json_encode($response));
|
2015-10-05 01:32:47 +03:00
|
|
|
}
|
|
|
|
|
2015-10-18 21:00:01 +03:00
|
|
|
/**
|
2020-04-22 22:48:56 +03:00
|
|
|
* Search for any provider that can handle the requested service.
|
2015-12-30 12:44:42 +02:00
|
|
|
*
|
2020-04-22 22:48:56 +03:00
|
|
|
* This method will return the database ID of the provider with the most available periods.
|
|
|
|
*
|
2020-10-21 21:37:47 +03:00
|
|
|
* @param string $date The date to be searched (Y-m-d).
|
2020-04-22 22:48:56 +03:00
|
|
|
* @param int $service_id The requested service ID.
|
|
|
|
*
|
|
|
|
* @return int Returns the ID of the provider that can provide the service at the selected date.
|
|
|
|
*
|
|
|
|
* @throws Exception
|
2015-10-18 21:00:01 +03:00
|
|
|
*/
|
2020-10-21 21:37:47 +03:00
|
|
|
protected function search_any_provider($date, $service_id)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-04-22 22:48:56 +03:00
|
|
|
$available_providers = $this->providers_model->get_available_providers();
|
2019-06-08 17:43:21 +03:00
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
$service = $this->services_model->get_row($service_id);
|
2019-06-08 17:43:21 +03:00
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
$provider_id = NULL;
|
2015-10-18 21:00:01 +03:00
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
$max_hours_count = 0;
|
2015-10-18 21:00:01 +03:00
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
foreach ($available_providers as $provider)
|
|
|
|
{
|
|
|
|
foreach ($provider['services'] as $provider_service_id)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-04-22 22:48:56 +03:00
|
|
|
if ($provider_service_id == $service_id)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-04-22 22:48:56 +03:00
|
|
|
// Check if the provider is available for the requested date.
|
2020-10-21 21:37:47 +03:00
|
|
|
$available_hours = $this->availability->get_available_hours($date, $service, $provider);
|
2015-10-18 21:00:01 +03:00
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
if (count($available_hours) > $max_hours_count)
|
|
|
|
{
|
|
|
|
$provider_id = $provider['id'];
|
|
|
|
$max_hours_count = count($available_hours);
|
2015-10-18 21:00:01 +03:00
|
|
|
}
|
|
|
|
}
|
2017-09-23 02:30:22 +03:00
|
|
|
}
|
2015-10-18 21:00:01 +03:00
|
|
|
}
|
2020-04-22 22:48:56 +03:00
|
|
|
|
|
|
|
return $provider_id;
|
2015-10-18 21:00:01 +03:00
|
|
|
}
|
2015-11-23 23:58:32 +02:00
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* [AJAX] Register the appointment to the database.
|
|
|
|
*
|
|
|
|
* Outputs a JSON string with the appointment ID.
|
|
|
|
*/
|
|
|
|
public function ajax_register_appointment()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$post_data = $this->input->post('post_data');
|
|
|
|
$captcha = $this->input->post('captcha');
|
|
|
|
$manage_mode = filter_var($post_data['manage_mode'], FILTER_VALIDATE_BOOLEAN);
|
|
|
|
$appointment = $post_data['appointment'];
|
|
|
|
$customer = $post_data['customer'];
|
|
|
|
|
2020-09-21 14:26:15 +03:00
|
|
|
// Check appointment availability before registering it to the database.
|
|
|
|
$appointment['id_users_provider'] = $this->check_datetime_availability();
|
|
|
|
|
|
|
|
if ( ! $appointment['id_users_provider'])
|
|
|
|
{
|
|
|
|
throw new Exception(lang('requested_hour_is_unavailable'));
|
|
|
|
}
|
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
$provider = $this->providers_model->get_row($appointment['id_users_provider']);
|
|
|
|
$service = $this->services_model->get_row($appointment['id_services']);
|
|
|
|
|
|
|
|
$require_captcha = $this->settings_model->get_setting('require_captcha');
|
|
|
|
$captcha_phrase = $this->session->userdata('captcha_phrase');
|
|
|
|
|
|
|
|
// Validate the CAPTCHA string.
|
|
|
|
if ($require_captcha === '1' && $captcha_phrase !== $captcha)
|
|
|
|
{
|
|
|
|
$this->output
|
|
|
|
->set_content_type('application/json')
|
|
|
|
->set_output(json_encode([
|
|
|
|
'captcha_verification' => FALSE
|
|
|
|
]));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->customers_model->exists($customer))
|
|
|
|
{
|
|
|
|
$customer['id'] = $this->customers_model->find_record_id($customer);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($appointment['location']) && ! empty($service['location']))
|
|
|
|
{
|
|
|
|
$appointment['location'] = $service['location'];
|
|
|
|
}
|
|
|
|
|
2020-09-23 13:48:07 +03:00
|
|
|
// Save customer language (the language which is used to render the booking page).
|
2020-12-02 23:12:54 +03:00
|
|
|
$customer['language'] = config('language');
|
2020-04-22 22:48:56 +03:00
|
|
|
$customer_id = $this->customers_model->add($customer);
|
|
|
|
|
|
|
|
$appointment['id_users_customer'] = $customer_id;
|
|
|
|
$appointment['is_unavailable'] = (int)$appointment['is_unavailable']; // needs to be type casted
|
|
|
|
$appointment['id'] = $this->appointments_model->add($appointment);
|
|
|
|
$appointment['hash'] = $this->appointments_model->get_value('hash', $appointment['id']);
|
|
|
|
|
|
|
|
$settings = [
|
|
|
|
'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'),
|
|
|
|
'time_format' => $this->settings_model->get_setting('time_format')
|
|
|
|
];
|
|
|
|
|
2020-12-05 12:46:05 +03:00
|
|
|
$this->synchronization->sync_appointment_saved($appointment, $service, $provider, $customer, $settings, $manage_mode);
|
2020-10-21 21:37:47 +03:00
|
|
|
$this->notifications->notify_appointment_saved($appointment, $service, $provider, $customer, $settings, $manage_mode);
|
2020-04-22 22:48:56 +03:00
|
|
|
|
|
|
|
$response = [
|
|
|
|
'appointment_id' => $appointment['id'],
|
|
|
|
'appointment_hash' => $appointment['hash']
|
|
|
|
];
|
|
|
|
}
|
|
|
|
catch (Exception $exception)
|
|
|
|
{
|
|
|
|
$this->output->set_status_header(500);
|
|
|
|
|
|
|
|
$response = [
|
|
|
|
'message' => $exception->getMessage(),
|
|
|
|
'trace' => config('debug') ? $exception->getTrace() : []
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->output
|
|
|
|
->set_content_type('application/json')
|
|
|
|
->set_output(json_encode($response));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 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.
|
|
|
|
*
|
|
|
|
* @return int Returns the ID of the provider that is available for the appointment.
|
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
protected function check_datetime_availability()
|
|
|
|
{
|
|
|
|
$post_data = $this->input->post('post_data');
|
|
|
|
|
|
|
|
$appointment = $post_data['appointment'];
|
|
|
|
|
2020-10-21 21:37:47 +03:00
|
|
|
$date = date('Y-m-d', strtotime($appointment['start_datetime']));
|
2020-04-22 22:48:56 +03:00
|
|
|
|
2020-10-21 21:37:47 +03:00
|
|
|
if ($appointment['id_users_provider'] === ANY_PROVIDER)
|
2020-04-22 22:48:56 +03:00
|
|
|
{
|
|
|
|
|
2020-10-21 21:37:47 +03:00
|
|
|
$appointment['id_users_provider'] = $this->search_any_provider($date, $appointment['id_services']);
|
2020-04-22 22:48:56 +03:00
|
|
|
|
2020-10-21 21:37:47 +03:00
|
|
|
return $appointment['id_users_provider'];
|
2020-04-22 22:48:56 +03:00
|
|
|
}
|
|
|
|
|
2020-10-21 21:37:47 +03:00
|
|
|
$service = $this->services_model->get_row($appointment['id_services']);
|
2020-04-22 22:48:56 +03:00
|
|
|
|
2020-10-21 21:37:47 +03:00
|
|
|
$exclude_appointment_id = isset($appointment['id']) ? $appointment['id'] : NULL;
|
|
|
|
|
|
|
|
$provider = $this->providers_model->get_row($appointment['id_users_provider']);
|
2020-04-22 22:48:56 +03:00
|
|
|
|
2020-11-12 15:46:51 +03:00
|
|
|
$available_hours = $this->availability->get_available_hours($date, $service, $provider, $exclude_appointment_id);
|
2020-04-22 22:48:56 +03:00
|
|
|
|
|
|
|
$is_still_available = FALSE;
|
|
|
|
|
2020-11-12 15:46:51 +03:00
|
|
|
$appointment_hour = date('H:i', strtotime($appointment['start_datetime']));
|
2020-04-22 22:48:56 +03:00
|
|
|
|
2020-11-12 15:46:51 +03:00
|
|
|
foreach ($available_hours as $available_hour)
|
|
|
|
{
|
|
|
|
if ($appointment_hour === $available_hour)
|
2020-04-22 22:48:56 +03:00
|
|
|
{
|
|
|
|
$is_still_available = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $is_still_available ? $appointment['id_users_provider'] : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* [AJAX] Get Unavailable Dates
|
|
|
|
*
|
|
|
|
* Get an array with the available dates of a specific provider, service and month of the year. Provide the
|
|
|
|
* "provider_id", "service_id" and "selected_date" as GET parameters to the request. The "selected_date" parameter
|
|
|
|
* must have the Y-m-d format.
|
|
|
|
*
|
|
|
|
* Outputs a JSON string with the unavailable dates. that are unavailable.
|
|
|
|
*/
|
|
|
|
public function ajax_get_unavailable_dates()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$provider_id = $this->input->get('provider_id');
|
|
|
|
$service_id = $this->input->get('service_id');
|
|
|
|
$appointment_id = $this->input->get_post('appointment_id');
|
|
|
|
$manage_mode = $this->input->get_post('manage_mode');
|
|
|
|
$selected_date_string = $this->input->get('selected_date');
|
|
|
|
$selected_date = new DateTime($selected_date_string);
|
|
|
|
$number_of_days_in_month = (int)$selected_date->format('t');
|
|
|
|
$unavailable_dates = [];
|
|
|
|
|
2020-10-21 21:37:47 +03:00
|
|
|
$provider_ids = $provider_id === ANY_PROVIDER
|
2020-04-22 22:48:56 +03:00
|
|
|
? $this->search_providers_by_service($service_id)
|
|
|
|
: [$provider_id];
|
|
|
|
|
2020-10-21 21:37:47 +03:00
|
|
|
$exclude_appointment_id = $manage_mode ? $appointment_id : NULL;
|
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
// Get the service record.
|
|
|
|
$service = $this->services_model->get_row($service_id);
|
|
|
|
|
|
|
|
for ($i = 1; $i <= $number_of_days_in_month; $i++)
|
|
|
|
{
|
|
|
|
$current_date = new DateTime($selected_date->format('Y-m') . '-' . $i);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finding at least one slot of availability.
|
2020-10-21 21:37:47 +03:00
|
|
|
foreach ($provider_ids as $current_provider_id)
|
2020-04-22 22:48:56 +03:00
|
|
|
{
|
2020-10-21 21:37:47 +03:00
|
|
|
$provider = $this->providers_model->get_row($current_provider_id);
|
2020-04-22 22:48:56 +03:00
|
|
|
|
2020-10-21 21:37:47 +03:00
|
|
|
$available_hours = $this->availability->get_available_hours(
|
|
|
|
$current_date->format('Y-m-d'),
|
|
|
|
$service,
|
|
|
|
$provider,
|
|
|
|
$exclude_appointment_id
|
|
|
|
);
|
2020-04-22 22:48:56 +03:00
|
|
|
|
|
|
|
if ( ! empty($available_hours))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No availability amongst all the provider.
|
|
|
|
if (empty($available_hours))
|
|
|
|
{
|
|
|
|
$unavailable_dates[] = $current_date->format('Y-m-d');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = $unavailable_dates;
|
|
|
|
}
|
|
|
|
catch (Exception $exception)
|
|
|
|
{
|
|
|
|
$this->output->set_status_header(500);
|
|
|
|
|
|
|
|
$response = [
|
|
|
|
'message' => $exception->getMessage(),
|
|
|
|
'trace' => config('debug') ? $exception->getTrace() : []
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->output
|
|
|
|
->set_content_type('application/json')
|
|
|
|
->set_output(json_encode($response));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search for any provider that can handle the requested service.
|
|
|
|
*
|
|
|
|
* This method will return the database ID of the providers affected to the requested service.
|
|
|
|
*
|
|
|
|
* @param int $service_id The requested service ID.
|
|
|
|
*
|
|
|
|
* @return array Returns the ID of the provider that can provide the requested service.
|
|
|
|
*/
|
|
|
|
protected function search_providers_by_service($service_id)
|
|
|
|
{
|
|
|
|
$available_providers = $this->providers_model->get_available_providers();
|
|
|
|
$provider_list = [];
|
|
|
|
|
|
|
|
foreach ($available_providers as $provider)
|
|
|
|
{
|
|
|
|
foreach ($provider['services'] as $provider_service_id)
|
|
|
|
{
|
|
|
|
if ($provider_service_id === $service_id)
|
|
|
|
{
|
|
|
|
// Check if the provider is affected to the selected service.
|
|
|
|
$provider_list[] = $provider['id'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $provider_list;
|
|
|
|
}
|
2020-08-15 14:41:13 +03:00
|
|
|
|
|
|
|
|
2015-10-05 01:32:47 +03:00
|
|
|
}
|