CRM-16087 - Changes in relationship status are not invoking updates to membership...
authoratif-shaikh <shaikh388@gmail.com>
Tue, 31 Mar 2015 12:13:10 +0000 (17:43 +0530)
committeratif-shaikh <shaikh388@gmail.com>
Tue, 31 Mar 2015 12:13:10 +0000 (17:43 +0530)
https://issues.civicrm.org/jira/browse/CRM-16087

CRM/Contact/BAO/Contact.php
CRM/Contact/BAO/Relationship.php

index 4f409950f65a87bba4f2177709fbe51264cbfcc6..70c2dae2c2f4caf58ee620b52fd970d9c3ee0993 100644 (file)
@@ -815,17 +815,23 @@ WHERE     civicrm_contact.id = " . CRM_Utils_Type::escape($id, 'Integer');
     }
 
     $contactType = $contact->contact_type;
-
-    if ($restore) {
-      return self::contactTrashRestore($contact, TRUE);
-    }
-
     // currently we only clear employer cache.
-    // we are not deleting inherited membership if any.
+    // we are now deleting inherited membership if any.
     if ($contact->contact_type == 'Organization') {
+      $action = $restore ? CRM_Core_Action::ENABLE : CRM_Core_Action::DISABLE;
+      $relationshipDtls = CRM_Contact_BAO_Relationship::getRelationship($id);
+      if (!empty($relationshipDtls)) {
+        foreach ($relationshipDtls as $rId => $details) {
+          CRM_Contact_BAO_Relationship::disableEnableRelationship($rId, $action);
+        }
+      }
       CRM_Contact_BAO_Contact_Utils::clearAllEmployee($id);
     }
 
+    if ($restore) {
+      return self::contactTrashRestore($contact, TRUE);
+    }
+
     // start a new transaction
     $transaction = new CRM_Core_Transaction();
 
index d2b35abb6bb59976006062c7e5969933da65a68c..efa0dfd758c8e9227b798c4cff44d4459b48260e 100644 (file)
@@ -64,17 +64,10 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship {
         'contactTarget' => $relationship->contact_id_b,
         'contact' => $params['contact_id_a'],
       );
-      CRM_Contact_BAO_Relationship::relatedMemberships($params['contact_id_a'], $params, $ids, (empty($params['id']) ?
-        CRM_Core_Action::ADD : CRM_Core_Action::UPDATE));
-    }
 
-    // Alter related membership if the is_active param is changed.
-    if (!empty($params['id']) && array_key_exists('is_active', $params)) {
-      $action = CRM_Core_Action::DISABLE;
-      if (!empty($params['is_active'])) {
-        $action = CRM_Core_Action::ENABLE;
-      }
-      CRM_Contact_BAO_Relationship::disableEnableRelationship($params['id'], $action);
+      //CRM-16087 removed additional call to function relatedMemberships which is already called by disableEnableRelationship
+      //resulting in memership being created twice
+      self::disableEnableRelationship($params['id'], $action, $params, $ids);
     }
 
     self::addRecent($params, $relationship);
@@ -680,35 +673,45 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship {
    *
    * @param $action
    */
-  public static function disableEnableRelationship($id, $action) {
+  public static function disableEnableRelationship($id, $action, $params = array(), $ids = array(), $active = FALSE) {
     $relationship = self::clearCurrentEmployer($id, $action);
 
     if (CRM_Core_Permission::access('CiviMember')) {
       // create $params array which is required to delete memberships
       // of the related contacts.
-      $params = array(
-        'relationship_type_id' => "{$relationship->relationship_type_id}_a_b",
-        'contact_check' => array($relationship->contact_id_b => 1),
-      );
-
-      $ids = array();
+      if (empty($params)) {
+        $params = array(
+          'relationship_type_id' => "{$relationship->relationship_type_id}_a_b",
+          'contact_check' => array($relationship->contact_id_b => 1),
+        );
+      }
+      else {
+        if (array_key_exists('is_active', $params) && empty($params['is_active'])) {
+          $action = CRM_Core_Action::DISABLE;
+        }
+        else {
+          $action = CRM_Core_Action::ENABLE;
+          $active = TRUE;
+        }
+      }
+      $contact_id_a = empty($params['contact_id_a']) ? $relationship->contact_id_a : $params['contact_id_a'];
       // calling relatedMemberships to delete/add the memberships of
       // related contacts.
       if ($action & CRM_Core_Action::DISABLE) {
-        CRM_Contact_BAO_Relationship::relatedMemberships($relationship->contact_id_a,
+        CRM_Contact_BAO_Relationship::relatedMemberships($contact_id_a,
           $params,
           $ids,
           CRM_Core_Action::DELETE,
-          FALSE
+          $active
         );
       }
       elseif ($action & CRM_Core_Action::ENABLE) {
         $ids['contact'] = $relationship->contact_id_a;
-        CRM_Contact_BAO_Relationship::relatedMemberships($relationship->contact_id_a,
+        CRM_Contact_BAO_Relationship::relatedMemberships($contact_id_a,
           $params,
           $ids,
-          CRM_Core_Action::ADD,
-          FALSE
+          empty($params['id']) ? CRM_Core_Action::ADD : CRM_Core_Action::UPDATE,
+          $active
         );
       }
     }
@@ -1488,8 +1491,8 @@ SELECT relationship_type_id, relationship_direction
             $relDirection = $dao->relationship_direction;
           }
           $relTypeIds = explode(CRM_Core_DAO::VALUE_SEPARATOR, $relTypeId);
-          if (in_array($values[$cid]['relationshipTypeId'], $relTypeIds)) {
-            CRM_Member_BAO_Membership::deleteRelatedMemberships($membershipId, $mainRelatedContactId);
+          if (in_array($values[$cid]['relationshipTypeId'], $relTypeIds) && !empty($membershipValues['owner_membership_id'])) {
+            CRM_Member_BAO_Membership::deleteRelatedMemberships($membershipValues['owner_membership_id'], $membershipValues['membership_contact_id']);
           }
           continue;
         }
@@ -1575,9 +1578,17 @@ SELECT count(*)
           // membership=>relationship then we need to
           // delete the membership record created for
           // previous relationship.
-
-          if (self::isDeleteRelatedMembership($relTypeIds, $contactId, $mainRelatedContactId, $relTypeId, CRM_Utils_Array::value('relationship_ids', $params))) {
-            CRM_Member_BAO_Membership::deleteRelatedMemberships($membershipId, $mainRelatedContactId);
+          // CRM-16087 we need to pass ownerMembershipId to deleteRelatedMemberships function
+          if (empty($params['relationship_ids']) && !empty($params['id'])) {
+            $relIds = array($params['id']);
+          }
+          else {
+            $relIds = CRM_Utils_Array::value('relationship_ids', $params);
+          }
+          if (self::isDeleteRelatedMembership($relTypeIds, $contactId, $mainRelatedContactId, $relTypeId,
+          $relIds
+          ) && !empty($membershipValues['owner_membership_id'])) {
+            CRM_Member_BAO_Membership::deleteRelatedMemberships($membershipValues['owner_membership_id'], $membershipValues['membership_contact_id']);
           }
         }
       }