2016-04-02 15:59:31 +03:00
|
|
|
/* ----------------------------------------------------------------------------
|
2022-01-18 15:05:42 +03:00
|
|
|
* Easy!Appointments - Online Appointment Scheduler
|
2016-04-02 15:59:31 +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-06 11:53:16 +03:00
|
|
|
* @since v1.5.0
|
2016-04-02 15:59:31 +03:00
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
2022-01-06 11:53:16 +03:00
|
|
|
/**
|
2022-01-14 11:26:44 +03:00
|
|
|
* Customers page.
|
2022-01-06 11:53:16 +03:00
|
|
|
*
|
2022-01-14 11:26:44 +03:00
|
|
|
* This module implements the functionality of the customers page.
|
2022-01-06 11:53:16 +03:00
|
|
|
*/
|
|
|
|
App.Pages.Customers = (function () {
|
|
|
|
const $customers = $('#customers');
|
2022-01-17 16:24:58 +03:00
|
|
|
const $filterCustomers = $('#filter-customers');
|
|
|
|
const $id = $('#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 $timezone = $('#timezone');
|
|
|
|
const $language = $('#language');
|
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 16:24:58 +03:00
|
|
|
const $notes = $('#notes');
|
|
|
|
const $formMessage = $('#form-message');
|
|
|
|
const $customerAppointments = $('#customer-appointments');
|
2023-12-11 11:54:29 +03:00
|
|
|
|
2022-01-06 11:53:16 +03:00
|
|
|
let filterResults = {};
|
|
|
|
let filterLimit = 20;
|
2016-04-02 15:59:31 +03:00
|
|
|
|
|
|
|
/**
|
2022-01-17 16:24:58 +03:00
|
|
|
* Add the page event listeners.
|
2016-04-02 15:59:31 +03:00
|
|
|
*/
|
2022-01-17 16:24:58 +03:00
|
|
|
function addEventListeners() {
|
2016-04-02 15:59:31 +03:00
|
|
|
/**
|
|
|
|
* Event: Filter Customers Form "Submit"
|
2020-12-08 14:23:37 +03:00
|
|
|
*
|
|
|
|
* @param {jQuery.Event} event
|
2016-04-02 15:59:31 +03:00
|
|
|
*/
|
2022-01-17 16:24:58 +03:00
|
|
|
$customers.on('submit', '#filter-customers form', (event) => {
|
2020-12-08 14:23:37 +03:00
|
|
|
event.preventDefault();
|
2022-01-17 16:24:58 +03:00
|
|
|
const key = $filterCustomers.find('.key').val();
|
|
|
|
$filterCustomers.find('.selected').removeClass('selected');
|
2022-01-06 11:53:16 +03:00
|
|
|
filterLimit = 20;
|
|
|
|
resetForm();
|
|
|
|
filter(key);
|
2016-04-02 15:59:31 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2016-05-20 10:08:41 +03:00
|
|
|
* Event: Filter Entry "Click"
|
2016-04-02 15:59:31 +03:00
|
|
|
*
|
|
|
|
* Display the customer data of the selected row.
|
2022-01-17 16:24:58 +03:00
|
|
|
*
|
|
|
|
* @param {jQuery.Event} event
|
2016-04-02 15:59:31 +03:00
|
|
|
*/
|
2022-01-17 16:24:58 +03:00
|
|
|
$customers.on('click', '.customer-row', (event) => {
|
|
|
|
if ($filterCustomers.find('.filter').prop('disabled')) {
|
2016-04-02 15:59:31 +03:00
|
|
|
return; // Do nothing when user edits a customer record.
|
|
|
|
}
|
|
|
|
|
2022-01-17 23:31:53 +03:00
|
|
|
const customerId = $(event.currentTarget).attr('data-id');
|
2022-01-17 16:24:58 +03:00
|
|
|
const customer = filterResults.find((filterResult) => Number(filterResult.id) === Number(customerId));
|
2016-04-02 15:59:31 +03:00
|
|
|
|
2022-01-06 11:53:16 +03:00
|
|
|
display(customer);
|
2016-05-20 10:08:41 +03:00
|
|
|
$('#filter-customers .selected').removeClass('selected');
|
2022-01-17 23:31:53 +03:00
|
|
|
$(event.currentTarget).addClass('selected');
|
2016-04-02 15:59:31 +03:00
|
|
|
$('#edit-customer, #delete-customer').prop('disabled', false);
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Add Customer Button "Click"
|
|
|
|
*/
|
2022-01-17 16:24:58 +03:00
|
|
|
$customers.on('click', '#add-customer', () => {
|
2022-01-06 11:53:16 +03:00
|
|
|
resetForm();
|
2022-01-17 16:24:58 +03:00
|
|
|
$customers.find('#add-edit-delete-group').hide();
|
|
|
|
$customers.find('#save-cancel-group').show();
|
|
|
|
$customers.find('.record-details').find('input, select, textarea').prop('disabled', false);
|
2022-01-19 11:39:13 +03:00
|
|
|
$customers.find('.record-details .form-label span').prop('hidden', false);
|
2022-01-17 16:24:58 +03:00
|
|
|
$filterCustomers.find('button').prop('disabled', true);
|
|
|
|
$filterCustomers.find('.results').css('color', '#AAA');
|
2016-04-02 15:59:31 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Edit Customer Button "Click"
|
|
|
|
*/
|
2022-01-17 16:24:58 +03:00
|
|
|
$customers.on('click', '#edit-customer', () => {
|
|
|
|
$customers.find('.record-details').find('input, select, textarea').prop('disabled', false);
|
2022-01-19 11:39:13 +03:00
|
|
|
$customers.find('.record-details .form-label span').prop('hidden', false);
|
2022-01-17 16:24:58 +03:00
|
|
|
$customers.find('#add-edit-delete-group').hide();
|
|
|
|
$customers.find('#save-cancel-group').show();
|
|
|
|
$filterCustomers.find('button').prop('disabled', true);
|
|
|
|
$filterCustomers.find('.results').css('color', '#AAA');
|
2016-04-02 15:59:31 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Cancel Customer Add/Edit Operation Button "Click"
|
|
|
|
*/
|
2022-01-17 16:24:58 +03:00
|
|
|
$customers.on('click', '#cancel-customer', () => {
|
|
|
|
const id = $id.val();
|
|
|
|
|
2022-01-06 11:53:16 +03:00
|
|
|
resetForm();
|
2022-01-17 16:24:58 +03:00
|
|
|
|
2020-05-06 20:15:11 +03:00
|
|
|
if (id) {
|
2022-01-06 11:53:16 +03:00
|
|
|
select(id, true);
|
2016-04-02 15:59:31 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Save Add/Edit Customer Operation "Click"
|
|
|
|
*/
|
2022-01-17 16:24:58 +03:00
|
|
|
$customers.on('click', '#save-customer', () => {
|
2022-01-07 11:02:30 +03:00
|
|
|
const customer = {
|
2022-01-17 16:24:58 +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: $notes.val(),
|
|
|
|
timezone: $timezone.val(),
|
2023-12-11 11:54:29 +03:00
|
|
|
language: $language.val() || 'english',
|
|
|
|
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-04-02 15:59:31 +03:00
|
|
|
};
|
|
|
|
|
2022-01-17 16:24:58 +03:00
|
|
|
if ($id.val()) {
|
|
|
|
customer.id = $id.val();
|
2016-04-02 15:59:31 +03:00
|
|
|
}
|
|
|
|
|
2022-01-06 11:53:16 +03:00
|
|
|
if (!validate()) {
|
2017-11-14 15:52:59 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-04-02 15:59:31 +03:00
|
|
|
|
2022-01-06 11:53:16 +03:00
|
|
|
save(customer);
|
2016-04-02 15:59:31 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Delete Customer Button "Click"
|
|
|
|
*/
|
2022-01-17 16:24:58 +03:00
|
|
|
$customers.on('click', '#delete-customer', () => {
|
|
|
|
const customerId = $id.val();
|
2022-01-07 11:02:30 +03:00
|
|
|
const buttons = [
|
2017-06-16 09:51:05 +03:00
|
|
|
{
|
2022-01-18 10:22:25 +03:00
|
|
|
text: lang('cancel'),
|
2023-02-20 10:24:49 +03:00
|
|
|
click: (event, messageModal) => {
|
|
|
|
messageModal.dispose();
|
2023-12-22 13:35:41 +03:00
|
|
|
},
|
2017-06-16 09:51:05 +03:00
|
|
|
},
|
|
|
|
{
|
2022-01-18 10:22:25 +03:00
|
|
|
text: lang('delete'),
|
2023-02-20 10:24:49 +03:00
|
|
|
click: (event, messageModal) => {
|
2022-01-06 11:53:16 +03:00
|
|
|
remove(customerId);
|
2023-02-20 10:24:49 +03:00
|
|
|
messageModal.dispose();
|
2023-12-22 13:35:41 +03:00
|
|
|
},
|
|
|
|
},
|
2017-06-16 09:51:05 +03:00
|
|
|
];
|
2016-04-02 15:59:31 +03:00
|
|
|
|
2022-01-18 10:22:25 +03:00
|
|
|
App.Utils.Message.show(lang('delete_customer'), lang('delete_record_prompt'), buttons);
|
2016-04-02 15:59:31 +03:00
|
|
|
});
|
2022-01-06 11:53:16 +03:00
|
|
|
}
|
2016-04-02 15:59:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Save a customer record to the database (via ajax post).
|
|
|
|
*
|
2016-05-15 13:17:12 +03:00
|
|
|
* @param {Object} customer Contains the customer data.
|
2016-04-02 15:59:31 +03:00
|
|
|
*/
|
2022-01-06 11:53:16 +03:00
|
|
|
function save(customer) {
|
|
|
|
App.Http.Customers.save(customer).then((response) => {
|
2022-01-18 10:22:25 +03:00
|
|
|
App.Layouts.Backend.displayNotification(lang('customer_saved'));
|
2022-01-06 11:53:16 +03:00
|
|
|
resetForm();
|
|
|
|
$('#filter-customers .key').val('');
|
|
|
|
filter('', response.id, true);
|
|
|
|
});
|
|
|
|
}
|
2016-04-02 15:59:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a customer record from database.
|
|
|
|
*
|
2016-05-15 13:17:12 +03:00
|
|
|
* @param {Number} id Record id to be deleted.
|
2016-04-02 15:59:31 +03:00
|
|
|
*/
|
2022-01-06 11:53:16 +03:00
|
|
|
function remove(id) {
|
|
|
|
App.Http.Customers.destroy(id).then(() => {
|
2022-01-18 10:22:25 +03:00
|
|
|
App.Layouts.Backend.displayNotification(lang('customer_deleted'));
|
2022-01-06 11:53:16 +03:00
|
|
|
resetForm();
|
|
|
|
filter($('#filter-customers .key').val());
|
|
|
|
});
|
|
|
|
}
|
2016-04-02 15:59:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate customer data before save (insert or update).
|
|
|
|
*/
|
2022-01-06 11:53:16 +03:00
|
|
|
function validate() {
|
2022-01-17 16:24:58 +03:00
|
|
|
$formMessage.removeClass('alert-danger').hide();
|
2021-11-23 12:10:09 +03:00
|
|
|
$('.is-invalid').removeClass('is-invalid');
|
2016-04-02 15:59:31 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
// Validate required fields.
|
2022-01-07 11:02:30 +03:00
|
|
|
let missingRequired = false;
|
2016-04-02 15:59:31 +03:00
|
|
|
|
2022-01-17 16:24:58 +03:00
|
|
|
$('.required').each((index, requiredField) => {
|
2020-05-12 21:52:32 +03:00
|
|
|
if ($(requiredField).val() === '') {
|
2021-11-23 12:10:09 +03:00
|
|
|
$(requiredField).addClass('is-invalid');
|
2016-04-02 15:59:31 +03:00
|
|
|
missingRequired = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (missingRequired) {
|
2022-01-18 10:22:25 +03:00
|
|
|
throw new Error(lang('fields_are_required'));
|
2016-04-02 15:59:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate email address.
|
2022-01-24 17:48:20 +03:00
|
|
|
const email = $email.val();
|
|
|
|
|
|
|
|
if (email && !App.Utils.Validation.email(email)) {
|
2022-01-17 16:24:58 +03:00
|
|
|
$email.addClass('is-invalid');
|
2022-01-18 10:22:25 +03:00
|
|
|
throw new Error(lang('invalid_email'));
|
2016-04-02 15:59:31 +03:00
|
|
|
}
|
|
|
|
|
2022-01-24 17:48:20 +03:00
|
|
|
// Validate phone number.
|
|
|
|
const phoneNumber = $phoneNumber.val();
|
|
|
|
|
|
|
|
if (phoneNumber && !App.Utils.Validation.phone(phoneNumber)) {
|
|
|
|
$phoneNumber.addClass('is-invalid');
|
|
|
|
throw new Error(lang('invalid_phone'));
|
|
|
|
}
|
|
|
|
|
2016-04-02 15:59:31 +03:00
|
|
|
return true;
|
2020-05-06 20:15:11 +03:00
|
|
|
} catch (error) {
|
2022-01-17 16:24:58 +03:00
|
|
|
$formMessage.addClass('alert-danger').text(error.message).show();
|
2016-04-02 15:59:31 +03:00
|
|
|
return false;
|
|
|
|
}
|
2022-01-06 11:53:16 +03:00
|
|
|
}
|
2016-04-02 15:59:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Bring the customer form back to its initial state.
|
|
|
|
*/
|
2022-01-06 11:53:16 +03:00
|
|
|
function resetForm() {
|
2022-01-17 16:24:58 +03:00
|
|
|
$customers.find('.record-details').find('input, select, textarea').val('').prop('disabled', true);
|
2022-01-19 11:39:13 +03:00
|
|
|
$customers.find('.record-details .form-label span').prop('hidden', true);
|
2022-01-17 16:24:58 +03:00
|
|
|
$customers.find('.record-details #timezone').val('UTC');
|
2016-04-02 15:59:31 +03:00
|
|
|
|
2022-01-17 16:24:58 +03:00
|
|
|
$language.val('english');
|
2020-06-16 18:01:27 +03:00
|
|
|
|
2022-01-17 16:24:58 +03:00
|
|
|
$customerAppointments.empty();
|
2016-04-02 15:59:31 +03:00
|
|
|
|
2022-01-17 16:24:58 +03:00
|
|
|
$customers.find('#edit-customer, #delete-customer').prop('disabled', true);
|
|
|
|
$customers.find('#add-edit-delete-group').show();
|
|
|
|
$customers.find('#save-cancel-group').hide();
|
2016-04-02 15:59:31 +03:00
|
|
|
|
2022-01-17 16:24:58 +03:00
|
|
|
$customers.find('.record-details .is-invalid').removeClass('is-invalid');
|
|
|
|
$customers.find('.record-details #form-message').hide();
|
|
|
|
|
|
|
|
$filterCustomers.find('button').prop('disabled', false);
|
|
|
|
$filterCustomers.find('.selected').removeClass('selected');
|
|
|
|
$filterCustomers.find('.results').css('color', '');
|
2022-01-06 11:53:16 +03:00
|
|
|
}
|
2016-04-02 15:59:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a customer record into the form.
|
|
|
|
*
|
2016-05-15 13:17:12 +03:00
|
|
|
* @param {Object} customer Contains the customer record data.
|
2016-04-02 15:59:31 +03:00
|
|
|
*/
|
2022-01-06 11:53:16 +03:00
|
|
|
function display(customer) {
|
2022-01-17 16:24:58 +03:00
|
|
|
$id.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);
|
|
|
|
$notes.val(customer.notes);
|
|
|
|
$timezone.val(customer.timezone);
|
|
|
|
$language.val(customer.language || 'english');
|
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);
|
2022-01-17 16:24:58 +03:00
|
|
|
|
|
|
|
$customerAppointments.empty();
|
2020-09-08 10:42:58 +03:00
|
|
|
|
|
|
|
if (!customer.appointments.length) {
|
|
|
|
$('<p/>', {
|
2023-12-22 13:35:41 +03:00
|
|
|
'text': lang('no_records_found'),
|
2022-01-17 16:24:58 +03:00
|
|
|
}).appendTo($customerAppointments);
|
2020-09-08 10:42:58 +03:00
|
|
|
}
|
|
|
|
|
2022-01-17 16:24:58 +03:00
|
|
|
customer.appointments.forEach((appointment) => {
|
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_PROVIDER &&
|
|
|
|
parseInt(appointment.id_users_provider) !== vars('user_id')
|
2021-11-06 19:38:37 +03:00
|
|
|
) {
|
2020-05-12 21:52:32 +03:00
|
|
|
return;
|
2018-07-29 16:59:18 +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 &&
|
|
|
|
vars('secretary_providers').indexOf(appointment.id_users_provider) === -1
|
2021-11-06 19:38:37 +03:00
|
|
|
) {
|
2020-05-12 21:52:32 +03:00
|
|
|
return;
|
2018-07-29 16:59:18 +03:00
|
|
|
}
|
|
|
|
|
2022-01-07 11:02:30 +03:00
|
|
|
const start = App.Utils.Date.format(
|
2021-11-24 10:34:26 +03:00
|
|
|
moment(appointment.start_datetime).toDate(),
|
2022-01-18 10:18:22 +03:00
|
|
|
vars('date_format'),
|
|
|
|
vars('time_format'),
|
2023-12-22 13:35:41 +03:00
|
|
|
true,
|
2021-11-06 19:38:37 +03:00
|
|
|
);
|
2022-01-07 11:02:30 +03:00
|
|
|
|
|
|
|
const end = App.Utils.Date.format(
|
2021-11-24 10:34:26 +03:00
|
|
|
moment(appointment.end_datetime).toDate(),
|
2022-01-18 10:18:22 +03:00
|
|
|
vars('date_format'),
|
|
|
|
vars('time_format'),
|
2023-12-22 13:35:41 +03:00
|
|
|
true,
|
2021-11-06 19:38:37 +03:00
|
|
|
);
|
2020-05-07 19:47:14 +03:00
|
|
|
|
|
|
|
$('<div/>', {
|
|
|
|
'class': 'appointment-row',
|
|
|
|
'data-id': appointment.id,
|
|
|
|
'html': [
|
2020-09-08 10:42:58 +03:00
|
|
|
// Service - Provider
|
|
|
|
|
|
|
|
$('<a/>', {
|
2022-01-17 23:31:53 +03:00
|
|
|
'href': App.Utils.Url.siteUrl(`calendar/reschedule/${appointment.hash}`),
|
2020-09-08 10:42:58 +03:00
|
|
|
'html': [
|
|
|
|
$('<i/>', {
|
2023-12-22 13:35:41 +03:00
|
|
|
'class': 'fas fa-edit me-1',
|
2020-09-08 10:42:58 +03:00
|
|
|
}),
|
|
|
|
$('<strong/>', {
|
2021-11-06 19:38:37 +03:00
|
|
|
'text':
|
|
|
|
appointment.service.name +
|
|
|
|
' - ' +
|
|
|
|
appointment.provider.first_name +
|
|
|
|
' ' +
|
2023-12-22 13:35:41 +03:00
|
|
|
appointment.provider.last_name,
|
2020-09-08 10:42:58 +03:00
|
|
|
}),
|
2023-12-22 13:35:41 +03:00
|
|
|
$('<br/>'),
|
|
|
|
],
|
2020-09-08 10:42:58 +03:00
|
|
|
}),
|
|
|
|
|
|
|
|
// Start
|
|
|
|
|
|
|
|
$('<small/>', {
|
2023-12-22 13:35:41 +03:00
|
|
|
'text': start,
|
2020-05-07 19:47:14 +03:00
|
|
|
}),
|
|
|
|
$('<br/>'),
|
2020-09-08 10:42:58 +03:00
|
|
|
|
|
|
|
// End
|
|
|
|
|
|
|
|
$('<small/>', {
|
2023-12-22 13:35:41 +03:00
|
|
|
'text': end,
|
2020-05-07 19:47:14 +03:00
|
|
|
}),
|
2020-09-08 10:42:58 +03:00
|
|
|
$('<br/>'),
|
|
|
|
|
|
|
|
// Timezone
|
|
|
|
|
|
|
|
$('<small/>', {
|
2023-12-22 13:35:41 +03:00
|
|
|
'text': vars('timezones')[appointment.provider.timezone],
|
|
|
|
}),
|
|
|
|
],
|
2021-11-06 19:38:37 +03:00
|
|
|
}).appendTo('#customer-appointments');
|
2016-04-02 15:59:31 +03:00
|
|
|
});
|
2022-01-06 11:53:16 +03:00
|
|
|
}
|
2016-04-02 15:59:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter customer records.
|
|
|
|
*
|
2021-11-18 08:34:07 +03:00
|
|
|
* @param {String} keyword This keyword string is used to filter the customer records.
|
2016-05-15 13:17:12 +03:00
|
|
|
* @param {Number} selectId Optional, if set then after the filter operation the record with the given
|
|
|
|
* ID will be selected (but not displayed).
|
2022-01-07 11:07:07 +03:00
|
|
|
* @param {Boolean} show Optional (false), if true then the selected record will be displayed on the form.
|
2016-04-02 15:59:31 +03:00
|
|
|
*/
|
2022-01-07 11:07:07 +03:00
|
|
|
function filter(keyword, selectId = null, show = false) {
|
2022-01-06 11:53:16 +03:00
|
|
|
App.Http.Customers.search(keyword, filterLimit).then((response) => {
|
|
|
|
filterResults = response;
|
2016-04-02 15:59:31 +03:00
|
|
|
|
2022-01-17 16:24:58 +03:00
|
|
|
$filterCustomers.find('.results').empty();
|
2020-05-06 20:15:11 +03:00
|
|
|
|
2022-01-17 16:24:58 +03:00
|
|
|
response.forEach((customer) => {
|
|
|
|
$('#filter-customers .results').append(getFilterHtml(customer)).append($('<hr/>'));
|
|
|
|
});
|
2020-05-12 21:52:32 +03:00
|
|
|
|
2022-01-06 11:53:16 +03:00
|
|
|
if (!response.length) {
|
2022-01-17 16:24:58 +03:00
|
|
|
$filterCustomers.find('.results').append(
|
2022-01-06 11:53:16 +03:00
|
|
|
$('<em/>', {
|
2023-12-22 13:35:41 +03:00
|
|
|
'text': lang('no_records_found'),
|
|
|
|
}),
|
2021-11-06 19:38:37 +03:00
|
|
|
);
|
2022-01-06 11:53:16 +03:00
|
|
|
} else if (response.length === filterLimit) {
|
|
|
|
$('<button/>', {
|
|
|
|
'type': 'button',
|
|
|
|
'class': 'btn btn-outline-secondary w-100 load-more text-center',
|
2022-01-18 10:22:25 +03:00
|
|
|
'text': lang('load_more'),
|
2022-01-17 16:24:58 +03:00
|
|
|
'click': () => {
|
2022-01-06 11:53:16 +03:00
|
|
|
filterLimit += 20;
|
2022-01-07 11:07:07 +03:00
|
|
|
filter(keyword, selectId, show);
|
2023-12-22 13:35:41 +03:00
|
|
|
},
|
2022-01-06 11:53:16 +03:00
|
|
|
}).appendTo('#filter-customers .results');
|
|
|
|
}
|
2020-05-06 20:15:11 +03:00
|
|
|
|
2022-01-06 11:53:16 +03:00
|
|
|
if (selectId) {
|
2022-01-07 11:07:07 +03:00
|
|
|
select(selectId, show);
|
2022-01-06 11:53:16 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-04-02 15:59:31 +03:00
|
|
|
|
|
|
|
/**
|
2016-05-15 13:17:12 +03:00
|
|
|
* Get the filter results row HTML code.
|
|
|
|
*
|
|
|
|
* @param {Object} customer Contains the customer data.
|
2016-04-02 15:59:31 +03:00
|
|
|
*
|
2016-05-15 13:17:12 +03:00
|
|
|
* @return {String} Returns the record HTML code.
|
2016-04-02 15:59:31 +03:00
|
|
|
*/
|
2022-01-06 11:53:16 +03:00
|
|
|
function getFilterHtml(customer) {
|
2022-05-27 10:25:21 +03:00
|
|
|
const name = (customer.first_name || '[No First Name]') + ' ' + (customer.last_name || '[No Last Name]');
|
2020-05-07 19:47:14 +03:00
|
|
|
|
2022-05-27 10:25:21 +03:00
|
|
|
let info = customer.email || '[No Email]';
|
2016-04-02 15:59:31 +03:00
|
|
|
|
2020-05-07 19:47:14 +03:00
|
|
|
info = customer.phone_number ? info + ', ' + customer.phone_number : info;
|
2016-04-02 15:59:31 +03:00
|
|
|
|
2020-05-07 19:47:14 +03:00
|
|
|
return $('<div/>', {
|
|
|
|
'class': 'customer-row entry',
|
|
|
|
'data-id': customer.id,
|
|
|
|
'html': [
|
|
|
|
$('<strong/>', {
|
2023-12-22 13:35:41 +03:00
|
|
|
'text': name,
|
2020-05-07 19:47:14 +03:00
|
|
|
}),
|
|
|
|
$('<br/>'),
|
2022-01-25 02:11:51 +03:00
|
|
|
$('<small/>', {
|
|
|
|
'class': 'text-muted',
|
2023-12-22 13:35:41 +03:00
|
|
|
'text': info,
|
2020-05-07 19:47:14 +03:00
|
|
|
}),
|
2023-12-22 13:35:41 +03:00
|
|
|
$('<br/>'),
|
|
|
|
],
|
2020-05-07 19:47:14 +03:00
|
|
|
});
|
2022-01-06 11:53:16 +03:00
|
|
|
}
|
2016-04-02 15:59:31 +03:00
|
|
|
|
|
|
|
/**
|
2016-05-15 13:17:12 +03:00
|
|
|
* Select a specific record from the current filter results.
|
|
|
|
*
|
|
|
|
* If the customer id does not exist in the list then no record will be selected.
|
2016-04-02 15:59:31 +03:00
|
|
|
*
|
2016-05-15 13:17:12 +03:00
|
|
|
* @param {Number} id The record id to be selected from the filter results.
|
2022-01-17 16:24:58 +03:00
|
|
|
* @param {Boolean} show Optional (false), if true then the method will display the record on the form.
|
2016-04-02 15:59:31 +03:00
|
|
|
*/
|
2022-01-06 11:53:16 +03:00
|
|
|
function select(id, show = false) {
|
2016-05-20 10:08:41 +03:00
|
|
|
$('#filter-customers .selected').removeClass('selected');
|
2016-04-02 15:59:31 +03:00
|
|
|
|
2020-05-12 21:52:32 +03:00
|
|
|
$('#filter-customers .entry[data-id="' + id + '"]').addClass('selected');
|
2016-04-02 15:59:31 +03:00
|
|
|
|
2022-01-06 11:53:16 +03:00
|
|
|
if (show) {
|
2022-01-17 16:24:58 +03:00
|
|
|
const customer = filterResults.find((filterResult) => Number(filterResult.id) === Number(id));
|
2020-05-12 21:52:32 +03:00
|
|
|
|
2022-01-06 11:53:16 +03:00
|
|
|
display(customer);
|
2020-05-12 21:52:32 +03:00
|
|
|
|
|
|
|
$('#edit-customer, #delete-customer').prop('disabled', false);
|
2016-04-02 15:59:31 +03:00
|
|
|
}
|
2022-01-06 11:53:16 +03:00
|
|
|
}
|
|
|
|
|
2022-01-14 11:26:44 +03:00
|
|
|
/**
|
|
|
|
* Initialize the module.
|
|
|
|
*/
|
2022-01-14 10:31:12 +03:00
|
|
|
function initialize() {
|
2022-01-06 11:53:16 +03:00
|
|
|
resetForm();
|
2022-01-17 16:24:58 +03:00
|
|
|
addEventListeners();
|
2022-01-06 11:53:16 +03:00
|
|
|
filter('');
|
|
|
|
}
|
2016-04-02 15:59:31 +03:00
|
|
|
|
2022-01-14 10:31:12 +03:00
|
|
|
document.addEventListener('DOMContentLoaded', initialize);
|
2022-01-06 11:53:16 +03:00
|
|
|
|
|
|
|
return {
|
|
|
|
filter,
|
|
|
|
save,
|
|
|
|
remove,
|
|
|
|
getFilterHtml,
|
|
|
|
resetForm,
|
2023-12-22 13:35:41 +03:00
|
|
|
select,
|
2022-01-06 11:53:16 +03:00
|
|
|
};
|
2016-04-02 15:59:31 +03:00
|
|
|
})();
|