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

dev/financial#180 Fix line item calculation regression on line items (incorrectly treating as exclusive) #21212

Merged
merged 3 commits into from
Aug 24, 2021

Conversation

eileenmcnaughton
Copy link
Contributor

@eileenmcnaughton eileenmcnaughton commented Aug 22, 2021

Overview

Fixes a 5.41 regression where total amount was assumed to be exclusive when generating line items and calculating tax when it is only sometimes exclusive

Before

Total amount is assumed to be tax-exclusive in all scenarios and line items calculated on that assumption

After

Total amount is assumed to be inclusive in the bao - the edge case where it is exclusive historically and tested to be so is moved to contribution.create api

Technical Details

A recent fix added tax_amount calculations to getLineItems and used that amount to calculate the tax amount.

However, in accordance with api_v3_TaxContributionPageTest I reached the conclusion that the 'normal behaviour' was to treat the passed in amount as inclusive. In fact that is historically the case ONLY IF tax_amount is not passed in - and it seems that it most commonly IS passed in.

This moves the weird handling to the v3 contribution api. The intention is that v4 contribution api and order api will always assume that it is inclusive - although we can discuss adding the flexibility to define exclusive instead from the order api.

Note that when line_item (e.g as core forms do) is passed the bug is not hit - the focus really is on the contribution.create api when it is allowing the BAO to calculate the line items.

Comments

@KarinG @mattwire @monishdeb @jitendrapurohit @christianwach this is the fix for the original sin - I think with this merged it will fix Contribution.api to 'do the right thing' when tax_amount is passed in. The fix for the order api becomes pretty simple at that point 05d1bba

However, I'm still expecting some tests that were altered AFTER the bug was introduced to fail with this or the order fix - just because the tests are wrong I think.

@civibot
Copy link

civibot bot commented Aug 22, 2021

(Standard links)

@civibot civibot bot added the 5.41 label Aug 22, 2021
@eileenmcnaughton eileenmcnaughton changed the title Fix line item calculation from exclusive to inclusive Fix line item calculation regression on line items (incorrectly treating as exclusive) Aug 23, 2021
@eileenmcnaughton eileenmcnaughton changed the title Fix line item calculation regression on line items (incorrectly treating as exclusive) dev/financial#180 Fix line item calculation regression on line items (incorrectly treating as exclusive) Aug 23, 2021
@eileenmcnaughton eileenmcnaughton force-pushed the 541-orig branch 3 times, most recently from 8f6f9b9 to d8375c2 Compare August 24, 2021 00:56
@monishdeb
Copy link
Member

@eileenmcnaughton I can confirm that the patch correctly sets the line_item.line_total to be exclusive of tax_amount and is stored separately in line_item.tax_amount. Unlike for contribution.total_amount which is inclusive of tax_amount and there is special handling at Contribution.create api3. But there is a test failure https://test.civicrm.org/job/CiviCRM-Core-PR/43292/testReport/junit/(root)/api_v3_TaxContributionPageTest/testCreateUpdateContributionChangeTotal/ which is related and needs similar fix.

As this patch is touching the essential part of financial code, I need anyone else to confirm my assessment.

@@ -392,8 +392,8 @@ public function testCreateUpdateContributionChangeTotal(): void {
]);

$this->assertEquals('300.00', $lineItems);
$this->assertEquals('320.00', $this->_getFinancialTrxnAmount($contribution['id']));
$this->assertEquals('320.00', $this->_getFinancialItemAmount($contribution['id']));
$this->assertEquals('300.00', $this->_getFinancialTrxnAmount($contribution['id']));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This reverts the changes made in the same commit as the original sin

@jitendrapurohit
Copy link
Contributor

Tested the changes on the following scenarios -

Frontend -

Online Contribution Page.
Online Membership Signup.
Online Membership Renewal.
Repeat the above test with priceset.

Backend -

New membership (w/o priceset)
Renew Membership
New Membership with live mode payment (w/o priceset).
Renew Membership with live mode payment.

Webform -

Webform purchase of New Membership.
Webform donation with taxable FT.
If there are different financial type (both taxable) line items on a single webform, the tax amount is not saved correctly, but it is also an issue before this PR so unrelated.

It passes on all the above steps - expected values are present in line_item table and the amounts looks fine on view contribution and view membership pages.

I think we're good to merge this 👍 .

@KarinG
Copy link
Contributor

KarinG commented Aug 24, 2021

Thank you @jitendrapurohit 👍

@eileenmcnaughton -> is this in addition to the other PR?

@KarinG
Copy link
Contributor

KarinG commented Aug 24, 2021

@jitendrapurohit - re: webform and tax amount -> I think the better_line items branch addresses that (I use it in D7 and need to revamp it’s on our Google doc for D8/D9 port and inclusion).

@eileenmcnaughton
Copy link
Contributor Author

@KarinG this fixes line_item.total_amount being incorrectly inclusive when calculated in the BAO - I'm not 100% sure if we need an order api fix on top of this

@jitendrapurohit I think you are saying

  1. we should merge this
  2. we need to do a further fix for the order api - I believe this is the extra fix - FIx order api regression #21230 - note the failing tests there I believe have all been altered recently to use the order api rather than contribution api & added the incorrect assumption into them
  3. we should back port this to 5.40 to ensure line_item.total_amount is not incorrectly inclusive

@eileenmcnaughton
Copy link
Contributor Author

Also @jitendrapurohit I think this would mostly impact contribution.create api calls but not so much core forms - which generally pass in 'line_item' as a parameter to contribution.create

@eileenmcnaughton
Copy link
Contributor Author

@monishdeb I think we should merge this based on @jitendrapurohit & then focus on #21230 which should address the originally identified bug

@monishdeb
Copy link
Member

Agree. Merging now based on @jitendrapurohit @KarinG and my feedback.

@monishdeb monishdeb merged commit 0fc2232 into civicrm:5.41 Aug 24, 2021
@eileenmcnaughton eileenmcnaughton deleted the 541-orig branch August 24, 2021 16:04
@KarinG
Copy link
Contributor

KarinG commented Aug 24, 2021

I'll add dev-master to our automated webform testing so that I can run this PR against all our tests.

@eileenmcnaughton
Copy link
Contributor Author

Thanks @monishdeb @jitendrapurohit @KarinG

I've rebased #21230 to just have the order api fix

I haven't updated the tests in it at this stage because I think it makes it possibly easier to check & agree the tests need updating

@KarinG
Copy link
Contributor

KarinG commented Aug 24, 2021

I'm getting the same error using dev-master right now [when not passing in line_items] -> but no new errors:
image

But I can confirm -> #21189 dev/rc#14 handle api calls post schema change #21189 is fixed ✅

@eileenmcnaughton
Copy link
Contributor Author

@KarinG #21230 is the fix I think we should be merging to fix ^^

@KarinG
Copy link
Contributor

KarinG commented Aug 24, 2021

Understood - just wanted to flag that no other issues surfaced on our end post merging this PR.

@eileenmcnaughton
Copy link
Contributor Author

thanks @KarinG

@agileware-justin
Copy link
Contributor

agileware-justin commented Aug 27, 2021

And here's the direct link to the related Gitlab for anyone else who is wondering, https://lab.civicrm.org/dev/financial/-/issues/180

(this could be the job of a Gitbot)

@eileenmcnaughton
Copy link
Contributor Author

@agileware-justin you ARE our gitbot

@eileenmcnaughton
Copy link
Contributor Author

lol

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants