2021-11-18 09:30:52 +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:30:52 +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:30:52 +03:00
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link https://easyappointments.org
|
|
|
|
* @since v1.5.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
2021-11-25 11:13:19 +03:00
|
|
|
* Calendar controller.
|
|
|
|
*
|
2021-11-18 09:30:52 +03:00
|
|
|
* Handles calendar related operations.
|
|
|
|
*
|
2021-11-25 11:13:19 +03:00
|
|
|
* @package Controllers
|
2021-11-18 09:30:52 +03:00
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
class Calendar extends EA_Controller
|
|
|
|
{
|
2024-05-11 17:42:01 +03:00
|
|
|
public array $allowed_customer_fields = [
|
|
|
|
'id',
|
|
|
|
'first_name',
|
|
|
|
'last_name',
|
|
|
|
'email',
|
|
|
|
'phone_number',
|
|
|
|
'address',
|
|
|
|
'city',
|
|
|
|
'state',
|
|
|
|
'zip_code',
|
|
|
|
'timezone',
|
|
|
|
'language',
|
|
|
|
'notes',
|
|
|
|
'custom_field_1',
|
|
|
|
'custom_field_2',
|
|
|
|
'custom_field_3',
|
|
|
|
'custom_field_4',
|
|
|
|
'custom_field_5',
|
|
|
|
];
|
2024-08-13 14:34:03 +03:00
|
|
|
|
|
|
|
public array $optional_customer_fields = [
|
|
|
|
//
|
|
|
|
];
|
|
|
|
|
2024-05-11 17:42:01 +03:00
|
|
|
public array $allowed_appointment_fields = [
|
|
|
|
'id',
|
|
|
|
'start_datetime',
|
|
|
|
'end_datetime',
|
|
|
|
'location',
|
|
|
|
'notes',
|
|
|
|
'color',
|
|
|
|
'status',
|
|
|
|
'is_unavailability',
|
|
|
|
'id_users_provider',
|
|
|
|
'id_users_customer',
|
|
|
|
'id_services',
|
|
|
|
];
|
|
|
|
|
2024-08-13 14:34:03 +03:00
|
|
|
public array $optional_appointment_fields = [
|
|
|
|
//
|
|
|
|
];
|
|
|
|
|
2021-11-18 09:30:52 +03:00
|
|
|
/**
|
|
|
|
* Calendar constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2021-11-25 11:13:19 +03:00
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->load->model('appointments_model');
|
2022-01-12 13:22:54 +03:00
|
|
|
$this->load->model('unavailabilities_model');
|
2023-11-17 10:40:29 +03:00
|
|
|
$this->load->model('blocked_periods_model');
|
2021-11-25 11:13:19 +03:00
|
|
|
$this->load->model('customers_model');
|
|
|
|
$this->load->model('services_model');
|
2021-11-18 09:30:52 +03:00
|
|
|
$this->load->model('providers_model');
|
2021-11-24 11:23:48 +03:00
|
|
|
$this->load->model('roles_model');
|
2021-11-18 09:30:52 +03:00
|
|
|
|
2021-11-25 11:13:19 +03:00
|
|
|
$this->load->library('accounts');
|
2021-11-18 09:30:52 +03:00
|
|
|
$this->load->library('google_sync');
|
|
|
|
$this->load->library('notifications');
|
|
|
|
$this->load->library('synchronization');
|
|
|
|
$this->load->library('timezones');
|
2022-06-19 20:05:45 +03:00
|
|
|
$this->load->library('webhooks_client');
|
2021-11-18 09:30:52 +03:00
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
/**
|
|
|
|
* Render the calendar page and display the selected appointment.
|
|
|
|
*
|
|
|
|
* This method will call the "index" callback to handle the page rendering.
|
|
|
|
*
|
|
|
|
* @param string $appointment_hash Appointment hash.
|
|
|
|
*/
|
2024-04-26 17:23:58 +03:00
|
|
|
public function reschedule(string $appointment_hash): void
|
2023-11-29 12:24:09 +03:00
|
|
|
{
|
|
|
|
$this->index($appointment_hash);
|
|
|
|
}
|
|
|
|
|
2021-11-25 11:13:19 +03:00
|
|
|
/**
|
|
|
|
* Display the main backend page.
|
|
|
|
*
|
2021-12-18 19:22:40 +03:00
|
|
|
* This method displays the main backend page. All login permission can view this page which displays a calendar
|
|
|
|
* with the events of the selected provider or service. If a user has more privileges he will see more menus at the
|
|
|
|
* top of the page.
|
2021-11-25 11:13:19 +03:00
|
|
|
*
|
2021-12-18 19:22:40 +03:00
|
|
|
* @param string $appointment_hash Appointment hash.
|
2021-11-25 11:13:19 +03:00
|
|
|
*/
|
2024-04-26 17:23:58 +03:00
|
|
|
public function index(string $appointment_hash = ''): void
|
2021-11-25 11:13:19 +03:00
|
|
|
{
|
2024-06-03 20:39:40 +03:00
|
|
|
session([
|
|
|
|
'dest_url' => site_url('calendar/index' . (!empty($appointment_hash) ? '/' . $appointment_hash : '')),
|
|
|
|
]);
|
2021-11-25 11:13:19 +03:00
|
|
|
|
2022-01-17 20:24:02 +03:00
|
|
|
$user_id = session('user_id');
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (cannot('view', PRIV_APPOINTMENTS)) {
|
|
|
|
if ($user_id) {
|
2022-01-17 20:24:02 +03:00
|
|
|
abort(403, 'Forbidden');
|
|
|
|
}
|
|
|
|
|
|
|
|
redirect('login');
|
|
|
|
|
2021-11-25 11:13:19 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$role_slug = session('role_slug');
|
|
|
|
|
|
|
|
$user = $this->users_model->find($user_id);
|
|
|
|
|
|
|
|
$secretary_providers = [];
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($role_slug === DB_SLUG_SECRETARY) {
|
2021-11-25 11:13:19 +03:00
|
|
|
$secretary = $this->secretaries_model->find(session('user_id'));
|
|
|
|
|
|
|
|
$secretary_providers = $secretary['providers'];
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$edit_appointment = null;
|
2021-11-25 11:13:19 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!empty($appointment_hash)) {
|
2021-12-18 19:22:40 +03:00
|
|
|
$occurrences = $this->appointments_model->get(['hash' => $appointment_hash]);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($appointment_hash !== '' && !empty($occurrences)) {
|
2021-12-18 19:22:40 +03:00
|
|
|
$edit_appointment = $occurrences[0];
|
|
|
|
|
|
|
|
$this->appointments_model->load($edit_appointment, ['customer']);
|
|
|
|
}
|
2021-11-25 11:13:19 +03:00
|
|
|
}
|
|
|
|
|
2022-01-12 13:22:54 +03:00
|
|
|
$privileges = $this->roles_model->get_permissions_by_slug($role_slug);
|
|
|
|
|
|
|
|
$available_providers = $this->providers_model->get_available_providers();
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($role_slug === DB_SLUG_PROVIDER) {
|
|
|
|
$available_providers = array_values(
|
|
|
|
array_filter($available_providers, function ($available_provider) use ($user_id) {
|
|
|
|
return (int) $available_provider['id'] === (int) $user_id;
|
2023-12-22 13:35:41 +03:00
|
|
|
}),
|
2023-11-29 12:24:09 +03:00
|
|
|
);
|
2022-01-24 23:57:17 +03:00
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($role_slug === DB_SLUG_SECRETARY) {
|
|
|
|
$available_providers = array_values(
|
|
|
|
array_filter($available_providers, function ($available_provider) use ($secretary_providers) {
|
|
|
|
return in_array($available_provider['id'], $secretary_providers);
|
2023-12-22 13:35:41 +03:00
|
|
|
}),
|
2023-11-29 12:24:09 +03:00
|
|
|
);
|
2022-01-24 23:57:17 +03:00
|
|
|
}
|
|
|
|
|
2022-01-12 13:22:54 +03:00
|
|
|
$available_services = $this->services_model->get_available_services();
|
2022-01-24 22:50:14 +03:00
|
|
|
|
|
|
|
$calendar_view = request('view', $user['settings']['calendar_view']);
|
2022-01-12 13:22:54 +03:00
|
|
|
|
2022-10-16 21:54:26 +03:00
|
|
|
$appointment_status_options = setting('appointment_status_options');
|
|
|
|
|
2022-01-12 13:22:54 +03:00
|
|
|
script_vars([
|
|
|
|
'user_id' => $user_id,
|
|
|
|
'role_slug' => $role_slug,
|
|
|
|
'date_format' => setting('date_format'),
|
|
|
|
'time_format' => setting('time_format'),
|
|
|
|
'first_weekday' => setting('first_weekday'),
|
2022-07-27 02:19:42 +03:00
|
|
|
'company_working_plan' => setting('company_working_plan'),
|
2022-01-12 13:22:54 +03:00
|
|
|
'timezones' => $this->timezones->to_array(),
|
|
|
|
'privileges' => $privileges,
|
2022-01-17 23:18:43 +03:00
|
|
|
'calendar_view' => $calendar_view,
|
2023-01-14 12:16:32 +03:00
|
|
|
'available_providers' => $available_providers,
|
2022-01-12 13:22:54 +03:00
|
|
|
'available_services' => $available_services,
|
2022-04-22 14:15:06 +03:00
|
|
|
'secretary_providers' => $secretary_providers,
|
2022-01-17 23:18:43 +03:00
|
|
|
'edit_appointment' => $edit_appointment,
|
2024-05-12 15:24:58 +03:00
|
|
|
'google_sync_feature' => config('google_sync_feature'),
|
2023-12-22 13:35:41 +03:00
|
|
|
'customers' => $this->customers_model->get(null, 50, null, 'update_datetime DESC'),
|
2024-03-30 23:12:04 +03:00
|
|
|
'default_language' => setting('default_language'),
|
|
|
|
'default_timezone' => setting('default_timezone'),
|
2022-01-12 13:22:54 +03:00
|
|
|
]);
|
|
|
|
|
2021-12-18 19:22:40 +03:00
|
|
|
html_vars([
|
2021-11-25 11:13:19 +03:00
|
|
|
'page_title' => lang('calendar'),
|
|
|
|
'active_menu' => PRIV_APPOINTMENTS,
|
|
|
|
'user_display_name' => $this->accounts->get_user_display_name($user_id),
|
2022-01-12 13:22:54 +03:00
|
|
|
'timezone' => session('timezone'),
|
2021-11-25 11:13:19 +03:00
|
|
|
'timezones' => $this->timezones->to_array(),
|
2022-03-29 11:15:24 +03:00
|
|
|
'grouped_timezones' => $this->timezones->to_grouped_array(),
|
2022-01-12 13:22:54 +03:00
|
|
|
'privileges' => $privileges,
|
2022-01-17 23:18:43 +03:00
|
|
|
'calendar_view' => $calendar_view,
|
2022-01-12 13:22:54 +03:00
|
|
|
'available_providers' => $available_providers,
|
|
|
|
'available_services' => $available_services,
|
2021-11-25 11:13:19 +03:00
|
|
|
'secretary_providers' => $secretary_providers,
|
2023-11-29 12:24:09 +03:00
|
|
|
'appointment_status_options' => json_decode($appointment_status_options, true) ?? [],
|
2022-01-05 10:31:51 +03:00
|
|
|
'require_first_name' => setting('require_first_name'),
|
|
|
|
'require_last_name' => setting('require_last_name'),
|
|
|
|
'require_email' => setting('require_email'),
|
|
|
|
'require_phone_number' => setting('require_phone_number'),
|
|
|
|
'require_address' => setting('require_address'),
|
|
|
|
'require_city' => setting('require_city'),
|
|
|
|
'require_zip_code' => setting('require_zip_code'),
|
2023-12-22 13:35:41 +03:00
|
|
|
'require_notes' => setting('require_notes'),
|
2021-11-25 11:13:19 +03:00
|
|
|
]);
|
2021-12-18 19:22:40 +03:00
|
|
|
|
2022-01-19 12:21:05 +03:00
|
|
|
$this->load->view('pages/calendar');
|
2021-11-25 11:13:19 +03:00
|
|
|
}
|
|
|
|
|
2021-11-18 09:30:52 +03:00
|
|
|
/**
|
|
|
|
* Save appointment changes that are made from the backend calendar page.
|
|
|
|
*/
|
2024-04-26 17:23:58 +03:00
|
|
|
public function save_appointment(): void
|
2021-11-18 09:30:52 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
2021-11-18 09:30:52 +03:00
|
|
|
$customer_data = request('customer_data');
|
2024-05-12 15:24:58 +03:00
|
|
|
|
2023-07-17 09:14:04 +03:00
|
|
|
$appointment_data = request('appointment_data');
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$this->check_event_permissions((int) $appointment_data['id_users_provider']);
|
2021-11-18 09:30:52 +03:00
|
|
|
|
2023-07-17 09:14:04 +03:00
|
|
|
// Save customer changes to the database.
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($customer_data) {
|
2022-01-12 13:22:54 +03:00
|
|
|
$customer = $customer_data;
|
2021-11-18 09:30:52 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$required_permissions = !empty($customer['id'])
|
2021-12-18 19:22:40 +03:00
|
|
|
? can('add', PRIV_CUSTOMERS)
|
|
|
|
: can('edit', PRIV_CUSTOMERS);
|
2021-11-18 09:30:52 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$required_permissions) {
|
2022-06-20 12:31:11 +03:00
|
|
|
throw new RuntimeException('You do not have the required permissions for this task.');
|
2021-11-18 09:30:52 +03:00
|
|
|
}
|
|
|
|
|
2024-05-11 17:42:01 +03:00
|
|
|
$this->customers_model->only($customer, $this->allowed_customer_fields);
|
2022-03-25 14:29:17 +03:00
|
|
|
|
2024-08-13 14:34:03 +03:00
|
|
|
$this->customers_model->optional($customer, $this->optional_customer_fields);
|
|
|
|
|
2021-11-18 09:30:52 +03:00
|
|
|
$customer['id'] = $this->customers_model->save($customer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save appointment changes to the database.
|
2023-11-29 12:24:09 +03:00
|
|
|
$manage_mode = !empty($appointment_data['id']);
|
2021-11-18 09:30:52 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($appointment_data) {
|
2022-01-12 13:22:54 +03:00
|
|
|
$appointment = $appointment_data;
|
2021-11-18 09:30:52 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$required_permissions = !empty($appointment['id'])
|
2021-12-18 19:22:40 +03:00
|
|
|
? can('add', PRIV_APPOINTMENTS)
|
|
|
|
: can('edit', PRIV_APPOINTMENTS);
|
2021-11-18 09:30:52 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$required_permissions) {
|
2022-06-20 12:31:11 +03:00
|
|
|
throw new RuntimeException('You do not have the required permissions for this task.');
|
2021-11-18 09:30:52 +03:00
|
|
|
}
|
|
|
|
|
2024-05-12 15:24:58 +03:00
|
|
|
// If the appointment does not contain the customer record id, then it means that is going to be inserted.
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!isset($appointment['id_users_customer'])) {
|
2021-11-18 09:30:52 +03:00
|
|
|
$appointment['id_users_customer'] = $customer['id'] ?? $customer_data['id'];
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($manage_mode && !empty($appointment['id'])) {
|
2022-07-26 16:43:37 +03:00
|
|
|
$this->synchronization->remove_appointment_on_provider_change($appointment['id']);
|
|
|
|
}
|
|
|
|
|
2024-05-11 17:42:01 +03:00
|
|
|
$this->appointments_model->only($appointment, $this->allowed_appointment_fields);
|
2022-03-25 14:29:17 +03:00
|
|
|
|
2024-08-13 14:34:03 +03:00
|
|
|
$this->appointments_model->optional($appointment, $this->optional_appointment_fields);
|
|
|
|
|
2021-11-18 09:30:52 +03:00
|
|
|
$appointment['id'] = $this->appointments_model->save($appointment);
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($appointment['id'])) {
|
2021-11-18 09:30:52 +03:00
|
|
|
throw new RuntimeException('The appointment ID is not available.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$appointment = $this->appointments_model->find($appointment['id']);
|
2023-12-13 14:22:16 +03:00
|
|
|
$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']);
|
2021-11-18 09:30:52 +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:30:52 +03:00
|
|
|
];
|
|
|
|
|
2023-03-13 11:06:18 +03:00
|
|
|
$this->synchronization->sync_appointment_saved($appointment, $service, $provider, $customer, $settings);
|
2021-11-18 09:30:52 +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:30:52 +03:00
|
|
|
|
2022-06-19 20:05:45 +03:00
|
|
|
$this->webhooks_client->trigger(WEBHOOK_APPOINTMENT_SAVE, $appointment);
|
|
|
|
|
2021-11-18 09:30:52 +03:00
|
|
|
json_response([
|
2023-12-22 13:35:41 +03:00
|
|
|
'success' => true,
|
2021-11-18 09:30:52 +03:00
|
|
|
]);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2021-11-18 09:30:52 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
private function check_event_permissions(int $provider_id): void
|
|
|
|
{
|
|
|
|
$user_id = (int) session('user_id');
|
|
|
|
$role_slug = session('role_slug');
|
|
|
|
|
|
|
|
if (
|
|
|
|
$role_slug === DB_SLUG_SECRETARY &&
|
|
|
|
!$this->secretaries_model->is_provider_supported($user_id, $provider_id)
|
|
|
|
) {
|
|
|
|
abort(403);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($role_slug === DB_SLUG_PROVIDER && $user_id !== $provider_id) {
|
|
|
|
abort(403);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-18 09:30:52 +03:00
|
|
|
/**
|
|
|
|
* Delete appointment from the database.
|
|
|
|
*
|
|
|
|
* This method deletes an existing appointment from the database. Once this action is finished it cannot be undone.
|
|
|
|
* Notification emails are send to both provider and customer and the delete action is executed to the Google
|
|
|
|
* Calendar account of the provider, if the "google_sync" setting is enabled.
|
|
|
|
*/
|
2024-04-26 17:23:58 +03:00
|
|
|
public function delete_appointment(): void
|
2021-11-18 09:30:52 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
|
|
|
if (cannot('delete', 'appointments')) {
|
2022-06-20 12:31:11 +03:00
|
|
|
throw new RuntimeException('You do not have the required permissions for this task.');
|
2021-11-18 09:30:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$appointment_id = request('appointment_id');
|
2023-11-29 12:24:09 +03:00
|
|
|
$cancellation_reason = (string) request('cancellation_reason');
|
2021-11-18 09:30:52 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($appointment_id)) {
|
2022-06-20 12:31:11 +03:00
|
|
|
throw new InvalidArgumentException('No appointment id provided.');
|
2021-11-18 09:30:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store appointment data for later use in this method.
|
|
|
|
$appointment = $this->appointments_model->find($appointment_id);
|
2023-07-17 09:14:04 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$this->check_event_permissions((int) $appointment['id_users_provider']);
|
2023-07-17 09:14:04 +03:00
|
|
|
|
2023-12-13 14:22:16 +03:00
|
|
|
$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']);
|
2021-11-18 09:30:52 +03:00
|
|
|
|
|
|
|
$settings = [
|
|
|
|
'company_name' => setting('company_name'),
|
|
|
|
'company_email' => setting('company_email'),
|
|
|
|
'company_link' => setting('company_link'),
|
|
|
|
'date_format' => setting('date_format'),
|
2023-12-22 13:35:41 +03:00
|
|
|
'time_format' => setting('time_format'),
|
2021-11-18 09:30:52 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
// Delete appointment record from the database.
|
|
|
|
$this->appointments_model->delete($appointment_id);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$this->notifications->notify_appointment_deleted(
|
|
|
|
$appointment,
|
|
|
|
$service,
|
|
|
|
$provider,
|
|
|
|
$customer,
|
|
|
|
$settings,
|
2023-12-22 13:35:41 +03:00
|
|
|
$cancellation_reason,
|
2023-11-29 12:24:09 +03:00
|
|
|
);
|
2021-11-18 09:30:52 +03:00
|
|
|
|
|
|
|
$this->synchronization->sync_appointment_deleted($appointment, $provider);
|
|
|
|
|
2022-06-19 20:05:45 +03:00
|
|
|
$this->webhooks_client->trigger(WEBHOOK_APPOINTMENT_DELETE, $appointment);
|
|
|
|
|
2021-11-18 09:30:52 +03:00
|
|
|
json_response([
|
2023-12-22 13:35:41 +03:00
|
|
|
'success' => true,
|
2021-11-18 09:30:52 +03:00
|
|
|
]);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2021-11-18 09:30:52 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-01-18 14:54:41 +03:00
|
|
|
* Insert of update unavailability to database.
|
2021-11-18 09:30:52 +03:00
|
|
|
*/
|
2024-04-26 17:23:58 +03:00
|
|
|
public function save_unavailability(): void
|
2021-11-18 09:30:52 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
2021-11-18 09:30:52 +03:00
|
|
|
// Check privileges
|
2022-01-18 14:54:41 +03:00
|
|
|
$unavailability = request('unavailability');
|
2021-11-18 09:30:52 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$required_permissions = !isset($unavailability['id'])
|
2021-12-18 19:22:40 +03:00
|
|
|
? can('add', PRIV_APPOINTMENTS)
|
|
|
|
: can('edit', PRIV_APPOINTMENTS);
|
2021-11-18 09:30:52 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$required_permissions) {
|
2022-06-20 12:31:11 +03:00
|
|
|
throw new RuntimeException('You do not have the required permissions for this task.');
|
2021-11-18 09:30:52 +03:00
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$provider_id = (int) $unavailability['id_users_provider'];
|
2023-07-17 09:14:04 +03:00
|
|
|
|
|
|
|
$this->check_event_permissions($provider_id);
|
|
|
|
|
|
|
|
$provider = $this->providers_model->find($provider_id);
|
2021-11-18 09:30:52 +03:00
|
|
|
|
2022-06-19 20:05:45 +03:00
|
|
|
$unavailability_id = $this->unavailabilities_model->save($unavailability);
|
2021-11-18 09:30:52 +03:00
|
|
|
|
2022-06-19 20:05:45 +03:00
|
|
|
$unavailability = $this->unavailabilities_model->find($unavailability_id);
|
2021-11-18 09:30:52 +03:00
|
|
|
|
2022-06-19 20:05:45 +03:00
|
|
|
$this->synchronization->sync_unavailability_saved($unavailability, $provider);
|
2021-11-18 09:30:52 +03:00
|
|
|
|
2022-06-19 20:05:45 +03:00
|
|
|
$this->webhooks_client->trigger(WEBHOOK_UNAVAILABILITY_SAVE, $unavailability);
|
2021-11-18 09:30:52 +03:00
|
|
|
|
|
|
|
json_response([
|
2023-11-29 12:24:09 +03:00
|
|
|
'success' => true,
|
2023-12-22 13:35:41 +03:00
|
|
|
'warnings' => $warnings ?? [],
|
2021-11-18 09:30:52 +03:00
|
|
|
]);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2021-11-18 09:30:52 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-01-18 14:54:41 +03:00
|
|
|
* Delete an unavailability from database.
|
2021-11-18 09:30:52 +03:00
|
|
|
*/
|
2024-04-26 17:23:58 +03:00
|
|
|
public function delete_unavailability(): void
|
2021-11-18 09:30:52 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
|
|
|
if (cannot('delete', PRIV_APPOINTMENTS)) {
|
2022-06-20 12:31:11 +03:00
|
|
|
throw new RuntimeException('You do not have the required permissions for this task.');
|
2021-11-18 09:30:52 +03:00
|
|
|
}
|
|
|
|
|
2022-01-18 14:54:41 +03:00
|
|
|
$unavailability_id = request('unavailability_id');
|
2021-11-18 09:30:52 +03:00
|
|
|
|
2023-07-18 08:54:47 +03:00
|
|
|
$unavailability = $this->unavailabilities_model->find($unavailability_id);
|
2023-10-09 17:48:35 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$this->check_event_permissions((int) $unavailability['id_users_provider']);
|
2021-11-18 09:30:52 +03:00
|
|
|
|
2022-01-18 14:54:41 +03:00
|
|
|
$provider = $this->providers_model->find($unavailability['id_users_provider']);
|
2021-11-18 09:30:52 +03:00
|
|
|
|
2022-06-19 20:05:45 +03:00
|
|
|
$this->unavailabilities_model->delete($unavailability_id);
|
2021-11-18 09:30:52 +03:00
|
|
|
|
2023-03-13 11:06:18 +03:00
|
|
|
$this->synchronization->sync_unavailability_deleted($unavailability, $provider);
|
2021-11-18 09:30:52 +03:00
|
|
|
|
2022-06-19 20:05:45 +03:00
|
|
|
$this->webhooks_client->trigger(WEBHOOK_UNAVAILABILITY_DELETE, $unavailability);
|
2021-11-18 09:30:52 +03:00
|
|
|
|
|
|
|
json_response([
|
2023-12-22 13:35:41 +03:00
|
|
|
'success' => true,
|
2021-11-18 09:30:52 +03:00
|
|
|
]);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2021-11-18 09:30:52 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert of update working plan exceptions to database.
|
|
|
|
*/
|
2024-04-26 17:23:58 +03:00
|
|
|
public function save_working_plan_exception(): void
|
2021-11-18 09:30:52 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
|
|
|
if (cannot('edit', PRIV_USERS)) {
|
2022-06-20 12:31:11 +03:00
|
|
|
throw new RuntimeException('You do not have the required permissions for this task.');
|
2021-11-18 09:30:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$date = request('date');
|
|
|
|
|
2024-05-24 20:59:53 +03:00
|
|
|
$original_date = request('original_date');
|
|
|
|
|
2021-11-18 09:30:52 +03:00
|
|
|
$working_plan_exception = request('working_plan_exception');
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$working_plan_exception) {
|
|
|
|
$working_plan_exception = null;
|
2023-07-10 09:22:55 +03:00
|
|
|
}
|
|
|
|
|
2021-11-18 09:30:52 +03:00
|
|
|
$provider_id = request('provider_id');
|
|
|
|
|
|
|
|
$this->providers_model->save_working_plan_exception($provider_id, $date, $working_plan_exception);
|
|
|
|
|
2024-05-27 17:46:38 +03:00
|
|
|
if ($original_date && $date !== $original_date) {
|
2024-05-24 20:59:53 +03:00
|
|
|
$this->providers_model->delete_working_plan_exception($provider_id, $original_date);
|
|
|
|
}
|
|
|
|
|
2021-11-18 09:30:52 +03:00
|
|
|
json_response([
|
2023-12-22 13:35:41 +03:00
|
|
|
'success' => true,
|
2021-11-18 09:30:52 +03:00
|
|
|
]);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2021-11-18 09:30:52 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a working plan exceptions time period to database.
|
|
|
|
*/
|
2024-04-26 17:23:58 +03:00
|
|
|
public function delete_working_plan_exception(): void
|
2021-11-18 09:30:52 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
2021-12-18 19:22:40 +03:00
|
|
|
$required_permissions = can('edit', PRIV_CUSTOMERS);
|
2021-11-18 09:30:52 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$required_permissions) {
|
2022-06-20 12:31:11 +03:00
|
|
|
throw new RuntimeException('You do not have the required permissions for this task.');
|
2021-11-18 09:30:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$date = request('date');
|
|
|
|
|
|
|
|
$provider_id = request('provider_id');
|
|
|
|
|
|
|
|
$this->providers_model->delete_working_plan_exception($provider_id, $date);
|
|
|
|
|
|
|
|
json_response([
|
2023-12-22 13:35:41 +03:00
|
|
|
'success' => true,
|
2021-11-18 09:30:52 +03:00
|
|
|
]);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2021-11-18 09:30:52 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Calendar Events
|
|
|
|
*
|
|
|
|
* This method will return all the calendar events within a specified period.
|
|
|
|
*/
|
2024-04-26 17:23:58 +03:00
|
|
|
public function get_calendar_appointments_for_table_view(): void
|
2021-11-18 09:30:52 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
2022-11-30 13:22:43 +03:00
|
|
|
$required_permissions = can('view', PRIV_APPOINTMENTS);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$required_permissions) {
|
2022-11-30 13:22:43 +03:00
|
|
|
throw new RuntimeException('You do not have the required permissions for this task.');
|
|
|
|
}
|
2023-01-14 12:16:32 +03:00
|
|
|
|
2022-01-17 23:18:43 +03:00
|
|
|
$start_date = request('start_date') . ' 00:00:00';
|
2021-11-18 09:30:52 +03:00
|
|
|
|
2022-01-17 23:18:43 +03:00
|
|
|
$end_date = request('end_date') . ' 23:59:59';
|
2021-11-18 09:30:52 +03:00
|
|
|
|
|
|
|
$response = [
|
|
|
|
'appointments' => $this->appointments_model->get([
|
|
|
|
'start_datetime >=' => $start_date,
|
2023-12-22 13:35:41 +03:00
|
|
|
'end_datetime <=' => $end_date,
|
2021-11-18 09:30:52 +03:00
|
|
|
]),
|
2022-01-18 19:38:02 +03:00
|
|
|
'unavailabilities' => $this->unavailabilities_model->get([
|
2021-11-18 09:30:52 +03:00
|
|
|
'start_datetime >=' => $start_date,
|
2023-12-22 13:35:41 +03:00
|
|
|
'end_datetime <=' => $end_date,
|
|
|
|
]),
|
2021-11-18 09:30:52 +03:00
|
|
|
];
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($response['appointments'] as &$appointment) {
|
2023-12-13 14:22:16 +03:00
|
|
|
$appointment['provider'] = $this->providers_model->find($appointment['id_users_provider']);
|
|
|
|
$appointment['service'] = $this->services_model->find($appointment['id_services']);
|
|
|
|
$appointment['customer'] = $this->customers_model->find($appointment['id_users_customer']);
|
2021-11-18 09:30:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
unset($appointment);
|
|
|
|
|
|
|
|
$user_id = session('user_id');
|
|
|
|
|
|
|
|
$role_slug = session('role_slug');
|
|
|
|
|
|
|
|
// If the current user is a provider he must only see his own appointments.
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($role_slug === DB_SLUG_PROVIDER) {
|
|
|
|
foreach ($response['appointments'] as $index => $appointment) {
|
|
|
|
if ((int) $appointment['id_users_provider'] !== (int) $user_id) {
|
2021-11-18 09:30:52 +03:00
|
|
|
unset($response['appointments'][$index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 17:01:04 +03:00
|
|
|
$response['appointments'] = array_values($response['appointments']);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($response['unavailabilities'] as $index => $unavailability) {
|
|
|
|
if ((int) $unavailability['id_users_provider'] !== (int) $user_id) {
|
2022-10-03 17:01:04 +03:00
|
|
|
unset($response['unavailabilities'][$index]);
|
2021-11-18 09:30:52 +03:00
|
|
|
}
|
|
|
|
}
|
2022-10-03 17:01:04 +03:00
|
|
|
|
|
|
|
$response['unavailabilities'] = array_values($response['unavailabilities']);
|
2021-11-18 09:30:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the current user is a secretary he must only see the appointments of his providers.
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($role_slug === DB_SLUG_SECRETARY) {
|
2021-11-18 09:30:52 +03:00
|
|
|
$providers = $this->secretaries_model->find($user_id)['providers'];
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($response['appointments'] as $index => $appointment) {
|
|
|
|
if (!in_array((int) $appointment['id_users_provider'], $providers)) {
|
2021-11-18 09:30:52 +03:00
|
|
|
unset($response['appointments'][$index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 17:01:04 +03:00
|
|
|
$response['appointments'] = array_values($response['appointments']);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($response['unavailabilities'] as $index => $unavailability) {
|
|
|
|
if (!in_array((int) $unavailability['id_users_provider'], $providers)) {
|
2022-10-03 17:01:04 +03:00
|
|
|
unset($response['unavailabilities'][$index]);
|
2021-11-18 09:30:52 +03:00
|
|
|
}
|
|
|
|
}
|
2022-10-03 17:01:04 +03:00
|
|
|
|
|
|
|
$response['unavailabilities'] = array_values($response['unavailabilities']);
|
2021-11-18 09:30:52 +03:00
|
|
|
}
|
|
|
|
|
2024-04-26 17:23:58 +03:00
|
|
|
foreach ($response['unavailabilities'] as &$unavailability) {
|
|
|
|
$unavailability['provider'] = $this->providers_model->find($unavailability['id_users_provider']);
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($unavailability);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
// Add blocked periods to the response.
|
2023-11-17 11:42:57 +03:00
|
|
|
$start_date = request('start_date');
|
|
|
|
$end_date = request('end_date');
|
|
|
|
$response['blocked_periods'] = $this->blocked_periods_model->get_for_period($start_date, $end_date);
|
|
|
|
|
2021-11-18 09:30:52 +03:00
|
|
|
json_response($response);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2021-11-18 09:30:52 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the registered appointments for the given date period and record.
|
|
|
|
*
|
2022-01-18 14:54:41 +03:00
|
|
|
* This method returns the database appointments and unavailability periods for the user selected date period and
|
2021-11-18 09:30:52 +03:00
|
|
|
* record type (provider or service).
|
|
|
|
*/
|
2024-04-26 17:23:58 +03:00
|
|
|
public function get_calendar_appointments(): void
|
2021-11-18 09:30:52 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
|
|
|
if (cannot('view', PRIV_APPOINTMENTS)) {
|
2022-06-20 12:31:11 +03:00
|
|
|
throw new RuntimeException('You do not have the required permissions for this task.');
|
2021-11-18 09:30:52 +03:00
|
|
|
}
|
|
|
|
|
2022-01-25 00:07:24 +03:00
|
|
|
$record_id = request('record_id');
|
2022-06-19 20:05:45 +03:00
|
|
|
|
2024-04-26 17:40:33 +03:00
|
|
|
$is_all = request('record_id') === FILTER_TYPE_ALL;
|
2021-11-18 09:30:52 +03:00
|
|
|
|
2024-04-26 17:45:31 +03:00
|
|
|
$filter_type = request('filter_type');
|
|
|
|
|
2024-04-26 17:40:33 +03:00
|
|
|
if (!$filter_type && !$is_all) {
|
2021-11-18 09:30:52 +03:00
|
|
|
json_response([
|
|
|
|
'appointments' => [],
|
2023-12-22 13:35:41 +03:00
|
|
|
'unavailabilities' => [],
|
2021-11-18 09:30:52 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-25 00:07:24 +03:00
|
|
|
$record_id = $this->db->escape($record_id);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($filter_type == FILTER_TYPE_PROVIDER) {
|
2021-11-18 09:30:52 +03:00
|
|
|
$where_id = 'id_users_provider';
|
2023-11-29 12:24:09 +03:00
|
|
|
} elseif ($filter_type === FILTER_TYPE_SERVICE) {
|
2021-11-18 09:30:52 +03:00
|
|
|
$where_id = 'id_services';
|
2023-11-29 12:24:09 +03:00
|
|
|
} else {
|
2022-01-25 00:07:24 +03:00
|
|
|
$where_id = $record_id;
|
2021-11-18 09:30:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get appointments
|
|
|
|
$start_date = $this->db->escape(request('start_date'));
|
|
|
|
$end_date = $this->db->escape(date('Y-m-d', strtotime(request('end_date') . ' +1 day')));
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$where_clause =
|
|
|
|
$where_id .
|
|
|
|
' = ' .
|
|
|
|
$record_id .
|
|
|
|
'
|
|
|
|
AND ((start_datetime > ' .
|
|
|
|
$start_date .
|
|
|
|
' AND start_datetime < ' .
|
|
|
|
$end_date .
|
|
|
|
')
|
|
|
|
or (end_datetime > ' .
|
|
|
|
$start_date .
|
|
|
|
' AND end_datetime < ' .
|
|
|
|
$end_date .
|
|
|
|
')
|
|
|
|
or (start_datetime <= ' .
|
|
|
|
$start_date .
|
|
|
|
' AND end_datetime >= ' .
|
|
|
|
$end_date .
|
|
|
|
'))
|
2022-01-18 14:54:41 +03:00
|
|
|
AND is_unavailability = 0
|
2021-11-18 09:30:52 +03:00
|
|
|
';
|
|
|
|
|
|
|
|
$response['appointments'] = $this->appointments_model->get($where_clause);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($response['appointments'] as &$appointment) {
|
2023-12-13 14:22:16 +03:00
|
|
|
$appointment['provider'] = $this->providers_model->find($appointment['id_users_provider']);
|
|
|
|
$appointment['service'] = $this->services_model->find($appointment['id_services']);
|
|
|
|
$appointment['customer'] = $this->customers_model->find($appointment['id_users_customer']);
|
2021-11-18 09:30:52 +03:00
|
|
|
}
|
|
|
|
|
2024-04-26 17:40:33 +03:00
|
|
|
unset($appointment);
|
|
|
|
|
2022-01-18 14:54:41 +03:00
|
|
|
// Get unavailability periods (only for provider).
|
|
|
|
$response['unavailabilities'] = [];
|
2021-11-18 09:30:52 +03:00
|
|
|
|
2024-04-26 17:45:31 +03:00
|
|
|
if ($filter_type == FILTER_TYPE_PROVIDER || $is_all) {
|
2023-11-29 12:24:09 +03:00
|
|
|
$where_clause =
|
|
|
|
$where_id .
|
|
|
|
' = ' .
|
|
|
|
$record_id .
|
|
|
|
'
|
|
|
|
AND ((start_datetime > ' .
|
|
|
|
$start_date .
|
|
|
|
' AND start_datetime < ' .
|
|
|
|
$end_date .
|
|
|
|
')
|
|
|
|
or (end_datetime > ' .
|
|
|
|
$start_date .
|
|
|
|
' AND end_datetime < ' .
|
|
|
|
$end_date .
|
|
|
|
')
|
|
|
|
or (start_datetime <= ' .
|
|
|
|
$start_date .
|
|
|
|
' AND end_datetime >= ' .
|
|
|
|
$end_date .
|
|
|
|
'))
|
2022-01-18 14:54:41 +03:00
|
|
|
AND is_unavailability = 1
|
2021-11-18 09:30:52 +03:00
|
|
|
';
|
|
|
|
|
2022-01-18 14:54:41 +03:00
|
|
|
$response['unavailabilities'] = $this->unavailabilities_model->get($where_clause);
|
2021-11-18 09:30:52 +03:00
|
|
|
}
|
|
|
|
|
2023-04-15 15:39:12 +03:00
|
|
|
$user_id = session('user_id');
|
|
|
|
|
|
|
|
$role_slug = session('role_slug');
|
|
|
|
|
|
|
|
// If the current user is a provider he must only see his own appointments.
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($role_slug === DB_SLUG_PROVIDER) {
|
|
|
|
foreach ($response['appointments'] as $index => $appointment) {
|
|
|
|
if ((int) $appointment['id_users_provider'] !== (int) $user_id) {
|
2023-04-15 15:39:12 +03:00
|
|
|
unset($response['appointments'][$index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$response['appointments'] = array_values($response['appointments']);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($response['unavailabilities'] as $index => $unavailability) {
|
|
|
|
if ((int) $unavailability['id_users_provider'] !== (int) $user_id) {
|
2023-04-15 15:39:12 +03:00
|
|
|
unset($response['unavailabilities'][$index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-26 17:23:58 +03:00
|
|
|
unset($unavailability);
|
|
|
|
|
2023-04-15 15:39:12 +03:00
|
|
|
$response['unavailabilities'] = array_values($response['unavailabilities']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the current user is a secretary he must only see the appointments of his providers.
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($role_slug === DB_SLUG_SECRETARY) {
|
2023-04-15 15:39:12 +03:00
|
|
|
$providers = $this->secretaries_model->find($user_id)['providers'];
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($response['appointments'] as $index => $appointment) {
|
|
|
|
if (!in_array((int) $appointment['id_users_provider'], $providers)) {
|
2023-04-15 15:39:12 +03:00
|
|
|
unset($response['appointments'][$index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$response['appointments'] = array_values($response['appointments']);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($response['unavailabilities'] as $index => $unavailability) {
|
|
|
|
if (!in_array((int) $unavailability['id_users_provider'], $providers)) {
|
2023-04-15 15:39:12 +03:00
|
|
|
unset($response['unavailabilities'][$index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$response['unavailabilities'] = array_values($response['unavailabilities']);
|
|
|
|
}
|
2023-11-29 12:24:09 +03:00
|
|
|
|
2024-05-12 15:24:58 +03:00
|
|
|
foreach ($response['unavailabilities'] as &$unavailability) {
|
|
|
|
$unavailability['provider'] = $this->providers_model->find($unavailability['id_users_provider']);
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($unavailability);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
// Add blocked periods to the response.
|
|
|
|
$start_date = request('start_date');
|
|
|
|
$end_date = request('end_date');
|
|
|
|
$response['blocked_periods'] = $this->blocked_periods_model->get_for_period($start_date, $end_date);
|
2023-04-15 15:39:12 +03:00
|
|
|
|
2021-11-18 09:30:52 +03:00
|
|
|
json_response($response);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2021-11-18 09:30:52 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|