CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviMember (Moderator: Deepak Srivastava) »
  • Duplicate Inherited memberships
Pages: [1]

Author Topic: Duplicate Inherited memberships  (Read 2655 times)

jackrabbithanna

  • I post occasionally
  • **
  • Posts: 61
  • Karma: 3
  • Quick like bunny
    • http://www.skvare.com
  • CiviCRM version: 4.3.5
  • CMS version: Drupal 7.23
  • MySQL version: 5.1
  • PHP version: 5.3.3
Duplicate Inherited memberships
August 28, 2013, 08:16:02 am
I have a strange situation. 

I've setup a membership type that organizations can purchase/have set up that is set to create inherited memberships based on the Employee/Employer Relationship type. 

When I add new individual contacts and set their employer to the organization with the group membership, the individual will receive 2 memberships (one dupe).  The individual has only the one relationship and this is verified in the DB. 

no matter whether the relationship is created manually on the relationship tab or by setting the Employer Field.

Also it makes no difference whether the membership was created via the group membership contrib form or via the backend admin.

Makes no difference the status of the membership.

What does seem to make a difference is whether contacts which already have a relationship exist or not when the memberships is created.  From my testing the situation only happens for contacts that are added and related AFTER the organization has the membership.

Can anyone think of why a contact would inherit 2 memberships instead of one when there is only one Relationship?

Civi 4.3.3
« Last Edit: August 28, 2013, 08:19:22 am by jackrabbithanna »

jackrabbithanna

  • I post occasionally
  • **
  • Posts: 61
  • Karma: 3
  • Quick like bunny
    • http://www.skvare.com
  • CiviCRM version: 4.3.5
  • CMS version: Drupal 7.23
  • MySQL version: 5.1
  • PHP version: 5.3.3
Re: Duplicate Inherited memberships
August 28, 2013, 09:19:13 am
Caused by this post hook I had:
But why?  A duplicate relationship is NOT created.....

function hook_civicrm_post( $op, $objectName, $objectId, &$objectRef  ){


if($op=='create'){
if ($objectName=='Relationship'){

$params = array(
  'version' => 3,
  'sequential' => 1,
  'id' => $objectId,
  'is_permission_a_b' => 1,
);
$result = civicrm_api('Relationship', 'create', $params);

}
}

}

jackrabbithanna

  • I post occasionally
  • **
  • Posts: 61
  • Karma: 3
  • Quick like bunny
    • http://www.skvare.com
  • CiviCRM version: 4.3.5
  • CMS version: Drupal 7.23
  • MySQL version: 5.1
  • PHP version: 5.3.3
Re: Duplicate Inherited memberships
August 28, 2013, 09:57:27 am
Updated my hook to be more proper, but the condition still exists.   Some how the api 'create' call is actually generating a membership even though it is updateing (including the 'id' in the $params)

function hook_civicrm_post( $op, $objectName, $objectId, &$objectRef  ){

if($op=='create'){
if ($objectName=='Relationship'){


$params = array(
  'version' => 3,
  'sequential' => 1,
  'id' => $objectId,
   'contact_id_a' => $objectRef->contact_id_a,
   'contact_id_b' => $objectRef->contact_id_b,
     'is_permission_a_b' => 1,
);
$result = civicrm_api('Relationship', 'create', $params);

}
}

}

jackrabbithanna

  • I post occasionally
  • **
  • Posts: 61
  • Karma: 3
  • Quick like bunny
    • http://www.skvare.com
  • CiviCRM version: 4.3.5
  • CMS version: Drupal 7.23
  • MySQL version: 5.1
  • PHP version: 5.3.3
Re: Duplicate Inherited memberships
August 28, 2013, 12:24:23 pm
This bug still occurs even when using the obsoleted 'update' parameter to the api call as below:

function awccivicrm_civicrm_post( $op, $objectName, $objectId, &$objectRef  ){



if($op=='create'){
if ($objectName=='Relationship'){

$params = array(
  'version' => 3,
  'sequential' => 1,
  'id' => $objectId,
   'contact_id_a' => $objectRef->contact_id_a,
   'contact_id_b' => $objectRef->contact_id_b,
     'is_permission_a_b' => 1,
);
$result = civicrm_api('Relationship', 'update', $params);

}
}

}

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Duplicate Inherited memberships
August 28, 2013, 01:56:07 pm
The function that creates the inherited relationship has been causing problems for a while. Put simply the function should check whether it is required to create an inherited relationship & be called from the BAO.

At the moment the form layer does it's own calculations as to whether to create one & calls the function conditionally - making the behaviour unpredicatable from the API (& meaning it has to be replicated in several places)
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

