Newer
Older

Patrick Müller
committed
/**
* Control for selecting a user / address / contact data for ERP entities (e.g. invoice customer).
*
* @module package/quiqqer/erp/bin/backend/controls/userData/UserData
* @author www.pcsg.de (Henning Leutz)
* @author www.pcsg.de (Patrick Müller)
*
* @event onChange [userData, this]
*/
define('package/quiqqer/erp/bin/backend/controls/userData/UserData', [
'qui/QUI',
'qui/controls/Control',
'package/quiqqer/customer/bin/backend/controls/customer/address/Window',
'package/quiqqer/erp/bin/backend/controls/userData/ContactEmailSelectWindow',
'Users',
'Locale',
'Ajax',
'Mustache',
'text!package/quiqqer/erp/bin/backend/controls/userData/UserData.html',
'css!package/quiqqer/erp/bin/backend/controls/userData/UserData.css'
], function(QUI, QUIControl, AddressWindow, ContactEmailSelectWindow, Users, QUILocale, QUIAjax, Mustache, template) {
'use strict';

Patrick Müller
committed
const pkg = 'quiqqer/erp';
const fields = [
'userId',
'addressId',
'contactPerson',
'contactEmail',
'suffix',
'firstname',
'lastname',
'name',
'company',
'street_no',
'zip',
'city',
'country',
'isCommercial'
];
return new Class({
Extends: QUIControl,
Type: 'package/quiqqer/erp/bin/backend/controls/userData/UserData',

Patrick Müller
committed
Binds: [
'toggleExtras',
'editCustomer',
'$setDataByUserId',

Patrick Müller
committed
'$onInject',
'$fireChange',
'setValue'

Patrick Müller
committed
],
options: {

Patrick Müller
committed
addressId: false,
editContactPerson: true,

Patrick Müller
committed

Patrick Müller
committed
contactPerson: '',
contactEmail: '',
company: false,
street_no: false,
zip: false,
city: false,

Patrick Müller
committed
labelUser: QUILocale.get(pkg, 'UserData.tpl.labelCustomer'),

Patrick Müller
committed
userSelectWindowControl: 'package/quiqqer/customer/bin/backend/controls/customer/Select',
userPanelControl: 'package/quiqqer/customer/bin/backend/Handler'

Patrick Müller
committed
},

Patrick Müller
committed
this.parent(options);
this.addEvents({
onInject: this.$onInject
});
this.$CustomerSelect = null;
this.$ContactPerson = null;

Patrick Müller
committed
this.$BtnContactPersonSelect = null;

Patrick Müller
committed
this.$BtnContactEmailSelect = null;
this.$RowContactPerson = null;
this.$Extras = null;
this.$Company = null;
this.$Street = null;
this.$Zip = null;
this.$City = null;
this.$Table = null;
this.$AddressRow = null;

Patrick Müller
committed
this.$AddressField = null;

Patrick Müller
committed
this.$extrasAreOpen = false;

Patrick Müller
committed

Patrick Müller
committed
this.$setValues = false;
this.$Panel = null;
},
/**
* Create the DOMNoe Element
* @returns {*}
*/

Patrick Müller
committed
const labelUser = this.getAttribute('labelUser');
function ignoreAutoFill(node)
{
node.role = 'presentation';
node.autocomplete = 'off';
}

Patrick Müller
committed
this.$Elm = new Element('div', {
html: Mustache.render(template, {
labelTitle: labelUser,
labelCustomer: labelUser,
labelAddress: QUILocale.get('quiqqer/core', 'address'),
labelCompany: QUILocale.get('quiqqer/core', 'company'),
labelStreet: QUILocale.get('quiqqer/core', 'street'),
labelZip: QUILocale.get('quiqqer/core', 'zip'),
labelCity: QUILocale.get('quiqqer/core', 'city'),
labelExtra: QUILocale.get(pkg, 'UserData.btn.extra'),
labelUserEdit: QUILocale.get(pkg, 'UserData.btn.userEdit', {labelUser}),
labelContactPerson: QUILocale.get(pkg, 'UserData.tpl.labelContactPerson'),
labelContactEmail: QUILocale.get(pkg, 'UserData.tpl.labelContactEmail'),

Patrick Müller
committed
placeholderContactPerson: QUILocale.get(pkg, 'UserData.tpl.placeholderContactPerson'),
placeholderContactEmail: QUILocale.get(pkg, 'UserData.tpl.placeholderContactEmail'),

Patrick Müller
committed
userSelectWindowControl: this.getAttribute('userSelectWindowControl')

Patrick Müller
committed
})
});
// Extras (show address fields)
this.$Extras = this.$Elm.getElement('.quiqqer-erp-userdata-address-opener');
this.$Extras.addEvent('click', this.toggleExtras);
// Customer edit (button that opens customer / user panel)
this.$CustomerEdit = this.$Elm.getElement('.quiqqer-erp-userdata-address-userEdit');
this.$CustomerEdit.addEvent('click', this.editCustomer);
// Contact person
this.$RowContactPerson = this.$Elm.getElement('.quiqqer-erp-userdata-row-contactPerson');
this.$ContactPerson = this.$Elm.getElement('input[name="contact_person"]');

Patrick Müller
committed
this.$BtnContactPersonSelect = this.$Elm.getElement('button[name="select-contact-id-address"]');
this.$ContactPerson.addEvent('keyup', () => {
this.setAttribute('contactPerson', this.$ContactPerson.value);
this.$fireChange();
});
this.$BtnContactPersonSelect.addEvent('click', () => {
new AddressWindow({
autoclose: false,
userId: this.getAttribute('userId'),
events: {

Patrick Müller
committed
onSubmit: (Win, addressId, address) => {
Win.close();
this.$setContactPersonByAddress(address);
this.$fireChange();
}
}
}).open();
});
// Contact email

Patrick Müller
committed
this.$RowContactEmail = this.$Elm.getElement('.quiqqer-erp-userdata-row-contactEmail');
this.$RowContactEmail.setStyle('display', 'none');

Patrick Müller
committed
this.$ContactEmail = this.$Elm.getElement('input[name="contact_email"]');
this.$BtnContactEmailSelect = this.$Elm.getElement('button[name="select-contact-email"]');
this.$ContactEmail.addEvent('keyup', () => {
this.setAttribute('contactEmail', this.$ContactEmail.value);
this.$fireChange();
});
this.$BtnContactEmailSelect.addEvent('click', () => {
new ContactEmailSelectWindow({
autoclose: false,
userId: this.getAttribute('userId'),
events: {

Patrick Müller
committed
onSubmit: (emailAddress, Win) => {
Win.close();
this.$ContactEmail.value = emailAddress;
this.setAttribute('contactEmail', emailAddress);
this.$fireChange();
}
}
}).open();
});
// Customer address
this.$Company = this.$Elm.getElement('[name="company"]');
this.$Street = this.$Elm.getElement('[name="street_no"]');
this.$Zip = this.$Elm.getElement('[name="zip"]');
this.$City = this.$Elm.getElement('[name="city"]');
ignoreAutoFill(this.$Company);
ignoreAutoFill(this.$Street);
ignoreAutoFill(this.$Zip);
ignoreAutoFill(this.$City);
this.$Table = this.$Elm.getElement('.quiqqer-erp-userdata--customer');
this.$rows = this.$Table.getElements('.closable');
this.$AddressRow = this.$Table.getElement('.address-row');
this.$AddressField = this.$Table.getElement('[name="address"]');

Patrick Müller
committed
this.$AddressDisplay = null;

Patrick Müller
committed
this.$AddressField.type = 'hidden';
this.$AddressDisplay = new Element('input', {

Patrick Müller
committed
disabled: true
}).inject(this.$AddressField, 'after');
return this.$Elm;
},
/**
* Return the data value
*
* @return {Object}
*/

Patrick Müller
committed
const result = {};
fields.forEach((field) => {
if (this.getAttribute(field)) {
result[field] = this.getAttribute(field);

Patrick Müller
committed
} else {
result[field] = false;

Patrick Müller
committed
}
});
return result;
},
getAddress: function() {
return {
id: this.getAttribute('addressId'),
contactEmail: this.getAttribute('contactEmail'),
salutation: this.getAttribute('salutation'),
firstname: this.getAttribute('firstname'),
lastname: this.getAttribute('lastname'),
city: this.getAttribute('city'),
zip: this.getAttribute('zip'),
company: this.getAttribute('company'),
street_no: this.getAttribute('street_no'),
country: this.getAttribute('country')
};
},

Patrick Müller
committed
/**
* Set the complete data values
*
* @param {Object} data

Patrick Müller
committed
*/

Patrick Müller
committed
if (this.$CustomerEdit) {
this.$CustomerEdit.setStyle('display', 'inline');
}
let addressPromise = Promise.resolve();

Patrick Müller
committed

Patrick Müller
committed
if ('userId' in data && data.userId) {

Patrick Müller
committed
if (this.$CustomerSelect) {

Patrick Müller
committed
this.$setValues = true;
this.$CustomerSelect.addItem(data.userId);

Patrick Müller
committed
}
dataPromise = this.$setDataByUserId(data.userId);

Patrick Müller
committed
}

Patrick Müller
committed
if ('addressId' in data && data.addressId) {
addressPromise = this.setAddressId(data.addressId);
}
return dataPromise.then(addressPromise).then(() => {

Patrick Müller
committed
fields.forEach((field) => {
if (typeof data[field] !== 'undefined') {
this.setAttribute(field, data[field]);
}
});
this.$refreshValues();
});
},
/**
* Refresh the displayed address values.
*
* @return {void}
*/
$refreshValues: function() {
const checkVal = function(val) {

Patrick Müller
committed
return !(!val || val === '' || val === 'false');
};
if (checkVal(this.getAttribute('company'))) {
this.$Company.value = this.getAttribute('company');
}
if (checkVal(this.getAttribute('street_no'))) {
this.$Street.value = this.getAttribute('street_no');
}
if (checkVal(this.getAttribute('zip'))) {
this.$Zip.value = this.getAttribute('zip');
}
if (checkVal(this.getAttribute('city'))) {
this.$City.value = this.getAttribute('city');
}
this.$AddressDisplay.value = this.$getAddressLabel();
if (this.getAttribute('isCommercial')) {
if (this.getAttribute('contactPerson') !== false) {

Patrick Müller
committed
this.$ContactPerson.value = this.getAttribute('contactPerson');
} else {
if (this.getAttribute('contact_person') !== false) {
this.$ContactPerson.value = this.getAttribute('contact_person');
}

Patrick Müller
committed
}
this.$RowContactPerson.setStyle('display', null);
} else {
this.$RowContactPerson.setStyle('display', 'none');
}
if (this.getAttribute('contactEmail') !== false) {

Patrick Müller
committed
this.$ContactEmail.value = this.getAttribute('contactEmail');
} else {
if (this.getAttribute('contact_email') !== false) {
this.$ContactEmail.value = this.getAttribute('contact_email');
}

Patrick Müller
committed
}

Patrick Müller
committed
if (!this.getAttribute('userId')) {
this.$RowContactEmail.setStyle('display', 'none');
} else {
this.$RowContactEmail.setStyle('display', null);
}

Patrick Müller
committed
},
/**
* Get address label.
*
* @param {Object} [address] - Build address label based on given address; if omitted, use attributes.
* @return {string}
*/
$getAddressLabel: function(address) {

Patrick Müller
committed
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
const getVal = (key) => {
let val;
if (address) {
val = key in address ? address.key : false;
} else {
val = this.getAttribute(key);
}
if (!val || val === '' || val === 'false') {
return false;
}
return val;
};
const addressLabelParts = [];
const company = getVal('company');
if (company) {
addressLabelParts.push(company);
}
const streetNo = getVal('street_no');
if (streetNo) {
addressLabelParts.push(streetNo);
}
const zip = getVal('zip');
if (zip) {
addressLabelParts.push(zip);
}
const city = getVal('city');
if (city) {
addressLabelParts.push(city);
}
return addressLabelParts.join(', ');
},
/**
* Set address data via user id.
*
* @param userId
* @return {Promise}
*/

