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
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
2021-11-06 18:21:27 +03:00
|
|
|
* Appointments controller.
|
2021-10-28 15:01:17 +03:00
|
|
|
*
|
|
|
|
* Handles the appointment related operations.
|
2015-10-05 01:32:47 +03:00
|
|
|
*
|
|
|
|
* @package Controllers
|
|
|
|
*/
|
2020-11-16 11:29:36 +03:00
|
|
|
class Appointments extends EA_Controller {
|
2015-10-18 21:00:01 +03:00
|
|
|
/**
|
2021-11-06 18:21:27 +03:00
|
|
|
* Appointments constructor.
|
2015-10-18 21:00:01 +03:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
2015-10-18 21:00:01 +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');
|
2021-10-28 15:01:17 +03:00
|
|
|
$this->load->model('service_categories_model');
|
2020-04-22 22:48:56 +03:00
|
|
|
$this->load->model('services_model');
|
|
|
|
$this->load->model('customers_model');
|
|
|
|
$this->load->model('settings_model');
|
2021-10-28 15:01:17 +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');
|
2021-10-28 15:01:17 +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
|
2021-10-28 15:01:17 +03:00
|
|
|
* customer followed the appointment manage link that was sent 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
|
|
|
*/
|
2021-10-28 15:01:17 +03:00
|
|
|
public function index(string $appointment_hash = '')
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2021-11-12 19:09:31 +03:00
|
|
|
if ( ! is_app_installed())
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2021-11-12 19:09:31 +03:00
|
|
|
redirect('installation');
|
2021-11-12 19:23:43 +03:00
|
|
|
|
2021-11-12 19:09:31 +03:00
|
|
|
return;
|
|
|
|
}
|
2015-10-18 21:00:01 +03:00
|
|
|
|
2021-11-12 19:09:31 +03:00
|
|
|
$available_services = $this->services_model->get_available_services();
|
|
|
|
$available_providers = $this->providers_model->get_available_providers();
|
|
|
|
$company_name = setting('company_name');
|
|
|
|
$book_advance_timeout = setting('book_advance_timeout');
|
|
|
|
$date_format = setting('date_format');
|
|
|
|
$time_format = setting('time_format');
|
|
|
|
$first_weekday = setting('first_weekday');
|
|
|
|
$require_phone_number = setting('require_phone_number');
|
|
|
|
$show_field['phone-number'] = setting('show_phone_number');
|
|
|
|
$show_field['address'] = setting('show_address');
|
|
|
|
$show_field['city'] = setting('show_city');
|
|
|
|
$show_field['zip-code'] = setting('show_zip_code');
|
|
|
|
$show_field['notes'] = setting('show_notes');
|
|
|
|
$display_cookie_notice = setting('display_cookie_notice');
|
|
|
|
$cookie_notice_content = setting('cookie_notice_content');
|
|
|
|
$display_terms_and_conditions = setting('display_terms_and_conditions');
|
|
|
|
$terms_and_conditions_content = setting('terms_and_conditions_content');
|
|
|
|
$display_privacy_policy = setting('display_privacy_policy');
|
|
|
|
$privacy_policy_content = setting('privacy_policy_content');
|
|
|
|
$display_any_provider = setting('display_any_provider');
|
|
|
|
$timezones = $this->timezones->to_array();
|
|
|
|
|
|
|
|
// Remove the data that are not needed inside the $available_providers array.
|
|
|
|
foreach ($available_providers as &$available_provider)
|
|
|
|
{
|
|
|
|
$this->providers_model->only($available_provider, [
|
|
|
|
'id',
|
|
|
|
'first_name',
|
|
|
|
'last_name',
|
|
|
|
'services',
|
|
|
|
'timezone'
|
|
|
|
]);
|
|
|
|
}
|
2020-04-22 22:48:56 +03:00
|
|
|
|
2021-11-12 19:09:31 +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;
|
2020-04-22 22:48:56 +03:00
|
|
|
|
2021-11-12 19:09:31 +03:00
|
|
|
$results = $this->appointments_model->get(['hash' => $appointment_hash]);
|
2015-10-05 01:32:47 +03:00
|
|
|
|
2021-11-12 19:09:31 +03:00
|
|
|
if (empty($results))
|
|
|
|
{
|
|
|
|
// The requested appointment was not found in the database.
|
2021-11-15 11:49:19 +03:00
|
|
|
$this->load->view('pages/booking/booking_message_page', [
|
2021-11-12 19:09:31 +03:00
|
|
|
'message_title' => lang('appointment_not_found'),
|
|
|
|
'message_text' => lang('appointment_does_not_exist_in_db'),
|
|
|
|
'message_icon' => base_url('assets/img/error.png')
|
|
|
|
]);
|
2021-10-28 15:01:17 +03:00
|
|
|
|
2021-11-12 19:09:31 +03:00
|
|
|
return;
|
|
|
|
}
|
2020-06-05 19:24:05 +03:00
|
|
|
|
2021-11-12 19:09:31 +03:00
|
|
|
// If the requested appointment begin date is lower than book_advance_timeout. Display a message to the
|
|
|
|
// customer.
|
|
|
|
$start_datetime = strtotime($results[0]['start_datetime']);
|
2020-10-20 16:03:48 +03:00
|
|
|
|
2021-11-12 19:09:31 +03:00
|
|
|
$limit = strtotime('+' . $book_advance_timeout . ' minutes', strtotime('now'));
|
2021-10-28 15:01:17 +03:00
|
|
|
|
2021-11-12 19:09:31 +03:00
|
|
|
if ($start_datetime < $limit)
|
|
|
|
{
|
|
|
|
$hours = floor($book_advance_timeout / 60);
|
|
|
|
$minutes = ($book_advance_timeout % 60);
|
2021-10-28 15:01:17 +03:00
|
|
|
|
2021-11-15 11:49:19 +03:00
|
|
|
$this->load->view('pages/booking/booking_message_page', [
|
2021-11-12 19:09:31 +03:00
|
|
|
'message_title' => lang('appointment_locked'),
|
|
|
|
'message_text' => strtr(lang('appointment_locked_message'), [
|
|
|
|
'{$limit}' => sprintf('%02d:%02d', $hours, $minutes)
|
|
|
|
]),
|
|
|
|
'message_icon' => base_url('assets/img/error.png')
|
|
|
|
]);
|
2015-10-05 01:32:47 +03:00
|
|
|
|
2021-11-12 19:09:31 +03:00
|
|
|
return;
|
|
|
|
}
|
2021-10-28 15:01:17 +03:00
|
|
|
|
2021-11-12 19:09:31 +03:00
|
|
|
$appointment = $results[0];
|
2021-10-28 15:01:17 +03:00
|
|
|
|
2021-11-12 19:09:31 +03:00
|
|
|
$provider = $this->providers_model->find($appointment['id_users_provider']);
|
2015-10-05 01:32:47 +03:00
|
|
|
|
2021-11-12 19:09:31 +03:00
|
|
|
$customer = $this->customers_model->find($appointment['id_users_customer']);
|
2018-06-24 20:08:45 +03:00
|
|
|
|
2021-11-12 19:09:31 +03:00
|
|
|
$customer_token = md5(uniqid(mt_rand(), TRUE));
|
2015-10-07 23:51:12 +03:00
|
|
|
|
2021-11-12 19:09:31 +03:00
|
|
|
// Cache the token for 10 minutes.
|
|
|
|
$this->cache->save('customer-token-' . $customer_token, $customer['id'], 600);
|
2017-09-23 02:30:22 +03:00
|
|
|
}
|
2021-11-12 19:09:31 +03:00
|
|
|
else
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2021-11-12 19:09:31 +03:00
|
|
|
// The customer is going to book a new appointment so there is no need for the manage functionality to
|
|
|
|
// be initialized.
|
|
|
|
$manage_mode = FALSE;
|
|
|
|
$customer_token = FALSE;
|
|
|
|
$appointment = [];
|
|
|
|
$provider = [];
|
|
|
|
$customer = [];
|
2015-10-05 01:32:47 +03:00
|
|
|
}
|
2015-10-18 21:00:01 +03:00
|
|
|
|
2021-11-15 11:49:19 +03:00
|
|
|
$this->load->view('pages/booking/booking_page', [
|
2021-11-12 19:09:31 +03:00
|
|
|
'available_services' => $available_services,
|
|
|
|
'available_providers' => $available_providers,
|
|
|
|
'company_name' => $company_name,
|
|
|
|
'manage_mode' => $manage_mode,
|
|
|
|
'customer_token' => $customer_token,
|
|
|
|
'date_format' => $date_format,
|
|
|
|
'time_format' => $time_format,
|
|
|
|
'first_weekday' => $first_weekday,
|
|
|
|
'require_phone_number' => $require_phone_number,
|
|
|
|
'show_field' => $show_field,
|
|
|
|
'appointment_data' => $appointment,
|
|
|
|
'provider_data' => $provider,
|
|
|
|
'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,
|
|
|
|
'timezones' => $timezones,
|
|
|
|
'display_any_provider' => $display_any_provider,
|
|
|
|
]);
|
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
|
|
|
*/
|
2021-10-28 15:01:17 +03:00
|
|
|
public function cancel(string $appointment_hash)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2021-11-12 19:23:43 +03:00
|
|
|
$exceptions = [];
|
|
|
|
|
2021-10-28 15:01:17 +03:00
|
|
|
$occurrences = $this->appointments_model->get(['hash' => $appointment_hash]);
|
2020-04-22 22:48:56 +03:00
|
|
|
|
2021-10-28 15:01:17 +03:00
|
|
|
if (empty($occurrences))
|
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.');
|
|
|
|
}
|
|
|
|
|
2021-10-28 15:01:17 +03:00
|
|
|
$appointment = $occurrences[0];
|
|
|
|
|
|
|
|
$provider = $this->providers_model->find($appointment['id_users_provider']);
|
|
|
|
|
|
|
|
$customer = $this->customers_model->find($appointment['id_users_customer']);
|
|
|
|
|
|
|
|
$service = $this->services_model->find($appointment['id_services']);
|
2015-10-05 01:32:47 +03:00
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
$settings = [
|
2021-10-28 15:01:17 +03:00
|
|
|
'company_name' => setting('company_name'),
|
|
|
|
'company_email' => setting('company_email'),
|
|
|
|
'company_link' => setting('company_link'),
|
|
|
|
'date_format' => setting('date_format'),
|
|
|
|
'time_format' => setting('time_format')
|
2017-09-15 14:36:37 +03:00
|
|
|
];
|
2015-10-05 01:32:47 +03:00
|
|
|
|
2021-10-28 15:01:17 +03:00
|
|
|
$this->appointments_model->delete($appointment['id']);
|
2015-10-05 01:32:47 +03:00
|
|
|
|
2020-10-21 21:37:47 +03:00
|
|
|
$this->synchronization->sync_appointment_deleted($appointment, $provider);
|
2021-10-28 15:01:17 +03:00
|
|
|
|
2020-10-21 21:37:47 +03:00
|
|
|
$this->notifications->notify_appointment_deleted($appointment, $service, $provider, $customer, $settings);
|
2017-09-23 02:30:22 +03:00
|
|
|
}
|
2021-10-28 15:01:17 +03:00
|
|
|
catch (Throwable $e)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2021-10-28 15:01:17 +03:00
|
|
|
$exceptions[] = $e;
|
2015-10-05 01:32:47 +03:00
|
|
|
}
|
|
|
|
|
2021-11-15 11:49:19 +03:00
|
|
|
$this->load->view('pages/booking/booking_message_page', [
|
2020-04-22 22:48:56 +03:00
|
|
|
'message_title' => lang('appointment_cancelled_title'),
|
|
|
|
'message_text' => lang('appointment_cancelled'),
|
2021-11-12 19:23:43 +03:00
|
|
|
'message_icon' => base_url('assets/img/success.png'),
|
|
|
|
'exceptions' => $exceptions
|
|
|
|
]);
|
2015-10-05 01:32:47 +03:00
|
|
|
}
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
/**
|
2021-10-28 15:01:17 +03:00
|
|
|
* Display the appointment registration success page.
|
2015-11-23 23:58:32 +02:00
|
|
|
*
|
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
|
|
|
*/
|
2021-10-28 15:01:17 +03:00
|
|
|
public function book_success(string $appointment_hash)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2021-10-28 15:01:17 +03:00
|
|
|
$occurrences = $this->appointments_model->get(['hash' => $appointment_hash]);
|
2020-04-22 22:48:56 +03:00
|
|
|
|
2021-10-28 15:01:17 +03:00
|
|
|
if (empty($occurrences))
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-04-22 22:48:56 +03:00
|
|
|
redirect('appointments'); // The appointment does not exist.
|
2021-11-12 19:23:43 +03:00
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
return;
|
2015-11-23 23:58:32 +02:00
|
|
|
}
|
2020-04-22 22:48:56 +03:00
|
|
|
|
2021-10-28 15:01:17 +03:00
|
|
|
$appointment = $occurrences[0];
|
|
|
|
|
2020-04-08 11:30:27 +03:00
|
|
|
unset($appointment['notes']);
|
|
|
|
|
2021-10-28 15:01:17 +03:00
|
|
|
$customer = $this->customers_model->find($appointment['id_users_customer']);
|
2020-12-14 21:40:56 +03:00
|
|
|
|
2021-10-28 15:01:17 +03:00
|
|
|
$provider = $this->providers_model->find($appointment['id_users_provider']);
|
2020-04-08 11:30:27 +03:00
|
|
|
|
2021-10-28 15:01:17 +03:00
|
|
|
$service = $this->services_model->find($appointment['id_services']);
|
2020-04-08 11:30:27 +03:00
|
|
|
|
2021-10-28 15:01:17 +03:00
|
|
|
$company_name = setting('company_name');
|
2019-12-04 11:48:57 +03:00
|
|
|
|
2021-11-12 19:23:43 +03:00
|
|
|
$exceptions = $this->session->flashdata('book_success') ?? [];
|
2019-12-04 11:48:57 +03:00
|
|
|
|
2021-11-15 11:49:19 +03:00
|
|
|
$this->load->view('pages/booking/booking_success_page', [
|
2021-11-12 23:27:11 +03:00
|
|
|
'page_title' => lang('success'),
|
2017-09-15 14:36:37 +03:00
|
|
|
'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'],
|
|
|
|
],
|
2020-12-14 21:40:56 +03:00
|
|
|
'customer_data' => [
|
|
|
|
'id' => $customer['id'],
|
|
|
|
'first_name' => $customer['first_name'],
|
|
|
|
'last_name' => $customer['last_name'],
|
|
|
|
'email' => $customer['email'],
|
|
|
|
'timezone' => $customer['timezone'],
|
|
|
|
],
|
2017-09-15 14:36:37 +03:00
|
|
|
'service_data' => $service,
|
|
|
|
'company_name' => $company_name,
|
2021-11-12 19:30:09 +03:00
|
|
|
'exceptions' => $exceptions,
|
|
|
|
'scripts' => [
|
2021-11-12 23:27:11 +03:00
|
|
|
'https://apis.google.com/js/client.js',
|
2021-11-12 19:30:09 +03:00
|
|
|
asset_url('assets/vendor/datejs/date.min.js'),
|
|
|
|
asset_url('assets/vendor/moment/moment.min.js'),
|
|
|
|
asset_url('assets/vendor/moment-timezone/moment-timezone-with-data.min.js'),
|
|
|
|
asset_url('assets/js/frontend_book_success.js'),
|
|
|
|
asset_url('assets/js/general_functions.js')
|
|
|
|
]
|
2021-11-12 19:23:43 +03:00
|
|
|
]);
|
2015-11-23 23:58:32 +02:00
|
|
|
}
|
|
|
|
|
2015-10-05 01:32:47 +03:00
|
|
|
/**
|
2021-10-28 15:01:17 +03:00
|
|
|
* Get the available appointment hours for the selected date.
|
2015-10-05 01:32:47 +03:00
|
|
|
*
|
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-15 14:36:37 +03:00
|
|
|
public function ajax_get_available_hours()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2021-10-28 15:01:17 +03:00
|
|
|
$provider_id = request('provider_id');
|
|
|
|
$service_id = request('service_id');
|
|
|
|
$selected_date = request('selected_date');
|
2020-04-22 22:48:56 +03:00
|
|
|
|
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
|
|
|
{
|
2021-11-12 19:23:43 +03:00
|
|
|
json_response([]);
|
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.
|
2021-10-28 15:01:17 +03:00
|
|
|
$exclude_appointment_id = request('manage_mode') === 'true' ? request('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.
|
2021-10-28 15:01:17 +03:00
|
|
|
$service = $this->services_model->find($service_id);
|
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
if ($provider_id === ANY_PROVIDER)
|
|
|
|
{
|
2021-10-28 15:01:17 +03:00
|
|
|
$providers = $this->providers_model->get();
|
|
|
|
|
|
|
|
$available_hours = [];
|
|
|
|
|
|
|
|
foreach ($providers as $provider)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2021-10-28 15:01:17 +03:00
|
|
|
if ( ! in_array($service_id, $provider['services']))
|
2021-10-18 14:04:32 +03:00
|
|
|
{
|
2021-10-28 15:01:17 +03:00
|
|
|
continue;
|
2021-10-18 14:04:32 +03:00
|
|
|
}
|
2021-10-28 15:01:17 +03:00
|
|
|
|
|
|
|
$provider_available_hours = $this->availability->get_available_hours($selected_date, $service, $provider, $exclude_appointment_id);
|
|
|
|
|
2021-10-18 14:04:32 +03:00
|
|
|
$available_hours = array_merge($available_hours, $provider_available_hours);
|
2017-09-15 14:36:37 +03:00
|
|
|
}
|
2015-12-01 01:10:16 +02:00
|
|
|
|
2021-10-28 15:01:17 +03:00
|
|
|
$response = array_unique(array_values($available_hours));
|
2021-10-18 14:04:32 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-28 15:01:17 +03:00
|
|
|
$provider = $this->providers_model->find($provider_id);
|
2016-07-21 23:36:17 +03:00
|
|
|
|
2021-10-18 14:04:32 +03:00
|
|
|
$response = $this->availability->get_available_hours($selected_date, $service, $provider, $exclude_appointment_id);
|
|
|
|
}
|
2021-11-12 19:23:43 +03:00
|
|
|
|
|
|
|
json_response($response);
|
2017-09-23 02:30:22 +03:00
|
|
|
}
|
2021-10-28 15:01:17 +03:00
|
|
|
catch (Throwable $e)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2021-11-12 19:23:43 +03:00
|
|
|
json_exception($e);
|
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.
|
|
|
|
*
|
2021-10-28 15:01:17 +03:00
|
|
|
* @param int $service_id Service ID
|
|
|
|
* @param string $date Selected date (Y-m-d).
|
|
|
|
* @param string|null $hour Selected hour (H:i).
|
2020-04-22 22:48:56 +03:00
|
|
|
*
|
2021-11-08 12:44:36 +03:00
|
|
|
* @return int|null Returns the ID of the provider that can provide the service at the selected date.
|
2020-04-22 22:48:56 +03:00
|
|
|
*
|
|
|
|
* @throws Exception
|
2015-10-18 21:00:01 +03:00
|
|
|
*/
|
2021-11-08 12:44:36 +03:00
|
|
|
protected function search_any_provider(int $service_id, string $date, string $hour = NULL): ?int
|
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
|
|
|
|
2021-10-28 15:01:17 +03:00
|
|
|
$service = $this->services_model->find($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
|
|
|
|
2021-10-28 15:01:17 +03:00
|
|
|
if (count($available_hours) > $max_hours_count && (empty($hour) || in_array($hour, $available_hours, FALSE)))
|
2020-04-22 22:48:56 +03:00
|
|
|
{
|
|
|
|
$provider_id = $provider['id'];
|
2021-10-28 15:01:17 +03:00
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
$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
|
|
|
|
|
|
|
/**
|
2020-12-09 15:46:27 +03:00
|
|
|
* Register the appointment to the database.
|
2020-04-22 22:48:56 +03:00
|
|
|
*/
|
|
|
|
public function ajax_register_appointment()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2021-10-28 15:01:17 +03:00
|
|
|
$post_data = request('post_data');
|
|
|
|
$captcha = request('captcha');
|
2020-04-22 22:48:56 +03:00
|
|
|
$manage_mode = filter_var($post_data['manage_mode'], FILTER_VALIDATE_BOOLEAN);
|
|
|
|
$appointment = $post_data['appointment'];
|
|
|
|
$customer = $post_data['customer'];
|
|
|
|
|
2021-10-28 15:01:17 +03:00
|
|
|
if ( ! array_key_exists('address', $customer))
|
|
|
|
{
|
|
|
|
$customer['address'] = '';
|
|
|
|
}
|
2021-11-12 19:23:43 +03:00
|
|
|
|
2021-10-28 15:01:17 +03:00
|
|
|
if ( ! array_key_exists('city', $customer))
|
|
|
|
{
|
|
|
|
$customer['city'] = '';
|
|
|
|
}
|
2021-11-12 19:23:43 +03:00
|
|
|
|
2021-10-28 15:01:17 +03:00
|
|
|
if ( ! array_key_exists('zip_code', $customer))
|
|
|
|
{
|
|
|
|
$customer['zip_code'] = '';
|
|
|
|
}
|
2021-11-12 19:23:43 +03:00
|
|
|
|
2021-10-28 15:01:17 +03:00
|
|
|
if ( ! array_key_exists('notes', $customer))
|
|
|
|
{
|
|
|
|
$customer['notes'] = '';
|
|
|
|
}
|
2021-11-12 19:23:43 +03:00
|
|
|
|
2021-10-28 15:01:17 +03:00
|
|
|
if ( ! array_key_exists('phone_number', $customer))
|
|
|
|
{
|
|
|
|
$customer['address'] = '';
|
|
|
|
}
|
2021-05-28 16:39:14 +03:00
|
|
|
|
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'));
|
|
|
|
}
|
|
|
|
|
2021-10-28 15:01:17 +03:00
|
|
|
$provider = $this->providers_model->find($appointment['id_users_provider']);
|
|
|
|
|
|
|
|
$service = $this->services_model->find($appointment['id_services']);
|
2020-04-22 22:48:56 +03:00
|
|
|
|
2021-10-28 15:01:17 +03:00
|
|
|
$require_captcha = (bool)setting('require_captcha');
|
|
|
|
|
|
|
|
$captcha_phrase = session('captcha_phrase');
|
2020-04-22 22:48:56 +03:00
|
|
|
|
|
|
|
// Validate the CAPTCHA string.
|
2021-10-28 15:01:17 +03:00
|
|
|
if ($require_captcha && $captcha_phrase !== $captcha)
|
2020-04-22 22:48:56 +03:00
|
|
|
{
|
2021-11-12 19:23:43 +03:00
|
|
|
json_response([
|
|
|
|
'captcha_verification' => FALSE
|
|
|
|
]);
|
2020-04-22 22:48:56 +03:00
|
|
|
|
|
|
|
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).
|
2021-10-28 15:01:17 +03:00
|
|
|
$customer['language'] = session('language') ?? config('language');
|
|
|
|
$customer_id = $this->customers_model->save($customer);
|
2020-04-22 22:48:56 +03:00
|
|
|
|
|
|
|
$appointment['id_users_customer'] = $customer_id;
|
2021-10-28 15:01:17 +03:00
|
|
|
$appointment['is_unavailable'] = (int)$appointment['is_unavailable'];
|
|
|
|
$appointment['id'] = $this->appointments_model->save($appointment);
|
|
|
|
$appointment['hash'] = $this->appointments_model->value($appointment['id'], 'hash');
|
2020-04-22 22:48:56 +03:00
|
|
|
|
|
|
|
$settings = [
|
2021-10-28 15:01:17 +03:00
|
|
|
'company_name' => setting('company_name'),
|
|
|
|
'company_link' => setting('company_link'),
|
|
|
|
'company_email' => setting('company_email'),
|
|
|
|
'date_format' => setting('date_format'),
|
|
|
|
'time_format' => setting('time_format')
|
2020-04-22 22:48:56 +03:00
|
|
|
];
|
|
|
|
|
2020-12-05 12:46:05 +03:00
|
|
|
$this->synchronization->sync_appointment_saved($appointment, $service, $provider, $customer, $settings, $manage_mode);
|
2021-10-28 15:01:17 +03:00
|
|
|
|
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']
|
|
|
|
];
|
2021-11-12 19:23:43 +03:00
|
|
|
|
|
|
|
json_response($response);
|
2020-04-22 22:48:56 +03:00
|
|
|
}
|
2021-10-28 15:01:17 +03:00
|
|
|
catch (Throwable $e)
|
2020-04-22 22:48:56 +03:00
|
|
|
{
|
2021-11-12 19:23:43 +03:00
|
|
|
json_exception($e);
|
2020-04-22 22:48:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the provider is still available in the selected appointment date.
|
|
|
|
*
|
2021-10-28 15:01:17 +03:00
|
|
|
* It is possible that two or more customers select the same appointment date and time concurrently. The app won't
|
|
|
|
* allow this to happen, so one of the two will eventually get the selected date and the other one will have
|
|
|
|
* to choose for another one.
|
|
|
|
*
|
|
|
|
* Use this method just before the customer confirms the appointment registration. If the selected date was reserved
|
|
|
|
* in the meanwhile, the customer must be prompted to select another time.
|
2020-04-22 22:48:56 +03:00
|
|
|
*
|
|
|
|
* @return int Returns the ID of the provider that is available for the appointment.
|
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2021-10-28 15:01:17 +03:00
|
|
|
protected function check_datetime_availability(): ?int
|
2020-04-22 22:48:56 +03:00
|
|
|
{
|
2021-10-28 15:01:17 +03:00
|
|
|
$post_data = request('post_data');
|
2020-04-22 22:48:56 +03:00
|
|
|
|
|
|
|
$appointment = $post_data['appointment'];
|
|
|
|
|
2021-01-20 17:21:59 +03:00
|
|
|
$appointment_start = new DateTime($appointment['start_datetime']);
|
2021-10-28 15:01:17 +03:00
|
|
|
|
2021-01-20 17:21:59 +03:00
|
|
|
$date = $appointment_start->format('Y-m-d');
|
2021-10-28 15:01:17 +03:00
|
|
|
|
2021-01-20 17:21:59 +03:00
|
|
|
$hour = $appointment_start->format('H:i');
|
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
|
|
|
{
|
2021-01-20 17:21:59 +03:00
|
|
|
$appointment['id_users_provider'] = $this->search_any_provider($appointment['id_services'], $date, $hour);
|
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
|
|
|
}
|
|
|
|
|
2021-10-28 15:01:17 +03:00
|
|
|
$service = $this->services_model->find($appointment['id_services']);
|
2020-04-22 22:48:56 +03:00
|
|
|
|
2021-10-28 15:01:17 +03:00
|
|
|
$exclude_appointment_id = $appointment['id'] ?? NULL;
|
2020-10-21 21:37:47 +03:00
|
|
|
|
2021-10-28 15:01:17 +03:00
|
|
|
$provider = $this->providers_model->find($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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-09 15:46:27 +03:00
|
|
|
* Get Unavailable Dates
|
2020-04-22 22:48:56 +03:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
{
|
2021-10-28 15:01:17 +03:00
|
|
|
$provider_id = request('provider_id');
|
|
|
|
$service_id = request('service_id');
|
|
|
|
$appointment_id = request('appointment_id');
|
|
|
|
$manage_mode = filter_var(request('manage_mode'), FILTER_VALIDATE_BOOLEAN);
|
|
|
|
$selected_date_string = request('selected_date');
|
2020-04-22 22:48:56 +03:00
|
|
|
$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.
|
2021-10-28 15:01:17 +03:00
|
|
|
$service = $this->services_model->find($service_id);
|
2020-04-22 22:48:56 +03:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2021-10-28 15:01:17 +03:00
|
|
|
$provider = $this->providers_model->find($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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-12 19:23:43 +03:00
|
|
|
json_response($unavailable_dates);
|
2020-04-22 22:48:56 +03:00
|
|
|
}
|
2021-10-28 15:01:17 +03:00
|
|
|
catch (Throwable $e)
|
2020-04-22 22:48:56 +03:00
|
|
|
{
|
2021-11-12 19:23:43 +03:00
|
|
|
json_exception($e);
|
2020-04-22 22:48:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2021-10-28 15:01:17 +03:00
|
|
|
protected function search_providers_by_service(int $service_id): array
|
2020-04-22 22:48:56 +03:00
|
|
|
{
|
|
|
|
$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;
|
|
|
|
}
|
2015-10-05 01:32:47 +03:00
|
|
|
}
|