Skip to content

Commit

Permalink
Project60#18 modified because it should only extend a membership as s…
Browse files Browse the repository at this point in the history
…oon as a contribution is completed.
  • Loading branch information
jaapjansma committed Jun 20, 2018
1 parent 7868c13 commit f36348c
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CRM/Admin/Form/Setting/MembershipExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function buildQuickForm( ) {

$this->addElement('checkbox',
"update_membership_status",
ts("Update membership status and end date"));
ts("Extend membership when contribution is completed"));

$this->addElement('checkbox',
"hide_auto_renewal",
Expand Down
64 changes: 63 additions & 1 deletion CRM/Membership/PaidByLogic.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class CRM_Membership_PaidByLogic
/** stores the pre/post hook records */
protected $monitoring_stack = array();

/** stores the pre/post hook records for contribution status changed */
protected $contribution_status_monitoring_stack = array();

public static function getSingleton()
{
if (self::$singleton === NULL) {
Expand Down Expand Up @@ -458,8 +461,13 @@ public function membershipPaymentCreatePOST($contribution_id, $membership_id) {
return;
}

// Calculate new end date and set this as the new membership end date.
$completed_status = civicrm_api3('OptionValue', 'getvalue', array('name' => 'Completed', 'option_group_id' => 'contribution_status', 'return' => 'value'));
$contribution = civicrm_api3('Contribution', 'getsingle', array('id' => $contribution_id));
if ($contribution['contribution_status_id'] != $completed_status) {
return; // Do not calculate the new end date as the contribution is not yet completed.
}

// Calculate new end date and set this as the new membership end date.
$membership = civicrm_api3('Membership', 'getsingle', array('id' => $membership_id));
$currentEndDate = new DateTime($membership['end_date']);
$newDates = CRM_Member_BAO_MembershipType::getRenewalDatesForMembershipType($membership_id, $contribution['receive_date']);
Expand All @@ -479,4 +487,58 @@ public function membershipPaymentCreatePOST($contribution_id, $membership_id) {
civicrm_api3('Membership', 'create', $membershipParams);
}
}

/**
* Contribution status monitor function.
*
* Monitor for contribution status changed to completed to update the membership end date.
*
* @param $contribution_id
* @param $params
* @throws Exception only if something's wrong with the pre/post call sequence - shouldn't happen
*/
public function contributionUpdatePRE($contribution_id, $params) {
$settings = CRM_Membership_Settings::getSettings();
if (!$settings->getSetting('update_membership_status')) {
return;
}

$completed_status = civicrm_api3('OptionValue', 'getvalue', array('name' => 'Completed', 'option_group_id' => 'contribution_status', 'return' => 'value'));
$contribution = civicrm_api3('Contribution', 'getsingle', array('id' => $contribution_id));
if ($params['contribution_status_id'] != $completed_status) {
return;
}
if ($params['contribution_status_id'] == $contribution['contribution_status_id']) {
return;
}

$this->contribution_status_monitoring_stack[$contribution_id] = $contribution;
}

/**
* Contribution status monitor function.
*
* Monitor for contribution status changed to completed to update the membership end date.
*
* @param $contribution_id
* @param $object
* @throws Exception only if something's wrong with the pre/post call sequence - shouldn't happen
*/
public function contributionUpdatePOST($contribution_id, $object) {
$settings = CRM_Membership_Settings::getSettings();
if (!$settings->getSetting('update_membership_status')) {
return;
}

if (!isset($this->contribution_status_monitoring_stack[$contribution_id])) {
return;
}

$membershipPayments = civicrm_api3('MembershipPayment', 'get', array('contribution_id' => $contribution_id, 'options' => array('limit' => 0)));
foreach($membershipPayments['values'] as $membershipPayment) {
$this->membershipPaymentCreatePOST($contribution_id, $membershipPayment['membership_id']);
}

unset($this->contribution_status_monitoring_stack[$contribution_id]);
}
}
8 changes: 8 additions & 0 deletions membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ function membership_civicrm_pre($op, $objectName, $id, &$params) {
$logic->membershipUpdatePre($id, $params);
}
}
if ($objectName == 'Contribution' && $op == 'edit') {
$logic = CRM_Membership_PaidByLogic::getSingleton();
$logic->contributionUpdatePRE($id, $params);
}
}

