diff --git a/CRM/Contact/BAO/Relationship.php b/CRM/Contact/BAO/Relationship.php index 000987cea66f..7af0b6c01049 100644 --- a/CRM/Contact/BAO/Relationship.php +++ b/CRM/Contact/BAO/Relationship.php @@ -300,7 +300,9 @@ public static function add(&$params, $ids = array(), $contactId = NULL) { 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 @@ public static function buildRelationshipTypeOptions($params = array()) { 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; + } + } diff --git a/CRM/Contact/Form/Relationship.php b/CRM/Contact/Form/Relationship.php index 897856150fb1..cdc2898fded8 100644 --- a/CRM/Contact/Form/Relationship.php +++ b/CRM/Contact/Form/Relationship.php @@ -417,10 +417,14 @@ public function postProcess() { $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 @@ private function updateAction($params) { 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 @@ private function saveRelationshipNotes($relationshipIds, $note) { * @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'); - } } } diff --git a/tests/phpunit/api/v3/RelationshipTest.php b/tests/phpunit/api/v3/RelationshipTest.php index bd75c42112fc..b56f5c7c28d5 100644 --- a/tests/phpunit/api/v3/RelationshipTest.php +++ b/tests/phpunit/api/v3/RelationshipTest.php @@ -109,6 +109,46 @@ public function testRelationshipCreateEmpty() { $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. */