Skip to content

Commit

Permalink
Merge pull request civicrm#18351 from eileenmcnaughton/line
Browse files Browse the repository at this point in the history
Add test to cover existing v3 api setting of tax_amount on line items
  • Loading branch information
seamuslee001 authored Sep 5, 2020
2 parents 116c6c9 + 0b17a87 commit f412f88
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
19 changes: 13 additions & 6 deletions CRM/Financial/BAO/FinancialTypeAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,17 @@ public static function retrieve(&$params, &$defaults = [], &$allValues = []) {
* @param array $params
* Reference array contains the values submitted by the form.
* @param array $ids
* Reference array contains the id.
* Reference array contains one possible value
* - entityFinancialAccount.
*
* @return object
* @return CRM_Financial_DAO_EntityFinancialAccount
*
* @throws \CRM_Core_Exception
*/
public static function add(&$params, &$ids = NULL) {
public static function add(&$params, $ids = NULL) {
// action is taken depending upon the mode
$financialTypeAccount = new CRM_Financial_DAO_EntityFinancialAccount();
if ($params['entity_table'] != 'civicrm_financial_type') {
if ($params['entity_table'] !== 'civicrm_financial_type') {
$financialTypeAccount->entity_id = $params['entity_id'];
$financialTypeAccount->entity_table = $params['entity_table'];
$financialTypeAccount->find(TRUE);
Expand All @@ -71,6 +74,7 @@ public static function add(&$params, &$ids = NULL) {
$financialTypeAccount->copyValues($params);
self::validateRelationship($financialTypeAccount);
$financialTypeAccount->save();
unset(Civi::$statics['CRM_Core_PseudoConstant']['taxRates']);
return $financialTypeAccount;
}

Expand All @@ -80,6 +84,7 @@ public static function add(&$params, &$ids = NULL) {
* @param int $financialTypeAccountId
* @param int $accountId
*
* @throws \CRM_Core_Exception
*/
public static function del($financialTypeAccountId, $accountId = NULL) {
// check if financial type is present
Expand All @@ -102,6 +107,7 @@ public static function del($financialTypeAccountId, $accountId = NULL) {

foreach ($dependency as $name) {
$daoString = 'CRM_' . $name[0] . '_DAO_' . $name[1];
/* @var \CRM_Core_DAO $dao */
$dao = new $daoString();
$dao->financial_type_id = $financialTypeId;
if ($dao->find(TRUE)) {
Expand All @@ -111,7 +117,7 @@ public static function del($financialTypeAccountId, $accountId = NULL) {
}

if ($check) {
if ($name[1] == 'PremiumsProduct' || $name[1] == 'Product') {
if ($name[1] === 'PremiumsProduct' || $name[1] === 'Product') {
CRM_Core_Session::setStatus(ts('You cannot remove an account with a %1 relationship while the Financial Type is used for a Premium.', [1 => $relationValues[$financialTypeAccountId]]));
}
else {
Expand All @@ -136,6 +142,7 @@ public static function del($financialTypeAccountId, $accountId = NULL) {
* Payment instrument value.
*
* @return null|int
* @throws \CiviCRM_API3_Exception
*/
public static function getInstrumentFinancialAccount($paymentInstrumentValue) {
if (!isset(\Civi::$statics[__CLASS__]['instrument_financial_accounts'][$paymentInstrumentValue])) {
Expand Down Expand Up @@ -249,7 +256,7 @@ public static function createDefaultFinancialAccounts($financialType) {
/**
* Validate account relationship with financial account type
*
* @param obj $financialTypeAccount of CRM_Financial_DAO_EntityFinancialAccount
* @param CRM_Financial_DAO_EntityFinancialAccount $financialTypeAccount of CRM_Financial_DAO_EntityFinancialAccount
*
* @throws CRM_Core_Exception
*/
Expand Down
26 changes: 26 additions & 0 deletions tests/phpunit/api/v3/LineItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* @group headless
*/
class api_v3_LineItemTest extends CiviUnitTestCase {
use CRM_Financial_Form_SalesTaxTrait;

protected $params;
protected $_entity = 'line_item';

Expand Down Expand Up @@ -48,6 +50,30 @@ public function setUp() {
];
}

/**
* Test tax is calculated correctly on the line item.
*/
public function testCreateLineItemWithTax() {
$this->enableSalesTaxOnFinancialType('Donation');
$this->params['financial_type_id'] = 'Donation';
$result = $this->callAPISuccess('LineItem', 'create', $this->params);
$lineItem = $this->callAPISuccessGetSingle('LineItem', ['id' => $result['id']]);
$this->assertEquals(5, $lineItem['tax_amount']);
$this->assertEquals(50, $lineItem['line_total']);
}

/**
* Enable tax for the given financial type.
*
* @todo move to a trait, share.
*
* @param string $type
*/
public function enableSalesTaxOnFinancialType($type) {
$this->enableTaxAndInvoicing();
$this->relationForFinancialTypeWithFinancialAccount(CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', $type));
}

/**
* Test basic create line item.
*
Expand Down

0 comments on commit f412f88

Please sign in to comment.