From f98f30a85c457bb41bc420aa689ce78b40b23c01 Mon Sep 17 00:00:00 2001
From: Johannes Hoermann <j.hoermann@adito.de>
Date: Tue, 17 Sep 2019 09:17:47 +0200
Subject: [PATCH] fix copy bulkmail

---
 entity/BulkMail_entity/BulkMail_entity.aod    |  1 -
 .../copybulkmail/onActionProcess.js           |  8 +++++
 entity/BulkMail_entity/grantUpdateProcess.js  |  2 +-
 .../recordcontainers/db/onDBInsert.js         | 33 ++++++++++++++++++-
 4 files changed, 41 insertions(+), 3 deletions(-)

diff --git a/entity/BulkMail_entity/BulkMail_entity.aod b/entity/BulkMail_entity/BulkMail_entity.aod
index dbd39ab7d3..5b41f08661 100644
--- a/entity/BulkMail_entity/BulkMail_entity.aod
+++ b/entity/BulkMail_entity/BulkMail_entity.aod
@@ -50,7 +50,6 @@
       <onValueChange>%aditoprj%/entity/BulkMail_entity/entityfields/documenttemplate_id/onValueChange.js</onValueChange>
       <onValueChangeTypes>
         <element>MASK</element>
-        <element>PROCESS_SETVALUE</element>
       </onValueChangeTypes>
     </entityField>
     <entityConsumer>
diff --git a/entity/BulkMail_entity/entityfields/copybulkmail/onActionProcess.js b/entity/BulkMail_entity/entityfields/copybulkmail/onActionProcess.js
index 2a9ba1d3ac..7c24cc7374 100644
--- a/entity/BulkMail_entity/entityfields/copybulkmail/onActionProcess.js
+++ b/entity/BulkMail_entity/entityfields/copybulkmail/onActionProcess.js
@@ -2,5 +2,13 @@ import("system.vars");
 import("system.neon");
 import("Bulkmail_lib");
 
+// How the copy works:
+// - the Bulkmail-context is called in NEW mode and CopyBulkMailId_param set to the current bulkmail id
+// - the valueProcess of field BULKMAILID is used to load the data via loadEntity and sets the fields
+//   (Note: I also could have done it like in offer / order and provide everything as parameter,
+//    but I tried if i it is possible to do it with less processes and a central process loading the data.
+// - The $field.bindata field is set seperately in it's value process
+// - The Recipients are added in onDBInsert of the record container
+
 BulkMailUtils.copy(vars.get("$field.BULKMAILID"));
     
\ No newline at end of file
diff --git a/entity/BulkMail_entity/grantUpdateProcess.js b/entity/BulkMail_entity/grantUpdateProcess.js
index 31a44323d1..25e2b4db25 100644
--- a/entity/BulkMail_entity/grantUpdateProcess.js
+++ b/entity/BulkMail_entity/grantUpdateProcess.js
@@ -2,4 +2,4 @@ import("Bulkmail_lib");
 import("system.vars");
 import("system.result");
 
-result.object(!BulkMailUtils.isStatusSendingOrSent);
\ No newline at end of file
+result.object(!BulkMailUtils.isStatusSendingOrSent(vars.get("$field.STATUS")));
\ No newline at end of file
diff --git a/entity/BulkMail_entity/recordcontainers/db/onDBInsert.js b/entity/BulkMail_entity/recordcontainers/db/onDBInsert.js
index f1f6065954..b7a1299898 100644
--- a/entity/BulkMail_entity/recordcontainers/db/onDBInsert.js
+++ b/entity/BulkMail_entity/recordcontainers/db/onDBInsert.js
@@ -1,7 +1,9 @@
+import("system.logging");
 import("Sql_lib");
 import("DocumentTemplate_lib");
 import("system.result");
 import("system.vars");
+import("system.entities");
 import("system.db");
 import("system.util");
 import("Document_lib");
@@ -26,7 +28,36 @@ if (template.content)
         "", template.content, template.filename, "", "", SqlUtils.getBinariesAlias());
 }
 
-var contactIds = JSON.parse(vars.getString("$param.PresetRecipients_param"));
+var contactIds = [];
+
+if (vars.get("$param.CopyBulkMailId_param"))
+{
+    logging.log(JSON.stringify(["COPY: ",
+    "BulkMailRecipient_entity",
+    "BulkMailRecipients",
+    ["BulkMailId_param", vars.get("$param.CopyBulkMailId_param")],
+    ["#UID"]
+
+], null, "\t"))
+    var config = entities.createConfigForLoadingRows()
+                         .entity("BulkMailRecipient_entity")
+                         .provider("BulkMailRecipients")
+                         .addParameter("BulkMailId_param", vars.get("$param.CopyBulkMailId_param"))
+                         .fields(["CONTACT_ID"]);
+                         
+    var copiedRecipients = entities.getRows(config);
+        logging.log(JSON.stringify([copiedRecipients], null, "\t"))
+
+    contactIds = copiedRecipients.map(function(pRecipient) 
+    {
+        return pRecipient["CONTACT_ID"];
+    });
+}
+
+if (vars.getString("$param.PresetRecipients_param"))
+    contactIds = contactIds.concat(JSON.parse(vars.getString("$param.PresetRecipients_param")));
+
+logging.log(JSON.stringify([contactIds], null, "\t"))
 
 if (contactIds && contactIds.length > 0)
     BulkMailUtils.addRecipients(bulkMailId, BulkMailUtils.filterNewRecipients(bulkMailId, contactIds));
\ No newline at end of file
-- 
GitLab