From 2444277fa7a56855b378466e77f702ab8a8151ea Mon Sep 17 00:00:00 2001
From: Henning Leutz <leutz@pcsg.de>
Date: Mon, 16 Mar 2020 15:18:17 +0100
Subject: [PATCH] fix: quiqqer/products#234

---
 ajax/frontend/redeem.php                  |  9 ++-
 ajax/frontend/removeCoupons.php           | 40 ++++++++++++
 bin/frontend/controls/CouponCodeInput.css |  6 +-
 bin/frontend/controls/CouponCodeInput.js  | 76 +++++++++++++++++++++--
 locale.xml                                |  5 ++
 5 files changed, 128 insertions(+), 8 deletions(-)
 create mode 100644 ajax/frontend/removeCoupons.php

diff --git a/ajax/frontend/redeem.php b/ajax/frontend/redeem.php
index 45e12d2..2c1bde4 100644
--- a/ajax/frontend/redeem.php
+++ b/ajax/frontend/redeem.php
@@ -38,8 +38,13 @@ function ($code, $orderHash) {
             return false;
         }
 
-        $Order     = QUI\ERP\Order\Handler::getInstance()->getOrderByHash($orderHash);
-        $coupons   = $Order->getDataEntry('quiqqer-coupons');
+        $Order   = QUI\ERP\Order\Handler::getInstance()->getOrderByHash($orderHash);
+        $coupons = $Order->getDataEntry('quiqqer-coupons');
+
+        if (empty($coupons)) {
+            $coupons = [];
+        }
+
         $coupons[] = $code;
 
         $coupons = \array_unique($coupons);
diff --git a/ajax/frontend/removeCoupons.php b/ajax/frontend/removeCoupons.php
new file mode 100644
index 0000000..d9a4857
--- /dev/null
+++ b/ajax/frontend/removeCoupons.php
@@ -0,0 +1,40 @@
+<?php
+
+/**
+ * This file contains package_quiqqer_coupons_ajax_frontend_removeCoupons
+ */
+
+/**
+ * Remove all coupons from an order
+ *
+ * @param string $orderHash - Order hash
+ */
+QUI::$Ajax->registerFunction(
+    'package_quiqqer_coupons_ajax_frontend_removeCoupons',
+    function ($orderHash) {
+        $Order    = QUI\ERP\Order\Handler::getInstance()->getOrderByHash($orderHash);
+        $Articles = $Order->getArticles();
+
+        foreach ($Articles as $index => $Article) {
+            $customData = $Article->getCustomData();
+
+            if (!isset($customData['package'])) {
+                continue;
+            }
+
+            if ($customData['package'] !== 'quiqqer/coupon') {
+                continue;
+            }
+
+            if (!isset($customData['code'])) {
+                continue;
+            }
+
+            $Order->removeArticle($index);
+        }
+
+        $Order->setData('quiqqer-coupons', []);
+        $Order->save();
+    },
+    ['orderHash']
+);
diff --git a/bin/frontend/controls/CouponCodeInput.css b/bin/frontend/controls/CouponCodeInput.css
index d292430..b283f8e 100644
--- a/bin/frontend/controls/CouponCodeInput.css
+++ b/bin/frontend/controls/CouponCodeInput.css
@@ -23,4 +23,8 @@ div + .quiqqer-coupons-field {
 .quiqqer-coupons-couponcodeinput input {
     line-height: 36px;
     margin-right: 1rem;
-}
\ No newline at end of file
+}
+
+.quiqqer-coupons-remove .fa {
+    margin-right: 0;
+}
diff --git a/bin/frontend/controls/CouponCodeInput.js b/bin/frontend/controls/CouponCodeInput.js
index 146bc73..a055182 100644
--- a/bin/frontend/controls/CouponCodeInput.js
+++ b/bin/frontend/controls/CouponCodeInput.js
@@ -75,6 +75,35 @@ define('package/quiqqer/coupons/bin/frontend/controls/CouponCodeInput', [
                 event.stop();
                 self.$submit();
             });
