Skip to content

Commit 90f5420

Browse files
jarednormanforkataadammathysAlistairNormanbenjaminwil
committed
Refactor line item total calculations
This updates Spree::OrderUpdater to use the new ItemTotal class, avoiding duplicating all of that logic. Co-authored-by: Chris Todorov <chris@super.gd> Co-authored-by: Adam Mueller <adam@super.gd> Co-authored-by: Alistair Norman <alistair@super.gd> Co-authored-by: benjamin wil <benjamin@super.gd> Co-authored-by: Harmony Bouvier <harmony@super.gd> Co-authored-by: Sofia Besenski <sofia@super.gd>
1 parent dbc5940 commit 90f5420

File tree

1 file changed

+13
-33
lines changed

1 file changed

+13
-33
lines changed

core/app/models/spree/order_updater.rb

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def recalculate_adjustments
113113
# It also fits the criteria for sales tax as outlined here:
114114
# http://www.boe.ca.gov/formspubs/pub113/
115115
update_promotions
116-
update_taxes
116+
update_tax_adjustments
117117
update_item_totals
118118
end
119119

@@ -198,24 +198,8 @@ def update_promotions
198198
Spree::Config.promotions.order_adjuster_class.new(order).call
199199
end
200200

201-
def update_taxes
201+
def update_tax_adjustments
202202
Spree::Config.tax_adjuster_class.new(order).adjust!
203-
204-
[*line_items, *shipments].each do |item|
205-
tax_adjustments = item.adjustments.select { |adjustment|
206-
adjustment.tax? && !adjustment.marked_for_destruction?
207-
}
208-
209-
# Tax adjustments come in not one but *two* exciting flavours:
210-
# Included & additional
211-
212-
# Included tax adjustments are those which are included in the price.
213-
# These ones should not affect the eventual total price.
214-
#
215-
# Additional tax adjustments are the opposite, affecting the final total.
216-
item.included_tax_total = tax_adjustments.select(&:included?).sum(&:amount)
217-
item.additional_tax_total = tax_adjustments.reject(&:included?).sum(&:amount)
218-
end
219203
end
220204

221205
def update_cancellations
@@ -224,21 +208,17 @@ def update_cancellations
224208

225209
def update_item_totals
226210
[*line_items, *shipments].each do |item|
227-
# The cancellation_total isn't persisted anywhere but is included in
228-
# the adjustment_total
229-
item.adjustment_total = item.adjustments.
230-
reject { |adjustment| adjustment.included? || adjustment.marked_for_destruction? }.
231-
sum(&:amount)
232-
233-
if item.changed?
234-
item.update_columns(
235-
promo_total: item.promo_total,
236-
included_tax_total: item.included_tax_total,
237-
additional_tax_total: item.additional_tax_total,
238-
adjustment_total: item.adjustment_total,
239-
updated_at: Time.current,
240-
)
241-
end
211+
Spree::ItemTotal.new(item).recalculate!
212+
213+
next unless item.changed?
214+
215+
item.update_columns(
216+
promo_total: item.promo_total,
217+
included_tax_total: item.included_tax_total,
218+
additional_tax_total: item.additional_tax_total,
219+
adjustment_total: item.adjustment_total,
220+
updated_at: Time.current,
221+
)
242222
end
243223
end
244224
end

0 commit comments

Comments
 (0)