From 922c63b0eb3c5ad56d5cd21f0d9ce5e818142993 Mon Sep 17 00:00:00 2001
From: Johannes Hoermann <j.hoermann@adito.de>
Date: Fri, 28 Jun 2019 10:49:56 +0200
Subject: [PATCH] preset offer terms with attrs from org if its a person

---
 .../entityfields/deliveryterms/valueProcess.js | 14 ++++++++++----
 .../children/contactid_param/valueProcess.js   | 18 +++++++++---------
 .../entityfields/paymentterms/valueProcess.js  | 11 ++++++++---
 process/Contact_lib/process.js                 | 18 ++++++++++++++++++
 4 files changed, 45 insertions(+), 16 deletions(-)

diff --git a/entity/Offer_entity/entityfields/deliveryterms/valueProcess.js b/entity/Offer_entity/entityfields/deliveryterms/valueProcess.js
index 0ae20dad6e..dbe9f498ff 100644
--- a/entity/Offer_entity/entityfields/deliveryterms/valueProcess.js
+++ b/entity/Offer_entity/entityfields/deliveryterms/valueProcess.js
@@ -1,8 +1,10 @@
+import("system.logging");
 import("AttributeRegistry_basic");
 import("system.neon");
 import("system.result");
 import("system.vars");
 import("Attribute_lib");
+import("Contact_lib");
 
 if (vars.exists("$param.OfferDeliveryTerm_param") && vars.get("$param.OfferDeliveryTerm_param")) 
 {
@@ -10,9 +12,13 @@ if (vars.exists("$param.OfferDeliveryTerm_param") && vars.get("$param.OfferDeliv
 } 
 else if (vars.get("$sys.recordstate") == neon.OPERATINGSTATE_NEW)
 {
-    var contactId = vars.getString("$field.CONTACT_ID");
-    if (contactId)
-    { 
-        result.string(AttributeRelationUtils.getAttribute($AttributeRegistry.deliveryTerm(), contactId));
+    var contactIds = ContactUtils.getPersOrgIds(vars.getString("$field.CONTACT_ID"))
+    if (contactIds.length >= 3 && contactIds[2])
+    {
+        var orgContactId = ContactUtils.getOrgContactId(contactIds[2]);
+        if (orgContactId)
+        { 
+            result.string(AttributeRelationUtils.getAttribute($AttributeRegistry.deliveryTerm(), orgContactId));
+        }
     }
 }
\ No newline at end of file
diff --git a/entity/Offer_entity/entityfields/objects/children/contactid_param/valueProcess.js b/entity/Offer_entity/entityfields/objects/children/contactid_param/valueProcess.js
index 22a6e045a3..7b92c1bb14 100644
--- a/entity/Offer_entity/entityfields/objects/children/contactid_param/valueProcess.js
+++ b/entity/Offer_entity/entityfields/objects/children/contactid_param/valueProcess.js
@@ -1,10 +1,10 @@
-import("system.result");
-import("system.vars");
-import("Contact_lib");
-
-if (vars.get("$field.CONTACT_ID"))
-{
-    var ids = ContactUtils.getPersOrgIds(vars.getString("$field.CONTACT_ID"));
-    if (ids.length >= 3 && ids[2])
-        result.string(ContactUtils.getPersOrgIds(vars.getString("$field.CONTACT_ID"))[2]);
+import("system.result");
+import("system.vars");
+import("Contact_lib");
+
+if (vars.get("$field.CONTACT_ID"))
+{
+    var ids = ContactUtils.getPersOrgIds(vars.getString("$field.CONTACT_ID"));
+    if (ids.length >= 3 && ids[2])
+        result.string(ids[2]);
 }
\ No newline at end of file
diff --git a/entity/Offer_entity/entityfields/paymentterms/valueProcess.js b/entity/Offer_entity/entityfields/paymentterms/valueProcess.js
index e75c10c975..020e52b587 100644
--- a/entity/Offer_entity/entityfields/paymentterms/valueProcess.js
+++ b/entity/Offer_entity/entityfields/paymentterms/valueProcess.js
@@ -1,3 +1,4 @@
+import("Contact_lib");
 import("AttributeRegistry_basic");
 import("system.neon");
 import("system.result");
@@ -10,9 +11,13 @@ if (vars.exists("$param.OfferPaymentTerm_param") && vars.get("$param.OfferPaymen
 }
 else if (vars.get("$sys.recordstate") == neon.OPERATINGSTATE_NEW)
 {
-    var contactId = vars.getString("$field.CONTACT_ID");
-    if (contactId)
+    var contactIds = ContactUtils.getPersOrgIds(vars.getString("$field.CONTACT_ID"))
+    if (contactIds.length >= 3 && contactIds[2])
     {
-        result.string(AttributeRelationUtils.getAttribute($AttributeRegistry.paymentTerm(), contactId));
+        var orgContactId = ContactUtils.getOrgContactId(contactIds[2]);
+        if (orgContactId)
+        { 
+            result.string(AttributeRelationUtils.getAttribute($AttributeRegistry.paymentTerm(), orgContactId));
+        }
     }
 }
\ No newline at end of file
diff --git a/process/Contact_lib/process.js b/process/Contact_lib/process.js
index 5cca68dfd2..3ea1aece0b 100644
--- a/process/Contact_lib/process.js
+++ b/process/Contact_lib/process.js
@@ -292,6 +292,24 @@ ContactUtils.getPersOrgIds = function(pContactId)
     return [];
 }
 
+/**
+ * get the contactId from the OrganisationId
+ * 
+ * @param {String} pOrgId
+ * @return {String} the contactId or ""
+ */
+ContactUtils.getOrgContactId = function(pOrgId)
+{
+    if (pOrgId) {
+        return db.cell(SqlCondition.begin()
+                        .andPrepare("CONTACT.ORGANISATION_ID", pOrgId)
+                        .and("CONTACT.PERSON_ID is null")
+                        .buildSql("select CONTACTID from CONTACT", "1=0"));
+    }
+    
+    return "";
+}
+
 /**
  * get the name of the person or organisation
  * 
-- 
GitLab