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’) note: since JPY doesn’t have cents (the price doesn’t get multiplied by 100)

then we need to do that in order to calculate the proper exchange rate



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/models/workshop_price.rb', line 130

def bank
  unless @bank
    @bank = Money::Bank::VariableExchange.new
    base_cents = price_currency == 'JPY' ? (price_cents.to_f * 100) : price_cents.to_f
    unless alt1_price_currency.blank?
      alt1_cents = alt1_price_currency == 'JPY' ? (alt1_price_cents.to_f * 100) : alt1_price_cents.to_f
      @bank.add_rate(price_currency, alt1_price_currency, alt1_cents / base_cents)
      @bank.add_rate(alt1_price_currency, price_currency, base_cents / alt1_cents)
    end
    unless alt2_price_currency.blank?
      alt2_cents = alt2_price_currency == 'JPY' ? (alt2_price_cents.to_f * 100) : alt2_price_cents.to_f
      @bank.add_rate(price_currency, alt2_price_currency, alt2_cents / base_cents)
      @bank.add_rate(alt2_price_currency, price_currency, base_cents / alt2_cents)
    end
    if !alt1_price_currency.blank? && !alt2_price_currency.blank?
      @bank.add_rate(alt1_price_currency, alt2_price_currency, alt2_cents / alt1_cents)
      @bank.add_rate(alt2_price_currency, alt1_price_currency, alt1_cents / alt2_cents)
    end
  end
  return @bank
end

#currency_listObject

return list of currencies used, in a format for a dropdown list ex: [[‘USD’, ‘USD’], [‘EUR’, ‘EUR’]]




112
113
114
115
116
117
# File 'app/models/workshop_price.rb', line 112

def currency_list
  list = [[price_currency, price_currency]]
  list << [alt1_price_currency, alt1_price_currency] unless alt1_price_currency.blank?
  list << [alt2_price_currency, alt2_price_currency] unless alt2_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

#payment_schedule(from_date = nil) ⇒ Object

return array of when payments should be made. if ‘from_date` is specified, then actual dates are returned. Otherwise number of days




85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/workshop_price.rb', line 85

def payment_schedule(from_date = nil)
  schedule = []
  if recurring_payments?
    (0...recurring_number).each do |period|
      xdays = period * recurring_period
      schedule << {due_on: (from_date ? from_date.to_date + xdays.days : xdays),
                   period_payment: payment_price,
                   total_due: (period + 1) * payment_price}
    end
    # adjust the last entry
    schedule.last[:total_due] = price
    schedule.last[:period_payment] = price - (recurring_number - 1) * payment_price
  else
    schedule << {due_on: (from_date ? from_date.to_date : 0), period_payment: payment_price, total_due: payment_price}
  end
  schedule
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.nil? ? false : (num_sold >= total_available)
end

#specific_payment_schedule(from_date, on_date = Date.today) ⇒ Object

return the payment schedule entry that is before the specified date




105
106
107
# File 'app/models/workshop_price.rb', line 105

def specific_payment_schedule(from_date, on_date = Date.today)
  payment_schedule(from_date).reverse.detect {|item| item[:due_on] <= on_date}
end

#to_base_currency(money) ⇒ Object

Convert an amount in an alternate currency into the base currency




121
122
123
# File 'app/models/workshop_price.rb', line 121

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 > Date.today) || (!valid_until.nil? && valid_until < Date.today))
end