Class: Gera::DirectionRate

Inherits:
ApplicationRecord show all
Includes:
Authority::Abilities, AutoLogger, DirectionSupport, Mathematic
Defined in:
app/models/gera/direction_rate.rb

Overview

Конечный курс Finite direction rate

Constant Summary collapse

UnknownExchangeRate =
Class.new StandardError

Instance Method Summary collapse

Methods included from DirectionSupport

#direction, #direction=

Methods included from Mathematic

#calculate_base_rate, #calculate_comission, #calculate_finite_rate, #calculate_profits, #calculate_total_using_regular_comission, #calculate_total_using_reverse_comission, #diff_percents, #money_exchange, #money_reverse_exchange

Instance Method Details

#base_rateObject



85
86
87
# File 'app/models/gera/direction_rate.rb', line 85

def base_rate
  RateFromMultiplicator.new(base_rate_value).freeze
end

#calculate_rateObject



128
129
130
131
132
133
134
# File 'app/models/gera/direction_rate.rb', line 128

def calculate_rate
  self.base_rate_value = currency_rate.rate_value
  raise UnknownExchangeRate, "No exchange_rate for #{ps_from}->#{ps_to}" unless exchange_rate

  self.rate_percent = exchange_rate.comission_percents
  self.rate_value = calculate_finite_rate base_rate_value, rate_percent unless rate_percent.nil?
end

#currency_pairObject



49
50
51
# File 'app/models/gera/direction_rate.rb', line 49

def currency_pair
  @currency_pair ||= CurrencyPair.new income_currency, outcome_currency
end

#dumpObject



108
109
110
111
# File 'app/models/gera/direction_rate.rb', line 108

def dump
  as_json(only: %i[id ps_from_id ps_to_id currency_rate_id rate_value base_rate_value rate_percent created_at])
    .merge currency_rate: currency_rate.dump, dump_version: 1
end

#exchange(amount) ⇒ Object



41
42
43
# File 'app/models/gera/direction_rate.rb', line 41

def exchange(amount)
  rate.exchange amount, outcome_currency
end

#exchange_notificationObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/models/gera/direction_rate.rb', line 113

def exchange_notification
  ExchangeNotification.find_by(
    income_payment_system_id: income_payment_system_id,
    outcome_payment_system_id: outcome_payment_system_id
  ) ||
    ExchangeNotification.find_by(
      income_payment_system_id: income_payment_system_id,
      outcome_payment_system_id: nil
    ) ||
    ExchangeNotification.find_by(
      income_payment_system_id: nil,
      outcome_payment_system_id: outcome_payment_system_id
    )
end

#get_profit_result(income_amount) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/models/gera/direction_rate.rb', line 93

def get_profit_result(income_amount)
  res = calculate_profits(
    base_rate: base_rate_value,
    comission: rate_percent,
    ps_interest: ps_comission,
    income_amount: income_amount
  )

  diff = res.finite_rate.to_f.as_percentage_of(rate_value.to_f) - 100

  logger.warn "direction_rate_id=#{id} Calculates finite rate (#{res.finite_rate}) does not equal to current (#{rate_value}). Difference is #{diff}" if diff.abs > 0

  res
end

#in_moneyObject



65
66
67
68
69
# File 'app/models/gera/direction_rate.rb', line 65

def in_money
  return 1 if rate_value < 1

  rate_value
end

#income_currencyObject



53
54
55
# File 'app/models/gera/direction_rate.rb', line 53

def income_currency
  ps_from.currency
end

#inverse_direction_rateObject



89
90
91
# File 'app/models/gera/direction_rate.rb', line 89

def inverse_direction_rate
  Universe.direction_rates_repository.get_matrix[ps_to_id][ps_from_id]
end

#out_moneyObject



71
72
73
74
75
# File 'app/models/gera/direction_rate.rb', line 71

def out_money
  return 1.0 / rate_value if rate_value < 1

  1
end

#outcome_currencyObject



57
58
59
# File 'app/models/gera/direction_rate.rb', line 57

def outcome_currency
  ps_to.currency
end

#ps_comissionObject



61
62
63
# File 'app/models/gera/direction_rate.rb', line 61

def ps_comission
  ps_to.commission
end

#rateObject



77
78
79
# File 'app/models/gera/direction_rate.rb', line 77

def rate
  RateFromMultiplicator.new(rate_value).freeze
end

#rate_moneyObject



81
82
83
# File 'app/models/gera/direction_rate.rb', line 81

def rate_money
  Money.from_amount(rate_value, currency_rate.currency_to)
end

#reverse_exchange(amount) ⇒ Object



45
46
47
# File 'app/models/gera/direction_rate.rb', line 45

def reverse_exchange(amount)
  rate.reverse_exchange amount, income_currency
end