Skip to content

Commit

Permalink
Merge pull request #9590 from JMAConsulting/CRM-19585-6
Browse files Browse the repository at this point in the history
[ready-for-core-team-review]CRM-19585, fixed tax issues
  • Loading branch information
monishdeb authored Dec 29, 2016
2 parents eac1f38 + 1ec0422 commit 02862ee
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 20 deletions.
6 changes: 4 additions & 2 deletions CRM/Price/BAO/PriceField.php
Original file line number Diff line number Diff line change
Expand Up @@ -590,11 +590,13 @@ public static function addQuickFormElement(
* Include inactive options.
* @param bool $reset
* Discard stored values.
* @param bool $isDefaultContributionPriceSet
* Discard tax amount calculation for price set = default_contribution_amount.
*
* @return array
* array of options
*/
public static function getOptions($fieldId, $inactiveNeeded = FALSE, $reset = FALSE) {
public static function getOptions($fieldId, $inactiveNeeded = FALSE, $reset = FALSE, $isDefaultContributionPriceSet = FALSE) {
static $options = array();
if ($reset) {
$options = array();
Expand All @@ -613,7 +615,7 @@ public static function getOptions($fieldId, $inactiveNeeded = FALSE, $reset = FA
// ToDo - Code for Hook Invoke

foreach ($options[$fieldId] as $priceFieldId => $priceFieldValues) {
if (isset($priceFieldValues['financial_type_id']) && array_key_exists($priceFieldValues['financial_type_id'], $taxRates)) {
if (isset($priceFieldValues['financial_type_id']) && array_key_exists($priceFieldValues['financial_type_id'], $taxRates) && !$isDefaultContributionPriceSet) {
$options[$fieldId][$priceFieldId]['tax_rate'] = $taxRates[$priceFieldValues['financial_type_id']];
$taxAmount = CRM_Contribute_BAO_Contribution_Utils::calculateTaxAmount($priceFieldValues['amount'], $options[$fieldId][$priceFieldId]['tax_rate']);
$options[$fieldId][$priceFieldId]['tax_amount'] = $taxAmount['tax_amount'];
Expand Down
38 changes: 20 additions & 18 deletions CRM/Price/BAO/PriceSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,11 @@ public static function getSetDetail($setID, $required = TRUE, $validOnly = FALSE

$dao = CRM_Core_DAO::executeQuery($sql, $params);

$isDefaultContributionPriceSet = FALSE;
if ('default_contribution_amount' == CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $setID)) {
$isDefaultContributionPriceSet = TRUE;
}

$visibility = CRM_Core_PseudoConstant::visibility('name');
while ($dao->fetch()) {
$fieldID = $dao->id;
Expand All @@ -587,7 +592,7 @@ public static function getSetDetail($setID, $required = TRUE, $validOnly = FALSE
}
$setTree[$setID]['fields'][$fieldID][$field] = $dao->$field;
}
$setTree[$setID]['fields'][$fieldID]['options'] = CRM_Price_BAO_PriceField::getOptions($fieldID, FALSE);
$setTree[$setID]['fields'][$fieldID]['options'] = CRM_Price_BAO_PriceField::getOptions($fieldID, FALSE, FALSE, $isDefaultContributionPriceSet);
}

// also get the pre and post help from this price set
Expand Down Expand Up @@ -798,20 +803,19 @@ public static function processAmount($fields, &$params, &$lineItem, $component =
$firstOption = reset($field['options']);
$params["price_{$id}"] = array($firstOption['id'] => $params["price_{$id}"]);
CRM_Price_BAO_LineItem::format($id, $params, $field, $lineItem);
if (CRM_Utils_Array::value('tax_rate', $field['options'][key($field['options'])])) {
$lineItem = self::setLineItem($field, $lineItem, key($field['options']));
$totalTax += $field['options'][key($field['options'])]['tax_amount'] * $lineItem[key($field['options'])]['qty'];
}
if (CRM_Utils_Array::value('name', $field['options'][key($field['options'])]) == 'contribution_amount') {
$optionValueId = key($field['options']);

if (CRM_Utils_Array::value('name', $field['options'][$optionValueId]) == 'contribution_amount') {
$taxRates = CRM_Core_PseudoConstant::getTaxRates();
if (array_key_exists($params['financial_type_id'], $taxRates)) {
$field['options'][key($field['options'])]['tax_rate'] = $taxRates[$params['financial_type_id']];
$taxAmount = CRM_Contribute_BAO_Contribution_Utils::calculateTaxAmount($field['options'][key($field['options'])]['amount'], $field['options'][key($field['options'])]['tax_rate']);
$field['options'][key($field['options'])]['tax_amount'] = round($taxAmount['tax_amount'], 2);
$lineItem = self::setLineItem($field, $lineItem, key($field['options']));
$totalTax += $field['options'][key($field['options'])]['tax_amount'] * $lineItem[key($field['options'])]['qty'];
$taxAmount = CRM_Contribute_BAO_Contribution_Utils::calculateTaxAmount($field['options'][$optionValueId]['amount'], $field['options'][$optionValueId]['tax_rate']);
$field['options'][$optionValueId]['tax_amount'] = round($taxAmount['tax_amount'], 2);
}
}
if (CRM_Utils_Array::value('tax_rate', $field['options'][$optionValueId])) {
$lineItem = self::setLineItem($field, $lineItem, $optionValueId, $totalTax);
}
$totalPrice += $lineItem[$firstOption['id']]['line_total'] + CRM_Utils_Array::value('tax_amount', $lineItem[key($field['options'])]);
break;

Expand All @@ -825,8 +829,7 @@ public static function processAmount($fields, &$params, &$lineItem, $component =

CRM_Price_BAO_LineItem::format($id, $params, $field, $lineItem, $amount_override);
if (CRM_Utils_Array::value('tax_rate', $field['options'][$optionValueId])) {
$lineItem = self::setLineItem($field, $lineItem, $optionValueId);
$totalTax += $field['options'][$optionValueId]['tax_amount'];
$lineItem = self::setLineItem($field, $lineItem, $optionValueId, $totalTax);
if (CRM_Utils_Array::value('field_title', $lineItem[$optionValueId]) == 'Membership Amount') {
$lineItem[$optionValueId]['line_total'] = $lineItem[$optionValueId]['unit_price'] = CRM_Utils_Rule::cleanMoney($lineItem[$optionValueId]['line_total'] - $lineItem[$optionValueId]['tax_amount']);
}
Expand All @@ -849,8 +852,7 @@ public static function processAmount($fields, &$params, &$lineItem, $component =

CRM_Price_BAO_LineItem::format($id, $params, $field, $lineItem);
if (CRM_Utils_Array::value('tax_rate', $field['options'][$optionValueId])) {
$lineItem = self::setLineItem($field, $lineItem, $optionValueId);
$totalTax += $field['options'][$optionValueId]['tax_amount'];
$lineItem = self::setLineItem($field, $lineItem, $optionValueId, $totalTax);
}
$totalPrice += $lineItem[$optionValueId]['line_total'] + CRM_Utils_Array::value('tax_amount', $lineItem[$optionValueId]);
if (
Expand All @@ -867,8 +869,7 @@ public static function processAmount($fields, &$params, &$lineItem, $component =
CRM_Price_BAO_LineItem::format($id, $params, $field, $lineItem);
foreach ($params["price_{$id}"] as $optionId => $option) {
if (CRM_Utils_Array::value('tax_rate', $field['options'][$optionId])) {
$lineItem = self::setLineItem($field, $lineItem, $optionId);
$totalTax += $field['options'][$optionId]['tax_amount'];
$lineItem = self::setLineItem($field, $lineItem, $optionId, $totalTax);
}
$totalPrice += $lineItem[$optionId]['line_total'] + CRM_Utils_Array::value('tax_amount', $lineItem[$optionId]);
if (
Expand Down Expand Up @@ -1660,10 +1661,11 @@ public static function copyPriceSet($baoName, $id, $newId) {
* @param array $field
* @param array $lineItem
* @param int $optionValueId
* @param float $totalTax
*
* @return array
*/
public static function setLineItem($field, $lineItem, $optionValueId) {
public static function setLineItem($field, $lineItem, $optionValueId, &$totalTax) {
if ($field['html_type'] == 'Text') {
$taxAmount = $field['options'][$optionValueId]['tax_amount'] * $lineItem[$optionValueId]['qty'];
}
Expand All @@ -1673,7 +1675,7 @@ public static function setLineItem($field, $lineItem, $optionValueId) {
$taxRate = $field['options'][$optionValueId]['tax_rate'];
$lineItem[$optionValueId]['tax_amount'] = $taxAmount;
$lineItem[$optionValueId]['tax_rate'] = $taxRate;

$totalTax += $taxAmount;
return $lineItem;
}

Expand Down
76 changes: 76 additions & 0 deletions tests/phpunit/CRM/Contribute/Form/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -775,4 +775,80 @@ protected function getCreditCardParams() {
);
}

/**
* Test the submit function for FT with tax.
*/
public function testSubmitSaleTax() {
$this->enableTaxAndInvoicing();
$this->relationForFinancialTypeWithFinancialAccount($this->_financialTypeId);
$form = new CRM_Contribute_Form_Contribution();

$form->testSubmit(array(
'total_amount' => 100,
'financial_type_id' => $this->_financialTypeId,
'receive_date' => '04/21/2015',
'receive_date_time' => '11:27PM',
'contact_id' => $this->_individualId,
'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
'contribution_status_id' => 1,
'price_set_id' => 0,
),
CRM_Core_Action::ADD
);
$contribution = $this->callAPISuccessGetSingle('Contribution',
array(
'contact_id' => $this->_individualId,
'return' => array('tax_amount', 'total_amount'),
)
);
$this->assertEquals(110, $contribution['total_amount']);
$this->assertEquals(10, $contribution['tax_amount']);
$this->callAPISuccessGetCount('FinancialTrxn', array(), 1);
$this->callAPISuccessGetCount('FinancialItem', array(), 2);
$lineItem = $this->callAPISuccessGetSingle('LineItem', array('contribution_id' => $contribution['id']));
$this->assertEquals(100, $lineItem['line_total']);
$this->assertEquals(10, $lineItem['tax_amount']);
}

/**
* Test the submit function for FT without tax.
*/
public function testSubmitWithOutSaleTax() {
$this->enableTaxAndInvoicing();
$this->relationForFinancialTypeWithFinancialAccount($this->_financialTypeId);
$form = new CRM_Contribute_Form_Contribution();

$form->testSubmit(array(
'total_amount' => 100,
'financial_type_id' => 3,
'receive_date' => '04/21/2015',
'receive_date_time' => '11:27PM',
'contact_id' => $this->_individualId,
'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
'contribution_status_id' => 1,
'price_set_id' => 0,
),
CRM_Core_Action::ADD
);
$contribution = $this->callAPISuccessGetSingle('Contribution',
array(
'contact_id' => $this->_individualId,
'return' => array('tax_amount', 'total_amount'),
)
);
$this->assertEquals(100, $contribution['total_amount']);
$this->assertEquals(NULL, $contribution['tax_amount']);
$this->callAPISuccessGetCount('FinancialTrxn', array(), 1);
$this->callAPISuccessGetCount('FinancialItem', array(), 1);
$lineItem = $this->callAPISuccessGetSingle(
'LineItem',
array(
'contribution_id' => $contribution['id'],
'return' => array('line_total', 'tax_amount'),
)
);
$this->assertEquals(100, $lineItem['line_total']);
$this->assertTrue(empty($lineItem['tax_amount']));
}

}
26 changes: 26 additions & 0 deletions tests/phpunit/CiviTest/CiviUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3650,6 +3650,32 @@ protected function enableTaxAndInvoicing($params = array()) {
return Civi::settings()->set('contribution_invoice_settings', $contributeSetting);
}

/**
* Add Sales Tax relation for financial type with financial account.
*
* @param int $financialTypeId
*
* @return obj
*/
protected function relationForFinancialTypeWithFinancialAccount($financialTypeId) {
$params = array(
'name' => 'Sales tax account ' . substr(sha1(rand()), 0, 4),
'financial_account_type_id' => key(CRM_Core_PseudoConstant::accountOptionValues('financial_account_type', NULL, " AND v.name LIKE 'Liability' ")),
'is_deductible' => 1,
'is_tax' => 1,
'tax_rate' => 10,
'is_active' => 1,
);
$account = CRM_Financial_BAO_FinancialAccount::add($params);
$entityParams = array(
'entity_table' => 'civicrm_financial_type',
'account_relationship' => key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Sales Tax Account is' ")),
'entity_id' => $financialTypeId,
'financial_account_id' => $account->id,
);
return CRM_Financial_BAO_FinancialTypeAccount::add($entityParams);
}

/**
* Create price set with contribution test for test setup.
*
Expand Down

0 comments on commit 02862ee

Please sign in to comment.