Module: SolidusLegacyPromotions::SpreeOrderUpdaterPatch

Defined in:
app/patches/models/solidus_legacy_promotions/spree_order_updater_patch.rb

Instance Method Summary collapse

Instance Method Details

#recalculate_item_totalsObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/patches/models/solidus_legacy_promotions/spree_order_updater_patch.rb', line 18

def recalculate_item_totals
  [*line_items, *shipments].each do |item|
    Spree::Config.item_total_class.new(item).recalculate!

    # The cancellation_total isn't persisted anywhere but is included in
    # the adjustment_total.
    #
    # 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 { |adjustment|
      adjustment.eligible? &&
        !adjustment.marked_for_destruction? &&
        !adjustment.included?
    }.sum(&:amount)

    next unless item.changed?

    item.assign_attributes(
      promo_total: item.promo_total,
      included_tax_total: item.included_tax_total,
      additional_tax_total: item.additional_tax_total,
      adjustment_total: item.adjustment_total
    )
  end
end

#update_adjustment_totalObject



5
6
7
8
9
10
11
12
13
14
15
16
# File 'app/patches/models/solidus_legacy_promotions/spree_order_updater_patch.rb', line 5

def update_adjustment_total
  update_adjustments

  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)
  order.included_tax_total = all_items.sum(&:included_tax_total) + order_tax_adjustments.select(&:included?).sum(&:amount)
  order.additional_tax_total = all_items.sum(&:additional_tax_total) + order_tax_adjustments.reject(&:included?).sum(&:amount)

  recalculate_order_total
end