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) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • API Membership request
Pages: [1]

Author Topic: API Membership request  (Read 4191 times)

mclyde

  • I post frequently
  • ***
  • Posts: 171
  • Karma: 3
    • International Society for Bayesian Analysis
  • CiviCRM version: 4.2.7
  • CMS version: Drupal 6.x
  • MySQL version: 5.136
  • PHP version: 5.2.16
API Membership request
May 20, 2011, 11:55:29 am
the Membership API function can create a new membership or if you pass in a membership ID it will update an existing membership.
That works for a straight renewal but you have to calculate the new dates to pass in.   

Would it be possible to borrow more from the renewMembership code in CRM/Member/BAO/Membership.php to let this function (or perhaps a new function) handle renewals with upsell?  if params includes  membershipTypeID update the appropriate membership  (if in the same organization) or create a new membership  if none exist in the organization?

I would like to call this from a drupal module (uc_civicrm_products). the current renewMembership function has the contribution page form ogject as a required argument, which is not necessary from my usage.  (I have verified that removing the references to the form object the rest of teh code will create memberships)  I've been recoding using the API calls, but find that I am reinventing the code that is in the renew membership function already.  As one would like the extension to Drupal to have the same functionality as the civcrm methods,  having a public version of that would be really usefull!

Thanks!
Merlise
Merlise Clyde

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: API Membership request
May 22, 2011, 01:16:28 pm
Hi,

So api.membership.create already
- can work with membership type+contact id +....
- can work with id (to renew, but you have to provide the date)

You are suggesting to add
- id without date (and it automatically calculate the start/end date)
- id with membership type, consider it as an upsell
Not sure what to do with the date, does it start and the end of the current membership ?

Make sense to me, but haven't used the new upselling feature, so not sure how that's done. Eileen, you are using it already ?

Could you code it and create an issue with a patch+ test code ?

That's in api/v3/Membership.php and tests/phpunit/api/v3/MembershipTest.php

tip: use civicrm/ajax/doc to test (easier than to run unit tests) and join us on irc.

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: API Membership request
May 22, 2011, 03:29:20 pm
tip: use civicrm/ajax/doc to test (easier than to run unit tests)

Nah - write a test to test because then you have a working test @ the end + you wind up writing tests for all the odd scenarios you hit along the way. It's more robust!

NB - there's another thread on the forum (will have to search) where we decide the syntax for options & things but I think it would be

$params['option.member_upsell'] = 1;

if you want to create a specific optional behaviour
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

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: API Membership request
May 22, 2011, 08:24:02 pm

A few comments:

1. I took a quick look at the function and it depends a wee bit too much on the $form object the way it stands now. Seperating the $form object from it will require some work unfortunately. In general, I dont think the BAO functions should be using the $form object directly, so doing the seperation will be a good thing

2. I'm with eileen and completely agree that tests are the better way and repeatable etc. Another way of approaching it is using "test driven methodology". So if you are willing to write a comprehensive set of unit tests to test the "new" function without the "$form" object, i'd be willing to take a closer look at it and try to separate the $form object from the core functionality. That said I am quite slammed right now with a few too many things happening, and will take me a few weeks to get to this

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

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: API Membership request
May 22, 2011, 09:06:13 pm
Hi Lobo,

I seem to keep hitting this problem of code being a bit too integrated with the forms rather than being standalone in the BAO (I think payment processor :: create was an example I hit recently). Is there a policy to try to move functionality/ put new functionality into BAO functions instead of in the forms or am I just being hopeful.
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

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: API Membership request
May 22, 2011, 11:59:15 pm
Hi,

I think the general policy is "patch welcome" ;)

As for the test, now that it doesn't reset the db every test, and that you have the option not to reset the db at all, the time of running a test suite is not in hours anymore but min. Nearly TDD ready indeed

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

mclyde

  • I post frequently
  • ***
  • Posts: 171
  • Karma: 3
    • International Society for Bayesian Analysis
  • CiviCRM version: 4.2.7
  • CMS version: Drupal 6.x
  • MySQL version: 5.136
  • PHP version: 5.2.16
