-
-
Notifications
You must be signed in to change notification settings - Fork 825
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
CRM-16228, fixed tax calculation as per https://github.com/civicrm/ci… #9643
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -851,4 +851,82 @@ public function testSubmitWithOutSaleTax() { | |
$this->assertTrue(empty($lineItem['tax_amount'])); | ||
} | ||
|
||
/** | ||
* Test form submission CRM-16228. | ||
*/ | ||
public function testSubmitContributionPageWithPriceSetHavingTaxCRM16228() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because unit tests are so important, it's also important that unit tests have very clear names. A name like "testContributionPageWithPriceSetHavingTaxHasCorrectTaxAndTotalAmounts" would be better than "testSubmitContributionPageWithPriceSetHavingTaxCRM16228" because it plainly says what's being tested, without requiring someone to look-up and read through a JIRA ticket. This is an easy fix to improve our ability to keep writing unit tests on financial code. |
||
$this->enableTaxAndInvoicing(); | ||
$financialType = $this->createFinancialType(); | ||
$this->relationForFinancialTypeWithFinancialAccount($financialType['id'], 5); | ||
$priceSetID = $this->createContributionPriceSet($financialType['id']); | ||
$priceFieldId = $this->addTaxTextPriceFields($priceSetID, $financialType['id'], '16.85'); | ||
$form = new CRM_Contribute_Form_Contribution(); | ||
$submitParams = array( | ||
'financial_type_id' => $financialType['id'], | ||
'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' => $priceSetID, | ||
'price_' . $priceFieldId => 10, | ||
); | ||
$form->testSubmit($submitParams, CRM_Core_Action::ADD); | ||
$contribution = $this->callAPISuccessGetSingle('Contribution', | ||
array( | ||
'contact_id' => $this->_individualId, | ||
'return' => array('tax_amount', 'total_amount'), | ||
) | ||
); | ||
$this->assertEquals($contribution['tax_amount'], '8.43', 'Wrong Tax amount is calculated and stored.'); | ||
$this->assertEquals($contribution['total_amount'], '176.93', 'Total amount no matching.'); | ||
$lineItem = $this->callAPISuccessGetSingle('LineItem', array( | ||
'contribution_id' => $contribution['id'], | ||
)); | ||
$this->assertEquals($lineItem['tax_amount'], '8.43', 'Wrong Tax amount is calculated and stored.'); | ||
$this->assertEquals($lineItem['line_total'], '168.50', 'Total amount no matching.'); | ||
} | ||
|
||
/** | ||
* Test form submission CRM-16228. | ||
*/ | ||
public function testSubmitContributionPageWithPriceSetHaving2PriceFieldTaxCRM16228() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As mentioned above, it would be very helpful to rename this test to "testContributionPageWithPriceSetHaving2PriceFieldTaxHasCorrectTaxAndTotalAmounts" (because: plainly says what's being tested; doesn't require examining a JIRA ticket to understand). An important and easy fix to improve our ability to keep writing unit tests on financial code. |
||
$this->enableTaxAndInvoicing(); | ||
$financialType = $this->createFinancialType(); | ||
$this->relationForFinancialTypeWithFinancialAccount($financialType['id'], 5); | ||
$priceSetID = $this->createContributionPriceSet($financialType['id']); | ||
$priceFieldId = $this->addTaxTextPriceFields($priceSetID, $financialType['id'], '16.85'); | ||
$priceFieldId2 = $this->addTaxTextPriceFields($priceSetID, $financialType['id'], '13.33'); | ||
$form = new CRM_Contribute_Form_Contribution(); | ||
$submitParams = array( | ||
'financial_type_id' => $financialType['id'], | ||
'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' => $priceSetID, | ||
'price_' . $priceFieldId => 10, | ||
'price_' . $priceFieldId2 => 100, | ||
); | ||
$form->testSubmit($submitParams, CRM_Core_Action::ADD); | ||
$contribution = $this->callAPISuccessGetSingle('Contribution', | ||
array( | ||
'contact_id' => $this->_individualId, | ||
'return' => array('tax_amount', 'total_amount'), | ||
) | ||
); | ||
$this->assertEquals($contribution['tax_amount'], '75.08', 'Wrong Tax amount is calculated and stored.'); | ||
$this->assertEquals($contribution['total_amount'], '1576.58', 'Total amount no matching.'); | ||
$lineItem = $this->callAPISuccess('LineItem', 'get', array( | ||
'contribution_id' => $contribution['id'], | ||
)); | ||
$lineTotal = array('1333', '168.50'); | ||
$lineTaxAmount = array('66.65', '8.43'); | ||
foreach ($lineItem['values'] as $line) { | ||
$this->assertEquals(CRM_Utils_Array::value('tax_amount', $line), array_pop($lineTaxAmount), 'Wrong Tax amount is calculated and stored.'); | ||
$this->assertEquals($line['line_total'], array_pop($lineTotal), 'Total amount no matching.'); | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1416,4 +1416,86 @@ public function addPriceFields(&$params) { | |
); | ||
} | ||
|
||
/** | ||
* Test form submission CRM-16228. | ||
*/ | ||
public function testSubmitContributionPageWithPriceSetHavingTaxCRM16228() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to above comments, it would be very helpful to rename this test to "testContributionPageWithPriceSetHavingTaxHasCorrectTaxAndTotalAmounts" . |
||
$this->_priceSetParams['is_quick_config'] = 0; | ||
$this->enableTaxAndInvoicing(); | ||
$financialType = $this->createFinancialType(); | ||
$financialAccount = $this->relationForFinancialTypeWithFinancialAccount($financialType['id'], 5); | ||
$this->setUpContributionPage(); | ||
$submitParams = array( | ||
'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']), | ||
'id' => (int) $this->_ids['contribution_page'], | ||
'first_name' => 'Billy', | ||
'last_name' => 'Gruff', | ||
'email' => 'billy@goat.gruff', | ||
'is_pay_later' => TRUE, | ||
); | ||
$priceSetID = reset($this->_ids['price_set']); | ||
$priceFieldId = $this->addTaxTextPriceFields($priceSetID, $financialType['id'], '16.85'); | ||
$submitParams['price_' . $priceFieldId] = 100; | ||
$this->callAPISuccess('contribution_page', 'submit', $submitParams); | ||
$contribution = $this->callAPISuccessGetSingle('contribution', array( | ||
'contribution_page_id' => $this->_ids['contribution_page'], | ||
'contribution_status_id' => 2, | ||
'return' => array('total_amount', 'tax_amount'), | ||
)); | ||
$this->assertEquals($contribution['tax_amount'], '84.25', 'Wrong Tax amount is calculated and stored.'); | ||
// 10 + (100 * 16.85) + (100 * 16.85 * 0.05) | ||
$this->assertEquals($contribution['total_amount'], '1779.25', 'Total amount no matching.'); | ||
$lineItem = $this->callAPISuccess('LineItem', 'get', array( | ||
'contribution_id' => $contribution['id'], | ||
)); | ||
$lineTotal = array(1685, 10); | ||
$lineTaxAmount = array('84.25', NULL); | ||
foreach ($lineItem['values'] as $line) { | ||
$this->assertEquals(CRM_Utils_Array::value('tax_amount', $line), array_pop($lineTaxAmount), 'Wrong Tax amount is calculated and stored.'); | ||
$this->assertEquals($line['line_total'], array_pop($lineTotal), 'Total amount no matching.'); | ||
} | ||
} | ||
|
||
/** | ||
* Test form submission CRM-16228. | ||
*/ | ||
public function testSubmitContributionPageWithPriceSetHaving2PriceFieldTaxCRM16228() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to above comments, it would be very helpful to rename this test to "testContributionPageWithPriceSetHaving2PriveFieldTaxHasCorrectTaxAndTotalAmounts" . |
||
$this->_priceSetParams['is_quick_config'] = 0; | ||
$this->enableTaxAndInvoicing(); | ||
$financialType = $this->createFinancialType(); | ||
$financialAccount = $this->relationForFinancialTypeWithFinancialAccount($financialType['id'], 5); | ||
$this->setUpContributionPage(); | ||
$submitParams = array( | ||
'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']), | ||
'id' => (int) $this->_ids['contribution_page'], | ||
'first_name' => 'Billy', | ||
'last_name' => 'Gruff', | ||
'email' => 'billy@goat.gruff', | ||
'is_pay_later' => TRUE, | ||
); | ||
$priceSetID = reset($this->_ids['price_set']); | ||
$priceFieldId = $this->addTaxTextPriceFields($priceSetID, $financialType['id'], '16.85'); | ||
$submitParams['price_' . $priceFieldId] = 100; | ||
$priceFieldId = $this->addTaxTextPriceFields($priceSetID, $financialType['id'], '13.33'); | ||
$submitParams['price_' . $priceFieldId] = 10; | ||
$this->callAPISuccess('contribution_page', 'submit', $submitParams); | ||
$contribution = $this->callAPISuccessGetSingle('contribution', array( | ||
'contribution_page_id' => $this->_ids['contribution_page'], | ||
'contribution_status_id' => 2, | ||
'return' => array('total_amount', 'tax_amount'), | ||
)); | ||
$this->assertEquals($contribution['tax_amount'], '90.92', 'Wrong Tax amount is calculated and stored.'); | ||
// 10 + (100 * 16.85) + (100 * 16.85 * 0.05) + (10 * 13.33) + (10 * 13.33 * 0.05) | ||
$this->assertEquals($contribution['total_amount'], '1919.22', 'Total amount no matching.'); | ||
$lineItem = $this->callAPISuccess('LineItem', 'get', array( | ||
'contribution_id' => $contribution['id'], | ||
)); | ||
$lineTotal = array('133.30', '1685', '10'); | ||
$lineTaxAmount = array('6.67', '84.25', NULL); | ||
foreach ($lineItem['values'] as $line) { | ||
$this->assertEquals(CRM_Utils_Array::value('tax_amount', $line), array_pop($lineTaxAmount), 'Wrong Tax amount is calculated and stored.'); | ||
$this->assertEquals($line['line_total'], array_pop($lineTotal), 'Total amount no matching.'); | ||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this an odd place to have $htmlType? A static utility function, which calculates a tax based on an amount and a rate, shouldn't need to know something like $htmlType. I see that it's used to decide whether or not to round the amount, but why does this rounding happen in this function -- or specifically, why does it happen only for certain values of $htmlType? Shouldn't rounding happen somewhere later, just at the time this value actually needs to be rounded (e.g., at display time)?