+
+            if (this.isInOrder()) {
+                var OrderProcess = this.getOrderProcess();
+
+                new Promise(function (resolve) {
+                    if (!OrderProcess.isLoaded()) {
+                        return OrderProcess.addEvent('onLoad', resolve);
+                    }
+
+                    resolve();
+                }).then(function () {
+                    if (OrderProcess.getAttribute('basketEditable') === false) {
+                        // remove coupon codes button
+                        new Element('button', {
+                            'class': 'quiqqer-coupons-remove',
+                            'html' : '<span class="fa fa-close"></span>',
+                            title  : QUILocale.get(lg, 'remove.coupons.button'),
+                            events : {
+                                click: function (e) {
+                                    e.stop();
+                                    self.removeCouponsFromOrder().catch(function (e) {
+                                        console.error(e);
+                                    });
+                                }
+                            }
+                        }).inject(self.$Elm.getElement('.quiqqer-coupons-couponcodeinput'));
+                    }
+                });
+            }
         },
 
         /**
@@ -97,10 +126,6 @@ define('package/quiqqer/coupons/bin/frontend/controls/CouponCodeInput', [
             this.Loader.show();
 
             // get order process
-            var OrderProcessNode = this.getElm().getParent(
-                '[data-qui="package/quiqqer/order/bin/frontend/controls/OrderProcess"]'
-            );
-
             if (!this.isInOrder()) {
                 QUIAjax.get('package_quiqqer_order_ajax_frontend_basket_getLastOrder', function (order) {
                     CouponCodes.addCouponCodeToBasket(code, order.hash).then(function (redeemed) {
@@ -123,7 +148,7 @@ define('package/quiqqer/coupons/bin/frontend/controls/CouponCodeInput', [
                 return;
             }
 
-            var OrderProcess = window.QUI.Controls.getById(OrderProcessNode.get('data-quiid'));
+            var OrderProcess = this.getOrderProcess();
 
             OrderProcess.Loader.show();
 
@@ -152,6 +177,47 @@ define('package/quiqqer/coupons/bin/frontend/controls/CouponCodeInput', [
             );
 
             return !!OrderProcessNode;
+        },
+
+        /**
+         * Return the order process
+         *
+         * @return {Object}
+         */
+        getOrderProcess: function () {
+            var OrderProcessNode = this.getElm().getParent(
+                '[data-qui="package/quiqqer/order/bin/frontend/controls/OrderProcess"]'
+            );
+
+            return window.QUI.Controls.getById(OrderProcessNode.get('data-quiid'));
+        },
+
+        /**
+         * Remove all coupons from the order
+         *
+         * @return {Promise}
+         */
+        removeCouponsFromOrder: function () {
+            if (!this.isInOrder()) {
+                return Promise.resolve();
+            }
+
+            var self = this;
+
+            return new Promise(function (resolve) {
+                var OrderProcess = self.getOrderProcess();
+
+                OrderProcess.Loader.show();
+                OrderProcess.getOrder().then(function (orderHash) {
+                    QUIAjax.get('package_quiqqer_coupons_ajax_frontend_removeCoupons', function () {
+                        resolve();
+                        OrderProcess.reload();
+                    }, {
+                        'package': 'quiqqer/coupon',
+                        orderHash: orderHash
+                    });
+                });
+            });
         }
     });
 });
diff --git a/locale.xml b/locale.xml
index 02b516e..bdffa68 100644
--- a/locale.xml
+++ b/locale.xml
@@ -390,5 +390,10 @@
             <de><![CDATA[Einlösen]]></de>
             <en><![CDATA[Redeem]]></en>
         </locale>
+
+        <locale name="remove.coupons.button">
+            <de><![CDATA[Alle Coupons entfernen]]></de>
+            <en><![CDATA[Remove all coupons]]></en>
+        </locale>
     </groups>
 </locales>
-- 
GitLab