From c02c17dfecd48827b8f3e8951e143f9a084a3d4a Mon Sep 17 00:00:00 2001 From: eileenmcnaughton Date: Wed, 16 Dec 2015 16:00:34 +0000 Subject: [PATCH] CRM-17718 update repeattransaction api action to give precedence to contribution_recur for campaign id --- CRM/Contribute/BAO/Contribution.php | 10 +++ api/v3/Contribution.php | 10 +++ tests/phpunit/api/v3/ContributionTest.php | 85 +++++++++++++++++++++-- 3 files changed, 99 insertions(+), 6 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 46a830b7c1..2e64f674ed 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -2040,6 +2040,16 @@ LEFT JOIN civicrm_contribution contribution ON ( componentPayment.contribution_ 'contribution_recur_id' => $contributionParams['contribution_recur_id'], 'options' => array('limit' => 1), )); + if (!empty($contributionParams['contribution_recur_id'])) { + $recurringContribution = civicrm_api3('ContributionRecur', 'getsingle', array( + 'id' => $contributionParams['contribution_recur_id'], + )); + if (!empty($recurringContribution['campaign_id'])) { + // CRM-17718 the campaign id on the contribution recur record should get precedence. + $contributionParams['campaign_id'] = $recurringContribution['campaign_id']; + } + } + $contributionParams['skipLineItem'] = TRUE; $contributionParams['status_id'] = 'Pending'; $contributionParams['financial_type_id'] = $templateContribution['financial_type_id']; diff --git a/api/v3/Contribution.php b/api/v3/Contribution.php index dc1a9861ab..4ee8872a01 100644 --- a/api/v3/Contribution.php +++ b/api/v3/Contribution.php @@ -648,6 +648,16 @@ function _civicrm_api3_contribution_repeattransaction_spec(&$params) { 'name' => 'trxn_id', 'type' => CRM_Utils_Type::T_STRING, ); + $params['campaign_id'] = array( + 'title' => 'Campaign ID', + 'name' => 'campaign_id', + 'type' => CRM_Utils_Type::T_INT, + 'pseudoconstant' => array( + 'table' => 'civicrm_campaign', + 'keyColumn' => 'id', + 'labelColumn' => 'title', + ), + ); $params['payment_processor_id'] = array( 'description' => ts('Payment processor ID, will be loaded from contribution_recur if not provided'), 'title' => 'Payment processor ID', diff --git a/tests/phpunit/api/v3/ContributionTest.php b/tests/phpunit/api/v3/ContributionTest.php index 664f4f99d1..2c82baaeda 100644 --- a/tests/phpunit/api/v3/ContributionTest.php +++ b/tests/phpunit/api/v3/ContributionTest.php @@ -1451,8 +1451,6 @@ class api_v3_ContributionTest extends CiviUnitTestCase { */ public function testRepeatTransactionAlteredAmount() { $paymentProcessorID = $this->paymentProcessorCreate(); - $campaignID = $this->campaignCreate(); - $campaignID2 = $this->campaignCreate(); $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array( 'contact_id' => $this->_individualId, 'installments' => '12', @@ -1468,7 +1466,6 @@ class api_v3_ContributionTest extends CiviUnitTestCase { $this->_params, array( 'contribution_recur_id' => $contributionRecur['id'], - 'campaign_id' => $campaignID, )) ); @@ -1478,7 +1475,6 @@ class api_v3_ContributionTest extends CiviUnitTestCase { 'trxn_id' => uniqid(), 'total_amount' => '400', 'fee_amount' => 50, - 'campaign_id' => $campaignID2, )); $lineItemParams = array( 'entity_id' => $originalContribution['id'], @@ -1497,7 +1493,6 @@ class api_v3_ContributionTest extends CiviUnitTestCase { ); $this->callAPISuccessGetSingle('contribution', array( 'total_amount' => 400, - 'campaign_id' => $campaignID2, 'fee_amount' => 50, 'net_amount' => 350, )); @@ -1517,8 +1512,86 @@ class api_v3_ContributionTest extends CiviUnitTestCase { unset($expectedLineItem['id'], $expectedLineItem['entity_id']); unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']); $this->assertEquals($expectedLineItem, $lineItem2['values'][0]); + } - $this->quickCleanUpFinancialEntities(); + /** + * CRM-16397 test appropriate action if total amount has changed for single line items. + */ + public function testRepeatTransactionPassedInCampaign() { + $paymentProcessorID = $this->paymentProcessorCreate(); + $campaignID = $this->campaignCreate(); + $campaignID2 = $this->campaignCreate(); + $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array( + 'contact_id' => $this->_individualId, + 'installments' => '12', + 'frequency_interval' => '1', + 'amount' => '100', + 'contribution_status_id' => 1, + 'start_date' => '2012-01-01 00:00:00', + 'currency' => 'USD', + 'frequency_unit' => 'month', + 'payment_processor_id' => $paymentProcessorID, + )); + $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge( + $this->_params, + array( + 'contribution_recur_id' => $contributionRecur['id'], + 'campaign_id' => $campaignID, + )) + ); + + $this->callAPISuccess('contribution', 'repeattransaction', array( + 'original_contribution_id' => $originalContribution['id'], + 'contribution_status_id' => 'Completed', + 'trxn_id' => uniqid(), + 'campaign_id' => $campaignID2, + )); + + $this->callAPISuccessGetSingle('contribution', array( + 'total_amount' => 100, + 'campaign_id' => $campaignID2, + )); + } + + /** + * CRM-17718 campaign stored on contribution recur gets priority. + * + * This reflects the fact we permit people to update them. + */ + public function testRepeatTransactionUpdatedCampaign() { + $paymentProcessorID = $this->paymentProcessorCreate(); + $campaignID = $this->campaignCreate(); + $campaignID2 = $this->campaignCreate(); + $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array( + 'contact_id' => $this->_individualId, + 'installments' => '12', + 'frequency_interval' => '1', + 'amount' => '100', + 'contribution_status_id' => 1, + 'start_date' => '2012-01-01 00:00:00', + 'currency' => 'USD', + 'frequency_unit' => 'month', + 'payment_processor_id' => $paymentProcessorID, + 'campaign_id' => $campaignID, + )); + $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge( + $this->_params, + array( + 'contribution_recur_id' => $contributionRecur['id'], + 'campaign_id' => $campaignID2, + )) + ); + + $this->callAPISuccess('contribution', 'repeattransaction', array( + 'original_contribution_id' => $originalContribution['id'], + 'contribution_status_id' => 'Completed', + 'trxn_id' => uniqid(), + )); + + $this->callAPISuccessGetSingle('contribution', array( + 'total_amount' => 100, + 'campaign_id' => $campaignID, + )); } /** -- 2.25.1