From efa57448ca4ff42f3112dd19c2e1b18ac177ca17 Mon Sep 17 00:00:00 2001
From: Johannes Hoermann <j.hoermann@adito.de>
Date: Wed, 20 Mar 2019 09:44:42 +0100
Subject: [PATCH] fix salesproject

---
 .../entityfields/reasons/mandatoryProcess.js  |  5 +-
 .../recordcontainers/db/onDBUpdate.js         | 52 +++++++++++--------
 process/KeywordRegistry_basic/process.js      |  1 +
 3 files changed, 31 insertions(+), 27 deletions(-)

diff --git a/entity/Salesproject_entity/entityfields/reasons/mandatoryProcess.js b/entity/Salesproject_entity/entityfields/reasons/mandatoryProcess.js
index 2d2df52524..086a5d36d4 100644
--- a/entity/Salesproject_entity/entityfields/reasons/mandatoryProcess.js
+++ b/entity/Salesproject_entity/entityfields/reasons/mandatoryProcess.js
@@ -1,9 +1,6 @@
-import("system.logging");
 import("system.result");
 import("system.vars");
 
 // IDs: SalesprojectState Order and Lost
 var res = vars.get("$field.STATE") && (vars.get("$field.STATE") == 'd8a60f60-a4e6-46ee-88ec-bac53e1afedd' || vars.get("$field.STATE") == '130bb53a-a97e-455e-8f34-8d445e985474');
-result.string(res);
-
-logging.log(res)
\ No newline at end of file
+result.string(res);
\ No newline at end of file
diff --git a/entity/Salesproject_entity/recordcontainers/db/onDBUpdate.js b/entity/Salesproject_entity/recordcontainers/db/onDBUpdate.js
index 20ec5095d2..b27fe64038 100644
--- a/entity/Salesproject_entity/recordcontainers/db/onDBUpdate.js
+++ b/entity/Salesproject_entity/recordcontainers/db/onDBUpdate.js
@@ -4,6 +4,7 @@ import("system.vars");
 import("Salesproject_lib");
 import("Sql_lib");
 import("system.db");
+import("KeywordRegistry_basic");
 
 var needToUpdateForecast = false;
 
@@ -11,6 +12,8 @@ var needToUpdateForecast = false;
 vars.get("$local.changed").forEach(function(fieldName) {
     var typeValue;
     var fieldVar;
+    var clearReason;
+    
     
     switch (fieldName) {
         case "SALESPROJECT.PHASE":
@@ -27,19 +30,33 @@ vars.get("$local.changed").forEach(function(fieldName) {
             // Lost
             if ( vars.get("$field.STATE") == 'd8a60f60-a4e6-46ee-88ec-bac53e1afedd')
             {
-                _updateDataToLost("OFFER", $KeywordRegistry.offerStatus());
+                // set all offers of the salesproject to lost
+                db.updateData("OFFER", ["STATUS"], null, ["a5682630-01ae-4769-8f03-47dc1db44da4"], SqlCondition.equals("OFFER.SALESPROJECT_ID", vars.getString("$field.SALESPROJECTID"), "1=2"));
                 _updateReasons();
+                clearReason = false;
             }
             // Order
-            else if(vars.get("$field.STATE") == '130bb53a-a97e-455e-8f34-8d445e985474')
+            else if (vars.get("$field.STATE") == '130bb53a-a97e-455e-8f34-8d445e985474')
             {
-                _updateDataToLost("SALESPROJECT_COMPETITION", $KeywordRegistry.salesprojectCompetitionState()); 
+                // set all competitions of the salesproject to lost
+                db.updateData("SALESPROJECT_COMPETITION", ["STATUS"], null, ["ff73172c-8517-4b6e-98cb-43aebacd9af1"], SqlCondition.equals("SALESPROJECT_COMPETITION.SALESPROJECT_ID", vars.getString("$field.SALESPROJECTID"), "1=2"));
                 _updateReasons();
+                clearReason = false;
+            }
+            // if state changed, but not to one of the above states
+            else if (clearReason == undefined)
+            {
+                clearReason = true;
             }
-            else
-                _updateReasons("del");
             break;
     }
+    
+    // It clearReason is undefined, it the state did not change. So do nothing
+    if (clearReason != undefined)
+    {
+        _clearReasons(clearReason);
+    }
+    
 
     if (typeValue) {
         needToUpdateForecast = true;
@@ -53,26 +70,15 @@ if (needToUpdateForecast)
 
 neon.refresh()
 
-function _updateReasons(pDelete)
+function _updateReasons()
 {
-    var reasons = "";
-    if(!pDelete)
-        vars.getString("field.REASONS");
-    if(reasons != null)
+    var reasons = vars.getString("field.REASONS");
+    if(reasons)
         db.updateData("SALESPROJECT", ["REASONS"], null, [reasons], SqlCondition.equals("SALESPROJECT.SALESPROJECTID", vars.getString("$field.SALESPROJECTID"), "1=2"));
 }
 
-/**
- * when project is state offer sets competitors to lost, when project is state lost sets offer to lost
- * @param pTable {String}
- * @param pContainer {String}
- */
-function _updateDataToLost(pTable, pContainer )
+function _clearReasons(pReallyClear)
 {
-     var statusLost = db.table(SqlCondition.begin()
-                .andPrepare("AB_KEYWORD_ENTRY.CONTAINER", pContainer)
-                .andPrepare("AB_KEYWORD_ENTRY.TITLE", "Lost")
-                .buildSql("select KEYID from AB_KEYWORD_ENTRY", "1=2"));
-    
-    db.updateData(pTable, ["STATUS"], null, statusLost, SqlCondition.equals(pTable + ".SALESPROJECT_ID", vars.getString("$field.SALESPROJECTID"), "1=2"));
-}
+    if (pReallyClear)
+        db.updateData("SALESPROJECT", ["REASONS"], null, [""], SqlCondition.equals("SALESPROJECT.SALESPROJECTID", vars.getString("$field.SALESPROJECTID"), "1=2"));
+}
\ No newline at end of file
diff --git a/process/KeywordRegistry_basic/process.js b/process/KeywordRegistry_basic/process.js
index ffd3454e51..2addda46a4 100644
--- a/process/KeywordRegistry_basic/process.js
+++ b/process/KeywordRegistry_basic/process.js
@@ -60,3 +60,4 @@ $KeywordRegistry.salesprojectCompetitionState = function(){return "SalesprojectC
 $KeywordRegistry.objectRelationType = function(){return "ObjectRelationType"};
 $KeywordRegistry.deliveryTerm = function(){return "DeliveryTerm"};
 $KeywordRegistry.paymentTerm = function(){return "PaymentTerm"};
+
-- 
GitLab