Sha256: 44f5fb519c9991354fa18a5801b448e79484a3b63ab0ad631bfb18cfaf490f0d

Contents?: true

Size: 1.39 KB

Versions: 3

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module SolidusLegacyPromotions
  module SpreeOrderUpdaterDecorator
    def update_adjustment_total
      recalculate_adjustments

      all_items = line_items + shipments
      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)

      update_order_total
    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.adjustment_total = item.adjustments.
          select(&:eligible?).
          reject(&:included?).
          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
      end
    end
    Spree::OrderUpdater.prepend self
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
solidus_legacy_promotions-4.4.2 app/decorators/solidus_legacy_promotions/models/spree_order_updater_decorator.rb
solidus_legacy_promotions-4.4.1 app/decorators/solidus_legacy_promotions/models/spree_order_updater_decorator.rb
solidus_legacy_promotions-4.4.0 app/decorators/solidus_legacy_promotions/models/spree_order_updater_decorator.rb