6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
# File 'lib/ru_exchange.rb', line 6
def self.update_currencies
RuExchange.log "Updating currency. Time: " + Time.now.strftime('%d.%m.%Y %H:%M')
cur = Dashboard::Currency.find_or_create_by(id: 1)
begin
cur_new = Dashboard::Currency.where('created_at >= ?', 24.hours.ago).first || Dashboard::Currency.new
rescue ActiveRecord::RecordNotUnique
retry
end
cur_new = Dashboard::Currency.new if (0..3).include?(Time.now.strftime('%H').to_i)
RuExchange.log 'Currency is new record: ' + cur_new.new_record?.to_s
RuExchange.log ''
if true
update_uah = true
update_rur = true
end
begin
if update_rur && result_rur = RuExchange.get_rur
cur.rur = result_rur
cur.rur_changed_at = Time.now
pack_rur = Dashboard::Package.where(rur_auto: true)
RuExchange.log 'Preparing to update package prices in rur'
pack_rur.each do |pack|
unless pack.usd.nil?
pack.rur = Dashboard::Currency.auto_price(pack.usd.to_f * result_rur.to_f)
pack.rur_changed_at = Time.now
pack.rur_changed_by = -1
puts pack.errors.inspect unless pack.save
RuExchange.log 'Package: ' + pack.name
RuExchange.log 'Package USD: ' + pack.usd.to_s
RuExchange.log "Math RUR: #{pack.usd.to_s} * #{result_rur.to_s} = #{pack.usd.to_f * result_rur.to_f}"
RuExchange.log 'Package RUR AutoPrice: ' + pack.rur.to_s
RuExchange.log '------'
r_c = Dashboard::ReportCurrency.new
r_c.package_id = pack.id
r_c.currency = 'RUR'
r_c.ex_rate = result_rur.to_f
r_c.price_in_usd = pack.usd
r_c.price_in_curr = pack.usd.to_f * result_rur.to_f
r_c.auto_price = Dashboard::Currency.auto_price(pack.usd.to_f * result_rur.to_f)
r_c.error = nil
r_c.status = 1
r_c.save
else
RuExchange.log 'No USD set for package'
r_c = Dashboard::ReportCurrency.new
r_c.error = ['custom' => 'Не задана цена в долларах'].to_json
r_c.status = 3
r_c.currency = 'RUR'
r_c.package_id = pack.id
r_c.ex_rate = result_rur.to_f
r_c.save
end
end
else
RuExchange.log 'Not new record. Nothing to save'
end
if cur_new.rur_changed_at.nil?
cur_new.rur = result_rur
cur_new.rur_changed_at = Time.now
end
rescue => exception
RuExchange.log exception.class.to_s
RuExchange.log exception.to_s
RuExchange.log exception.backtrace.join("\n")
r_c = Dashboard::ReportCurrency.new
r_c.error = [exception.to_s => exception.backtrace.join("\n")].to_json
r_c.status = 2
puts r_c.errors.inspect unless r_c.save
end
RuExchange.log ''
begin
if update_uah && result_uah = RuExchange.get_uah
cur.uah = result_uah
cur.uah_changed_at = Time.now
pack_uah = Dashboard::Package.where(uah_auto: true)
RuExchange.log 'Preparing to update package prices in uah'
pack_uah.each do |pack|
unless pack.usd.nil?
pack.uah = Dashboard::Currency.auto_price(pack.usd.to_f * result_uah.to_f)
pack.uah_changed_at = Time.now
pack.uah_changed_by = -1
pack.save
RuExchange.log 'Package: ' + pack.name
RuExchange.log 'Package USD: ' + pack.usd.to_s
RuExchange.log "Math UAH: #{pack.usd.to_s} * #{result_uah.to_s} = #{pack.usd.to_f * result_uah.to_f}"
RuExchange.log 'Package UAH AutoPrice: ' + pack.uah.to_s
RuExchange.log '------'
r_c = Dashboard::ReportCurrency.new
r_c.package_id = pack.id
r_c.currency = 'UAH'
r_c.ex_rate = result_uah.to_f
r_c.price_in_usd = pack.usd
r_c.price_in_curr = pack.usd.to_f * result_uah.to_f
r_c.auto_price = Dashboard::Currency.auto_price(pack.usd.to_f * result_uah.to_f)
r_c.error = nil
r_c.status = 1
r_c.save
else
RuExchange.log 'No USD set for package'
r_c = Dashboard::ReportCurrency.new
r_c.error = ['custom' => 'Не задана цена в долларах'].to_json
r_c.status = 3
r_c.currency = 'UAH'
r_c.package_id = pack.id
r_c.ex_rate = result_uah.to_f
r_c.save
end
end
end
if cur_new.uah_changed_at.nil?
cur_new.uah = result_uah
cur_new.uah_changed_at = Time.now
end
rescue => exception
RuExchange.log exception.class.to_s
RuExchange.log exception.to_s
RuExchange.log exception.backtrace.join("\n")
end
begin
if cur.save
RuExchange.log 'First currency updated!'
else
RuExchange.log '--- ERROR First currency not updated!'
end
if cur_new.save
RuExchange.log 'Current currency updated!'
RuExchange.log 'RUR: ' + result_rur.to_s
RuExchange.log 'UAH: ' + result_uah.to_s
else
RuExchange.log '--- ERROR Current currency not updated!'
end
rescue => exception
RuExchange.log exception.class.to_s
RuExchange.log exception.to_s
RuExchange.log exception.backtrace.join("\n")
r_c = Dashboard::ReportCurrency.new
r_c.error = [exception.to_s => exception.backtrace.join("\n")].to_json
r_c.status = 2
r_c.save
end
RuExchange.log 'FINISHED'
RuExchange.log ''
end
|