Skip to content
Code-Schnipsel Gruppen Projekte
Dialogs.js 17,2 KiB
Newer Older
  • Learn to ignore specific revisions
  • Henning Leutz's avatar
    Henning Leutz committed
    /**
     * @module package/quiqqer/invoice/bin/backend/utils/Dialogs
     * @author www.pcsg.de (Henning Leutz)
    
     *
     * @event (global) onQuiqqerInvoiceCreateCreditNoteDialogOpen [invoiceId, Win]
     * @event (global) onQuiqqerInvoiceCreateCreditNoteDialogSubmit [creditNoteId, Win]
    
    Henning Leutz's avatar
    Henning Leutz committed
     */
    define('package/quiqqer/invoice/bin/backend/utils/Dialogs', [
    
        'qui/QUI',
        'Locale',
        'package/quiqqer/invoice/bin/Invoices',
    
        'qui/controls/windows/Popup',
    
    
        'css!package/quiqqer/invoice/bin/backend/utils/Dialogs.css'
    
    ], function (QUI, QUILocale, Invoices, QUIConfirm, QUIPopup) {
    
        'use strict';
    
        const lg = 'quiqqer/invoice';
    
    Henning Leutz's avatar
    Henning Leutz committed
    
        return {
    
            /**
             * Opens the print dialog for a specific invoice
             *
             * @param {String} invoiceId - Invoice ID or Hash
    
             * @param {String} [entityType]
    
    Henning Leutz's avatar
    Henning Leutz committed
             * @return {Promise}
             */
    
            openPrintDialog: function (invoiceId, entityType) {
    
                entityType = entityType || 'Invoice';
    
    
                return Invoices.getInvoiceHistory(invoiceId).then(function (comments) {
                    return new Promise(function (resolve) {
    
                        require([
                            'package/quiqqer/erp/bin/backend/controls/OutputDialog'
    
                        ], function (OutputDialog) {
    
                                entityId: invoiceId,
    
                                entityPlugin: 'quiqqer/invoice',
    
                                comments: comments.length ? comments : false
    
    Henning Leutz's avatar
    Henning Leutz committed
                    });
                });
            },
    
            /**
             * Opens a storno / cancellation dialog for a specific invoice
             *
             * @param {String} invoiceId - Invoice ID or Hash
             * @return {Promise}
             */
    
            openStornoDialog: function (invoiceId) {
                return Invoices.get(invoiceId).then(function (result) {
    
                    const id = result.id_prefix + result.id;
    
                    return new Promise(function (resolve, reject) {
    
    Henning Leutz's avatar
    Henning Leutz committed
                        new QUIConfirm({
    
                            icon: 'fa fa-ban',
                            texticon: 'fa fa-ban',
                            title: QUILocale.get(lg, 'dialog.invoice.reversal.title', {
    
    Henning Leutz's avatar
    Henning Leutz committed
                                invoiceId: id
                            }),
    
                            text: QUILocale.get(lg, 'dialog.invoice.reversal.text', {
    
    Henning Leutz's avatar
    Henning Leutz committed
                                invoiceId: id
                            }),
                            information: QUILocale.get(lg, 'dialog.invoice.reversal.information', {
                                invoiceId: id
                            }),
    
                            autoclose: false,
                            ok_button: {
                                text: QUILocale.get(lg, 'dialog.invoice.reversal.submit'),
    
    Henning Leutz's avatar
    Henning Leutz committed
                                textimage: 'fa fa-ban'
    
                            maxHeight: 500,
                            maxWidth: 750,
                            events: {
    
                                onOpen: function (Win) {
    
                                    const Container = Win.getContent().getElement('.textbody');
    
    Henning Leutz's avatar
    Henning Leutz committed
    
                                    // #locale
    
                                    const Label = new Element('label', {
                                        html: '<span>' + QUILocale.get(
                                            lg,
                                            'dialog.invoice.reversal.reason.title'
                                        ) + '</span>',
    
    Henning Leutz's avatar
    Henning Leutz committed
                                        styles: {
    
                                            display: 'block',
    
    Henning Leutz's avatar
    Henning Leutz committed
                                            fontWeight: 'bold',
    
                                            marginTop: 20,
                                            width: 'calc(100% - 100px)'
    
    Henning Leutz's avatar
    Henning Leutz committed
                                        }
                                    }).inject(Container);
    
    
                                    const Reason = new Element('textarea', {
                                        name: 'reason',
                                        autofocus: true,
    
    Henning Leutz's avatar
    Henning Leutz committed
                                        placeholder: QUILocale.get(lg, 'dialog.invoice.reversal.reason.placeholder'),
    
                                        styles: {
                                            height: 160,
    
    Henning Leutz's avatar
    Henning Leutz committed
                                            marginTop: 10,
    
                                            width: '100%'
    
    Henning Leutz's avatar
    Henning Leutz committed
                                        }
                                    }).inject(Label);
    
                                    Reason.focus();
                                },
    
                                onSubmit: function (Win) {
    
                                    const Reason = Win.getContent().getElement('[name="reason"]');
                                    const value = Reason.value;
    
    Henning Leutz's avatar
    Henning Leutz committed
    
                                    if (value === '') {
    
                                        Reason.focus();
                                        Reason.required = true;
    
                                        if ('reportValidity' in Reason) {
                                            Reason.reportValidity();
                                        }
    
    
    Henning Leutz's avatar
    Henning Leutz committed
                                        return;
                                    }
    
    Henning Leutz's avatar
    Henning Leutz committed
                                    Win.Loader.show();
    
                                    Invoices.reversalInvoice(result.hash, value).then(function (result) {
    
    Henning Leutz's avatar
    Henning Leutz committed
                                        Win.close();
    
    Henning Leutz's avatar
    Henning Leutz committed
                                        resolve(result);
    
                                    }).catch(function (Exception) {
    
    Henning Leutz's avatar
    Henning Leutz committed
                                        Win.close();
                                        reject(Exception);
                                    });
                                },
    
    Henning Leutz's avatar
    Henning Leutz committed
                                onCancel: resolve
                            }
                        }).open();
                    });
    
    Henning Leutz's avatar
    Henning Leutz committed
                });
            },
    
            /**
             * Alias for openStornoDialog()
             *
             * @param {String} invoiceId - Invoice ID or Hash
             * @return {*|Promise}
             */
    
            openCancellationDialog: function (invoiceId) {
    
    Henning Leutz's avatar
    Henning Leutz committed
                return this.openStornoDialog(invoiceId);
            },
    
            /**
             * Alias for openStornoDialog()
             *
             * @param {String} invoiceId - Invoice ID or Hash
             * @return {*|Promise}
             */
    
            openReversalDialog: function (invoiceId) {
    
    Henning Leutz's avatar
    Henning Leutz committed
                return this.openStornoDialog(invoiceId);
            },
    
            /**
             * Opens a copy dialog for a specific invoice
             *
             * @param {String} invoiceId - Invoice ID or Hash
             * @return {Promise}
             */
    
            openCopyDialog: function (invoiceId) {
                return Invoices.get(invoiceId).then(function (result) {
    
                    const id = result.id_prefix + result.id;
    
                    return new Promise(function (resolve) {
    
    Henning Leutz's avatar
    Henning Leutz committed
                        new QUIConfirm({
    
                            title: QUILocale.get(lg, 'dialog.invoice.copy.title'),
                            text: QUILocale.get(lg, 'dialog.invoice.copy.text'),
    
    Henning Leutz's avatar
    Henning Leutz committed
                            information: QUILocale.get(lg, 'dialog.invoice.copy.information', {
                                id: id
                            }),
    
                            icon: 'fa fa-copy',
                            texticon: 'fa fa-copy',
                            maxHeight: 400,
                            maxWidth: 600,
                            autoclose: false,
                            ok_button: {
                                text: QUILocale.get('quiqqer/system', 'copy'),
    
    Henning Leutz's avatar
    Henning Leutz committed
                                textimage: 'fa fa-copy'
                            },
    
                                onSubmit: function (Win) {
    
    Henning Leutz's avatar
    Henning Leutz committed
                                    Win.Loader.show();
    
    
                                    Invoices.copyInvoice(result.hash).then(function (newId) {
    
    Henning Leutz's avatar
    Henning Leutz committed
                                        Win.close();
                                        resolve(newId);
    
                                    }).then(function () {
    
    Henning Leutz's avatar
    Henning Leutz committed
                                        Win.Loader.hide();
                                    });
                                }
    
    Henning Leutz's avatar
    Henning Leutz committed
                        }).open();
                    });
    
    Henning Leutz's avatar
    Henning Leutz committed
                });
            },
    
            /**
             * Opens a credit note dialog for a specific invoice
             *
             * @param {String} invoiceId - Invoice ID or Hash
             * @return {Promise}
             */
    
            openCreateCreditNoteDialog: function (invoiceId) {
    
                const self = this;
    
                return Invoices.get(invoiceId).then(function (result) {
    
                    let paymentHasRefund = false;
                    const id = result.id_prefix + result.id;
    
                    return new Promise(function (resolve, reject) {
    
    Henning Leutz's avatar
    Henning Leutz committed
                        new QUIConfirm({
    
                            icon: 'fa fa-clipboard',
                            texticon: 'fa fa-clipboard',
                            title: QUILocale.get(lg, 'dialog.invoice.createCreditNote.title', {
    
    Henning Leutz's avatar
    Henning Leutz committed
                                invoiceId: id
                            }),
    
                            text: QUILocale.get(lg, 'dialog.invoice.createCreditNote.text', {
    
    Henning Leutz's avatar
    Henning Leutz committed
                                invoiceId: id
                            }),
                            information: QUILocale.get(lg, 'dialog.invoice.createCreditNote.information', {
                                invoiceId: id
                            }),
    
                            autoclose: false,
                            ok_button: {
                                text: QUILocale.get(lg, 'dialog.invoice.createCreditNote.submit'),
    
    Henning Leutz's avatar
    Henning Leutz committed
                                textimage: 'fa fa-clipboard'
    
                            maxHeight: 400,
                            maxWidth: 600,
                            events: {
    
                                onOpen: function (Win) {
    
                                    Invoices.hasRefund(id).then(function (hasRefund) {
    
                                        paymentHasRefund = hasRefund;
    
    
                                        QUI.fireEvent('quiqqerInvoiceCreateCreditNoteDialogOpen', [id, Win]);
    
    
                                        if (!paymentHasRefund) {
    
                                        const Content = Win.getContent(),
                                            Body = Content.getElement('.textbody');
    
    
                                        new Element('label', {
                                            'class': 'quiqqer-invoice-dialog-refund-label',
    
                                            html: '<input type="checkbox" name="refund" />' + QUILocale.get(
                                                lg,
                                                'dialog.invoice.createCreditNote.refund'
                                            ),
                                            styles: {
                                                cursor: 'pointer',
                                                display: 'block',
    
                                onSubmit: function (Win) {
    
    Henning Leutz's avatar
    Henning Leutz committed
                                    Win.Loader.show();
    
    
                                    const Content = Win.getContent(),
                                        Refund = Content.getElement('[name="refund"]');
    
                                    const createInvoice = function (values) {
    
                                        values = values || {};
    
    
                                        Invoices.createCreditNote(result.hash, values).then(function (newId) {
    
                                            QUI.fireEvent(
                                                'quiqqerInvoiceCreateCreditNoteDialogSubmit',
                                                [newId, Win]
                                            );
    
    
                                            resolve(newId);
                                            Win.close();
    
                                        }).catch(function (Err) {
    
                                            Win.Loader.hide();
                                            console.error(Err);
    
                                            reject(Err);
                                        });
                                    };
    
    
                                    if (paymentHasRefund && Refund.checked) {
    
                                        self.openRefundWindow(invoiceId).then(function (RefundWindow) {
    
                                            if (!RefundWindow) {
                                                Win.Loader.hide();
                                                return;
                                            }
    
                                            createInvoice({
                                                refund: RefundWindow.getValues()
                                            });
    
                                        }).catch(function (Err) {
    
                                            Win.Loader.hide();
                                            console.error(Err);
                                        });
                                        return;
                                    }
    
                                    createInvoice();
    
                                onCancel: function () {
    
    Henning Leutz's avatar
    Henning Leutz committed
                                    resolve(false);
                                }
    
    Henning Leutz's avatar
    Henning Leutz committed
                        }).open();
                    });
    
    Henning Leutz's avatar
    Henning Leutz committed
                });
    
            },
    
            /**
             *
             * @param invoiceId
             * @return {Promise}
             */
    
            openRefundWindow: function (invoiceId) {
                return new Promise(function (resolve) {
    
                    require([
                        'package/quiqqer/invoice/bin/backend/controls/panels/refund/Window'
    
                    ], function (RefundWindow) {
    
                        new RefundWindow({
    
                            invoiceId: invoiceId,
    
                            autoRefund: false,
    
                                onSubmit: resolve,
    
                                onCancel: function () {
    
                                    resolve(false);
                                }
                            }
                        }).open();
                    });
                });
    
            },
    
            openDownloadDialog: function (hash) {
                new QUIPopup({
                    icon: 'fa fa-download',
                    title: QUILocale.get(lg, 'dialog.invoice.download.title'),
                    autoclose: false,
                    maxHeight: 400,
                    maxWidth: 600,
                    buttons: false,
                    events: {
                        onOpen: function (Win) {
                            Win.Loader.show();
    
                            const Content = Win.getContent();
                            Content.classList.add('quiqqer-invoice-download-dialog');
    
                            Content.set(
                                'html',
    
                                '<h3>' + QUILocale.get(lg, 'dialog.invoice.download.header') + '</h3>' +
                                QUILocale.get(lg, 'dialog.invoice.download.text') +
                                '<div class="quiqqer-invoice-download-dialog-buttons">' +
                                '   <button value="PDF" class="qui-button">PDF</button>' +
                                '   <button value="PROFILE_BASIC" class="qui-button">ZUGFeRD Basic</button>' +
                                '   <button value="PROFILE_EN16931" class="qui-button">ZUGFeRD EN16931</button>' +
                                '   <button value="PROFILE_EXTENDED" class="qui-button">ZUGFeRD Extended</button>' +
                                '   <button value="PROFILE_XRECHNUNG_2_3" class="qui-button">XRechnung 2.3</button>' +
                                '   <button value="PROFILE_XRECHNUNG_3" class="qui-button">XRechnung 3</button>' +
                                '</div>'
                            );
    
                            Content.querySelectorAll('button').forEach(function (Button) {
                                Button.addEventListener('click', function () {
                                    const id = 'download-invoice-' + hash + '-' + Button.value;
    
                                    new Element('iframe', {
                                        src: URL_OPT_DIR + 'quiqqer/invoice/bin/backend/download.php?' + Object.toQueryString({
                                            invoice: hash,
                                            type: Button.value
                                        }),
                                        id: id,
                                        styles: {
                                            position: 'absolute',
                                            top: -200,
                                            left: -200,
                                            width: 50,
                                            height: 50
                                        }
                                    }).inject(document.body);
    
                                    (function () {
                                        document.getElements('#' + id).destroy();
                                    }).delay(1000, this);
                                });
                            });
    
                            Win.Loader.hide();
                        }
                    }
                }).open();