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

Calculate tax rate for items including discounts #36

Open
tamagokun opened this issue Oct 12, 2016 · 0 comments
Open

Calculate tax rate for items including discounts #36

tamagokun opened this issue Oct 12, 2016 · 0 comments

Comments

@tamagokun
Copy link

Using this extension on a fairly large production Spree installation and were running into problems when calculating taxes when an order promotion was involved.

Example: An item is $50, but there is a whole order promotion for 50% off, so the line item is $25, however taxes are based on the individual line items, so the customer pays tax for $50.

This is probably a deal breaker for most people wanting to use this extension, so I came up with a way to take discounts into account when calculating tax rates.


First step is to understand how to get the "final" price of the line item so the proper tax rate can be calculated. This is not so bad when there are %off promos, but it gets dicey when there are $off promotions because it becomes hard to tell where those discounts are going. As a general rule, I will distribute all discount amounts across the line items evenly. Here's how the equation looks in code:

adjustment_total = line_item.order.adjustments.eligible.sum(:amount)
item_total = line_item.order.line_items.sum('price * quantity')
taxable_discount_amount = (item.price * (adjustment_total / item_total)).to_d.truncate(2).to_f
return line_item.price + taxable_discount_amount

The to_d.truncate(2).to_f is a little weird, because I kept getting rounding issues, but that combination seemed to fix it.

This has been working well for awhile, however there are a few other places where I needed to append values to a model's tax_cloud_cache_key or re-trigger tax rates manually in certain places. There were many times where orders would change, but taxes would not get re-calculated, or it would just return a cached value that wasn't correct.

I'd love to open up a discussion about this problem, and if anyone thinks the code I came up with this is useful, I could submit a PR with the additions.

Thanks!

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

No branches or pull requests

1 participant