Re: API Membership request
May 23, 2011, 07:00:23 am
here is the part of  how I coded  the create/renew/upgrade in the uc_civicrm_products module  (the input will be contact_id, membership_type_id :  (Note: the current BAO function doesn't restrict upsell to within an organization yet so results will be different)

(apologies in advance for coding -- this is my first foray into php programming! so suggestions are welcome)


Code: [Select]

case 'civimember':
   require_once 'CRM/Core/BAO/CustomField.php';
   require_once 'CRM/Member/BAO/Membership.php';
    require_once 'CRM/Member/BAO/MembershipType.php';
    require_once 'api/v2/Membership.php';
    require_once 'api/v2/Contact.php';
    require_once 'CRM/Utils/Hook.php';

 // purchased membershiptype
   $membershipTypeID = $data['membership_type_id'];

  // get details of organization for new membership.

   $memtyp_id['id'] = $membershipTypeID;
   $newMembershipDetails = & civicrm_membership_types_get($memtyp_id);
   $new_org =  $newMembershipDetails[$membershipTypeID]['member_of_contact_id'];
   $org_params = array('contact_id' => $new_org);
   $org_contact = & civicrm_contact_get($org_params);
   $organization_name = $org_contact[$new_org]['display_name'];
 
  // now get contact's memberships
  $params = array('contact_id' => $contact_id,);
  $existMembership = &  civicrm_membership_contact_get($params);
  $existMembership = $existMembership[$contact_id];

// remove  pending and cancelled memberships  do not update those
   foreach($existMembership as $key => $value) {
     if (in_array('Pending',$value) ||  in_array('Cancelled', $value)) {
         unset($existMembership[$key]); }
}   

//  sort by end_date to find most recent first  desc and then membership type asc
   $sortkeys = array();
   foreach($existMembership as $k=>$v) {
       $sortkey['membership_type_id'][$k] = $v['membership_type_id'];
       $sortkey['end_date'][$k] = $v['end_date'];
}
   array_multisort($sortkey['end_date'], SORT_DESC, $sortkey['membership_type_id'], SORT_ASC, $existMembership);

//  for each membership_id find memberhip type's organization
//  id in membdata is membership_id; civicrm_membership_typeS_ gets
//    needs the id of membership types

   $membership_to_update = null;
   $renewal = FALSE;
    foreach ( $existMembership as $key => $memdata) {
     $memtyp_id = array('id' => $memdata['membership_type_id']);
     $temp = & civicrm_membership_types_get($memtyp_id);
     if ($new_org ==
$temp[$memdata['membership_type_id']]['member_of_contact_id']) {
     $membership_to_update = $memdata['id'];
     $renewal = TRUE;
     break;}
     }
   
   $params['membership_type_id'] = $membershipTypeID;
   if (! $renewal) {//  no memberships of that organization create new
      $params['join_data'] =  date('Ymd');
      // have civi calculate dates for membership.
      $calcDates = CRM_Member_BAO_Membershiptype::getDatesForMembershipTypE($params['membership_type_id'],     $params['join_date'], $params['start_date']);
      $params = array_merge($params, $calcDates);
      $params['source'] =  'Online Contribution: ' . $organization_name . ' New Member Dues';
      $api_results[$fid] =& civicrm_contact_membership_create($params);
      }
 // renew or upsell
else {         
    $calcDates =
    CRM_Member_BAO_MembershipType::getRenewalDatesForMembershipType(  $membership_to_update, null, $membershipTypeID );
     $params = array_merge($params, $calcDates);
     $params['id'] = $membership_to_update;
     $params['source'] =  'Online Contribution: '  .  $organization_name
        . ' Dues Renewal';
     $api_results[$fid] =& civicrm_contact_membership_create($params);

 }
//    else { $cid = 20;    test using this contact page
//      require_once 'CRM/Contribute/BAO/ContributionPage.php';
 //     $civiform =  CRM_Contribute_BAO_ContributionPage::copy($cid);
//  _civicrm_object_to_array(CRM_Member_BAO_Membership::renewMembership($contact_id,            $membershipTypeID, $is_test, $civiform, date('YmdHis'), null,null ),  $membership);
 //    $membership['source'] = 'civi code';
 //    $api_results[$fid] =  $membership;
 //   }

            break;
Merlise Clyde

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • API Membership request

This forum was archived on 2017-11-26.