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.6.2 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.6.1 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.6.0 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.5.3 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.5.2 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.5.1 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.5.0 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.2.6 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.3.2 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.4.0 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.4.0.rc2 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.3.1 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.4.0.rc1 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.3.0 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.3.0.rc3 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.3.0.rc2 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.3.0.rc1 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.2.5 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.1.14 app/models/spree/promotion/actions/create_item_adjustments.rb
spree_core-4.0.8 app/models/spree/promotion/actions/create_item_adjustments.rb