From 0a1f145f9e018ca374a43c7e9e47b8cc65ced8c8 Mon Sep 17 00:00:00 2001
From: "j.goderbauer" <j.goderbauer@adito.de>
Date: Thu, 28 Nov 2019 15:18:55 +0100
Subject: [PATCH] Activity: new offer -> automatically links offer to activity
 # Conflicts: #
 entity/Activity_entity/entityfields/newoffer/onActionProcess.js

---
 .../entityfields/newoffer/onActionProcess.js   |  2 +-
 entity/Offer_entity/Offer_entity.aod           |  6 ++++++
 .../activityid_param/documentation.adoc        |  2 ++
 .../recordcontainers/db/onDBInsert.js          | 18 ++++++++++++++++--
 process/Offer_lib/process.js                   |  6 +++++-
 5 files changed, 30 insertions(+), 4 deletions(-)
 create mode 100644 entity/Offer_entity/entityfields/activityid_param/documentation.adoc

diff --git a/entity/Activity_entity/entityfields/newoffer/onActionProcess.js b/entity/Activity_entity/entityfields/newoffer/onActionProcess.js
index 09323bf30c5..2973002c2cf 100644
--- a/entity/Activity_entity/entityfields/newoffer/onActionProcess.js
+++ b/entity/Activity_entity/entityfields/newoffer/onActionProcess.js
@@ -1,4 +1,4 @@
 import("system.vars");
 import("Offer_lib");
 
-OfferUtils.createNewOffer(null, null, vars.getString("$field.ACTIVITYID"));
\ No newline at end of file
+OfferUtils.createNewOffer(null, null, null, vars.get("$field.ACTIVITYID"));
\ No newline at end of file
diff --git a/entity/Offer_entity/Offer_entity.aod b/entity/Offer_entity/Offer_entity.aod
index 210cf13a6f7..8256b181b4e 100644
--- a/entity/Offer_entity/Offer_entity.aod
+++ b/entity/Offer_entity/Offer_entity.aod
@@ -932,6 +932,12 @@
       <name>OfferStatus_param</name>
       <expose v="true" />
     </entityParameter>
+    <entityParameter>
+      <name>ActivityId_param</name>
+      <expose v="true" />
+      <mandatory v="false" />
+      <documentation>%aditoprj%/entity/Offer_entity/entityfields/activityid_param/documentation.adoc</documentation>
+    </entityParameter>
   </entityFields>
   <recordContainers>
     <dbRecordContainer>
diff --git a/entity/Offer_entity/entityfields/activityid_param/documentation.adoc b/entity/Offer_entity/entityfields/activityid_param/documentation.adoc
new file mode 100644
index 00000000000..0e34cbb98e3
--- /dev/null
+++ b/entity/Offer_entity/entityfields/activityid_param/documentation.adoc
@@ -0,0 +1,2 @@
+Param to automatically add an activitylink in an existing activity to a newly created offer. This will be done in on save.
+Usefull when creating a new offer from the activity point of view.
\ No newline at end of file
diff --git a/entity/Offer_entity/recordcontainers/db/onDBInsert.js b/entity/Offer_entity/recordcontainers/db/onDBInsert.js
index cf7eb05721d..74f0bb15903 100644
--- a/entity/Offer_entity/recordcontainers/db/onDBInsert.js
+++ b/entity/Offer_entity/recordcontainers/db/onDBInsert.js
@@ -1,6 +1,20 @@
+import("system.db");
+import("system.datetime");
+import("system.util");
 import("system.neon");
 import("system.vars");
 import("Offer_lib");
 
-if (vars.get("$sys.recordstate") == neon.OPERATINGSTATE_NEW && vars.exists("$param.OfferOriginal_Id_param"))
-    OfferUtils.copyOfferItems(vars.getString("$param.OfferOriginal_Id_param"), vars.get("$local.uid"));
+if (vars.get("$sys.recordstate") == neon.OPERATINGSTATE_NEW)
+{
+    if (vars.exists("$param.OfferOriginal_Id_param"))
+        OfferUtils.copyOfferItems(vars.getString("$param.OfferOriginal_Id_param"), vars.get("$local.uid"));
+    
+    var activityId = vars.get("$param.ActivityId_param");
+    if (activityId)
+    {
+        var activityLinkColumns = ["ACTIVITYLINKID", "ACTIVITY_ID", "DATE_NEW", "USER_NEW", "OBJECT_TYPE", "OBJECT_ROWID"];
+        var activityLinkValues = [util.getNewUUID(), activityId,  datetime.date(), vars.get("$sys.user"), "Offer", vars.get("$field.OFFERID")];
+        db.insertData("ACTIVITYLINK", activityLinkColumns, null, activityLinkValues);
+    }
+}
\ No newline at end of file
diff --git a/process/Offer_lib/process.js b/process/Offer_lib/process.js
index 67df25ece2c..a3bb020cc10 100644
--- a/process/Offer_lib/process.js
+++ b/process/Offer_lib/process.js
@@ -72,8 +72,9 @@ OfferUtils.isDeletable = function(status) {
 
 /**
  * Create a new offer and open the offer context in NEW-mode
+ * //TODO: function comment
  */
-OfferUtils.createNewOffer = function(pContextId, pRowId, pRelationId)
+OfferUtils.createNewOffer = function(pContextId, pRowId, pRelationId, pActivityId)
 {
     var params = {};
     
@@ -86,6 +87,9 @@ OfferUtils.createNewOffer = function(pContextId, pRowId, pRelationId)
     if (pRelationId)
         params["ContactId_param"] = pRelationId;
     
+    if (pActivityId)
+        params["ActivityId_param"] = pActivityId;
+    
     neon.openContext("Offer", null, null, neon.OPERATINGSTATE_NEW, params);
 }
 
-- 
GitLab