From 9aab869374beee80e0785798359bb9556be68ab8 Mon Sep 17 00:00:00 2001
From: Johannes Hoermann <j.hoermann@adito.de>
Date: Thu, 22 Nov 2018 17:06:21 +0100
Subject: [PATCH] Salesproject Milestones on insert

---
 aliasDefinition/Data_alias/Data_alias.aod     | 27 +++++-----
 .../conditionProcess.js                       |  3 ++
 .../Salesproject_entity.aod                   | 10 ++--
 .../entityfields/projectcode/onValidation.js  |  6 +--
 .../entityfields/projectcode/valueProcess.js  |  6 +--
 .../salesprojectid/valueProcess.js            |  7 ---
 .../{status => state}/possibleItemsProcess.js |  0
 entity/Salesproject_entity/onDBInsert.js      |  5 ++
 .../SalesprojectEdit_view.aod                 |  4 +-
 .../SalesprojectFilter_view.aod               |  2 +-
 .../SalesprojectPreview_view.aod              |  4 +-
 .../example_salesproject/SALESPROJECT_gfk.xml |  2 +-
 .../db_changes/struct/create_salesproject.xml |  2 +-
 process/Salesproject_lib/process.js           | 52 +++++++++++++++----
 14 files changed, 79 insertions(+), 51 deletions(-)
 delete mode 100644 entity/Salesproject_entity/entityfields/salesprojectid/valueProcess.js
 rename entity/Salesproject_entity/entityfields/{status => state}/possibleItemsProcess.js (100%)
 create mode 100644 entity/Salesproject_entity/onDBInsert.js

diff --git a/aliasDefinition/Data_alias/Data_alias.aod b/aliasDefinition/Data_alias/Data_alias.aod
index b61203d40a..61cab7a73c 100644
--- a/aliasDefinition/Data_alias/Data_alias.aod
+++ b/aliasDefinition/Data_alias/Data_alias.aod
@@ -2771,19 +2771,6 @@
                 <title></title>
                 <description></description>
               </entityFieldDb>
-              <entityFieldDb>
-                <name>STATUS</name>
-                <dbName></dbName>
-                <primaryKey v="false" />
-                <columnType v="4" />
-                <size v="10" />
-                <scale v="0" />
-                <notNull v="false" />
-                <isUnique v="false" />
-                <index v="false" />
-                <title></title>
-                <description></description>
-              </entityFieldDb>
               <entityFieldDb>
                 <name>PROJECTCODE</name>
                 <dbName></dbName>
@@ -2876,6 +2863,20 @@
                 <title></title>
                 <description></description>
               </entityFieldDb>
+              <entityFieldDb>
+                <name>STATE</name>
+                <dbName></dbName>
+                <primaryKey v="false" />
+                <columnType v="4" />
+                <size v="10" />
+                <scale v="0" />
+                <notNull v="false" />
+                <isUnique v="false" />
+                <index v="false" />
+                <documentation></documentation>
+                <title></title>
+                <description></description>
+              </entityFieldDb>
             </entityFields>
           </entityDb>
           <entityDb>
diff --git a/entity/SalesprojectCycle_entity/conditionProcess.js b/entity/SalesprojectCycle_entity/conditionProcess.js
index 46b431f089..4048fcb70b 100644
--- a/entity/SalesprojectCycle_entity/conditionProcess.js
+++ b/entity/SalesprojectCycle_entity/conditionProcess.js
@@ -1,8 +1,11 @@
 import("system.db");
 import("system.result");
+import("system.vars");
+import("system.logging");
 import("Sql_lib");
 
 var cond = new SqlCondition();
+logging.log(vars.get("$param.SalesprojectId_param"))
 cond.andPrepareVars("SALESPROJECT_CYCLE.SALESPROJECT_ID", "$param.SalesprojectId_param");
 
 //TODO: use a preparedCondition when available
diff --git a/entity/Salesproject_entity/Salesproject_entity.aod b/entity/Salesproject_entity/Salesproject_entity.aod
index a78ae4df6d..91fa179c02 100644
--- a/entity/Salesproject_entity/Salesproject_entity.aod
+++ b/entity/Salesproject_entity/Salesproject_entity.aod
@@ -5,10 +5,9 @@
   <majorModelMode>DISTRIBUTED</majorModelMode>
   <recordContainerType>DB</recordContainerType>
   <caption>Salesproject</caption>
