From 3cd938e99c8704ddc62a52d12af3e4158b2d7e40 Mon Sep 17 00:00:00 2001
From: Johannes Hoermann <j.hoermann@adito.de>
Date: Thu, 28 Mar 2019 15:18:51 +0100
Subject: [PATCH] Offer fixes

---
 entity/Offeritem_entity/Offeritem_entity.aod  |  3 +
 .../entityfields/discount/onValidation.js     | 13 ++++
 .../entityfields/quantity/onValidation.js     | 28 ++++----
 .../entityfields/quantity/onValueChange.js    | 71 +++++++++----------
 .../entityfields/vat/onValidation.js          | 13 ++++
 .../_____LANGUAGE_EXTRA.aod                   |  6 ++
 .../_____LANGUAGE_de/_____LANGUAGE_de.aod     |  7 ++
 .../_____LANGUAGE_en/_____LANGUAGE_en.aod     |  9 +++
 .../OfferitemEdit_view/OfferitemEdit_view.aod | 15 ++++
 .../OfferitemFilter_view.aod                  |  5 ++
 process/Util_lib/process.js                   | 59 +++++++++++++++
 11 files changed, 177 insertions(+), 52 deletions(-)
 create mode 100644 entity/Offeritem_entity/entityfields/discount/onValidation.js
 create mode 100644 entity/Productprice_entity/entityfields/vat/onValidation.js

diff --git a/entity/Offeritem_entity/Offeritem_entity.aod b/entity/Offeritem_entity/Offeritem_entity.aod
index 236c205986..ec65a90423 100644
--- a/entity/Offeritem_entity/Offeritem_entity.aod
+++ b/entity/Offeritem_entity/Offeritem_entity.aod
@@ -18,6 +18,7 @@
       <title>Discount %</title>
       <contentType>NUMBER</contentType>
       <outputFormat>#,##0</outputFormat>
+      <onValidation>%aditoprj%/entity/Offeritem_entity/entityfields/discount/onValidation.js</onValidation>
     </entityField>
     <entityField>
       <name>GROUPCODEID</name>
@@ -68,6 +69,7 @@
       <title>Article</title>
       <consumer>Products</consumer>
       <linkedContext>Product</linkedContext>
+      <mandatory v="true" />
       <displayValueProcess>%aditoprj%/entity/Offeritem_entity/entityfields/product_id/displayValueProcess.js</displayValueProcess>
       <onValueChange>%aditoprj%/entity/Offeritem_entity/entityfields/product_id/onValueChange.js</onValueChange>
       <onValueChangeTypes>
@@ -80,6 +82,7 @@
       <title>Quantity</title>
       <contentType>NUMBER</contentType>
       <outputFormat>#</outputFormat>
+      <mandatory v="true" />
       <valueProcess>%aditoprj%/entity/Offeritem_entity/entityfields/quantity/valueProcess.js</valueProcess>
       <onValidation>%aditoprj%/entity/Offeritem_entity/entityfields/quantity/onValidation.js</onValidation>
       <onValueChange>%aditoprj%/entity/Offeritem_entity/entityfields/quantity/onValueChange.js</onValueChange>
