From c2913d66f4cfb5c1e131d18c186a7699d1652f90 Mon Sep 17 00:00:00 2001
From: Tobias Feldmann <t.feldmann@adito.de>
Date: Wed, 19 Dec 2018 16:19:38 +0100
Subject: [PATCH] Lookup in Offer

---
 entity/Offer_entity/Offer_entity.aod          | 21 ---------
 .../relation_id/displayValueProcess.js        |  8 ++--
 .../relation_id/linkedContextProcess.js       |  3 +-
 process/Relation_lib/process.js               | 44 +++++++++++++++++++
 4 files changed, 48 insertions(+), 28 deletions(-)

diff --git a/entity/Offer_entity/Offer_entity.aod b/entity/Offer_entity/Offer_entity.aod
index 7e1f94098ab..1ff147af49a 100644
--- a/entity/Offer_entity/Offer_entity.aod
+++ b/entity/Offer_entity/Offer_entity.aod
@@ -319,15 +319,6 @@
         </entityParameter>
       </children>
     </entityOutgoingField>
-    <entityField>
-      <name>PERS_FIRSTNAME</name>
-    </entityField>
-    <entityField>
-      <name>PERS_LASTNAME</name>
-    </entityField>
-    <entityField>
-      <name>ORG_NAME</name>
-    </entityField>
     <entityOutgoingField>
       <name>OfferSalesproject_dfo</name>
       <fieldType>DEPENDENCY_OUT</fieldType>
@@ -457,18 +448,6 @@
           <name>RELATION_PERS_ID.value</name>
           <recordfield>RELATION.PERS_ID</recordfield>
         </dbRecordFieldMapping>
-        <dbRecordFieldMapping>
-          <name>ORG_NAME.value</name>
-          <recordfield>ORG.NAME</recordfield>
-        </dbRecordFieldMapping>
-        <dbRecordFieldMapping>
-          <name>PERS_FIRSTNAME.value</name>
-          <recordfield>PERS.FIRSTNAME</recordfield>
-        </dbRecordFieldMapping>
-        <dbRecordFieldMapping>
-          <name>PERS_LASTNAME.value</name>
-          <recordfield>PERS.LASTNAME</recordfield>
-        </dbRecordFieldMapping>
       </recordFieldMappings>
     </dbRecordContainer>
   </recordContainers>
diff --git a/entity/Offer_entity/entityfields/relation_id/displayValueProcess.js b/entity/Offer_entity/entityfields/relation_id/displayValueProcess.js
index 1d6c953ad45..03e388b961d 100644
--- a/entity/Offer_entity/entityfields/relation_id/displayValueProcess.js
+++ b/entity/Offer_entity/entityfields/relation_id/displayValueProcess.js
@@ -1,9 +1,7 @@
 import("system.result");
 import("system.vars");
 import("Relation_lib");
+import("system.neon");
 
-result.string(RelationUtils.getNameByPersOrg("$field.RELATION_PERS_ID", 
-                                             "$field.RELATION_ORG_ID", 
-                                             "$field.PERS_FIRSTNAME", 
-                                             "$field.PERS_LASTNAME",
-                                             "$field.ORG_NAME"));
\ No newline at end of file
+
+result.string(RelationUtils.getNameByPersOrgWithRelationId(vars.getString("$field.RELATION_ID")));
\ No newline at end of file
diff --git a/entity/Offer_entity/entityfields/relation_id/linkedContextProcess.js b/entity/Offer_entity/entityfields/relation_id/linkedContextProcess.js
index 57c48f6133b..d48ffe036d8 100644
--- a/entity/Offer_entity/entityfields/relation_id/linkedContextProcess.js
+++ b/entity/Offer_entity/entityfields/relation_id/linkedContextProcess.js
@@ -1,5 +1,4 @@
 import("system.result");
 import("Relation_lib");
 
-result.string(RelationUtils.getContextByPersOrg("$field.RELATION_PERS_ID", 
-                                                "$field.RELATION_ORG_ID"));
\ No newline at end of file
+result.string(RelationUtils.getContextByRelationId(vars.getString("$field.RELATION_ID")));
\ No newline at end of file
diff --git a/process/Relation_lib/process.js b/process/Relation_lib/process.js
index ec3ecf9bbe8..47de7d6eab9 100644
--- a/process/Relation_lib/process.js
+++ b/process/Relation_lib/process.js
@@ -97,6 +97,25 @@ RelationUtils.getContextByPersOrg = function(pPersIdField, pOrgIdField)
     }
 }
 
+RelationUtils.getContextByRelationId = function(pRelationId)
+{
+    if(!pRelationId)
+        return "";
+    else 
+    {
+        switch (RelationUtils.getRelationTypeByRelation(pRelationId))
+        {
+            case 1: // Org
+                return "Org_context";
+            case 2: // private Pers
+            case 3: // Pers
+                return "Pers_context";
+            default:
+                return "";
+        }
+    }
+}
+
 RelationUtils.getNameByPersOrg = function(pPersIdField, pOrgIdField, pPersFirstnameField, 
                                             pPersLastnameField, pOrgnameField)
 {
@@ -118,6 +137,31 @@ RelationUtils.getNameByPersOrg = function(pPersIdField, pOrgIdField, pPersFirstn
     }
 }
 
+RelationUtils.getNameByPersOrgWithRelationId = function(pRelationId)
+{
+    
+    var data = db.array(db.ROW, SqlCondition.begin()
+                            .andPrepare("RELATION.RELATIONID", pRelationId)
+                            .buildSelect("select RELATION.PERS_ID, RELATION.ORG_ID, PERS.FIRSTNAME, PERS.LASTNAME, ORG.NAME from RELATION RELATION join ORG on ORG.ORGID = RELATION.ORG_ID left join PERS on PERS.PERSID = RELATION.PERS_ID", "1 = 2"));                                    
+ 
+    if(!data[0] && !data[1])
+        return "";
+    else 
+    {
+        switch (RelationUtils.getRelationTypeByPersOrg(data[0], data[1]))
+        {
+            case 1: // Org   
+                return data[4];
+            case 2: // private Pers
+            case 3: // Pers
+                return data[2]
+                .concat(" " , data[3]);
+            default:
+                return "";
+        }
+    }
+}
+
 /**
  * returns the from string for the relation joined with org, pers, address 
  *
-- 
GitLab