From 937278a8ff162785a6ea250202bad700b296fb2a Mon Sep 17 00:00:00 2001
From: "j.goderbauer" <j.goderbauer@adito.de>
Date: Tue, 26 Mar 2019 09:50:01 +0100
Subject: [PATCH] Contact person: new Contact: exclude combinations that
 already exist

---
 entity/Contact_entity/Contact_entity.aod      |  6 ++-
 .../valueProcess.js                           |  5 +++
 .../Organisation_entity.aod                   | 42 ++++++++++++++++---
 .../recordcontainers/db/conditionProcess.js   |  6 ++-
 4 files changed, 51 insertions(+), 8 deletions(-)
 create mode 100644 entity/Contact_entity/entityfields/organisations/children/excludeorganisationsbypersonid/valueProcess.js

diff --git a/entity/Contact_entity/Contact_entity.aod b/entity/Contact_entity/Contact_entity.aod
index ecdd154429..4ce8a38fd6 100644
--- a/entity/Contact_entity/Contact_entity.aod
+++ b/entity/Contact_entity/Contact_entity.aod
@@ -48,13 +48,17 @@
       <dependency>
         <name>dependency</name>
         <entityName>Organisation_entity</entityName>
-        <fieldName>Organisations</fieldName>
+        <fieldName>WithPersonIdFilter</fieldName>
       </dependency>
       <children>
         <entityParameter>
           <name>WithPrivate_param</name>
           <valueProcess>%aditoprj%/entity/Contact_entity/entityfields/organisations/children/withprivate_param/valueProcess.js</valueProcess>
         </entityParameter>
+        <entityParameter>
+          <name>ExcludeOrganisationsByPersonId</name>
+          <valueProcess>%aditoprj%/entity/Contact_entity/entityfields/organisations/children/excludeorganisationsbypersonid/valueProcess.js</valueProcess>
+        </entityParameter>
       </children>
     </entityConsumer>
     <entityParameter>
diff --git a/entity/Contact_entity/entityfields/organisations/children/excludeorganisationsbypersonid/valueProcess.js b/entity/Contact_entity/entityfields/organisations/children/excludeorganisationsbypersonid/valueProcess.js
new file mode 100644
index 0000000000..b273dc66b2
--- /dev/null
+++ b/entity/Contact_entity/entityfields/organisations/children/excludeorganisationsbypersonid/valueProcess.js
@@ -0,0 +1,5 @@
+import("system.vars");
+import("system.result");
+
+var personId = vars.get("$field.PERSON_ID");
+result.string(personId);
\ No newline at end of file
diff --git a/entity/Organisation_entity/Organisation_entity.aod b/entity/Organisation_entity/Organisation_entity.aod
index ea7df11cc5..f16cf4d876 100644
--- a/entity/Organisation_entity/Organisation_entity.aod
+++ b/entity/Organisation_entity/Organisation_entity.aod
@@ -140,12 +140,6 @@
           <fieldName>Organisations</fieldName>
           <isConsumer v="false" />
         </entityDependency>
-        <entityDependency>
-          <name>7aa5f9b4-6986-4593-a6d8-c4fb03da9c68</name>
-          <entityName>Contact_entity</entityName>
-          <fieldName>Organisations</fieldName>
-          <isConsumer v="false" />
-        </entityDependency>
       </dependencies>
       <children>
         <entityParameter>
@@ -156,6 +150,10 @@
           <name>WithPrivate_param</name>
           <expose v="true" />
         </entityParameter>
+        <entityParameter>
+          <name>ExcludeOrganisationsByPersonId</name>
+          <expose v="false" />
+        </entityParameter>
       </children>
     </entityProvider>
     <entityConsumer>
@@ -633,6 +631,38 @@
         </entityParameter>
       </children>
     </entityConsumer>
+    <entityParameter>
+      <name>ExcludeOrganisationsByPersonId</name>
+      <expose v="true" />
+      <description>PARAMETER</description>
+    </entityParameter>
+    <entityProvider>
+      <name>WithPersonIdFilter</name>
+      <fieldType>DEPENDENCY_IN</fieldType>
+      <dependencies>
+        <entityDependency>
+          <name>5a456b04-f0ca-4a45-9c1f-bdfdf074434a</name>
+          <entityName>Contact_entity</entityName>
+          <fieldName>Organisations</fieldName>
+          <isConsumer v="false" />
+        </entityDependency>
+      </dependencies>
+      <children>
+        <entityParameter>
+          <name>ContactId_param</name>
+          <expose v="false" />
+        </entityParameter>
+        <entityParameter>
+          <name>ExcludeOrganisationsByPersonId</name>
+          <title></title>
+          <expose v="true" />
+        </entityParameter>
+        <entityParameter>
+          <name>WithPrivate_param</name>
+          <expose v="true" />
+        </entityParameter>
+      </children>
+    </entityProvider>
   </entityFields>
   <recordContainers>
     <dbRecordContainer>
diff --git a/entity/Organisation_entity/recordcontainers/db/conditionProcess.js b/entity/Organisation_entity/recordcontainers/db/conditionProcess.js
index d8bc46d7f9..d8f6c2d5a8 100644
--- a/entity/Organisation_entity/recordcontainers/db/conditionProcess.js
+++ b/entity/Organisation_entity/recordcontainers/db/conditionProcess.js
@@ -10,7 +10,11 @@ var cond = SqlCondition.begin()
 if (vars.getString("$param.WithPrivate_param") != "true")
     cond.andPrepare("ORGANISATION.ORGANISATIONID", "0", "# <> ?");
 
-//TODO: exclude already used (or validation of already used combinations)
+var excludeOrgsWithPersonId = vars.get("$param.ExcludeOrganisationsByPersonId")
+if (excludeOrgsWithPersonId)
+    cond.andAttachPrepared(SqlCondition.begin()
+                                       .andPrepare("CONTACT.PERSON_ID", excludeOrgsWithPersonId)
+                                       .buildSql("ORGANISATION.ORGANISATIONID not in (select CONTACT.ORGANISATION_ID from CONTACT", null, ")"));
 
 //TODO: use a preparedCondition when available #1030812 #1034026
 result.string(db.translateCondition(cond.build("1 = 1")));
\ No newline at end of file
-- 
GitLab