diff --git a/entity/Offeritem_entity/entityfields/discount/onValidation.js b/entity/Offeritem_entity/entityfields/discount/onValidation.js
new file mode 100644
index 0000000000..18bd49fe0d
--- /dev/null
+++ b/entity/Offeritem_entity/entityfields/discount/onValidation.js
@@ -0,0 +1,13 @@
+import("system.result");
+import("system.vars");
+import("Util_lib");
+import("Entity_lib");
+
+var value = ProcessHandlingUtils.getOnValidationValue(vars.get("$field.DISCOUNT"));
+
+var validationResult = NumberUtils.validateIsInside("Discount", value, 0, 100);
+
+if (validationResult)
+{
+    result.string(validationResult);
+}
\ No newline at end of file
diff --git a/entity/Offeritem_entity/entityfields/quantity/onValidation.js b/entity/Offeritem_entity/entityfields/quantity/onValidation.js
index aa24a80d84..a7fe04aaf5 100644
--- a/entity/Offeritem_entity/entityfields/quantity/onValidation.js
+++ b/entity/Offeritem_entity/entityfields/quantity/onValidation.js
@@ -1,16 +1,12 @@
-import("system.logging");
-import("system.translate");
-import("system.result");
-import("system.vars");
-import("Entity_lib");
-
-logging.log("valid test 1: " + vars.get("$field.QUANTITY"));
-
-var quatity = vars.exists("$field.QUANTITY") ? vars.get("$field.QUANTITY") : "";
-quatity = ProcessHandlingUtils.getOnValidationValue(quatity);
-
-if (parseInt(quatity) <= 0)
-{
-    logging("valid test 2: " +  vars.get("$field.QUANTITY"));
-    result.string(translate.text("${QUANTITY_LOWER_THAN_1}"));
-}
+import("system.translate");
+import("system.result");
+import("system.vars");
+import("Entity_lib");
+
+var quatity = vars.exists("$field.QUANTITY") ? vars.get("$field.QUANTITY") : "";
+quatity = ProcessHandlingUtils.getOnValidationValue(quatity);
+
+if (parseInt(quatity) <= 0)
+{
+    result.string(translate.text("${QUANTITY_LOWER_THAN_1}"));
+}
diff --git a/entity/Offeritem_entity/entityfields/quantity/onValueChange.js b/entity/Offeritem_entity/entityfields/quantity/onValueChange.js
index dea8c2c669..5076a25842 100644
--- a/entity/Offeritem_entity/entityfields/quantity/onValueChange.js
+++ b/entity/Offeritem_entity/entityfields/quantity/onValueChange.js
@@ -1,37 +1,36 @@
-import("system.logging");
-import("system.vars");
-import("system.neon");
-import("Product_lib");
-import("Util_lib");
-import("Entity_lib");
-import("Attribute_lib");
-
-var pid = vars.get("$field.PRODUCT_ID");
-var newQuantity = ProcessHandlingUtils.getOnValidationValue(vars.get("$field.QUANTITY"));
-if(pid != "" && newQuantity != "")
-{
-    var curr = vars.exists("$param.Currency_param") ? vars.get("$param.Currency_param") : "";
-    var contactid = vars.exists("$param.ContactId_param") ? vars.get("$param.ContactId_param") : "";
-    var pricelist = AttributeRelationUtils.getAttribute("97b449a5-d9b4-42ff-b9b0-4f8b27b8a9ec", contactid) || "";
-    
-    var PriceListFilter = { currency: curr, quantity: newQuantity, relationId: contactid, priceList: pricelist };
-    
-    var ProductDetails = ProductUtils.getProductDetails(pid, PriceListFilter);
-    
-    if(ProductDetails.productId != undefined && ProductDetails.PriceListToUse != null)
-    {
-        vars.set("$field.PRICE", ProductDetails.PriceListToUse.price);
-        vars.set("$field.VAT", ProductDetails.PriceListToUse.vat);
-    }
-}
-
-
-//checks if the value is <= 0, if so fallback to 1
-var quatity = vars.exists("$field.QUANTITY") ? vars.get("$field.QUANTITY") : "";
-quatity = ProcessHandlingUtils.getOnValidationValue(quatity);
-
-if (parseInt(quatity) <= 0)
-{
-    neon.setFieldValue("$field.QUANTITY", "1");
-}
+import("system.vars");
+import("system.neon");
+import("Product_lib");
+import("Util_lib");
+import("Entity_lib");
+import("Attribute_lib");
+
+var pid = vars.get("$field.PRODUCT_ID");
+var newQuantity = ProcessHandlingUtils.getOnValidationValue(vars.get("$field.QUANTITY"));
+if(pid != "" && newQuantity != "")
+{
+    var curr = vars.exists("$param.Currency_param") ? vars.get("$param.Currency_param") : "";
+    var contactid = vars.exists("$param.ContactId_param") ? vars.get("$param.ContactId_param") : "";
+    var pricelist = AttributeRelationUtils.getAttribute("97b449a5-d9b4-42ff-b9b0-4f8b27b8a9ec", contactid) || "";
+    
+    var PriceListFilter = { currency: curr, quantity: newQuantity, relationId: contactid, priceList: pricelist };
+    
+    var ProductDetails = ProductUtils.getProductDetails(pid, PriceListFilter);
+    
+    if(ProductDetails.productId != undefined && ProductDetails.PriceListToUse != null)
+    {
+        vars.set("$field.PRICE", ProductDetails.PriceListToUse.price);
+        vars.set("$field.VAT", ProductDetails.PriceListToUse.vat);
+    }
+}
+
+
+//checks if the value is <= 0, if so fallback to 1
+var quatity = vars.exists("$field.QUANTITY") ? vars.get("$field.QUANTITY") : "";
+quatity = ProcessHandlingUtils.getOnValidationValue(quatity);
+
+if (parseInt(quatity) <= 0)
+{
+    neon.setFieldValue("$field.QUANTITY", "1");
+}
     