Patrick Müller
committed
this.$oldUserId = this.getAttribute('userId');
//this.$clearData();

Patrick Müller
committed
this.setAttribute('userId', userId);
this.setAttribute('userUuid', userId);

Patrick Müller
committed
if (this.$CustomerEdit) {
this.$CustomerEdit.setStyle('display', 'inline');
}
if (!this.$Elm) {
return Promise.resolve();
}
if (!userId || userId === '') {
return Promise.resolve();
}

Patrick Müller
committed
this.$BtnContactPersonSelect.disabled = true;
this.$BtnContactEmailSelect.disabled = true;

Patrick Müller
committed
return this.$getUser().then((User) => {
if (!User) {
return;
}
this.setAttribute('isCommercial', 0);
if (parseInt(User.getAttribute('quiqqer.erp.isNettoUser')) === 1 || this.getAttribute('company')) {
this.setAttribute('isCommercial', 1);
}

Patrick Müller
committed
return this.getAddressList(User).then((addresses) => {
return [User, addresses];
});
}).then((result) => {
if (!result) {
return;
}
const addresses = result[1];
if (!addresses.length) {
this.$AddressRow.setStyle('display', 'none');
return;
}
let defaultAddress = false;
for (let i = 0; i < addresses.length; i++) {
if (addresses[i].default) {
defaultAddress = addresses[i];
break;
}
}
if (!defaultAddress) {
defaultAddress = addresses[0];
}

Patrick Müller
committed
// Set address data
this.$setDataByAddress(defaultAddress);
}).then((contactEmailAddress) => {
if (contactEmailAddress && !this.$setValues) {
this.$ContactEmail.value = contactEmailAddress;
this.setAttribute('contactEmail', contactEmailAddress);
}
}).then(() => {

Patrick Müller
committed
this.$BtnContactPersonSelect.disabled = false;
this.$BtnContactEmailSelect.disabled = false;

Patrick Müller
committed
}).then(() => {
this.$fireChange();
}).catch((err) => {
console.error(err);

Patrick Müller
committed
});
},
/**
* Sets data by specific address.
*

Patrick Müller
committed
* @return {void}
*/

