dev/membership#27 Update outdated membership statuses in preProcess rather than submit
authoreileen <emcnaughton@wikimedia.org>
Mon, 28 Sep 2020 00:05:38 +0000 (13:05 +1300)
committereileen <emcnaughton@wikimedia.org>
Mon, 28 Sep 2020 00:20:27 +0000 (13:20 +1300)
Per https://lab.civicrm.org/dev/membership/-/issues/27 the function fixMembershipBeforeRenew is agreed to be a useful part of this flow
(ie we should set to expired before renewing if that is the correct pre-renewal status). As discussed on that
issue this moves that handling to the pre-process function.

In the process I hit what appears to be an unreleased regression affecting fixMembershipBeforeRenew borking
when changeDate = NULL from https://github.com/civicrm/civicrm-core/commit/2cb6497039fde605f5d64b2fe4fa0548cd5d1e07#diff-f43c8498e32f5b2d68ab27bcd243ca36L1136

The regression only affects master

CRM/Member/BAO/Membership.php
CRM/Member/Form/MembershipRenewal.php
templates/CRM/Member/Form/MembershipRenewal.tpl

index 32ff6e0259103ba0c1ac2267c8deb379aa55984b..41fa61e50f2569aff31bf3f7fcc3be234dff142b 100644 (file)
@@ -1125,13 +1125,13 @@ AND civicrm_membership.is_test = %2";
    *   Reference to the array.
    *   containing all values of
    *   the current membership
-   * @param string $changeToday
+   * @param string|null $changeToday
    *   In case today needs
    *   to be customised, null otherwise
    *
    * @throws \CRM_Core_Exception
    */
-  public static function fixMembershipStatusBeforeRenew(&$currentMembership, $changeToday) {
+  public static function fixMembershipStatusBeforeRenew(&$currentMembership, $changeToday = NULL) {
     $today = 'now';
     if ($changeToday) {
       $today = CRM_Utils_Date::processDate($changeToday, NULL, FALSE, 'Y-m-d');
@@ -1151,8 +1151,6 @@ AND civicrm_membership.is_test = %2";
       throw new CRM_Core_Exception(ts('Oops, it looks like there is no valid membership status corresponding to the membership start and end dates for this membership. Contact the site administrator for assistance.'));
     }
 
-    $currentMembership['today_date'] = $today;
-
     if ($status['id'] !== $currentMembership['status_id']) {
       $oldStatus = $currentMembership['status_id'];
       $memberDAO = new CRM_Member_DAO_Membership();
@@ -1181,10 +1179,7 @@ AND civicrm_membership.is_test = %2";
           $currentMembership['end_date'],
           $format
         ),
-        'modified_date' => CRM_Utils_Date::customFormat(
-          $currentMembership['today_date'],
-          $format
-        ),
+        'modified_date' => date('Y-m-d H:i:s', strtotime($today)),
         'membership_type_id' => $currentMembership['membership_type_id'],
         'max_related' => $currentMembership['max_related'] ?? 0,
       ];
index 55600909effea365e61022f7fea67247585aeec4..8feaa97b8bb358f6f59a5a288b8d7dbb1d149d8c 100644 (file)
@@ -137,10 +137,11 @@ class CRM_Member_Form_MembershipRenewal extends CRM_Member_Form {
     $this->assign('formClass', 'membershiprenew');
     parent::preProcess();
 
-    $this->assign('endDate', CRM_Utils_Date::customFormat(CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership',
-      $this->_id, 'end_date'
-    )
-    ));
+    // @todo - we should store this as a property & re-use in setDefaults - for now that's a bigger change.
+    $currentMembership = civicrm_api3('Membership', 'getsingle', ['id' => $this->_id]);
+    CRM_Member_BAO_Membership::fixMembershipStatusBeforeRenew($currentMembership);
+
+    $this->assign('endDate', $currentMembership['end_date']);
     $this->assign('membershipStatus',
       CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipStatus',
         CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership',
@@ -770,9 +771,6 @@ class CRM_Member_Form_MembershipRenewal extends CRM_Member_Form {
       return CRM_Member_BAO_Membership::create($memParams);
     }
 
-    // Check and fix the membership if it is STALE
-    CRM_Member_BAO_Membership::fixMembershipStatusBeforeRenew($currentMembership, $changeToday);
-
     $isMembershipCurrent = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipStatus', $currentMembership['status_id'], 'is_current_member');
 
     // CRM-7297 Membership Upsell - calculate dates based on new membership type
index 73b1b6850a4b8570057736960de48ec954459562..aa8ff8fb6ed0cb886ebb19d8cdc92d283caba53e 100644 (file)
@@ -68,7 +68,7 @@
       </tr>
       <tr class="crm-member-membershiprenew-form-block-end_date">
         <td class="label">{ts}Membership End Date{/ts}</td>
-        <td class="html-adjust">&nbsp;{$endDate}</td>
+        <td class="html-adjust">&nbsp;{$endDate|crmDate}</td>
       </tr>
       <tr class="crm-member-membershiprenew-form-block-renewal_date">
         <td class="label">{$form.renewal_date.label}</td>