-  <onValidation>%aditoprj%/entity/Salesproject_entity/onValidation.js</onValidation>
-  <afterOperatingState>%aditoprj%/entity/Salesproject_entity/afterOperatingState.js</afterOperatingState>
   <iconId>VAADIN:FILTER</iconId>
   <alias>Data_alias</alias>
+  <onDBInsert>%aditoprj%/entity/Salesproject_entity/onDBInsert.js</onDBInsert>
   <entityFields>
     <entityIncomingField>
       <name>#INCOMING</name>
@@ -92,7 +91,6 @@
       <name>SALESPROJECTID</name>
       <tableName>SALESPROJECT</tableName>
       <columnName>SALESPROJECTID</columnName>
-      <valueProcess>%aditoprj%/entity/Salesproject_entity/entityfields/salesprojectid/valueProcess.js</valueProcess>
     </entityField>
     <entityField>
       <name>STARTDATE</name>
@@ -105,12 +103,12 @@
       <valueProcess>%aditoprj%/entity/Salesproject_entity/entityfields/startdate/valueProcess.js</valueProcess>
     </entityField>
     <entityField>
-      <name>STATUS</name>
+      <name>STATE</name>
       <title>Status</title>
       <tableName>SALESPROJECT</tableName>
-      <columnName>STATUS</columnName>
+      <columnName>STATE</columnName>
       <mandatory v="true" />
-      <possibleItemsProcess>%aditoprj%/entity/Salesproject_entity/entityfields/status/possibleItemsProcess.js</possibleItemsProcess>
+      <possibleItemsProcess>%aditoprj%/entity/Salesproject_entity/entityfields/state/possibleItemsProcess.js</possibleItemsProcess>
     </entityField>
     <entityField>
       <name>USER_EDIT</name>
diff --git a/entity/Salesproject_entity/entityfields/projectcode/onValidation.js b/entity/Salesproject_entity/entityfields/projectcode/onValidation.js
index 811153527d..d98945cc17 100644
--- a/entity/Salesproject_entity/entityfields/projectcode/onValidation.js
+++ b/entity/Salesproject_entity/entityfields/projectcode/onValidation.js
@@ -4,10 +4,8 @@ import("system.neon");
 import("Salesproject_lib");
 import("Util_lib");
 
-var SalesprojectUtils = new SalesprojectUtils();
-
 if( vars.get("$sys.operatingstate") == neon.OPERATINGSTATE_NEW
-    && !SalesprojectUtils.validateProjectNumber(ProcessHandlingUtil.getOnValidationValue(vars.get("$field.PROJECTCODE"))) )
+    && !Salesproject.validateProjectNumber(ProcessHandlingUtil.getOnValidationValue(vars.get("$field.PROJECTCODE"))) )
 {
-    vars.set( "$field.PROJECTCODE", SalesprojectUtils.getNextProjectNumber().toString("1 = 1") );
+    vars.set( "$field.PROJECTCODE", Salesproject.getNextProjectNumber().toString("1 = 1") );
 }
\ No newline at end of file
diff --git a/entity/Salesproject_entity/entityfields/projectcode/valueProcess.js b/entity/Salesproject_entity/entityfields/projectcode/valueProcess.js
index ae603c5b65..140b1f23cd 100644
--- a/entity/Salesproject_entity/entityfields/projectcode/valueProcess.js
+++ b/entity/Salesproject_entity/entityfields/projectcode/valueProcess.js
@@ -4,8 +4,6 @@ import("system.neon");
 import("Salesproject_lib");
 
 if(vars.get("$sys.operatingstate") == neon.OPERATINGSTATE_NEW)
