From 746c8ac1408dee78384d1616cc8796c503e42cf9 Mon Sep 17 00:00:00 2001
From: Henning Leutz <leutz@pcsg.de>
Date: Thu, 20 Feb 2020 10:11:12 +0100
Subject: [PATCH] feat: candyman-gmbh/projektplanung#84 - shipping rule via
 percent order

---
 .../controls/shippingRules/CreateRule.html    |  5 +-
 .../controls/shippingRules/CreateRule.js      | 49 ++++++++++---------
 bin/backend/controls/shippingRules/Rule.html  |  3 ++
 bin/backend/controls/shippingRules/Rule.js    | 49 ++++++++++---------
 bin/backend/utils/ShippingUtils.js            |  7 ++-
 locale.xml                                    |  8 +++
 .../Methods/Standard/ShippingType.php         |  2 +
 src/QUI/ERP/Shipping/Rules/Factory.php        | 10 ++++
 src/QUI/ERP/Shipping/Rules/ShippingRule.php   |  3 ++
 src/QUI/ERP/Shipping/Types/ShippingEntry.php  | 22 +++++++++
 10 files changed, 108 insertions(+), 50 deletions(-)

diff --git a/bin/backend/controls/shippingRules/CreateRule.html b/bin/backend/controls/shippingRules/CreateRule.html
index bb9051f..7760325 100644
--- a/bin/backend/controls/shippingRules/CreateRule.html
+++ b/bin/backend/controls/shippingRules/CreateRule.html
@@ -54,6 +54,9 @@
                             <option value="PERCENTAGE">
                                 {{discountPercentage}}
                             </option>
+                            <option value="PERCENTAGE_ORDER">
+                                {{discountPercentageOrder}}
+                            </option>
                         </select>
                         <input type="number"
                                name="discount"
@@ -231,4 +234,4 @@
         </tbody>
     </table>
 
