Skip to content
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

[wip] CRM-19585, added function to Calculate Tax for each item when Financi… #9685

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -5457,4 +5457,25 @@ public static function createProportionalFinancialEntries($entityParams, $lineIt
}
}

/**
* Calculate Tax for each item when Financial Type is changed.
*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to add the logic of the calculation to the method description. Otherwise, we have to read the code in order to understand what is going on

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is just repeatedly calling a different function that does the calculation. I don't think it should be documenting details that is not responsible for.

* @param array $lineItem
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to know what are the values expected in this $lineItem array

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

explained in updated PR

*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to this empty line here

Copy link
Contributor

@JoeMurray JoeMurray Mar 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://www.drupal.org/node/1354#functions suggests a blank line between args and no blank line after last one.

* @param int $contributionId
*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for this empty line here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, https://www.drupal.org/node/1354#functions suggests a blank line between args and no blank line after last one.

*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing @return

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Partially fixed in updated PR.

public static function calculateTaxAfterChangeInFinancialTypeForLineItems($lineItem, $contributionId) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this method name and its description are confusing. Usually, the method name should describe what it does and not when it should be executed. It says it calculates the Tax after the Financial Type is changed, but looking at the code, it looks like it can do the calculation anytime, even if the Financial Type hasn't been changed.

Copy link
Contributor

@JoeMurray JoeMurray Mar 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pradeep, there is merit in @davialexandre comment, but the code at some level needs to be either reversing out financial_items for both the line_item and its tax and posting new entries (I think that is the spec at https://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+Data+Flow but Confluence is down at the moment) or maybe (I don't think so) creating a difference transaction for financial_line_item for both line item and tax. Could you respond to his comment? Thx.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about adding tests to this new method?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pradeep, you should respond to each comment indicating if you accept it or provide reason(s) for disagreeing. In this case, indicate that you have added a test in updated PR.

$taxAmount = 0;
$previousLineItem = CRM_Price_BAO_LineItem::getLineItemsByContributionID($contributionId);
foreach ($lineItem as $items) {
foreach ($items as $item) {
$lineTotal = CRM_Utils_Array::value('line_total', CRM_Utils_Array::value($item['id'], $previousLineItem));
$lineTaxAmount = CRM_Contribute_BAO_Contribution_Utils::calculateTaxAmount($lineTotal, $item['tax_rate']);
$taxAmount += $lineTaxAmount['tax_amount'];
}
}
return $taxAmount;
}

}
22 changes: 22 additions & 0 deletions tests/phpunit/CRM/Contribute/BAO/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1228,4 +1228,26 @@ public function createContributionWithTax() {
return array($contribution, $financialAccount);
}

/**
* test for function calculateTaxAfterChangeInFinancialTypeForLineItems()
*/
public function testcalculateTaxAfterChangeInFinancialTypeForLineItems() {
list($contribution, $financialAccount) = $this->createContributionWithTax();
$lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($contribution['id']);
foreach ($lineItems as $id => $lineItem) {
$lineItems[$id]['line_total'] = 300;
$lineItems[$id]['tax_rate'] = 20;
$lineItems[$id]['id'] = $id;
}
$taxAmount = CRM_Contribute_BAO_Contribution::calculateTaxAfterChangeInFinancialTypeForLineItems(array($lineItems), $contribution['id']);
$this->assertEquals($taxAmount, 20.00, 'Amount does not match.');
foreach ($lineItems as $id => $lineItem) {
$lineItems[$id]['line_total'] = 300;
$lineItems[$id]['tax_rate'] = 0;
$lineItems[$id]['id'] = $id;
}
$taxAmount = CRM_Contribute_BAO_Contribution::calculateTaxAfterChangeInFinancialTypeForLineItems(array($lineItems), $contribution['id']);
$this->assertEquals($taxAmount, 0, 'Amount does not match.');
}

}