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

[ready-for-core-team-review]CRM-19585, created function to calculate financial item amount when co… #9574

Merged
merged 1 commit into from
Dec 28, 2016
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
31 changes: 31 additions & 0 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -5336,4 +5336,35 @@ public static function recordAlwaysAccountsReceivable(&$trxnParams, $contributio
$trxnParams['from_financial_account_id'] = $params['to_financial_account_id'];
}

/**
* Calculate financial item amount when contribution is updated.
*
* @param array $params
* contribution params
* @param array $amountParams
*
* @param string $context
*
* @return float
*/
public static function calculateFinancialItemAmount($params, $amountParams, $context) {
if (!empty($params['is_quick_config'])) {
$amount = $amountParams['item_amount'];
if (!$amount) {
$amount = $params['total_amount'];
if ($context === NULL) {
$amount -= CRM_Utils_Array::value('tax_amount', $params, 0);
}
}
}
else {
$amount = $amountParams['line_total'];
if ($context == 'changedAmount') {
$amount -= $amountParams['previous_line_total'];
}
$amount *= $amountParams['diff'];
}
return $amount;
}

}
68 changes: 68 additions & 0 deletions tests/phpunit/CRM/Contribute/BAO/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -937,4 +937,72 @@ public function testAllowUpdateRevenueRecognitionDate() {
$this->assertFalse($allowUpdate);
}

/**
* Test calculateFinancialItemAmount().
*/
public function testcalculateFinancialItemAmount() {
$testParams = array(
array(
'params' => array(),
'amountParams' => array(
'line_total' => 100,
'previous_line_total' => 300,
'diff' => 1,
),
'context' => 'changedAmount',
'expectedItemAmount' => -200,
),
array(
'params' => array(),
'amountParams' => array(
'line_total' => 100,
'previous_line_total' => 100,
'diff' => -1,
),
'context' => 'changePaymentInstrument',
'expectedItemAmount' => -100,
),
array(
'params' => array(
'is_quick_config' => TRUE,
'total_amount' => 110,
'tax_amount' => 10,
),
'amountParams' => array(
'item_amount' => 100,
),
'context' => 'changedAmount',
'expectedItemAmount' => 100,
),
array(
'params' => array(
'is_quick_config' => TRUE,
'total_amount' => 110,
'tax_amount' => 10,
),
'amountParams' => array(
'item_amount' => NULL,
),
'context' => 'changedAmount',
'expectedItemAmount' => 110,
),
array(
'params' => array(
'is_quick_config' => TRUE,
'total_amount' => 110,
'tax_amount' => 10,
),
'amountParams' => array(
'item_amount' => NULL,
),
'context' => NULL,
'expectedItemAmount' => 100,
),
);
foreach ($testParams as $params) {
$itemAmount = CRM_Contribute_BAO_Contribution::calculateFinancialItemAmount($params['params'], $params['amountParams'], $params['context']);
$this->assertEquals($itemAmount, $params['expectedItemAmount'], 'Invalid Financial Item amount.');
}
}

}