2016-07-17 15:43:50 +03:00
|
|
|
/* ----------------------------------------------------------------------------
|
2022-01-18 15:05:42 +03:00
|
|
|
* Easy!Appointments - Online Appointment Scheduler
|
2016-07-17 15:43:50 +03:00
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2021-12-18 19:43:45 +03:00
|
|
|
* @copyright Copyright (c) Alex Tselegidis
|
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link https://easyappointments.org
|
2022-01-18 15:01:22 +03:00
|
|
|
* @since v1.5.0
|
2016-07-17 15:43:50 +03:00
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
2022-01-14 11:26:44 +03:00
|
|
|
* Unavailabilities modal component.
|
2016-07-17 15:43:50 +03:00
|
|
|
*
|
2022-01-14 11:26:44 +03:00
|
|
|
* This module implements the unavailabilities modal functionality.
|
2016-07-17 15:43:50 +03:00
|
|
|
*
|
2022-01-14 10:43:53 +03:00
|
|
|
* Old Name: BackendCalendarUnavailabilityEventsModal
|
2016-07-17 15:43:50 +03:00
|
|
|
*/
|
2022-01-14 10:43:53 +03:00
|
|
|
App.Components.UnavailabilitiesModal = (function () {
|
2022-01-17 06:46:18 +03:00
|
|
|
const $unavailabilitiesModal = $('#unavailabilities-modal');
|
2022-01-18 17:55:21 +03:00
|
|
|
const $id = $('#unavailability-id');
|
|
|
|
const $startDatetime = $('#unavailability-start');
|
|
|
|
const $endDatetime = $('#unavailability-end');
|
|
|
|
const $selectProvider = $('#unavailability-provider');
|
|
|
|
const $notes = $('#unavailability-notes');
|
2022-01-17 06:46:18 +03:00
|
|
|
const $saveUnavailability = $('#save-unavailability');
|
|
|
|
const $insertUnavailability = $('#insert-unavailability');
|
|
|
|
const $selectFilterItem = $('#select-filter-item');
|
2022-01-18 17:55:21 +03:00
|
|
|
const $reloadAppointments = $('#reload-appointments');
|
2022-01-17 06:46:18 +03:00
|
|
|
|
2022-05-24 11:47:56 +03:00
|
|
|
/**
|
|
|
|
* Update the displayed timezone.
|
|
|
|
*/
|
|
|
|
function updateTimezone() {
|
|
|
|
const providerId = $selectProvider.val();
|
|
|
|
|
|
|
|
const provider = vars('available_providers').find(
|
|
|
|
(availableProvider) => Number(availableProvider.id) === Number(providerId)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (provider && provider.timezone) {
|
|
|
|
$unavailabilitiesModal.find('.provider-timezone').text(
|
|
|
|
vars('timezones')[provider.timezone]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-14 11:26:44 +03:00
|
|
|
/**
|
2022-01-17 06:46:18 +03:00
|
|
|
* Add the component event listeners.
|
2022-01-14 11:26:44 +03:00
|
|
|
*/
|
2022-01-17 06:46:18 +03:00
|
|
|
function addEventListeners() {
|
2022-05-24 11:47:56 +03:00
|
|
|
/**
|
|
|
|
* Event: Provider "Change"
|
|
|
|
*/
|
|
|
|
$selectProvider.on('change', () => {
|
|
|
|
updateTimezone();
|
|
|
|
});
|
2023-02-02 10:54:47 +03:00
|
|
|
|
2016-07-17 15:43:50 +03:00
|
|
|
/**
|
2022-01-18 14:54:41 +03:00
|
|
|
* Event: Manage Unavailability Dialog Save Button "Click"
|
2016-07-17 15:43:50 +03:00
|
|
|
*
|
2022-01-18 14:54:41 +03:00
|
|
|
* Stores the unavailability period changes or inserts a new record.
|
2016-07-17 15:43:50 +03:00
|
|
|
*/
|
2022-01-17 06:46:18 +03:00
|
|
|
$saveUnavailability.on('click', () => {
|
|
|
|
$unavailabilitiesModal.find('.modal-message').addClass('d-none');
|
|
|
|
$unavailabilitiesModal.find('.is-invalid').removeClass('is-invalid');
|
2020-09-28 15:17:47 +03:00
|
|
|
|
2023-02-02 10:54:47 +03:00
|
|
|
const startMoment = moment($startDatetime[0]._flatpickr.selectedDates[0]);
|
2020-09-28 15:17:47 +03:00
|
|
|
|
2021-11-24 11:23:36 +03:00
|
|
|
if (!startMoment.isValid()) {
|
2022-01-18 17:55:21 +03:00
|
|
|
$startDatetime.addClass('is-invalid');
|
2020-09-28 15:17:47 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-02 10:54:47 +03:00
|
|
|
const endMoment = moment($endDatetime[0]._flatpickr.selectedDates[0]);
|
2016-07-17 15:43:50 +03:00
|
|
|
|
2021-11-24 11:23:36 +03:00
|
|
|
if (!endMoment.isValid()) {
|
2022-01-18 17:55:21 +03:00
|
|
|
$endDatetime.addClass('is-invalid');
|
2021-11-24 11:23:36 +03:00
|
|
|
|
2020-09-28 15:17:47 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-24 11:23:36 +03:00
|
|
|
if (startMoment.isAfter(endMoment)) {
|
2016-07-17 15:43:50 +03:00
|
|
|
// Start time is after end time - display message to user.
|
2022-01-17 06:46:18 +03:00
|
|
|
$unavailabilitiesModal
|
2021-11-06 19:38:37 +03:00
|
|
|
.find('.modal-message')
|
2022-01-18 10:22:25 +03:00
|
|
|
.text(lang('start_date_before_end_error'))
|
2016-07-17 15:43:50 +03:00
|
|
|
.addClass('alert-danger')
|
2020-06-18 21:37:11 +03:00
|
|
|
.removeClass('d-none');
|
2017-11-14 15:52:59 +03:00
|
|
|
|
2022-01-18 17:55:21 +03:00
|
|
|
$startDatetime.addClass('is-invalid');
|
2022-01-17 06:46:18 +03:00
|
|
|
|
2022-01-18 17:55:21 +03:00
|
|
|
$endDatetime.addClass('is-invalid');
|
2021-11-24 11:23:36 +03:00
|
|
|
|
2016-07-17 15:43:50 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-18 14:54:41 +03:00
|
|
|
// Unavailability period records go to the appointments table.
|
2022-01-17 06:46:18 +03:00
|
|
|
const unavailability = {
|
2021-11-24 11:23:36 +03:00
|
|
|
start_datetime: startMoment.format('YYYY-MM-DD HH:mm:ss'),
|
|
|
|
end_datetime: endMoment.format('YYYY-MM-DD HH:mm:ss'),
|
2022-01-17 06:46:18 +03:00
|
|
|
notes: $unavailabilitiesModal.find('#unavailability-notes').val(),
|
|
|
|
id_users_provider: $('#unavailability-provider').val()
|
2016-07-17 15:43:50 +03:00
|
|
|
};
|
|
|
|
|
2022-01-18 17:55:21 +03:00
|
|
|
if ($id.val() !== '') {
|
2016-07-17 15:43:50 +03:00
|
|
|
// Set the id value, only if we are editing an appointment.
|
2022-01-18 17:55:21 +03:00
|
|
|
unavailability.id = $id.val();
|
2016-07-17 15:43:50 +03:00
|
|
|
}
|
|
|
|
|
2022-01-12 13:22:54 +03:00
|
|
|
const successCallback = () => {
|
2016-07-17 15:43:50 +03:00
|
|
|
// Display success message to the user.
|
2022-01-18 14:54:41 +03:00
|
|
|
App.Layouts.Backend.displayNotification(lang('unavailability_saved'));
|
2016-07-17 15:43:50 +03:00
|
|
|
|
2020-05-03 23:40:29 +03:00
|
|
|
// Close the modal dialog and refresh the calendar appointments.
|
2022-01-17 06:46:18 +03:00
|
|
|
$unavailabilitiesModal.find('.alert').addClass('d-none');
|
2021-11-24 11:23:36 +03:00
|
|
|
|
2022-01-17 06:46:18 +03:00
|
|
|
$unavailabilitiesModal.modal('hide');
|
2021-11-24 11:23:36 +03:00
|
|
|
|
2022-01-18 19:38:25 +03:00
|
|
|
$reloadAppointments.trigger('click');
|
2016-07-17 15:43:50 +03:00
|
|
|
};
|
|
|
|
|
2022-01-18 14:54:41 +03:00
|
|
|
App.Http.Calendar.saveUnavailability(unavailability, successCallback, null);
|
2016-07-17 15:43:50 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2022-01-18 14:54:41 +03:00
|
|
|
* Event : Insert Unavailability Time Period Button "Click"
|
2016-07-17 15:43:50 +03:00
|
|
|
*
|
|
|
|
* When the user clicks this button a popup dialog appears and the use can set a time period where
|
|
|
|
* he cannot accept any appointments.
|
|
|
|
*/
|
2022-01-17 06:46:18 +03:00
|
|
|
$insertUnavailability.on('click', () => {
|
|
|
|
resetModal();
|
2022-01-12 13:22:54 +03:00
|
|
|
|
2022-01-14 10:43:53 +03:00
|
|
|
const $dialog = $('#unavailabilities-modal');
|
2016-07-17 15:43:50 +03:00
|
|
|
|
|
|
|
// Set the default datetime values.
|
2022-01-12 13:22:54 +03:00
|
|
|
const startMoment = moment();
|
2021-11-24 10:34:26 +03:00
|
|
|
|
2022-01-12 13:22:54 +03:00
|
|
|
const currentMin = parseInt(startMoment.format('mm'));
|
2016-07-17 15:43:50 +03:00
|
|
|
|
|
|
|
if (currentMin > 0 && currentMin < 15) {
|
2021-11-24 10:34:26 +03:00
|
|
|
startMoment.set({minutes: 15});
|
2016-07-17 15:43:50 +03:00
|
|
|
} else if (currentMin > 15 && currentMin < 30) {
|
2021-11-24 10:34:26 +03:00
|
|
|
startMoment.set({minutes: 30});
|
2016-07-17 15:43:50 +03:00
|
|
|
} else if (currentMin > 30 && currentMin < 45) {
|
2021-11-24 10:34:26 +03:00
|
|
|
startMoment.set({minutes: 45});
|
2016-07-17 15:43:50 +03:00
|
|
|
} else {
|
2021-11-24 10:34:26 +03:00
|
|
|
startMoment.add(1, 'hour').set({minutes: 0});
|
2016-07-17 15:43:50 +03:00
|
|
|
}
|
|
|
|
|
2016-07-18 23:55:05 +03:00
|
|
|
if ($('.calendar-view').length === 0) {
|
2022-01-18 17:55:21 +03:00
|
|
|
$selectProvider.val($selectFilterItem.val()).closest('.form-group').hide();
|
2016-07-18 23:55:05 +03:00
|
|
|
}
|
|
|
|
|
2023-02-02 10:54:47 +03:00
|
|
|
$startDatetime[0]._flatpickr.setDate(startMoment.toDate())
|
|
|
|
$endDatetime[0]._flatpickr.setDate(startMoment.add(1, 'hour').toDate());
|
|
|
|
|
2022-01-18 14:54:41 +03:00
|
|
|
$dialog.find('.modal-header h3').text(lang('new_unavailability_title'));
|
2016-07-17 15:43:50 +03:00
|
|
|
$dialog.modal('show');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-01-18 14:54:41 +03:00
|
|
|
* Reset unavailability dialog form.
|
2016-07-17 15:43:50 +03:00
|
|
|
*
|
2022-01-14 10:43:53 +03:00
|
|
|
* Reset the "#unavailabilities-modal" dialog. Use this method to bring the dialog to the initial state
|
2016-07-17 15:43:50 +03:00
|
|
|
* before it becomes visible to the user.
|
|
|
|
*/
|
2022-01-17 06:46:18 +03:00
|
|
|
function resetModal() {
|
2022-01-18 17:55:21 +03:00
|
|
|
$id.val('');
|
2016-07-17 15:43:50 +03:00
|
|
|
|
|
|
|
// Set default time values
|
2022-01-18 10:18:22 +03:00
|
|
|
const start = App.Utils.Date.format(moment().toDate(), vars('date_format'), vars('time_format'), true);
|
2021-11-24 10:34:26 +03:00
|
|
|
|
2022-01-12 13:22:54 +03:00
|
|
|
const end = App.Utils.Date.format(
|
|
|
|
moment().add(1, 'hour').toDate(),
|
2022-01-18 10:18:22 +03:00
|
|
|
vars('date_format'),
|
|
|
|
vars('time_format'),
|
2022-01-12 13:22:54 +03:00
|
|
|
true
|
|
|
|
);
|
2021-11-24 10:34:26 +03:00
|
|
|
|
2023-02-02 10:54:47 +03:00
|
|
|
App.Utils.UI.initializeDatetimepicker($startDatetime);
|
|
|
|
|
2022-01-18 17:55:21 +03:00
|
|
|
$startDatetime.val(start);
|
2016-07-17 15:43:50 +03:00
|
|
|
|
2022-05-26 16:17:33 +03:00
|
|
|
App.Utils.UI.initializeDatetimepicker($endDatetime);
|
|
|
|
|
2022-01-18 17:55:21 +03:00
|
|
|
$endDatetime.val(end);
|
2016-07-17 15:43:50 +03:00
|
|
|
|
2022-01-18 14:54:41 +03:00
|
|
|
// Clear the unavailability notes field.
|
2022-01-18 17:55:21 +03:00
|
|
|
$notes.val('');
|
2022-01-12 13:22:54 +03:00
|
|
|
}
|
2016-07-17 15:43:50 +03:00
|
|
|
|
2022-01-14 11:26:44 +03:00
|
|
|
/**
|
|
|
|
* Initialize the module.
|
|
|
|
*/
|
2022-01-12 13:22:54 +03:00
|
|
|
function initialize() {
|
2022-01-18 10:18:22 +03:00
|
|
|
for (const index in vars('available_providers')) {
|
|
|
|
const provider = vars('available_providers')[index];
|
2016-07-18 23:55:05 +03:00
|
|
|
|
2022-01-18 17:55:21 +03:00
|
|
|
$selectProvider.append(new Option(provider.first_name + ' ' + provider.last_name, provider.id));
|
2016-07-18 23:55:05 +03:00
|
|
|
}
|
|
|
|
|
2022-01-17 06:46:18 +03:00
|
|
|
addEventListeners();
|
2022-01-12 13:22:54 +03:00
|
|
|
}
|
|
|
|
|
2022-01-17 06:46:18 +03:00
|
|
|
document.addEventListener('DOMContentLoaded', initialize);
|
|
|
|
|
2022-01-12 13:22:54 +03:00
|
|
|
return {
|
2022-10-16 22:24:50 +03:00
|
|
|
resetModal
|
2016-07-17 15:43:50 +03:00
|
|
|
};
|
2022-01-12 13:22:54 +03:00
|
|
|
})();
|