Patrick Müller
committed
this.setAttributes(address);
this.$AddressField.value = address.id;
this.setAttribute('id', address.id);

Patrick Müller
committed
this.$refreshValues();
this.$AddressRow.setStyle('display', null);
},
/**
* Clears all fields.
*
* @return {void}
*/

Patrick Müller
committed
this.$Company.set('value', '');
this.$Street.set('value', '');
this.$Zip.set('value', '');
this.$City.set('value', '');
this.$AddressRow.setStyle('display', 'none');

Patrick Müller
committed
this.$BtnContactPersonSelect.disabled = true;

Patrick Müller
committed
this.$BtnContactEmailSelect.disabled = true;
fields.forEach((field) => {
this.setAttribute(field, null);
});
},
/**

Patrick Müller
committed
*
* @param {String|Number} addressId
* @return {Promise}
*/

Patrick Müller
committed
var self = this;

Patrick Müller
committed
if (self.getAttribute('userId') === '') {
return Promise.resolve([]);
}
QUIAjax.get('ajax_users_address_get', function(address) {

Patrick Müller
committed
if (!('id' in address)) {
address.id = addressId;

Patrick Müller
committed
}

Patrick Müller
committed
self.$setDataByAddress(address);
self.$fireChange();
resolve();

Patrick Müller
committed
}, {
uid: self.getAttribute('userId'),
aid: addressId
});
});
},
/**
* Return the loaded user object.
*
* @returns {Promise}
*/

