Skip to content

Commit

Permalink
Project60#18: added functionality to update membership end date when …
Browse files Browse the repository at this point in the history
…a membership payment is added.
  • Loading branch information
jaapjansma committed Jun 19, 2018
1 parent f863012 commit 7868c13
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 12 deletions.
9 changes: 7 additions & 2 deletions CRM/Admin/Form/Setting/MembershipExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,12 @@ public function buildQuickForm( ) {
array('multiple' => "multiple", 'class' => 'crm-select2'));

$this->addElement('checkbox',
"hide_auto_renewal",
ts("Hide Auto Renewal"));
"update_membership_status",
ts("Update membership status and end date"));

$this->addElement('checkbox',
"hide_auto_renewal",
ts("Hide Auto Renewal"));

$this->addElement('select',
"paid_by_field",
Expand Down Expand Up @@ -161,6 +165,7 @@ function postProcess() {
$settings->setSetting('membership_number_show', CRM_Utils_Array::value('membership_number_show', $values), FALSE);
$settings->setSetting('hide_auto_renewal', CRM_Utils_Array::value('membership_number_show', $values), FALSE);
$settings->setSetting('paid_via_field', $values['paid_via_field'], FALSE);
$settings->setSetting('update_membership_status', $values['update_membership_status'], FALSE);
$settings->setSetting('paid_by_field', $values['paid_by_field'], FALSE);
if (is_array($values['live_statuses']) && !empty($values['live_statuses'])) {
$settings->setSetting('live_statuses', $values['live_statuses'], FALSE);
Expand Down
53 changes: 45 additions & 8 deletions CRM/Membership/PaidByLogic.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,11 @@ public function assignSepaInstallment($mandate_id, $contribution_recur_id, $cont
$paid_via = $settings->getPaidViaField();
if (!$paid_via) return;

// then assign
CRM_Core_DAO::executeQuery("
INSERT IGNORE INTO civicrm_membership_payment (membership_id, contribution_id)
SELECT
entity_id AS membership_id,
{$contribution_id} AS contribution_id
FROM {$paid_via['table_name']}
WHERE {$paid_via['column_name']} = {$contribution_recur_id};");
// Create membership payment with the api. So that the pre and post hooks are invoked.
$membership_dao = CRM_Core_DAO::executeQuery("SELECT entity_id AS membership_id FROM {$paid_via['table_name']} WHERE {$paid_via['column_name']} = {$contribution_recur_id}");
while($membership_dao->fetch()) {
civicrm_api3('MembershipPayment', 'create', array('membership_id' => $membership_dao->membership_id, 'contribution_id' => $contribution_id));
}
}


Expand Down Expand Up @@ -442,4 +439,44 @@ public function membershipUpdatePOST($membership_id, $object) {
}
}
}


/**
* MEMBERSHIP PAYMENT STATUS MONITORING
*
* If a membership payment is added also update the end date of the membership.
* We don't check the status of the contribution as we assume only pending or completed contributions
* will be added to the membership.
*
* @param $contribution_id integer Contribution ID
* @param $membership_id object Contribution BAO object (?)
* @throws Exception only if something's wrong with the pre/post call sequence - shouldn't happen
*/
public function membershipPaymentCreatePOST($contribution_id, $membership_id) {
$settings = CRM_Membership_Settings::getSettings();
if (!$settings->getSetting('update_membership_status')) {
return;
}

// Calculate new end date and set this as the new membership end date.
$contribution = civicrm_api3('Contribution', 'getsingle', array('id' => $contribution_id));
$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']);
$newEndDate = new DateTime($newDates['end_date']);
if ($newEndDate > $currentEndDate) {
$membershipStatus = CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate(
CRM_Utils_Date::customFormat($membership['start_date'], '%Y%m%d'),
$newDates['end_date'],
CRM_Utils_Date::customFormat($membership['join_date'], '%Y%m%d'),
'today',
FALSE,
$membership['membership_type_id']
);
$membershipParams['status_id'] = $membershipStatus['id'];
$membershipParams['id'] = $membership_id;
$membershipParams['end_date'] = $newDates['end_date'];
civicrm_api3('Membership', 'create', $membershipParams);
}
}
}
4 changes: 4 additions & 0 deletions membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ function membership_civicrm_post($op, $objectName, $objectId, &$objectRef) {
$logic->membershipUpdatePOST($objectId, $objectRef);
}
}
if ($objectName == 'MembershipPayment' && $op == 'create') {
$logic = CRM_Membership_PaidByLogic::getSingleton();
$logic->membershipPaymentCreatePOST($objectRef->contribution_id, $objectRef->membership_id);
}
}


Expand Down
6 changes: 5 additions & 1 deletion templates/CRM/Admin/Form/Setting/MembershipExtension.hlp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
<p>{ts}The field has to be an integer type, active, read only, and searchable custom field to be eligible.{/ts}</p>
{/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>
{/htxt}

{htxt id='id-paid-via-end-status'}
<p>{ts}For any membership status you select, the linked recurring contribution or mandate will be ended automatically should a membership be set to that status.{/ts}</p>
{/htxt}
Expand Down Expand Up @@ -70,4 +74,4 @@

{htxt id='id-hide-auto-renewal'}
<p>{ts}The built-in auto renewal feature interferes with the P60 membership concept, so we recommend not to use it. If you activate this option the fields will be hidden from the UI and the column in the tab and the search result will be freed up for something else.{/ts}</p>
{/htxt}
{/htxt}
5 changes: 4 additions & 1 deletion templates/CRM/Admin/Form/Setting/MembershipExtension.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
<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 @@ -69,7 +73,6 @@
<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>
</table>
</div>
</div>
Expand Down

0 comments on commit 7868c13

Please sign in to comment.