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/Button',
'qui/controls/buttons/ButtonMultiple',
'package/quiqqer/invoice/bin/backend/utils/Dialogs',
'package/quiqqer/erp/bin/backend/controls/Comments',
'package/quiqqer/erp/bin/backend/controls/articles/Text',
'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, Locker, QUILocale, Mustache, Users, Editors,
templateData, templatePost, templateMissing) {
Type : 'package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice',
'openComments',
'openAddCommentDialog',
'openVerification',
'$openCategory',
'$closeCategory',

Henning Leutz
committed
'$onKeyUp',
'toggleSort',
'$showLockMessage'
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({
icon: 'fa fa-money'
});
this.parent(options);
this.$ArticleList = null;
this.$ArticleListSummary = null;
this.$AddProduct = null;
this.$AddressDelivery = null;
this.$AddSeparator = null;
this.$SortSeparator = null;
this.$locked = false;
onCreate : this.$onCreate,
onInject : this.$onInject,
onDestroy: this.$onDestroy
});
Invoices.addEvents({
onDeleteInvoice: this.$onDeleteInvoice
/**
* 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 + ' (';
if (this.getAttribute('isbrutto')) {
title = title + QUILocale.get(lg, 'brutto');
} else {
title = title + QUILocale.get(lg, 'netto');
}
title = title + ')';
this.setAttribute('title', title);
this.parent();
},
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* 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.length) {
self.$serializedList = {
articles: data.articles.articles
};
self.setAttribute('articles', data.articles.articles);
}
if (data.invoice_address) {
self.setAttribute('invoice_address', data.invoice_address);
}
self.refresh();
});
},
if (this.$locked) {
return Promise.resolve();
}
return Invoices.saveInvoice(
this.getAttribute('invoiceId'),
this.getCurrentData()
).then(function () {

Henning Leutz
committed
this.showSavedIconAnimation();
}.bind(this)).catch(function (err) {
console.error(err);
/**
* Post the temporary invoice
*
* @return {Promise}
*/
post: function () {
var self = this;
this.Loader.show();
this.$unloadCategory(false);
return Invoices.saveInvoice(
this.getAttribute('invoiceId'),
this.getCurrentData()

Henning Leutz
committed
return Promise.all([
Invoices.postInvoice(self.getAttribute('invoiceId')),
Invoices.getSetting('temporaryInvoice', 'openPrintDialogAfterPost'),
Data

Henning Leutz
committed
]);
}).then(function (result) {
var newInvoiceHash = result[0],
openPrintDialogAfterPost = result[1],
Data = result[2];

Henning Leutz
committed
if (!openPrintDialogAfterPost) {
self.destroy();
return;
}
var entityType;
switch (parseInt(Data.type)) {
case 3:
entityType = 'CreditNote';
break;
case 4:
entityType = 'Canceled';
break;
default:
entityType = 'Invoice';
}

Henning Leutz
committed
// open print dialog
Dialogs.openPrintDialog(newInvoiceHash, entityType).then(function () {

Henning Leutz
committed
self.destroy();
});
}).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}}
*/
getCurrentData: function () {
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'),
time_for_payment : this.getAttribute('time_for_payment'),
payment_method : this.getAttribute('payment_method'),
additional_invoice_text: this.getAttribute('additional_invoice_text'),
addressDelivery : this.getAttribute('addressDelivery'),
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}
*/
openData: function () {
this.Loader.show();
return this.$closeCategory().then(function () {
var Container = self.getContent().getElement('.container');
Container.setStyle('height', null);
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'),
textInvoiceDeliveryAddress : QUILocale.get(lg, 'deliveryAddress'),
messageDifferentDeliveryAddress: QUILocale.get(lg, 'message.different,delivery.address'),
textAddresses : QUILocale.get(lg, 'address'),
textCompany : QUILocale.get(lg, 'company'),
textStreet : QUILocale.get(lg, 'street'),
textZip : QUILocale.get(lg, 'zip'),
textCity : QUILocale.get(lg, 'city'),
textCountry : QUILocale.get(lg, 'country')
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);
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')
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', '');
}
});
});
}).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);
});
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');
var Data = QUI.Controls.getById(quiId);
var EditorId = QUI.Controls.getById(editorIdQUIId);
var OrderedBy = QUI.Controls.getById(orderedByIdQUIId);

Henning Leutz
committed
return;
}

Henning Leutz
committed
var userId = Data.getValue().userId;
self.setAttribute('customer_id', userId);
self.setAttribute('invoice_address_id', Data.getValue().addressId);

