From beeb6d7f80cef8545eb78ee3a151841936df76c0 Mon Sep 17 00:00:00 2001
From: Henning Leutz <leutz@pcsg.de>
Date: Fri, 10 Jan 2020 09:13:06 +0100
Subject: [PATCH] feat: quiqqer/invoice#54 - comments for temporary invoices &
 invoices

---
 ajax/invoices/addComment.php                  |  16 +-
 .../controls/panels/TemporaryInvoice.js       | 169 ++++++++++++++++--
 locale.xml                                    |   9 +
 .../Accounting/Invoice/InvoiceTemporary.php   |  18 ++
 4 files changed, 192 insertions(+), 20 deletions(-)

diff --git a/ajax/invoices/addComment.php b/ajax/invoices/addComment.php
index ff4e25e..61ec5f3 100644
--- a/ajax/invoices/addComment.php
+++ b/ajax/invoices/addComment.php
@@ -14,7 +14,21 @@
     'package_quiqqer_invoice_ajax_invoices_addComment',
     function ($invoiceId, $comment) {
         $Invoices = QUI\ERP\Accounting\Invoice\Handler::getInstance();
-        $Invoice  = $Invoices->getInvoice($invoiceId);
+        $Invoice  = null;
+
+        try {
+            $Invoice = $Invoices->getInvoice($invoiceId);
+        } catch (\Exception $Exception) {
+            QUI\System\Log::addDebug($Exception->getMessage());
+        }
+
+        if ($Invoice === null) {
+            try {
+                $Invoice = $Invoices->getTemporaryInvoice($invoiceId);
+            } catch (\Exception $Exception) {
+                QUI\System\Log::addDebug($Exception->getMessage());
+            }
+        }
 
         $Invoice->addComment($comment);
     },
