From fde5534336b9b3d798808431d997907b34ad919f Mon Sep 17 00:00:00 2001 From: Jitendra Purohit Date: Mon, 17 Apr 2017 19:30:37 +0530 Subject: [PATCH 1/2] CRM-18177 - Do not update existing membership status if renewal payment is failed --- CRM/Contribute/BAO/Contribution.php | 28 +++++++++++++-- CRM/Member/BAO/Membership.php | 12 ++++++- tests/phpunit/api/v3/ContributionTest.php | 43 +++++++++++++++++++++++ 3 files changed, 80 insertions(+), 3 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 4629db2ed2bd..a1be7033479b 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -1727,7 +1727,19 @@ public static function transitionComponents($params, $processContributionObject if ($contributionStatusId == array_search('Cancelled', $contributionStatuses)) { if (is_array($memberships)) { foreach ($memberships as $membership) { - if ($membership) { + $update = TRUE; + //Update Membership status if there is no other completed contribution associated with the membership. + $relatedContributions = CRM_Member_BAO_Membership::getMembershipContributionId($membership->id, TRUE); + foreach ($relatedContributions as $contriId) { + if ($contriId == $contributionId) { + continue; + } + $status = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contriId, 'contribution_status_id'); + if ($status == array_search('Completed', $contributionStatuses)) { + $update = FALSE; + } + } + if ($membership && $update) { $newStatus = array_search('Cancelled', $membershipStatuses); // Create activity @@ -1777,7 +1789,19 @@ public static function transitionComponents($params, $processContributionObject elseif ($contributionStatusId == array_search('Failed', $contributionStatuses)) { if (is_array($memberships)) { foreach ($memberships as $membership) { - if ($membership) { + $update = TRUE; + //Update Membership status if there is no other completed contribution associated with the membership. + $relatedContributions = CRM_Member_BAO_Membership::getMembershipContributionId($membership->id, TRUE); + foreach ($relatedContributions as $contriId) { + if ($contriId == $contributionId) { + continue; + } + $status = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contriId, 'contribution_status_id'); + if ($status == array_search('Completed', $contributionStatuses)) { + $update = FALSE; + } + } + if ($membership && $update) { $membership->status_id = array_search('Expired', $membershipStatuses); $membership->save(); diff --git a/CRM/Member/BAO/Membership.php b/CRM/Member/BAO/Membership.php index 07716ced1026..7cedc4318c76 100644 --- a/CRM/Member/BAO/Membership.php +++ b/CRM/Member/BAO/Membership.php @@ -2121,14 +2121,24 @@ public function processPriceSet($membershipId, $lineItem) { * * @param int $membershipId * Membership id. + * @all bool + * if more than one payment associated with membership id need to be returned. * * @return int * contribution id */ - public static function getMembershipContributionId($membershipId) { + public static function getMembershipContributionId($membershipId, $all = FALSE) { $membershipPayment = new CRM_Member_DAO_MembershipPayment(); $membershipPayment->membership_id = $membershipId; + if ($all && $membershipPayment->find()) { + $contributionIds = array(); + while ($membershipPayment->fetch()) { + $contributionIds[] = $membershipPayment->contribution_id; + } + return $contributionIds; + } + if ($membershipPayment->find(TRUE)) { return $membershipPayment->contribution_id; } diff --git a/tests/phpunit/api/v3/ContributionTest.php b/tests/phpunit/api/v3/ContributionTest.php index b2b838f138bf..33478b7928c7 100644 --- a/tests/phpunit/api/v3/ContributionTest.php +++ b/tests/phpunit/api/v3/ContributionTest.php @@ -2835,6 +2835,7 @@ public function testPendingToCompleteContribution() { 'status_id' => 'Completed', )); $this->assertEquals(1, $activity['count']); + // 2.c 'Change membership type' activity created to record Membership status change from Grace to Current $activity = $this->callAPISuccess('Activity', 'get', array( 'activity_type_id' => 'Change Membership Status', @@ -2843,6 +2844,48 @@ public function testPendingToCompleteContribution() { )); $this->assertEquals(1, $activity['count']); $this->assertEquals('Status changed from Grace to Current', $activity['values'][$activity['id']]['subject']); + + //Create another pending contribution for renewal + $contribution = $this->callAPISuccess('contribution', 'create', array( + 'domain_id' => 1, + 'contact_id' => $this->_ids['contact'], + 'receive_date' => date('Ymd'), + 'total_amount' => 20.00, + 'financial_type_id' => 1, + 'payment_instrument_id' => 'Credit Card', + 'non_deductible_amount' => 10.00, + 'trxn_id' => 'rdhfi88', + 'invoice_id' => 'dofhiewuyr', + 'source' => 'SSF', + 'contribution_status_id' => 2, + 'contribution_page_id' => $this->_ids['contribution_page'], + 'api.membership_payment.create' => array('membership_id' => $this->_ids['membership']), + )); + + $this->callAPISuccess('line_item', 'create', array( + 'entity_id' => $contribution['id'], + 'entity_table' => 'civicrm_contribution', + 'contribution_id' => $contribution['id'], + 'price_field_id' => $this->_ids['price_field'][0], + 'qty' => 1, + 'unit_price' => 20, + 'line_total' => 20, + 'financial_type_id' => 1, + 'price_field_value_id' => $this->_ids['price_field_value'][0], + )); + + //Update it to Failed. + $form->_params['id'] = $contribution['id']; + $form->_params['contribution_status_id'] = 4; + try { + $form->testSubmit($form->_params, CRM_Core_Action::UPDATE); + } + catch (Civi\Payment\Exception\PaymentProcessorException $e) { + $error = TRUE; + } + //Existing membership should not get updated to expired. + $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership'])); + $this->assertNotEquals($membership['status_id'], 4); } /** From 9e206c53f3ff785161b1352c0be26ed2d6fff6be Mon Sep 17 00:00:00 2001 From: Jitendra Purohit Date: Sun, 14 May 2017 12:19:15 +0530 Subject: [PATCH 2/2] minor fixes --- CRM/Contribute/BAO/Contribution.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index a1be7033479b..37518424d982 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -1734,8 +1734,7 @@ public static function transitionComponents($params, $processContributionObject if ($contriId == $contributionId) { continue; } - $status = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contriId, 'contribution_status_id'); - if ($status == array_search('Completed', $contributionStatuses)) { + if (CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $contriId) === 'Completed') { $update = FALSE; } } @@ -1796,8 +1795,7 @@ public static function transitionComponents($params, $processContributionObject if ($contriId == $contributionId) { continue; } - $status = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contriId, 'contribution_status_id'); - if ($status == array_search('Completed', $contributionStatuses)) { + if (CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $contriId) === 'Completed') { $update = FALSE; } }