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

Refactor VAT taxation Part I: Fix existing functionality and tests #519

Closed
wants to merge 15 commits into from
Closed

Refactor VAT taxation Part I: Fix existing functionality and tests #519

wants to merge 15 commits into from

Conversation

mamhoff
Copy link
Contributor

@mamhoff mamhoff commented Nov 18, 2015

This is the PR to #461. It will be large - I hope though smaller than the one done to Spree 3.1.

With the new VAT taxation system, a default zone
is no longer mandatory. It is perfectly valid to have
taxes that are included in price only for specific foreign zones.
This class method returns all those zones that have at least one member
in common (even via a state that is part of a country), as well as
itself.
Both Spree::Zone.match and Spree::Zone.with_shared_members employ almost
the same SQL for doing what they need to do. Moved into a named scope to
reduce understanding effort.

Improve documentation, follow Rubocop recommendations
TaxRate.match does some really hard-to-understand calculations at the
end, only so that TaxRate.adjust gets exactly the right array for other
really complex code. This only affects VAT taxation, and the code in
question is, unfortunately, somewhat questionable.

The tests for TaxRate.adjust test the same behaviour. They're xit'ed for
now, but will be added again once I'm done refactoring TaxRate.adjust.
Half this method is now covered by Spree::Zone.with_shared_members. The
other half has to come from refactoring Spree::TaxRate.adjust.
Move the comments from TaxRate#potentially_applicable to TaxRate.adjust
and comment every line in the refactored adjust method.
Especially the ones for prices that include tax (aka VAT) were rather
hard to work with (very long context lines, lots of instance variables
instead of `let`s etc.).

Changed that and `xit`ed those that fail. They fail because of bugs in
the VAT code. Before, they had the right descriptions and wrong
expectations.
Also refactor carefully and add much more commentary.

In order for refunds to work like they should, rather than always
loading the default tax zone's rates, and randomly discarding one, this
commit introduces a "Do we need to refund" check, and then adds the
default zone's VAT rates.
This broke the reimbursement tests.
It does almost the same thing as TaxRate.for_zone, except for an early
return when the Argument is nil. Since it's only called from
TaxRate.adjust, at a point where we've already made sure that passed in
zone is not nil, the method is unnecessary.
@mamhoff
Copy link
Contributor Author

mamhoff commented Nov 22, 2015

The initial attempt at directly manipulating prices in the line item did not work, as line items can get their price from all sorts of places. So right now I've been restricting myself to a more iterative approach: Simplify tax_rate.rb and zone.rb, and clean up the tests. They're still a little complicated to see through, but hopefully somewhat more readable.

Because the initial attempt failed, I force-pushed. Lateron, I realized that was a shitty idea because all of your comments got lost :( - I am deeply sorry for that. Otherwise, this would be ready to merge, and for the next piece, I'd open up a new PR.

@mamhoff mamhoff changed the title WIP: Refactor VAT taxation Refactor VAT taxation Part I: Fix existing functionality and tests Nov 22, 2015
@tvdeyen tvdeyen mentioned this pull request Nov 22, 2015
@mamhoff
Copy link
Contributor Author

mamhoff commented Nov 23, 2015

I'm breaking this up into smaller PR's to make sure all commits pass the CI individually.

@mamhoff
Copy link
Contributor Author

mamhoff commented Dec 2, 2015

Closing in favour of #536

@mamhoff mamhoff closed this Dec 2, 2015
@mamhoff mamhoff deleted the refactor-vat-taxation branch May 24, 2016 19:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant