From 3e25f85d12019ddf39fc867d3025f08434035945 Mon Sep 17 00:00:00 2001
From: "b.ulrich" <b.ulrich@adito.de>
Date: Wed, 1 Jul 2020 15:02:43 +0200
Subject: [PATCH] =?UTF-8?q?[Projekt:=20Entwicklung=20-=20Neon][TicketNr.:?=
 =?UTF-8?q?=201059167][Kontakt/Firma:=20Kontakt=20mit=20mehreren=20funktio?=
 =?UTF-8?q?nen=20wird=20komplett=20gel=C3=B6scht,=20wenn=20bei=20einer=20F?=
 =?UTF-8?q?unktion=20entfernt]?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 entity/Person_entity/grantDeleteProcess.js | 36 +++++++++++++++-------
 1 file changed, 25 insertions(+), 11 deletions(-)

diff --git a/entity/Person_entity/grantDeleteProcess.js b/entity/Person_entity/grantDeleteProcess.js
index 7b130d228f..2c8fe13800 100644
--- a/entity/Person_entity/grantDeleteProcess.js
+++ b/entity/Person_entity/grantDeleteProcess.js
@@ -1,20 +1,34 @@
+import("Sql_lib");
 import("Entity_lib");
 import("Employee_lib");
 import("Context_lib");
 import("system.vars");
 import("system.result");
 
-var contactId = vars.get("$field.CONTACTID");
-var currentContext = ContextUtils.getCurrentContextId();
+var canDelete = false;
+var personId = vars.get("$field.PERSONID");
+var personCount = newSelect("count(*)")
+                        .from("CONTACT")
+                        .join("PERSON", "PERSON.PERSONID = CONTACT.PERSON_ID")
+                        .where("PERSON.PERSONID", personId)
+                        .cell()
+//only delete when the contact doesnt have any functions, because that would currently lead to the person and all his functions being deleted,
+// since there is only one person contact behind all the contacts.
+//ToDo: come back to this and implement the solution for deleting a function (#1059167) once it'S decided how that's being implemented
+if(personCount == 1) 
+{
+    var contactId = vars.get("$field.CONTACTID");
+    var currentContext = ContextUtils.getCurrentContextId();
 
-var canDelete = new HasLinkedObjectTester()
-    .andNoEntityRows("Activity_entity", "LinkedObjects", {ObjectId_param : currentContext, RowId_param : contactId}) //Activities
-    .andNoEntityRows("Task_entity", "Tasks", {ObjectId_param : currentContext, RowId_param : contactId}) //Tasks
-    .andNoEntityRows("Document_entity", "Documents", {AssignmentTable_param : "CONTACT", AssignmentRowId_param : contactId}) //Documents
-    .and(function () {return !EmployeeUtils.isUser(contactId);})
-    .andNoEntityRows("Offer_entity", "ContactOffers", {ContactId_param : contactId}) //Offers
-    .andNoEntityRows("Contract_entity", "Contracts", {ContactId_param : contactId}) //Contracts
-    .andNoEntityRows("ObjectTree_entity", "TreeProvider", {ObjectIds_param : JSON.stringify([contactId, vars.get("$field.PERSON_ID")]), ObjectTypes_param : JSON.stringify([currentContext, "PrivatePerson"])})
-    .validate();
+    canDelete = new HasLinkedObjectTester()
+        .andNoEntityRows("Activity_entity", "LinkedObjects", {ObjectId_param : currentContext, RowId_param : contactId}) //Activities
+        .andNoEntityRows("Task_entity", "Tasks", {ObjectId_param : currentContext, RowId_param : contactId}) //Tasks
+        .andNoEntityRows("Document_entity", "Documents", {AssignmentTable_param : "CONTACT", AssignmentRowId_param : contactId}) //Documents
+        .and(function () {return !EmployeeUtils.isUser(contactId);})
+        .andNoEntityRows("Offer_entity", "ContactOffers", {ContactId_param : contactId}) //Offers
+        .andNoEntityRows("Contract_entity", "Contracts", {ContactId_param : contactId}) //Contracts
+        .andNoEntityRows("ObjectTree_entity", "TreeProvider", {ObjectIds_param : JSON.stringify([contactId, vars.get("$field.PERSON_ID")]), ObjectTypes_param : JSON.stringify([currentContext, "PrivatePerson"])})
+        .validate();
+}
 
 result.string(canDelete);
-- 
GitLab