Skip to content
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
2 changes: 1 addition & 1 deletion core/app/models/spree/order_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def recalculate_order_total
def update_adjustment_total
update_adjustments

all_items = line_items + shipments
all_items = (line_items + shipments).reject(&:marked_for_destruction?)
# Ignore any adjustments that have been marked for destruction in our
# calculations. They'll get removed when/if we persist the order.
valid_adjustments = adjustments.reject(&:marked_for_destruction?)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module SpreeOrderUpdaterPatch
def update_adjustment_total
update_adjustments

all_items = line_items + shipments
all_items = (line_items + shipments).reject(&:marked_for_destruction?)
order_tax_adjustments = adjustments.select(&:eligible?).select(&:tax?)

order.adjustment_total = all_items.sum(&:adjustment_total) + adjustments.select(&:eligible?).sum(&:amount)
Expand All @@ -25,10 +25,11 @@ def recalculate_item_totals
# Core doesn't have "eligible" adjustments anymore, so we need to
# override the adjustment_total calculation to exclude them for legacy
# promotions.
item.adjustment_total = item.adjustments.
select(&:eligible?).
reject(&:included?).
sum(&:amount)
item.adjustment_total = item.adjustments.select { |adjustment|
adjustment.eligible? &&
!adjustment.marked_for_destruction? &&
!adjustment.included?
}.sum(&:amount)

if item.changed?
item.update_columns(
Expand Down
10 changes: 6 additions & 4 deletions promotions/app/models/solidus_promotions/order_adjuster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ def call
order.reset_current_discounts

unless dry_run
# Since automations might have added a line item, we need to recalculate item total and item count here.
order.item_total = order.line_items.sum(&:amount)
order.item_count = order.line_items.sum(&:quantity)
order.promo_total = (order.line_items + order.shipments).sum(&:promo_total)
# Since automations might have added a line item, we need to recalculate
# item total and item count here.
line_items = order.line_items.reject(&:marked_for_destruction?)
order.item_total = line_items.sum(&:amount)
order.item_count = line_items.sum(&:quantity)
order.promo_total = (line_items + order.shipments).sum(&:promo_total)
end
order
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ def call

attr_reader :order

# Walk through the discounts for an item and update adjustments for it. Once
# all of the discounts have been added as adjustments, remove any old promotion
# adjustments that weren't touched.
# Walk through the discounts for an item and update adjustments for it.
# Once all of the discounts have been added as adjustments, mark any old
# promotion adjustments that weren't touched for destruction.
#
# @private
# @param [#adjustments] item a {Spree::LineItem} or {Spree::Shipment}
# @param [Array<SolidusPromotions::ItemDiscount>] item_discounts a list of calculated discounts for an item
# @param [Array<SolidusPromotions::ItemDiscount>] item_discounts a list of
# calculated discounts for an item
# @return [void]
def update_adjustments(item, item_discounts)
promotion_adjustments = item.adjustments.select(&:promotion?)
Expand All @@ -52,7 +53,7 @@ def update_adjustments(item, item_discounts)
# Remove any promotion adjustments tied to promotion benefits which no longer match.
unmatched_adjustments = promotion_adjustments - active_adjustments

item.adjustments.destroy(unmatched_adjustments)
unmatched_adjustments.each(&:mark_for_destruction)
end

# Update or create a new promotion adjustment on an item.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,12 @@
let(:old_promotion_benefit) { create(:promotion, :with_adjustable_action, apply_automatically: false).actions.first }
let!(:adjustment) { create(:adjustment, source: old_promotion_benefit, adjustable: line_item) }

it "removes the old adjustment from the line item" do
it "marks the old adjustment for destruction" do
adjustable.reload
expect {
subject
}.to change { adjustable.reload.adjustments.length }.by(-1)
}.to change { adjustable.adjustments.first.marked_for_destruction? }
.from(false).to(true)
end
end
end
Expand Down
Loading