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) »
  • Prorating memberships
Pages: [1] 2

Author Topic: Prorating memberships  (Read 7192 times)

cbh

  • I post occasionally
  • **
  • Posts: 71
  • Karma: 0
  • CiviCRM version: 4.1.3
  • CMS version: Drupal 7.34
  • MySQL version: 5.1.71-log
  • PHP version: 5.5.3
Prorating memberships
January 20, 2010, 07:08:56 am
Hi there

Is there any way to prorate a membership - for example, for a fixed term membership running for the calendar year, for a $500 fee. User joins in June, and we only want them to pay $250. I've done searches on the site. Can't see anything that would do this in the membership type.

Thanks!
Chris Hutcheson

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: Prorating memberships
January 20, 2010, 07:19:09 am

you can do this via the membership type hook:

http://wiki.civicrm.org/confluence/display/CRMDOC/CiviCRM+hook+specification#CiviCRMhookspecification-hookcivicrmmembershipTypeValues

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

mistresszhivago

  • Guest
Re: Prorating memberships
September 15, 2010, 07:22:32 pm
cbh - Did you ever find a solution?  I looked at the hook, but it just looked like giving 50% to some memberships.  I too, need a fix that prorates on a percentage for WHENEVER they sign up.  Membership runs 1 year from Jan1 - Dec 31.  If they sign up in March, June, August, November, etc.  It should evenly divide the membership fee and calculate accordingly.

Hopefully you were able to find a fix - I just installed it today and this is a major issue... ???

Dave Greenberg

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 5760
  • Karma: 226
    • My CiviCRM Blog
Re: Prorating memberships
September 16, 2010, 05:32:28 pm
The 50% code shown is only an example. You can use this hook to modify the pricing for memberships based on anything that you can write code in the hook for. So for your case, you would include a php function in your hook implementation which figures out a pro-rate percentage based on the current date and modifies the "full price" for each membership type accordingly.

I'd recommend that you review the "Extending CiviCRM" chapter in the online book to get a better handle on hooks and how they can be used:
http://en.flossmanuals.net/CiviCRM/DevelopIntro
Protect your investment in CiviCRM by  becoming a Member!

cbh

  • I post occasionally
  • **
  • Posts: 71
  • Karma: 0
  • CiviCRM version: 4.1.3
  • CMS version: Drupal 7.34
  • MySQL version: 5.1.71-log
  • PHP version: 5.5.3
Re: Prorating memberships
September 16, 2010, 05:33:59 pm
Hi there.

Sorry, but I haven't gone any further with the prorating issue at this point - there's been no pressure to do so...

Cheers
Chris

ethanc

  • I’m new here
  • *
  • Posts: 6
  • Karma: 0
Re: Prorating memberships
May 04, 2011, 10:08:04 am
I'm trying to set up a prorated-by-month membership structure using the membershipTypeValues hook, but the $fee variable is not getting passed to the function, resulting in a blank minimum_fee on the rendered page.  Any idea why it's not working as expected?

Quote
$fees = array(
'1' => '21.00',
'2' => '17.00',
'3' => '13.00',
'4' => '9.00',
'5' => '5.00',
'6' => '50.00',
'7' => '47.00',
'8' => '43.00',
'9' => '39.00',
'10' => '35.00',
'11' => '30.00',
'12' => '25.00',
);

$fee = $fees[date('n')];

function prorate_civicrm_membershipTypeValues( &$form, &$membershipTypeValues ) {
   global $fee;
   $membershipTypeValues[1]['name'] = 'Member';
   $membershipTypeValues[1]['minimum_fee'] = $fee;
}

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: Prorating memberships
May 04, 2011, 10:33:54 am

put a debug in the code and check what the $fee variable is

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

ethanc

  • I’m new here
  • *
  • Posts: 6
  • Karma: 0
Re: Prorating memberships
May 04, 2011, 10:52:12 am
I created a standalone php file with an arbitrary function instead of prorate_etc and it works as expected, $fee is 5.00

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: Prorating memberships
May 04, 2011, 01:23:21 pm

Can you add debug statements in the functional call when the hook is called and see what values your hook function returns

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

ethanc

  • I’m new here
  • *
  • Posts: 6
  • Karma: 0
Re: Prorating memberships
May 04, 2011, 05:15:12 pm
var_dump($fee); gives me NULL
« Last Edit: May 05, 2011, 03:24:44 pm by ethanc »

herb

  • I’m new here
  • *
  • Posts: 27
  • Karma: 2
Re: Prorating memberships
November 13, 2012, 02:14:23 pm
I added the following code to a module for a client. It prorates fixed memberships on a monthly basis (you could adapt it for daily or weekly if you so wish). It also accounts for the rollover date so that when the purchaser gets an extra year they get charged the prorated amount for the current membership period plus the full charge for the next membership period.

