From ff25e3dae20bd00fbbb33990fff7ce7d19daf5d3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Patrick=20M=C3=BCller?= <p.mueller@pcsg.de>
Date: Fri, 23 Aug 2019 11:48:16 +0200
Subject: [PATCH] feat: ShippingTime field and frontend view

---
 locale.xml                                    | 13 ++++
 settings.xml                                  | 17 ++++++
 src/QUI/ERP/Shipping/EventHandler.php         | 60 +++++++++++++++++++
 .../Fields/ShippingTimeFrontendView.php       | 14 +++--
 .../Products/Fields/ShippingTimePeriod.php    | 27 ++++++++-
 src/QUI/ERP/Shipping/Shipping.php             | 10 ++++
 6 files changed, 135 insertions(+), 6 deletions(-)

diff --git a/locale.xml b/locale.xml
index dec71cd..4b907e7 100644
--- a/locale.xml
+++ b/locale.xml
@@ -175,6 +175,10 @@
             <de><![CDATA[ca. [from] bis [to]]]></de>
             <en><![CDATA[approx. [from] to [to]]]></en>
         </locale>
+        <locale name="fields.ShippingTimeFrontendView.timeperiod.period">
+            <de><![CDATA[ca. [period]]]></de>
+            <en><![CDATA[approx. [period]]]></en>
+        </locale>
         <locale name="fields.ShippingTimeFrontendView.timeperiod.unit_single.second">
             <de><![CDATA[Sekunde]]></de>
             <en><![CDATA[second]]></en>
@@ -345,6 +349,15 @@
             ]]></en>
         </locale>
 
+        <locale name="shipping.settings.deliveryTimeDefault">
+            <de><![CDATA[Standard-Lieferzeit]]></de>
+            <en><![CDATA[Default delivery time]]></en>
+        </locale>
+        <locale name="shipping.settings.deliveryTimeDefault.description">
+            <de><![CDATA[Legt den Standardwert für das "Lieferzeit"-Feld fest, wenn ein Produkt neu angelegt wird bzw. kein Feldwert gesetzt ist.]]></de>
+            <en><![CDATA[Determines the default value for the "Delivery time" field for newly created products or if no field value is set.]]></en>
+        </locale>
+
         <locale name="shipping.order.title">
             <de><![CDATA[Versand: [shipping]]]></de>
             <en><![CDATA[Shipping: [shipping]]]></en>
diff --git a/settings.xml b/settings.xml
index c2fab44..21e2e1c 100644
--- a/settings.xml
+++ b/settings.xml
@@ -15,6 +15,10 @@
                 <conf name="ruleFields">
                     <type><![CDATA[string]]></type>
                 </conf>
+                <conf name="deliveryTimeDefault">
+                    <type><![CDATA[string]]></type>
+                    <defaultvalue><![CDATA[{"from":"","to":"","unit":"second","option":"immediately_available"}]]></defaultvalue>
+                </conf>
             </section>
 
             <section name="no_rules">
@@ -76,6 +80,19 @@
                                 <locale group="quiqqer/shipping" var="shipping.settings.ruleFields.description"/>
                             </description>
                         </input>
+
+                        <input type="hidden"
+                               conf="shipping.deliveryTimeDefault"
+                               data-qui="package/quiqqer/shipping/bin/backend/controls/products/fields/ShippingTimePeriod"
+                        >
+                            <text>
+                                <locale group="quiqqer/shipping" var="shipping.settings.deliveryTimeDefault"/>
+                            </text>
+
+                            <description>
+                                <locale group="quiqqer/shipping" var="shipping.settings.deliveryTimeDefault.description"/>
+                            </description>
+                        </input>
                     </settings>
 
                     <settings title="mail" name="mail">
diff --git a/src/QUI/ERP/Shipping/EventHandler.php b/src/QUI/ERP/Shipping/EventHandler.php
index 4735590..4d5bd18 100644
--- a/src/QUI/ERP/Shipping/EventHandler.php
+++ b/src/QUI/ERP/Shipping/EventHandler.php
@@ -7,6 +7,7 @@
 namespace QUI\ERP\Shipping;
 
 use QUI;
+use QUI\ERP\Products\Handler\Fields as ProductFields;
 use \Quiqqer\Engine\Collector;
 
 /**
@@ -23,6 +24,8 @@ class EventHandler
      */
     public static function onPackageSetup()
     {
+        // Translations
+
         $languages = QUI\Translator::getAvailableLanguages();
 
         // create locale
@@ -66,6 +69,9 @@ public static function onPackageSetup()
         } catch (QUI\Exception $Exception) {
             QUI\System\Log::addNotice($Exception->getMessage());
         }
