CRM-20432: Change Membership status to current when related contribution payment...
authorAlok Patel <alok@agileware.com.au>
Thu, 12 Oct 2017 06:07:51 +0000 (11:37 +0530)
committerAlok Patel <alok@agileware.com.au>
Mon, 16 Oct 2017 05:19:17 +0000 (10:49 +0530)
CRM/Contribute/Form/AdditionalPayment.php
tests/phpunit/CRM/Contribute/Form/AdditionalPaymentTest.php

index 230c7e8600d3ee965a22c4ff39690b5a2311667f..76c46bb6244e4fa4fe6b686f489fc579aab701be 100644 (file)
@@ -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,
index 779e60cf7187d0a63951ffc78eef75454ad480d6..6cae3eedbe3ed06f29d38cd954ec1d17cd1782b4 100644 (file)
@@ -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.
    */