From f7f2d770d17bcbb59136b7f73849089289d0e245 Mon Sep 17 00:00:00 2001
From: Johannes Hoermann <j.hoermann@adito.de>
Date: Thu, 25 Jul 2019 14:34:31 +0200
Subject: [PATCH] fix object tree and salesproject

---
 .../ObjectTree_entity/ObjectTree_entity.aod   |  3 ++
 .../Salesproject_entity.aod                   |  2 ++
 .../entityfields/state/onValueChange.js       | 33 +++++------------
 .../recordcontainers/db/onDBUpdate.js         | 36 +++++++++++++++----
 4 files changed, 42 insertions(+), 32 deletions(-)

diff --git a/entity/ObjectTree_entity/ObjectTree_entity.aod b/entity/ObjectTree_entity/ObjectTree_entity.aod
index c3e65c493c..8ede388f92 100644
--- a/entity/ObjectTree_entity/ObjectTree_entity.aod
+++ b/entity/ObjectTree_entity/ObjectTree_entity.aod
@@ -129,6 +129,9 @@
       <valueProcess>%aditoprj%/entity/ObjectTree_entity/entityfields/objectrelationtypeid/valueProcess.js</valueProcess>
       <displayValueProcess>%aditoprj%/entity/ObjectTree_entity/entityfields/objectrelationtypeid/displayValueProcess.js</displayValueProcess>
       <onValueChange>%aditoprj%/entity/ObjectTree_entity/entityfields/objectrelationtypeid/onValueChange.js</onValueChange>
+      <onValueChangeTypes>
+        <element>MASK</element>
+      </onValueChangeTypes>
     </entityField>
     <entityConsumer>
       <name>Objects</name>
diff --git a/entity/Salesproject_entity/Salesproject_entity.aod b/entity/Salesproject_entity/Salesproject_entity.aod
index 002facde19..0b3fcb0250 100644
--- a/entity/Salesproject_entity/Salesproject_entity.aod
+++ b/entity/Salesproject_entity/Salesproject_entity.aod
@@ -101,6 +101,8 @@
       <onValueChange>%aditoprj%/entity/Salesproject_entity/entityfields/state/onValueChange.js</onValueChange>
       <onValueChangeTypes>
         <element>MASK</element>
+        <element>PROCESS</element>
+        <element>PROCESS_SETVALUE</element>
       </onValueChangeTypes>
     </entityField>
     <entityField>
diff --git a/entity/Salesproject_entity/entityfields/state/onValueChange.js b/entity/Salesproject_entity/entityfields/state/onValueChange.js
index 2514501632..9ec0f299aa 100644
--- a/entity/Salesproject_entity/entityfields/state/onValueChange.js
+++ b/entity/Salesproject_entity/entityfields/state/onValueChange.js
@@ -1,28 +1,11 @@
-import("system.vars");
+import("system.logging");
 import("Sql_lib");
+import("system.vars");
 import("system.db");
-import("KeywordRegistry_basic");
-import("Keyword_lib");
-import("system.translate");
-import("system.question");
-import("system.neon");
-
-if (vars.get("$sys.recordstate") != neon.OPERATINGSTATE_NEW)
-{
-    var state = vars.get("local.value");
-
-    // Postponed
-    if (state == $KeywordRegistry.salesprojectState$postponed())
-    {
-        var stateName = KeywordUtils.getViewValue($KeywordRegistry.salesprojectState(), state);
-        var clearForecast = question.askYesNo(translate.withArguments("Clear forecast and set state to '%0'?", [stateName]), translate.withArguments("Would you really like to clear the forecast and set the state to '%0'?", [stateName]) + "\n" + translate.text("This cannot be undone!"), false);
 
-        if (!clearForecast)
-        {
-            // reset to value from db
-            neon.setFieldValue("$field.STATE", db.cell(SqlCondition.begin()
-                                                                   .andPrepareVars("SALESPROJECT.SALESPROJECTID", "$field.SALESPROJECTID")
-                                                                   .buildSql("select STATE from SALESPROJECT", "1=2")));
-        }
-    }
-}
+logging.log(JSON.stringify(db.cell(SqlCondition.begin()
+                                                      .andPrepareVars("SALESPROJECT.SALESPROJECTID", "$field.SALESPROJECTID")
+                                                      .buildSql("select STATE from SALESPROJECT", "1=2")), null, "\t"))
+vars.set("$context.statusBefore", db.cell(SqlCondition.begin()
+                                                      .andPrepareVars("SALESPROJECT.SALESPROJECTID", "$field.SALESPROJECTID")
+                                                      .buildSql("select STATE from SALESPROJECT", "1=2")));
\ 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 44bc3193d9..c600e30c47 100644
--- a/entity/Salesproject_entity/recordcontainers/db/onDBUpdate.js
+++ b/entity/Salesproject_entity/recordcontainers/db/onDBUpdate.js
@@ -1,3 +1,5 @@
+import("system.logging");
+import("Context_lib");
 import("system.neon");
 import("system.translate");
 import("system.vars");
