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

TaxHelpers#rates_for_item now respects the validity period of tax rates #3768

Merged
merged 8 commits into from
Oct 13, 2020
Merged

TaxHelpers#rates_for_item now respects the validity period of tax rates #3768

merged 8 commits into from
Oct 13, 2020

Conversation

knorrli
Copy link
Contributor

@knorrli knorrli commented Sep 22, 2020

Description
When calculating the tax for line items whose tax is included in the price, solidus uses TaxRates in the calculation even though they are invalid according to their start_date and/or end_date. This results in incorrect tax calculations if there are multiple tax rates for the same tax category and only some of them are currently valid.

This PR ensures that TaxHelpers#rates_for_item only returns tax rates that are currently valid. Before, it would return any tax rate that matched the items' tax category.

This addresses #3766

Checklist:

  • I have followed Pull Request guidelines
  • I have added a detailed description into each commit message
  • I have updated Guides and README accordingly to this change (if needed)
  • I have added tests to cover this change (if needed)
  • I have attached screenshots to this PR for visual changes (if needed)

@knorrli
Copy link
Contributor Author

knorrli commented Sep 22, 2020

As suggested in #3766, I have opened a PR and moved the change to the TaxHelpers#rates_for_item method.

I am currently still unsure about two things:

  • Maybe it would make sense to add an .active scope to TaxRate. If so, would you like me to do this in this PR directly?
  • I didn't create a new spec file for the TaxHelpers module and instead added a test to the DefaultTax calculator which is one of two places affected by the change. Would you like me to create a spec file for the TaxHelpers module? If so, where should it be (since TaxHelpers is in models/ for some reason...?).

I'm very open for suggestions and improvements.

@aldesantis
Copy link
Member

@jugglinghobo thanks for this! Answers to your concerns below:

  • Maybe it would make sense to add an .active scope to TaxRate. If so, would you like me to do this in this PR directly?

Yes, I think it would be useful. 🙂 Feel free to do it in this PR!

  • I didn't create a new spec file for the TaxHelpers module and instead added a test to the DefaultTax calculator which is one of two places affected by the change. Would you like me to create a spec file for the TaxHelpers module? If so, where should it be (since TaxHelpers is in models/ for some reason...?).

Yes, I think the test for this should be on the TaxHelpers module itself. You can put it in spec/models/spree/tax/tax_helpers_spec.rb. Testing modules can be a bit tricky (you'd usually create a "dummy" test class and include the module there, then test the method somehow), so let me know if you need any help.

@mamhoff
Copy link
Contributor

mamhoff commented Oct 9, 2020

@aldesantis @jugglinghobo I think this is actually a pretty important fix functionality-wise. I appreciate wanting to have very good specs that respect all the things, and maybe also a scope, but can we make issues for that and get this in? I'm seeing clients write workarounds in their exporter code for tax advisors because this doesn't work. I'd also be happy to take this over if necessary. (cc @tvdeyen @mntmn).

@knorrli
Copy link
Contributor Author

knorrli commented Oct 9, 2020

@mamhoff @aldesantis I'm sorry this fell off my radar. If you can wait, I'll try to finish this tonight or tomorrow. If you don't hear back until sunday or if you need a solution quicker, feel free to take over, and if you leave out anything (e.g. scope), feel free to assign an issue to me or whatever you'd like.

Does that work for you, or would you like me to do anything else? This is my first solidus / open source contribution.

@aldesantis
Copy link
Member

aldesantis commented Oct 9, 2020

@jugglinghobo even if this was merged today, it still wouldn't be available until the next Solidus release, so tonight/tomorrow works perfectly. Thanks for your help on this one!

@kennyadsl kennyadsl added this to the 2.11 milestone Oct 9, 2020
@mamhoff
Copy link
Contributor

mamhoff commented Oct 9, 2020

I just didn't want this to sit for another couple of weeks. If you're on it, great! 👍

TaxHelpers#rates_for_item considers the validity period of tax rates and
only returns tax rates that are currently valid.

* Added specs for TaxHelpers#rates_for_item
* Removed specs from DefaultTaxCalculator spec because they don't belong
there
* remove trailing whitespace
* remove extra line at beginning of block
This scope returns all tax rates that are currently active according to
their start date and expiry date, i.e. if the current time is after the
start date and before the expiry date.
core/spec/models/spree/tax_rate_spec.rb Outdated Show resolved Hide resolved
core/spec/models/spree/tax_rate_spec.rb Outdated Show resolved Hide resolved
core/spec/models/spree/tax_rate_spec.rb Outdated Show resolved Hide resolved
core/spec/models/spree/tax_rate_spec.rb Outdated Show resolved Hide resolved
core/spec/models/spree/tax_rate_spec.rb Outdated Show resolved Hide resolved
core/spec/models/spree/tax_rate_spec.rb Outdated Show resolved Hide resolved
core/spec/models/spree/tax_rate_spec.rb Outdated Show resolved Hide resolved
* add space between brackets
@knorrli
Copy link
Contributor Author

knorrli commented Oct 10, 2020

@aldesantis

As discussed I've added a spec for TaxHelpers#rates_for_item. IMO this is all that's required to fix the bug.
I also added the TaxRate.active scope (identical to the one in Promotion), but it is unused at the moment.

Please suggest improvements and advise on how to proceed. I didn't squash yet because I thought maybe you'd like to add the scope in a separate PR.

PS: AFAICT there are no specs for Promotion.active, if the new specs are accepted then these could be reused there.

Copy link
Member

@aldesantis aldesantis left a comment

Choose a reason for hiding this comment

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

@jugglinghobo thanks, the tests are okay except for one minor thing. After that, GTM!

core/spec/models/spree/tax/tax_helpers_spec.rb Outdated Show resolved Hide resolved
Copy link
Member

@aldesantis aldesantis left a comment

Choose a reason for hiding this comment

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

Looks great, thanks so much! 🙏

Copy link
Contributor

@mamhoff mamhoff left a comment

Choose a reason for hiding this comment

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

Nicely done!

Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

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

Thanks!

@kennyadsl kennyadsl merged commit 3101760 into solidusio:master Oct 13, 2020
@kennyadsl kennyadsl added changelog:solidus_core Changes to the solidus_core gem and removed Needs Core Team Review labels Oct 13, 2020
@knorrli
Copy link
Contributor Author

knorrli commented Oct 13, 2020

Thank you guys!

@knorrli knorrli deleted the consider-only-active-rates-when-calculating-item-taxes branch October 13, 2020 11:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog:solidus_core Changes to the solidus_core gem
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants