iflrandevu/assets/js/backend_customers_helper.js

505 lines
17 KiB
JavaScript
Raw Normal View History

/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
* @link http://easyappointments.org
* @since v1.0.0
* ---------------------------------------------------------------------------- */
2018-01-23 12:08:37 +03:00
(function () {
'use strict';
/**
* CustomersHelper Class
*
* This class contains the methods that are used in the backend customers page.
*
* @class CustomersHelper
*/
function CustomersHelper() {
this.filterResults = {};
this.filterLimit = 20;
2017-01-21 22:02:38 +03:00
}
/**
* Binds the default event handlers of the backend customers page.
*/
2018-01-23 12:08:37 +03:00
CustomersHelper.prototype.bindEventHandlers = function () {
var instance = this;
/**
* Event: Filter Customers Form "Submit"
*/
2018-01-23 12:08:37 +03:00
$('#filter-customers form').submit(function (event) {
var key = $('#filter-customers .key').val();
$('#filter-customers .selected').removeClass('selected');
instance.filterLimit = 20;
instance.resetForm();
instance.filter(key);
return false;
});
/**
* Event: Filter Customers Clear Button "Click"
*/
2018-01-23 12:08:37 +03:00
$('#filter-customers .clear').click(function () {
$('#filter-customers .key').val('');
instance.filterLimit = 20;
instance.filter('');
instance.resetForm();
});
/**
* Event: Filter Entry "Click"
*
* Display the customer data of the selected row.
*/
2018-01-23 12:08:37 +03:00
$(document).on('click', '.entry', function () {
if ($('#filter-customers .filter').prop('disabled')) {
return; // Do nothing when user edits a customer record.
}
2016-07-15 21:52:21 +03:00
var customerId = $(this).attr('data-id');
var customer = instance.filterResults.find(function (filterResult) {
return Number(filterResult.id) === Number(customerId);
});
instance.display(customer);
$('#filter-customers .selected').removeClass('selected');
$(this).addClass('selected');
$('#edit-customer, #delete-customer').prop('disabled', false);
});
/**
* Event: Appointment Row "Click"
*
* Display appointment data of the selected row.
*/
2018-01-23 12:08:37 +03:00
$(document).on('click', '.appointment-row', function () {
$('#customer-appointments .selected').removeClass('selected');
$(this).addClass('selected');
2016-07-15 21:52:21 +03:00
var customerId = $('#filter-customers .selected').attr('data-id');
var customer = instance.filterResults.find(function (filterResult) {
return Number(filterResult.id) === Number(customerId);
});
2016-07-15 21:52:21 +03:00
var appointmentId = $(this).attr('data-id');
var appointment = customer.appointments.find(function (customerAppointment) {
return Number(customerAppointment.id) === Number(appointmentId);
});
instance.displayAppointment(appointment);
});
/**
* Event: Add Customer Button "Click"
*/
2018-01-23 12:08:37 +03:00
$('#add-customer').click(function () {
instance.resetForm();
$('#add-edit-delete-group').hide();
$('#save-cancel-group').show();
$('.record-details').find('input, textarea').prop('readonly', false);
$('.record-details').find('select').prop('disabled', false);
$('#filter-customers button').prop('disabled', true);
$('#filter-customers .results').css('color', '#AAA');
});
/**
* Event: Edit Customer Button "Click"
*/
2018-01-23 12:08:37 +03:00
$('#edit-customer').click(function () {
$('.record-details').find('input, textarea').prop('readonly', false);
$('.record-details select').prop('disabled', false);
$('#add-edit-delete-group').hide();
$('#save-cancel-group').show();
$('#filter-customers button').prop('disabled', true);
$('#filter-customers .results').css('color', '#AAA');
});
/**
* Event: Cancel Customer Add/Edit Operation Button "Click"
*/
2018-01-23 12:08:37 +03:00
$('#cancel-customer').click(function () {
var id = $('#customer-id').val();
instance.resetForm();
if (id) {
instance.select(id, true);
}
});
/**
* Event: Save Add/Edit Customer Operation "Click"
*/
2018-01-23 12:08:37 +03:00
$('#save-customer').click(function () {
var customer = {
2016-07-15 21:52:21 +03:00
first_name: $('#first-name').val(),
last_name: $('#last-name').val(),
email: $('#email').val(),
phone_number: $('#phone-number').val(),
address: $('#address').val(),
city: $('#city').val(),
zip_code: $('#zip-code').val(),
notes: $('#notes').val(),
timezone: $('#timezone').val()
};
if ($('#customer-id').val()) {
customer.id = $('#customer-id').val();
}
if (!instance.validate()) {
return;
}
instance.save(customer);
});
/**
* Event: Delete Customer Button "Click"
*/
2018-01-23 12:08:37 +03:00
$('#delete-customer').click(function () {
2016-07-15 21:52:21 +03:00
var customerId = $('#customer-id').val();
var buttons = [
{
text: EALang.delete,
2018-01-23 12:08:37 +03:00
click: function () {
instance.delete(customerId);
$('#message_box').dialog('close');
}
},
{
text: EALang.cancel,
2018-01-23 12:08:37 +03:00
click: function () {
$('#message_box').dialog('close');
}
}
];
GeneralFunctions.displayMessageBox(EALang.delete_customer,
2018-01-23 12:08:37 +03:00
EALang.delete_record_prompt, buttons);
});
};
/**
* Save a customer record to the database (via ajax post).
*
* @param {Object} customer Contains the customer data.
*/
2018-01-23 12:08:37 +03:00
CustomersHelper.prototype.save = function (customer) {
var url = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_save_customer';
var data = {
2016-07-15 21:52:21 +03:00
csrfToken: GlobalVariables.csrfToken,
customer: JSON.stringify(customer)
};
$.post(url, data)
.done(function (response) {
Backend.displayNotification(EALang.customer_saved);
this.resetForm();
$('#filter-customers .key').val('');
this.filter('', response.id, true);
}.bind(this))
.fail(GeneralFunctions.ajaxFailureHandler);
};
/**
* Delete a customer record from database.
*
* @param {Number} id Record id to be deleted.
*/
2018-01-23 12:08:37 +03:00
CustomersHelper.prototype.delete = function (id) {
var url = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_delete_customer';
var data = {
2016-07-15 21:52:21 +03:00
csrfToken: GlobalVariables.csrfToken,
customer_id: id
};
$.post(url, data)
.done(function () {
Backend.displayNotification(EALang.customer_deleted);
this.resetForm();
this.filter($('#filter-customers .key').val());
}.bind(this))
.fail(GeneralFunctions.ajaxFailureHandler);
};
/**
* Validate customer data before save (insert or update).
*/
2018-01-23 12:08:37 +03:00
CustomersHelper.prototype.validate = function () {
$('#form-message')
.removeClass('alert-danger')
.hide();
$('.has-error').removeClass('has-error');
try {
// Validate required fields.
var missingRequired = false;
$('.required').each(function (index, requiredField) {
if ($(requiredField).val() === '') {
$(requiredField).closest('.form-group').addClass('has-error');
missingRequired = true;
}
});
if (missingRequired) {
throw new Error(EALang.fields_are_required);
}
// Validate email address.
if (!GeneralFunctions.validateEmail($('#email').val())) {
$('#email').closest('.form-group').addClass('has-error');
throw new Error(EALang.invalid_email);
}
return true;
} catch (error) {
$('#form-message')
.addClass('alert-danger')
.text(error.message)
.show();
return false;
}
};
/**
* Bring the customer form back to its initial state.
*/
2018-01-23 12:08:37 +03:00
CustomersHelper.prototype.resetForm = function () {
$('.record-details').find('input, textarea').val('');
$('.record-details').find('input, textarea').prop('readonly', true);
$('.record-details').find('select').prop('disabled', true);
$('#customer-appointments').empty();
2020-05-05 20:35:33 +03:00
$('#appointment-details').toggleClass('d-none', true).empty();
$('#edit-customer, #delete-customer').prop('disabled', true);
$('#add-edit-delete-group').show();
$('#save-cancel-group').hide();
$('.record-details .has-error').removeClass('has-error');
$('.record-details #form-message').hide();
$('#filter-customers button').prop('disabled', false);
$('#filter-customers .selected').removeClass('selected');
$('#filter-customers .results').css('color', '');
};
/**
* Display a customer record into the form.
*
* @param {Object} customer Contains the customer record data.
*/
2018-01-23 12:08:37 +03:00
CustomersHelper.prototype.display = function (customer) {
$('#customer-id').val(customer.id);
$('#first-name').val(customer.first_name);
$('#last-name').val(customer.last_name);
$('#email').val(customer.email);
$('#phone-number').val(customer.phone_number);
$('#address').val(customer.address);
$('#city').val(customer.city);
$('#zip-code').val(customer.zip_code);
$('#notes').val(customer.notes);
$('#timezone').val(customer.timezone);
$('#customer-appointments').empty();
customer.appointments.forEach(function (appointment) {
if (GlobalVariables.user.role_slug === Backend.DB_SLUG_PROVIDER && parseInt(appointment.id_users_provider) !== GlobalVariables.user.id) {
return;
}
if (GlobalVariables.user.role_slug === Backend.DB_SLUG_SECRETARY && GlobalVariables.secretaryProviders.indexOf(appointment.id_users_provider) === -1) {
return;
}
2016-07-15 21:52:21 +03:00
var start = GeneralFunctions.formatDate(Date.parse(appointment.start_datetime), GlobalVariables.dateFormat, true);
var end = GeneralFunctions.formatDate(Date.parse(appointment.end_datetime), GlobalVariables.dateFormat, true);
$('<div/>', {
'class': 'appointment-row',
'data-id': appointment.id,
'html': [
$('<span/>', {
'text': start + ' - ' + end
}),
$('<br/>'),
$('<span/>', {
'text': appointment.service.name
}),
$('<span/>', {
'text': appointment.provider.first_name + ' ' + appointment.provider.last_name
})
]
})
.appendTo('#customer-appointments');
});
$('#appointment-details').empty();
};
/**
* Filter customer records.
*
* @param {String} key This key string is used to filter the customer records.
* @param {Number} selectId Optional, if set then after the filter operation the record with the given
* ID will be selected (but not displayed).
* @param {Boolean} display Optional (false), if true then the selected record will be displayed on the form.
*/
2018-01-23 12:08:37 +03:00
CustomersHelper.prototype.filter = function (key, selectId, display) {
display = display || false;
var url = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_filter_customers';
var data = {
2016-07-15 21:52:21 +03:00
csrfToken: GlobalVariables.csrfToken,
key: key,
limit: this.filterLimit
2016-07-15 21:52:21 +03:00
};
$.post(url, data)
.done(function (response) {
this.filterResults = response;
$('#filter-customers .results').empty();
response.forEach(function (customer) {
$('#filter-customers .results')
.append(this.getFilterHtml(customer))
.append($('<hr/>'));
}.bind(this));
if (!response.length) {
$('#filter-customers .results').append(
$('<em/>', {
'text': EALang.no_records_found
})
);
} else if (response.length === this.filterLimit) {
$('<button/>', {
'type': 'button',
'class': 'btn btn-block btn-light load-more text-center',
'text': EALang.load_more,
'click': function () {
this.filterLimit += 20;
this.filter(key, selectId, display);
}.bind(this)
})
.appendTo('#filter-customers .results');
}
if (selectId) {
this.select(selectId, display);
}
}.bind(this))
.fail(GeneralFunctions.ajaxFailureHandler);
};
/**
* Get the filter results row HTML code.
*
* @param {Object} customer Contains the customer data.
*
* @return {String} Returns the record HTML code.
*/
2018-01-23 12:08:37 +03:00
CustomersHelper.prototype.getFilterHtml = function (customer) {
var name = customer.first_name + ' ' + customer.last_name;
var info = customer.email;
info = customer.phone_number ? info + ', ' + customer.phone_number : info;
return $('<div/>', {
'class': 'customer-row entry',
'data-id': customer.id,
'html': [
$('<strong/>', {
'text': name
}),
$('<br/>'),
$('<span/>', {
'text': info
}),
$('<br/>'),
]
});
};
/**
* 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.
*
* @param {Number} id The record id to be selected from the filter results.
* @param {Boolean} display Optional (false), if true then the method will display the record
* on the form.
*/
2018-01-23 12:08:37 +03:00
CustomersHelper.prototype.select = function (id, display) {
2016-10-10 19:29:48 +03:00
display = display || false;
$('#filter-customers .selected').removeClass('selected');
$('#filter-customers .entry[data-id="' + id + '"]').addClass('selected');
if (display) {
var customer = this.filterResults.find(function (filterResult) {
return Number(filterResult.id) === Number(id);
});
this.display(customer);
$('#edit-customer, #delete-customer').prop('disabled', false);
}
};
/**
* Display appointment details on customers backend page.
*
* @param {Object} appointment Appointment data
*/
2018-01-23 12:08:37 +03:00
CustomersHelper.prototype.displayAppointment = function (appointment) {
2016-07-15 21:52:21 +03:00
var start = GeneralFunctions.formatDate(Date.parse(appointment.start_datetime), GlobalVariables.dateFormat, true);
var end = GeneralFunctions.formatDate(Date.parse(appointment.end_datetime), GlobalVariables.dateFormat, true);
var timezone = GlobalVariables.timezones[GlobalVariables.user.timezone];
2020-06-19 14:56:02 +03:00
$('#appointment-details').empty();
$('<div/>', {
'html': [
$('<strong/>', {
'text': appointment.service.name
}),
$('<br/>'),
$('<span/>', {
'text': appointment.provider.first_name + ' ' + appointment.provider.last_name
}),
$('<br/>'),
$('<span/>', {
2020-06-19 14:56:02 +03:00
'text': start + ' - ' + end
}),
$('<br/>'),
$('<span/>', {
'text': EALang.timezone + ': ' + timezone
}),
$('<br/>')
]
})
.appendTo('#appointment-details');
$('#appointment-details').removeClass('d-none');
};
window.CustomersHelper = CustomersHelper;
})();