Code: [Select]
function mymodule_civicrm_membershipTypeValues( &$form, &$membershipTypeValues ) {
  require_once "CRM/Core/Error.php";
  require_once "api/api.php";
   
  foreach ( $membershipTypeValues as &$values) {
    $results = civicrm_api("MembershipType","get", array('version'=>'3','sequential'=>'1','id'=>$values[id]));
    //CRM_Core_Error::debug( 'res', $results );
     
    if(($results['is_error'] > 0 ) || $results['values'][0]['period_type'] != 'fixed') { return; }
   
    // Starting date as set in Member type
    $start_month = substr($results['values'][0]['fixed_period_start_day'], 0, strlen($results['values'][0]['fixed_period_start_day']) - 2);
   
    // Rollover date
    $rollover_month = substr($results['values'][0]['fixed_period_rollover_day'], 0, strlen($results['values'][0]['fixed_period_rollover_day']) - 2);
   
    $today = getdate();
    //$today = getdate(strtotime('2013-04-01')); // test different dates
   
    // Calcuate the number of months remaining in the membership period
    $months = $start_month - $today['mon'];
    if ($months < 0) { $months = 12 + $months; }
    $ratio = $months/12;
   
    // Note that we don't need to calculate for HST since
    // it doesn't make a difference in our ratio
    if (($today['mon'] >= $rollover_month) && ($today['mon'] <= $start_month)) {
      $values['minimum_fee'] = $values['minimum_fee'] + $values['minimum_fee'] * $ratio;
    } else {
      $values['minimum_fee'] = $values['minimum_fee'] * $ratio;
    }
  }
}
Web Developer
Freeform Solutions

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: Prorating memberships
February 08, 2013, 06:53:04 am
I've used the basic framework of the code provided, although it is altered to prorate daily, and on rolling membership terms...

My problem is that altering the $values['minimum_fee'] doesn't seem to affect the amount that is actually charged....should altering that variable do that?

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: Prorating memberships
February 08, 2013, 07:32:36 am
anyone know of a post hook for membership contribution pages???

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: Prorating memberships
February 08, 2013, 08:02:17 am

all civi forms invoke the buildForm, validate and postProcess hook

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: Prorating memberships
February 08, 2013, 09:04:38 am
 ;D Good stuff....thanks Lobo....I should have thought of that! ;D CiviCRM newbie here

got it to work prorating the amount in the validateForm hook

My customer wanted two billing periods, one starting on Dec1 and the next on June1.  I wanted to take into account leap years so this code does.  It prorates to the day.

I have the Membership types to rolling...gonna figure out how to change the end dates of new memberships somewhere else....so things will just roll on form there! But thats another forum topic....


Code: [Select]
function mycustommodule_civicrm_validateForm( $formName, &$fields, &$files, &$form, &$errors ){

if($formName = 'CRM_Contribute_Form_Contribution_Main' && $form->_values['fee']){
  foreach($form->_values['fee'] as &$fees){
   foreach($fees['options'] as &$fee) {
   
   $period1 = 182;
$period2 = 183;
$current_period = 0;
$ratio = 0.0;

  $today = getdate();



$today_year = $today['year'];
$next_year = (int)$today_year + 1;

$current_day_in_year = $today['yday'];

$june1 = getdate(strtotime($today['year']. '-06-01'));
$dec1 = getdate(strtotime($today['year']. '-12-01'));
$june1_day_in_year = $june1['yday'];
$dec1_day_in_year = $dec1['yday'];

if($current_day_in_year < $june1_day_in_year) {
  if(date('L')) $period1 += 1;
  $days = $period1 - ($current_day_in_year + 31);
$current_period = 1;
}

if($current_day_in_year > $june1_day_in_year && $current_day_in_year < $dec1_day_in_year) {
  $days = $period2 - ($current_day_in_year + 31);
$current_period = 2;
} 

if($current_day_in_year > $dec1_day_in_year) {
if(date('L',strtotime($next_year . '-01-02'))) $period1 +=1;
$days = $period1 - ($current_day_in_year - $dec1_day_in_year);
$current_period = 1;
}

if($current_day_in_year == $june1_day_in_year) { 
$days=$period2;
$current_period = 2;
}

if($current_day_in_year ==  $dec1_day_in_year) {
if(date('L',strtotime($next_year . '-01-02'))) $period1 +=1;
$days=$period1;
$current_period = 1;
}
   
         
    switch($current_period){
  case 1: $ratio = $days/$period1; break;
case 2: $ratio = $days/$period2; break;

}
   
$fee['amount'] = strval(round(($fee['amount'] * $ratio),2,PHP_ROUND_HALF_UP));




}

  }
}

}

« Last Edit: February 08, 2013, 09:09:24 am by jackrabbithanna »

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

This forum was archived on 2017-11-26.