Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failure to calculate net_amount correctly on update when fee_amount is not in the parameters. #13177

Merged
merged 1 commit into from
Nov 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ public static function calculateMissingAmountParams(&$params, $contributionID) {
// net_amount may need adjusting.
$contribution = civicrm_api3('Contribution', 'getsingle', array(
'id' => $contributionID,
'return' => array('total_amount', 'net_amount'),
'return' => array('total_amount', 'net_amount', 'fee_amount'),
));
$totalAmount = isset($params['total_amount']) ? $params['total_amount'] : CRM_Utils_Array::value('total_amount', $contribution);
$feeAmount = isset($params['fee_amount']) ? $params['fee_amount'] : CRM_Utils_Array::value('fee_amount', $contribution);
Expand Down
59 changes: 59 additions & 0 deletions tests/phpunit/api/v3/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1628,6 +1628,65 @@ public function testCreateUpdateContribution() {
$this->assertAPISuccess($result);
}

/**
* Check that net_amount is updated when a contribution is updated.
*
* Update fee amount AND total amount, just fee amount, just total amount
* and neither to check that net_amount is keep updated.
*/
public function testUpdateContributionNetAmountVariants() {
$contributionID = $this->contributionCreate(['contact_id' => $this->individualCreate()]);

$this->callAPISuccess('Contribution', 'create', [
'id' => $contributionID,
'total_amount' => 90,
'fee_amount' => 6,
]);
$contribution = $this->callAPISuccessGetSingle('Contribution', [
'id' => $contributionID,
'return' => ['net_amount', 'fee_amount', 'total_amount'],
]);
$this->assertEquals(6, $contribution['fee_amount']);
$this->assertEquals(90, $contribution['total_amount']);
$this->assertEquals(84, $contribution['net_amount']);

$this->callAPISuccess('Contribution', 'create', [
'id' => $contributionID,
'fee_amount' => 3,
]);
$contribution = $this->callAPISuccessGetSingle('Contribution', [
'id' => $contributionID,
'return' => ['net_amount', 'fee_amount', 'total_amount'],
]);
$this->assertEquals(3, $contribution['fee_amount']);
$this->assertEquals(90, $contribution['total_amount']);
$this->assertEquals(87, $contribution['net_amount']);

$this->callAPISuccess('Contribution', 'create', [
'id' => $contributionID,
'total_amount' => 200,
]);
$contribution = $this->callAPISuccessGetSingle('Contribution', [
'id' => $contributionID,
'return' => ['net_amount', 'fee_amount', 'total_amount'],
]);
$this->assertEquals(3, $contribution['fee_amount']);
$this->assertEquals(200, $contribution['total_amount']);
$this->assertEquals(197, $contribution['net_amount']);

$this->callAPISuccess('Contribution', 'create', [
'id' => $contributionID,
'payment_instrument' => 'Cash'
]);
$contribution = $this->callAPISuccessGetSingle('Contribution', [
'id' => $contributionID,
'return' => ['net_amount', 'fee_amount', 'total_amount'],
]);
$this->assertEquals(3, $contribution['fee_amount']);
$this->assertEquals(200, $contribution['total_amount']);
$this->assertEquals(197, $contribution['net_amount']);
}

/**
* Attempt (but fail) to delete a contribution without parameters.
*/
Expand Down