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

Break ItemAdjustments apart and put the logic in OrderUpdater #1466

Merged
merged 6 commits into from
Sep 30, 2016

Conversation

jordan-brough
Copy link
Contributor

This sets up tax updates in OrderUpdater so that all tax updates for an order
happen at the same time.

With ItemAdjustments we updated items in this order:

- item 1
  - update promotions
  - update taxes
  - update cancellations
  - update totals
- item 2
  - update promotions
  - update taxes
  - update cancellations
  - update totals

However, we need to invoke tax calculation on the order as a whole
(even though the results will be calculated & stored at the line/shipment level) in
order to provide a good extension point for external tax services. See #1252.
That means we need to do this:

- update promotions
  - item 1
  - item 2
- update taxes
  - item 1
  - item 2
- update cancellations
  - item 1
  - item 2
- update totals
  - item 1
  - item 2

I couldn't think of a reasonable way to refactor ItemAdjustments to be
able to do the above, without breaking backward compatibility in a
significant way. And if we're going to break backward compatibility
then moving this logic into OrderUpdater seemed like a better change
to me as a first step.

Once we have this in place we can start working on providing an
extension point in the "update taxes" step where external tax services
can be plugged in.

item.additional_tax_total +
item_cancellation_total

if item.changed?

Choose a reason for hiding this comment

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

Use next to skip iteration.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think I disagree with our rubocop rules here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 Updated in #1467

Copy link
Contributor

@jhawthorn jhawthorn left a comment

Choose a reason for hiding this comment

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

I like this as a start but have left some feedback.

We lost some good comments from the ItemAdjustments class, maybe some of them still apply to the new methods.

end

promo_sequences.each do |promo_sequence|
context 'with promo sequence #{promo_sequence}' do
Copy link
Contributor

Choose a reason for hiding this comment

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

Single quotes prevents interpolation here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! Fixed by the update that groups this loop with the previous one.

end
end

promo_sequences.each do |promo_sequence|
Copy link
Contributor

Choose a reason for hiding this comment

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

It might make sense to group this loop with the previous identical one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great idea. Done.

item.additional_tax_total +
item_cancellation_total

if item.changed?
Copy link
Contributor

Choose a reason for hiding this comment

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

I think I disagree with our rubocop rules here.


def update_item_totals
[*line_items, *shipments].each do |item|
item_cancellation_total = item.adjustments.select(&:cancellation?).sum(&:amount)
Copy link
Contributor

@jhawthorn jhawthorn Sep 22, 2016

Choose a reason for hiding this comment

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

It feels weird that promo_total, included_tax_total, and additional_tax_total are summed in their respective methods, but cancellations are not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed. I've added a comment to that effect -- cancellation_total isn't persisted anywhere so we have to extract it here. (The others need to be done in their respective methods so that, e.g., tax calculations have the updated promo totals).

@jordan-brough
Copy link
Contributor Author

jordan-brough commented Sep 23, 2016

@jhawthorn thanks for the feedback! I've added commits for all the comments (including bringing in most of the code comments from ItemAdjustments). I also rebased on latest master to get the updated rubocop rules.

With ItemAdjustments we updated items in this order:

    - item 1
      - update promotions
      - update taxes
      - update cancellations
      - update totals
    - item 2
      - update promotions
      - update taxes
      - update cancellations
      - update totals

However, we need to invoke tax calculation on the order as a whole
(even though the results will be stored at the line/shipment level) in
order to provide a good extension point for external tax services.
That means we need to do this:

    - update promotions
      - item 1
      - item 2
    - update taxes
      - item 1
      - item 2
    - update cancellations
      - item 1
      - item 2
    - update totals
      - item 1
      - item 2

I couldn't think of a reasonable way to refactor ItemAdjustments to be
able to do the above, without breaking backward compatibility in a
significant way. And if we're going to break backward compatibility
then moving this logic into OrderUpdater seemed like a better change
to me as a first step.

Once we have this in place we can start working on providing an
extension point in the "update taxes" step where external tax services
can be plugged in.
Shipments can't have item cancellations.
Bring in a bunch of helpful comments that were in the ItemAdjustments
class. And split apart update_item_promotions and
update_order_promotions to make the code clearer since they are
handled slightly differently.
Also fixes single quotes that were prevent interpolation.

# Update and select the best promotion adjustment for the order.
# We don't update the order.promo_total yet. Order totals are updated later
# in #update_adjustment_total since they include the totals from the ordre's
Copy link
Contributor

Choose a reason for hiding this comment

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

typo in ordre

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed. Thanks!

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.

This is an excellent step forward in removing some of the crazyness around taxes.

@jhawthorn jhawthorn merged commit 13d6b98 into solidusio:master Sep 30, 2016
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.

None yet

5 participants