Class: Gera::DirectionRate
Overview
Конечный курс Finite direction rate
Constant Summary
collapse
- UnknownExchangeRate =
Class.new StandardError
Instance Method Summary
collapse
#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_rate ⇒ Object
85
86
87
|
# File 'app/models/gera/direction_rate.rb', line 85
def base_rate
RateFromMultiplicator.new(base_rate_value).freeze
end
|
#calculate_rate ⇒ Object
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_pair ⇒ Object
49
50
51
|
# File 'app/models/gera/direction_rate.rb', line 49
def currency_pair
@currency_pair ||= CurrencyPair.new income_currency, outcome_currency
end
|
#dump ⇒ Object
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_notification ⇒ Object
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_money ⇒ Object
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_currency ⇒ Object
53
54
55
|
# File 'app/models/gera/direction_rate.rb', line 53
def income_currency
ps_from.currency
end
|
#inverse_direction_rate ⇒ Object
#out_money ⇒ Object
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_currency ⇒ Object
57
58
59
|
# File 'app/models/gera/direction_rate.rb', line 57
def outcome_currency
ps_to.currency
end
|
#ps_comission ⇒ Object
61
62
63
|
# File 'app/models/gera/direction_rate.rb', line 61
def ps_comission
ps_to.commission
end
|
#rate_money ⇒ Object
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
|