Sha256: c83d480bfdd6082bf6a26b9377335158bc18614544b366101376725e8629eb78

Contents?: true

Size: 877 Bytes

Versions: 10

Compression:

Stored size: 877 Bytes

Contents

class Promotion::Actions::CreateAdjustment < PromotionAction

  calculated_adjustments

  before_create do |a|
    c = a.build_calculator
    c.type = Promotion::Actions::CreateAdjustment.calculators.first.to_s
  end

  def perform(options = {})
    return unless order = options[:order]
    return if order.promotion_credit_exists?(promotion)
    if amount = calculator.compute(order)
      amount = order.item_total if amount > order.item_total
      order.adjustments.promotion.reload.clear
      order.update!
      create_adjustment("#{I18n.t(:promotion)} (#{promotion.name})", order, order)
    end
  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

  delegate :eligible?, :to => :promotion

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
spree_promo-0.70.7 app/models/promotion/actions/create_adjustment.rb
spree_promo-0.70.6 app/models/promotion/actions/create_adjustment.rb
spree_promo-0.70.5 app/models/promotion/actions/create_adjustment.rb
spree_promo-0.70.4 app/models/promotion/actions/create_adjustment.rb
spree_promo-0.70.3 app/models/promotion/actions/create_adjustment.rb
spree_promo-0.70.2 app/models/promotion/actions/create_adjustment.rb
spree_promo-0.70.1 app/models/promotion/actions/create_adjustment.rb
spree_promo-0.70.0 app/models/promotion/actions/create_adjustment.rb
spree_promo-0.70.0.rc2 app/models/promotion/actions/create_adjustment.rb
spree_promo-0.70.RC1 app/models/promotion/actions/create_adjustment.rb