Sha256: f9aa4de1de55abd74236ea90cc174dc6f47071f430a9fc6524ac9fbd3b5974ab

Contents?: true

Size: 1.41 KB

Versions: 43

Compression:

Stored size: 1.41 KB

Contents

module Spree
  module Adjustable
    module Adjuster
      class Promotion < Spree::Adjustable::Adjuster::Base
        def update
          promo_adjustments = adjustments.competing_promos.reload.map { |a| a.update!(adjustable) }
          promos_total = promo_adjustments.compact.sum
          choose_best_promo_adjustment unless promos_total == 0
          promo_total = best_promo_adjustment.try(:amount).to_f if best_promo_adjustment.try(:promotion?)

          update_totals(promo_total)
        end

        private

        # Picks one (and only one) competing discount to be eligible for
        # this order. This adjustment provides the most discount, and if
        # two adjustments have the same amount, then it will pick the
        # latest one.
        def choose_best_promo_adjustment
          if best_promo_adjustment
            other_promotions = adjustments.competing_promos.where.not(id: best_promo_adjustment.id)
            other_promotions.update_all(eligible: false)
          end
        end

        def best_promo_adjustment
          @best_promo_adjustment ||= begin
            adjustments.competing_promos.eligible.reorder("amount ASC, created_at DESC, id DESC").first
          end
        end

        def update_totals(promo_total)
          promo_total ||= 0.0
          @totals[:promo_total] = promo_total
          @totals[:taxable_adjustment_total] += promo_total
        end
      end
    end
  end
end

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
spree_core-3.2.9 app/models/spree/adjustable/adjuster/promotion.rb
spree_core-3.1.14 app/models/spree/adjustable/adjuster/promotion.rb
spree_core-3.3.6 app/models/spree/adjustable/adjuster/promotion.rb
spree_core-3.2.8 app/models/spree/adjustable/adjuster/promotion.rb
spree_core-3.1.13 app/models/spree/adjustable/adjuster/promotion.rb
spree_core-3.3.5 app/models/spree/adjustable/adjuster/promotion.rb
spree_core-3.3.4 app/models/spree/adjustable/adjuster/promotion.rb
spree_core-3.2.7 app/models/spree/adjustable/adjuster/promotion.rb
spree_core-3.1.12 app/models/spree/adjustable/adjuster/promotion.rb
spree_core-3.3.3 app/models/spree/adjustable/adjuster/promotion.rb
spree_core-3.2.6 app/models/spree/adjustable/adjuster/promotion.rb
spree_core-3.1.11 app/models/spree/adjustable/adjuster/promotion.rb
spree_core-3.1.10 app/models/spree/adjustable/adjuster/promotion.rb
spree_core-3.3.2 app/models/spree/adjustable/adjuster/promotion.rb
spree_core-3.2.5 app/models/spree/adjustable/adjuster/promotion.rb
spree_core-3.1.9 app/models/spree/adjustable/adjuster/promotion.rb
spree_core-3.3.1 app/models/spree/adjustable/adjuster/promotion.rb
spree_core-3.2.4 app/models/spree/adjustable/adjuster/promotion.rb
spree_core-3.1.8 app/models/spree/adjustable/adjuster/promotion.rb
spree_core-3.3.0 app/models/spree/adjustable/adjuster/promotion.rb