Class: WorkshopPrice

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
RankedModel
Defined in:
app/models/workshop_price.rb

Overview

todo

re-evaluate whether alternate currencies are really needed. I can’t

think of a real use case at the moment, and it adds unnecessary complexity.

Note: The currency of a WorkshopPrice is the base currency of it’s workshop. If they were different, we would need to always have an exchange rate available to convert to/from the workshop and the price currency. With the alternate price/currencies, the exchange rate is directly based on the prices in the two currencies.


Constant Summary collapse

PAYMENT_METHODS =
['Cash', 'Check', 'Credit Card', 'Money Order', 'PayPal', 'Wire Transfer']

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepare_prices(attributes = {}) ⇒ Object

For some reason, the initial monetized price gets created with the default Money currency. Need to use the current currency, as the internal fractional value depends on it. For example,

"15000".to_money('JPY').cents == 15000
"15000".to_money('EUR').cents == 1500000

Call this method on the attributes before passing into new() or update_attributes()




47
48
49
50
51
52
# File 'app/models/workshop_price.rb', line 47

def self.prepare_prices(attributes = {})
  attributes['price']       = attributes['price'].to_money(attributes['price_currency']) if attributes['price'].present? && attributes['price_currency'].present?
  attributes['alt1_price']  = attributes['alt1_price'].to_money(attributes['alt1_price_currency']) if attributes['alt1_price'].present? && attributes['alt1_price_currency'].present?
  attributes['alt2_price']  = attributes['alt2_price'].to_money(attributes['alt2_price_currency']) if attributes['alt2_price'].present? && attributes['alt2_price_currency'].present?
  return attributes
end

Instance Method Details

#bankObject

return a bank object filled with the exchange rates, based on the prices. then you can do: bank.exchange_with(price, ‘USD’)




99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/models/workshop_price.rb', line 99

def bank
  unless @bank
    @bank = Money::Bank::VariableExchange.new
    unless alt1_price_currency.blank?
      @bank.add_rate(price_currency, alt1_price_currency, alt1_price_cents.to_f / price_cents.to_f)
      @bank.add_rate(alt1_price_currency, price_currency, price_cents.to_f / alt1_price_cents.to_f)
    end
    unless alt2_price_currency.blank?
      @bank.add_rate(price_currency, alt2_price_currency, alt2_price_cents.to_f / price_cents.to_f)
      @bank.add_rate(alt2_price_currency, price_currency, price_cents.to_f / alt2_price_cents.to_f)
    end
    unless alt1_price_currency.blank? && alt2_price_currency.blank?
      @bank.add_rate(alt1_price_currency, alt2_price_currency, alt2_price_cents.to_f / alt1_price_cents.to_f)
      @bank.add_rate(alt2_price_currency, alt1_price_currency, alt1_price_cents.to_f / alt2_price_cents.to_f)
    end
  end
  return @bank
end

#currency_listObject




83
84
85
86
87
88
# File 'app/models/workshop_price.rb', line 83

def currency_list
  list = [[price_currency, price_currency]]
  list << [alt1_price_currency, alt1_price_currency] unless alt1_price_currency.blank?
  list << [alt2_price_currency, alt1_price_currency] unless alt1_price_currency.blank?
  return list
end

#payment_priceObject

returns the amount of a payment




73
74
75
# File 'app/models/workshop_price.rb', line 73

def payment_price
  recurring_payments? ? (price / recurring_number) : price
end

#price_formattedObject




67
68
69
# File 'app/models/workshop_price.rb', line 67

def price_formatted
  price.nil? ? '' : price.format(no_cents_if_whole: true, symbol: true)
end

#recurring_payments?Boolean


Returns:

  • (Boolean)


78
79
80
# File 'app/models/workshop_price.rb', line 78

def recurring_payments?
  recurring_number.to_i > 1
end

#sold_out?(num_sold) ⇒ Boolean

If the total_available is nil, then there are unlimited tickets to be sold.

Otherwise, check if we have sold out


Returns:

  • (Boolean)


62
63
64
# File 'app/models/workshop_price.rb', line 62

def sold_out?(num_sold)
  (total_available.blank? or total_available == 0) ? false : (num_sold >= total_available)
end

#to_base_currency(money) ⇒ Object

Convert an amount in an alternate currency into the base currency




92
93
94
# File 'app/models/workshop_price.rb', line 92

def to_base_currency(money)
  bank.exchange_with(money, price_currency)
end

#visible?Boolean


Returns:

  • (Boolean)


55
56
57
# File 'app/models/workshop_price.rb', line 55

def visible?
  !(disabled? || (!valid_starting_on.nil? && valid_starting_on > Time.now.to_date) || (!valid_until.nil? && valid_until < Time.now.to_date))
end