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
|
|
|
* Appointments modal component.
|
2016-07-17 15:43:50 +03:00
|
|
|
*
|
|
|
|
* This module implements the appointments modal functionality.
|
|
|
|
*
|
2022-01-14 10:39:11 +03:00
|
|
|
* Old Name: BackendCalendarAppointmentsModal
|
2016-07-17 15:43:50 +03:00
|
|
|
*/
|
2022-01-14 10:39:11 +03:00
|
|
|
App.Components.AppointmentsModal = (function () {
|
2022-01-17 06:33:01 +03:00
|
|
|
const $appointmentsModal = $('#appointments-modal');
|
|
|
|
const $startDatetime = $('#start-datetime');
|
|
|
|
const $endDatetime = $('#end-datetime');
|
|
|
|
const $filterExistingCustomers = $('#filter-existing-customers');
|
|
|
|
const $customerId = $('#customer-id');
|
|
|
|
const $firstName = $('#first-name');
|
|
|
|
const $lastName = $('#last-name');
|
|
|
|
const $email = $('#email');
|
|
|
|
const $phoneNumber = $('#phone-number');
|
|
|
|
const $address = $('#address');
|
|
|
|
const $city = $('#city');
|
|
|
|
const $zipCode = $('#zip-code');
|
2022-03-29 11:15:24 +03:00
|
|
|
const $language = $('#language');
|
|
|
|
const $timezone = $('#timezone');
|
2022-01-17 06:33:01 +03:00
|
|
|
const $customerNotes = $('#customer-notes');
|
|
|
|
const $selectCustomer = $('#select-customer');
|
|
|
|
const $saveAppointment = $('#save-appointment');
|
|
|
|
const $appointmentId = $('#appointment-id');
|
|
|
|
const $appointmentLocation = $('#appointment-location');
|
2022-10-16 21:54:26 +03:00
|
|
|
const $appointmentStatus = $('#appointment-status');
|
2022-01-18 21:01:26 +03:00
|
|
|
const $appointmentColor = $('#appointment-color');
|
2022-01-17 06:33:01 +03:00
|
|
|
const $appointmentNotes = $('#appointment-notes');
|
2022-01-18 17:55:21 +03:00
|
|
|
const $reloadAppointments = $('#reload-appointments');
|
2022-01-17 06:33:01 +03:00
|
|
|
const $selectFilterItem = $('#select-filter-item');
|
|
|
|
const $selectService = $('#select-service');
|
|
|
|
const $selectProvider = $('#select-provider');
|
|
|
|
const $insertAppointment = $('#insert-appointment');
|
|
|
|
const $existingCustomersList = $('#existing-customers-list');
|
|
|
|
const $newCustomer = $('#new-customer');
|
2023-12-11 11:54:29 +03:00
|
|
|
const $customField1 = $('#custom-field-1');
|
|
|
|
const $customField2 = $('#custom-field-2');
|
|
|
|
const $customField3 = $('#custom-field-3');
|
|
|
|
const $customField4 = $('#custom-field-4');
|
|
|
|
const $customField5 = $('#custom-field-5');
|
2022-01-17 06:33:01 +03:00
|
|
|
|
2024-01-06 13:06:48 +03:00
|
|
|
const moment = window.moment;
|
|
|
|
|
2022-01-14 11:26:44 +03:00
|
|
|
/**
|
|
|
|
* Update the displayed timezone.
|
|
|
|
*/
|
2020-05-06 20:23:49 +03:00
|
|
|
function updateTimezone() {
|
2022-01-17 06:33:01 +03:00
|
|
|
const providerId = $selectProvider.val();
|
2020-03-29 16:08:07 +03:00
|
|
|
|
2022-01-18 13:14:45 +03:00
|
|
|
const provider = vars('available_providers').find(
|
2023-12-22 13:35:41 +03:00
|
|
|
(availableProvider) => Number(availableProvider.id) === Number(providerId),
|
2022-01-18 13:14:45 +03:00
|
|
|
);
|
2020-03-29 16:08:07 +03:00
|
|
|
|
|
|
|
if (provider && provider.timezone) {
|
2022-01-18 10:18:22 +03:00
|
|
|
$('.provider-timezone').text(vars('timezones')[provider.timezone]);
|
2020-03-29 16:08:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-14 11:26:44 +03:00
|
|
|
/**
|
2022-01-17 06:33:01 +03:00
|
|
|
* Add the component event listeners.
|
2022-01-14 11:26:44 +03:00
|
|
|
*/
|
2022-01-17 06:33:01 +03:00
|
|
|
function addEventListeners() {
|
2016-07-17 15:43:50 +03:00
|
|
|
/**
|
|
|
|
* Event: Manage Appointments Dialog Save Button "Click"
|
|
|
|
*
|
2022-01-12 13:22:54 +03:00
|
|
|
* Stores the appointment changes or inserts a new appointment depending on the dialog mode.
|
2016-07-17 15:43:50 +03:00
|
|
|
*/
|
2022-01-17 06:33:01 +03:00
|
|
|
$saveAppointment.on('click', () => {
|
2016-07-17 15:43:50 +03:00
|
|
|
// Before doing anything the appointment data need to be validated.
|
2024-08-13 14:36:03 +03:00
|
|
|
if (!App.Components.AppointmentsModal.validateAppointmentForm()) {
|
2016-07-17 15:43:50 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID must exist on the object in order for the model to update the record and not to perform
|
|
|
|
// an insert operation.
|
|
|
|
|
2024-01-06 13:06:48 +03:00
|
|
|
const startDateTimeObject = App.Utils.UI.getDateTimePickerValue($startDatetime);
|
|
|
|
const startDatetime = moment(startDateTimeObject).format('YYYY-MM-DD HH:mm:ss');
|
|
|
|
|
|
|
|
const endDateTimeObject = App.Utils.UI.getDateTimePickerValue($endDatetime);
|
|
|
|
const endDatetime = moment(endDateTimeObject).format('YYYY-MM-DD HH:mm:ss');
|
2016-07-17 15:43:50 +03:00
|
|
|
|
2022-01-12 13:22:54 +03:00
|
|
|
const appointment = {
|
2022-01-17 06:33:01 +03:00
|
|
|
id_services: $selectService.val(),
|
|
|
|
id_users_provider: $selectProvider.val(),
|
2016-07-17 15:43:50 +03:00
|
|
|
start_datetime: startDatetime,
|
|
|
|
end_datetime: endDatetime,
|
2022-01-17 06:33:01 +03:00
|
|
|
location: $appointmentLocation.val(),
|
2022-01-18 21:01:26 +03:00
|
|
|
color: App.Components.ColorSelection.getColor($appointmentColor),
|
2022-10-16 21:54:26 +03:00
|
|
|
status: $appointmentStatus.val(),
|
2022-01-17 06:33:01 +03:00
|
|
|
notes: $appointmentNotes.val(),
|
2023-12-22 13:35:41 +03:00
|
|
|
is_unavailability: Number(false),
|
2016-07-17 15:43:50 +03:00
|
|
|
};
|
|
|
|
|
2022-01-17 06:33:01 +03:00
|
|
|
if ($appointmentId.val() !== '') {
|
2016-07-17 15:43:50 +03:00
|
|
|
// Set the id value, only if we are editing an appointment.
|
2022-01-17 06:33:01 +03:00
|
|
|
appointment.id = $appointmentId.val();
|
2016-07-17 15:43:50 +03:00
|
|
|
}
|
|
|
|
|
2022-01-12 13:22:54 +03:00
|
|
|
const customer = {
|
2022-01-17 06:33:01 +03:00
|
|
|
first_name: $firstName.val(),
|
|
|
|
last_name: $lastName.val(),
|
|
|
|
email: $email.val(),
|
|
|
|
phone_number: $phoneNumber.val(),
|
|
|
|
address: $address.val(),
|
|
|
|
city: $city.val(),
|
|
|
|
zip_code: $zipCode.val(),
|
2022-03-29 11:15:24 +03:00
|
|
|
language: $language.val(),
|
|
|
|
timezone: $timezone.val(),
|
2023-12-11 11:54:29 +03:00
|
|
|
notes: $customerNotes.val(),
|
|
|
|
custom_field_1: $customField1.val(),
|
|
|
|
custom_field_2: $customField2.val(),
|
|
|
|
custom_field_3: $customField3.val(),
|
|
|
|
custom_field_4: $customField4.val(),
|
2023-12-22 13:35:41 +03:00
|
|
|
custom_field_5: $customField5.val(),
|
2016-07-17 15:43:50 +03:00
|
|
|
};
|
|
|
|
|
2022-01-17 06:33:01 +03:00
|
|
|
if ($customerId.val() !== '') {
|
2016-07-17 15:43:50 +03:00
|
|
|
// Set the id value, only if we are editing an appointment.
|
2022-01-17 06:33:01 +03:00
|
|
|
customer.id = $customerId.val();
|
2017-11-02 16:19:54 +03:00
|
|
|
appointment.id_users_customer = customer.id;
|
2016-07-17 15:43:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Define success callback.
|
2022-01-17 06:33:01 +03:00
|
|
|
const successCallback = () => {
|
2016-07-17 15:43:50 +03:00
|
|
|
// Display success message to the user.
|
2022-01-18 10:22:25 +03:00
|
|
|
App.Layouts.Backend.displayNotification(lang('appointment_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 23:20:06 +03:00
|
|
|
$appointmentsModal.find('.alert').addClass('d-none');
|
|
|
|
$appointmentsModal.modal('hide');
|
2022-01-18 17:55:21 +03:00
|
|
|
$reloadAppointments.trigger('click');
|
2016-07-17 15:43:50 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// Define error callback.
|
2022-01-17 06:33:01 +03:00
|
|
|
const errorCallback = () => {
|
2022-01-18 10:22:25 +03:00
|
|
|
$appointmentsModal.find('.modal-message').text(lang('service_communication_error'));
|
2022-01-17 06:33:01 +03:00
|
|
|
$appointmentsModal.find('.modal-message').addClass('alert-danger').removeClass('d-none');
|
|
|
|
$appointmentsModal.find('.modal-body').scrollTop(0);
|
2016-07-17 15:43:50 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// Save appointment data.
|
2022-01-12 13:22:54 +03:00
|
|
|
App.Http.Calendar.saveAppointment(appointment, customer, successCallback, errorCallback);
|
2016-07-17 15:43:50 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Insert Appointment Button "Click"
|
|
|
|
*
|
2022-01-12 13:22:54 +03:00
|
|
|
* When the user presses this button, the manage appointment dialog opens and lets the user create a new
|
|
|
|
* appointment.
|
2016-07-17 15:43:50 +03:00
|
|
|
*/
|
2022-01-17 06:33:01 +03:00
|
|
|
$insertAppointment.on('click', () => {
|
2020-08-15 16:33:26 +03:00
|
|
|
$('.popover').remove();
|
|
|
|
|
2024-08-13 14:36:03 +03:00
|
|
|
App.Components.AppointmentsModal.resetModal();
|
2016-07-17 15:43:50 +03:00
|
|
|
|
|
|
|
// Set the selected filter item and find the next appointment time as the default modal values.
|
2022-01-17 23:20:06 +03:00
|
|
|
if ($selectFilterItem.find('option:selected').attr('type') === 'provider') {
|
2022-01-12 13:22:54 +03:00
|
|
|
const providerId = $('#select-filter-item').val();
|
2018-06-24 20:42:42 +03:00
|
|
|
|
2022-01-18 10:18:22 +03:00
|
|
|
const providers = vars('available_providers').filter(
|
2023-12-22 13:35:41 +03:00
|
|
|
(provider) => Number(provider.id) === Number(providerId),
|
2022-01-12 13:22:54 +03:00
|
|
|
);
|
2018-06-24 20:42:42 +03:00
|
|
|
|
|
|
|
if (providers.length) {
|
2022-01-17 06:33:01 +03:00
|
|
|
$selectService.val(providers[0].services[0]).trigger('change');
|
|
|
|
$selectProvider.val(providerId);
|
2016-07-17 15:43:50 +03:00
|
|
|
}
|
2022-01-17 06:33:01 +03:00
|
|
|
} else if ($selectFilterItem.find('option:selected').attr('type') === 'service') {
|
|
|
|
$selectService.find('option[value="' + $selectFilterItem.val() + '"]').prop('selected', true);
|
2016-07-17 15:43:50 +03:00
|
|
|
} else {
|
2022-01-17 06:33:01 +03:00
|
|
|
$selectService.find('option:first').prop('selected', true).trigger('change');
|
2016-07-17 15:43:50 +03:00
|
|
|
}
|
2022-05-26 16:17:33 +03:00
|
|
|
|
2022-05-24 11:47:56 +03:00
|
|
|
$selectProvider.trigger('change');
|
2016-07-17 15:43:50 +03:00
|
|
|
|
2022-01-17 06:33:01 +03:00
|
|
|
const serviceId = $selectService.val();
|
2020-05-07 20:00:33 +03:00
|
|
|
|
2022-01-18 10:18:22 +03:00
|
|
|
const service = vars('available_services').find(
|
2023-12-22 13:35:41 +03:00
|
|
|
(availableService) => Number(availableService.id) === Number(serviceId),
|
2022-01-12 13:22:54 +03:00
|
|
|
);
|
2016-07-17 15:43:50 +03:00
|
|
|
|
2022-01-12 13:22:54 +03:00
|
|
|
const duration = service ? service.duration : 60;
|
2020-12-08 10:57:22 +03:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-01-06 13:06:48 +03:00
|
|
|
App.Utils.UI.setDateTimePickerValue($startDatetime, startMoment.toDate());
|
|
|
|
App.Utils.UI.setDateTimePickerValue($endDatetime, startMoment.add(duration, 'minutes').toDate());
|
2023-12-11 11:54:29 +03:00
|
|
|
|
2016-07-17 15:43:50 +03:00
|
|
|
// Display modal form.
|
2022-01-18 10:22:25 +03:00
|
|
|
$appointmentsModal.find('.modal-header h3').text(lang('new_appointment_title'));
|
2021-11-24 10:34:26 +03:00
|
|
|
|
2022-01-17 06:33:01 +03:00
|
|
|
$appointmentsModal.modal('show');
|
2016-07-17 15:43:50 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Pick Existing Customer Button "Click"
|
2022-01-12 13:22:54 +03:00
|
|
|
*
|
2022-01-18 13:14:45 +03:00
|
|
|
* @param {jQuery.Event} event
|
2016-07-17 15:43:50 +03:00
|
|
|
*/
|
2022-01-17 06:33:01 +03:00
|
|
|
$selectCustomer.on('click', (event) => {
|
|
|
|
if (!$existingCustomersList.is(':visible')) {
|
2022-01-18 10:22:25 +03:00
|
|
|
$(event.target).find('span').text(lang('hide'));
|
2022-01-17 06:33:01 +03:00
|
|
|
$existingCustomersList.empty();
|
|
|
|
$existingCustomersList.slideDown('slow');
|
|
|
|
$filterExistingCustomers.fadeIn('slow').val('');
|
2022-01-18 13:14:45 +03:00
|
|
|
vars('customers').forEach((customer) => {
|
2020-05-07 19:47:14 +03:00
|
|
|
$('<div/>', {
|
|
|
|
'data-id': customer.id,
|
2023-12-11 11:54:29 +03:00
|
|
|
'text':
|
2023-12-22 13:35:41 +03:00
|
|
|
(customer.first_name || '[No First Name]') + ' ' + (customer.last_name || '[No Last Name]'),
|
2022-01-17 06:33:01 +03:00
|
|
|
}).appendTo($existingCustomersList);
|
2016-07-17 15:43:50 +03:00
|
|
|
});
|
|
|
|
} else {
|
2022-01-17 06:33:01 +03:00
|
|
|
$existingCustomersList.slideUp('slow');
|
|
|
|
$filterExistingCustomers.fadeOut('slow');
|
2022-01-18 10:22:25 +03:00
|
|
|
$(event.target).find('span').text(lang('select'));
|
2016-07-17 15:43:50 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Select Existing Customer From List "Click"
|
2022-01-14 11:26:44 +03:00
|
|
|
*
|
|
|
|
* @param {jQuery.Event}
|
2016-07-17 15:43:50 +03:00
|
|
|
*/
|
2022-01-17 06:33:01 +03:00
|
|
|
$appointmentsModal.on('click', '#existing-customers-list div', (event) => {
|
2022-01-12 13:22:54 +03:00
|
|
|
const customerId = $(event.target).attr('data-id');
|
2020-05-07 20:00:33 +03:00
|
|
|
|
2022-01-18 13:14:45 +03:00
|
|
|
const customer = vars('customers').find((customer) => Number(customer.id) === Number(customerId));
|
2016-07-17 15:43:50 +03:00
|
|
|
|
2020-05-07 20:00:33 +03:00
|
|
|
if (customer) {
|
2022-01-17 06:33:01 +03:00
|
|
|
$customerId.val(customer.id);
|
|
|
|
$firstName.val(customer.first_name);
|
|
|
|
$lastName.val(customer.last_name);
|
|
|
|
$email.val(customer.email);
|
|
|
|
$phoneNumber.val(customer.phone_number);
|
|
|
|
$address.val(customer.address);
|
|
|
|
$city.val(customer.city);
|
|
|
|
$zipCode.val(customer.zip_code);
|
2022-03-29 11:15:24 +03:00
|
|
|
$language.val(customer.language);
|
|
|
|
$timezone.val(customer.timezone);
|
2022-01-17 06:33:01 +03:00
|
|
|
$customerNotes.val(customer.notes);
|
2023-12-11 11:54:29 +03:00
|
|
|
$customField1.val(customer.custom_field_1);
|
|
|
|
$customField2.val(customer.custom_field_2);
|
|
|
|
$customField3.val(customer.custom_field_3);
|
|
|
|
$customField4.val(customer.custom_field_4);
|
|
|
|
$customField5.val(customer.custom_field_5);
|
2020-05-07 20:00:33 +03:00
|
|
|
}
|
|
|
|
|
2022-01-17 06:33:01 +03:00
|
|
|
$selectCustomer.trigger('click'); // Hide the list.
|
2016-07-17 15:43:50 +03:00
|
|
|
});
|
|
|
|
|
2022-01-12 13:22:54 +03:00
|
|
|
let filterExistingCustomersTimeout = null;
|
2020-12-09 17:00:15 +03:00
|
|
|
|
2016-07-17 15:43:50 +03:00
|
|
|
/**
|
|
|
|
* Event: Filter Existing Customers "Change"
|
2022-01-14 11:26:44 +03:00
|
|
|
*
|
|
|
|
* @param {jQuery.Event}
|
2016-07-17 15:43:50 +03:00
|
|
|
*/
|
2022-01-17 06:33:01 +03:00
|
|
|
$filterExistingCustomers.on('keyup', (event) => {
|
2020-12-09 17:00:15 +03:00
|
|
|
if (filterExistingCustomersTimeout) {
|
|
|
|
clearTimeout(filterExistingCustomersTimeout);
|
|
|
|
}
|
|
|
|
|
2022-01-12 13:22:54 +03:00
|
|
|
const keyword = $(event.target).val().toLowerCase();
|
2020-05-06 20:15:11 +03:00
|
|
|
|
2022-01-18 13:14:45 +03:00
|
|
|
filterExistingCustomersTimeout = setTimeout(() => {
|
2020-12-09 17:00:15 +03:00
|
|
|
$('#loading').css('visibility', 'hidden');
|
2016-07-17 15:43:50 +03:00
|
|
|
|
2022-01-12 13:22:54 +03:00
|
|
|
App.Http.Customers.search(keyword, 50)
|
|
|
|
.done((response) => {
|
2022-01-17 06:33:01 +03:00
|
|
|
$existingCustomersList.empty();
|
2016-07-17 15:43:50 +03:00
|
|
|
|
2022-01-12 13:22:54 +03:00
|
|
|
response.forEach((customer) => {
|
2020-05-07 19:47:14 +03:00
|
|
|
$('<div/>', {
|
|
|
|
'data-id': customer.id,
|
2023-12-11 11:54:29 +03:00
|
|
|
'text':
|
|
|
|
(customer.first_name || '[No First Name]') +
|
|
|
|
' ' +
|
2023-12-22 13:35:41 +03:00
|
|
|
(customer.last_name || '[No Last Name]'),
|
2022-01-17 06:33:01 +03:00
|
|
|
}).appendTo($existingCustomersList);
|
2020-12-09 17:00:15 +03:00
|
|
|
|
|
|
|
// Verify if this customer is on the old customer list.
|
2022-01-18 10:18:22 +03:00
|
|
|
const result = vars('customers').filter((existingCustomer) => {
|
2022-01-12 13:22:54 +03:00
|
|
|
return Number(existingCustomer.id) === Number(customer.id);
|
2020-12-09 17:00:15 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// Add it to the customer list.
|
|
|
|
if (!result.length) {
|
2022-01-18 10:18:22 +03:00
|
|
|
vars('customers').push(customer);
|
2020-12-09 17:00:15 +03:00
|
|
|
}
|
2021-11-06 19:38:37 +03:00
|
|
|
});
|
2020-12-09 17:00:15 +03:00
|
|
|
})
|
2022-01-12 13:22:54 +03:00
|
|
|
.fail(() => {
|
2020-12-09 17:00:15 +03:00
|
|
|
// If there is any error on the request, search by the local client database.
|
2022-01-17 06:33:01 +03:00
|
|
|
$existingCustomersList.empty();
|
2020-12-09 17:00:15 +03:00
|
|
|
|
2022-01-18 10:18:22 +03:00
|
|
|
vars('customers').forEach((customer) => {
|
2021-11-06 19:38:37 +03:00
|
|
|
if (
|
2022-01-12 13:22:54 +03:00
|
|
|
customer.first_name.toLowerCase().indexOf(keyword) !== -1 ||
|
|
|
|
customer.last_name.toLowerCase().indexOf(keyword) !== -1 ||
|
|
|
|
customer.email.toLowerCase().indexOf(keyword) !== -1 ||
|
|
|
|
customer.phone_number.toLowerCase().indexOf(keyword) !== -1 ||
|
|
|
|
customer.address.toLowerCase().indexOf(keyword) !== -1 ||
|
|
|
|
customer.city.toLowerCase().indexOf(keyword) !== -1 ||
|
|
|
|
customer.zip_code.toLowerCase().indexOf(keyword) !== -1 ||
|
|
|
|
customer.notes.toLowerCase().indexOf(keyword) !== -1
|
2021-11-06 19:38:37 +03:00
|
|
|
) {
|
2020-12-09 17:00:15 +03:00
|
|
|
$('<div/>', {
|
|
|
|
'data-id': customer.id,
|
2023-12-11 11:54:29 +03:00
|
|
|
'text':
|
|
|
|
(customer.first_name || '[No First Name]') +
|
|
|
|
' ' +
|
2023-12-22 13:35:41 +03:00
|
|
|
(customer.last_name || '[No Last Name]'),
|
2022-01-17 06:33:01 +03:00
|
|
|
}).appendTo($existingCustomersList);
|
2020-12-09 17:00:15 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
2022-01-18 13:14:45 +03:00
|
|
|
.always(() => {
|
2020-12-09 17:00:15 +03:00
|
|
|
$('#loading').css('visibility', '');
|
2016-07-17 15:43:50 +03:00
|
|
|
});
|
2020-12-09 17:00:15 +03:00
|
|
|
}, 1000);
|
2016-07-17 15:43:50 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Selected Service "Change"
|
|
|
|
*
|
2022-01-17 06:33:01 +03:00
|
|
|
* When the user clicks on a service, its available providers should become visible. We also need to
|
2016-07-17 15:43:50 +03:00
|
|
|
* update the start and end time of the appointment.
|
|
|
|
*/
|
2022-01-17 06:33:01 +03:00
|
|
|
$selectService.on('change', () => {
|
|
|
|
const serviceId = $selectService.val();
|
2023-12-11 11:54:29 +03:00
|
|
|
|
|
|
|
const providerId = $selectProvider.val();
|
2020-05-12 21:52:32 +03:00
|
|
|
|
2022-01-17 06:33:01 +03:00
|
|
|
$selectProvider.empty();
|
2016-07-17 15:43:50 +03:00
|
|
|
|
|
|
|
// Automatically update the service duration.
|
2022-01-18 10:18:22 +03:00
|
|
|
const service = vars('available_services').find((availableService) => {
|
2020-05-12 21:52:32 +03:00
|
|
|
return Number(availableService.id) === Number(serviceId);
|
2016-07-17 15:43:50 +03:00
|
|
|
});
|
|
|
|
|
2024-03-18 20:31:43 +03:00
|
|
|
if (service?.color) {
|
2024-12-19 22:06:30 +03:00
|
|
|
App.Components.ColorSelection.setColor($appointmentColor, service.color);
|
2024-03-18 20:31:43 +03:00
|
|
|
}
|
|
|
|
|
2022-01-12 13:22:54 +03:00
|
|
|
const duration = service ? service.duration : 60;
|
2020-12-08 10:57:22 +03:00
|
|
|
|
2024-01-06 13:06:48 +03:00
|
|
|
const startDateTimeObject = App.Utils.UI.getDateTimePickerValue($startDatetime);
|
|
|
|
const endDateTimeObject = new Date(startDateTimeObject.getTime() + duration * 60000);
|
|
|
|
App.Utils.UI.setDateTimePickerValue($endDatetime, endDateTimeObject);
|
2020-05-12 21:52:32 +03:00
|
|
|
|
2016-07-17 15:43:50 +03:00
|
|
|
// Update the providers select box.
|
2020-05-12 21:52:32 +03:00
|
|
|
|
2022-01-18 10:18:22 +03:00
|
|
|
vars('available_providers').forEach((provider) => {
|
2022-01-12 13:22:54 +03:00
|
|
|
provider.services.forEach((providerServiceId) => {
|
2022-01-17 07:26:57 +03:00
|
|
|
if (
|
2022-01-18 10:18:22 +03:00
|
|
|
vars('role_slug') === App.Layouts.Backend.DB_SLUG_PROVIDER &&
|
|
|
|
Number(provider.id) !== vars('user_id')
|
2022-01-17 07:26:57 +03:00
|
|
|
) {
|
2020-05-12 21:52:32 +03:00
|
|
|
return; // continue
|
2018-07-29 17:08:41 +03:00
|
|
|
}
|
|
|
|
|
2021-11-06 19:38:37 +03:00
|
|
|
if (
|
2022-01-18 10:18:22 +03:00
|
|
|
vars('role_slug') === App.Layouts.Backend.DB_SLUG_SECRETARY &&
|
2022-04-22 16:15:42 +03:00
|
|
|
vars('secretary_providers').indexOf(Number(provider.id)) === -1
|
2021-11-06 19:38:37 +03:00
|
|
|
) {
|
2020-05-12 21:52:32 +03:00
|
|
|
return; // continue
|
2018-07-29 17:08:41 +03:00
|
|
|
}
|
|
|
|
|
2022-01-17 06:33:01 +03:00
|
|
|
// If the current provider is able to provide the selected service, add him to the list box.
|
2020-05-06 20:15:11 +03:00
|
|
|
if (Number(providerServiceId) === Number(serviceId)) {
|
2022-01-17 06:33:01 +03:00
|
|
|
$selectProvider.append(new Option(provider.first_name + ' ' + provider.last_name, provider.id));
|
2016-07-17 15:43:50 +03:00
|
|
|
}
|
|
|
|
});
|
2023-12-11 11:54:29 +03:00
|
|
|
|
2023-06-20 19:02:32 +03:00
|
|
|
if ($selectProvider.find(`option[value="${providerId}"]`).length) {
|
|
|
|
$selectProvider.val(providerId);
|
|
|
|
}
|
2016-07-17 15:43:50 +03:00
|
|
|
});
|
|
|
|
});
|
2020-03-29 16:08:07 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Provider "Change"
|
|
|
|
*/
|
2022-01-17 06:33:01 +03:00
|
|
|
$selectProvider.on('change', () => {
|
2020-05-06 20:23:49 +03:00
|
|
|
updateTimezone();
|
2020-03-29 16:08:07 +03:00
|
|
|
});
|
|
|
|
|
2016-07-17 15:43:50 +03:00
|
|
|
/**
|
|
|
|
* Event: Enter New Customer Button "Click"
|
|
|
|
*/
|
2022-01-17 06:33:01 +03:00
|
|
|
$newCustomer.on('click', () => {
|
2022-03-29 11:15:24 +03:00
|
|
|
$customerId.val('');
|
|
|
|
$firstName.val('');
|
|
|
|
$lastName.val('');
|
|
|
|
$email.val('');
|
|
|
|
$phoneNumber.val('');
|
|
|
|
$address.val('');
|
|
|
|
$city.val('');
|
|
|
|
$zipCode.val('');
|
2024-03-27 18:09:15 +03:00
|
|
|
$language.val(vars('default_language'));
|
|
|
|
$timezone.val(vars('default_timezone'));
|
2022-03-29 11:15:24 +03:00
|
|
|
$customerNotes.val('');
|
2023-12-11 11:54:29 +03:00
|
|
|
$customField1.val('');
|
|
|
|
$customField2.val('');
|
|
|
|
$customField3.val('');
|
|
|
|
$customField4.val('');
|
|
|
|
$customField5.val('');
|
2016-07-17 15:43:50 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-10-10 19:29:48 +03:00
|
|
|
* Reset Appointment Dialog
|
2016-07-17 15:43:50 +03:00
|
|
|
*
|
|
|
|
* This method resets the manage appointment dialog modal to its initial state. After that you can make
|
|
|
|
* any modification might be necessary in order to bring the dialog to the desired state.
|
|
|
|
*/
|
2022-01-17 06:33:01 +03:00
|
|
|
function resetModal() {
|
2016-07-17 15:43:50 +03:00
|
|
|
// Empty form fields.
|
2022-01-17 06:33:01 +03:00
|
|
|
$appointmentsModal.find('input, textarea').val('');
|
2024-05-30 00:15:07 +03:00
|
|
|
$appointmentsModal.find('.modal-message').addClass('.d-none');
|
2022-05-26 16:17:33 +03:00
|
|
|
|
2023-06-07 09:38:27 +03:00
|
|
|
const defaultStatusValue = $appointmentStatus.find('option:first').val();
|
|
|
|
$appointmentStatus.val(defaultStatusValue);
|
2022-10-16 21:54:26 +03:00
|
|
|
|
2024-03-27 18:09:15 +03:00
|
|
|
$language.val(vars('default_language'));
|
2024-05-13 11:49:14 +03:00
|
|
|
$timezone.val(vars('default_timezone'));
|
2016-07-17 15:43:50 +03:00
|
|
|
|
2022-01-18 21:01:26 +03:00
|
|
|
// Reset color.
|
|
|
|
$appointmentColor.find('.color-selection-option:first').trigger('click');
|
|
|
|
|
2016-07-17 15:43:50 +03:00
|
|
|
// Prepare service and provider select boxes.
|
2022-01-17 06:33:01 +03:00
|
|
|
$selectService.val($selectService.eq(0).attr('value'));
|
2016-07-17 15:43:50 +03:00
|
|
|
|
2022-01-12 13:22:54 +03:00
|
|
|
// Fill the providers list box with providers that can serve the appointment's service and then select the
|
|
|
|
// user's provider.
|
2022-01-17 06:33:01 +03:00
|
|
|
$selectProvider.empty();
|
2022-01-18 10:18:22 +03:00
|
|
|
vars('available_providers').forEach((provider) => {
|
2022-01-17 06:33:01 +03:00
|
|
|
const serviceId = $selectService.val();
|
2020-05-06 20:15:11 +03:00
|
|
|
|
2022-01-12 13:22:54 +03:00
|
|
|
const canProvideService =
|
|
|
|
provider.services.filter((providerServiceId) => {
|
2021-11-06 19:38:37 +03:00
|
|
|
return Number(providerServiceId) === Number(serviceId);
|
|
|
|
}).length > 0;
|
2016-07-17 15:43:50 +03:00
|
|
|
|
2021-11-06 19:38:37 +03:00
|
|
|
if (canProvideService) {
|
2022-01-12 13:22:54 +03:00
|
|
|
// Add the provider to the list box.
|
2022-01-17 06:33:01 +03:00
|
|
|
$selectProvider.append(new Option(provider.first_name + ' ' + provider.last_name, provider.id));
|
2016-07-17 15:43:50 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Close existing customers-filter frame.
|
2022-01-17 06:33:01 +03:00
|
|
|
$existingCustomersList.slideUp('slow');
|
|
|
|
$filterExistingCustomers.fadeOut('slow');
|
2022-01-18 10:22:25 +03:00
|
|
|
$selectCustomer.find('span').text(lang('select'));
|
2016-07-17 15:43:50 +03:00
|
|
|
|
|
|
|
// Setup start and datetimepickers.
|
|
|
|
// Get the selected service duration. It will be needed in order to calculate the appointment end datetime.
|
2022-01-17 06:33:01 +03:00
|
|
|
const serviceId = $selectService.val();
|
2020-05-12 21:52:32 +03:00
|
|
|
|
2022-01-18 10:18:22 +03:00
|
|
|
const service = vars('available_services').forEach((service) => Number(service.id) === Number(serviceId));
|
2016-07-17 15:43:50 +03:00
|
|
|
|
2022-01-12 13:22:54 +03:00
|
|
|
const duration = service ? service.duration : 0;
|
2020-05-12 21:52:32 +03:00
|
|
|
|
2022-01-12 13:22:54 +03:00
|
|
|
const startDatetime = new Date();
|
|
|
|
const endDatetime = moment().add(duration, 'minutes').toDate();
|
2016-07-17 15:43:50 +03:00
|
|
|
|
2024-01-06 12:46:57 +03:00
|
|
|
App.Utils.UI.initializeDateTimePicker($startDatetime, {
|
2022-01-18 13:14:45 +03:00
|
|
|
onClose: () => {
|
2022-01-17 06:33:01 +03:00
|
|
|
const serviceId = $selectService.val();
|
2020-03-10 22:44:51 +03:00
|
|
|
|
|
|
|
// Automatically update the #end-datetime DateTimePicker based on service duration.
|
2022-01-18 10:18:22 +03:00
|
|
|
const service = vars('available_services').find(
|
2023-12-22 13:35:41 +03:00
|
|
|
(availableService) => Number(availableService.id) === Number(serviceId),
|
2022-01-12 13:22:54 +03:00
|
|
|
);
|
2020-05-12 21:52:32 +03:00
|
|
|
|
2024-01-06 13:06:48 +03:00
|
|
|
const startDateTimeObject = App.Utils.UI.getDateTimePickerValue($startDatetime);
|
|
|
|
const endDateTimeObject = new Date(startDateTimeObject.getTime() + service.duration * 60000);
|
|
|
|
App.Utils.UI.setDateTimePickerValue($endDatetime, endDateTimeObject);
|
2023-12-22 13:35:41 +03:00
|
|
|
},
|
2016-07-17 15:43:50 +03:00
|
|
|
});
|
2022-05-26 16:17:33 +03:00
|
|
|
|
2024-01-06 13:06:48 +03:00
|
|
|
App.Utils.UI.setDateTimePickerValue($startDatetime, startDatetime);
|
2016-07-17 15:43:50 +03:00
|
|
|
|
2024-01-06 12:46:57 +03:00
|
|
|
App.Utils.UI.initializeDateTimePicker($endDatetime);
|
2024-01-06 13:06:48 +03:00
|
|
|
App.Utils.UI.setDateTimePickerValue($endDatetime, endDatetime);
|
2022-01-12 13:22:54 +03:00
|
|
|
}
|
2016-07-17 15:43:50 +03:00
|
|
|
|
|
|
|
/**
|
2022-01-12 13:22:54 +03:00
|
|
|
* Validate the manage appointment dialog data.
|
|
|
|
*
|
|
|
|
* Validation checks need to run every time the data are going to be saved.
|
2016-07-17 15:43:50 +03:00
|
|
|
*
|
2020-04-27 21:14:20 +03:00
|
|
|
* @return {Boolean} Returns the validation result.
|
2016-07-17 15:43:50 +03:00
|
|
|
*/
|
2020-05-06 20:23:49 +03:00
|
|
|
function validateAppointmentForm() {
|
2016-10-10 19:29:48 +03:00
|
|
|
// Reset previous validation css formatting.
|
2022-01-17 06:33:01 +03:00
|
|
|
$appointmentsModal.find('.is-invalid').removeClass('is-invalid');
|
|
|
|
$appointmentsModal.find('.modal-message').addClass('d-none');
|
2016-07-17 15:43:50 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
// Check required fields.
|
2022-01-12 13:22:54 +03:00
|
|
|
let missingRequiredField = false;
|
2016-07-17 15:43:50 +03:00
|
|
|
|
2022-01-17 06:33:01 +03:00
|
|
|
$appointmentsModal.find('.required').each((index, requiredField) => {
|
2020-05-12 21:52:32 +03:00
|
|
|
if ($(requiredField).val() === '' || $(requiredField).val() === null) {
|
2021-11-23 12:10:09 +03:00
|
|
|
$(requiredField).addClass('is-invalid');
|
2016-07-17 15:43:50 +03:00
|
|
|
missingRequiredField = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (missingRequiredField) {
|
2022-01-18 10:22:25 +03:00
|
|
|
throw new Error(lang('fields_are_required'));
|
2016-07-17 15:43:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check email address.
|
2023-12-11 11:54:29 +03:00
|
|
|
if (
|
|
|
|
$appointmentsModal.find('#email').val() &&
|
|
|
|
!App.Utils.Validation.email($appointmentsModal.find('#email').val())
|
|
|
|
) {
|
2022-01-17 06:33:01 +03:00
|
|
|
$appointmentsModal.find('#email').addClass('is-invalid');
|
2022-01-18 10:22:25 +03:00
|
|
|
throw new Error(lang('invalid_email'));
|
2016-07-17 15:43:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check appointment start and end time.
|
2024-01-06 13:06:48 +03:00
|
|
|
const startDateTimeObject = App.Utils.UI.getDateTimePickerValue($startDatetime);
|
|
|
|
const endDateTimeObject = App.Utils.UI.getDateTimePickerValue($endDatetime);
|
|
|
|
|
|
|
|
if (startDateTimeObject > endDateTimeObject) {
|
2022-01-17 06:33:01 +03:00
|
|
|
$startDatetime.addClass('is-invalid');
|
|
|
|
$endDatetime.addClass('is-invalid');
|
2022-01-18 10:22:25 +03:00
|
|
|
throw new Error(lang('start_date_before_end_error'));
|
2016-07-17 15:43:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2020-05-06 20:15:11 +03:00
|
|
|
} catch (error) {
|
2022-01-17 06:33:01 +03:00
|
|
|
$appointmentsModal
|
|
|
|
.find('.modal-message')
|
|
|
|
.addClass('alert-danger')
|
|
|
|
.text(error.message)
|
|
|
|
.removeClass('d-none');
|
2016-07-17 15:43:50 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-14 11:26:44 +03:00
|
|
|
/**
|
|
|
|
* Initialize the module.
|
|
|
|
*/
|
2022-01-12 13:22:54 +03:00
|
|
|
function initialize() {
|
2022-01-17 06:33:01 +03:00
|
|
|
addEventListeners();
|
2022-01-12 13:22:54 +03:00
|
|
|
}
|
|
|
|
|
2022-01-17 06:44:22 +03:00
|
|
|
document.addEventListener('DOMContentLoaded', initialize);
|
|
|
|
|
2022-01-12 13:22:54 +03:00
|
|
|
return {
|
2023-12-22 13:35:41 +03:00
|
|
|
resetModal,
|
2024-08-13 14:36:03 +03:00
|
|
|
validateAppointmentForm,
|
2016-07-17 15:43:50 +03:00
|
|
|
};
|
2022-01-12 13:22:54 +03:00
|
|
|
})();
|