Sha256: d9a6a8ae7fb9f5ed63153448b8d4a9a04bc050f24682a2e9e8d8558a7cc726f5

Contents?: true

Size: 842 Bytes

Versions: 8

Compression:

Stored size: 842 Bytes

Contents

class PromotionCredit < ::Adjustment
  belongs_to :order
  scope :with_order, :conditions => "order_id IS NOT NULL"

  def calculate_adjustment
    adjustment_source && calculate_coupon_credit
  end

  # Checks if credit is still applicable to order
  # If source of adjustment is credit, it checks if it's still valid
  def applicable?
    adjustment_source && adjustment_source.eligible?(order) && super
  end

  # Calculates credit for the coupon.
  #
  # If coupon amount exceeds the order item_total, credit is adjusted.
  #
  # Always returns negative non positive.
  def calculate_coupon_credit
    return 0 if order.line_items.empty?
    amount = adjustment_source.calculator.compute(order.line_items).abs
    amount = order.item_total if amount > order.item_total
    -1 * amount
  end
  
  def total
    map(&:amount).sum
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
spree_promo-0.30.2 app/models/promotion_credit.rb
spree_promo-0.40.4 app/models/promotion_credit.rb
spree_promo-0.40.3 app/models/promotion_credit.rb
spree_promo-0.40.2 app/models/promotion_credit.rb
spree_promo-0.40.1 app/models/promotion_credit.rb
spree_promo-0.40.0 app/models/promotion_credit.rb
spree_promo-0.30.1 app/models/promotion_credit.rb
spree_promo-0.30.0 app/models/promotion_credit.rb