2016-07-17 15:43:50 +03:00
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Easy!Appointments - Open Source Web Scheduler
|
|
|
|
*
|
|
|
|
* @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
|
2016-07-17 15:43:50 +03:00
|
|
|
* @since v1.2.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
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');
|
|
|
|
const $customerNotes = $('#customer-notes');
|
|
|
|
const $selectCustomer = $('#select-customer');
|
|
|
|
const $saveAppointment = $('#save-appointment');
|
|
|
|
const $appointmentId = $('#appointment-id');
|
|
|
|
const $appointmentLocation = $('#appointment-location');
|
|
|
|
const $appointmentNotes = $('#appointment-notes');
|
|
|
|
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');
|
|
|
|
|
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-12 13:22:54 +03:00
|
|
|
const provider = App.Vars.available_providers.find(function (availableProvider) {
|
2020-05-06 20:15:11 +03:00
|
|
|
return Number(availableProvider.id) === Number(providerId);
|
2020-05-07 20:00:33 +03:00
|
|
|
});
|
2020-03-29 16:08:07 +03:00
|
|
|
|
|
|
|
if (provider && provider.timezone) {
|
2022-01-12 13:22:54 +03:00
|
|
|
$('.provider-timezone').text(App.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.
|
2020-05-06 20:23:49 +03:00
|
|
|
if (!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.
|
|
|
|
|
2022-01-17 06:33:01 +03:00
|
|
|
const startDatetime = moment($startDatetime.datetimepicker('getDate')).format('YYYY-MM-DD HH:mm:ss');
|
|
|
|
const endDatetime = moment($endDatetime.datetimepicker('getDate')).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(),
|
|
|
|
notes: $appointmentNotes.val(),
|
2022-01-12 13:22:54 +03:00
|
|
|
is_unavailable: 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(),
|
|
|
|
notes: $customerNotes.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.
|
2021-12-13 09:52:09 +03:00
|
|
|
Backend.displayNotification(App.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 06:33:01 +03:00
|
|
|
$appointmentsModal.find('.alert').addClass('d-none').modal('hide');
|
|
|
|
$selectFilterItem.trigger('change');
|
2016-07-17 15:43:50 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// Define error callback.
|
2022-01-17 06:33:01 +03:00
|
|
|
const errorCallback = () => {
|
|
|
|
$appointmentsModal.find('.modal-message').text(App.Lang.service_communication_error);
|
|
|
|
$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();
|
|
|
|
|
2022-01-17 06:33:01 +03:00
|
|
|
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 06:33:01 +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-12 13:22:54 +03:00
|
|
|
const providers = App.Vars.available_providers.filter(
|
|
|
|
(provider) => Number(provider.id) === Number(providerId)
|
|
|
|
);
|
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-01-17 06:33:01 +03:00
|
|
|
const serviceId = $selectService.val();
|
2020-05-07 20:00:33 +03:00
|
|
|
|
2022-01-12 13:22:54 +03:00
|
|
|
const service = App.Vars.available_services.find(
|
|
|
|
(availableService) => Number(availableService.id) === Number(serviceId)
|
|
|
|
);
|
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
|
|
|
}
|
|
|
|
|
2022-01-17 06:33:01 +03:00
|
|
|
$startDatetime.val(
|
|
|
|
App.Utils.Date.format(startMoment.toDate(), App.Vars.date_format, App.Vars.time_format, true)
|
|
|
|
);
|
|
|
|
|
|
|
|
$endDatetime.val(
|
|
|
|
App.Utils.Date.format(
|
|
|
|
startMoment.add(duration, 'minutes').toDate(),
|
|
|
|
App.Vars.date_format,
|
|
|
|
App.Vars.time_format,
|
|
|
|
true
|
|
|
|
)
|
|
|
|
);
|
2016-07-17 15:43:50 +03:00
|
|
|
|
|
|
|
// Display modal form.
|
2022-01-17 06:33:01 +03:00
|
|
|
$appointmentsModal.find('.modal-header h3').text(App.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
|
|
|
*
|
|
|
|
* @param {jQuery.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')) {
|
|
|
|
$(event.target).find('span').text(App.Lang.hide);
|
|
|
|
$existingCustomersList.empty();
|
|
|
|
$existingCustomersList.slideDown('slow');
|
|
|
|
$filterExistingCustomers.fadeIn('slow').val('');
|
2022-01-12 13:22:54 +03:00
|
|
|
App.Vars.customers.forEach(function (customer) {
|
2020-05-07 19:47:14 +03:00
|
|
|
$('<div/>', {
|
|
|
|
'data-id': customer.id,
|
|
|
|
'text': customer.first_name + ' ' + customer.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-12 13:22:54 +03:00
|
|
|
$(event.target).find('span').text(App.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-12 13:22:54 +03:00
|
|
|
const customer = App.Vars.customers.find(function (customer) {
|
2020-05-07 20:00:33 +03:00
|
|
|
return 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);
|
|
|
|
$customerNotes.val(customer.notes);
|
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
|
|
|
|
2021-10-28 14:30:39 +03:00
|
|
|
filterExistingCustomersTimeout = setTimeout(function () {
|
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,
|
|
|
|
'text': customer.first_name + ' ' + customer.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-12 13:22:54 +03:00
|
|
|
const result = App.Vars.customers.filter((existingCustomer) => {
|
|
|
|
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-12 13:22:54 +03:00
|
|
|
App.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-17 06:33:01 +03:00
|
|
|
App.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,
|
|
|
|
'text': customer.first_name + ' ' + customer.last_name
|
2022-01-17 06:33:01 +03:00
|
|
|
}).appendTo($existingCustomersList);
|
2020-12-09 17:00:15 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
2021-10-28 14:30:39 +03:00
|
|
|
.always(function () {
|
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();
|
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-12 13:22:54 +03:00
|
|
|
const service = App.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
|
|
|
});
|
|
|
|
|
2022-01-12 13:22:54 +03:00
|
|
|
const duration = service ? service.duration : 60;
|
2020-12-08 10:57:22 +03:00
|
|
|
|
2022-01-17 06:33:01 +03:00
|
|
|
const start = $startDatetime.datetimepicker('getDate');
|
|
|
|
$endDatetime.datetimepicker('setDate', new Date(start.getTime() + duration * 60000));
|
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-12 13:22:54 +03:00
|
|
|
App.Vars.available_providers.forEach((provider) => {
|
|
|
|
provider.services.forEach((providerServiceId) => {
|
2022-01-17 06:33:01 +03:00
|
|
|
if (App.Vars.role_slug === Backend.DB_SLUG_PROVIDER && Number(provider.id) !== App.Vars.user_id) {
|
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-12 13:22:54 +03:00
|
|
|
App.Vars.role_slug === Backend.DB_SLUG_SECRETARY &&
|
|
|
|
App.Vars.secretaryProviders.indexOf(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
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
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', () => {
|
|
|
|
$appointmentsModal
|
2021-11-06 19:38:37 +03:00
|
|
|
.find(
|
|
|
|
'#customer-id, #first-name, #last-name, #email, ' +
|
|
|
|
'#phone-number, #address, #city, #zip-code, #customer-notes'
|
|
|
|
)
|
|
|
|
.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('');
|
|
|
|
$appointmentsModal.find('.modal-message').fadeOut();
|
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-12 13:22:54 +03:00
|
|
|
App.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');
|
|
|
|
$selectCustomer.find('span').text(App.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-12 13:22:54 +03:00
|
|
|
const service = App.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();
|
|
|
|
let dateFormat;
|
2016-07-17 15:43:50 +03:00
|
|
|
|
2022-01-12 13:22:54 +03:00
|
|
|
switch (App.Vars.date_format) {
|
2016-07-17 15:43:50 +03:00
|
|
|
case 'DMY':
|
|
|
|
dateFormat = 'dd/mm/yy';
|
|
|
|
break;
|
|
|
|
case 'MDY':
|
|
|
|
dateFormat = 'mm/dd/yy';
|
|
|
|
break;
|
|
|
|
case 'YMD':
|
|
|
|
dateFormat = 'yy/mm/dd';
|
|
|
|
break;
|
|
|
|
default:
|
2022-01-12 13:22:54 +03:00
|
|
|
throw new Error('Invalid App.Vars.date_format value.');
|
2016-07-17 15:43:50 +03:00
|
|
|
}
|
|
|
|
|
2022-01-12 13:22:54 +03:00
|
|
|
const firstWeekDay = App.Vars.first_weekday;
|
|
|
|
|
|
|
|
const firstWeekDayNumber = App.Utils.Date.getWeekdayId(firstWeekDay);
|
2018-06-29 01:41:17 +03:00
|
|
|
|
2022-01-17 06:33:01 +03:00
|
|
|
$startDatetime.datetimepicker({
|
2016-07-17 15:43:50 +03:00
|
|
|
dateFormat: dateFormat,
|
2022-01-12 13:22:54 +03:00
|
|
|
timeFormat: App.Vars.time_format === 'regular' ? 'h:mm tt' : 'HH:mm',
|
2016-07-17 15:43:50 +03:00
|
|
|
|
|
|
|
// Translation
|
2021-11-06 19:38:37 +03:00
|
|
|
dayNames: [
|
2021-12-13 09:52:09 +03:00
|
|
|
App.Lang.sunday,
|
|
|
|
App.Lang.monday,
|
|
|
|
App.Lang.tuesday,
|
|
|
|
App.Lang.wednesday,
|
|
|
|
App.Lang.thursday,
|
|
|
|
App.Lang.friday,
|
|
|
|
App.Lang.saturday
|
2021-11-06 19:38:37 +03:00
|
|
|
],
|
|
|
|
dayNamesShort: [
|
2021-12-13 09:52:09 +03:00
|
|
|
App.Lang.sunday.substr(0, 3),
|
|
|
|
App.Lang.monday.substr(0, 3),
|
|
|
|
App.Lang.tuesday.substr(0, 3),
|
|
|
|
App.Lang.wednesday.substr(0, 3),
|
|
|
|
App.Lang.thursday.substr(0, 3),
|
|
|
|
App.Lang.friday.substr(0, 3),
|
|
|
|
App.Lang.saturday.substr(0, 3)
|
2021-11-06 19:38:37 +03:00
|
|
|
],
|
|
|
|
dayNamesMin: [
|
2021-12-13 09:52:09 +03:00
|
|
|
App.Lang.sunday.substr(0, 2),
|
|
|
|
App.Lang.monday.substr(0, 2),
|
|
|
|
App.Lang.tuesday.substr(0, 2),
|
|
|
|
App.Lang.wednesday.substr(0, 2),
|
|
|
|
App.Lang.thursday.substr(0, 2),
|
|
|
|
App.Lang.friday.substr(0, 2),
|
|
|
|
App.Lang.saturday.substr(0, 2)
|
2021-11-06 19:38:37 +03:00
|
|
|
],
|
|
|
|
monthNames: [
|
2021-12-13 09:52:09 +03:00
|
|
|
App.Lang.january,
|
|
|
|
App.Lang.february,
|
|
|
|
App.Lang.march,
|
|
|
|
App.Lang.april,
|
|
|
|
App.Lang.may,
|
|
|
|
App.Lang.june,
|
|
|
|
App.Lang.july,
|
|
|
|
App.Lang.august,
|
|
|
|
App.Lang.september,
|
|
|
|
App.Lang.october,
|
|
|
|
App.Lang.november,
|
|
|
|
App.Lang.december
|
2021-11-06 19:38:37 +03:00
|
|
|
],
|
2021-12-13 09:52:09 +03:00
|
|
|
prevText: App.Lang.previous,
|
|
|
|
nextText: App.Lang.next,
|
|
|
|
currentText: App.Lang.now,
|
|
|
|
closeText: App.Lang.close,
|
|
|
|
timeOnlyTitle: App.Lang.select_time,
|
|
|
|
timeText: App.Lang.time,
|
|
|
|
hourText: App.Lang.hour,
|
|
|
|
minuteText: App.Lang.minutes,
|
2020-04-27 21:14:20 +03:00
|
|
|
firstDay: firstWeekDayNumber,
|
2018-11-07 19:24:08 +03:00
|
|
|
onClose: function () {
|
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-12 13:22:54 +03:00
|
|
|
const service = App.Vars.available_services.find(
|
|
|
|
(availableService) => Number(availableService.id) === Number(serviceId)
|
|
|
|
);
|
2020-05-12 21:52:32 +03:00
|
|
|
|
2022-01-17 06:33:01 +03:00
|
|
|
const start = $startDatetime.datetimepicker('getDate');
|
|
|
|
$endDatetime.datetimepicker('setDate', new Date(start.getTime() + service.duration * 60000));
|
2020-03-10 22:44:51 +03:00
|
|
|
}
|
2016-07-17 15:43:50 +03:00
|
|
|
});
|
2022-01-17 06:33:01 +03:00
|
|
|
$startDatetime.datetimepicker('setDate', startDatetime);
|
2016-07-17 15:43:50 +03:00
|
|
|
|
2022-01-17 06:33:01 +03:00
|
|
|
$endDatetime.datetimepicker({
|
2016-07-17 15:43:50 +03:00
|
|
|
dateFormat: dateFormat,
|
2022-01-12 13:22:54 +03:00
|
|
|
timeFormat: App.Vars.time_format === 'regular' ? 'h:mm tt' : 'HH:mm',
|
2016-07-17 15:43:50 +03:00
|
|
|
|
|
|
|
// Translation
|
2021-11-06 19:38:37 +03:00
|
|
|
dayNames: [
|
2021-12-13 09:52:09 +03:00
|
|
|
App.Lang.sunday,
|
|
|
|
App.Lang.monday,
|
|
|
|
App.Lang.tuesday,
|
|
|
|
App.Lang.wednesday,
|
|
|
|
App.Lang.thursday,
|
|
|
|
App.Lang.friday,
|
|
|
|
App.Lang.saturday
|
2021-11-06 19:38:37 +03:00
|
|
|
],
|
|
|
|
dayNamesShort: [
|
2021-12-13 09:52:09 +03:00
|
|
|
App.Lang.sunday.substr(0, 3),
|
|
|
|
App.Lang.monday.substr(0, 3),
|
|
|
|
App.Lang.tuesday.substr(0, 3),
|
|
|
|
App.Lang.wednesday.substr(0, 3),
|
|
|
|
App.Lang.thursday.substr(0, 3),
|
|
|
|
App.Lang.friday.substr(0, 3),
|
|
|
|
App.Lang.saturday.substr(0, 3)
|
2021-11-06 19:38:37 +03:00
|
|
|
],
|
|
|
|
dayNamesMin: [
|
2021-12-13 09:52:09 +03:00
|
|
|
App.Lang.sunday.substr(0, 2),
|
|
|
|
App.Lang.monday.substr(0, 2),
|
|
|
|
App.Lang.tuesday.substr(0, 2),
|
|
|
|
App.Lang.wednesday.substr(0, 2),
|
|
|
|
App.Lang.thursday.substr(0, 2),
|
|
|
|
App.Lang.friday.substr(0, 2),
|
|
|
|
App.Lang.saturday.substr(0, 2)
|
2021-11-06 19:38:37 +03:00
|
|
|
],
|
|
|
|
monthNames: [
|
2021-12-13 09:52:09 +03:00
|
|
|
App.Lang.january,
|
|
|
|
App.Lang.february,
|
|
|
|
App.Lang.march,
|
|
|
|
App.Lang.april,
|
|
|
|
App.Lang.may,
|
|
|
|
App.Lang.june,
|
|
|
|
App.Lang.july,
|
|
|
|
App.Lang.august,
|
|
|
|
App.Lang.september,
|
|
|
|
App.Lang.october,
|
|
|
|
App.Lang.november,
|
|
|
|
App.Lang.december
|
2021-11-06 19:38:37 +03:00
|
|
|
],
|
2021-12-13 09:52:09 +03:00
|
|
|
prevText: App.Lang.previous,
|
|
|
|
nextText: App.Lang.next,
|
|
|
|
currentText: App.Lang.now,
|
|
|
|
closeText: App.Lang.close,
|
|
|
|
timeOnlyTitle: App.Lang.select_time,
|
|
|
|
timeText: App.Lang.time,
|
|
|
|
hourText: App.Lang.hour,
|
|
|
|
minuteText: App.Lang.minutes,
|
2020-04-27 21:14:20 +03:00
|
|
|
firstDay: firstWeekDayNumber
|
2016-07-17 15:43:50 +03:00
|
|
|
});
|
2022-01-17 06:33:01 +03:00
|
|
|
$endDatetime.datetimepicker('setDate', 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) {
|
2021-12-13 09:52:09 +03:00
|
|
|
throw new Error(App.Lang.fields_are_required);
|
2016-07-17 15:43:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check email address.
|
2022-01-17 06:33:01 +03:00
|
|
|
if (!App.Utils.Validation.email($appointmentsModal.find('#email').val())) {
|
|
|
|
$appointmentsModal.find('#email').addClass('is-invalid');
|
2021-12-13 09:52:09 +03:00
|
|
|
throw new Error(App.Lang.invalid_email);
|
2016-07-17 15:43:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check appointment start and end time.
|
2022-01-17 06:33:01 +03:00
|
|
|
const start = $startDatetime.datetimepicker('getDate');
|
|
|
|
const end = $endDatetime.datetimepicker('getDate');
|
2016-07-17 15:43:50 +03:00
|
|
|
if (start > end) {
|
2022-01-17 06:33:01 +03:00
|
|
|
$startDatetime.addClass('is-invalid');
|
|
|
|
$endDatetime.addClass('is-invalid');
|
2021-12-13 09:52:09 +03:00
|
|
|
throw new Error(App.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
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2022-01-17 06:33:01 +03:00
|
|
|
resetModal,
|
2022-01-12 13:22:54 +03:00
|
|
|
initialize
|
2016-07-17 15:43:50 +03:00
|
|
|
};
|
2022-01-12 13:22:54 +03:00
|
|
|
})();
|