From e7d8d87379f65471ec3276df6cfcd69da7b8ba1c Mon Sep 17 00:00:00 2001
From: "b.ulrich" <b.ulrich@adito.de>
Date: Fri, 16 Apr 2021 17:26:27 +0200
Subject: [PATCH] [Projekt: xRM-Sales][TicketNr.: 1079248][Angebotspreis Bug]

---
 .../recordcontainers/db/onDBInsert.js         | 31 +++++++------------
 process/Offer_lib/process.js                  | 10 ++++--
 2 files changed, 18 insertions(+), 23 deletions(-)

diff --git a/entity/Offeritem_entity/recordcontainers/db/onDBInsert.js b/entity/Offeritem_entity/recordcontainers/db/onDBInsert.js
index 7310f91eb3..e78ed8f9e2 100644
--- a/entity/Offeritem_entity/recordcontainers/db/onDBInsert.js
+++ b/entity/Offeritem_entity/recordcontainers/db/onDBInsert.js
@@ -16,34 +16,25 @@ if(oid != "")
     
     var insertStatements = JSON.parse(vars.get("$field.itemInsertStatements"));//insert statements get set by insertPartsList in the onvalueChanges of product_id and quantity
     var statements = [];
-    if(insertStatements)
-    {
-        statements = statements.concat(insertStatements);
-    }
     
     //update offer price
     var vals = oiUtils.getNetAndVat();
     var discountedVals = OfferItemUtils.getDiscountedNet(null, oid,  discount);
     if(discountedVals)
     {
-    insertStatements.push(
-        newWhere("OFFERITEM.OFFERITEMID", rowdata["OFFERITEM.OFFERITEMID"]).buildUpdateStatement({
-                "NET": vals[0],
-                "VAT": vals[1],
-                "DISCOUNTED_NET": discountedVals[0],
-                "DISCOUNTED_VAT": discountedVals[1]
-            })
-        )
+        statements.push(
+            newWhere("OFFER.OFFERID", rowdata["OFFERITEM.OFFER_ID"]).buildUpdateStatement({
+                    "NET": vals[0],
+                    "VAT": vals[1],
+                    "DISCOUNTED_NET": discountedVals[0],
+                    "DISCOUNTED_VAT": discountedVals[1]
+                })
+            )
     }
-    else
+
+    if(insertStatements)
     {
-        insertStatements.push(
-            newWhere("OFFERITEMID", rowdata["OFFERITEM.OFFERITEMID"])
-                    .buildUpdateStatement({
-                        "NET": vals[0],
-                        "VAT": vals[1]
-                    })
-            )
+        statements = statements.concat(insertStatements);
     }
 
     db.execute(statements, "Data_alias", 10000000);
diff --git a/process/Offer_lib/process.js b/process/Offer_lib/process.js
index 1fb67604ae..a9561a5068 100644
--- a/process/Offer_lib/process.js
+++ b/process/Offer_lib/process.js
@@ -631,7 +631,7 @@ OfferItemUtils.getDiscountedNet = function(pExcludedIs, pOfferId, pDiscount, pEx
     var vat = 0;
     var vatExcluded = 0;
     if(pOfferId){
-        var offeritemData = newSelect("QUANTITY, PRICE, DISCOUNT, VAT, OPTIONAL")
+        var offeritemData = newSelect("QUANTITY, PRICE, DISCOUNT, VAT, OPTIONAL, ITEMPOSITION")
         .from("OFFERITEM")
         .where("OFFERITEM.OFFER_ID", pOfferId)
         .andIfSet("OFFERITEM.GROUPCODEID", pExcludedProductgroups, SqlBuilder.NOT_IN())
@@ -640,8 +640,12 @@ OfferItemUtils.getDiscountedNet = function(pExcludedIs, pOfferId, pDiscount, pEx
         .table();
         for (var i = 0; i < offeritemData.length; i++)
         {
-            sum = eMath.addDec(sum, ItemUtils.prototype.getItemSum(offeritemData[i][0], offeritemData[i][1], offeritemData[i][2], offeritemData[i][4]));
-            vat = eMath.addDec(vat, ItemUtils.prototype.getItemVAT(offeritemData[i][0], offeritemData[i][1], offeritemData[i][2], offeritemData[i][3], offeritemData[i][4]));
+            if(!offeritemData[i][5].toString().includes(".")//only top items
+                && offeritemData[i][4] == "0")//which are not optional
+            {
+                sum = eMath.addDec(sum, eMath.subDec(offeritemData[i][1], offeritemData[i][2])*offeritemData[i][0]);
+                vat = eMath.addDec(vat, offeritemData[i][3]*eMath.subDec(offeritemData[i][1], offeritemData[i][2])*offeritemData[i][0]/100);
+            }
         }
        
         sum = eMath.subDec(sum, eMath.divDec(eMath.mulDec(sum, pDiscount), "100"));
-- 
GitLab