Skip to content

Commit

Permalink
Merge pull request #10173 from jitendrapurohit/CRM-18177
Browse files Browse the repository at this point in the history
CRM-18177 - Do not update existing membership status if renewal payme…
  • Loading branch information
eileenmcnaughton authored May 14, 2017
2 parents 090269a + 9e206c5 commit 20a05ba
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
26 changes: 24 additions & 2 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -1727,7 +1727,18 @@ 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;
}
if (CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $contriId) === 'Completed') {
$update = FALSE;
}
}
if ($membership && $update) {
$newStatus = array_search('Cancelled', $membershipStatuses);

// Create activity
Expand Down Expand Up @@ -1777,7 +1788,18 @@ 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;
}
if (CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $contriId) === 'Completed') {
$update = FALSE;
}
}
if ($membership && $update) {
$membership->status_id = array_search('Expired', $membershipStatuses);
$membership->save();

Expand Down
12 changes: 11 additions & 1 deletion CRM/Member/BAO/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
43 changes: 43 additions & 0 deletions tests/phpunit/api/v3/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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);
}

/**
Expand Down

0 comments on commit 20a05ba

Please sign in to comment.