From a99645b35fbab37798727208f10da99ab1a5588d Mon Sep 17 00:00:00 2001
From: "c.wimmer" <c.wimmer@cwimmer-nb.aditosoftware.local>
Date: Mon, 16 Nov 2020 16:22:36 +0100
Subject: [PATCH] [Projekt: Crowd-Development / Besuch vor Ort][TicketNr.:
 1068875][[Bug] Besuchsvorschlag Preview wenn Kontakt hinterlegt ist
 fehlerhaft]

---
 .../VisitRecommendation_entity.aod                       | 7 ++++++-
 .../entityfields/target_context/valueProcess.js          | 6 +++++-
 .../entityfields/target_id/valueProcess.js               | 7 +++++++
 .../recordcontainers/jdito/contentProcess.js             | 9 ++++++---
 4 files changed, 24 insertions(+), 5 deletions(-)
 create mode 100644 entity/VisitRecommendation_entity/entityfields/target_id/valueProcess.js

diff --git a/entity/VisitRecommendation_entity/VisitRecommendation_entity.aod b/entity/VisitRecommendation_entity/VisitRecommendation_entity.aod
index 3ee0de7653f..f545f2733aa 100644
--- a/entity/VisitRecommendation_entity/VisitRecommendation_entity.aod
+++ b/entity/VisitRecommendation_entity/VisitRecommendation_entity.aod
@@ -13,7 +13,7 @@
     <entityProvider>
       <name>#PROVIDER</name>
       <targetContextField>TARGET_CONTEXT</targetContextField>
-      <targetIdField>CONTACT_ID</targetIdField>
+      <targetIdField>TARGET_ID</targetIdField>
       <documentation>%aditoprj%/entity/VisitRecommendation_entity/entityfields/#provider/documentation.adoc</documentation>
       <dependencies>
         <entityDependency>
@@ -167,6 +167,7 @@
       <description></description>
       <consumer>PersonConsumer</consumer>
       <groupable v="true" />
+      <linkedContext>Person</linkedContext>
       <valueProcess>%aditoprj%/entity/VisitRecommendation_entity/entityfields/contact_person_id/valueProcess.js</valueProcess>
       <displayValueProcess>%aditoprj%/entity/VisitRecommendation_entity/entityfields/contact_person_id/displayValueProcess.js</displayValueProcess>
     </entityField>
@@ -237,6 +238,10 @@
     <entityField>
       <name>STATUS</name>
     </entityField>
+    <entityField>
+      <name>TARGET_ID</name>
+      <valueProcess>%aditoprj%/entity/VisitRecommendation_entity/entityfields/target_id/valueProcess.js</valueProcess>
+    </entityField>
   </entityFields>
   <recordContainers>
     <jDitoRecordContainer>
diff --git a/entity/VisitRecommendation_entity/entityfields/target_context/valueProcess.js b/entity/VisitRecommendation_entity/entityfields/target_context/valueProcess.js
index a16a18c5851..2899d9fff95 100644
--- a/entity/VisitRecommendation_entity/entityfields/target_context/valueProcess.js
+++ b/entity/VisitRecommendation_entity/entityfields/target_context/valueProcess.js
@@ -1,3 +1,7 @@
+import("system.vars");
 import("system.result");
 
-result.string("Organisation")
\ No newline at end of file
+if(vars.get("$field.CONTACT_PERSON_ID"))
+    result.string("Person")
+else
+    result.string("Organisation")
\ No newline at end of file
diff --git a/entity/VisitRecommendation_entity/entityfields/target_id/valueProcess.js b/entity/VisitRecommendation_entity/entityfields/target_id/valueProcess.js
new file mode 100644
index 00000000000..b68d32e6889
--- /dev/null
+++ b/entity/VisitRecommendation_entity/entityfields/target_id/valueProcess.js
@@ -0,0 +1,7 @@
+import("system.result");
+import("system.vars");
+
+if(vars.get("$field.CONTACT_PERSON_ID"))
+    result.string(vars.get("$field.CONTACT_PERSON_ID"))
+else
+    result.string(vars.get("$field.CONTACT_ID"))
\ No newline at end of file
diff --git a/entity/VisitRecommendation_entity/recordcontainers/jdito/contentProcess.js b/entity/VisitRecommendation_entity/recordcontainers/jdito/contentProcess.js
index e10e6cfdf71..6503563c543 100644
--- a/entity/VisitRecommendation_entity/recordcontainers/jdito/contentProcess.js
+++ b/entity/VisitRecommendation_entity/recordcontainers/jdito/contentProcess.js
@@ -105,7 +105,7 @@ if(idValues == false)
                         .and("ACTIVITY.CATEGORY", "VISIT")
 
     var visitFrequencyData = newSelect(["''"    //0
-                                        , "CONTACT.CONTACTID"   //1
+                                        , "CONTACT.CONTACTID" //1
                                         , "(" + newSelect("CONTACT.CONTACTID")
                                                 .from("CONTACT")
                                                 .leftJoin("AB_ATTRIBUTERELATION", "CONTACT.CONTACTID = AB_ATTRIBUTERELATION.OBJECT_ROWID")
@@ -120,7 +120,9 @@ if(idValues == false)
                                         ,"ADDRESS.CITY"    //6
                                         , "visitPlanFrequency.ID_VALUE"//7
                                         , activitySubQuery //8
-                                        , "org.NAME"])//9
+                                        , "org.NAME" //9
+                                        , "PERSON.PERSONID" //10
+                                ])
                             .from("CONTACT")
                             .join("ORGANISATION", "CONTACT.ORGANISATION_ID = org.ORGANISATIONID", "org")
                             .leftJoin("PERSON", "PERSON.PERSONID = CONTACT.PERSON_ID")
@@ -131,6 +133,7 @@ if(idValues == false)
      
     for (var i = 0; i < visitFrequencyData.length; i++)
     {
+        var isPersonContact = visitFrequencyData[i][10] != ""
         var dueDate = "";
         var lastVisitDate = "";
         var today = datetime.date();
@@ -177,7 +180,7 @@ if(idValues == false)
             "", //USER_NEW.displayValue
             "", //USER_NEW.value
             ContactUtils.getFullTitleByContactId(visitFrequencyData[i][1], false), //CONTACT_PERSON_ID.displayValue
-            visitFrequencyData[i][1], //CONTACT_PERSON_ID.value
+            isPersonContact ? visitFrequencyData[i][1]: "", //CONTACT_PERSON_ID.value
             "", //RESPONSIBLE.displayValue
             "" //RESPONSIBLE.value
         ];
-- 
GitLab