Sha256: e69cd6d16c63970c0da121733f78cb3d066c2b98c3b24089d0fc89ae4fe6208b

Contents?: true

Size: 1002 Bytes

Versions: 64

Compression:

Stored size: 1002 Bytes

Contents

module Spree
  class Promotion
    module Actions
      class CreateItemAdjustments < PromotionAction
        include Spree::CalculatedAdjustments
        include Spree::AdjustmentSource

        before_validation -> { self.calculator ||= Calculator::PercentOnLineItem.new }

        def perform(options = {})
          order     = options[:order]
          promotion = options[:promotion]

          create_unique_adjustments(order, order.line_items) do |line_item|
            promotion.line_item_actionable?(order, line_item)
          end
        end

        def compute_amount(line_item)
          return 0 unless promotion.line_item_actionable?(line_item.order, line_item)

          amounts = [line_item.amount, compute(line_item)]
          order   = line_item.order

          # Prevent negative order totals
          amounts << order.amount - order.adjustments.eligible.sum(:amount).abs if order.adjustments.eligible.any?

          amounts.min * -1
        end
      end
    end
  end
end

Version data entries

64 entries across 64 versions & 1 rubygems

Version Path
spree_core-4.10.1 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.10.0 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.9.0 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.8.3 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.8.2 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.7.3 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.6.6 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.5.5 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.0.9 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.1.15 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.2.7 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.3.3 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.4.1 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.5.4 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.6.5 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.7.2 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.7.1 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.6.4 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.7.0 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.6.3 app/models/spree/promotion/actions/create_item_adjustments.rb