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

Tax adjustments are not destroyed properly if there is more than one type of adjustable for relevant_items #909

Closed
AdnanTheExcellent opened this issue Feb 25, 2016 · 8 comments

Comments

@AdnanTheExcellent
Copy link

Using solidus 1.2.

I am having problem with taxes not destroying tax adjustments properly when Spree::TaxRate.adjust(order_tax_zone, items) is called and items consists of an array of different class types.

For example, our store taxes shipping (since we upcharge). if "items" input contained all our line items and shipments in the order, then this line of code fails:

https://github.com/solidusio/solidus/blob/v1.2/core/app/models/spree/tax_rate.rb#L90

Lets say my relevant items contains a LineItem and a Shipment and i call line 90, this is the SQL query that active record returns:

[16] pry(Spree::TaxRate)> Spree::Adjustment.where(adjustable: relevant_items)
  CACHE (0.1ms)  SELECT `spree_adjustments`.* FROM `spree_adjustments` WHERE `spree_adjustments`.**`adjustable_type` = 'Spree::LineItem'** AND `spree_adjustments`.`adjustable_id` IN (1071079807, 1056546687)

As you can see in the SQL query, it is only looking for line items. Thus, it only returns the tax adjustment for the line item. if i called this line instead:

[17] pry(Spree::TaxRate)> Spree::Adjustment.where(adjustable_id: relevant_items.map(&:id))
  CACHE (0.2ms)  SELECT `spree_adjustments`.* FROM `spree_adjustments` WHERE `spree_adjustments`.`adjustable_id` IN (1071079807, 1056546687)

then i get back the tax adjustments for the line item and the shipment.

@mamhoff
Copy link
Contributor

mamhoff commented Feb 25, 2016

Wow, that's a bug I didn't even know existed. This is fixed on the current master branch though.

@mamhoff
Copy link
Contributor

mamhoff commented Feb 25, 2016

The line you're proposing is good in most cases, but will destroy adjustments you might not want destroyed, unfortunately. (Example: You have a promo adjustment with ID 1 and a line item adjustment with ID 1, your code would destroy both...)

@AdnanTheExcellent
Copy link
Author

it still needs the .tax.destroy_all at the end. the code i proposed was to just show the initial sql query before the .tax for fetching the adjustments and how the two were returning different results. With the .tax it shouldnt destroy any other type of adjustments.

If you changed line 90 to:

Spree::Adjustment.where(adjustable_id: relevant_items.map(&:id)).tax.destroy_all

it should work fine

@mamhoff
Copy link
Contributor

mamhoff commented Feb 25, 2016

That will still not be foolproof. You would possibly be deleting shipment
or line item tax adjustments on another order. You can provide a PR that
actually fixes that issue on Solidus 1.2 or upgrade to master.
Am 25.02.2016 20:58 schrieb "Adnan Abdulally" [email protected]:

it still needs the .tax.destroy_all at the end. the code i proposed was
to just show the initial sql query before the .tax for fetching the
adjustments


Reply to this email directly or view it on GitHub
#909 (comment).

@AdnanTheExcellent
Copy link
Author

Ah ok, i see what you're saying now.

This should do the trick:

relevant_items.map(&:adjustments).map(&:tax).flatten.destroy_all

@mamhoff
Copy link
Contributor

mamhoff commented Feb 25, 2016

That would work. Please submit a pull request as it does fix a bug.
However, be aware that it also foregoes the efficiency of the query we had
before: now you create items.length SQL queries.
Am 25.02.2016 23:50 schrieb "Adnan Abdulally" [email protected]:

Ah ok, i see what you're saying now.

This should do the trick:

relevant_items.map(&:adjustments).map(&:tax).flatten.destroy_all


Reply to this email directly or view it on GitHub
#909 (comment).

@AdnanTheExcellent
Copy link
Author

True, but master does the same thing now that they extracted the code to Spree::Tax::ItemAdjuster. It only does 1 object at a time so i would have to loop over each item and do items.length SQL queries.

https://github.com/solidusio/solidus/blob/master/core/app/models/spree/tax/item_adjuster.rb#L28

@mamhoff
Copy link
Contributor

mamhoff commented Apr 1, 2016

I think this has been resolved in both master and Solidus 1.2.

@mamhoff mamhoff closed this as completed Apr 1, 2016
bbuchalter pushed a commit to TommyJohnWear/solidus that referenced this issue Nov 18, 2016
As a mentioned in solidusio#909,
the current implementation of the taxation system destroys
tax adjustments one item at a time.

This commit changes that so all the order is stripped of
all tax adjustments, resulting in less database calls.
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

2 participants