+
+        // Product fields
+        self::createProductFields();
     }
 
     /**
@@ -264,4 +270,58 @@ public static function onUserSaveBegin(QUI\Users\User $User)
             QUI\System\Log::writeDebugException($Exception);
         }
     }
+
+    /**
+     * Create all fixed product fields that quiqqer/shipping provides
+     *
+     * @return void
+     * @throws QUI\Exception
+     */
+    protected static function createProductFields()
+    {
+        $fields = [
+            Shipping::PRODUCT_FIELD_SHIPPING_TIME => [
+                'title'    => [
+                    'de' => 'Lieferzeit',
+                    'en' => 'Delivery time'
+                ],
+                'type'     => Shipping::PRODUCT_FIELD_TYPE_SHIPPING_TIME,
+                'public'   => true,
+                'standard' => true
+            ]
+        ];
+
+        $fieldsCreated = false;
+
+        foreach ($fields as $fieldId => $field) {
+            try {
+                ProductFields::getField($fieldId);
+                continue;
+            } catch (\Exception $Exception) {
+                // Field does not exist -> create it
+            }
+
+            try {
+                ProductFields::createField([
+                    'id'            => $fieldId,
+                    'type'          => $field['type'],
+                    'titles'        => $field['title'],
+                    'workingtitles' => $field['title'],
+                    'systemField'   => 0,
+                    'standardField' => !empty($field['standard']) ? 1 : 0,
+                    'publicField'   => !empty($field['public']) ? 1 : 0,
+                    'options'       => !empty($field['options']) ? $field['options'] : null
+                ]);
+            } catch (\Exception $Exception) {
+                QUI\System\Log::writeException($Exception);
+                continue;
+            }
+
+            $fieldsCreated = true;
+        }
+
+        if ($fieldsCreated) {
+            QUI\Translator::publish('quiqqer/products');
+        }
+    }
 }
diff --git a/src/QUI/ERP/Shipping/Products/Fields/ShippingTimeFrontendView.php b/src/QUI/ERP/Shipping/Products/Fields/ShippingTimeFrontendView.php
index bbab737..93fab77 100644
--- a/src/QUI/ERP/Shipping/Products/Fields/ShippingTimeFrontendView.php
+++ b/src/QUI/ERP/Shipping/Products/Fields/ShippingTimeFrontendView.php
@@ -54,11 +54,17 @@ public function create()
                 $singleTime = true;
 
                 if ($from === $to) {
-                    $valueText = $from;
+                    $valueText = $L->get($lg, 'fields.ShippingTimeFrontendView.timeperiod.period', [
+                        'period' => $from
+                    ]);
                 } elseif (empty($from) && !empty($to)) {
-                    $valueText = $to;
+                    $valueText = $L->get($lg, 'fields.ShippingTimeFrontendView.timeperiod.period', [
+                        'period' => $to
+                    ]);
                 } elseif (!empty($from) && empty($to)) {
-                    $valueText = $from;
+                    $valueText = $L->get($lg, 'fields.ShippingTimeFrontendView.timeperiod.period', [
+                        'period' => $from
+                    ]);
                 } else {
                     $valueText = $L->get($lg, 'fields.ShippingTimeFrontendView.timeperiod.from_to', [
                         'from' => $from,
@@ -80,6 +86,6 @@ public function create()
             'valueText' => $valueText
         ]);
 
-        return $Engine->fetch(\dirname(__FILE__).'/UnitSelectFrontendView.html');
+        return $Engine->fetch(\dirname(__FILE__).'/ShippingTimePeriodFrontendView.html');
     }
 }
diff --git a/src/QUI/ERP/Shipping/Products/Fields/ShippingTimePeriod.php b/src/QUI/ERP/Shipping/Products/Fields/ShippingTimePeriod.php
index 28d201c..7c432a3 100644
--- a/src/QUI/ERP/Shipping/Products/Fields/ShippingTimePeriod.php
+++ b/src/QUI/ERP/Shipping/Products/Fields/ShippingTimePeriod.php
@@ -59,7 +59,7 @@ public function cleanup($value)
         $value = parent::cleanup($value);
 
         if (empty($value)) {
-            return $value;
+            return $this->getDefaultValueFromConfig();
         }
 
         switch ($value['option']) {
@@ -69,7 +69,7 @@ public function cleanup($value)
                 break;
 
             default:
-                return $this->defaultValue;
+                return $this->getDefaultValueFromConfig();
         }
 
         return $value;
@@ -92,4 +92,27 @@ public function getFrontendView()
     {
         return new ShippingTimeFrontendView($this->getFieldDataForView());
     }
+
+    /**
+     * Get default value for the ShippingTimePeriod field
+     *
+     * @return array|null
+     */
+    protected function getDefaultValueFromConfig()
+    {
+        try {
+            $Conf = QUI::getPackage('quiqqer/shipping')->getConfig();
+        } catch (\Exception $Exception) {
+            QUI\System\Log::writeException($Exception);
+            return null;
+        }
+
+        $defaultValue = $Conf->get('shipping', 'deliveryTimeDefault');
+
+        if (empty($defaultValue)) {
+            return null;
+        }
+
+        return \json_decode($defaultValue, true);
+    }
 }
diff --git a/src/QUI/ERP/Shipping/Shipping.php b/src/QUI/ERP/Shipping/Shipping.php
index 2149402..449f7f3 100644
--- a/src/QUI/ERP/Shipping/Shipping.php
+++ b/src/QUI/ERP/Shipping/Shipping.php
@@ -17,6 +17,16 @@
  */
 class Shipping extends QUI\Utils\Singleton
 {
+    /**
+     * Product fields provided by quiqqer/shipping
+     */
+    const PRODUCT_FIELD_SHIPPING_TIME = 300;
+
+    /**
+     * Product field types provided by quiqqer/shipping
+     */
+    const PRODUCT_FIELD_TYPE_SHIPPING_TIME = 'shipping.ShippingTimePeriod';
+
     /**
      * Continue order if no rule was found
      */
-- 
GitLab