From d2784e389f05f466be583d0032accda9b331f741 Mon Sep 17 00:00:00 2001
From: Johannes Hoermann <j.hoermann@adito.de>
Date: Thu, 31 Jan 2019 11:37:26 +0100
Subject: [PATCH] [Projekt: Entwicklung - Neon][TicketNr.: 1032636][Vertrieb -
 Angebot - Wann ist das Feld "Vertriebsprojekt" verpflichtend]

---
 entity/Offer_entity/Offer_entity.aod          |  3 +-
 .../relation_org_id/valueProcess.js           |  0
 .../salesproject_id/mandatoryProcess.js       |  9 +++++
 .../recordcontainers/db/onDBInsert.js         |  1 -
 .../entityfields/relationtype/valueProcess.js |  2 +-
 process/Relation_lib/process.js               | 38 +++++++++++++++++++
 6 files changed, 50 insertions(+), 3 deletions(-)
 create mode 100644 entity/Offer_entity/entityfields/relation_org_id/valueProcess.js
 create mode 100644 entity/Offer_entity/entityfields/salesproject_id/mandatoryProcess.js

diff --git a/entity/Offer_entity/Offer_entity.aod b/entity/Offer_entity/Offer_entity.aod
index 9cc12f7202..225ae53b10 100644
--- a/entity/Offer_entity/Offer_entity.aod
+++ b/entity/Offer_entity/Offer_entity.aod
@@ -73,7 +73,7 @@
       <name>SALESPROJECT_ID</name>
       <title>Salesproject</title>
       <linkedContext>Salesproject_context</linkedContext>
-      <mandatory v="true" />
+      <mandatoryProcess>%aditoprj%/entity/Offer_entity/entityfields/salesproject_id/mandatoryProcess.js</mandatoryProcess>
       <outgoingField>OfferSalesproject_dfo</outgoingField>
       <state>INVISIBLE</state>
       <stateProcess>%aditoprj%/entity/Offer_entity/entityfields/salesproject_id/stateProcess.js</stateProcess>
@@ -254,6 +254,7 @@
     <entityField>
       <name>RELATION_ORG_ID</name>
       <searchable v="false" />
+      <valueProcess>%aditoprj%/entity/Offer_entity/entityfields/relation_org_id/valueProcess.js</valueProcess>
     </entityField>
     <entityField>
       <name>RELATION_PERS_ID</name>
diff --git a/entity/Offer_entity/entityfields/relation_org_id/valueProcess.js b/entity/Offer_entity/entityfields/relation_org_id/valueProcess.js
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/entity/Offer_entity/entityfields/salesproject_id/mandatoryProcess.js b/entity/Offer_entity/entityfields/salesproject_id/mandatoryProcess.js
new file mode 100644
index 0000000000..55c7c33b4f
--- /dev/null
+++ b/entity/Offer_entity/entityfields/salesproject_id/mandatoryProcess.js
@@ -0,0 +1,9 @@
+import("system.vars");
+import("system.result");
+import("Relation_lib");
+
+var type = RelationUtils.getRelationType(vars.get("$field.RELATION_ID"), vars.get("$field.RELATION_PERS_ID"), vars.get("$field.RELATION_ORG_ID"));
+
+result.string(type != 2);
+
+
diff --git a/entity/Offeritem_entity/recordcontainers/db/onDBInsert.js b/entity/Offeritem_entity/recordcontainers/db/onDBInsert.js
index 33f35dd3c9..2b35ec3e2b 100644
--- a/entity/Offeritem_entity/recordcontainers/db/onDBInsert.js
+++ b/entity/Offeritem_entity/recordcontainers/db/onDBInsert.js
@@ -23,5 +23,4 @@ if(oid != "")
 
     db.updateData("OFFER", cols, null, vals, SqlCondition.equals("OFFER.OFFERID", oid, "1 = 2"));
     
-    neon.refresh();
 }
\ No newline at end of file
diff --git a/entity/Relation_entity/entityfields/relationtype/valueProcess.js b/entity/Relation_entity/entityfields/relationtype/valueProcess.js
index d7e96ebf13..fe76276e3d 100644
--- a/entity/Relation_entity/entityfields/relationtype/valueProcess.js
+++ b/entity/Relation_entity/entityfields/relationtype/valueProcess.js
@@ -2,4 +2,4 @@ import("system.result");
 import("system.vars");
 import("Relation_lib")
 
-result.object(RelationUtils.getRelationTypeByPersOrg(vars.get("$field.PERS_ID"), vars.get("$field.ORG_ID")));
\ No newline at end of file
+result.object(RelationUtils.getRelationType(vars.get("$field.RELATIONID"), vars.get("$field.PERS_ID"), vars.get("$field.ORG_ID")));
\ No newline at end of file
diff --git a/process/Relation_lib/process.js b/process/Relation_lib/process.js
index dc3610977d..e3dac3e936 100644
--- a/process/Relation_lib/process.js
+++ b/process/Relation_lib/process.js
@@ -1,3 +1,4 @@
+import("system.neon");
 import("system.vars");
 import("system.result");
 import("system.db");
@@ -12,6 +13,43 @@ import("Context_lib");
  */
 function RelationUtils() {}
 
+/**
+ * Get the type of relation. <br>
+ * In recordstate NEW or EDIT it loads the pers- / orgid from the db.<br>
+ * But in the other states it uses the values pPersId, pOrgId directly (for performance).<br>
+ * <br>
+ * It only checks if pPersId / pOrgId are not empty. <br>
+ * Based on which parameter is empty / not empty it return s the type of the relation. <br>
+ *  <br>
+ * !!It does not check if the pers / org ids really exist!! <br>
+ * !!And it does not check if really any relation with this pers / org ids exist!! <br>
+ *  <br>
+ *  <br>
+ * It is meant to be used by entitys, where you can load pers and org with the DataRecord. <br>
+ * This saves an extra select from RELATION. <br>
+ *  <br>
+ *  <br>
+ * @param {String} pRelationId
+ * @param {String} pPersId selected from the RELATION table
+ * @param {String} pOrgId selected from the RELATION table
+ *  <br>
+ * @return {Integer} <br>0 if both ids are empty <br>
+ *                      1 if organisation <br>
+ *                      2 if privat person <br>
+ *                      3 if person of an organisation <br>
+ */
+RelationUtils.getRelationType = function(pRelationId, pPersId, pOrgId)
+{
+    if (vars.get("$sys.recordstate") == neon.OPERATINGSTATE_NEW || vars.get("$sys.recordstate") == neon.OPERATINGSTATE_EDIT)
+    {
+        return RelationUtils.getRelationTypeByRelation(pRelationId);
+    }
+    else
+    {
+        return RelationUtils.getRelationTypeByPersOrg(pPersId, pOrgId);
+    }
+}
+
 /**
  * get the type of relation for a relationId <br>
  * If you already have persId and orgId from the RELATION table, use getRelationTypeByPersOrg() <br>
-- 
GitLab