Skip to content

Commit

Permalink
Merge pull request #12032 from jitendrapurohit/core-80
Browse files Browse the repository at this point in the history
core/issues/80 - Current Employer is not reset after relationship is …
  • Loading branch information
colemanw authored May 14, 2018
2 parents b5b33b7 + df0c42c commit 17e1efd
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 33 deletions.
38 changes: 37 additions & 1 deletion CRM/Contact/BAO/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

}
46 changes: 14 additions & 32 deletions CRM/Contact/Form/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
),
);

}

/**
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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');
}
}
}

Expand Down
40 changes: 40 additions & 0 deletions tests/phpunit/api/v3/RelationshipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down

0 comments on commit 17e1efd

Please sign in to comment.