-{
-    var SalesprojectUtils = new SalesprojectUtils();
-    
-    result.string( SalesprojectUtils.getNextProjectNumber() );
+{    
+    result.string( Salesproject.getNextProjectNumber() );
 }
\ No newline at end of file
diff --git a/entity/Salesproject_entity/entityfields/salesprojectid/valueProcess.js b/entity/Salesproject_entity/entityfields/salesprojectid/valueProcess.js
deleted file mode 100644
index 5be18c3983..0000000000
--- a/entity/Salesproject_entity/entityfields/salesprojectid/valueProcess.js
+++ /dev/null
@@ -1,7 +0,0 @@
-import("system.util");
-import("system.vars");
-import("system.result");
-import("system.neon");
-
-if(vars.get("$sys.operatingstate") == neon.OPERATINGSTATE_NEW)
-    result.string(util.getNewUUID());
\ No newline at end of file
diff --git a/entity/Salesproject_entity/entityfields/status/possibleItemsProcess.js b/entity/Salesproject_entity/entityfields/state/possibleItemsProcess.js
similarity index 100%
rename from entity/Salesproject_entity/entityfields/status/possibleItemsProcess.js
rename to entity/Salesproject_entity/entityfields/state/possibleItemsProcess.js
diff --git a/entity/Salesproject_entity/onDBInsert.js b/entity/Salesproject_entity/onDBInsert.js
new file mode 100644
index 0000000000..677dab41bf
--- /dev/null
+++ b/entity/Salesproject_entity/onDBInsert.js
@@ -0,0 +1,5 @@
+import("system.vars");
+import("Salesproject_lib");
+
+Salesproject.insertMilestone(vars.getString("$field.SALESPROJECTID"), Salesproject_TYPES.PHASE, vars.get("$field.PHASE"));
+Salesproject.insertMilestone(vars.getString("$field.SALESPROJECTID"), Salesproject_TYPES.STATE, vars.get("$field.STATE"));
\ No newline at end of file
diff --git a/neonView/SalesprojectEdit_view/SalesprojectEdit_view.aod b/neonView/SalesprojectEdit_view/SalesprojectEdit_view.aod
index 0c2a387874..f0279b641d 100644
--- a/neonView/SalesprojectEdit_view/SalesprojectEdit_view.aod
+++ b/neonView/SalesprojectEdit_view/SalesprojectEdit_view.aod
@@ -30,8 +30,8 @@
           <entityField>INFO</entityField>
         </entityFieldLink>
         <entityFieldLink>
-          <name>218a0751-9a53-4639-9d79-f9463f399b93</name>
-          <entityField>STATUS</entityField>
+          <name>426a20c1-5d79-417b-bab1-d3100fabb25b</name>
+          <entityField>STATE</entityField>
         </entityFieldLink>
         <entityFieldLink>
           <name>c00b2121-c44d-41a1-8c32-e2339b4dd234</name>
diff --git a/neonView/SalesprojectFilter_view/SalesprojectFilter_view.aod b/neonView/SalesprojectFilter_view/SalesprojectFilter_view.aod
index ea47ded944..bf2e8d40d2 100644
--- a/neonView/SalesprojectFilter_view/SalesprojectFilter_view.aod
+++ b/neonView/SalesprojectFilter_view/SalesprojectFilter_view.aod
@@ -38,7 +38,7 @@
         </neonTableColumn>
         <neonTableColumn>
           <name>52a851b0-9059-4d57-8ff5-335c819359e8</name>
-          <entityField>STATUS</entityField>
+          <entityField>STATE</entityField>
         </neonTableColumn>
       </columns>
     </tableViewTemplate>
diff --git a/neonView/SalesprojectPreview_view/SalesprojectPreview_view.aod b/neonView/SalesprojectPreview_view/SalesprojectPreview_view.aod
index 27bb4a2a8c..6ede57f817 100644
--- a/neonView/SalesprojectPreview_view/SalesprojectPreview_view.aod
+++ b/neonView/SalesprojectPreview_view/SalesprojectPreview_view.aod
@@ -28,8 +28,8 @@
           <entityField>INFO</entityField>
         </entityFieldLink>
         <entityFieldLink>
-          <name>9fcb6c9d-1dbb-4246-9a2b-4c430d9664a0</name>
-          <entityField>STATUS</entityField>
+          <name>4e3d7a37-f55b-4c18-9ba1-ab4ab0bbb442</name>
+          <entityField>STATE</entityField>
         </entityFieldLink>
         <entityFieldLink>
           <name>ff0dcd67-56ec-4db1-8c53-531f22fda716</name>
diff --git a/others/db_changes/data/example_salesproject/SALESPROJECT_gfk.xml b/others/db_changes/data/example_salesproject/SALESPROJECT_gfk.xml
index 6d02bf80c0..f806e07377 100644
--- a/others/db_changes/data/example_salesproject/SALESPROJECT_gfk.xml
+++ b/others/db_changes/data/example_salesproject/SALESPROJECT_gfk.xml
@@ -9,7 +9,7 @@
         <column name="PROJECTTITLE" value="GfK"/>
         <column name="RELATION_ID" value="b219b58a-f120-42d8-9a64-0b176501eac7"/>
         <column name="INFO" value="evtl. noch mit Berater Müller sprechen, ob ihm bekannt"/>
-        <column name="STATUS" valueNumeric="1"/>
+        <column name="STATE" valueNumeric="1"/>
         <column name="PHASE" valueNumeric="4"/>
         <column name="VOLUME" valueNumeric="15"/>
         <column name="PHASE" valueNumeric="4"/>
diff --git a/others/db_changes/struct/create_salesproject.xml b/others/db_changes/struct/create_salesproject.xml
index 459288a2f4..fa50bd40ff 100644
--- a/others/db_changes/struct/create_salesproject.xml
+++ b/others/db_changes/struct/create_salesproject.xml
@@ -11,7 +11,7 @@
                 <constraints nullable="false"/>
             </column>  
             <column name="INFO" type="NCLOB"/>
-            <column name="STATUS" type="INTEGER"/>
+            <column name="STATE" type="INTEGER"/>
             <column name="PHASE" type="INTEGER"/>
             <column name="VOLUME" type="NUMERIC(14,2)"/>
             <column name="STARTDATE" type="TIMESTAMP"/>
diff --git a/process/Salesproject_lib/process.js b/process/Salesproject_lib/process.js
index 6f0456ff37..e3f10b6a49 100644
--- a/process/Salesproject_lib/process.js
+++ b/process/Salesproject_lib/process.js
@@ -3,31 +3,63 @@ import("system.db");
 import("system.eMath");
 import("Util_lib");
 
-function SalesprojectUtils(){
-    var that = this;
+/**
+*  Types for the milestone.
+*/
+Salesproject_TYPES = {
+    PHASE: "Phase",
+    STATE: "State"
+}
+
+/**
+ * Methods used by the Salesproject.
+ * 
+ * Do not create an instance of this!
+ */
+Salesproject = {
+    
     /**
      * Delivers the next valid project number (has to be unique)
      * 
      * @result {String} next valid project number
      */
-    this.getNextProjectNumber = function(){
+    getNextProjectNumber: function() {
         var JdUtils = new JDitoUtils();
         return JdUtils.getNextUniqueNumber("PROJECTCODE", "SALESPROJECT");
-    }
+    },
     
     /**
      * Checks if the passed project number is valid (has to be unique)
      * 
-     * @param {String} pProjectNumber project number to check
+     * @param {String} projectNumber project number to check
      * 
      * @result {boolean} passed number is valid
      */
-    this.validateProjectNumber = function(pProjectNumber){
+    validateProjectNumber: function(projectNumber) {
         var JdUtils = new JDitoUtils();
-        return JdUtils.validateUniqueNumber(pProjectNumber, "PROJECTCODE", "SALESPROJECT");
-    }
+        return JdUtils.validateUniqueNumber(projectNumber, "PROJECTCODE", "SALESPROJECT");
+    },
     
-    this.getProjectNumberValidationFailString = function(){
-        return translate.text("The project number already exists!");
+    /**
+     * Insert a new milestone.
+     * 
+     * @param {String} ID of the salesproject
+     * @param {TYPES} type type can either be Salesproject_TYPES.PHASE or Salesproject_TYPES.STATE
+     * @param {Integer} value value of the phase or state
+     * 
+     * @result {boolean} true if inserted, else false
+     */
+    insertMilestone: function(salesprojectId, type, value) {
+        if (type == Salesproject_TYPES.PHASE || type == Salesproject_TYPES.STATE) {
+            db.insertData(
+                "SALESPROJECT_CYCLE",
+                ["SALESPROJECT_CYCLEID", "SALESPROJECT_ID", "TYPE", "VALUE", "DATE_START", "USER_NEW", "DATE_NEW"],
+                null,
+                [util.getNewUUID(), salesprojectId, type, value, vars.get("$sys.date"), vars.get("$sys.user"), vars.get("$sys.date")]);
+                
+            return true;
+        }
+        
+        return false;
     }
 }
\ No newline at end of file
-- 
GitLab