2016-04-02 15:59:31 +03:00
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Easy!Appointments - Open Source Web Scheduler
|
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2017-01-31 09:35:34 +03:00
|
|
|
* @copyright Copyright (c) 2013 - 2017, Alex Tselegidis
|
2016-04-02 15:59:31 +03:00
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link http://easyappointments.org
|
|
|
|
* @since v1.0.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2016-05-15 13:17:12 +03:00
|
|
|
/**
|
|
|
|
* CustomersHelper Class
|
|
|
|
*
|
|
|
|
* This class contains the methods that are used in the backend customers page.
|
|
|
|
*
|
|
|
|
* @class CustomersHelper
|
|
|
|
*/
|
2016-04-02 15:59:31 +03:00
|
|
|
function CustomersHelper() {
|
|
|
|
this.filterResults = {};
|
2017-01-21 22:02:38 +03:00
|
|
|
}
|
2016-04-02 15:59:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Binds the default event handlers of the backend customers page.
|
|
|
|
*/
|
|
|
|
CustomersHelper.prototype.bindEventHandlers = function() {
|
|
|
|
var instance = this;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Filter Customers Form "Submit"
|
|
|
|
*/
|
|
|
|
$('#filter-customers form').submit(function(event) {
|
|
|
|
var key = $('#filter-customers .key').val();
|
2016-05-20 10:08:41 +03:00
|
|
|
$('#filter-customers .selected').removeClass('selected');
|
2016-04-02 15:59:31 +03:00
|
|
|
instance.resetForm();
|
|
|
|
instance.filter(key);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Filter Customers Clear Button "Click"
|
|
|
|
*/
|
|
|
|
$('#filter-customers .clear').click(function() {
|
|
|
|
$('#filter-customers .key').val('');
|
|
|
|
instance.filter('');
|
|
|
|
instance.resetForm();
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
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.
|
|
|
|
*/
|
2016-05-20 10:08:41 +03:00
|
|
|
$(document).on('click', '.entry', function() {
|
2016-04-02 15:59:31 +03:00
|
|
|
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 = {};
|
2016-04-02 15:59:31 +03:00
|
|
|
$.each(instance.filterResults, function(index, item) {
|
|
|
|
if (item.id == customerId) {
|
|
|
|
customer = item;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
instance.display(customer);
|
2016-05-20 10:08:41 +03:00
|
|
|
$('#filter-customers .selected').removeClass('selected');
|
|
|
|
$(this).addClass('selected');
|
2016-04-02 15:59:31 +03:00
|
|
|
$('#edit-customer, #delete-customer').prop('disabled', false);
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Appointment Row "Click"
|
|
|
|
*
|
|
|
|
* Display appointment data of the selected row.
|
|
|
|
*/
|
|
|
|
$(document).on('click', '.appointment-row', function() {
|
2016-05-20 10:08:41 +03:00
|
|
|
$('#customer-appointments .selected').removeClass('selected');
|
|
|
|
$(this).addClass('selected');
|
2016-04-02 15:59:31 +03:00
|
|
|
|
2016-07-15 21:52:21 +03:00
|
|
|
var customerId = $('#filter-customers .selected').attr('data-id');
|
|
|
|
var appointmentId = $(this).attr('data-id');
|
|
|
|
var appointment = {};
|
2016-04-02 15:59:31 +03:00
|
|
|
|
|
|
|
$.each(instance.filterResults, function(index, c) {
|
|
|
|
if (c.id === customerId) {
|
|
|
|
$.each(c.appointments, function(index, a) {
|
|
|
|
if (a.id == appointmentId) {
|
|
|
|
appointment = a;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
instance.displayAppointment(appointment);
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Add Customer Button "Click"
|
|
|
|
*/
|
|
|
|
$('#add-customer').click(function() {
|
|
|
|
instance.resetForm();
|
|
|
|
$('#add-edit-delete-group').hide();
|
|
|
|
$('#save-cancel-group').show();
|
2016-05-20 10:08:41 +03:00
|
|
|
$('.record-details').find('input, textarea').prop('readonly', false);
|
2016-04-02 15:59:31 +03:00
|
|
|
|
|
|
|
$('#filter-customers button').prop('disabled', true);
|
|
|
|
$('#filter-customers .results').css('color', '#AAA');
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Edit Customer Button "Click"
|
|
|
|
*/
|
|
|
|
$('#edit-customer').click(function() {
|
2016-05-20 10:08:41 +03:00
|
|
|
$('.record-details').find('input, textarea').prop('readonly', false);
|
2016-04-02 15:59:31 +03:00
|
|
|
$('#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"
|
|
|
|
*/
|
|
|
|
$('#cancel-customer').click(function() {
|
|
|
|
var id = $('#customer-id').val();
|
|
|
|
instance.resetForm();
|
|
|
|
if (id != '') {
|
|
|
|
instance.select(id, true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Save Add/Edit Customer Operation "Click"
|
|
|
|
*/
|
|
|
|
$('#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()
|
2016-04-02 15:59:31 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
if ($('#customer-id').val() != '') {
|
|
|
|
customer.id = $('#customer-id').val();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!instance.validate(customer)) return;
|
|
|
|
|
|
|
|
instance.save(customer);
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Delete Customer Button "Click"
|
|
|
|
*/
|
|
|
|
$('#delete-customer').click(function() {
|
2016-07-15 21:52:21 +03:00
|
|
|
var customerId = $('#customer-id').val();
|
2017-06-16 09:51:05 +03:00
|
|
|
var buttons = [
|
|
|
|
{
|
2017-09-11 17:09:15 +03:00
|
|
|
text: EALang.delete,
|
2017-06-16 09:51:05 +03:00
|
|
|
click: function() {
|
|
|
|
instance.delete(customerId);
|
|
|
|
$('#message_box').dialog('close');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 17:09:15 +03:00
|
|
|
text: EALang.cancel,
|
2017-06-16 09:51:05 +03:00
|
|
|
click: function() {
|
|
|
|
$('#message_box').dialog('close');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
];
|
2016-04-02 15:59:31 +03:00
|
|
|
|
2017-09-11 17:09:15 +03:00
|
|
|
GeneralFunctions.displayMessageBox(EALang.delete_customer,
|
|
|
|
EALang.delete_record_prompt, buttons);
|
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
|
|
|
*/
|
|
|
|
CustomersHelper.prototype.save = function(customer) {
|
2016-07-15 21:52:21 +03:00
|
|
|
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_save_customer';
|
|
|
|
var postData = {
|
|
|
|
csrfToken: GlobalVariables.csrfToken,
|
|
|
|
customer: JSON.stringify(customer)
|
|
|
|
};
|
2016-04-02 15:59:31 +03:00
|
|
|
|
|
|
|
$.post(postUrl, postData, function(response) {
|
2016-05-15 13:17:12 +03:00
|
|
|
if (!GeneralFunctions.handleAjaxExceptions(response)) {
|
|
|
|
return;
|
|
|
|
}
|
2016-04-02 15:59:31 +03:00
|
|
|
|
2017-09-11 17:09:15 +03:00
|
|
|
Backend.displayNotification(EALang.customer_saved);
|
2016-04-02 15:59:31 +03:00
|
|
|
this.resetForm();
|
|
|
|
$('#filter-customers .key').val('');
|
|
|
|
this.filter('', response.id, true);
|
|
|
|
}.bind(this), 'json').fail(GeneralFunctions.ajaxFailureHandler);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
*/
|
|
|
|
CustomersHelper.prototype.delete = function(id) {
|
2016-07-15 21:52:21 +03:00
|
|
|
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_delete_customer';
|
|
|
|
var postData = {
|
|
|
|
csrfToken: GlobalVariables.csrfToken,
|
|
|
|
customer_id: id
|
|
|
|
};
|
2016-04-02 15:59:31 +03:00
|
|
|
|
|
|
|
$.post(postUrl, postData, function(response) {
|
2016-05-15 13:17:12 +03:00
|
|
|
if (!GeneralFunctions.handleAjaxExceptions(response)) {
|
|
|
|
return;
|
|
|
|
}
|
2016-04-02 15:59:31 +03:00
|
|
|
|
2017-09-11 17:09:15 +03:00
|
|
|
Backend.displayNotification(EALang.customer_deleted);
|
2016-04-02 15:59:31 +03:00
|
|
|
this.resetForm();
|
|
|
|
this.filter($('#filter-customers .key').val());
|
|
|
|
}.bind(this), 'json').fail(GeneralFunctions.ajaxFailureHandler);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate customer data before save (insert or update).
|
|
|
|
*
|
2016-05-15 13:17:12 +03:00
|
|
|
* @param {Object} customer Contains the customer data.
|
2016-04-02 15:59:31 +03:00
|
|
|
*/
|
|
|
|
CustomersHelper.prototype.validate = function(customer) {
|
|
|
|
$('#form-message').hide();
|
|
|
|
$('.required').css('border', '');
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Validate required fields.
|
|
|
|
var missingRequired = false;
|
|
|
|
|
|
|
|
$('.required').each(function() {
|
|
|
|
if ($(this).val() == '') {
|
|
|
|
$(this).css('border', '2px solid red');
|
|
|
|
missingRequired = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (missingRequired) {
|
2017-09-11 17:09:15 +03:00
|
|
|
throw EALang.fields_are_required;
|
2016-04-02 15:59:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate email address.
|
|
|
|
if (!GeneralFunctions.validateEmail($('#email').val())) {
|
|
|
|
$('#email').css('border', '2px solid red');
|
2017-09-11 17:09:15 +03:00
|
|
|
throw EALang.invalid_email;
|
2016-04-02 15:59:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} catch(exc) {
|
|
|
|
$('#form-message').text(exc).show();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bring the customer form back to its initial state.
|
|
|
|
*/
|
|
|
|
CustomersHelper.prototype.resetForm = function() {
|
2016-05-20 10:08:41 +03:00
|
|
|
$('.record-details').find('input, textarea').val('');
|
|
|
|
$('.record-details').find('input, textarea').prop('readonly', true);
|
2016-04-02 15:59:31 +03:00
|
|
|
|
|
|
|
$('#customer-appointments').html('');
|
|
|
|
$('#appointment-details').html('');
|
|
|
|
$('#edit-customer, #delete-customer').prop('disabled', true);
|
|
|
|
$('#add-edit-delete-group').show();
|
|
|
|
$('#save-cancel-group').hide();
|
|
|
|
|
2016-05-20 10:08:41 +03:00
|
|
|
$('.record-details .required').css('border', '');
|
|
|
|
$('.record-details #form-message').hide();
|
2016-04-02 15:59:31 +03:00
|
|
|
|
|
|
|
$('#filter-customers button').prop('disabled', false);
|
2016-05-20 10:08:41 +03:00
|
|
|
$('#filter-customers .selected').removeClass('selected');
|
2016-04-02 15:59:31 +03:00
|
|
|
$('#filter-customers .results').css('color', '');
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
*/
|
|
|
|
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);
|
|
|
|
|
|
|
|
$('#customer-appointments').data('jsp').destroy();
|
|
|
|
$('#customer-appointments').empty();
|
|
|
|
$.each(customer.appointments, function(index, 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 html =
|
2016-04-02 15:59:31 +03:00
|
|
|
'<div class="appointment-row" data-id="' + appointment.id + '">' +
|
|
|
|
start + ' - ' + end + '<br>' +
|
|
|
|
appointment.service.name + ', ' +
|
|
|
|
appointment.provider.first_name + ' ' + appointment.provider.last_name +
|
|
|
|
'</div>';
|
|
|
|
$('#customer-appointments').append(html);
|
|
|
|
});
|
|
|
|
$('#customer-appointments').jScrollPane({ mouseWheelSpeed: 70 });
|
|
|
|
|
|
|
|
$('#appointment-details').empty();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter customer records.
|
|
|
|
*
|
2016-05-15 13:17:12 +03:00
|
|
|
* @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.
|
2016-04-02 15:59:31 +03:00
|
|
|
*/
|
|
|
|
CustomersHelper.prototype.filter = function(key, selectId, display) {
|
|
|
|
display = display || false;
|
|
|
|
|
2016-07-15 21:52:21 +03:00
|
|
|
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_filter_customers';
|
|
|
|
var postData = {
|
|
|
|
csrfToken: GlobalVariables.csrfToken,
|
|
|
|
key: key
|
|
|
|
};
|
2016-04-02 15:59:31 +03:00
|
|
|
|
|
|
|
$.post(postUrl, postData, function(response) {
|
2016-05-15 13:17:12 +03:00
|
|
|
if (!GeneralFunctions.handleAjaxExceptions(response)) {
|
|
|
|
return;
|
|
|
|
}
|
2016-04-02 15:59:31 +03:00
|
|
|
|
|
|
|
this.filterResults = response;
|
|
|
|
|
|
|
|
$('#filter-customers .results').data('jsp').destroy();
|
|
|
|
$('#filter-customers .results').html('');
|
|
|
|
$.each(response, function(index, customer) {
|
|
|
|
var html = this.getFilterHtml(customer);
|
|
|
|
$('#filter-customers .results').append(html);
|
|
|
|
}.bind(this));
|
|
|
|
$('#filter-customers .results').jScrollPane({ mouseWheelSpeed: 70 });
|
|
|
|
|
|
|
|
if (response.length == 0) {
|
2017-09-11 17:09:15 +03:00
|
|
|
$('#filter-customers .results').html('<em>' + EALang.no_records_found + '</em>');
|
2016-04-02 15:59:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (selectId != undefined) {
|
|
|
|
this.select(selectId, display);
|
|
|
|
}
|
|
|
|
|
|
|
|
}.bind(this), 'json').fail(GeneralFunctions.ajaxFailureHandler);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
*/
|
|
|
|
CustomersHelper.prototype.getFilterHtml = function(customer) {
|
|
|
|
var name = customer.first_name + ' ' + customer.last_name;
|
|
|
|
var info = customer.email;
|
|
|
|
info = (customer.phone_number != '' && customer.phone_number != null)
|
|
|
|
? info + ', ' + customer.phone_number : info;
|
|
|
|
|
|
|
|
var html =
|
2016-05-20 10:08:41 +03:00
|
|
|
'<div class="entry" data-id="' + customer.id + '">' +
|
2016-04-02 15:59:31 +03:00
|
|
|
'<strong>' +
|
|
|
|
name +
|
|
|
|
'</strong><br>' +
|
|
|
|
info +
|
|
|
|
'</div><hr>';
|
|
|
|
|
|
|
|
return html;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
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.
|
|
|
|
* @param {Boolean} display Optional (false), if true then the method will display the record
|
2016-04-02 15:59:31 +03:00
|
|
|
* on the form.
|
|
|
|
*/
|
|
|
|
CustomersHelper.prototype.select = function(id, display) {
|
2016-10-10 19:29:48 +03:00
|
|
|
display = display || false;
|
2016-04-02 15:59:31 +03:00
|
|
|
|
2016-05-20 10:08:41 +03:00
|
|
|
$('#filter-customers .selected').removeClass('selected');
|
2016-04-02 15:59:31 +03:00
|
|
|
|
2016-05-20 10:08:41 +03:00
|
|
|
$('#filter-customers .entry').each(function() {
|
2016-04-02 15:59:31 +03:00
|
|
|
if ($(this).attr('data-id') == id) {
|
2016-05-20 10:08:41 +03:00
|
|
|
$(this).addClass('selected');
|
2016-04-02 15:59:31 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (display) {
|
|
|
|
$.each(this.filterResults, function(index, customer) {
|
|
|
|
if (customer.id == id) {
|
|
|
|
this.display(customer);
|
|
|
|
$('#edit-customer, #delete-customer').prop('disabled', false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display appointment details on customers backend page.
|
|
|
|
*
|
2016-05-15 13:17:12 +03:00
|
|
|
* @param {Object} appointment Appointment data
|
2016-04-02 15:59:31 +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);
|
2016-04-02 15:59:31 +03:00
|
|
|
|
|
|
|
var html =
|
|
|
|
'<div>' +
|
|
|
|
'<strong>' + appointment.service.name + '</strong><br>' +
|
|
|
|
appointment.provider.first_name + ' ' + appointment.provider.last_name + '<br>' +
|
|
|
|
start + ' - ' + end + '<br>' +
|
|
|
|
'</div>';
|
|
|
|
|
|
|
|
$('#appointment-details').html(html);
|
|
|
|
};
|
|
|
|
|
|
|
|
window.CustomersHelper = CustomersHelper;
|
|
|
|
})();
|