Skip to content
Code-Schnipsel Gruppen Projekte
TemporaryInvoice.js 47,5 KiB
Newer Older
  • Learn to ignore specific revisions
  • Henning Leutz's avatar
    Henning Leutz committed
    /**
     * @module package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice
    
    Henning Leutz's avatar
    Henning Leutz committed
     * @author www.pcsg.de (Henning Leutz)
    
    Henning Leutz's avatar
    Henning Leutz committed
     *
     * Edit a Temporary Invoice and created a posted invoice
     */
    define('package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice', [
    
    Henning Leutz's avatar
    Henning Leutz committed
        'qui/QUI',
    
    Henning Leutz's avatar
    Henning Leutz committed
        'qui/controls/desktop/Panel',
    
    Henning Leutz's avatar
    Henning Leutz committed
        'qui/controls/buttons/Button',
        'qui/controls/buttons/ButtonMultiple',
    
        'qui/controls/buttons/Separator',
    
        'qui/controls/windows/Confirm',
    
        'qui/utils/Form',
    
        'controls/users/address/Select',
    
        'package/quiqqer/invoice/bin/Invoices',
    
    Henning Leutz's avatar
    Henning Leutz committed
        'package/quiqqer/invoice/bin/backend/controls/articles/Text',
    
    Henning Leutz's avatar
    Henning Leutz committed
        'package/quiqqer/payments/bin/backend/Payments',
    
        'Locale',
        'Mustache',
        'Users',
    
        'Editors',
    
        '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,
    
    Henning Leutz's avatar
    Henning Leutz committed
                 AddressSelect, Invoices, TextArticle,
    
                 Payments, Locker, QUILocale, Mustache, Users, Editors,
    
                 templateData, templatePost, templateMissing) {
    
    Henning Leutz's avatar
    Henning Leutz committed
        "use strict";
    
    
        var lg = 'quiqqer/invoice';
    
    
    Henning Leutz's avatar
    Henning Leutz committed
        return new Class({
    
            Extends: QUIPanel,
    
    Henning Leutz's avatar
    Henning Leutz committed
            Type   : 'package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice',
    
    Henning Leutz's avatar
    Henning Leutz committed
    
            Binds: [
    
    Henning Leutz's avatar
    Henning Leutz committed
                'save',
    
    Henning Leutz's avatar
    Henning Leutz committed
                'openData',
    
    Henning Leutz's avatar
    Henning Leutz committed
                'openArticles',
    
    Henning Leutz's avatar
    Henning Leutz committed
                'openVerification',
                '$openCategory',
                '$closeCategory',
    
    Henning Leutz's avatar
    Henning Leutz committed
                '$onCreate',
    
                '$onInject',
    
                '$onDestroy',
                '$onDeleteInvoice',
    
    Henning Leutz's avatar
    Henning Leutz committed
                '$onArticleReplaceClick',
    
    Henning Leutz's avatar
    Henning Leutz committed
                '$clickDelete',
    
                'toggleSort',
                '$showLockMessage'
    
    Henning Leutz's avatar
    Henning Leutz committed
            ],
    
    Henning Leutz's avatar
    Henning Leutz committed
    
            options: {
    
                invoiceId         : false,
                customer_id       : false,
                invoice_address_id: false,
                project_name      : '',
                date              : '',
                time_for_payment  : '',
                data              : {},
                articles          : []
    
    Henning Leutz's avatar
    Henning Leutz committed
            },
    
            initialize: function (options) {
                this.setAttributes({
                    icon: 'fa fa-money'
                });
    
                this.parent(options);
    
                this.$AdditionalText     = null;
    
                this.$ArticleList        = null;
                this.$ArticleListSummary = null;
                this.$AddProduct         = null;
    
    Henning Leutz's avatar
    Henning Leutz committed
                this.$ArticleSort        = null;
    
                this.$AddSeparator  = null;
                this.$SortSeparator = null;
    
    Henning Leutz's avatar
    Henning Leutz committed
                this.$serializedList = {};
    
    
    Henning Leutz's avatar
    Henning Leutz committed
                this.addEvents({
    
    Henning Leutz's avatar
    Henning Leutz committed
                    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();
            },
    
    
    Henning Leutz's avatar
    Henning Leutz committed
             * Saves the current data
    
    Henning Leutz's avatar
    Henning Leutz committed
             * @return {Promise}
    
    Henning Leutz's avatar
    Henning Leutz committed
            save: function () {
    
                if (this.$locked) {
                    return Promise.resolve();
                }
    
    
    Henning Leutz's avatar
    Henning Leutz committed
                this.Loader.show();
    
    Henning Leutz's avatar
    Henning Leutz committed
                this.$unloadCategory(false);
    
                return Invoices.saveInvoice(
                    this.getAttribute('invoiceId'),
                    this.getCurrentData()
                ).then(function () {
    
    Henning Leutz's avatar
    Henning Leutz committed
                    this.Loader.hide();
    
    Henning Leutz's avatar
    Henning Leutz committed
                }.bind(this)).catch(function (err) {
                    console.error(err);
    
                    console.error(err.getMessage());
    
    Henning Leutz's avatar
    Henning Leutz committed
                    this.Loader.hide();
                }.bind(this));
    
            /**
             * 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()
                ).then(function () {
    
                    return Promise.all([
                        Invoices.postInvoice(self.getAttribute('invoiceId')),
                        Invoices.getSetting('temporaryInvoice', 'openPrintDialogAfterPost')
                    ]);
                }).then(function (result) {
                    var newInvoiceHash           = result[0],
                        openPrintDialogAfterPost = result[1];
    
                    if (!openPrintDialogAfterPost) {
                        self.destroy();
                        return;
                    }
    
                    // open print dialog
                    require([
                        'package/quiqqer/invoice/bin/backend/controls/elements/PrintDialog'
                    ], function (PrintDialog) {
                        self.destroy();
    
                        new PrintDialog({
                            invoiceId: newInvoiceHash
                        }).open();
                    });
    
                }).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')
    
    Henning Leutz's avatar
    Henning Leutz committed
            /**
             * Return the current user data
             */
            getUserData: function () {
                return {
                    uid: this.getAttribute('customer_id'),
    
                    aid: this.getAttribute('invoice_address_id')
    
    Henning Leutz's avatar
    Henning Leutz committed
                };
            },
    
    
    Henning Leutz's avatar
    Henning Leutz committed
             * Open the data category
             *
             * @returns {Promise}
             */
            openData: function () {
    
    Henning Leutz's avatar
    Henning Leutz committed
                var self = this;
    
    Henning Leutz's avatar
    Henning Leutz committed
                this.renderDataDone = false;
    
    Henning Leutz's avatar
    Henning Leutz committed
                this.Loader.show();
    
                return this.$closeCategory().then(function () {
    
                    var Container = self.getContent().getElement('.container');
    
                    Container.setStyle('height', null);
    
    
                    Container.set({
    
    Henning Leutz's avatar
    Henning Leutz committed
                        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')
    
    Henning Leutz's avatar
    Henning Leutz committed
                        })
    
                    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')
    
                    }, 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', '');
                            }
                        });
                    });
    
    
                    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);
                    });
    
    Henning Leutz's avatar
    Henning Leutz committed
                }).then(function () {
    
                    var Content = self.getContent();
    
                    var quiId = Content.getElement(
    
    Henning Leutz's avatar
    Henning Leutz committed
                        '[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's avatar
    Henning Leutz committed
                    Data.addEvent('onChange', function () {
    
    Henning Leutz's avatar
    Henning Leutz committed
                        if (self.renderDataDone === false) {
    
                        var userId = Data.getValue().userId;
    
                        self.setAttribute('customer_id', userId);
    
                        self.setAttribute('invoice_address_id', Data.getValue().addressId);
    
                        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 (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'));
                    }
    
    Henning Leutz's avatar
    Henning Leutz committed
                    return Data.setValue(
                        self.getAttribute('customer_id'),
    
                        self.getAttribute('invoice_address_id')
    
    Henning Leutz's avatar
    Henning Leutz committed
                    );
                }).then(function () {
    
    Henning Leutz's avatar
    Henning Leutz committed
                    var Container = self.getContent().getElement('.container');
    
                    new QUIButton({
                        textimage: 'fa fa-list',
    
    Henning Leutz's avatar
    Henning Leutz committed
                        text     : QUILocale.get(lg, 'erp.panel.temporary.invoice.button.nextToArticles'),
    
    Henning Leutz's avatar
    Henning Leutz committed
                        styles   : {
                            display: 'block',
    
    Henning Leutz's avatar
    Henning Leutz committed
                            'float': 'right',
    
                            margin : '0 0 20px'
    
    Henning Leutz's avatar
    Henning Leutz committed
                        },
                        events   : {
                            onClick: function () {
                                self.openArticles();
                            }
                        }
                    }).inject(Container);
    
    
    Henning Leutz's avatar
    Henning Leutz committed
                }).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', {
    
                            value: payments[i].id
    
                        }).inject(Payments);
                    }
    
                    Payments.value = self.getAttribute('payment_method');
    
                }).then(function () {
                    // additional-invoice-text -> wysiwyg
                    return self.$loadAdditionalInvoiceText();
                }).then(function () {
    
    Henning Leutz's avatar
    Henning Leutz committed
                    self.getCategory('data').setActive();
    
    
                    return self.Loader.hide();
                }).then(function () {
    
    Henning Leutz's avatar
    Henning Leutz committed
                    return self.$openCategory();
    
    Henning Leutz's avatar
    Henning Leutz committed
                }).then(function () {
                    self.renderDataDone = true;
    
    Henning Leutz's avatar
    Henning Leutz committed
                });
            },
    
            /**
             * Open the product category
             *
             * @returns {Promise}
             */
    
    Henning Leutz's avatar
    Henning Leutz committed
            openArticles: function () {
    
    Henning Leutz's avatar
    Henning Leutz committed
                this.Loader.show();
    
    
                return self.$closeCategory().then(function (Container) {
    
    Henning Leutz's avatar
    Henning Leutz committed
                    return new Promise(function (resolve) {
                        require([
    
                            'package/quiqqer/invoice/bin/backend/controls/InvoiceArticleList',
                            'package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice.Summary'
                        ], function (List, Summary) {
                            self.$ArticleList = new List({
    
    Henning Leutz's avatar
    Henning Leutz committed
                                currency: self.getAttribute('currency'),
                                events  : {
    
    Henning Leutz's avatar
    Henning Leutz committed
                                    onArticleReplaceClick: self.$onArticleReplaceClick
                                },
    
    Henning Leutz's avatar
    Henning Leutz committed
                                styles  : {
    
                                    height: 'calc(100% - 120px)'
                                }
    
    Henning Leutz's avatar
    Henning Leutz committed
                            }).inject(Container);
    
    
                            Container.setStyle('height', '100%');
    
    
                            self.$ArticleListSummary = new Summary({
    
    Henning Leutz's avatar
    Henning Leutz committed
                                currency: self.getAttribute('currency'),
                                List    : self.$ArticleList,
                                styles  : {
    
                                    bottom  : -20,
    
                                    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);
    
                            self.$AddProduct.show();
                            self.$AddSeparator.show();
    
    Henning Leutz's avatar
    Henning Leutz committed
                            self.$SortSeparator.show();
                            self.$ArticleSort.show();
    
                            self.getCategory('articles').setActive();
    
    Henning Leutz's avatar
    Henning Leutz committed
                            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);
    
    
    Henning Leutz's avatar
    Henning Leutz committed
                            new QUIButton({
                                textimage: 'fa fa-check',
    
                                text     : QUILocale.get(lg, 'erp.panel.temporary.invoice.category.review.btnGoto'),
    
    Henning Leutz's avatar
    Henning Leutz committed
                                styles   : {
    
    Henning Leutz's avatar
    Henning Leutz committed
                                    'float': 'right',
                                    margin : '20px 0 0'
    
    Henning Leutz's avatar
    Henning Leutz committed
                                },
                                events   : {
    
    Henning Leutz's avatar
    Henning Leutz committed
                                }
                            }).inject(Container);
    
    
                            self.Loader.hide().then(resolve);
    
                        });
                    });
                }).then(function () {
                    return self.$openCategory();
                });
    
    Henning Leutz's avatar
    Henning Leutz committed
            },
    
            /**
             * Open the verification category
             *
             * @returns {Promise}
             */
            openVerification: function () {
    
                var self            = this,
                    ParentContainer = null,
                    FrameContainer  = null;
    
    Henning Leutz's avatar
    Henning Leutz committed
    
                this.Loader.show();
    
    
                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 : 1240,
    
                                        padding: 20,
    
                                        width  : 874
                                    },
                                    events : {
                                        onLoad: function (Box) {
    
                                            Box.getElm().addClass('quiqqer-invoice-backend-temporaryInvoice-preview');
    
                                }).inject(FrameContainer);
    
                }).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)) {
    
    Henning Leutz's avatar
    Henning Leutz committed
                        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
    
    Henning Leutz's avatar
    Henning Leutz committed
                        Missing.set('html', Mustache.render(templatePost, {
                            message: QUILocale.get(lg, 'message.invoice.ok')
                        }));
    
                            text    : QUILocale.get(lg, 'journal.btn.post'),
                            'class' : 'btn-green',
                            events  : {
    
                                onClick: self.post
    
                        }).inject(
                            Missing.getElement('.quiqqer-invoice-backend-temporaryInvoice-missing-button')
                        );
    
    Henning Leutz's avatar
    Henning Leutz committed
                    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 () {
    
    Henning Leutz's avatar
    Henning Leutz committed
                    return self.$openCategory();
                });
            },
    
            /**
             * Opens the product search
    
    Henning Leutz's avatar
    Henning Leutz committed
             *
             * @todo only if products are installed
    
    Henning Leutz's avatar
    Henning Leutz committed
             */
            openProductSearch: function () {
    
    Henning Leutz's avatar
    Henning Leutz committed
                var self = this;
    
    
    Henning Leutz's avatar
    Henning Leutz committed
                this.$AddProduct.setAttribute('textimage', 'fa fa-spinner fa-spin');
    
    
    Henning Leutz's avatar
    Henning Leutz committed
                return new Promise(function (resolve) {
                    require([
                        'package/quiqqer/invoice/bin/backend/controls/panels/product/AddProductWindow',
                        'package/quiqqer/invoice/bin/backend/controls/articles/Article'
                    ], function (Win, Article) {
                        new Win({
    
    Henning Leutz's avatar
    Henning Leutz committed
                            events: {
                                onSubmit: function (Win, article) {
                                    var Instance = new Article(article);
    
                                    if ("calculated_vatArray" in article) {
                                        Instance.setVat(article.calculated_vatArray.vat);
                                    }
    
    Henning Leutz's avatar
    Henning Leutz committed
                                    self.$ArticleList.addArticle(Instance);
                                    resolve(Instance);
                                }
    
    Henning Leutz's avatar
    Henning Leutz committed
                            }
    
    Henning Leutz's avatar
    Henning Leutz committed
                        }).open();
    
    Henning Leutz's avatar
    Henning Leutz committed
                        self.$AddProduct.setAttribute('textimage', 'fa fa-plus');
                    });
    
    Henning Leutz's avatar
    Henning Leutz committed
                });
    
            /**
             *
             * @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's avatar
    Henning Leutz committed
            /**
    
             * Close the current category and save it
    
    Henning Leutz's avatar
    Henning Leutz committed
             * @returns {Promise}
    
    Henning Leutz's avatar
    Henning Leutz committed
            $closeCategory: function () {
    
                var self = this;
    
                if (self.$AddProduct) {
                    self.$AddProduct.hide();
                    self.$AddSeparator.hide();
                    self.$SortSeparator.hide();
                    self.$ArticleSort.hide();
    
                if (self.$ArticleListSummary) {
                    moofx(self.$ArticleListSummary.getElm()).animate({
    
                        bottom : -20,
                        opacity: 0
                    }, {
                        duration: 250,
                        callback: function () {
    
                            self.$ArticleListSummary.destroy();
                            self.$ArticleListSummary = null;
                        }
    
                self.getContent().setStyle('padding', 0);
    
    Henning Leutz's avatar
    Henning Leutz committed
                return new Promise(function (resolve) {
    
                    var Container = self.getContent().getElement('.container');
    
    Henning Leutz's avatar
    Henning Leutz committed
                    if (!Container) {
                        Container = new Element('div', {
                            'class': 'container',
    
    Henning Leutz's avatar
    Henning Leutz committed
                            styles : {
                                opacity : 0,
    
    Henning Leutz's avatar
    Henning Leutz committed
                                position: 'relative',
    
    Henning Leutz's avatar
    Henning Leutz committed
                                top     : -50
    
    Henning Leutz's avatar
    Henning Leutz committed
                    }
    
                    moofx(Container).animate({
                        opacity: 0,
    
    Henning Leutz's avatar
    Henning Leutz committed
                        top    : -50
    
    Henning Leutz's avatar
    Henning Leutz committed
                    }, {
                        duration: 200,
                        callback: function () {
    
    Henning Leutz's avatar
    Henning Leutz committed
                            Container.set('html', '');
    
                            Container.setStyle('padding', 20);
    
                            self.save().then(function () {
                                resolve(Container);
                            }).catch(function () {
                                resolve(Container);
                            });
                        }
    
    Henning Leutz's avatar
    Henning Leutz committed
                    });
    
    Henning Leutz's avatar
    Henning Leutz committed
            },
    
            /**
             * Open the current category
             *
             * @returns {Promise}
             */
            $openCategory: function () {
                var self = this;
    
                return new Promise(function (resolve) {
                    var Container = self.getContent().getElement('.container');
    
                    if (!Container) {
                        resolve();
                        return;
                    }
    
                    moofx(Container).animate({
                        opacity: 1,
    
    Henning Leutz's avatar
    Henning Leutz committed
                        top    : 0
    
    Henning Leutz's avatar
    Henning Leutz committed
                    }, {
                        duration: 200,
                        callback: resolve
                    });
                });
    
            /**
             * Unload the category and reserve the data
    
    Henning Leutz's avatar
    Henning Leutz committed
             *
    
    Henning Leutz's avatar
    Henning Leutz committed
             * @param {Boolean} [destroyList] - destroy the article list, default = true
    
    Henning Leutz's avatar
    Henning Leutz committed
            $unloadCategory: function (destroyList) {
    
                var Container = this.getContent().getElement('.container');
    
    
    Henning Leutz's avatar
    Henning Leutz committed
                destroyList = typeof destroyList === 'undefined' ? true : destroyList;
    
    
                if (this.$ArticleList) {
                    this.setAttribute('articles', this.$ArticleList.save());
                    this.$serializedList = this.$ArticleList.serialize();
    
    
    Henning Leutz's avatar
    Henning Leutz committed
                    if (destroyList) {
                        this.$ArticleList.destroy();
                        this.$ArticleList = null;
                    }
    
                if (this.$AdditionalText) {
                    this.setAttribute(
                        'additional_invoice_text',
                        this.$AdditionalText.getContent()
                    );
                }
    
    
                var Form = Container.getElement('form');
    
                if (!Form) {
                    return;
                }
    
                var formData = QUIFormUtils.getFormData(Form);
                var data     = this.getAttribute('data') || {};
    
                // timefields
    
                if ("date" in formData) {
                    this.setAttribute('date', formData.date + ' 00:00:00');
    
                [
                    'time_for_payment',
                    'project_name',
                    'payment_method',
                    'editor_id',
                    'ordered_by'
                ].each(function (entry) {
    
                    if (!formData.hasOwnProperty(entry)) {
                        return;
                    }
    
    
                    if (entry === 'time_for_payment') {
                        formData[entry] = parseInt(formData[entry]);
                    }
    
    
                    this.setAttribute(entry, formData[entry]);
                    delete formData[entry];
                }.bind(this));
    
                this.setAttribute('data', Object.merge(data, formData));
            },
    
    
            /**
             * Event Handling
             */
    
    
    Henning Leutz's avatar
    Henning Leutz committed
            /**
             * event: on create
             */
            $onCreate: function () {
    
    Henning Leutz's avatar
    Henning Leutz committed
                var self = this;
    
                this.$AddProduct = new QUIButtonMultiple({
                    textimage: 'fa fa-plus',
    
    Henning Leutz's avatar
    Henning Leutz committed
                    text     : QUILocale.get(lg, 'erp.panel.temporary.invoice.buttonAdd'),
    
    Henning Leutz's avatar
    Henning Leutz committed
                    events   : {
    
    Henning Leutz's avatar
    Henning Leutz committed
                        onClick: function () {
    
    Henning Leutz's avatar
    Henning Leutz committed
                            if (self.$ArticleList) {
    
    Henning Leutz's avatar
    Henning Leutz committed
                                self.openProductSearch();
                            }
                        }
                    }
                });
    
                this.$AddProduct.hide();
    
                this.$AddProduct.appendChild({
    
    Henning Leutz's avatar
    Henning Leutz committed
                    text  : QUILocale.get(lg, 'erp.panel.temporary.invoice.buttonAdd.custom'),
    
    Henning Leutz's avatar
    Henning Leutz committed
                    events: {
                        onClick: function () {
    
    Henning Leutz's avatar
    Henning Leutz committed
                            if (self.$ArticleList) {
                                self.$ArticleList.insertNewProduct();
    
    Henning Leutz's avatar
    Henning Leutz committed
                            }
                        }
                    }
                });
    
                this.$AddProduct.appendChild({
    
    Henning Leutz's avatar
    Henning Leutz committed
                    text  : QUILocale.get(lg, 'erp.panel.temporary.invoice.buttonAdd.text'),
    
    Henning Leutz's avatar
    Henning Leutz committed
                    events: {
                        onClick: function () {
    
    Henning Leutz's avatar
    Henning Leutz committed
                            if (self.$ArticleList) {
                                self.$ArticleList.addArticle(new TextArticle());
    
    Henning Leutz's avatar
    Henning Leutz committed
                this.$AddSeparator  = new QUISeparator();
                this.$SortSeparator = new QUISeparator();
    
    Henning Leutz's avatar
    Henning Leutz committed
    
                // buttons
                this.addButton({
    
    Henning Leutz's avatar
    Henning Leutz committed
                    name     : 'save',
    
    Henning Leutz's avatar
    Henning Leutz committed
                    text     : QUILocale.get('quiqqer/system', 'save'),
    
    Henning Leutz's avatar
    Henning Leutz committed
                    textimage: 'fa fa-save',
    
    Henning Leutz's avatar
    Henning Leutz committed
                    events   : {
    
                        onClick: function () {
                            //quiqqer/invoice', 'message.invoice.save.successfully'
                            self.save().then(function () {
                                QUI.getMessageHandler().then(function (MH) {
                                    MH.addSuccess(
                                        QUILocale.get('quiqqer/invoice', 'message.invoice.save.successfully')
                                    );
                                });
                            });
                        }