Skip to content

Commit

Permalink
Refactor line item total calculations
Browse files Browse the repository at this point in the history
This updates Spree::OrderUpdater to use the new ItemTotal class,
avoiding duplicating all of that logic.

Co-authored-by: Chris Todorov <[email protected]>
Co-authored-by: Adam Mueller <[email protected]>
Co-authored-by: Alistair Norman <[email protected]>
Co-authored-by: benjamin wil <[email protected]>
Co-authored-by: Harmony Bouvier <[email protected]>
Co-authored-by: Sofia Besenski <[email protected]>
  • Loading branch information
7 people committed Jan 17, 2025
1 parent dbc5940 commit 90f5420
Showing 1 changed file with 13 additions and 33 deletions.
46 changes: 13 additions & 33 deletions core/app/models/spree/order_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def recalculate_adjustments
# It also fits the criteria for sales tax as outlined here:
# http://www.boe.ca.gov/formspubs/pub113/
update_promotions
update_taxes
update_tax_adjustments
update_item_totals
end

Expand Down Expand Up @@ -198,24 +198,8 @@ def update_promotions
Spree::Config.promotions.order_adjuster_class.new(order).call
end

def update_taxes
def update_tax_adjustments
Spree::Config.tax_adjuster_class.new(order).adjust!

[*line_items, *shipments].each do |item|
tax_adjustments = item.adjustments.select { |adjustment|
adjustment.tax? && !adjustment.marked_for_destruction?
}

# 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
Expand All @@ -224,21 +208,17 @@ def update_cancellations

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.adjustment_total = item.adjustments.
reject { |adjustment| adjustment.included? || adjustment.marked_for_destruction? }.
sum(&:amount)

if item.changed?
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
Spree::ItemTotal.new(item).recalculate!

next unless item.changed?

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
Expand Down

0 comments on commit 90f5420

Please sign in to comment.