Sha256: b6381f1e9a7db75b9e37736f85bd5581a5580c4a75c2e870fa1d0afb13b30560

Contents?: true

Size: 1.67 KB

Versions: 6

Compression:

Stored size: 1.67 KB

Contents

module Spree
  class Promotion
    module Actions
      class CreateAdjustment < PromotionAction
        calculated_adjustments

        delegate :eligible?, :to => :promotion

        before_validation :ensure_action_has_calculator

        def perform(options = {})
          return unless order = options[:order]
          # Nothing to do if the promotion is already associated with the order
          return if order.promotion_credit_exists?(promotion)

          order.adjustments.promotion.reload.clear
          order.update!
          create_adjustment("#{I18n.t(:promotion)} (#{promotion.name})", order, order)
          order.update!
        end

        # override of CalculatedAdjustments#create_adjustment so promotional
        # adjustments are added all the time. They will get their eligability
        # set to false if the amount is 0
        def create_adjustment(label, target, calculable, mandatory=false)
          amount = compute_amount(calculable)
          params = { :amount => amount,
                    :source => calculable,
                    :originator => self,
                    :label => label,
                    :mandatory => mandatory }
          target.adjustments.create(params, :without_protection => true)
        end

        # Ensure a negative amount which does not exceed the sum of the order's item_total and ship_total
        def compute_amount(calculable)
          [(calculable.item_total + calculable.ship_total), super.to_f.abs].min * -1
        end

        private
        def ensure_action_has_calculator
          return if self.calculator
          self.calculator = Calculator::FlatPercentItemTotal.new
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
spree_promo-1.1.6 app/models/spree/promotion/actions/create_adjustment.rb
spree_promo-1.1.5 app/models/spree/promotion/actions/create_adjustment.rb
spree_promo-1.1.4 app/models/spree/promotion/actions/create_adjustment.rb
spree_promo-1.1.3 app/models/spree/promotion/actions/create_adjustment.rb
spree_promo-1.1.2 app/models/spree/promotion/actions/create_adjustment.rb
spree_promo-1.1.2.rc1 app/models/spree/promotion/actions/create_adjustment.rb