jackrabbithanna

  • I post occasionally
  • **
  • Posts: 61
  • Karma: 3
  • Quick like bunny
    • http://www.skvare.com
  • CiviCRM version: 4.3.5
  • CMS version: Drupal 7.23
  • MySQL version: 5.1
  • PHP version: 5.3.3
Re: Duplicate Inherited memberships
August 29, 2013, 07:58:57 am
I am looking into the problem....Dlobo has told me to start looking at the code for bao for relationship create code, that the inherited membership creation is triggered there....maybe look for the check or create a check to see if the individual already has a membership based on that relationship.  I'm going to check out 4.4alpha3 to confirm the issue exists there, and try to fix it there.

when you say "(& menaing it has to be replicated in several places)"

Are you meaning that there are multiple places that this may need to be fixed?

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Duplicate Inherited memberships
August 29, 2013, 08:58:44 am

i was mistaken on irc, primarily because the code in the api has changed between 4.3 and 4.4 (eileen might have an explanation for this)

a. in 4.3, the api does the membership stuff

b. in 4.4, the above code has been removed

i suspect eileen can guide u a bit better on next steps with regard to this. maybe refactor the form code a bit and move it into the BAO?

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

jackrabbithanna

  • I post occasionally
  • **
  • Posts: 61
  • Karma: 3
  • Quick like bunny
    • http://www.skvare.com
  • CiviCRM version: 4.3.5
  • CMS version: Drupal 7.23
  • MySQL version: 5.1
  • PHP version: 5.3.3
Re: Duplicate Inherited memberships
August 29, 2013, 09:57:14 am
I was just fixing to post that I noticed significant differences between 4.3 and 4.4 with the following code:

function mymodule_civicrm_post( $op, $objectName, $objectId, &$objectRef  ){


dsm($objectRef);

if($op=='create'){
if ($objectName=='Relationship'){

$params = array(
  'version' => 3,
  'sequential' => 1,
  'id' => $objectId,
   'contact_id_a' => $objectRef->contact_id_a,
   'contact_id_b' => $objectRef->contact_id_b,
     'is_permission_a_b' => 1,
);
$result = civicrm_api('Relationship', 'create', $params);
dsm($result);
}
}

}

So same code used with different civi versions gives you this ordering of dsm() output:

in 4.3 I get output from the dsm() calls in this order:
output for object CRM_Contact_BAO_Relationship
output for object CRM_Contact_BAO_Relationship
output for object  CRM_Member_BAO_Membership
output for object CRM_Activity_DAO_Activity
output for $result no errors, actually updates the relationships is_permission_a_b flag in the end
output for object CRM_Member_BAO_Membership
output for object CRM_Activity_DAO_Activity

using 4.4 alpha3
The code in the end doesn't update the is_permission_a_b field for the relationship record at all

I get output from dsm() calls in this order:
output for object CRM_Contact_BAO_Relationship
then another output for object CRM_Contact_BAO_Relationship
then output for $result with an error, with message "Relationship already exists"
then output for $result with no errors, and the output shows the is_permission_a_b set to 1 (but after all is done, the relationship record does not have the is_permission_a_b flag set to 1)
then output for CRM_Member_BAO_Membership
then output for  CRM_Activity_DAO_Activity



Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Duplicate Inherited memberships
August 29, 2013, 01:02:57 pm
Hmm - looks like   CRM_Contact_BAO_Relationship::relatedMemberships($params['contact_id_a'], $values, $ids, $action);
 was removed from the api & put in the bao then removed from the BAO because the form was calling it as well

The right fix is for that function to only create the memberships if required & be called from the BAO (& preferably released from the forms - but at least if the function is clever enough to not create them twice it won't matter.

However, if you just want to jam the function back into the API & get the tests to pass it may be quicker (& the technical debt will be kicked down the line).

As an aside the Api-team support for the API is technically that tested behaviour is supported, other behaviour is not. I guess the membership create behaviour must not be tested / supported.
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

jackrabbithanna

  • I post occasionally
  • **
  • Posts: 61
  • Karma: 3
  • Quick like bunny
    • http://www.skvare.com
  • CiviCRM version: 4.3.5
  • CMS version: Drupal 7.23
  • MySQL version: 5.1
  • PHP version: 5.3.3
Re: Duplicate Inherited memberships
August 30, 2013, 09:47:19 am
Which version of Civi are we talking now, 4.4 alpha3 ?

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Duplicate Inherited memberships
August 30, 2013, 02:52:49 pm
yes  ;D
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviMember (Moderator: Deepak Srivastava) »
  • Duplicate Inherited memberships

This forum was archived on 2017-11-26.