From e81d05f2dcf5772f48ab447ac0e472de2e6d4a89 Mon Sep 17 00:00:00 2001 From: Jitendra Purohit Date: Fri, 2 Jun 2017 20:43:58 +0530 Subject: [PATCH 1/2] 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 de047c5c0ea3..d4d1eef84b0f 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -4601,6 +4601,7 @@ public static function completeOrder(&$input, &$ids, $objects, $transaction, $re '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 7cedc4318c76..6d206aa137b1 100644 --- a/CRM/Member/BAO/Membership.php +++ b/CRM/Member/BAO/Membership.php @@ -189,7 +189,7 @@ public static function add(&$params, $ids = array()) { ); // 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 @@ public static function fixMembershipStatusBeforeRenew(&$currentMembership, $chan $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 @@ public static function fixMembershipStatusBeforeRenew(&$currentMembership, $chan 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 @@ public static function processMembership($contactID, $membershipTypeID, $is_test 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 7e2579e3f357..626195a2ca6b 100644 --- a/tests/phpunit/api/v3/MembershipTest.php +++ b/tests/phpunit/api/v3/MembershipTest.php @@ -848,6 +848,19 @@ public function testMembershipCreateWithId() { ); $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'], )); From 9b158da74776bc2a942eb6dfaf880ebfa8b3b3a8 Mon Sep 17 00:00:00 2001 From: Monish Deb Date: Mon, 19 Jun 2017 22:59:04 +0530 Subject: [PATCH 2/2] Minor fix at unit test --- tests/phpunit/api/v3/MembershipTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/api/v3/MembershipTest.php b/tests/phpunit/api/v3/MembershipTest.php index 626195a2ca6b..3738afe92814 100644 --- a/tests/phpunit/api/v3/MembershipTest.php +++ b/tests/phpunit/api/v3/MembershipTest.php @@ -852,7 +852,7 @@ public function testMembershipCreateWithId() { //Update Status and check activities created. $updateStatus = array( 'id' => $result['id'], - 'status_id' => 6, + 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Member_BAO_Membership', 'status_id', 'Cancelled'), ); $this->callAPISuccess('Membership', 'create', $updateStatus); $activities = CRM_Activity_BAO_Activity::getContactActivity($this->_contactID);