From b8e45de5fcd81d32c80ecc00090fb06570131edc Mon Sep 17 00:00:00 2001 From: Omar abu hussein Date: Wed, 14 Nov 2018 23:06:56 +0200 Subject: [PATCH] http://dev/core#193: Ensure that tax amount is calculated when checking for order API line items --- CRM/Contribute/BAO/Contribution.php | 5 +- tests/phpunit/api/v3/OrderTest.php | 100 ++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 2 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 2ce7c31a30..da0d03a5ce 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -4939,7 +4939,8 @@ WHERE eft.financial_trxn_id IN ({$trxnId}, {$baseTrxnId['financialTrxnId']}) } /** - * Function to check line items. + * Checks if line items total amounts + * match the contribution total amount. * * @param array $params * array of order params. @@ -4955,7 +4956,7 @@ WHERE eft.financial_trxn_id IN ({$trxnId}, {$baseTrxnId['financialTrxnId']}) if (empty($item['financial_type_id'])) { $item['financial_type_id'] = $params['financial_type_id']; } - $lineItemAmount += $item['line_total']; + $lineItemAmount += $item['line_total'] + CRM_Utils_Array::value('tax_amount', $item, 0.00); } } diff --git a/tests/phpunit/api/v3/OrderTest.php b/tests/phpunit/api/v3/OrderTest.php index f164018596..b3902e4eb7 100644 --- a/tests/phpunit/api/v3/OrderTest.php +++ b/tests/phpunit/api/v3/OrderTest.php @@ -532,4 +532,104 @@ class api_v3_OrderTest extends CiviUnitTestCase { )); } + /** + * @expectedException CiviCRM_API3_Exception + * @expectedExceptionMessage Line item total doesn't match with total amount. + */ + public function testCreateOrderIfTotalAmountDoesNotMatchLineItemsAmountsIfNoTaxSupplied() { + $params = [ + 'contact_id' => $this->_individualId, + 'receive_date' => '2018-01-01', + 'total_amount' => 50, + 'financial_type_id' => $this->_financialTypeId, + 'contribution_status_id' => 1, + 'line_items' => [ + 0 => [ + 'line_item' => [ + '0' => [ + 'price_field_id' => 1, + 'price_field_value_id' => 1, + 'label' => 'Test 1', + 'field_title' => 'Test 1', + 'qty' => 1, + 'unit_price' => 40, + 'line_total' => 40, + 'financial_type_id' => 1, + 'entity_table' => 'civicrm_contribution', + ], + ] + ], + ], + ]; + + civicrm_api3('Order', 'create', $params); + } + + /** + * @expectedException CiviCRM_API3_Exception + * @expectedExceptionMessage Line item total doesn't match with total amount. + */ + public function testCreateOrderIfTotalAmountDoesNotMatchLineItemsAmountsIfTaxSupplied() { + $params = [ + 'contact_id' => $this->_individualId, + 'receive_date' => '2018-01-01', + 'total_amount' => 50, + 'financial_type_id' => $this->_financialTypeId, + 'contribution_status_id' => 1, + 'tax_amount' => 15, + 'line_items' => [ + 0 => [ + 'line_item' => [ + '0' => [ + 'price_field_id' => 1, + 'price_field_value_id' => 1, + 'label' => 'Test 1', + 'field_title' => 'Test 1', + 'qty' => 1, + 'unit_price' => 30, + 'line_total' => 30, + 'financial_type_id' => 1, + 'entity_table' => 'civicrm_contribution', + 'tax_amount' => 15, + ], + ] + ], + ], + ]; + + civicrm_api3('Order', 'create', $params); + } + + public function testCreateOrderIfTotalAmountDoesMatchLineItemsAmountsAndTaxSupplied() { + $params = [ + 'contact_id' => $this->_individualId, + 'receive_date' => '2018-01-01', + 'total_amount' => 50, + 'financial_type_id' => $this->_financialTypeId, + 'contribution_status_id' => 1, + 'tax_amount' => 15, + 'line_items' => [ + 0 => [ + 'line_item' => [ + '0' => [ + 'price_field_id' => 1, + 'price_field_value_id' => 1, + 'label' => 'Test 1', + 'field_title' => 'Test 1', + 'qty' => 1, + 'unit_price' => 35, + 'line_total' => 35, + 'financial_type_id' => 1, + 'entity_table' => 'civicrm_contribution', + 'tax_amount' => 15, + ], + ] + ], + ], + ]; + + $order = civicrm_api3('Order', 'create', $params); + $this->assertEquals(1, $order['count']); + } + } -- 2.25.1