From e81d05f2dcf5772f48ab447ac0e472de2e6d4a89 Mon Sep 17 00:00:00 2001 From: Jitendra Purohit Date: Fri, 2 Jun 2017 20:43:58 +0530 Subject: [PATCH] CRM-20675: Fix incorrect renewal activity created when membership updated via api --- CRM/Contribute/BAO/Contribution.php | 1 + CRM/Member/BAO/Membership.php | 17 ++++++++++++++++- tests/phpunit/api/v3/MembershipTest.php | 13 +++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index de047c5c0e..d4d1eef84b 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -4601,6 +4601,7 @@ WHERE eft.financial_trxn_id IN ({$trxnId}, {$baseTrxnId['financialTrxnId']}) 'contact_id' => $membership->contact_id, 'is_test' => $membership->is_test, 'membership_type_id' => $membership->membership_type_id, + 'membership_activity_status' => 'Completed', ); $currentMembership = CRM_Member_BAO_Membership::getContactMembership($membershipParams['contact_id'], diff --git a/CRM/Member/BAO/Membership.php b/CRM/Member/BAO/Membership.php index 7cedc4318c..6d206aa137 100644 --- a/CRM/Member/BAO/Membership.php +++ b/CRM/Member/BAO/Membership.php @@ -189,7 +189,7 @@ class CRM_Member_BAO_Membership extends CRM_Member_DAO_Membership { ); // 1. Update Schedule Membership Signup/Renwal activity to completed on successful payment of pending membership // 2. OR Create renewal activity scheduled if its membership renewal will be paid later - if (!empty($activityParams['id']) || $activityType == 'Membership Renewal') { + if (!empty($params['membership_activity_status']) && (!empty($activityParams['id']) || $activityType == 'Membership Renewal')) { CRM_Activity_BAO_Activity::addActivity($membership, $activityType, $targetContactID, $activityParams); break; } @@ -1170,6 +1170,7 @@ AND civicrm_membership.is_test = %2"; $currentMembership['today_date'] = $today; if ($status['id'] !== $currentMembership['status_id']) { + $oldStatus = $currentMembership['status_id']; $memberDAO = new CRM_Member_DAO_Membership(); $memberDAO->id = $currentMembership['id']; $memberDAO->find(TRUE); @@ -1216,6 +1217,19 @@ AND civicrm_membership.is_test = %2"; else { $logParams['modified_id'] = $currentMembership['contact_id']; } + + //Create activity for status change. + $allStatus = CRM_Member_BAO_Membership::buildOptions('status_id', 'get'); + CRM_Activity_BAO_Activity::addActivity($memberDAO, + 'Change Membership Status', + NULL, + array( + 'subject' => "Status changed from {$allStatus[$oldStatus]} to {$allStatus[$status['id']]}", + 'source_contact_id' => $logParams['modified_id'], + 'priority_id' => 'Normal', + ) + ); + CRM_Member_BAO_MembershipLog::add($logParams, CRM_Core_DAO::$_nullArray); } } @@ -1946,6 +1960,7 @@ INNER JOIN civicrm_contact contact ON ( contact.id = membership.contact_id AND if (!empty($currentMembership['id'])) { $ids['membership'] = $currentMembership['id']; } + $memParams['membership_activity_status'] = ($pending || $isPayLater) ? 'Scheduled' : 'Completed'; } //CRM-4555 if ($pending) { diff --git a/tests/phpunit/api/v3/MembershipTest.php b/tests/phpunit/api/v3/MembershipTest.php index 7e2579e3f3..626195a2ca 100644 --- a/tests/phpunit/api/v3/MembershipTest.php +++ b/tests/phpunit/api/v3/MembershipTest.php @@ -848,6 +848,19 @@ class api_v3_MembershipTest extends CiviUnitTestCase { ); $result = $this->callAPISuccess('membership', 'create', $params); + + //Update Status and check activities created. + $updateStatus = array( + 'id' => $result['id'], + 'status_id' => 6, + ); + $this->callAPISuccess('Membership', 'create', $updateStatus); + $activities = CRM_Activity_BAO_Activity::getContactActivity($this->_contactID); + $this->assertEquals(2, count($activities)); + $activityNames = array_flip(CRM_Utils_Array::collect('activity_name', $activities)); + $this->assertArrayHasKey('Membership Signup', $activityNames); + $this->assertArrayHasKey('Change Membership Status', $activityNames); + $this->callAPISuccess('Membership', 'Delete', array( 'id' => $result['id'], )); -- 2.25.1