\ No newline at end of file
diff --git a/entity/Productprice_entity/entityfields/vat/onValidation.js b/entity/Productprice_entity/entityfields/vat/onValidation.js
new file mode 100644
index 0000000000..974d4c311f
--- /dev/null
+++ b/entity/Productprice_entity/entityfields/vat/onValidation.js
@@ -0,0 +1,13 @@
+import("system.result");
+import("system.vars");
+import("Util_lib");
+import("Entity_lib");
+
+var value = ProcessHandlingUtils.getOnValidationValue(vars.get("$field.VAT"));
+
+var validationResult = NumberUtils.validateIsInside("VAT", value, 0, 100);
+
+if (validationResult)
+{
+    result.string(validationResult);
+}
\ No newline at end of file
diff --git a/language/_____LANGUAGE_EXTRA/_____LANGUAGE_EXTRA.aod b/language/_____LANGUAGE_EXTRA/_____LANGUAGE_EXTRA.aod
index b9304b3e3d..2e47678c93 100644
--- a/language/_____LANGUAGE_EXTRA/_____LANGUAGE_EXTRA.aod
+++ b/language/_____LANGUAGE_EXTRA/_____LANGUAGE_EXTRA.aod
@@ -2655,6 +2655,12 @@
     <entry>
       <key>true</key>
     </entry>
+    <entry>
+      <key>Protected</key>
+    </entry>
+    <entry>
+      <key>${MIN_MAX_ERROR} field: %0, value: %1, min: %2, max: %3</key>
+    </entry>
   </keyValueMap>
   <font name="Dialog" style="0" size="11" />
   <sqlModels>
diff --git a/language/_____LANGUAGE_de/_____LANGUAGE_de.aod b/language/_____LANGUAGE_de/_____LANGUAGE_de.aod
index ea1f024278..355302ddf9 100644
--- a/language/_____LANGUAGE_de/_____LANGUAGE_de.aod
+++ b/language/_____LANGUAGE_de/_____LANGUAGE_de.aod
@@ -3416,6 +3416,13 @@
     <entry>
       <key>true</key>
     </entry>
+    <entry>
+      <key>${MIN_MAX_ERROR} field: %0, value: %1, min: %2, max: %3</key>
+      <value>%0 muss zwischen %2 und %3 sein.</value>
+    </entry>
+    <entry>
+      <key>protected</key>
+    </entry>
   </keyValueMap>
   <font name="Dialog" style="0" size="11" />
 </language>
diff --git a/language/_____LANGUAGE_en/_____LANGUAGE_en.aod b/language/_____LANGUAGE_en/_____LANGUAGE_en.aod
index eb9613d23d..f9ebdddbda 100644
--- a/language/_____LANGUAGE_en/_____LANGUAGE_en.aod
+++ b/language/_____LANGUAGE_en/_____LANGUAGE_en.aod
@@ -1390,6 +1390,7 @@
     </entry>
     <entry>
       <key>${ORGTYPE_OTHER}</key>
+      <value>Other</value>
     </entry>
     <entry>
       <key>Haiti</key>
@@ -2185,6 +2186,7 @@
     </entry>
     <entry>
       <key>${NUMBER}</key>
+      <value>Number</value>
     </entry>
     <entry>
       <key>Name \"%0\" already used for container \"%1\"</key>
@@ -2680,6 +2682,13 @@
     <entry>
       <key>true</key>
     </entry>
+    <entry>
+      <key>Protected</key>
+    </entry>
+    <entry>
+      <key>${MIN_MAX_ERROR} field: %0, value: %1, min: %2, max: %3</key>
+      <value>%0 has to be between %2 and %3.</value>
+    </entry>
   </keyValueMap>
   <font name="Dialog" style="0" size="11" />
 </language>
diff --git a/neonView/OfferitemEdit_view/OfferitemEdit_view.aod b/neonView/OfferitemEdit_view/OfferitemEdit_view.aod
index 0286bfb270..ff753701e6 100644
--- a/neonView/OfferitemEdit_view/OfferitemEdit_view.aod
+++ b/neonView/OfferitemEdit_view/OfferitemEdit_view.aod
@@ -33,6 +33,10 @@
           <name>2b635ddb-d52c-4063-af11-aea8eeee151b</name>
           <entityField>PRICE</entityField>
         </entityFieldLink>
+        <entityFieldLink>
+          <name>4f339738-6358-463e-b941-3b2693ab115a</name>
+          <entityField>DISCOUNT</entityField>
+        </entityFieldLink>
         <entityFieldLink>
           <name>c0a22aa4-b09d-4d8b-8d24-1750eb7ba5ca</name>
           <entityField>VAT</entityField>
