From 6576ae79453539c259f8f7ee72bc1d4945e91a9f Mon Sep 17 00:00:00 2001 From: Ahed Date: Tue, 6 Apr 2021 12:26:11 +0300 Subject: [PATCH] dev/core#2512 Set membership's status properly when recording the payment --- CRM/Contribute/BAO/Contribution.php | 21 ++++++++---- .../Contribute/Form/AdditionalPaymentTest.php | 32 +++++++++++++++++++ 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 5c1b3034ac..36ac0007db 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -4759,11 +4759,8 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac 'membership_activity_status' => 'Completed', ]; - $currentMembership = CRM_Member_BAO_Membership::getContactMembership($membershipParams['contact_id'], - $membershipParams['membership_type_id'], - $membershipParams['is_test'], - $membershipParams['id'] - ); + $currentMembership = []; + CRM_Member_BAO_Membership::retrieve($membershipParams, $currentMembership); // CRM-8141 update the membership type with the value recorded in log when membership created/renewed // this picks up membership type changes during renewals @@ -4781,7 +4778,8 @@ LIMIT 1;"; $membershipParams['membership_type_id'] = $dao->membership_type_id; } } - if (empty($membership['end_date']) || (int) $membership['status_id'] !== CRM_Core_PseudoConstant::getKey('CRM_Member_BAO_Membership', 'status_id', 'Pending')) { + $pendingMembershipStatusID = CRM_Core_PseudoConstant::getKey('CRM_Member_BAO_Membership', 'status_id', 'Pending'); + if (empty($membership['end_date']) || (int) $membership['status_id'] !== $pendingMembershipStatusID) { // Passing num_terms to the api triggers date calculations, but for pending memberships these may be already calculated. // sigh - they should be consistent but removing the end date check causes test failures & maybe UI too? // The api assumes num_terms is a special sauce for 'is_renewal' so we need to not pass it when updating a pending to completed. @@ -4799,7 +4797,16 @@ LIMIT 1;"; 'start_date', 'end_date', ], NULL); - if ($currentMembership) { + if ($currentMembership && (int) $currentMembership['status_id'] === $pendingMembershipStatusID) { + $currentMembership['join_date'] = $currentMembership['join_date'] ?? NULL; + $currentMembership['start_date'] = $currentMembership['start_date'] ?? NULL; + $currentMembership['end_date'] = $currentMembership['end_date'] ?? NULL; + + $dates = CRM_Member_BAO_MembershipType::getDatesForMembershipType($membershipParams['membership_type_id'], + $currentMembership['join_date'], $currentMembership['start_date'], $currentMembership['end_date'] + ); + } + else { /* * Fixed FOR CRM-4433 * In BAO/Membership.php(renewMembership function), we skip the extend membership date and status diff --git a/tests/phpunit/CRM/Contribute/Form/AdditionalPaymentTest.php b/tests/phpunit/CRM/Contribute/Form/AdditionalPaymentTest.php index 229317008a..6925b110f4 100644 --- a/tests/phpunit/CRM/Contribute/Form/AdditionalPaymentTest.php +++ b/tests/phpunit/CRM/Contribute/Form/AdditionalPaymentTest.php @@ -313,6 +313,38 @@ class CRM_Contribute_Form_AdditionalPaymentTest extends CiviUnitTestCase { $this->validateAllPayments(); } + /** + * Test the Membership status renaming after completing the pending pay later Contribution. + * + * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception + */ + public function testMembershipStatusAfterCompletingPayLaterContributionWithRenamedMembershipStatus() { + $this->renameNewMembershipStatus('Fresh'); + $this->createPendingOrder(); + $membership = $this->createPendingMembershipAndRecordContribution($this->_contributionId); + // pay additional amount + $this->submitPayment(100); + $this->callAPISuccessGetSingle('Contribution', ['id' => $this->_contributionId]); + $contributionMembership = $this->callAPISuccessGetSingle('Membership', ['id' => $membership['id']]); + $membershipStatus = $this->callAPISuccessGetSingle('MembershipStatus', ['id' => $contributionMembership['status_id']]); + $this->assertEquals('Fresh', $membershipStatus['name']); + $this->validateAllPayments(); + } + + /** + * @param $membershipStatusName + * + * @throws \CRM_Core_Exception + */ + private function renameNewMembershipStatus($membershipStatusName) { + $params = [ + 'name' => 'New', + 'api.MembershipStatus.create' => ['name' => $membershipStatusName], + ]; + $this->callAPISuccess('MembershipStatus', 'get', $params); + } + /** * @param $contributionId * -- 2.25.1