core/issues/80 - Current Employer is not reset after relationship is updated
authorJitendra Purohit <jitendra@fuzion.co.nz>
Thu, 26 Apr 2018 12:21:27 +0000 (17:51 +0530)
committerJitendra Purohit <jitendra@fuzion.co.nz>
Fri, 11 May 2018 11:22:27 +0000 (16:52 +0530)
Additional fixes

Additional fixes

CRM/Contact/BAO/Relationship.php
CRM/Contact/Form/Relationship.php
tests/phpunit/api/v3/RelationshipTest.php

index 000987cea66f0bf8515c337d1e6cd41ac0003e59..7af0b6c01049dff61661e4301e14001b23710e1d 100644 (file)
@@ -300,7 +300,9 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship {
     if ($type == 6) {
       CRM_Contact_BAO_Household::updatePrimaryContact($params['contact_id_b'], $params['contact_id_a']);
     }
-
+    if (!empty($relationshipId) && self::isCurrentEmployerNeedingToBeCleared($params, $relationshipId, $type)) {
+      CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($params['contact_id_a']);
+    }
     $relationship = new CRM_Contact_BAO_Relationship();
     //@todo this code needs to be updated for the possibility that not all fields are set
     // by using $relationship->copyValues($params);
@@ -2218,4 +2220,38 @@ AND cc.sort_name LIKE '%$name%'";
     return $nameToLabels;
   }
 
+  /**
+   * Process the params from api, form and check if current
+   * employer should be set or unset.
+   *
+   * @param array $params
+   * @param int $relationshipId
+   * @param int|NULL $updatedRelTypeID
+   *
+   * @return bool
+   *   TRUE if current employer needs to be cleared.
+   */
+  public static function isCurrentEmployerNeedingToBeCleared($params, $relationshipId, $updatedRelTypeID = NULL) {
+    $existingTypeID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Relationship', $relationshipId, 'relationship_type_id');
+    $existingTypeName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', $existingTypeID, 'name_b_a');
+    $updatedRelTypeID = $updatedRelTypeID ? $updatedRelTypeID : $existingTypeID;
+
+    if ($existingTypeName !== 'Employer of') {
+      return FALSE;
+    }
+    //Clear employer if relationship is expired.
+    if (!empty($params['end_date']) && strtotime($params['end_date']) < time()) {
+      return TRUE;
+    }
+    //current employer checkbox is disabled on the form.
+    //inactive or relationship type(employer of) is updated.
+    if ((isset($params['is_current_employer']) && empty($params['is_current_employer']))
+      || ((isset($params['is_active']) && empty($params['is_active'])))
+      || $existingTypeID != $updatedRelTypeID) {
+      return TRUE;
+    }
+
+    return FALSE;
+  }
+
 }
index 897856150fb19c187c79604dc0f23183d6a4a530..cdc2898fded815b46fb43f294c4cde1274301bf9 100644 (file)
@@ -417,10 +417,14 @@ class CRM_Contact_Form_Relationship extends CRM_Core_Form {
     $this->setEmploymentRelationship($params, $relationshipIds);
 
     // Refresh contact tabs which might have been affected
-    $this->ajaxResponse['updateTabs'] = array(
-      '#tab_member' => CRM_Contact_BAO_Contact::getCountComponent('membership', $this->_contactId),
-      '#tab_contribute' => CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId),
+    $this->ajaxResponse = array(
+      'reloadBlocks' => array('#crm-contactinfo-content'),
+      'updateTabs' => array(
+        '#tab_member' => CRM_Contact_BAO_Contact::getCountComponent('membership', $this->_contactId),
+        '#tab_contribute' => CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId),
+      ),
     );
