From 98a52f2ba7601baee0f9b29b1ae7aea16093009e Mon Sep 17 00:00:00 2001
From: Benjamin Ulrich <b.ulrich@adito.de>
Date: Wed, 26 May 2021 15:36:56 +0000
Subject: [PATCH] Merge branch
 'sales_pn_1080452_salesproject_autoprobab_notheigher' into '2021.1'

[Projekt: xRM-Sales][TicketNr.: 1080452][Automatische Wahrscheinlichkeit soll...

See merge request xrm/basic!949

(cherry picked from commit 365d67bde10ac1415fe05e0d07ba1a745e72c560)

bdb10a19 [Projekt: xRM-Sales][TicketNr.: 1080452][Automatische Wahrscheinlichkeit soll...
6806bbb2 merged 2021.1 into sales_pn_1080452_salesproject_autoprobab_notheigher
9ce8032a [Projekt: xRM-Sales][TicketNr.: 1080452][Automatische Wahrscheinlichkeit soll...
---
 .../Salesproject_entity.aod                   |  4 +++
 .../entityfields/phase/onValueChange.js       | 25 +++++++++++++++++++
 .../recordcontainers/db/onDBUpdate.js         | 11 --------
 3 files changed, 29 insertions(+), 11 deletions(-)
 create mode 100644 entity/Salesproject_entity/entityfields/phase/onValueChange.js

diff --git a/entity/Salesproject_entity/Salesproject_entity.aod b/entity/Salesproject_entity/Salesproject_entity.aod
index 7f800c660a..f40a10ef01 100644
--- a/entity/Salesproject_entity/Salesproject_entity.aod
+++ b/entity/Salesproject_entity/Salesproject_entity.aod
@@ -47,6 +47,10 @@
       <mandatory v="true" />
       <state>EDITABLE</state>
       <displayValueProcess>%aditoprj%/entity/Salesproject_entity/entityfields/phase/displayValueProcess.js</displayValueProcess>
+      <onValueChange>%aditoprj%/entity/Salesproject_entity/entityfields/phase/onValueChange.js</onValueChange>
+      <onValueChangeTypes>
+        <element>MASK</element>
+      </onValueChangeTypes>
     </entityField>
     <entityField>
       <name>PROJECTCODE</name>
diff --git a/entity/Salesproject_entity/entityfields/phase/onValueChange.js b/entity/Salesproject_entity/entityfields/phase/onValueChange.js
new file mode 100644
index 0000000000..4fbd8be482
--- /dev/null
+++ b/entity/Salesproject_entity/entityfields/phase/onValueChange.js
@@ -0,0 +1,25 @@
+import("Sql_lib");
+import("system.neon");
+import("system.vars");
+
+var currPobab = parseInt(
+    newSelect("SALESPROJECT.PROBABILITY")
+    .from("SALESPROJECT")
+    .where("SALESPROJECT.SALESPROJECTID", vars.get("$field.SALESPROJECTID"))
+    .cell()
+);
+var autoProbab = parseInt(
+    newSelect("SALESPROJECTPHASEDEFINITION.AUTOPROBABILITY")
+    .from("SALESPROJECTPHASEDEFINITION")
+    .where("SALESPROJECTPHASEDEFINITION.PHASE", vars.get("$this.value"))
+    .cell()
+);
+
+if(isNaN(autoProbab) || autoProbab < currPobab)
+{
+    neon.setFieldValue("$field.PROBABILITY", currPobab);
+}
+else
+{
+    neon.setFieldValue("$field.PROBABILITY", autoProbab);
+}
diff --git a/entity/Salesproject_entity/recordcontainers/db/onDBUpdate.js b/entity/Salesproject_entity/recordcontainers/db/onDBUpdate.js
index 8c73baf3b5..c9dfa3fa5c 100644
--- a/entity/Salesproject_entity/recordcontainers/db/onDBUpdate.js
+++ b/entity/Salesproject_entity/recordcontainers/db/onDBUpdate.js
@@ -22,17 +22,6 @@ vars.get("$local.changed").forEach(function(fieldName) {
     
     switch (fieldName) {
         case "SALESPROJECT.PHASE":
-            typeValue = "SalesprojectPhase";
-            var autoProbab = newSelect("SALESPROJECTPHASEDEFINITION.AUTOPROBABILITY")
-                .from("SALESPROJECTPHASEDEFINITION")
-                .where("SALESPROJECTPHASEDEFINITION.PHASE", vars.get("$field.PHASE")).cell();
-            if(!isNaN(parseInt(autoProbab)))
-            {
-                newWhere("SALESPROJECT.SALESPROJECTID", vars.get("$field.SALESPROJECTID"))
-                .updateFields({
-                    "PROBABILITY": autoProbab
-                }, "SALESPROJECT");
-            }
             break;
         case "SALESPROJECT.STATUS":
             // Milestone
-- 
GitLab