From e5b9a6bdb1ce7cebd3d519f213eb67931973fdb4 Mon Sep 17 00:00:00 2001 From: Matthew Wire Date: Tue, 14 Sep 2021 16:26:24 +0100 Subject: [PATCH] If a template contribution is updated we need to update the amount on the recurring contribution --- CRM/Contribute/BAO/Contribution.php | 12 +++++- CRM/Contribute/BAO/ContributionRecur.php | 31 ++++++++++++++ .../Contribute/BAO/ContributionRecurTest.php | 41 +++++++++++++++++++ 3 files changed, 83 insertions(+), 1 deletion(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 4601737ae0..3501d7e790 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -23,7 +23,7 @@ use Civi\Api4\PledgePayment; * @package CRM * @copyright CiviCRM LLC https://civicrm.org/licensing */ -class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution { +class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution implements Civi\Test\HookInterface { /** * Static field for all the contribution information that we can potentially import @@ -607,6 +607,16 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution { return $contribution; } + /** + * Event fired after modifying a contribution. + * @param \Civi\Core\Event\PostEvent $event + */ + public static function self_hook_civicrm_post(\Civi\Core\Event\PostEvent $event) { + if ($event->action === 'edit') { + CRM_Contribute_BAO_ContributionRecur::updateOnTemplateUpdated($event->object); + } + } + /** * Get the values for pseudoconstants for name->value and reverse. * diff --git a/CRM/Contribute/BAO/ContributionRecur.php b/CRM/Contribute/BAO/ContributionRecur.php index 980782b6a0..c119082f82 100644 --- a/CRM/Contribute/BAO/ContributionRecur.php +++ b/CRM/Contribute/BAO/ContributionRecur.php @@ -977,6 +977,37 @@ INNER JOIN civicrm_contribution con ON ( con.id = mp.contribution_id ) civicrm_api3('ContributionRecur', 'create', $params); } + /** + * If a template contribution is updated we need to update the amount on the recurring contribution. + * + * @param \CRM_Contribute_DAO_Contribution $contribution + * + * @throws \API_Exception + * @throws \Civi\API\Exception\UnauthorizedException + */ + public static function updateOnTemplateUpdated(CRM_Contribute_DAO_Contribution $contribution) { + if (empty($contribution->contribution_recur_id)) { + return; + } + $contributionRecur = ContributionRecur::get(FALSE) + ->addWhere('id', '=', $contribution->contribution_recur_id) + ->execute() + ->first(); + + if ($contribution->total_amount === NULL) { + $contribution->find(TRUE); + } + + if (!CRM_Utils_Money::equals($contributionRecur['amount'], $contribution->total_amount, $contribution->currency)) { + ContributionRecur::update(FALSE) + ->addValue('amount', $contribution->total_amount) + ->addValue('currency', $contribution->currency) + ->addValue('modified_date', 'now') + ->addWhere('id', '=', $contributionRecur['id']) + ->execute(); + } + } + /** * Is this recurring contribution now complete. * diff --git a/tests/phpunit/CRM/Contribute/BAO/ContributionRecurTest.php b/tests/phpunit/CRM/Contribute/BAO/ContributionRecurTest.php index 6a370f248a..c33977a332 100644 --- a/tests/phpunit/CRM/Contribute/BAO/ContributionRecurTest.php +++ b/tests/phpunit/CRM/Contribute/BAO/ContributionRecurTest.php @@ -344,6 +344,47 @@ class CRM_Contribute_BAO_ContributionRecurTest extends CiviUnitTestCase { $this->assertEquals('AUD', $repeatContribution['values'][$repeatContribution['id']]['currency']); } + /** + * Test that is_template contribution is used where available + * + * @throws \API_Exception + * @throws \CiviCRM_API3_Exception + * @throws \Civi\API\Exception\UnauthorizedException + */ + public function testTemplateContributionUpdatesRecur(): void { + $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $this->_params); + $contributionRecur = reset($contributionRecur['values']); + // Create the template + $templateContrib = $this->callAPISuccess('Contribution', 'create', [ + 'contribution_recur_id' => $contributionRecur['id'], + 'total_amount' => '3.00', + 'financial_type_id' => 1, + 'source' => 'Template Contribution', + 'payment_instrument_id' => 1, + 'currency' => 'AUD', + 'contact_id' => $this->individualCreate(), + 'contribution_status_id' => 1, + 'receive_date' => 'yesterday', + 'is_template' => 1, + ]); + $this->callAPISuccess('Contribution', 'create', [ + 'id' => $templateContrib['id'], + 'contribution_recur_id' => $contributionRecur['id'], + 'total_amount' => '2.00', + 'currency' => 'USD', + ]); + $updatedContributionRecur = \Civi\Api4\ContributionRecur::get(FALSE) + ->addWhere('id', '=', $contributionRecur['id']) + ->execute() + ->first(); + $this->assertEquals('USD', $updatedContributionRecur['currency']); + $this->assertEquals('2.00', $updatedContributionRecur['amount']); + $this->assertGreaterThan( + strtotime($contributionRecur['modified_date']), + strtotime($updatedContributionRecur['modified_date']) + ); + } + /** * Test to check if correct membership is auto renewed. * -- 2.25.1