Patrick Müller
committed
const userId = this.getAttribute('userId');
if (!userId || userId === '') {
return Promise.reject();
}
const User = Users.get(userId);
if (User.isLoaded()) {

Patrick Müller
committed
return Promise.resolve(User);
}
return User.load();
},
/**
* Get user address list.
*
* @param User
* @return {Promise}
*/

Patrick Müller
committed
return new Promise((resolve, reject) => {
if (!User.getId()) {
return resolve([]);
}

Patrick Müller
committed
return User.getAddressList().then((result) => {
if (result.length) {
return resolve(result);
}
// create new address
return this.openCreateAddressDialog(User).then(function() {

Patrick Müller
committed
return User.getAddressList().then(resolve);
}).catch(reject);

Patrick Müller
committed
resolve([]);
});
});
},
/**
* Open the address window
*
* @return {Promise}
*/

Patrick Müller
committed
var self = this;
return new Promise(function(resolve, reject) {

Patrick Müller
committed
new AddressWindow({
userId: self.getAttribute('userId'),
events: {

Patrick Müller
committed
resolve(addressId);
Win.close();
},
onCancel: reject
}
}).open();
});
},
/**
* Events
*/
/**
* event on import
*/

Patrick Müller
committed
const CustomerSelectElm = this.$Elm.getElement('[name="customer"]');
this.$Elm.getElement('button[name="select-address"]').addEvent('click', () => {
this.openAddressWindow().then((addressId) => {
return this.setAddressId(addressId);

Patrick Müller
committed
// nothing
});
});
QUI.parse(this.$Elm).then(() => {
this.$CustomerSelect = QUI.Controls.getById(
CustomerSelectElm.get('data-quiid')
);
this.$CustomerSelect.addEvents({
onChange: (Control) => {
if (this.$loading) {
this.$loading = false;
return;
}
if (this.$setValues) {

Patrick Müller
committed
return;
}
this.$setDataByUserId(Control.getValue());
},
onSearch: (Control, searchTerm) => {
if (searchTerm) {
this.setAttribute('name', searchTerm);
} else {
this.setAttribute('name', false);
}
this.$fireChange();
},
onRemoveItem: () => {
if (this.$CustomerEdit) {
this.$CustomerEdit.setStyle('display', 'none');
}

Patrick Müller
committed
this.$RowContactPerson.setStyle('display', 'none');
this.$RowContactEmail.setStyle('display', 'none');

Patrick Müller
committed
this.$BtnContactPersonSelect.disabled = true;

Patrick Müller
committed
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
this.$fireChange();
}
});
if (this.getAttribute('userId')) {
this.$CustomerSelect.addItem(this.getAttribute('userId'));
} else {
this.$loading = false;
if (this.getAttribute('name')) {
this.$CustomerSelect.$Search.value = this.getAttribute('name');
}
}
if (this.getElm().getParent('.qui-panel')) {
this.$Panel = QUI.Controls.getById(
this.getElm().getParent('.qui-panel').get('data-quiid')
);
}
});
},
/**
* fire the change event
*/