diff --git a/bin/backend/controls/panels/TemporaryInvoice.js b/bin/backend/controls/panels/TemporaryInvoice.js
index 34cfaaf..25c0ba0 100644
--- a/bin/backend/controls/panels/TemporaryInvoice.js
+++ b/bin/backend/controls/panels/TemporaryInvoice.js
@@ -15,6 +15,7 @@ define('package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice', [
     'qui/utils/Form',
     'controls/users/address/Select',
     'package/quiqqer/invoice/bin/Invoices',
+    'package/quiqqer/erp/bin/backend/controls/Comments',
     'package/quiqqer/invoice/bin/backend/controls/articles/Text',
     'package/quiqqer/payments/bin/backend/Payments',
     'utils/Lock',
@@ -30,7 +31,7 @@ define('package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice', [
     'css!package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice.css'
 
 ], function (QUI, QUIPanel, QUIButton, QUIButtonMultiple, QUISeparator, QUIConfirm, QUIFormUtils,
-             AddressSelect, Invoices, TextArticle,
+             AddressSelect, Invoices, Comments, TextArticle,
              Payments, Locker, QUILocale, Mustache, Users, Editors,
              templateData, templatePost, templateMissing) {
     "use strict";
@@ -47,6 +48,8 @@ define('package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice', [
             'post',
             'openData',
             'openArticles',
+            'openComments',
+            'openAddCommentDialog',
             'openVerification',
             '$openCategory',
             '$closeCategory',
@@ -140,6 +143,32 @@ define('package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice', [
             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.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();
+            });
+        },
+
         /**
          * Saves the current data
          *
@@ -549,6 +578,51 @@ define('package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice', [
             });
         },
 
+        /**
+         * 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
          *
@@ -1073,6 +1147,15 @@ define('package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice', [
                 }
             });
 
+            this.addCategory({
+                name  : 'comments',
+                icon  : 'fa fa-comments',
+                text  : QUILocale.get(lg, 'erp.panel.temporary.invoice.category.comments'),
+                events: {
+                    onClick: this.openComments
+                }
+            });
+
             this.addCategory({
                 name  : 'verification',
                 icon  : 'fa fa-check',
@@ -1114,24 +1197,8 @@ define('package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice', [
                     self.$getLockGroups()
                 );
             }).then(function () {
-                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();
-
+                return self.doRefresh();
+            }).then(function () {
                 return Invoices.getMissingAttributes(invoiceId);
             }).then(function (missing) {
                 if (Object.getLength(missing)) {
@@ -1394,6 +1461,70 @@ define('package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice', [
             }
 
             this.$ArticleSort.setNormal();
+        },
+
+        //region comments
+
+        /**
+         * Open the add dialog window
+         */
+        openAddCommentDialog: function () {
+            var self = this;
+
+            new QUIConfirm({
+                title    : QUILocale.get(lg, 'dialog.add.comment.title'),
+                icon     : 'fa fa-edit',
+                maxHeight: 600,
+                maxWidth : 800,
+                events   : {
+                    onOpen: function (Win) {
+                        Win.getContent().set('html', '');
+                        Win.Loader.show();
+
+                        require([
+                            'Editors'
+                        ], function (Editors) {
+                            Editors.getEditor(null).then(function (Editor) {
+                                Win.$Editor = Editor;
+
+                                Win.$Editor.addEvent('onLoaded', function () {
+                                    Win.$Editor.switchToWYSIWYG();
+                                    Win.$Editor.showToolbar();
+                                    Win.$Editor.setContent(self.getAttribute('content'));
+                                    Win.Loader.hide();
+                                });
+
+                                Win.$Editor.inject(Win.getContent());
+                                Win.$Editor.setHeight(200);
+                            });
+                        });
+                    },
+
+                    onSubmit: function (Win) {
+                        Win.Loader.show();
+
+                        self.addComment(Win.$Editor.getContent()).then(function () {
+                            return self.doRefresh();
+                        }).then(function () {
+                            Win.$Editor.destroy();
+                            Win.close();
+
+                            self.refreshComments();
+                        });
+                    }
+                }
+            }).open();
+        },
+
+        /**
+         * add a comment to the order
+         *
+         * @param {String} message
+         */
+        addComment: function (message) {
+            return Invoices.addComment(this.getAttribute('invoiceId'), message);
         }
+
+        //endregion
     });
 });
diff --git a/locale.xml b/locale.xml
index 0e0f3e8..c57dd25 100644
--- a/locale.xml
+++ b/locale.xml
@@ -714,6 +714,10 @@
             <de><![CDATA[Zusätzlicher Rechnungstext]]></de>
             <en><![CDATA[Additional invoice text]]></en>
         </locale>
+        <locale name="erp.panel.temporary.invoice.category.comments">
+            <de><![CDATA[Kommentare]]></de>
+            <en><![CDATA[Comments]]></en>
+        </locale>
         <locale name="erp.panel.temporary.invoice.deleteButton.title">
             <de><![CDATA[Rechnung löschen]]></de>
             <en><![CDATA[Delete Invoice]]></en>
@@ -1139,6 +1143,11 @@
             <en><![CDATA[Date]]></en>
         </locale>
 
+        <locale name="dialog.add.comment.title">
+            <de><![CDATA[Kommentar hinzufügen]]></de>
+            <en><![CDATA[Add new comment]]></en>
+        </locale>
+
         <locale name="exception.post.invoices.missing.attributes">
             <de><![CDATA[Die Rechnungen können leider nicht gebucht werden, einige besitzen Fehler.]]></de>
             <en><![CDATA[Unfortunately, the invoices cannot be posted, some have errors.]]></en>
diff --git a/src/QUI/ERP/Accounting/Invoice/InvoiceTemporary.php b/src/QUI/ERP/Accounting/Invoice/InvoiceTemporary.php
index fc323dd..ac7a74e 100644
--- a/src/QUI/ERP/Accounting/Invoice/InvoiceTemporary.php
+++ b/src/QUI/ERP/Accounting/Invoice/InvoiceTemporary.php
@@ -1320,6 +1320,11 @@ public function getComments()
      */
     public function addComment($message)
     {
+        $message = \strip_tags($message, '<div><span><pre><p><br><hr>
+            <ul><ol><li><dl><dt><dd><strong><em><b><i><u>
+            <img><table><tbody><td><tfoot><th><thead><tr>'
+        );
+
         $this->Comments->addComment($message);
         $this->save();
 
@@ -1327,6 +1332,19 @@ public function addComment($message)
             'quiqqerInvoiceTemporaryInvoiceAddComment',
             [$this, $message]
         );
+
+        $User = QUI::getUserBySession();
+        
+        $this->addHistory(
+            QUI::getLocale()->get(
+                'quiqqer/invoice',
+                'history.message.addComment',
+                [
+                    'username' => $User->getName(),
+                    'uid'      => $User->getId()
+                ]
+            )
+        );
     }
 
     //endregion
-- 
GitLab