+
   }
 
   /**
@@ -546,10 +550,7 @@ class CRM_Contact_Form_Relationship extends CRM_Core_Form {
       throw new CRM_Core_Exception('Relationship create error ' . $e->getMessage());
     }
 
-    $this->clearCurrentEmployer($params);
-
     $this->setMessage(array('saved' => TRUE));
-
     return array($params, array($this->_relationshipId));
   }
 
@@ -648,38 +649,19 @@ class CRM_Contact_Form_Relationship extends CRM_Core_Form {
    * @param array $relationshipIds
    */
   private function setEmploymentRelationship($params, $relationshipIds) {
-    if (
-      !empty($params['is_current_employer']) &&
-      $this->_allRelationshipNames[$params['relationship_type_id']]["name_a_b"] == 'Employee of') {
-      $employerParams = array();
-      foreach ($relationshipIds as $id) {
+    $employerParams = array();
+    foreach ($relationshipIds as $id) {
+      if (!CRM_Contact_BAO_Relationship::isCurrentEmployerNeedingToBeCleared($params, $id)
+        //don't think this is required to check again.
+        && $this->_allRelationshipNames[$params['relationship_type_id']]["name_a_b"] == 'Employee of') {
         // Fixme this is dumb why do we have to look this up again?
         $rel = CRM_Contact_BAO_Relationship::getRelationshipByID($id);
         $employerParams[$rel->contact_id_a] = $rel->contact_id_b;
       }
+    }
+    if (!empty($employerParams)) {
       // @todo this belongs in the BAO.
       CRM_Contact_BAO_Contact_Utils::setCurrentEmployer($employerParams);
-      // Refresh contact summary if in ajax mode
-      $this->ajaxResponse['reloadBlocks'] = array('#crm-contactinfo-content');
-    }
-  }
-
-  /**
-   * Clears the current employer if the relationship type
-   * get changed, disabled or 'current employer' checkbox get unchecked.
-   *
-   * @param $params
-   */
-  private function clearCurrentEmployer($params) {
-    // @todo this belongs in the BAO.
-    if ($this->_isCurrentEmployer) {
-      $relChanged = $params['relationship_type_id'] != $this->_values['relationship_type_id'];
-      if (!$params['is_active'] || !$params['is_current_employer'] || $relChanged) {
-        CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($this->_values['contact_id_a']);
-
-        // Refresh contact summary if in ajax mode
-        $this->ajaxResponse['reloadBlocks'] = array('#crm-contactinfo-content');
-      }
     }
   }
 
index 6b9501bf90025808ab55bf4c8494dd4e96a31497..f1a24aa2c513ae652b44aae95276da122da398b5 100644 (file)
@@ -109,6 +109,46 @@ class api_v3_RelationshipTest extends CiviUnitTestCase {
     $this->callAPIFailure('relationship', 'create', array());
   }
 
+  /**
+   * Test Current Employer is correctly set.
+   */
+  public function testCurrentEmployerRelationship() {
+    $employerRelationshipID = $this->callAPISuccessGetValue('RelationshipType', array(
+      'return' => "id",
+      'name_b_a' => "Employer Of",
+    ));
+    $employerRelationship = $this->callAPISuccess('Relationship', 'create', array(
+      'contact_id_a' => $this->_cId_a,
+      'contact_id_b' => $this->_cId_b,
+      'relationship_type_id' => $employerRelationshipID,
+    ));
+    $params = array($this->_cId_a => $this->_cId_b);
+    CRM_Contact_BAO_Contact_Utils::setCurrentEmployer($params);
+
+    //Check if current employer is correctly set.
+    $employer = $this->callAPISuccessGetValue('Contact', array(
+      'return' => "current_employer",
+      'id' => $this->_cId_a,
+    ));
+    $organisation = $this->callAPISuccessGetValue('Contact', array(
+      'return' => "sort_name",
+      'id' => $this->_cId_b,
+    ));
+    $this->assertEquals($employer, $organisation);
+
+    //Update relationship type
+    $update = $this->callAPISuccess('Relationship', 'create', array(
+      'id' => $employerRelationship['id'],
+      'relationship_type_id' => $this->_relTypeID,
+    ));
+    $employeeContact = $this->callAPISuccessGetSingle('Contact', array(
+      'return' => array("current_employer"),
+      'id' => $this->_cId_a,
+    ));
+    //current employer should be removed.
+    $this->assertEmpty($employeeContact['current_employer']);
+  }
+
   /**
    * Check if required fields are not passed.
    */