Henning Leutz
committed
Promise.all([
Invoices.getPaymentTime(userId),
Invoices.isNetto(userId)
]).then(function (result) {
var paymentTime = result[0];
var isNetto = result[1];

Henning Leutz
committed
Content.getElement('[name="time_for_payment"]').value = paymentTime;
self.setAttribute('isbrutto', !isNetto);
self.setAttribute('time_for_payment', paymentTime);
self.refresh();

Henning Leutz
committed
});
//Data.setAddress();
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 (self.getAttribute('editor_id')) {
EditorId.addItem(self.getAttribute('editor_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 (self.getAttribute('ordered_by')) {
OrderedBy.addItem(self.getAttribute('ordered_by'));
}
// invoice address
var address = self.getAttribute('invoice_address');
if (!address) {
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/invoice/bin/backend/controls/panels/DeliveryAddress"]'
).get('data-quiid')
);
if (self.getAttribute('delivery_address')) {
var deliveryAddress = self.getAttribute('delivery_address');
try {
deliveryAddress = JSON.decode(deliveryAddress);
if (deliveryAddress) {
self.getContent().getElement('[name="differentDeliveryAddress"]').checked = true;
self.$AddressDelivery.setAttribute('userId', self.getAttribute('customer_id'));
self.$AddressDelivery.setValue(deliveryAddress);
}
} catch (e) {
}
var Container = self.getContent().getElement('.container');
new QUIButton({
textimage: 'fa fa-list',
text : QUILocale.get(lg, 'erp.panel.temporary.invoice.button.nextToArticles'),
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];
}
}).inject(Payments);
}
Payments.value = self.getAttribute('payment_method');
}).then(function () {
// additional-invoice-text -> wysiwyg
return self.$loadAdditionalInvoiceText();
}).then(function () {
return self.Loader.hide();
}).then(function () {
}).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({
currency: self.getAttribute('currency'),
events : {
onArticleReplaceClick: self.$onArticleReplaceClick
},
Container.setStyle('height', '100%');
self.$ArticleListSummary = new Summary({
currency: self.getAttribute('currency'),
List : self.$ArticleList,
styles : {
left : 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);
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);
text : QUILocale.get(lg, 'erp.panel.temporary.invoice.category.review.btnGoto'),
onClick: self.openVerification
});
});
}).then(function () {
return self.$openCategory();
});
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
/**
* 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();
});
},
/**
* Refresh the comment display
*/
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);
},
/**
* Open the verification category
*
* @returns {Promise}
*/
openVerification: function () {
var self = this,
ParentContainer = null,
FrameContainer = null;
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);
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 : {
'min-height': 1240,
height : '100%',
padding : 20,
width : 874
},
events : {
onLoad: function (Box) {
Box.getElm().addClass('quiqqer-invoice-backend-temporaryInvoice-preview');
}).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
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 : {
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
});
} else {
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);
}
} else {
// post available
Missing.set('html', Mustache.render(templatePost, {
message: QUILocale.get(lg, 'message.invoice.ok')
}));
text : QUILocale.get(lg, 'journal.btn.post'),
'class' : 'btn-green',
events : {
},
disabled: self.$locked
}).inject(
Missing.getElement('.quiqqer-invoice-backend-temporaryInvoice-missing-button')
);
self.Loader.hide().then(function () {
return new Promise(function (resolve) {
moofx(Missing).animate({
opacity: 1,
bottom : 0
}, {
callback: function () {
self.Loader.hide().then(resolve);
}
});
});
});
return self.$openCategory();
}).catch(function (err) {
console.error('ERROR');
console.error(err);
return self.$openCategory();
});
},
/**
* Opens the product search
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) {
new Win({
user : self.getUserData(),
events: {
onSubmit: function (Win, article) {
var Instance = new Article(article);
if ("calculated_vatArray" in article) {
Instance.setVat(article.calculated_vatArray.vat);
}
self.$ArticleList.addArticle(Instance);
resolve(Instance);
}
self.$AddProduct.setAttribute('textimage', 'fa fa-plus');
});
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
/**
*
* @return {Promise}
*/
$loadAdditionalInvoiceText: function () {
var self = this;
return new Promise(function (resolve) {
var EditorParent = new Element('div').inject(
self.getContent().getElement('.additional-invoice-text')
);
Editors.getEditor(null).then(function (Editor) {
self.$AdditionalText = Editor;
// minimal toolbar
self.$AdditionalText.setAttribute('buttons', {
lines: [
[[
{
type : "button",
button: "Bold"
},
{
type : "button",
button: "Italic"
},
{
type : "button",
button: "Underline"
},
{
type: "separator"
},
{
type : "button",
button: "RemoveFormat"
},
{
type: "separator"
},
{
type : "button",
button: "NumberedList"
},
{
type : "button",
button: "BulletedList"
}
]]
]
});
self.$AdditionalText.addEvent('onLoaded', function () {
self.$AdditionalText.switchToWYSIWYG();
self.$AdditionalText.showToolbar();
self.$AdditionalText.setContent(self.getAttribute('additional_invoice_text'));
resolve();
});
self.$AdditionalText.inject(EditorParent);
self.$AdditionalText.setHeight(200);
});
});
},

Henning Leutz
committed
* Close the current category and save it

Henning Leutz
committed
var self = this;
if (self.$AddProduct) {
self.$AddProduct.hide();
self.$AddSeparator.hide();
self.$SortSeparator.hide();
self.$ArticleSort.hide();

Henning Leutz
committed
if (self.$ArticleListSummary) {
moofx(self.$ArticleListSummary.getElm()).animate({
bottom : -20,
opacity: 0
}, {
duration: 250,
callback: function () {

Henning Leutz
committed
self.$ArticleListSummary.destroy();
self.$ArticleListSummary = null;
}
}

Henning Leutz
committed
self.getContent().setStyle('padding', 0);

Henning Leutz
committed
var Container = self.getContent().getElement('.container');
if (!Container) {
Container = new Element('div', {
'class': 'container',

Henning Leutz
committed
}).inject(self.getContent());
}
moofx(Container).animate({
opacity: 0,
}, {
duration: 200,
callback: function () {

Henning Leutz
committed
self.$unloadCategory();
if (self.$AddressDelivery) {
self.$AddressDelivery.destroy();
self.$AddressDelivery = null;
}

Henning Leutz
committed
self.save().then(function () {
resolve(Container);
}).catch(function () {
resolve(Container);
});
}

Henning Leutz
committed
});