@@ -47,5 +51,16 @@
         </entityFieldLink>
       </fields>
     </genericViewTemplate>
+    <genericViewTemplate>
+      <name>Price</name>
+      <editMode v="true" />
+      <entityField>#ENTITY</entityField>
+      <fields>
+        <entityFieldLink>
+          <name>9200df17-11e2-4a1a-babb-ea48c6f88a93</name>
+          <entityField>TotalPrice</entityField>
+        </entityFieldLink>
+      </fields>
+    </genericViewTemplate>
   </children>
 </neonView>
diff --git a/neonView/OfferitemFilter_view/OfferitemFilter_view.aod b/neonView/OfferitemFilter_view/OfferitemFilter_view.aod
index 9a10ee740c..5ec130f3ba 100644
--- a/neonView/OfferitemFilter_view/OfferitemFilter_view.aod
+++ b/neonView/OfferitemFilter_view/OfferitemFilter_view.aod
@@ -11,6 +11,7 @@
   <children>
     <tableViewTemplate>
       <name>Offeritems</name>
+      <autoNewRow v="true" />
       <entityField>#ENTITY</entityField>
       <columns>
         <neonTableColumn>
@@ -49,6 +50,10 @@
           <name>a31fd16c-4237-4cd9-a9de-2267f186d342</name>
           <entityField>INFO</entityField>
         </neonTableColumn>
+        <neonTableColumn>
+          <name>60a36c38-103f-4fdb-9e8a-b8fd6d441f14</name>
+          <entityField>TotalPrice</entityField>
+        </neonTableColumn>
       </columns>
     </tableViewTemplate>
   </children>
diff --git a/process/Util_lib/process.js b/process/Util_lib/process.js
index bc9a4fe69f..82af36de2b 100644
--- a/process/Util_lib/process.js
+++ b/process/Util_lib/process.js
@@ -38,6 +38,65 @@ StringUtils.concat = function(pSeparator, pElements)
    return res;
 };
 
+/**
+ * Class containing static utility functions for numbers
+ * Do not create an instance of this
+ * 
+ * @class
+ */
+function NumberUtils(){}
+
+/**
+ * Check iv the value is inside of the min / max values. 
+ * INCLUDING min / max
+ *
+ * @param {Number} pValue value to check
+ * @param {Number} pMin min value INCLUSIVE
+ * @param {Number} pMax max value INCLUSIVE 
+ * @param {Boolean} [pIgnoreNull=true] return True if pValue is null
+ * 
+ * @return {Boolean}
+ */
+NumberUtils.isInside = function(pValue, pMin, pMax, pIgnoreNull)
+{
+    if (pIgnoreNull == undefined)
+        pIgnoreNull = true;
+    
+    return pValue >= pMin && pValue <= pMax || pIgnoreNull && (pValue == null || isNaN(pValue));
+};
+
+/**
+ * For use in validationProcess. Calls result.string(...) with error message, if number is not inside of the given values
+ * INCLUDING min / max.
+ * 
+ * @param {Number} pTitle title to display in error message. Should be the name of the field and it will be translated.
+ * @param {Number} pValue value to check
+ * @param {Number} pMin min value INCLUSIVE
+ * @param {Number} pMax max value INCLUSIVE 
+ * @param {Boolean} [pIgnoreNull=true] return True if pValue is null
+ * 
+ * @return {String|False} returns the error message or false 
+ * 
+ * @example
+ *  var value = ProcessHandlingUtils.getOnValidationValue(vars.get("$field.DISCOUNT")); <br>
+ *   <br>
+ *  var validationResult = NumberUtils.validateIsInside("Discount", value, 0, 100); <br>
+ *   <br>
+ *  if (validationResult) <br>
+ *  { <br>
+ *      result.string(validationResult); <br>
+ *  } <br>
+ */
+NumberUtils.validateIsInside = function(pTitle, pValue, pMin, pMax, pIgnoreNull)
+{
+    var discount = parseInt(pValue);
+    if (!NumberUtils.isInside(discount, 0, 100, pIgnoreNull))
+    {
+        return (translate.withArguments("${MIN_MAX_ERROR} field: %0, value: %1, min: %2, max: %3", [translate.text(pTitle), discount, pMin, pMax]));
+    }
+    return false;
+}
+
 /**
  * Class containing static utility functions for use with arrays
  * Do not create an instance of this!
-- 
GitLab