2021-11-18 09:12:16 +03:00
|
|
|
<?php defined('BASEPATH') or exit('No direct script access allowed');
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
2022-01-18 15:05:42 +03:00
|
|
|
* Easy!Appointments - Online Appointment Scheduler
|
2021-11-18 09:12:16 +03:00
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2021-12-18 19:43:45 +03:00
|
|
|
* @copyright Copyright (c) Alex Tselegidis
|
2021-11-18 09:12:16 +03:00
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link https://easyappointments.org
|
|
|
|
* @since v1.0.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Booking controller.
|
|
|
|
*
|
|
|
|
* Handles the booking related operations.
|
|
|
|
*
|
2021-12-17 12:55:37 +03:00
|
|
|
* Notice: This file used to have the booking page related code which since v1.5 has now moved to the Booking.php
|
2021-11-18 09:12:16 +03:00
|
|
|
* controller for improved consistency.
|
|
|
|
*
|
|
|
|
* @package Controllers
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
class Booking extends EA_Controller
|
|
|
|
{
|
2021-11-18 09:12:16 +03:00
|
|
|
/**
|
|
|
|
* Booking constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->load->model('appointments_model');
|
|
|
|
$this->load->model('providers_model');
|
|
|
|
$this->load->model('admins_model');
|
|
|
|
$this->load->model('secretaries_model');
|
2023-10-26 05:54:13 +03:00
|
|
|
$this->load->model('service_categories_model');
|
2021-11-18 09:12:16 +03:00
|
|
|
$this->load->model('services_model');
|
|
|
|
$this->load->model('customers_model');
|
|
|
|
$this->load->model('settings_model');
|
2022-03-25 14:44:05 +03:00
|
|
|
$this->load->model('consents_model');
|
2021-11-18 09:12:16 +03:00
|
|
|
|
|
|
|
$this->load->library('timezones');
|
|
|
|
$this->load->library('synchronization');
|
|
|
|
$this->load->library('notifications');
|
|
|
|
$this->load->library('availability');
|
2022-06-19 20:05:45 +03:00
|
|
|
$this->load->library('webhooks_client');
|
2021-11-18 09:12:16 +03:00
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
/**
|
|
|
|
* Render the booking page and display the selected appointment.
|
|
|
|
*
|
|
|
|
* This method will call the "index" callback to handle the page rendering.
|
|
|
|
*
|
|
|
|
* @param string $appointment_hash
|
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function reschedule(string $appointment_hash): void
|
2023-11-29 12:24:09 +03:00
|
|
|
{
|
|
|
|
html_vars(['appointment_hash' => $appointment_hash]);
|
|
|
|
|
|
|
|
$this->index();
|
|
|
|
}
|
|
|
|
|
2021-11-18 09:12:16 +03:00
|
|
|
/**
|
2021-12-18 19:22:40 +03:00
|
|
|
* Render the booking page.
|
|
|
|
*
|
|
|
|
* This method creates the appointment book wizard.
|
2021-11-18 09:12:16 +03:00
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function index(): void
|
2021-11-18 09:12:16 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!is_app_installed()) {
|
2021-12-18 19:22:40 +03:00
|
|
|
redirect('installation');
|
|
|
|
|
2021-11-18 09:12:16 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-24 18:38:03 +03:00
|
|
|
$company_name = setting('company_name');
|
2022-01-24 20:36:12 +03:00
|
|
|
$company_logo = setting('company_logo');
|
|
|
|
$company_color = setting('company_color');
|
2022-01-24 18:38:03 +03:00
|
|
|
$disable_booking = setting('disable_booking');
|
|
|
|
$google_analytics_code = setting('google_analytics_code');
|
|
|
|
$matomo_analytics_url = setting('matomo_analytics_url');
|
2022-03-25 14:44:05 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($disable_booking) {
|
2022-01-24 18:38:03 +03:00
|
|
|
$disable_booking_message = setting('disable_booking_message');
|
|
|
|
|
|
|
|
html_vars([
|
2023-11-29 12:24:09 +03:00
|
|
|
'show_message' => true,
|
2022-01-24 18:38:03 +03:00
|
|
|
'page_title' => lang('page_title') . ' ' . $company_name,
|
|
|
|
'message_title' => lang('booking_is_disabled'),
|
|
|
|
'message_text' => $disable_booking_message,
|
|
|
|
'message_icon' => base_url('assets/img/error.png'),
|
|
|
|
'google_analytics_code' => $google_analytics_code,
|
2023-12-22 13:35:41 +03:00
|
|
|
'matomo_analytics_url' => $matomo_analytics_url,
|
2022-01-24 18:38:03 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->load->view('pages/booking_message');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$available_services = $this->services_model->get_available_services(true);
|
|
|
|
$available_providers = $this->providers_model->get_available_providers(true);
|
2021-12-17 12:55:37 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($available_providers as &$available_provider) {
|
2023-01-18 12:59:57 +03:00
|
|
|
// Only expose the required provider data.
|
2021-12-18 19:22:40 +03:00
|
|
|
|
2021-12-17 12:55:37 +03:00
|
|
|
$this->providers_model->only($available_provider, [
|
|
|
|
'id',
|
|
|
|
'first_name',
|
|
|
|
'last_name',
|
|
|
|
'services',
|
2023-12-22 13:35:41 +03:00
|
|
|
'timezone',
|
2021-12-17 12:55:37 +03:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2021-11-18 09:12:16 +03:00
|
|
|
$date_format = setting('date_format');
|
|
|
|
$time_format = setting('time_format');
|
|
|
|
$first_weekday = setting('first_weekday');
|
2021-12-20 11:38:37 +03:00
|
|
|
$display_first_name = setting('display_first_name');
|
|
|
|
$require_first_name = setting('require_first_name');
|
|
|
|
$display_last_name = setting('display_last_name');
|
|
|
|
$require_last_name = setting('require_last_name');
|
|
|
|
$display_email = setting('display_email');
|
|
|
|
$require_email = setting('require_email');
|
|
|
|
$display_phone_number = setting('display_phone_number');
|
2021-11-18 09:12:16 +03:00
|
|
|
$require_phone_number = setting('require_phone_number');
|
2021-12-20 11:38:37 +03:00
|
|
|
$display_address = setting('display_address');
|
|
|
|
$require_address = setting('require_address');
|
|
|
|
$display_city = setting('display_city');
|
|
|
|
$require_city = setting('require_city');
|
|
|
|
$display_zip_code = setting('display_zip_code');
|
|
|
|
$require_zip_code = setting('require_zip_code');
|
|
|
|
$display_notes = setting('display_notes');
|
|
|
|
$require_notes = setting('require_notes');
|
2021-11-18 09:12:16 +03:00
|
|
|
$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');
|
2022-01-24 22:02:17 +03:00
|
|
|
$display_login_button = setting('display_login_button');
|
2022-01-24 17:31:42 +03:00
|
|
|
$display_delete_personal_information = setting('display_delete_personal_information');
|
2021-12-17 12:55:37 +03:00
|
|
|
$book_advance_timeout = setting('book_advance_timeout');
|
2022-05-27 11:04:40 +03:00
|
|
|
$theme = request('theme', setting('theme', 'default'));
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($theme) || !file_exists(__DIR__ . '/../../assets/css/themes/' . $theme . '.min.css')) {
|
2022-05-27 11:04:40 +03:00
|
|
|
$theme = 'default';
|
|
|
|
}
|
2021-12-17 12:55:37 +03:00
|
|
|
|
2021-11-18 09:12:16 +03:00
|
|
|
$timezones = $this->timezones->to_array();
|
|
|
|
$grouped_timezones = $this->timezones->to_grouped_array();
|
2022-03-25 14:44:05 +03:00
|
|
|
|
2022-01-19 12:25:28 +03:00
|
|
|
$appointment_hash = html_vars('appointment_hash');
|
2021-11-18 09:12:16 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!empty($appointment_hash)) {
|
2021-12-17 12:55:37 +03:00
|
|
|
// Load the appointments data and enable the manage mode of the booking page.
|
2021-11-18 09:12:16 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$manage_mode = true;
|
2021-11-18 09:12:16 +03:00
|
|
|
|
|
|
|
$results = $this->appointments_model->get(['hash' => $appointment_hash]);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($results)) {
|
2021-12-17 12:55:37 +03:00
|
|
|
html_vars([
|
2023-11-29 12:24:09 +03:00
|
|
|
'show_message' => true,
|
2021-12-17 12:55:37 +03:00
|
|
|
'page_title' => lang('page_title') . ' ' . $company_name,
|
2021-11-18 09:12:16 +03:00
|
|
|
'message_title' => lang('appointment_not_found'),
|
|
|
|
'message_text' => lang('appointment_does_not_exist_in_db'),
|
2022-01-19 12:18:00 +03:00
|
|
|
'message_icon' => base_url('assets/img/error.png'),
|
|
|
|
'google_analytics_code' => $google_analytics_code,
|
2023-12-22 13:35:41 +03:00
|
|
|
'matomo_analytics_url' => $matomo_analytics_url,
|
2021-11-18 09:12:16 +03:00
|
|
|
]);
|
|
|
|
|
2022-01-19 12:21:05 +03:00
|
|
|
$this->load->view('pages/booking_message');
|
2021-12-18 19:22:40 +03:00
|
|
|
|
2021-11-18 09:12:16 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-18 12:59:57 +03:00
|
|
|
// Make sure the appointment can still be rescheduled.
|
2021-12-18 19:22:40 +03:00
|
|
|
|
2021-11-18 09:12:16 +03:00
|
|
|
$start_datetime = strtotime($results[0]['start_datetime']);
|
|
|
|
|
|
|
|
$limit = strtotime('+' . $book_advance_timeout . ' minutes', strtotime('now'));
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($start_datetime < $limit) {
|
2021-11-18 09:12:16 +03:00
|
|
|
$hours = floor($book_advance_timeout / 60);
|
2021-12-17 12:55:37 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$minutes = $book_advance_timeout % 60;
|
2021-11-18 09:12:16 +03:00
|
|
|
|
2021-12-17 12:55:37 +03:00
|
|
|
html_vars([
|
2023-11-29 12:24:09 +03:00
|
|
|
'show_message' => true,
|
2021-12-17 12:55:37 +03:00
|
|
|
'page_title' => lang('page_title') . ' ' . $company_name,
|
2021-11-18 09:12:16 +03:00
|
|
|
'message_title' => lang('appointment_locked'),
|
|
|
|
'message_text' => strtr(lang('appointment_locked_message'), [
|
2023-12-22 13:35:41 +03:00
|
|
|
'{$limit}' => sprintf('%02d:%02d', $hours, $minutes),
|
2021-11-18 09:12:16 +03:00
|
|
|
]),
|
2022-01-19 12:18:00 +03:00
|
|
|
'message_icon' => base_url('assets/img/error.png'),
|
|
|
|
'google_analytics_code' => $google_analytics_code,
|
2023-12-22 13:35:41 +03:00
|
|
|
'matomo_analytics_url' => $matomo_analytics_url,
|
2021-11-18 09:12:16 +03:00
|
|
|
]);
|
|
|
|
|
2022-01-19 12:21:05 +03:00
|
|
|
$this->load->view('pages/booking_message');
|
2021-12-18 19:22:40 +03:00
|
|
|
|
2021-11-18 09:12:16 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$appointment = $results[0];
|
|
|
|
$provider = $this->providers_model->find($appointment['id_users_provider']);
|
|
|
|
$customer = $this->customers_model->find($appointment['id_users_customer']);
|
2023-11-29 12:24:09 +03:00
|
|
|
$customer_token = md5(uniqid(mt_rand(), true));
|
2021-11-18 09:12:16 +03:00
|
|
|
|
|
|
|
// Cache the token for 10 minutes.
|
|
|
|
$this->cache->save('customer-token-' . $customer_token, $customer['id'], 600);
|
2023-11-29 12:24:09 +03:00
|
|
|
} else {
|
|
|
|
$manage_mode = false;
|
|
|
|
$customer_token = false;
|
|
|
|
$appointment = null;
|
|
|
|
$provider = null;
|
|
|
|
$customer = null;
|
2021-11-18 09:12:16 +03:00
|
|
|
}
|
|
|
|
|
2021-12-17 12:55:37 +03:00
|
|
|
script_vars([
|
2022-01-13 13:33:40 +03:00
|
|
|
'manage_mode' => $manage_mode,
|
2021-12-17 12:55:37 +03:00
|
|
|
'available_services' => $available_services,
|
|
|
|
'available_providers' => $available_providers,
|
|
|
|
'date_format' => $date_format,
|
|
|
|
'time_format' => $time_format,
|
|
|
|
'first_weekday' => $first_weekday,
|
|
|
|
'display_cookie_notice' => $display_cookie_notice,
|
|
|
|
'display_any_provider' => setting('display_any_provider'),
|
2023-02-13 11:36:08 +03:00
|
|
|
'future_booking_limit' => setting('future_booking_limit'),
|
2022-10-16 18:07:45 +03:00
|
|
|
'appointment_data' => $appointment,
|
|
|
|
'provider_data' => $provider,
|
2023-12-22 13:35:41 +03:00
|
|
|
'customer_data' => $customer,
|
2024-03-27 18:09:15 +03:00
|
|
|
'default_language' => setting('default_language'),
|
|
|
|
'default_timezone' => setting('default_timezone'),
|
2021-12-17 12:55:37 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
html_vars([
|
2021-11-18 09:12:16 +03:00
|
|
|
'available_services' => $available_services,
|
|
|
|
'available_providers' => $available_providers,
|
2022-05-27 11:04:40 +03:00
|
|
|
'theme' => $theme,
|
2021-11-18 09:12:16 +03:00
|
|
|
'company_name' => $company_name,
|
2022-01-24 20:36:12 +03:00
|
|
|
'company_logo' => $company_logo,
|
2022-01-24 22:50:14 +03:00
|
|
|
'company_color' => $company_color === '#ffffff' ? '' : $company_color,
|
2021-11-18 09:12:16 +03:00
|
|
|
'date_format' => $date_format,
|
|
|
|
'time_format' => $time_format,
|
|
|
|
'first_weekday' => $first_weekday,
|
2021-12-20 11:38:37 +03:00
|
|
|
'display_first_name' => $display_first_name,
|
|
|
|
'require_first_name' => $require_first_name,
|
|
|
|
'display_last_name' => $display_last_name,
|
|
|
|
'require_last_name' => $require_last_name,
|
|
|
|
'display_email' => $display_email,
|
|
|
|
'require_email' => $require_email,
|
|
|
|
'display_phone_number' => $display_phone_number,
|
|
|
|
'require_phone_number' => $require_phone_number,
|
|
|
|
'display_address' => $display_address,
|
|
|
|
'require_address' => $require_address,
|
|
|
|
'display_city' => $display_city,
|
|
|
|
'require_city' => $require_city,
|
|
|
|
'display_zip_code' => $display_zip_code,
|
|
|
|
'require_zip_code' => $require_zip_code,
|
|
|
|
'display_notes' => $display_notes,
|
|
|
|
'require_notes' => $require_notes,
|
2021-11-18 09:12:16 +03:00
|
|
|
'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,
|
2021-12-17 12:55:37 +03:00
|
|
|
'display_any_provider' => $display_any_provider,
|
2022-01-24 22:02:17 +03:00
|
|
|
'display_login_button' => $display_login_button,
|
2022-01-24 17:31:42 +03:00
|
|
|
'display_delete_personal_information' => $display_delete_personal_information,
|
2022-01-19 12:18:00 +03:00
|
|
|
'google_analytics_code' => $google_analytics_code,
|
|
|
|
'matomo_analytics_url' => $matomo_analytics_url,
|
2021-11-18 09:12:16 +03:00
|
|
|
'timezones' => $timezones,
|
|
|
|
'grouped_timezones' => $grouped_timezones,
|
2021-12-17 12:55:37 +03:00
|
|
|
'manage_mode' => $manage_mode,
|
|
|
|
'customer_token' => $customer_token,
|
|
|
|
'appointment_data' => $appointment,
|
|
|
|
'provider_data' => $provider,
|
2023-12-22 13:35:41 +03:00
|
|
|
'customer_data' => $customer,
|
2021-11-18 09:12:16 +03:00
|
|
|
]);
|
2021-12-17 12:55:37 +03:00
|
|
|
|
2022-01-19 12:21:05 +03:00
|
|
|
$this->load->view('pages/booking');
|
2021-12-17 12:55:37 +03:00
|
|
|
}
|
|
|
|
|
2021-11-18 09:12:16 +03:00
|
|
|
/**
|
|
|
|
* Register the appointment to the database.
|
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function register(): void
|
2021-11-18 09:12:16 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
2023-12-22 14:48:06 +03:00
|
|
|
$disable_booking = setting('disable_booking');
|
|
|
|
|
|
|
|
if ($disable_booking) {
|
|
|
|
abort(403);
|
|
|
|
}
|
|
|
|
|
2021-11-18 09:12:16 +03:00
|
|
|
$post_data = request('post_data');
|
|
|
|
$captcha = request('captcha');
|
|
|
|
$appointment = $post_data['appointment'];
|
|
|
|
$customer = $post_data['customer'];
|
2023-01-14 12:08:06 +03:00
|
|
|
$manage_mode = filter_var($post_data['manage_mode'], FILTER_VALIDATE_BOOLEAN);
|
2021-11-18 09:12:16 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!array_key_exists('address', $customer)) {
|
2021-11-18 09:12:16 +03:00
|
|
|
$customer['address'] = '';
|
|
|
|
}
|
2021-12-17 12:55:37 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!array_key_exists('city', $customer)) {
|
2021-11-18 09:12:16 +03:00
|
|
|
$customer['city'] = '';
|
|
|
|
}
|
2021-12-17 12:55:37 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!array_key_exists('zip_code', $customer)) {
|
2021-11-18 09:12:16 +03:00
|
|
|
$customer['zip_code'] = '';
|
|
|
|
}
|
2021-12-17 12:55:37 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!array_key_exists('notes', $customer)) {
|
2021-11-18 09:12:16 +03:00
|
|
|
$customer['notes'] = '';
|
|
|
|
}
|
2021-12-17 12:55:37 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!array_key_exists('phone_number', $customer)) {
|
2024-04-24 14:29:10 +03:00
|
|
|
$customer['phone_number'] = '';
|
2021-11-18 09:12:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check appointment availability before registering it to the database.
|
|
|
|
$appointment['id_users_provider'] = $this->check_datetime_availability();
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$appointment['id_users_provider']) {
|
2022-06-20 12:31:11 +03:00
|
|
|
throw new RuntimeException(lang('requested_hour_is_unavailable'));
|
2021-11-18 09:12:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$provider = $this->providers_model->find($appointment['id_users_provider']);
|
|
|
|
|
|
|
|
$service = $this->services_model->find($appointment['id_services']);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$require_captcha = (bool) setting('require_captcha');
|
2021-11-18 09:12:16 +03:00
|
|
|
|
|
|
|
$captcha_phrase = session('captcha_phrase');
|
|
|
|
|
|
|
|
// Validate the CAPTCHA string.
|
2023-10-23 12:46:41 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($require_captcha && strtoupper($captcha_phrase) !== strtoupper($captcha)) {
|
2021-11-18 09:12:16 +03:00
|
|
|
json_response([
|
2023-12-22 13:35:41 +03:00
|
|
|
'captcha_verification' => false,
|
2021-11-18 09:12:16 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($this->customers_model->exists($customer)) {
|
2021-11-18 09:12:16 +03:00
|
|
|
$customer['id'] = $this->customers_model->find_record_id($customer);
|
2023-10-23 12:46:41 +03:00
|
|
|
|
|
|
|
$existing_appointments = $this->appointments_model->get([
|
|
|
|
'id_users_customer' => $customer['id'],
|
|
|
|
'start_datetime <=' => $appointment['start_datetime'],
|
2023-12-22 13:35:41 +03:00
|
|
|
'end_datetime >=' => $appointment['end_datetime'],
|
2023-10-23 12:46:41 +03:00
|
|
|
]);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (count($existing_appointments)) {
|
2023-10-23 12:46:41 +03:00
|
|
|
throw new RuntimeException(lang('customer_is_already_booked'));
|
|
|
|
}
|
2021-11-18 09:12:16 +03:00
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($appointment['location']) && !empty($service['location'])) {
|
2021-11-18 09:12:16 +03:00
|
|
|
$appointment['location'] = $service['location'];
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($appointment['color']) && !empty($service['color'])) {
|
2022-01-19 11:11:42 +03:00
|
|
|
$appointment['color'] = $service['color'];
|
|
|
|
}
|
|
|
|
|
2022-03-25 14:44:05 +03:00
|
|
|
$customer_ip = $this->input->ip_address();
|
|
|
|
|
|
|
|
// Create the consents (if needed).
|
|
|
|
$consent = [
|
|
|
|
'first_name' => $customer['first_name'] ?? '-',
|
|
|
|
'last_name' => $customer['last_name'] ?? '-',
|
|
|
|
'email' => $customer['email'] ?? '-',
|
2023-12-22 13:35:41 +03:00
|
|
|
'ip' => $customer_ip,
|
2022-03-25 14:44:05 +03:00
|
|
|
];
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (setting('display_terms_and_conditions')) {
|
2022-03-25 14:44:05 +03:00
|
|
|
$consent['type'] = 'terms-and-conditions';
|
|
|
|
|
|
|
|
$this->consents_model->save($consent);
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (setting('display_privacy_policy')) {
|
2022-03-25 14:44:05 +03:00
|
|
|
$consent['type'] = 'privacy-policy';
|
|
|
|
|
|
|
|
$this->consents_model->save($consent);
|
|
|
|
}
|
|
|
|
|
2021-11-18 09:12:16 +03:00
|
|
|
// Save customer language (the language which is used to render the booking page).
|
|
|
|
$customer['language'] = session('language') ?? config('language');
|
2022-03-25 14:29:17 +03:00
|
|
|
|
|
|
|
$this->customers_model->only($customer, [
|
|
|
|
'id',
|
|
|
|
'first_name',
|
|
|
|
'last_name',
|
|
|
|
'email',
|
|
|
|
'phone_number',
|
|
|
|
'address',
|
|
|
|
'city',
|
|
|
|
'state',
|
|
|
|
'zip_code',
|
|
|
|
'timezone',
|
2023-12-22 13:35:41 +03:00
|
|
|
'language',
|
2024-02-07 12:45:04 +03:00
|
|
|
'custom_field_1',
|
|
|
|
'custom_field_2',
|
|
|
|
'custom_field_3',
|
|
|
|
'custom_field_4',
|
|
|
|
'custom_field_5',
|
2022-03-25 14:29:17 +03:00
|
|
|
]);
|
|
|
|
|
2021-11-18 09:12:16 +03:00
|
|
|
$customer_id = $this->customers_model->save($customer);
|
2021-11-25 10:40:37 +03:00
|
|
|
$customer = $this->customers_model->find($customer_id);
|
2021-11-18 09:12:16 +03:00
|
|
|
|
|
|
|
$appointment['id_users_customer'] = $customer_id;
|
2023-11-29 12:24:09 +03:00
|
|
|
$appointment['is_unavailability'] = false;
|
2022-10-16 21:54:26 +03:00
|
|
|
$appointment['color'] = $service['color'];
|
|
|
|
|
|
|
|
$appointment_status_options_json = setting('appointment_status_options', '[]');
|
2023-11-29 12:24:09 +03:00
|
|
|
$appointment_status_options = json_decode($appointment_status_options_json, true) ?? [];
|
|
|
|
$appointment['status'] = $appointment_status_options[0] ?? null;
|
2022-03-25 14:29:17 +03:00
|
|
|
|
|
|
|
$this->appointments_model->only($appointment, [
|
2023-01-18 12:59:57 +03:00
|
|
|
'id',
|
2022-03-25 14:29:17 +03:00
|
|
|
'start_datetime',
|
|
|
|
'end_datetime',
|
|
|
|
'location',
|
|
|
|
'notes',
|
2022-10-16 21:54:26 +03:00
|
|
|
'color',
|
|
|
|
'status',
|
2022-03-25 14:29:17 +03:00
|
|
|
'is_unavailability',
|
|
|
|
'id_users_provider',
|
|
|
|
'id_users_customer',
|
2023-12-22 13:35:41 +03:00
|
|
|
'id_services',
|
2022-03-25 14:29:17 +03:00
|
|
|
]);
|
2022-03-25 14:44:05 +03:00
|
|
|
|
2021-11-25 10:40:37 +03:00
|
|
|
$appointment_id = $this->appointments_model->save($appointment);
|
|
|
|
$appointment = $this->appointments_model->find($appointment_id);
|
2021-11-18 09:12:16 +03:00
|
|
|
|
|
|
|
$settings = [
|
|
|
|
'company_name' => setting('company_name'),
|
|
|
|
'company_link' => setting('company_link'),
|
|
|
|
'company_email' => setting('company_email'),
|
|
|
|
'date_format' => setting('date_format'),
|
2023-12-22 13:35:41 +03:00
|
|
|
'time_format' => setting('time_format'),
|
2021-11-18 09:12:16 +03:00
|
|
|
];
|
|
|
|
|
2023-03-13 11:06:18 +03:00
|
|
|
$this->synchronization->sync_appointment_saved($appointment, $service, $provider, $customer, $settings);
|
2021-11-18 09:12:16 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$this->notifications->notify_appointment_saved(
|
|
|
|
$appointment,
|
|
|
|
$service,
|
|
|
|
$provider,
|
|
|
|
$customer,
|
|
|
|
$settings,
|
2023-12-22 13:35:41 +03:00
|
|
|
$manage_mode,
|
2023-11-29 12:24:09 +03:00
|
|
|
);
|
2021-11-18 09:12:16 +03:00
|
|
|
|
2022-06-19 20:05:45 +03:00
|
|
|
$this->webhooks_client->trigger(WEBHOOK_APPOINTMENT_SAVE, $appointment);
|
|
|
|
|
2021-11-18 09:12:16 +03:00
|
|
|
$response = [
|
|
|
|
'appointment_id' => $appointment['id'],
|
2023-12-22 13:35:41 +03:00
|
|
|
'appointment_hash' => $appointment['hash'],
|
2021-11-18 09:12:16 +03:00
|
|
|
];
|
2021-12-17 12:55:37 +03:00
|
|
|
|
2021-11-18 09:12:16 +03:00
|
|
|
json_response($response);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2021-11-18 09:12:16 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the provider is still available in the selected appointment date.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2023-03-13 11:06:18 +03:00
|
|
|
* @return int|null Returns the ID of the provider that is available for the appointment.
|
2021-11-18 09:12:16 +03:00
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
protected function check_datetime_availability(): ?int
|
|
|
|
{
|
|
|
|
$post_data = request('post_data');
|
|
|
|
|
|
|
|
$appointment = $post_data['appointment'];
|
|
|
|
|
|
|
|
$appointment_start = new DateTime($appointment['start_datetime']);
|
|
|
|
|
|
|
|
$date = $appointment_start->format('Y-m-d');
|
|
|
|
|
|
|
|
$hour = $appointment_start->format('H:i');
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($appointment['id_users_provider'] === ANY_PROVIDER) {
|
2021-11-18 09:12:16 +03:00
|
|
|
$appointment['id_users_provider'] = $this->search_any_provider($appointment['id_services'], $date, $hour);
|
|
|
|
|
|
|
|
return $appointment['id_users_provider'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$service = $this->services_model->find($appointment['id_services']);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$exclude_appointment_id = $appointment['id'] ?? null;
|
2021-11-18 09:12:16 +03:00
|
|
|
|
|
|
|
$provider = $this->providers_model->find($appointment['id_users_provider']);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$available_hours = $this->availability->get_available_hours(
|
|
|
|
$date,
|
|
|
|
$service,
|
|
|
|
$provider,
|
2023-12-22 13:35:41 +03:00
|
|
|
$exclude_appointment_id,
|
2023-11-29 12:24:09 +03:00
|
|
|
);
|
2021-11-18 09:12:16 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$is_still_available = false;
|
2021-11-18 09:12:16 +03:00
|
|
|
|
|
|
|
$appointment_hour = date('H:i', strtotime($appointment['start_datetime']));
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($available_hours as $available_hour) {
|
|
|
|
if ($appointment_hour === $available_hour) {
|
|
|
|
$is_still_available = true;
|
2021-11-18 09:12:16 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
return $is_still_available ? $appointment['id_users_provider'] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 Service ID
|
|
|
|
* @param string $date Selected date (Y-m-d).
|
|
|
|
* @param string|null $hour Selected hour (H:i).
|
|
|
|
*
|
|
|
|
* @return int|null Returns the ID of the provider that can provide the service at the selected date.
|
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
protected function search_any_provider(int $service_id, string $date, string $hour = null): ?int
|
|
|
|
{
|
|
|
|
$available_providers = $this->providers_model->get_available_providers(true);
|
|
|
|
|
|
|
|
$service = $this->services_model->find($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.
|
|
|
|
$available_hours = $this->availability->get_available_hours($date, $service, $provider);
|
|
|
|
|
|
|
|
if (
|
|
|
|
count($available_hours) > $max_hours_count &&
|
|
|
|
(empty($hour) || in_array($hour, $available_hours))
|
|
|
|
) {
|
|
|
|
$provider_id = $provider['id'];
|
|
|
|
|
|
|
|
$max_hours_count = count($available_hours);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $provider_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the available appointment hours for the selected date.
|
|
|
|
*
|
|
|
|
* This method answers to an AJAX request. It calculates the available hours for the given service, provider and
|
|
|
|
* date.
|
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function get_available_hours(): void
|
2023-11-29 12:24:09 +03:00
|
|
|
{
|
|
|
|
try {
|
2023-12-22 14:48:06 +03:00
|
|
|
$disable_booking = setting('disable_booking');
|
|
|
|
|
|
|
|
if ($disable_booking) {
|
|
|
|
abort(403);
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$provider_id = request('provider_id');
|
|
|
|
$service_id = request('service_id');
|
|
|
|
$selected_date = request('selected_date');
|
|
|
|
|
|
|
|
// Do not continue if there was no provider selected (more likely there is no provider in the system).
|
|
|
|
|
|
|
|
if (empty($provider_id)) {
|
|
|
|
json_response();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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_appointment_id = request('manage_mode') ? request('appointment_id') : null;
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
$service = $this->services_model->find($service_id);
|
|
|
|
|
|
|
|
if ($provider_id === ANY_PROVIDER) {
|
|
|
|
$providers = $this->providers_model->get();
|
|
|
|
|
|
|
|
$available_hours = [];
|
|
|
|
|
|
|
|
foreach ($providers as $provider) {
|
|
|
|
if (!in_array($service_id, $provider['services'])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$provider_available_hours = $this->availability->get_available_hours(
|
|
|
|
$selected_date,
|
|
|
|
$service,
|
|
|
|
$provider,
|
2023-12-22 13:35:41 +03:00
|
|
|
$exclude_appointment_id,
|
2023-11-29 12:24:09 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
$available_hours = array_merge($available_hours, $provider_available_hours);
|
|
|
|
}
|
|
|
|
|
|
|
|
$available_hours = array_unique(array_values($available_hours));
|
|
|
|
|
|
|
|
sort($available_hours);
|
|
|
|
|
|
|
|
$response = $available_hours;
|
|
|
|
} else {
|
|
|
|
$provider = $this->providers_model->find($provider_id);
|
|
|
|
|
|
|
|
$response = $this->availability->get_available_hours(
|
|
|
|
$selected_date,
|
|
|
|
$service,
|
|
|
|
$provider,
|
2023-12-22 13:35:41 +03:00
|
|
|
$exclude_appointment_id,
|
2023-11-29 12:24:09 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
json_response($response);
|
|
|
|
} catch (Throwable $e) {
|
|
|
|
json_exception($e);
|
|
|
|
}
|
2021-11-18 09:12:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-05-26 15:50:06 +03:00
|
|
|
* Get Unavailable Dates
|
2021-11-18 09:12:16 +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
|
2022-05-26 15:50:06 +03:00
|
|
|
* must have the "Y-m-d" format.
|
2021-11-18 09:12:16 +03:00
|
|
|
*
|
2022-01-18 14:54:41 +03:00
|
|
|
* Outputs a JSON string with the unavailability dates. that are unavailability.
|
2021-11-18 09:12:16 +03:00
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function get_unavailable_dates(): void
|
2021-11-18 09:12:16 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
2023-12-22 14:48:06 +03:00
|
|
|
$disable_booking = setting('disable_booking');
|
|
|
|
|
|
|
|
if ($disable_booking) {
|
|
|
|
abort(403);
|
|
|
|
}
|
|
|
|
|
2021-11-18 09:12:16 +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');
|
|
|
|
$selected_date = new DateTime($selected_date_string);
|
2023-11-29 12:24:09 +03:00
|
|
|
$number_of_days_in_month = (int) $selected_date->format('t');
|
2022-05-26 15:50:06 +03:00
|
|
|
$unavailable_dates = [];
|
2021-11-18 09:12:16 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$provider_ids =
|
|
|
|
$provider_id === ANY_PROVIDER ? $this->search_providers_by_service($service_id) : [$provider_id];
|
2021-11-18 09:12:16 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$exclude_appointment_id = $manage_mode ? $appointment_id : null;
|
2021-11-18 09:12:16 +03:00
|
|
|
|
|
|
|
// Get the service record.
|
|
|
|
$service = $this->services_model->find($service_id);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
for ($i = 1; $i <= $number_of_days_in_month; $i++) {
|
2021-11-18 09:12:16 +03:00
|
|
|
$current_date = new DateTime($selected_date->format('Y-m') . '-' . $i);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($current_date < new DateTime(date('Y-m-d 00:00:00'))) {
|
2022-01-18 14:54:41 +03:00
|
|
|
// Past dates become immediately unavailability.
|
2022-05-26 15:50:06 +03:00
|
|
|
$unavailable_dates[] = $current_date->format('Y-m-d');
|
2021-11-18 09:12:16 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finding at least one slot of availability.
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($provider_ids as $current_provider_id) {
|
2021-11-18 09:12:16 +03:00
|
|
|
$provider = $this->providers_model->find($current_provider_id);
|
|
|
|
|
|
|
|
$available_hours = $this->availability->get_available_hours(
|
|
|
|
$current_date->format('Y-m-d'),
|
|
|
|
$service,
|
|
|
|
$provider,
|
2023-12-22 13:35:41 +03:00
|
|
|
$exclude_appointment_id,
|
2021-11-18 09:12:16 +03:00
|
|
|
);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!empty($available_hours)) {
|
2021-11-18 09:12:16 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No availability amongst all the provider.
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($available_hours)) {
|
2022-05-26 15:50:06 +03:00
|
|
|
$unavailable_dates[] = $current_date->format('Y-m-d');
|
2021-11-18 09:12:16 +03:00
|
|
|
}
|
|
|
|
}
|
2023-10-23 12:46:41 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (count($unavailable_dates) === $number_of_days_in_month) {
|
2023-07-11 09:11:19 +03:00
|
|
|
json_response([
|
2023-12-22 13:35:41 +03:00
|
|
|
'is_month_unavailable' => true,
|
2023-07-11 09:11:19 +03:00
|
|
|
]);
|
2023-10-23 12:46:41 +03:00
|
|
|
|
|
|
|
return;
|
2023-07-11 09:11:19 +03:00
|
|
|
}
|
2021-11-18 09:12:16 +03:00
|
|
|
|
2022-05-26 15:50:06 +03:00
|
|
|
json_response($unavailable_dates);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2021-11-18 09:12:16 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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(int $service_id): array
|
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
$available_providers = $this->providers_model->get_available_providers(true);
|
2021-11-18 09:12:16 +03:00
|
|
|
$provider_list = [];
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($available_providers as $provider) {
|
|
|
|
foreach ($provider['services'] as $provider_service_id) {
|
|
|
|
if ($provider_service_id === $service_id) {
|
2021-11-18 09:12:16 +03:00
|
|
|
// Check if the provider is affected to the selected service.
|
|
|
|
$provider_list[] = $provider['id'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $provider_list;
|
|
|
|
}
|
|
|
|
}
|