From 6f1164c67764cd6c030c6dd0c5884d6090086650 Mon Sep 17 00:00:00 2001 From: larssandergreen Date: Tue, 29 Aug 2023 12:33:59 -0600 Subject: [PATCH] Fix membership leap year bug --- CRM/Member/BAO/MembershipType.php | 18 +++++++++++------- .../phpunit/CRM/Member/BAO/MembershipTest.php | 4 ---- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/CRM/Member/BAO/MembershipType.php b/CRM/Member/BAO/MembershipType.php index 690279bd42..0d686deacb 100644 --- a/CRM/Member/BAO/MembershipType.php +++ b/CRM/Member/BAO/MembershipType.php @@ -521,7 +521,12 @@ class CRM_Member_BAO_MembershipType extends CRM_Member_DAO_MembershipType implem $date = $membershipDetails->end_date; } $date = explode('-', $date); - // We have to add 1 day first in case it's the end of the month, then subtract afterwards + $year = $date[0]; + $month = $date[1]; + $day = $date[2]; + + // $logStartDate is used for the membership log only, except if the membership is monthly + // then we add 1 day first in case it's the end of the month, then subtract afterwards // eg. 2018-02-28 should renew to 2018-03-31, if we just added 1 month we'd get 2018-03-28 $logStartDate = date('Y-m-d', mktime(0, 0, 0, (double) $date[1], @@ -529,11 +534,6 @@ class CRM_Member_BAO_MembershipType extends CRM_Member_DAO_MembershipType implem (double) $date[0] )); - $date = explode('-', $logStartDate); - $year = $date[0]; - $month = $date[1]; - $day = $date[2]; - switch ($membershipTypeDetails['duration_unit']) { case 'year': //need to check if the upsell is from rolling to fixed and adjust accordingly @@ -548,6 +548,10 @@ class CRM_Member_BAO_MembershipType extends CRM_Member_DAO_MembershipType implem break; case 'month': + $date = explode('-', $logStartDate); + $year = $date[0]; + $month = $date[1]; + $day = $date[2] - 1; $month = $month + ($numRenewTerms * $membershipTypeDetails['duration_interval']); break; @@ -561,7 +565,7 @@ class CRM_Member_BAO_MembershipType extends CRM_Member_DAO_MembershipType implem else { $endDate = date('Y-m-d', mktime(0, 0, 0, $month, - $day - 1, + $day, $year )); } diff --git a/tests/phpunit/CRM/Member/BAO/MembershipTest.php b/tests/phpunit/CRM/Member/BAO/MembershipTest.php index 325dc033e9..67cfe4388f 100644 --- a/tests/phpunit/CRM/Member/BAO/MembershipTest.php +++ b/tests/phpunit/CRM/Member/BAO/MembershipTest.php @@ -466,10 +466,6 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase { /** * Renew membership with change in membership type. * - * @fixme Note that this test fails when today is August 29 2019 (and maybe - * other years?): Verify correct end date is calculated after membership - * renewal Failed asserting that two strings are equal. - * Expected-'2021-03-01' Actual+'2021-02-28' * @throws \CRM_Core_Exception */ public function testRenewMembership(): void { -- 2.25.1