Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core/issues/80 - Current Employer is not reset after relationship is … #12032

Merged
merged 1 commit into from
May 14, 2018

Conversation

jitendrapurohit
Copy link
Contributor

@jitendrapurohit jitendrapurohit commented Apr 26, 2018

…updated

Overview

Current Employer is not reset after relationship is updated.

Before

On editing a relationship type from Employer of to some other value, current employer value is still shown for the contact.

After

Current Employer is deleted when relationship type is updated.

Technical Details

Moved the logic to BAO layer as commented on CRM_Contact_Form_Relationship-> clearCurrentEmployer function.

Comments

Added unit test.


Gitlab issue - https://lab.civicrm.org/dev/core/issues/80

// @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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the $params['is_current_employer'] functionality does seem to be form-relevant - ie there could be 2 employers & one (or possibly neither?) could be form-selected - I do like the move to bao for rel type change tho

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested on assigning relationship through search results and we don't offer current employer checkbox on that form. I found another issue(before this PR) which failed to remove current employer when the checkbox is unchecked while editing the relationship. It has been fixed in the changes made here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jitendrapurohit just spotted one more thing - looks like the original code would also clear current employer if toggled to inactive (presumably date changes could trigger that too but I think that could stay out of scope since it was not part of the previous behaviour)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this brings another related issue on dmaster which fails to reset current employer after is_active checkbox is unchecked and the form is saved. Added fix for the same to this PR.

if (!empty($relationshipId)) {
$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');
if ($existingTypeName == 'Employer of' && (empty($params['is_active']) || empty($params['is_current_employer']) || $existingTypeID != $type)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs to be

 if (empty($params['is_current_employer']) && $existingTypeName == 'Employer of' && (empty($params['is_active'])  || $existingTypeID != $type))

Or else it gets hit when you do an api update. 'is_current_employer' is basically an override that can be passed in & which will skip that clear behaviour.

There is a weirdness with the form that I think you could assign ANY relationship with current_employer in the form but then it might be wiped by an api update of some sort - but I don't know how valid that path is - seems pretty obscure really

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the above format - it fails on the form layer and doesn't remove employer_id when is_current_employer is unchecked.

The change is now modified to look for 'is_current_employer' variable on the form itself instead of BAO and clear the employer if it is not enabled. Tested various scenarios and looks fine to me.

@@ -304,7 +304,7 @@ public static function add(&$params, $ids = array(), $contactId = NULL) {
if (!empty($relationshipId)) {
$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');
if (empty($params['is_current_employer']) && $existingTypeName == 'Employer of' && (empty($params['is_active']) || $existingTypeID != $type)) {
if ($existingTypeName == 'Employer of' && (empty($params['is_active']) || $existingTypeID != $type)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we still need to have !$params['is_current_employer'] here to respect any flag from the form?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the PR which should respect both API and form params. A new function is added which aims to decide if we need to set the current employer for the relationship or not. Tagging it as wip until I do a manual testing for some good number of scenarios. As a quick testing on the contact summary form, it worked fine for me.

@jitendrapurohit jitendrapurohit force-pushed the core-80 branch 2 times, most recently from b2c60b0 to 7aedab9 Compare May 3, 2018 13:21
@jitendrapurohit jitendrapurohit changed the title core/issues/80 - Current Employer is not reset after relationship is … wip - core/issues/80 - Current Employer is not reset after relationship is … May 3, 2018
@jitendrapurohit jitendrapurohit changed the title wip - core/issues/80 - Current Employer is not reset after relationship is … core/issues/80 - Current Employer is not reset after relationship is … May 10, 2018
@jitendrapurohit
Copy link
Contributor Author

jitendrapurohit commented May 10, 2018

Done initial testing from my side and looks good on API as well as on form layer. removing wip from the title.


$isExpired = FALSE;
if (!empty($params['end_date']) && strtotime($params['end_date']) < time()) {
$isExpired = TRUE;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stylistically it might be nicer to return FALSE here

* @return bool
* TRUE if current employer needs to be enabled.
*/
public static function isCurrentEmployerSetInParams($params, $relationshipId, $updatedRelTypeID = NULL) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like the function name is 'isCurrentEmployerNeedingToBeCleared'

@eileenmcnaughton
Copy link
Contributor

@jitendrapurohit I took a look at this & tested it & it seems good. I had some readability concerns - ie. the function name seems wrong. Also, I think you could make that IF more readable if you used early returns instead.

ie

if ($existingTypeName !==  'Employer of') {
  return FALSE;
}

if (!empty($params['is_current_employer']))) {
  return FALSE;
}

I guess that doesn't quite work with your efforts to share with the form function? But I feel the readability is a bit tricky.

Also I note that calling

self::clearCurrentEmployer($relationshipId, CRM_Core_Action::DELETE);

really just does some unnecessary work & then wraps

CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($relationship->contact_id_a);

@jitendrapurohit
Copy link
Contributor Author

jitendrapurohit commented May 11, 2018

Updated the PR as per above comments. Seems returning early for !empty($params['is_current_employer']) would not unset the current employer if checkbox is enabled but the relationship is expired. I've tested the latest update for relationship create/update and it seems to be working fine 👍

@eileenmcnaughton
Copy link
Contributor

OK - I think this is good to merge - maybe squash the commits first?

@jitendrapurohit
Copy link
Contributor Author

yep - done now.

@colemanw
Copy link
Member

@civicrm-builder retest this please

@eileenmcnaughton
Copy link
Contributor

test this please

@colemanw colemanw merged commit 17e1efd into civicrm:master May 14, 2018
@jitendrapurohit jitendrapurohit deleted the core-80 branch May 15, 2018 03:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants