Skip to content

Commit

Permalink
SUP-72584 When membership is edited, check settings and membership st…
Browse files Browse the repository at this point in the history
…atus before adjusting or using a specific membership end date
  • Loading branch information
agileware-justin committed Jul 15, 2024
1 parent baa54d5 commit df6c5b5
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
1 change: 1 addition & 0 deletions api/v3/Membershiputils/Adjustmembershipenddate.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function civicrm_api3_membershiputils_Adjustmembershipenddate($params): array {
// If this option is enabled then action, otherwise skip
if (Civi::settings()->get('adjust_membership_end_date')) {
// Get all memberships
// Note: This intentionally updates both Primary and Non-primary memberships because CiviCRM has a long history of bugs when it comes to correctly inheriting changes from Primary to non-primary memberships
$memberships = Membership::get()
->addSelect('id', 'end_date')
->addWhere('status_id:name', 'IN', [
Expand Down
1 change: 1 addition & 0 deletions api/v3/Membershiputils/Specificmembershipenddate.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function civicrm_api3_membershiputils_Specificmembershipenddate($params): array
->get('specific_membership_end_date'));

// Get all memberships
// Note: This intentionally updates both Primary and Non-primary memberships because CiviCRM has a long history of bugs when it comes to correctly inheriting changes from Primary to non-primary memberships
\Civi\Api4\Membership::update(TRUE)
->addValue('end_date', $new_end_date->format('Y-m-d'))
->addWhere('status_id:name', 'IN', [
Expand Down
4 changes: 2 additions & 2 deletions info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<url desc="Support">https://agileware.com.au/contact</url>
<url desc="Licensing">http://www.gnu.org/licenses/agpl-3.0.html</url>
</urls>
<releaseDate>2024-06-07</releaseDate>
<version>1.4.3</version>
<releaseDate>2024-07-15</releaseDate>
<version>1.4.4</version>
<develStage>stable</develStage>
<compatibility>
<ver>5.27</ver>
Expand Down
20 changes: 16 additions & 4 deletions membershiputils.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,22 +177,34 @@ function membershiputils_adjustmembershipenddate($end_date) {
return $new_end_date->format($date_format);
}

function membershiputils_civicrm_pre($op, $objectName, $id, &$params) {
function membershiputils_civicrm_pre($op, $objectName, $id, &$params)
{
// If the Membership is being created or edited and the end date has been set then adjust
// Skip when 'null' is set as the end_date as this indicates a Lifetime membership term with on end date
if (('Membership' == $objectName) && ('edit' == $op || 'create' == $op) && $params['end_date'] && 'null' !== $params['end_date']) {
if (Civi::settings()->get('adjust_membership_end_date') || Civi::settings()->get('use_specific_membership_end_date')) {
// This is where CiviCRM may pass in the end date in two different formats

// Get all the membership statuses
$membershipStatuses = \Civi\Api4\MembershipStatus::get(TRUE)
->addSelect('id', 'name')
->execute()
->indexBy('id')
->getArrayCopy();

$currentStatus = $membershipStatuses[$params['status_id']]['name'];

if (Civi::settings()->get('adjust_membership_end_date') && $currentStatus == 'New' || $currentStatus == 'Current' || $currentStatus == 'Grace') {
$params['end_date'] = membershiputils_adjustmembershipenddate($params['end_date']);
}
} elseif (Civi::settings()->get('use_specific_membership_end_date') && $currentStatus == 'New' || $currentStatus == 'Current') {
$params['end_date'] = membershiputils_adjustmembershipenddate($params['end_date']);
}
}

function membershiputils_civicrm_post($op, $objectName, $id, &$params) {
// This function is required to work around weirdness in CiviCRM which sometimes does not set the membership dates, they may be NULL
if ('Membership' == $objectName && 'create' == $op) {
if (Civi::settings()->get('adjust_membership_end_date') || Civi::settings()->get('use_specific_membership_end_date')) {
// If end date has been set then do not try to calculate it now
// If end date has not been set then try to calculate it now
if (!$params->end_date) {
$dates = CRM_Member_BAO_MembershipType::getDatesForMembershipType($params->membership_type_id, NULL, NULL, NULL);

Expand Down
2 changes: 1 addition & 1 deletion settings/Membershiputils.setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
'is_domain' => 1,
'is_contact' => 0,
'title' => E::ts('Specific Membership End Date'),
'description' => E::ts('Set the Membership End Date to the end of month. Applies to New, Current and Grace memberships only. Leave blank to disable.'),
'description' => E::ts('Set the Membership End Date to the end of month. Applies to New and Current memberships only. Leave blank to disable.'),
'settings_pages' => ['membershiputils' => ['weight' => 10]],
],
'membershiputils_type_change_notification' => [
Expand Down

0 comments on commit df6c5b5

Please sign in to comment.