-</form>
\ No newline at end of file
+</form>
diff --git a/bin/backend/controls/shippingRules/CreateRule.js b/bin/backend/controls/shippingRules/CreateRule.js
index 124c2f9..d7510fd 100644
--- a/bin/backend/controls/shippingRules/CreateRule.js
+++ b/bin/backend/controls/shippingRules/CreateRule.js
@@ -55,30 +55,31 @@ define('package/quiqqer/shipping/bin/backend/controls/shippingRules/CreateRule',
             this.$Elm.addClass('quiqqer-shipping-rule-create');
 
             this.$Elm.set('html', Mustache.render(template, {
-                generalHeader         : QUILocale.get(lg, 'shipping.edit.template.general'),
-                title                 : QUILocale.get(lg, 'shipping.edit.template.title'),
-                workingTitle          : QUILocale.get('quiqqer/system', 'workingtitle'),
-                calculationPriority   : QUILocale.get(lg, 'shipping.edit.template.calculationPriority'),
-                discountTitle         : QUILocale.get(lg, 'shipping.edit.template.discount'),
-                discountDescription   : QUILocale.get(lg, 'shipping.edit.template.discount.description'),
-                discountAbsolute      : QUILocale.get(lg, 'shipping.edit.template.discount.absolute'),
-                discountPercentage    : QUILocale.get(lg, 'shipping.edit.template.discount.percentage'),
-                statusTitle           : QUILocale.get(lg, 'shipping.edit.template.status'),
-                statusDescription     : QUILocale.get(lg, 'shipping.edit.template.status.description'),
-                noRulesTitle          : QUILocale.get(lg, 'shipping.edit.template.noRules'),
-                noRulesText           : QUILocale.get(lg, 'shipping.edit.template.noRules.text'),
-                unitTitle             : QUILocale.get(lg, 'shipping.edit.template.unit'),
-                unitHeader            : QUILocale.get(lg, 'shipping.edit.template.unitTitle'),
-                usageHeader           : QUILocale.get(lg, 'shipping.edit.template.usage'),
-                usageFrom             : QUILocale.get(lg, 'shipping.edit.template.usage.from'),
-                usageTo               : QUILocale.get(lg, 'shipping.edit.template.usage.to'),
-                usageAmountOf         : QUILocale.get(lg, 'shipping.edit.template.shopping.amount.of'),
-                usageAmountTo         : QUILocale.get(lg, 'shipping.edit.template.shopping.amount.to'),
-                usageValueOf          : QUILocale.get(lg, 'shipping.edit.template.purchase.value.of'),
-                usageValueTo          : QUILocale.get(lg, 'shipping.edit.template.purchase.value.to'),
-                usageAssignmentUser   : QUILocale.get(lg, 'shipping.edit.template.assignment.user'),
-                usageAssignmentProduct: QUILocale.get(lg, 'shipping.edit.template.assignment.product'),
-                usageAssignmentArea   : QUILocale.get(lg, 'shipping.edit.template.assignment.areas'),
+                generalHeader          : QUILocale.get(lg, 'shipping.edit.template.general'),
+                title                  : QUILocale.get(lg, 'shipping.edit.template.title'),
+                workingTitle           : QUILocale.get('quiqqer/system', 'workingtitle'),
+                calculationPriority    : QUILocale.get(lg, 'shipping.edit.template.calculationPriority'),
+                discountTitle          : QUILocale.get(lg, 'shipping.edit.template.discount'),
+                discountDescription    : QUILocale.get(lg, 'shipping.edit.template.discount.description'),
+                discountAbsolute       : QUILocale.get(lg, 'shipping.edit.template.discount.absolute'),
+                discountPercentage     : QUILocale.get(lg, 'shipping.edit.template.discount.percentage'),
+                discountPercentageOrder: QUILocale.get(lg, 'shipping.edit.template.discount.percentageOrder'),
+                statusTitle            : QUILocale.get(lg, 'shipping.edit.template.status'),
+                statusDescription      : QUILocale.get(lg, 'shipping.edit.template.status.description'),
+                noRulesTitle           : QUILocale.get(lg, 'shipping.edit.template.noRules'),
+                noRulesText            : QUILocale.get(lg, 'shipping.edit.template.noRules.text'),
+                unitTitle              : QUILocale.get(lg, 'shipping.edit.template.unit'),
+                unitHeader             : QUILocale.get(lg, 'shipping.edit.template.unitTitle'),
+                usageHeader            : QUILocale.get(lg, 'shipping.edit.template.usage'),
+                usageFrom              : QUILocale.get(lg, 'shipping.edit.template.usage.from'),
+                usageTo                : QUILocale.get(lg, 'shipping.edit.template.usage.to'),
+                usageAmountOf          : QUILocale.get(lg, 'shipping.edit.template.shopping.amount.of'),
+                usageAmountTo          : QUILocale.get(lg, 'shipping.edit.template.shopping.amount.to'),
+                usageValueOf           : QUILocale.get(lg, 'shipping.edit.template.purchase.value.of'),
+                usageValueTo           : QUILocale.get(lg, 'shipping.edit.template.purchase.value.to'),
+                usageAssignmentUser    : QUILocale.get(lg, 'shipping.edit.template.assignment.user'),
+                usageAssignmentProduct : QUILocale.get(lg, 'shipping.edit.template.assignment.product'),
+                usageAssignmentArea    : QUILocale.get(lg, 'shipping.edit.template.assignment.areas'),
 
                 productHeader                 : QUILocale.get(lg, 'shipping.edit.template.assignment.product.header'),
                 usageAssignmentProductOnly    : QUILocale.get(lg, 'shipping.edit.template.assignment.product.only'),
diff --git a/bin/backend/controls/shippingRules/Rule.html b/bin/backend/controls/shippingRules/Rule.html
index f8e1c28..9e45a8b 100644
--- a/bin/backend/controls/shippingRules/Rule.html
+++ b/bin/backend/controls/shippingRules/Rule.html
@@ -54,6 +54,9 @@
                             <option value="1">
                                 {{discountPercentage}}
                             </option>
+                            <option value="PERCENTAGE_ORDER">
+                                {{discountPercentageOrder}}
+                            </option>
                         </select>
                         <input type="number"
                                step="0.01"
diff --git a/bin/backend/controls/shippingRules/Rule.js b/bin/backend/controls/shippingRules/Rule.js
index 80cd2a7..0e8acd7 100644
--- a/bin/backend/controls/shippingRules/Rule.js
+++ b/bin/backend/controls/shippingRules/Rule.js
@@ -73,30 +73,31 @@ define('package/quiqqer/shipping/bin/backend/controls/shippingRules/Rule', [
             });
 
             this.$Elm.set('html', Mustache.render(template, {
-                generalHeader         : QUILocale.get(lg, 'shipping.edit.template.general'),
-                title                 : QUILocale.get(lg, 'shipping.edit.template.title'),
-                workingTitle          : QUILocale.get('quiqqer/system', 'workingtitle'),
-                calculationPriority   : QUILocale.get(lg, 'shipping.edit.template.calculationPriority'),
-                discountTitle         : QUILocale.get(lg, 'shipping.edit.template.discount'),
-                discountDescription   : QUILocale.get(lg, 'shipping.edit.template.discount.description'),
-                discountAbsolute      : QUILocale.get(lg, 'shipping.edit.template.discount.absolute'),
-                discountPercentage    : QUILocale.get(lg, 'shipping.edit.template.discount.percentage'),
-                statusTitle           : QUILocale.get(lg, 'shipping.edit.template.status'),
-                statusDescription     : QUILocale.get(lg, 'shipping.edit.template.status.description'),
-                noRulesTitle          : QUILocale.get(lg, 'shipping.edit.template.noRules'),
-                noRulesText           : QUILocale.get(lg, 'shipping.edit.template.noRules.text'),
-                unitTitle             : QUILocale.get(lg, 'shipping.edit.template.unit'),
-                unitHeader            : QUILocale.get(lg, 'shipping.edit.template.unitTitle'),
-                usageHeader           : QUILocale.get(lg, 'shipping.edit.template.usage'),
-                usageFrom             : QUILocale.get(lg, 'shipping.edit.template.usage.from'),
-                usageTo               : QUILocale.get(lg, 'shipping.edit.template.usage.to'),
-                usageAmountOf         : QUILocale.get(lg, 'shipping.edit.template.shopping.amount.of'),
-                usageAmountTo         : QUILocale.get(lg, 'shipping.edit.template.shopping.amount.to'),
-                usageValueOf          : QUILocale.get(lg, 'shipping.edit.template.purchase.value.of'),
-                usageValueTo          : QUILocale.get(lg, 'shipping.edit.template.purchase.value.to'),
-                usageAssignmentUser   : QUILocale.get(lg, 'shipping.edit.template.assignment.user'),
-                usageAssignmentProduct: QUILocale.get(lg, 'shipping.edit.template.assignment.product'),
-                usageAssignmentArea   : QUILocale.get(lg, 'shipping.edit.template.assignment.areas'),
+                generalHeader          : QUILocale.get(lg, 'shipping.edit.template.general'),
+                title                  : QUILocale.get(lg, 'shipping.edit.template.title'),
+                workingTitle           : QUILocale.get('quiqqer/system', 'workingtitle'),
+                calculationPriority    : QUILocale.get(lg, 'shipping.edit.template.calculationPriority'),
+                discountTitle          : QUILocale.get(lg, 'shipping.edit.template.discount'),
+                discountDescription    : QUILocale.get(lg, 'shipping.edit.template.discount.description'),
+                discountAbsolute       : QUILocale.get(lg, 'shipping.edit.template.discount.absolute'),
+                discountPercentage     : QUILocale.get(lg, 'shipping.edit.template.discount.percentage'),
+                discountPercentageOrder: QUILocale.get(lg, 'shipping.edit.template.discount.percentageOrder'),
+                statusTitle            : QUILocale.get(lg, 'shipping.edit.template.status'),
+                statusDescription      : QUILocale.get(lg, 'shipping.edit.template.status.description'),
+                noRulesTitle           : QUILocale.get(lg, 'shipping.edit.template.noRules'),
+                noRulesText            : QUILocale.get(lg, 'shipping.edit.template.noRules.text'),
+                unitTitle              : QUILocale.get(lg, 'shipping.edit.template.unit'),
+                unitHeader             : QUILocale.get(lg, 'shipping.edit.template.unitTitle'),
+                usageHeader            : QUILocale.get(lg, 'shipping.edit.template.usage'),
+                usageFrom              : QUILocale.get(lg, 'shipping.edit.template.usage.from'),
+                usageTo                : QUILocale.get(lg, 'shipping.edit.template.usage.to'),
+                usageAmountOf          : QUILocale.get(lg, 'shipping.edit.template.shopping.amount.of'),
+                usageAmountTo          : QUILocale.get(lg, 'shipping.edit.template.shopping.amount.to'),
+                usageValueOf           : QUILocale.get(lg, 'shipping.edit.template.purchase.value.of'),
+                usageValueTo           : QUILocale.get(lg, 'shipping.edit.template.purchase.value.to'),
+                usageAssignmentUser    : QUILocale.get(lg, 'shipping.edit.template.assignment.user'),
+                usageAssignmentProduct : QUILocale.get(lg, 'shipping.edit.template.assignment.product'),
+                usageAssignmentArea    : QUILocale.get(lg, 'shipping.edit.template.assignment.areas'),
 
                 productHeader                 : QUILocale.get(lg, 'shipping.edit.template.assignment.product.header'),
                 usageAssignmentProductOnly    : QUILocale.get(lg, 'shipping.edit.template.assignment.product.only'),
diff --git a/bin/backend/utils/ShippingUtils.js b/bin/backend/utils/ShippingUtils.js
index f115459..17bcd1c 100644
--- a/bin/backend/utils/ShippingUtils.js
+++ b/bin/backend/utils/ShippingUtils.js
@@ -51,6 +51,11 @@ define('package/quiqqer/shipping/bin/backend/utils/ShippingUtils', [
                     'quiqqer/shipping',
                     'discount.type.abs'
                 );
+            } else if (parseInt(ruleData.discount_type) === 2) {
+                ruleData.discount_type_text = QUILocale.get(
+                    'quiqqer/shipping',
+                    'discount.type.percentageOrder'
+                );
             } else {
                 ruleData.discount_type_text = QUILocale.get(
                     'quiqqer/shipping',
@@ -61,4 +66,4 @@ define('package/quiqqer/shipping/bin/backend/utils/ShippingUtils', [
             return ruleData;
         }
     };
-});
\ No newline at end of file
+});
diff --git a/locale.xml b/locale.xml
index 072336e..2acb928 100644
--- a/locale.xml
+++ b/locale.xml
@@ -85,6 +85,10 @@
             <de><![CDATA[Auf / Abschlag % (prozentual)]]></de>
             <en><![CDATA[Discount % (percentage)]]></en>
         </locale>
+        <locale name="discount.type.percentageOrder">
+            <de><![CDATA[Warenwert Auf / Abschlag % (prozentual vom Warenwert)]]></de>
+            <en><![CDATA[Value of goods Discount % (as a percentage of the value of the goods)]]></en>
+        </locale>
 
         <locale name="message.no.rule.found.order.continue">
             <de><![CDATA[
@@ -597,6 +601,10 @@
             <de><![CDATA[Auf / Abschlag % (prozentual)]]></de>
             <en><![CDATA[Discount % (percentage)]]></en>
         </locale>
+        <locale name="shipping.edit.template.discount.percentageOrder">
+            <de><![CDATA[Warenwert Auf / Abschlag % (prozentual vom Warenwert)]]></de>
+            <en><![CDATA[Value of goods Discount % (as a percentage of the value of the goods)]]></en>
+        </locale>
         <locale name="shipping.edit.template.discount.type">
             <de><![CDATA[Typ]]></de>
             <en><![CDATA[Type]]></en>
diff --git a/src/QUI/ERP/Shipping/Methods/Standard/ShippingType.php b/src/QUI/ERP/Shipping/Methods/Standard/ShippingType.php
index 94e871a..0cb78d1 100644
--- a/src/QUI/ERP/Shipping/Methods/Standard/ShippingType.php
+++ b/src/QUI/ERP/Shipping/Methods/Standard/ShippingType.php
@@ -145,6 +145,8 @@ public function canUsedBy(
             return false;
         }
 
+        Debug::addLog("{$this->getTitle()} :: {$ShippingEntry->getTitle()} :: User is in areas");
+
         $userGroups = QUI\Utils\UserGroups::parseUsersGroupsString(
             $ShippingEntry->getAttribute('user_groups')
         );
diff --git a/src/QUI/ERP/Shipping/Rules/Factory.php b/src/QUI/ERP/Shipping/Rules/Factory.php
index e9a36f6..5759303 100644
--- a/src/QUI/ERP/Shipping/Rules/Factory.php
+++ b/src/QUI/ERP/Shipping/Rules/Factory.php
@@ -27,6 +27,11 @@ class Factory extends QUI\CRUD\Factory
      */
     const DISCOUNT_TYPE_PERCENTAGE = 1;
 
+    /**
+     * percentage discount from order sum (total) // PERCENTAGE_ORDER
+     */
+    const DISCOUNT_TYPE_PC_ORDER = 2;
+
     /**
      * Handler constructor.
      */
@@ -135,8 +140,13 @@ public function createChild($data = [])
                 $data['discount_type'] = RuleFactory::DISCOUNT_TYPE_PERCENTAGE;
                 break;
 
+            case 'PERCENTAGE_ORDER':
+                $data['discount_type'] = RuleFactory::DISCOUNT_TYPE_PC_ORDER;
+                break;
+
             case RuleFactory::DISCOUNT_TYPE_ABS:
             case RuleFactory::DISCOUNT_TYPE_PERCENTAGE:
+            case RuleFactory::DISCOUNT_TYPE_PC_ORDER:
                 break;
 
             default:
diff --git a/src/QUI/ERP/Shipping/Rules/ShippingRule.php b/src/QUI/ERP/Shipping/Rules/ShippingRule.php
index 806c554..34f5761 100644
--- a/src/QUI/ERP/Shipping/Rules/ShippingRule.php
+++ b/src/QUI/ERP/Shipping/Rules/ShippingRule.php
@@ -86,6 +86,9 @@ public function __construct($id, Factory $Factory)
                 $attributes['discount_type'] === 'PERCENTAGE'
             ) {
                 $attributes['discount_type'] = RuleFactory::DISCOUNT_TYPE_PERCENTAGE;
+            } elseif ($attributes['discount_type'] === RuleFactory::DISCOUNT_TYPE_PC_ORDER ||
+                      $attributes['discount_type'] === 'PERCENTAGE_ORDER') {
+                $attributes['discount_type'] = RuleFactory::DISCOUNT_TYPE_PC_ORDER;
             } else {
                 $attributes['discount_type'] = RuleFactory::DISCOUNT_TYPE_ABS;
             }
diff --git a/src/QUI/ERP/Shipping/Types/ShippingEntry.php b/src/QUI/ERP/Shipping/Types/ShippingEntry.php
index f0c26fc..ce1f6c5 100644
--- a/src/QUI/ERP/Shipping/Types/ShippingEntry.php
+++ b/src/QUI/ERP/Shipping/Types/ShippingEntry.php
@@ -205,6 +205,8 @@ public function getPrice()
         $rules = $this->getShippingRules();
         $price = 0;
 
+        $Order = $this->Order;
+
         foreach ($rules as $Rule) {
             $discount = $Rule->getAttribute('discount');
             $type     = $Rule->getDiscountType();
@@ -214,6 +216,26 @@ public function getPrice()
                 continue;
             }
 
+            if ($type === QUI\ERP\Shipping\Rules\Factory::DISCOUNT_TYPE_PC_ORDER && $Order) {
+                try {
+                    /* @var $Order QUI\ERP\Order\Order */
+                    $Order       = $this->Order;
+                    $Calculation = $Order->getPriceCalculation();
+                    $nettoSum    = $Calculation->getNettoSum()->get();
+
+                    if (!$nettoSum) {
+                        continue;
+                    }
+
+                    $pc    = \round($nettoSum * ($discount / 100));
+                    $price = $price + $pc;
+
+                    continue;
+                } catch (QUI\Exception $Exception) {
+                    QUI\System\Log::addDebug($Exception->getMessage());
+                }
+            }
+
             $pc    = \round($price * ($discount / 100));
             $price = $price + $pc;
         }
-- 
GitLab