From ed4cc29d26284f5f219dae7b7d24429ee7271eaf Mon Sep 17 00:00:00 2001 From: Jose Torres Date: Wed, 3 Aug 2016 09:38:26 -0700 Subject: [PATCH] crm-19150 Preserve contribution when associated membership is deleted (step 1 of 2 | API and BAO) --- CRM/Member/BAO/Membership.php | 14 ++++++----- api/v3/Membership.php | 24 ++++++++++++++++++- tests/phpunit/api/v3/MembershipTest.php | 31 +++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/CRM/Member/BAO/Membership.php b/CRM/Member/BAO/Membership.php index 8789a02ef7..f6e57ea0d1 100644 --- a/CRM/Member/BAO/Membership.php +++ b/CRM/Member/BAO/Membership.php @@ -603,10 +603,10 @@ INNER JOIN civicrm_membership_type type ON ( type.id = membership.membership_ty * @return int * Id of deleted Membership on success, false otherwise. */ - public static function del($membershipId) { + public static function del($membershipId, $preserveContrib = 0) { //delete related first and then delete parent. self::deleteRelatedMemberships($membershipId); - return self::deleteMembership($membershipId); + return self::deleteMembership($membershipId, $preserveContrib); } /** @@ -618,7 +618,7 @@ INNER JOIN civicrm_membership_type type ON ( type.id = membership.membership_ty * @return int * Id of deleted Membership on success, false otherwise. */ - public static function deleteMembership($membershipId) { + public static function deleteMembership($membershipId, $preserveContrib = 0) { // CRM-12147, retrieve membership data before we delete it for hooks $params = array('id' => $membershipId); $memValues = array(); @@ -654,7 +654,7 @@ INNER JOIN civicrm_membership_type type ON ( type.id = membership.membership_ty $params['source_record_id'] = $membershipId; CRM_Activity_BAO_Activity::deleteActivity($params); } - self::deleteMembershipPayment($membershipId); + self::deleteMembershipPayment($membershipId, $preserveContrib); $results = $membership->delete(); $transaction->commit(); @@ -1500,14 +1500,16 @@ WHERE civicrm_membership.contact_id = civicrm_contact.id * @return object * $membershipPayment deleted membership payment object */ - public static function deleteMembershipPayment($membershipId) { + public static function deleteMembershipPayment($membershipId, $preserveContrib = 0) { $membershipPayment = new CRM_Member_DAO_MembershipPayment(); $membershipPayment->membership_id = $membershipId; $membershipPayment->find(); while ($membershipPayment->fetch()) { - CRM_Contribute_BAO_Contribution::deleteContribution($membershipPayment->contribution_id); + if (!$preserveContrib) { + CRM_Contribute_BAO_Contribution::deleteContribution($membershipPayment->contribution_id); + } CRM_Utils_Hook::pre('delete', 'MembershipPayment', $membershipPayment->id, $membershipPayment); $membershipPayment->delete(); CRM_Utils_Hook::post('delete', 'MembershipPayment', $membershipPayment->id, $membershipPayment); diff --git a/api/v3/Membership.php b/api/v3/Membership.php index 7a60a5f0ba..b47553e676 100644 --- a/api/v3/Membership.php +++ b/api/v3/Membership.php @@ -32,6 +32,18 @@ * @package CiviCRM_APIv3 */ +/** + * Adjust Metadata for Delete action. + * + * The metadata is used for setting defaults, documentation & validation. + * + * @param array $params + * Array of parameters determined by getfields. + */ +function _civicrm_api3_membership_delete_spec(&$params) { + $params['preserve_contribution']['api.required'] = 0; +} + /** * Deletes an existing contact Membership. * @@ -42,7 +54,17 @@ * API result array. */ function civicrm_api3_membership_delete($params) { - return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params); + if (isset($params['preserve_contribution'])){ + if (CRM_Member_BAO_Membership::del($params['id'], $params['preserve_contribution'])) { + return civicrm_api3_create_success(TRUE, $params); + } + else { + return civicrm_api3_create_error('Could not delete membership'); + } + } + else { + return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params); + } } /** diff --git a/tests/phpunit/api/v3/MembershipTest.php b/tests/phpunit/api/v3/MembershipTest.php index 9132aa3563..270214fd52 100644 --- a/tests/phpunit/api/v3/MembershipTest.php +++ b/tests/phpunit/api/v3/MembershipTest.php @@ -132,6 +132,37 @@ class api_v3_MembershipTest extends CiviUnitTestCase { $this->callAPIFailure('membership', 'delete', $membershipId); } + /** + * Test membership deletion and with the preserve contribution param. + */ + public function testMembershipDeletePreserveContribution() { + $membershipID = $this->contactMembershipCreate($this->_params); //DELETE + $this->assertDBRowExist('CRM_Member_DAO_Membership', $membershipID); //DELETE + $ContributionCreate = $this->callAPISuccess('Contribution', 'create', array( + 'sequential' => 1, + 'financial_type_id' => "Member Dues", + 'total_amount' => 100, + 'contact_id' => $this->_params['contact_id'], + )); + $membershipPaymentCreate = $this->callAPISuccess('MembershipPayment', 'create', array( + 'sequential' => 1, + 'contribution_id' => $ContributionCreate['values'][0]['id'], + 'membership_id' => $membershipID, + )); + $memParams = array( + 'id' => $membershipID, + 'preserve_contribution' => 1, + ); + $contribParams = array( + 'id' => $ContributionCreate['values'][0]['id'], + ); + $this->callAPIAndDocument('membership', 'delete', $memParams, __FUNCTION__, __FILE__); + $this->assertDBRowNotExist('CRM_Member_DAO_Membership', $membershipID); + $this->assertDBRowExist('CRM_Contribute_DAO_Contribution', $ContributionCreate['values'][0]['id']); + $this->callAPISuccess('Contribution', 'delete', $contribParams); + $this->assertDBRowNotExist('CRM_Contribute_DAO_Contribution', $ContributionCreate['values'][0]['id']); + } + /** * Test membership get. */ -- 2.25.1