From df0c42ccc6e41915d308201838678cd01f76dd8c Mon Sep 17 00:00:00 2001 From: Jitendra Purohit Date: Thu, 26 Apr 2018 17:51:27 +0530 Subject: [PATCH] core/issues/80 - Current Employer is not reset after relationship is updated Additional fixes Additional fixes --- CRM/Contact/BAO/Relationship.php | 38 ++++++++++++++++++- CRM/Contact/Form/Relationship.php | 46 +++++++---------------- tests/phpunit/api/v3/RelationshipTest.php | 40 ++++++++++++++++++++ 3 files changed, 91 insertions(+), 33 deletions(-) diff --git a/CRM/Contact/BAO/Relationship.php b/CRM/Contact/BAO/Relationship.php index 000987cea6..7af0b6c010 100644 --- a/CRM/Contact/BAO/Relationship.php +++ b/CRM/Contact/BAO/Relationship.php @@ -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; + } + } diff --git a/CRM/Contact/Form/Relationship.php b/CRM/Contact/Form/Relationship.php index 897856150f..cdc2898fde 100644 --- a/CRM/Contact/Form/Relationship.php +++ b/CRM/Contact/Form/Relationship.php @@ -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'); - } } } diff --git a/tests/phpunit/api/v3/RelationshipTest.php b/tests/phpunit/api/v3/RelationshipTest.php index 6b9501bf90..f1a24aa2c5 100644 --- a/tests/phpunit/api/v3/RelationshipTest.php +++ b/tests/phpunit/api/v3/RelationshipTest.php @@ -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. */ -- 2.25.1