@@ -31,7 +33,10 @@ vars.get("$local.changed").forEach(function(fieldName) {
             if (state == $KeywordRegistry.salesprojectState$lost())
             {
                 // set all offers of the salesproject to lost
-                db.updateData("OFFER", ["STATUS"], null, [$KeywordRegistry.offerStatus$lost()], SqlCondition.equals("OFFER.OBJECT_ROWID", vars.getString("$local.uid"), "1=2"));
+                db.updateData("OFFER", ["STATUS"], null, [$KeywordRegistry.offerStatus$lost()], SqlCondition.begin()
+                                                                                                            .andPrepare("OFFER.OBJECT_ROWID", vars.getString("$local.uid"))
+                                                                                                            .andPrepare("OFFER.OBJECT_TYPE", ContextUtils.getCurrentContextId())
+                                                                                                            .build("1=2"));
                 _updateReasons();
                 clearReason = false;
             }
@@ -39,16 +44,31 @@ vars.get("$local.changed").forEach(function(fieldName) {
             else if (state == $KeywordRegistry.salesprojectState$order())
             {
                 // set all competitions of the salesproject to lost
-                db.updateData("COMPETITION", ["STATUS"], null, [$KeywordRegistry.competitionState$lost()], SqlCondition.equals("COMPETITION.OBJECT_ROWID", vars.getString("$local.uid"), "1=2"));
+                db.updateData("COMPETITION", ["STATUS"], null, [$KeywordRegistry.competitionState$lost()], SqlCondition.begin()
+                                                                                                            .andPrepare("COMPETITION.OBJECT_ROWID", vars.getString("$local.uid"))
+                                                                                                            .andPrepare("COMPETITION.OBJECT_TYPE", ContextUtils.getCurrentContextId())
+                                                                                                            .build("1=2"));
                 _updateReasons();
                 clearReason = false;
             }
             // Postponed
             else if (state == $KeywordRegistry.salesprojectState$postponed())
             {
-                // do not notify as the user just got a question, if he really would like to clear the forecast
-                needToUpdateForecast = false;
-                db.deleteData("FORECAST", SqlCondition.equals("FORECAST.OBJECT_ROWID", vars.getString("$local.uid"), "1=2"));
+                // ask if forecast should be cleared. Else reset to old value.
+                var stateName = KeywordUtils.getViewValue($KeywordRegistry.salesprojectState(), state);
+                var clearForecast = question.askYesNo(translate.withArguments("Clear forecast and set state to '%0'?", [stateName]), translate.withArguments("Would you really like to clear the forecast and set the state to '%0'?", [stateName]) + "\n" + translate.text("This cannot be undone!"), false);
+
+                if (!clearForecast)
+                {
+                    // reset to value from db
+                    db.updateData("SALESPROJECT", ["STATE"], null, [vars.get("$context.statusBefore")], SqlCondition.begin()
+                                                                                                            .andPrepare("SALESPROJECT.SALESPROJECTID", vars.getString("$local.uid"))
+                                                                                                            .build("1=2"));
+                } else {
+                    // do not notify as the user just got a question, if he really would like to clear the forecast
+                    needToUpdateForecast = false;
+                    db.deleteData("FORECAST", SqlCondition.equals("FORECAST.OBJECT_ROWID", vars.getString("$local.uid"), "1=2"));
+                }                
             }
             // if state changed, but not to one of the above states
             else
@@ -58,7 +78,7 @@ vars.get("$local.changed").forEach(function(fieldName) {
             break;
     }
     
-    // It clearReason is undefined, it the state did not change. So do nothing
+    // If clearReason is undefined, it the state did not change. So do nothing
     if (clearReason != undefined)
     {
         _clearReasons(clearReason);
@@ -70,13 +90,15 @@ vars.get("$local.changed").forEach(function(fieldName) {
         if (needToUpdateForecast === null)
             needToUpdateForecast = true;
         
-        Salesproject.insertMilestone(vars.getString("$local.uid"), typeValue, vars.getString(fieldVar));
+        Salesproject.insertMilestone(vars.getString("$local.uid"), typeValue, rowdata[fieldName]);
     }
 });
 
 if (needToUpdateForecast)
     Salesproject.notifyToUpdateForecast();
 
+neon.refreshAll();
+
 function _updateReasons()
 {
     var reasons = rowdata["SALESPROJECT.REASONS"];
-- 
GitLab