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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 0 additions & 82 deletions core/app/models/spree/item_adjustments.rb

This file was deleted.

86 changes: 81 additions & 5 deletions core/app/models/spree/order_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,22 @@ def run_hooks
update_hooks.each { |hook| order.send hook }
end

# This will update and select the best promotion adjustment, update tax
# adjustments, update cancellation adjustments, and then update the total
# fields (promo_total, included_tax_total, additional_tax_total, and
# adjustment_total) on the item.
# @return [void]
def recalculate_adjustments
adjustables = [*line_items, *shipments, order]

adjustables.each do |adjustable|
Spree::ItemAdjustments.new(adjustable).update
end
# Promotion adjustments must be applied first, then tax adjustments.
# This fits the criteria for VAT tax as outlined here:
# http://www.hmrc.gov.uk/vat/managing/charging/discounts-etc.htm#1
# It also fits the criteria for sales tax as outlined here:
# http://www.boe.ca.gov/formspubs/pub113/
update_item_promotions
update_order_promotions
update_taxes
update_cancellations
update_item_totals
end

# Updates the following Order total values:
Expand Down Expand Up @@ -164,5 +174,71 @@ def update_payment_state
def round_money(n)
(n * 100).round / 100.0
end

def update_item_promotions
[*line_items, *shipments].each do |item|
promotion_adjustments = item.adjustments.select(&:promotion?)

promotion_adjustments.each(&:update!)
Spree::Config.promotion_chooser_class.new(promotion_adjustments).update

item.promo_total = promotion_adjustments.select(&:eligible?).sum(&:amount)
end
end

# 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 order's
# line items and/or shipments.
def update_order_promotions
promotion_adjustments = order.adjustments.select(&:promotion?)
promotion_adjustments.each(&:update!)
Spree::Config.promotion_chooser_class.new(promotion_adjustments).update
end

def update_taxes
[*line_items, *shipments].each do |item|
tax_adjustments = item.adjustments.select(&:tax?)

tax_adjustments.each(&:update!)
# Tax adjustments come in not one but *two* exciting flavours:
# Included & additional

# Included tax adjustments are those which are included in the price.
# These ones should not affect the eventual total price.
#
# Additional tax adjustments are the opposite, affecting the final total.
item.included_tax_total = tax_adjustments.select(&:included?).sum(&:amount)
item.additional_tax_total = tax_adjustments.reject(&:included?).sum(&:amount)
end
end

def update_cancellations
line_items.each do |line_item|
line_item.adjustments.select(&:cancellation?).each(&:update!)
end
end

def update_item_totals
[*line_items, *shipments].each do |item|
# The cancellation_total isn't persisted anywhere but is included in
# the adjustment_total
item_cancellation_total = item.adjustments.select(&:cancellation?).sum(&:amount)

item.adjustment_total = item.promo_total +
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

item.update_columns(
promo_total: item.promo_total,
included_tax_total: item.included_tax_total,
additional_tax_total: item.additional_tax_total,
adjustment_total: item.adjustment_total,
updated_at: Time.current,
)
end
end
end
end
end
Loading