/**
Expand All @@ -176,6 +180,10 @@ function membership_civicrm_post($op, $objectName, $objectId, &$objectRef) {
$logic = CRM_Membership_PaidByLogic::getSingleton();
$logic->membershipPaymentCreatePOST($objectRef->contribution_id, $objectRef->membership_id);
}
if ($objectName == 'Contribution' && $op == 'edit') {
$logic = CRM_Membership_PaidByLogic::getSingleton();
$logic->contributionUpdatePOST($objectId, $objectRef);
}
}


Expand Down
3 changes: 2 additions & 1 deletion templates/CRM/Admin/Form/Setting/MembershipExtension.hlp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
{/htxt}

{htxt id='update-membership-status'}
<p>{ts}As soon as the sepa mandate is debitted the contribution is created and added to the membership. If you check this box the end date and the status of the membership will be updated.{/ts}</p>
<p>{ts}Extend the membership as soon as the membership contribution is set to completed.{/ts}</p>
<p>{ts}This will calculate the new end date and a new status.{/ts}</p>
{/htxt}

{htxt id='id-paid-via-end-status'}
Expand Down
8 changes: 4 additions & 4 deletions templates/CRM/Admin/Form/Setting/MembershipExtension.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@
<td>{$form.paid_via_field.label}&nbsp;<a onclick='CRM.help("{ts}Paid Via Field{/ts}", {literal}{"id":"id-paid-via","file":"CRM\/Admin\/Form\/Setting\/MembershipExtension"}{/literal}); return false;' href="#" title="{ts}Help{/ts}" class="helpicon"></a></td>
<td>{$form.paid_via_field.html}</td>
</tr>
<tr>
<td>{$form.update_membership_status.label}&nbsp;<a onclick='CRM.help("{ts}Update membership status and end date{/ts}", {literal}{"id":"update-membership-status","file":"CRM\/Admin\/Form\/Setting\/MembershipExtension"}{/literal}); return false;' href="#" title="{ts}Help{/ts}" class="helpicon"></a></td>
<td>{$form.update_membership_status.html}</td>
</tr>
<tr>
<td>{$form.paid_via_end_with_status.label}&nbsp;<a onclick='CRM.help("{ts}End with status{/ts}", {literal}{"id":"id-paid-via-end-status","file":"CRM\/Admin\/Form\/Setting\/MembershipExtension"}{/literal}); return false;' href="#" title="{ts}Help{/ts}" class="helpicon"></a></td>
<td>{$form.paid_via_end_with_status.html}</td>
Expand All @@ -73,6 +69,10 @@
<td>{$form.hide_auto_renewal.label}&nbsp;<a onclick='CRM.help("{ts}Hide Auto Renewal{/ts}", {literal}{"id":"id-hide-auto-renewal","file":"CRM\/Admin\/Form\/Setting\/MembershipExtension"}{/literal}); return false;' href="#" title="{ts}Help{/ts}" class="helpicon"></a></td>
<td>{$form.hide_auto_renewal.html}</td>
</tr>
<tr>
<td>{$form.update_membership_status.label}&nbsp;<a onclick='CRM.help("{ts}Update membership status and end date{/ts}", {literal}{"id":"update-membership-status","file":"CRM\/Admin\/Form\/Setting\/MembershipExtension"}{/literal}); return false;' href="#" title="{ts}Help{/ts}" class="helpicon"></a></td>
<td>{$form.update_membership_status.html}</td>
</tr>
</table>
</div>
</div>
Expand Down

0 comments on commit f36348c

Please sign in to comment.