Class: Logistics::Core::Currency

Inherits:
Lookup show all
Defined in:
app/models/logistics/core/currency.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

as_json

Class Method Details

.convert_currency(suggested_currency, default_currency, unit_price) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/logistics/core/currency.rb', line 17

def self.convert_currency(suggested_currency, default_currency, unit_price)
  result = []
  if not suggested_currency.nil? and not suggested_currency == ''
    if suggested_currency != default_currency
      d_currency = Currency.find(default_currency)
      s_currency = Currency.find(suggested_currency)
      if s_currency.current_selling_rate == 1
        unit_price = d_currency.current_selling_rate * unit_price
      else
        unit_price = (d_currency.current_buying_rate/s_currency.current_buying_rate)*unit_price
      end
      return result.push(unit_price, suggested_currency)
    end
  end
  return result.push(unit_price, default_currency)
end

Instance Method Details

#current_buying_rateObject



11
12
13
14
15
# File 'app/models/logistics/core/currency.rb', line 11

def current_buying_rate
  current_rate = self.currency_rates.where('rate_date <= ? AND currency_id = ?', Time.zone.now, self.id)
                     .order('rate_date DESC').first
  current_rate.rate_to_base_buying
end

#current_selling_rateObject



5
6
7
8
9
# File 'app/models/logistics/core/currency.rb', line 5

def current_selling_rate
  current_rate = self.currency_rates.where('rate_date <= ? AND currency_id = ?', Time.zone.now, self.id)
                     .order('rate_date DESC').first
  current_rate.rate_to_base_selling
end