From: Alok Patel Date: Thu, 12 Oct 2017 06:07:51 +0000 (+0530) Subject: CRM-20432: Change Membership status to current when related contribution payment... X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=425f113a3e7e3a07d971403d7fc9846ca2119d0c;p=civicrm-core.git CRM-20432: Change Membership status to current when related contribution payment is recorded from pending status. --- diff --git a/CRM/Contribute/Form/AdditionalPayment.php b/CRM/Contribute/Form/AdditionalPayment.php index 230c7e8600..76c46bb624 100644 --- a/CRM/Contribute/Form/AdditionalPayment.php +++ b/CRM/Contribute/Form/AdditionalPayment.php @@ -355,6 +355,34 @@ class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_Abstract ); $contributionStatusID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $this->_contributionId, 'contribution_status_id'); if ($contributionStatuses[$contributionStatusID] == 'Pending') { + // CRM-20432, Updating the Pending status of Membership Record along with contribution. + // Updating the membership status to Current from Pending. + // + // Updating the status directly here is good idea instead of doing it at Schedule job level, + // In which we may have to go through all the memberships which are pending and then their contributions of each. + // Which could make the process slower and time consuming for large records. + $membershipStatuses = CRM_Member_PseudoConstant::membershipStatus(); + $pendingStatusId = array_search('Pending', $membershipStatuses); + + $membership_payments = civicrm_api3('membership_payment', 'get', + array( + 'contribution_id' => $this->_contributionId, + 'membership_id.status_id' => $pendingStatusId, + ) + ); + + if ($membership_payments["count"] > 0) { + $membership_payments = $membership_payments["values"]; + foreach ($membership_payments as $membership_payment) { + $membership_id = $membership_payment["membership_id"]; + $membership = civicrm_api3('Membership', 'getsingle', array( + 'id' => $membership_id, + )); + $currentStatusId = array_search('Current', $membershipStatuses); + $membership["status_id"] = $currentStatusId; + civicrm_api3('Membership', 'create', $membership); + } + } civicrm_api3('Contribution', 'create', array( 'id' => $this->_contributionId, diff --git a/tests/phpunit/CRM/Contribute/Form/AdditionalPaymentTest.php b/tests/phpunit/CRM/Contribute/Form/AdditionalPaymentTest.php index 779e60cf71..6cae3eedbe 100644 --- a/tests/phpunit/CRM/Contribute/Form/AdditionalPaymentTest.php +++ b/tests/phpunit/CRM/Contribute/Form/AdditionalPaymentTest.php @@ -206,6 +206,52 @@ class CRM_Contribute_Form_AdditionalPaymentTest extends CiviUnitTestCase { $this->checkResults(array(30, 70), 2); } + /** + * Test the Membership status after completing the pending pay later Contribution. + */ + public function testMembershipStatusAfterCompletingPayLaterContribution() { + $this->createContribution('Pending'); + $membership = $this->createPendingMembershipAndRecordContribution($this->_contributionId); + // pay additional amount + $this->submitPayment(100); + $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId)); + $contributionMembership = $this->callAPISuccessGetSingle('Membership', array('id' => $membership["id"])); + $membershipStatus = $this->callAPISuccessGetSingle('MembershipStatus', array('id' => $contributionMembership["status_id"])); + $this->assertEquals('Current', $membershipStatus['name']); + } + + private function createPendingMembershipAndRecordContribution($contributionId) { + $this->_individualId = $this->individualCreate(); + $membershipTypeAnnualFixed = $this->callAPISuccess('membership_type', 'create', array( + 'domain_id' => 1, + 'name' => "AnnualFixed", + 'member_of_contact_id' => 1, + 'duration_unit' => "year", + 'duration_interval' => 1, + 'period_type' => "fixed", + 'fixed_period_start_day' => "101", + 'fixed_period_rollover_day' => "1231", + 'relationship_type_id' => 20, + 'financial_type_id' => 2, + )); + $membershipStatuses = CRM_Member_PseudoConstant::membershipStatus(); + $pendingStatusId = array_search('Pending', $membershipStatuses); + $membership = $this->callAPISuccess('Membership', 'create', array( + 'contact_id' => $this->_individualId, + 'membership_type_id' => $membershipTypeAnnualFixed['id'], + )); + // Updating Membership status to Pending + $membership = $this->callAPISuccess('Membership', 'create', array( + 'id' => $membership["id"], + 'status_id' => $pendingStatusId, + )); + $membershipPayment = $this->callAPISuccess('MembershipPayment', 'create', array( + 'membership_id' => $membership["id"], + 'contribution_id' => $contributionId, + )); + return $membership; + } + /** * Test the submit function that completes the pending pay later Contribution with multiple payments. */