Newer
Older
* @module package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice
* Edit a Temporary Invoice and created a posted invoice
define('package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice', [
'qui/controls/buttons/ButtonMultiple',
'qui/controls/buttons/Separator',
'qui/controls/windows/Confirm',
'qui/utils/Form',
'controls/users/address/Select',
'package/quiqqer/invoice/bin/backend/utils/Dialogs',
'package/quiqqer/erp/bin/backend/controls/Comments',
'package/quiqqer/erp/bin/backend/controls/articles/Text',
'package/quiqqer/payments/bin/backend/Payments',
'package/quiqqer/customer/bin/backend/controls/customer/address/Window',
'utils/Lock',
'text!package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice.Data.html',
'text!package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice.Post.html',
'text!package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice.Missing.html',
'css!package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice.css'
], function (QUI, QUIPanel, QUIButton, QUIButtonMultiple, QUISeparator, QUIConfirm, QUIFormUtils,
AddressSelect, Invoices, Dialogs, Comments, TextArticle,
Payments, AddressWindow, Locker, QUILocale, Mustache, Users, Editors,
templateData, templatePost, templateMissing) {
Type : 'package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice',
'save',
'post',
'openData',
'openArticles',
'openComments',
'openAddCommentDialog',
'openVerification',
'$openCategory',
'$closeCategory',
'$onDeleteInvoice',
'$onArticleReplaceClick',
'$clickDelete',
'toggleSort',
options: {
invoiceId : false,
customer_id : false,
invoice_address : false,
invoice_address_id: false,
project_name : '',
date : '',
time_for_payment : '',
data : {},
articles : []
},
initialize: function (options) {
this.setAttributes({
this.$AdditionalText = null;
this.$ArticleList = null;
this.$ArticleListSummary = null;
this.$AddProduct = null;
this.$ArticleSort = null;
this.$AddressDelivery = null;
this.$AddSeparator = null;
this.$SortSeparator = null;
this.$locked = false;
this.$serializedList = {};
onCreate : this.$onCreate,
onInject : this.$onInject,
* Return the lock key
*
* @return {string}
$getLockKey: function () {
return 'lock-invoice-temporary-' + this.getAttribute('invoiceId');
},
/**
* Return the lock group
* @return {string}
*/
$getLockGroups: function () {
return 'quiqqer/invoice';
},
/**
* Panel refresh
*/
refresh: function () {
var title = this.getAttribute('invoiceId');
title = title + QUILocale.get(lg, 'brutto.panel.title');
title = title + QUILocale.get(lg, 'netto.panel.title');
this.setAttribute('title', title);
this.parent();
},
/**
* Refresh the invoice data
*/
doRefresh: function () {
var self = this,
invoiceId = this.getAttribute('invoiceId');
return Invoices.getTemporaryInvoice(invoiceId).then(function (data) {
self.setAttributes(data);
if (data.articles.articles && data.articles.articles.length) {
self.$serializedList = {
articles: data.articles.articles
};
self.setAttribute('articles', data.articles.articles);
}
if (data.invoice_address) {
self.setAttribute('invoice_address', data.invoice_address);
}
/**
* Saves the current data
*
* @return {Promise}
*/
save: function () {
if (this.$locked) {
return Promise.resolve();
}
this.Loader.show();
this.$unloadCategory(false);
return Invoices.saveInvoice(
this.getAttribute('invoiceId'),
this.getCurrentData()
).then(function () {
this.Loader.hide();
this.showSavedIconAnimation();
}.bind(this)).catch(function (err) {
console.error(err);
console.error(err.getMessage());
post: function () {
var self = this;
this.Loader.show();
this.$unloadCategory(false);
return Invoices.saveInvoice(
this.getAttribute('invoiceId'),
this.getCurrentData()
).then(function (Data) {
return Promise.all([
Invoices.postInvoice(self.getAttribute('invoiceId')),
Invoices.getSetting('temporaryInvoice', 'openPrintDialogAfterPost'),
Data
]);
}).then(function (result) {
var newInvoiceHash = result[0],
openPrintDialogAfterPost = result[1],
Data = result[2];
if (!openPrintDialogAfterPost) {
self.destroy();
return;
}
var entityType;
switch (parseInt(Data.type)) {
case 3:
entityType = 'CreditNote';
break;
case 4:
entityType = 'Canceled';
break;
default:
entityType = 'Invoice';
}
// open print dialog
Dialogs.openPrintDialog(newInvoiceHash, entityType).then(function () {
self.destroy();

Henning Leutz
committed
});
}).catch(function (err) {
console.error(err);
console.error(err.getMessage());
this.Loader.hide();
}.bind(this));
* @returns {{customer_id, invoice_address_id, project_name, articles, date, time_for_payment}}
var deliveryAddress = this.getAttribute('addressDelivery');
if (!deliveryAddress) {
deliveryAddress = this.getAttribute('delivery_address');
}
return {
customer_id : this.getAttribute('customer_id'),
invoice_address_id : this.getAttribute('invoice_address_id'),
project_name : this.getAttribute('project_name'),
articles : this.getAttribute('articles'),
date : this.getAttribute('date'),
editor_id : this.getAttribute('editor_id'),
ordered_by : this.getAttribute('ordered_by'),
contact_person : this.getAttribute('contact_person'),
time_for_payment : this.getAttribute('time_for_payment'),
payment_method : this.getAttribute('payment_method'),
additional_invoice_text: this.getAttribute('additional_invoice_text'),
addressDelivery : deliveryAddress,
processing_status : this.getAttribute('processing_status')
};
},
/**
* Return the current user data
*/
getUserData: function () {
return {
uid: this.getAttribute('customer_id'),
aid: this.getAttribute('invoice_address_id')
};
* Open the data category
*
* @returns {Promise}
this.renderDataDone = false;
this.Loader.show();
return this.$closeCategory().then(function () {
var Container = self.getContent().getElement('.container');
Container.setStyle('height', null);
Container.set({
html: Mustache.render(templateData, {
textInvoiceData : QUILocale.get(lg, 'erp.panel.temporary.invoice.category.data.textInvoiceData'),
textInvoiceDate : QUILocale.get(lg, 'erp.panel.temporary.invoice.category.data.textInvoiceDate'),
textTermOfPayment : QUILocale.get(lg, 'erp.panel.temporary.invoice.category.data.textTermOfPayment'),
textProjectName : QUILocale.get(lg, 'erp.panel.temporary.invoice.category.data.textProjectName'),
textOrderedBy : QUILocale.get(lg, 'erp.panel.temporary.invoice.category.data.textOrderedBy'),
textEditor : QUILocale.get(lg, 'erp.panel.temporary.invoice.category.data.textEditor'),
textInvoicePayment: QUILocale.get(lg, 'erp.panel.temporary.invoice.category.data.textInvoicePayment'),
textPaymentMethod : QUILocale.get(lg, 'erp.panel.temporary.invoice.category.data.textPaymentMethod'),
textInvoiceText : QUILocale.get(lg, 'erp.panel.temporary.invoice.category.data.textInvoiceText'),
textStatus : QUILocale.get(lg, 'erp.panel.temporary.invoice.category.data.textStatus'),
textContactPerson : QUILocale.get(lg, 'erp.panel.temporary.invoice.category.data.textContactPerson'),
textInvoiceDeliveryAddress: QUILocale.get(lg, 'deliveryAddress'),
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
var Form = Container.getElement('form');
QUIFormUtils.setDataToForm(self.getAttribute('data'), Form);
// set invoice date to today
// quiqqer/invoice#46
var local = new Date();
local.setMinutes(local.getMinutes() - local.getTimezoneOffset());
var dateDate = local.toJSON().slice(0, 10);
QUIFormUtils.setDataToForm({
date : dateDate,
time_for_payment : self.getAttribute('time_for_payment'),
project_name : self.getAttribute('project_name'),
editor_id : self.getAttribute('editor_id'),
processing_status: self.getAttribute('processing_status'),
contact_person : self.getAttribute('contact_person')
}, Form);
Form.elements.date.set('disabled', true);
Form.elements.date.set('title', QUILocale.get(lg, 'permissions.set.invoice.date'));
require(['Permissions'], function (Permissions) {
Permissions.hasPermission('quiqqer.invoice.changeDate').then(function (has) {
if (has) {
Form.elements.date.set('disabled', false);
Form.elements.date.set('title', '');
Container.getElements('[name="select-contact-id-address"]').addEvent('click', function () {
new AddressWindow({
autoclose: false,
userId : self.getAttribute('customer_id'),
events : {
onSubmit: function (Win, addressId, address) {
Win.close();
self.$setContactPersonByAddress(address);
}
}
}).open();
});
if (self.getAttribute('customer_id')) {
Container.getElements('[name="select-contact-id-address"]').set('disabled', false);
return QUI.parse(Container);
}).then(function () {
return new Promise(function (resolve, reject) {
var Form = self.getContent().getElement('form');
require(['utils/Controls'], function (ControlUtils) {
ControlUtils.parse(Form).then(resolve);
}, reject);
});
}).then(function () {
var Content = self.getContent();
var quiId = Content.getElement(
'[data-qui="package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice.UserData"]'
).get('data-quiid');
var editorIdQUIId = Content.getElement('[name="editorId"]').get('data-quiid');
var orderedByIdQUIId = Content.getElement('[name="orderedBy"]').get('data-quiid');

Henning Leutz
committed
var Data = QUI.Controls.getById(quiId);
var EditorId = QUI.Controls.getById(editorIdQUIId);
var OrderedBy = QUI.Controls.getById(orderedByIdQUIId);

Henning Leutz
committed
OrderedBy.setAttribute('showAddressName', false);

Henning Leutz
committed
Data.addEvent('onChange', function () {
if (self.renderDataDone === false) {
return;
}

Henning Leutz
committed

Henning Leutz
committed
self.setAttribute('customer_id', parseInt(userId));
self.setAttribute('invoice_address_id', Data.getValue().addressId);
if (!userId) {
Content.getElements('[name="select-contact-id-address"]').set('disabled', true);
Content.getElements('[name="contact_person"]').set('value', '');
} else {
Content.getElements('[name="select-contact-id-address"]').set('disabled', false);

Henning Leutz
committed
Users.get(userId).loadIfNotLoaded().then(function (User) {
var addressId = User.getAttribute('quiqqer.erp.customer.contact.person');
if (User.getAttribute('quiqqer.erp.standard.payment')) {
self.getContent()
.getElement('[name="payment_method"]')
.value = User.getAttribute('quiqqer.erp.standard.payment');
}
if (!addressId) {
return;
}

Henning Leutz
committed
addressId = parseInt(addressId);
User.getAddressList().then(function (addressList) {
for (var i = 0, len = addressList.length; i < len; i++) {
if (addressList[i].id === addressId) {
self.$setContactPersonByAddress(addressList[i]);
}
}

Henning Leutz
committed
});
// reset deliver address
if (self.$AddressDelivery) {
self.$AddressDelivery.setAttribute('userId', userId);
}
Promise.all([
Invoices.getPaymentTime(userId),
Invoices.isNetto(userId)
]).then(function (result) {
var paymentTime = result[0];
var isNetto = result[1];
Content.getElement('[name="time_for_payment"]').value = paymentTime;
self.setAttribute('isbrutto', !isNetto);
self.setAttribute('time_for_payment', paymentTime);
self.refresh();
});
});
// editor
EditorId.addEvent('onChange', function () {
self.setAttribute('editor_id', EditorId.getValue());
});
if (typeof window.QUIQQER_EMPLOYEE_GROUP !== 'undefined') {
EditorId.setAttribute('search', true);
EditorId.setAttribute('searchSettings', {
filter: {
filter_group: window.QUIQQER_EMPLOYEE_GROUP
}
});
}
if (parseInt(self.getAttribute('editor_id'))) {
EditorId.addItem(self.getAttribute('editor_id'));
} else {
EditorId.addItem(USER.id);
}
// ordered by
OrderedBy.addEvent('onChange', function () {
self.setAttribute('ordered_by', OrderedBy.getValue());
});
if (typeof window.QUIQQER_CUSTOMER_GROUP !== 'undefined') {
OrderedBy.setAttribute('search', true);
OrderedBy.setAttribute('searchSettings', {
filter: {
filter_group: window.QUIQQER_CUSTOMER_GROUP
}
});
}
if (parseInt(self.getAttribute('ordered_by'))) {
OrderedBy.addItem(parseInt(self.getAttribute('ordered_by')));
}
// invoice address
var address = self.getAttribute('invoice_address');
address.userId = self.getAttribute('customer_id');
address.addressId = self.getAttribute('invoice_address_id');
return Data.setValue(address);
}).then(function () {
// delivery address
self.$AddressDelivery = QUI.Controls.getById(
self.getContent().getElement(
'[data-qui="package/quiqqer/erp/bin/backend/controls/DeliveryAddress"]'
var deliveryAddress = self.getAttribute('addressDelivery');
if (!deliveryAddress) {
deliveryAddress = self.getAttribute('delivery_address');
if (deliveryAddress) {
deliveryAddress = JSON.decode(deliveryAddress);
if (deliveryAddress) {
self.$AddressDelivery.setAttribute('userId', self.getAttribute('customer_id'));
self.$AddressDelivery.setValue(deliveryAddress);
}
}).then(function () {
var Container = self.getContent().getElement('.container');
new QUIButton({
textimage: 'fa fa-list',
text : QUILocale.get(lg, 'erp.panel.temporary.invoice.button.nextToArticles'),
styles : {
display: 'block',
'float': 'right',
margin : '0 0 20px'
},
events : {
onClick: function () {
self.openArticles().catch(function (e) {
console.error(e);
}).then(function () {
return Payments.getPayments();
}).then(function (payments) {
// load payments
var Payments = self.getContent().getElement('[name="payment_method"]');
new Element('option', {
html : '',
value: ''
}).inject(Payments);
var i, len, title;
var current = QUILocale.getCurrent();
for (i = 0, len = payments.length; i < len; i++) {
title = payments[i].title;
if (typeOf(title) === 'object' && typeof title[current] !== 'undefined') {
title = title[current];
}
new Element('option', {
html : title,
value: payments[i].id
}).inject(Payments);
}
Payments.value = self.getAttribute('payment_method');
}).then(function () {
// additional-invoice-text -> wysiwyg
return self.$loadAdditionalInvoiceText();
}).then(function () {
self.getCategory('data').setActive();
return self.Loader.hide();
}).then(function () {
return self.$openCategory();
}).then(function () {
self.renderDataDone = true;
});
* Open the product category
*
* @returns {Promise}
var self = this;
return self.$closeCategory().then(function (Container) {
return new Promise(function (resolve) {
require([
'package/quiqqer/erp/bin/backend/controls/articles/ArticleList',
'package/quiqqer/erp/bin/backend/controls/articles/ArticleSummary'
], function (List, Summary) {
self.$ArticleList = new List({
nettoinput: !self.getAttribute('isbrutto'),
currency : self.getAttribute('currency'),
events : {
onArticleReplaceClick: self.$onArticleReplaceClick
},
height: 'calc(100% - 120px)'
}
}).inject(Container);
Container.setStyle('height', '100%');
self.$ArticleListSummary = new Summary({
currency: self.getAttribute('currency'),
List : self.$ArticleList,
styles : {
bottom : -20,
left : 0,
opacity : 0,
position: 'absolute'
}
}).inject(Container.getParent());
moofx(self.$ArticleListSummary.getElm()).animate({
bottom : 0,
opacity: 1
});
self.$ArticleList.setUser(self.getUserData());
if (self.$serializedList) {
self.$ArticleList.unserialize(self.$serializedList);
}
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
self.$AddProduct.show();
self.$AddSeparator.show();
self.$SortSeparator.show();
self.$ArticleSort.show();
self.getCategory('articles').setActive();
new QUIButton({
textimage: 'fa fa-info',
text : QUILocale.get(lg, 'erp.panel.temporary.invoice.button.data'),
styles : {
'float': 'left',
margin : '20px 0 0'
},
events : {
onClick: self.openData
}
}).inject(Container);
new QUIButton({
textimage: 'fa fa-check',
text : QUILocale.get(lg, 'erp.panel.temporary.invoice.category.review.btnGoto'),
styles : {
'float': 'right',
margin : '20px 0 0'
},
events : {
onClick: self.openVerification
}
}).inject(Container);
self.Loader.hide().then(resolve);
});
});
}).then(function () {
return self.$openCategory();
/**
* open the comments
*
* @return {Promise<Promise>}
*/
openComments: function () {
var self = this;
this.Loader.show();
this.getCategory('comments').setActive();
return this.$closeCategory().then(function () {
self.refreshComments();
}).then(function () {
return self.$openCategory();
}).then(function () {
self.Loader.hide();
});
/**
*/
refreshComments: function () {
var Container = this.getContent().getElement('.container');
Container.set('html', '');
new QUIButton({
textimage: 'fa fa-comments',
text : QUILocale.get(lg, 'invoice.panel.comment.add'),
styles : {
'float' : 'right',
marginBottom: 10
},
events : {
onClick: this.openAddCommentDialog
}
}).inject(Container);
new Comments({
comments: this.getAttribute('comments')
}).inject(Container);
},
openVerification: function () {
var self = this,
ParentContainer = null,
FrameContainer = null;
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
return this.$closeCategory().then(function (Container) {
FrameContainer = new Element('div', {
'class': 'quiqqer-invoice-backend-temporaryInvoice-previewContainer'
}).inject(Container);
Container.setStyle('overflow', 'hidden');
Container.setStyle('padding', 0);
Container.setStyle('height', '100%');
ParentContainer = Container;
return Invoices.getTemporaryInvoicePreview(
self.getAttribute('invoiceId'),
self.getCurrentData()
).then(function (html) {
return new Promise(function (resolve) {
require(['qui/controls/elements/Sandbox'], function (Sandbox) {
new Sandbox({
content: html,
styles : {
height : '100%',
padding: 20,
width : '95%'
},
events : {
onLoad: function (Box) {
Box.getElm().addClass('quiqqer-invoice-backend-temporaryInvoice-preview');
}
}
}).inject(FrameContainer);
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
}).then(function () {
// check invoice date
var Now = new Date();
Now.setHours(0, 0, 0, 0);
var InvoiceDate = new Date(self.getAttribute('date'));
if (InvoiceDate < Now) {
new QUIConfirm({
title : QUILocale.get(lg, 'window.invoice.date.past.title'),
text : QUILocale.get(lg, 'window.invoice.date.past.title'),
information : QUILocale.get(lg, 'window.invoice.date.past.content'),
icon : 'fa fa-clock-o',
texticon : 'fa fa-clock-o',
maxHeight : 400,
maxWidth : 600,
autoclose : false,
cancel_button: {
text : QUILocale.get(lg, 'window.invoice.date.past.cancel.text'),
textimage: 'fa fa-close'
},
ok_button : {
text : QUILocale.get(lg, 'window.invoice.date.past.ok.text'),
textimage: 'fa fa-check'
},
events : {
onSubmit: function (Win) {
Win.Loader.show();
var Today = new Date()
var today = Today.toISOString().split('T')[0];
self.setAttribute('date', today + ' 00:00:00');
self.save().then(function () {
self.openVerification();
Win.close();
});
}
}
}).open();
}
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
}).then(function () {
return Invoices.getMissingAttributes(self.getAttribute('invoiceId'));
}).then(function (missing) {
var Missing = new Element('div', {
'class': 'quiqqer-invoice-backend-temporaryInvoice-missing',
styles : {
opacity: 0,
bottom : -20
}
}).inject(ParentContainer);
if (Object.getLength(missing)) {
Missing.set('html', Mustache.render(templateMissing, {
message: QUILocale.get(lg, 'message.invoice.missing')
}));
var Info = new Element('info', {
'class': 'quiqqer-invoice-backend-temporaryInvoice-missing-miss-message',
styles : {
display: 'none',
opacity: 0
}
}).inject(ParentContainer);
Missing.getElement(
'.quiqqer-invoice-backend-temporaryInvoice-missing-miss-button'
).addEvent('click', function () {
var isShow = parseInt(Info.getStyle('opacity'));
if (isShow) {
moofx(Info).animate({
bottom : 60,
opacity: 0
}, {
callback: function () {
Info.setStyle('display', 'none');
}
});
} else {
Info.setStyle('display', null);

Henning Leutz
committed
moofx(Info).animate({
bottom : 80,
opacity: 1
});
}
});
for (var missed in missing) {
if (!missing.hasOwnProperty(missed)) {
continue;
}
new Element('div', {
'class': 'messages-message message-error',
html : missing[missed]
}).inject(Info);
Missing.getElement(
'.quiqqer-invoice-backend-temporaryInvoice-missing-miss-button'
).click();
} else {
// post available
Missing.set('html', Mustache.render(templatePost, {
message: QUILocale.get(lg, 'message.invoice.ok')
}));
new QUIButton({
text : QUILocale.get(lg, 'journal.btn.post'),
'class' : 'btn-green',
events : {
onClick: self.post
},
disabled: self.$locked
}).inject(
Missing.getElement('.quiqqer-invoice-backend-temporaryInvoice-missing-button')
);
self.getCategory('verification').setActive();
self.Loader.hide().then(function () {
return new Promise(function (resolve) {
moofx(Missing).animate({
opacity: 1,
bottom : 0
}, {
callback: function () {
self.Loader.hide().then(resolve);
}
});
});
}).then(function () {
return self.$openCategory();
}).catch(function (err) {
console.error('ERROR');
console.error(err);
return self.$openCategory();
});
},
/**
* Opens the product search
*
* @todo only if products are installed
*/
openProductSearch: function () {
var self = this;
this.$AddProduct.setAttribute('textimage', 'fa fa-spinner fa-spin');
return new Promise(function (resolve) {
require([
'package/quiqqer/erp/bin/backend/controls/articles/product/AddProductWindow',
'package/quiqqer/invoice/bin/backend/controls/articles/Article'
], function (Win, Article) {
onLoad: function (Instance, ProductSearch) {
ProductSearch.Loader.show();
require(['package/quiqqer/invoice/bin/Invoices'], function (Invoices) {
Invoices.getSetting('invoice', 'productDescriptionSource').then(function (src) {
if (parseInt(src)) {
productDescriptionSource = parseInt(src);
Instance.setAttribute('fields', [productDescriptionSource]);
}
ProductSearch.Loader.hide();
});
});
},
onSubmit: function (Win, article) {
var Instance = new Article(article);
if ("calculated_vatArray" in article) {
Instance.setVat(article.calculated_vatArray.vat);
}
if (productDescriptionSource &&
typeof article.fields !== 'undefined' &&
typeof article.fields[productDescriptionSource] !== 'undefined') {
var field = article.fields[productDescriptionSource];
var current = QUILocale.getCurrent();
if (field && typeof field[current] !== 'undefined') {
Instance.setAttribute('description', field[current]);
Instance.setAttribute('description', field);