From 4d1c4caffabf565c07d9af8bd890a4340d387843 Mon Sep 17 00:00:00 2001
From: Henning Leutz <leutz@pcsg.de>
Date: Fri, 16 Sep 2022 14:43:05 +0200
Subject: [PATCH] feat: quiqqer/erp#78

---
 bin/backend/load.js                   |  4 +--
 locale.xml                            | 29 +++++++++++++++++
 settings.xml                          | 47 +++++++++++++++++++++++++--
 src/QUI/ERP/Shipping/EventHandler.php | 27 +++++++++++++++
 4 files changed, 102 insertions(+), 5 deletions(-)

diff --git a/bin/backend/load.js b/bin/backend/load.js
index c9bdba6..73e908f 100644
--- a/bin/backend/load.js
+++ b/bin/backend/load.js
@@ -1,5 +1,3 @@
-console.log('load shipping');
-
 require(['qui/QUI'], function (QUI) {
     "use strict";
 
@@ -39,7 +37,7 @@ require(['qui/QUI'], function (QUI) {
                                 events: {
                                     onSubmit: function (Instance, value) {
                                         PriceFactorWindow.Loader.show();
-                                        
+
                                         const currency = ArticleList.getAttribute('currency');
                                         const vat = 19;
                                         let shippingData;
diff --git a/locale.xml b/locale.xml
index 29fcb90..0287a87 100644
--- a/locale.xml
+++ b/locale.xml
@@ -293,6 +293,35 @@
                         <p class="content-message-information">Please note: The delivery times only allow an estimated time of arrival after shipping!</p>]]>
             </en>
         </locale>
+
+        <locale name="shipping.defaultShipping.settings.title">
+            <de><![CDATA[Standard Versand]]></de>
+            <en><![CDATA[Default shipping]]></en>
+        </locale>
+        <locale name="shipping.defaultShipping.settings.defaultShipping">
+            <de><![CDATA[Standard Versand]]></de>
+            <en><![CDATA[Default shipping]]></en>
+        </locale>
+        <locale name="shipping.defaultShipping.settings.defaultShipping.desc">
+            <de><![CDATA[Lege fest welcher Versand als Standard Versand verwendet werden soll.]]></de>
+            <en><![CDATA[Specify which shipping should be used as default shipping.]]></en>
+        </locale>
+        <locale name="shipping.defaultShipping.settings.addDefaultShipping">
+            <de><![CDATA[Standard Versand automatisch hinzufügen]]></de>
+            <en><![CDATA[Add standard shipping automatically]]></en>
+        </locale>
+        <locale name="shipping.defaultShipping.settings.addDefaultShipping.desc">
+            <de><![CDATA[
+            Der Standard Versand wird einer Bestellung hinzugefügt falls diese Bestellung noch keinen Versand hat.
+            Diese Einstellung hat nur Auswirkung im Backend und nicht auf Frontend Aktionen.
+            Wenn ein Nutzer eine Bestellung ausführt, wird dieser Versand nicht beachtet.
+            ]]></de>
+            <en><![CDATA[
+            The default shipping will be added to an order if this order has no shipping yet.
+            This setting has effect only in backend and not on frontend actions.
+            When a user executes an order, this shipping is ignored.
+            ]]></en>
+        </locale>
     </groups>
 
     <groups name="quiqqer/shipping" datatype="php">
diff --git a/settings.xml b/settings.xml
index e52baba..aa87a05 100644
--- a/settings.xml
+++ b/settings.xml
@@ -24,6 +24,14 @@
                     <type><![CDATA[bool]]></type>
                     <defaultvalue>0</defaultvalue>
                 </conf>
+
+                <conf name="defaultShipping">
+                    <type><![CDATA[integer]]></type>
+                </conf>
+                <conf name="addDefaultShipping">
+                    <type><![CDATA[bool]]></type>
+                    <defaultvalue>0</defaultvalue>
+                </conf>
             </section>
 
             <section name="no_rules">
@@ -103,10 +111,12 @@
 
                         <input type="checkbox" conf="shipping.showShippingInfoAfterPrice">
                             <text>
-                                <locale group="quiqqer/shipping" var="shipping.settings.showShippingInfoAfterPrice.enable"/>
+                                <locale group="quiqqer/shipping"
+                                        var="shipping.settings.showShippingInfoAfterPrice.enable"/>
                             </text>
                             <description>
-                                <locale group="quiqqer/shipping" var="shipping.settings.showShippingInfoAfterPrice.enable.description"/>
+                                <locale group="quiqqer/shipping"
+                                        var="shipping.settings.showShippingInfoAfterPrice.enable.description"/>
                             </description>
                         </input>
 
@@ -142,6 +152,39 @@
                         </input>
                     </settings>
 
+                    <settings title="defaultShipping" name="defaultShipping">
+                        <title>
+                            <locale group="quiqqer/shipping" var="shipping.defaultShipping.settings.title"/>
+                        </title>
+
+                        <input conf="shipping.defaultShipping"
+                               data-qui="package/quiqqer/shipping/bin/backend/controls/ShippingSelect"
+                               data-qui-options-multiple="0"
+                               data-qui-options-max="1"
+                        >
+                            <text>
+                                <locale group="quiqqer/shipping"
+                                        var="shipping.defaultShipping.settings.defaultShipping"/>
+                            </text>
+                            <description>
+                                <locale group="quiqqer/shipping"
+                                        var="shipping.defaultShipping.settings.defaultShipping.desc"/>
+                            </description>
+                        </input>
+                        
+                        <input conf="shipping.addDefaultShipping" type="checkbox">
+                            <text>
+                                <locale group="quiqqer/shipping"
+                                        var="shipping.defaultShipping.settings.addDefaultShipping"/>
+                            </text>
+                            <description>
+                                <locale group="quiqqer/shipping"
+                                        var="shipping.defaultShipping.settings.addDefaultShipping.desc"/>
+                            </description>
+                        </input>
+                    </settings>
+
+
                     <settings title="mail" name="mail">
                         <title>
                             <locale group="quiqqer/shipping" var="shipping.mail.settings.title"/>
diff --git a/src/QUI/ERP/Shipping/EventHandler.php b/src/QUI/ERP/Shipping/EventHandler.php
index f97654e..fd0a3d2 100644
--- a/src/QUI/ERP/Shipping/EventHandler.php
+++ b/src/QUI/ERP/Shipping/EventHandler.php
@@ -500,4 +500,31 @@ public static function onQuiqqerProductsPriceEnd(Collector $Collector, QUI\ERP\P
 
         $Collector->append($html);
     }
+
+    /**
+     * @param \QUI\ERP\Order\AbstractOrder $Order
+     * @return void
+     *
+     * @throws \QUI\Exception
+     */
+    public static function onQuiqqerOrderShippingOnEmpty(QUI\ERP\Order\AbstractOrder $Order)
+    {
+        if (!QUI::isBackend()) {
+            return;
+        }
+
+        $Config  = QUI::getPackage('quiqqer/shipping')->getConfig();
+        $default = $Config->getValue('shipping', 'defaultShipping');
+        $add     = $Config->getValue('shipping', 'addDefaultShipping');
+
+        if (empty($add)) {
+            return;
+        }
+
+        try {
+            $Shipping = Shipping::getInstance()->getShippingEntry($default);
+            $Order->setShipping($Shipping);
+        } catch (QUI\Exception $Exception) {
+        }
+    }
 }
-- 
GitLab