Class: Spree::PriceBook

Inherits:
Base
  • Object
show all
Defined in:
app/models/spree/price_book.rb

Instance Method Summary collapse

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'app/models/spree/price_book.rb', line 57

def active?
  active_from <= Time.zone.now && (active_to.nil? || active_to >= Time.zone.now)
end

#explicit?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'app/models/spree/price_book.rb', line 49

def explicit?
  parent_id.blank?
end

#factored?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'app/models/spree/price_book.rb', line 53

def factored?
  parent_id.present?
end

#load_prices_from_parent(force_update = false) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/spree/price_book.rb', line 61

def load_prices_from_parent(force_update = false)
  return if explicit?

  parent.prices.each do |parent_price|
    price = prices.find_or_initialize_by(
      currency: currency,
      variant_id: parent_price.variant_id,
    )

    if force_update || price.new_record?
      price.amount = parent_price.amount * price_adjustment_factor
      price.save!
    end
  end
end

#set_price_adjustment_factorObject



77
78
79
80
81
82
83
84
85
86
87
# File 'app/models/spree/price_book.rb', line 77

def set_price_adjustment_factor
  if price_adjustment_factor.blank?
    if parent.present? && parent.currency != currency && 
      self.price_adjustment_factor = Spree::CurrencyRate
        .find_by(base_currency: parent.currency, currency: currency)
        .try(:exchange_rate)
    else
      self.price_adjustment_factor = 1.0
    end
  end
end