Patrick Müller
committed
if (this.$triggerChange) {
clearTimeout(this.$triggerChange);
}

Patrick Müller
committed
this.fireEvent('change', [this.getValue(), this]);
}).delay(100, this);
},
/**
* Address creation
*/
/**
*
* @param User
* @return {Promise}
*/
openCreateAddressDialog: function(User) {
return new Promise(function(resolve, reject) {

Patrick Müller
committed
require([
'package/quiqqer/erp/bin/backend/controls/userData/customers/customer/address/Window'

Patrick Müller
committed
new Win({
userId: User.getId(),
events: {
onSubmit: resolve,

Patrick Müller
committed
reject('No User selected');
}
}
}).open();
});
});
},
/**
* Extras
*/
/**
* Toggle the extra view
*
* @returns {Promise}
*/

Patrick Müller
committed
if (this.$extrasAreOpen) {
return this.closeExtras();
}
return this.openExtras();
},
/**
* Open the extra data
*
* @return {Promise}
*/

Patrick Müller
committed
var self = this;

Patrick Müller
committed
self.$rows.setStyles({

Patrick Müller
committed
overflow: 'hidden',
position: 'relative'
});
self.$rows.setStyle('display', 'block');
var height = self.$rows[0].getScrollSize().y;
moofx(self.$rows).animate({
height: height
}, {
duration: 250,

Patrick Müller
committed
self.$rows.setStyles({

Patrick Müller
committed
overflow: null,
position: null
});
moofx(self.$rows).animate({
opacity: 1
}, {
duration: 250,

Patrick Müller
committed
self.$Extras.set({
html: '<span class="fa fa-chevron-up"></span> ' +
QUILocale.get(pkg, 'UserData.btn.extraClose')
});
self.$extrasAreOpen = true;
resolve();
}
});
}
});
});
},
/**
* Close the extra data
*
* @return {Promise}
*/

Patrick Müller
committed
var self = this;

Patrick Müller
committed
moofx(self.$rows).animate({
opacity: 0
}, {
duration: 250,

Patrick Müller
committed
self.$rows.setStyle('display', 'none');
self.$extrasAreOpen = false;
self.$Extras.set({
html: '<span class="fa fa-chevron-right"></span> ' +
QUILocale.get(pkg, 'UserData.btn.extra')
});
resolve();
}
});
});
},
/**
* open the user edit panel for the customer
*/

Patrick Müller
committed
var self = this;
if (this.$Panel) {
this.$Panel.Loader.show();
}
require(['package/quiqqer/customer/bin/backend/Handler'], function(CustomerHandler) {
CustomerHandler.openCustomer(self.getAttribute('userId')).then(function() {

Patrick Müller
committed
self.$Panel.Loader.hide();
});
});
},
/**
* Set the contact person by an address data object to the contact person input field.
*
* @param {Object} address - Address data
* @return {void}
*/
$setContactPersonByAddress: function(address) {

Patrick Müller
committed
if (typeof address.salutation === 'undefined' &&
typeof address.firstname === 'undefined' &&
typeof address.lastname === 'undefined') {
return;
}
const parts = [];
if (typeof address.salutation !== 'undefined') {
parts.push(address.salutation);
}
if (typeof address.firstname !== 'undefined') {
parts.push(address.firstname);
}
if (typeof address.lastname !== 'undefined') {
parts.push(address.lastname);
}
// E-Mail address
const emailAddresses = JSON.decode(address.mail);
if (emailAddresses.length) {
const contactEmail = emailAddresses[0].trim();
if (contactEmail !== '') {
this.$ContactEmail.value = contactEmail;
this.setAttribute('contactEmail', contactEmail);
}
}

Patrick Müller
committed
this.$ContactPerson.value = parts.join(' ').trim();
this.setAttribute('contactPerson', this.$ContactPerson.value);
},
/**
* @param {Number} userId
* @return {Promise<String>}
*/
$getContactEmailAddress: function(userId) {

Patrick Müller
committed
return new Promise((resolve, reject) => {
QUIAjax.get('package_quiqqer_erp_ajax_userData_getContactEmailAddress', resolve, {
'package': pkg,

Patrick Müller
committed
});
});
}
});
});