return $amount;
}
+ /**
+ * Calculate net amount.
+ *
+ * @param array $netAmount
+ *
+ * @param float $taxAmount
+ *
+ * @return array
+ */
+ public static function calculateNetAmount($netAmount, $taxAmount) {
+ if ($taxAmount) {
+ $netAmount -= $taxAmount;
+ }
+ return CRM_Utils_Money::format($netAmount, NULL, '%a');
+ }
+
}
}
if (isset($defaults['net_amount'])) {
- $defaults['net_amount'] = CRM_Utils_Money::format($defaults['net_amount'], NULL, '%a');
+ $defaults['net_amount'] = CRM_Contribute_BAO_Contribution::calculateNetAmount($defaults['net_amount'], $defaults['tax_amount']);
}
if ($this->_contributionType) {
if (!empty($fields['total_amount']) && (!empty($fields['net_amount']) || !empty($fields['fee_amount']))) {
$sum = CRM_Utils_Rule::cleanMoney($fields['net_amount']) + CRM_Utils_Rule::cleanMoney($fields['fee_amount']);
- // For taxable contribution we need to deduct taxable amount from
- // (net amount + fee amount) before comparing it with total amount
- if (!empty($self->_values['tax_amount'])) {
- $componentDetails = CRM_Contribute_BAO_Contribution::getComponentDetails($self->_id);
- if (!(CRM_Utils_Array::value('membership', $componentDetails) ||
- CRM_Utils_Array::value('participant', $componentDetails))
- ) {
- $sum = CRM_Utils_Money::format($sum - $self->_values['tax_amount'], NULL, '%a');
- }
- }
if (CRM_Utils_Rule::cleanMoney($fields['total_amount']) != $sum) {
$errors['total_amount'] = ts('The sum of fee amount and net amount must be equal to total amount');
}
}
/**
+<<<<<<< HEAD
* Test calculateFinancialItemAmount().
*/
public function testcalculateFinancialItemAmount() {
}
}
+ /**
+ * Test calculateNetAmount.
+ */
+ public function testcalculateNetAmount() {
+ $testParams = array(
+ array(
+ 'net_amount' => 100,
+ 'tax_amount' => 10,
+ 'expectedNetAmount' => 90,
+ ),
+ array(
+ 'net_amount' => 200,
+ 'tax_amount' => 0,
+ 'expectedNetAmount' => 200,
+ ),
+ array(
+ 'net_amount' => 300,
+ 'tax_amount' => NULL,
+ 'expectedNetAmount' => 300,
+ ),
+ array(
+ 'net_amount' => -100,
+ 'tax_amount' => 20,
+ 'expectedNetAmount' => -120,
+ ),
+ );
+
+ foreach ($testParams as $params) {
+ $netAmount = CRM_Contribute_BAO_Contribution::calculateNetAmount($params['net_amount'], $params['tax_amount']);
+ $this->assertEquals($netAmount, $params['expectedNetAmount'], 'Invalid Net amount.');
+ }
+ }
+
}