forked from ManageIQ/manageiq-ui-classic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchargeback_rate_controller.rb
373 lines (325 loc) · 14.2 KB
/
chargeback_rate_controller.rb
1
2
3
4
5
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
class ChargebackRateController < ApplicationController
before_action :check_privileges
before_action :get_session_data
after_action :cleanup_action
after_action :set_session_data
include Mixins::GenericButtonMixin
include Mixins::GenericListMixin
include Mixins::GenericSessionMixin
include Mixins::GenericFormMixin
include Mixins::GenericShowMixin
include Mixins::BreadcrumbsMixin
BUTTON_ALLOWED_ACTIONS = {
'chargeback_rates_copy' => :cb_rate_edit,
'chargeback_rates_delete' => :delete,
'chargeback_rates_edit' => :cb_rate_edit,
'chargeback_rates_new' => :cb_rate_edit
}.freeze
def button
@edit = session[:edit] # Restore @edit for adv search box
@refresh_div = "main_div" # Default div for button.rjs to refresh
action = params[:pressed]
evaluate_button(action, BUTTON_ALLOWED_ACTIONS)
return if performed?
if action.ends_with?("_copy", "_edit", "_new") && @flash_array.nil?
javascript_redirect(:action => @refresh_partial, :id => @redirect_id, :pressed => action)
elsif action.ends_with?("_delete")
javascript_redirect(:action => 'show_list')
elsif @refresh_div == "main_div" && @lastaction == "show_list"
replace_gtl_main_div
else
super
end
end
def cb_rate_edit
@_params[:id] ||= find_checked_items[0]
rate = new_rate_edit? ? ChargebackRate.new : ChargebackRate.find(params[:id])
if params[:pressed] == 'chargeback_rates_edit' && rate.default?
add_flash(_("Default Chargeback Rate \"%{name}\" cannot be edited.") % {:name => rate.description}, :error)
return
end
@redirect_id = params[:id] if params[:id]
@refresh_partial = "edit"
end
def edit
assert_privileges(params[:pressed]) if params[:pressed]
case params[:button]
when "cancel"
if params[:id]
flash_msg = _("Edit of Chargeback Rate \"%{name}\" was cancelled by the user") % {:name => session[:edit][:new][:description]}
else
flash_msg = _("Add of new Chargeback Rate was cancelled by the user")
end
@edit = session[:edit] = nil # clean out the saved info
session[:changed] = false
javascript_redirect(:action => @lastaction, :id => params[:id], :flash_msg => flash_msg)
when "save", "add"
id = params[:button] == "save" ? params[:id] : "new"
return unless load_edit("cbrate_edit__#{id}")
@rate = params[:button] == "add" ? ChargebackRate.new : ChargebackRate.find(params[:id])
if @edit[:new][:description].nil? || @edit[:new][:description] == ""
render_flash(_("Description is required"), :error)
return
end
@rate.description = @edit[:new][:description]
@rate.rate_type = @edit[:new][:rate_type] if @edit[:new][:rate_type]
cb_rate_set_record_vars
# Detect errors saving tiers
tiers_valid = @rate_tiers.all? { |tiers| tiers.all?(&:valid?) }
@rate.chargeback_rate_details.replace(@rate_details)
@rate.chargeback_rate_details.each_with_index do |_detail, i|
@rate_details[i].save_tiers(@rate_tiers[i])
end
tiers_valid &&= @rate_details.all? { |rate_detail| rate_detail.errors.messages.blank? }
if tiers_valid && @rate.save
if params[:button] == "add"
AuditEvent.success(build_created_audit(@rate, @edit))
flash_msg = _("Chargeback Rate \"%{name}\" was added") % {:name => @rate.description}
else
AuditEvent.success(build_saved_audit(@rate, @edit))
flash_msg = _("Chargeback Rate \"%{name}\" was saved") % {:name => @rate.description}
end
@edit = session[:edit] = nil # clean out the saved info
session[:changed] = @changed = false
javascript_redirect(:action => @lastaction, :id => params[:id], :flash_msg => flash_msg)
else
@rate.errors.each do |field, msg|
add_flash("#{field.to_s.capitalize} #{msg}", :error)
end
@rate_details.each do |detail|
display_detail_errors(detail, detail.errors)
end
@rate_tiers.each_with_index do |tiers, detail_index|
tiers.each do |tier|
display_detail_errors(@rate_details[detail_index], tier.errors)
end
end
@changed = session[:changed] = (@edit[:new] != @edit[:current])
javascript_flash
end
when "reset", nil # displaying edit from for actions: new, edit or copy
@in_a_form = true
session[:changed] = params[:pressed] == 'chargeback_rates_copy'
@rate = new_rate_edit? ? ChargebackRate.new : ChargebackRate.find(params[:id])
cb_rate_set_form_vars
javascript_redirect(:action => 'edit',
:id => params[:id],
:flash_msg => _("All changes have been reset"),
:flash_warning => true) if params[:button] == "reset"
end
end
# AJAX driven routine to check for changes in ANY field on the form
def form_field_changed
return unless load_edit("cbrate_edit__#{params[:id]}")
cb_rate_get_form_vars
render :update do |page|
page << javascript_prologue
changed = (@edit[:new] != @edit[:current])
# Update the new column with the code of the currency selected by the user
page.replace('chargeback_rate_currency', :partial => 'cb_rate_currency')
page << javascript_for_miq_button_visibility(changed)
end
end
# Delete all selected or single displayed action(s)
def delete
assert_privileges("chargeback_rates_delete")
rates = []
if !params[:id] # showing a list
rates = find_checked_items
if rates.empty?
add_flash(_("No Chargeback Rates were selected for deletion"), :error)
end
else # showing 1 rate, delete it
cb_rate = ChargebackRate.find_by(:id => params[:id])
if cb_rate.nil?
add_flash(_("Chargeback Rate no longer exists"), :error)
else
rates.push(params[:id])
end
end
process_cb_rates(rates, 'destroy') if rates.present?
flash_to_session
end
# Add a new tier at the end
def tier_add
detail_index = params[:detail_index]
ii = detail_index.to_i
@edit = session[:edit]
detail = @edit[:new][:details][ii]
@edit[:new][:num_tiers][ii] = detail[:chargeback_tiers].to_a.length if detail[:chargeback_tiers]
@edit[:new][:num_tiers][ii] = 1 unless @edit[:new][:num_tiers][ii] || @edit[:new][:num_tiers][ii].zero?
@edit[:new][:num_tiers][ii] += 1
tier_index = @edit[:new][:num_tiers][ii] - 1
tier_list = @edit[:new][:tiers][ii]
tier_list[tier_index] = {}
tier = tier_list[tier_index]
tier[:start] = tier_list[tier_index - 1][:finish]
tier[:finish] = Float::INFINITY
tier[:fixed_rate] = 0.0
tier[:variable_rate] = 0.0
code_currency = Currency.find_by(:id => detail[:currency]).code
add_row(detail_index, tier_index - 1, code_currency)
end
# Remove the selected tier
def tier_remove
@edit = session[:edit]
index = params[:index]
detail_index, tier_to_remove_index = index.split("-")
detail_index = detail_index.to_i
@edit[:new][:num_tiers][detail_index] = @edit[:new][:num_tiers][detail_index] - 1
# Delete tier record
@edit[:new][:tiers][detail_index].delete_at(tier_to_remove_index.to_i)
@changed = session[:changed] = true
render :update do |page|
page << javascript_prologue
page.replace_html("chargeback_rate_edit_form", :partial => "cb_rate_edit_table")
page << javascript_for_miq_button_visibility(@changed)
end
end
def title
@title = _("Chargeback Rates")
end
private ############################
# Common Schedule button handler routines
def process_cb_rates(rates, task)
process_elements(rates, ChargebackRate, task)
end
# Set form variables for edit
def cb_rate_set_form_vars
@edit = {}
@edit[:new] = HashWithIndifferentAccess.new
@edit[:current] = HashWithIndifferentAccess.new
@edit[:new][:tiers] = []
@edit[:new][:num_tiers] = []
@edit[:new][:description] = @rate.description
@edit[:new][:rate_type] = @rate.rate_type || "Compute"
@edit[:new][:details] = []
tiers = []
rate_details = @rate.chargeback_rate_details
rate_details = ChargebackRateDetail.default_rate_details_for(@edit[:new][:rate_type]) if new_rate_edit?
# Select the currency of the first chargeback_rate_detail. All the chargeback_rate_details have the same currency
@edit[:new][:currency] = rate_details[0].detail_currency.id
@edit[:new][:code_currency] = "#{rate_details[0].detail_currency.symbol} [#{rate_details[0].detail_currency.full_name}]"
rate_details.each_with_index do |detail, detail_index|
temp = detail.slice(*ChargebackRateDetail::FORM_ATTRIBUTES)
temp[:report_column_name] = Dictionary.gettext(detail.chargeable_field.metric_key, :type => :column, :notfound => :titleize)
temp[:group] = detail.chargeable_field.group
temp[:per_time] ||= "hourly"
temp[:currency] = detail.detail_currency.id
if detail.chargeable_field.detail_measure.present?
temp[:detail_measure] = {}
temp[:detail_measure][:measures] = detail.chargeable_field.detail_measure.measures
temp[:chargeback_rate_detail_measure_id] = detail.chargeable_field.detail_measure.id
end
temp[:id] = params[:pressed] == 'chargeback_rates_copy' ? nil : detail.id
temp[:sub_metrics] = detail.sub_metrics
temp[:sub_metric_human] = detail.sub_metric_human
tiers[detail_index] ||= []
detail.chargeback_tiers.each do |tier|
new_tier = tier.slice(*ChargebackTier::FORM_ATTRIBUTES)
new_tier[:id] = params[:pressed] == 'chargeback_rates_copy' ? nil : tier.id
new_tier[:chargeback_rate_detail_id] = params[:pressed] == 'chargeback_rates_copy' ? nil : detail.id
new_tier[:start] = new_tier[:start].to_f
new_tier[:finish] = ChargebackTier.to_float(new_tier[:finish])
tiers[detail_index].push(new_tier)
end
@edit[:new][:tiers][detail_index] = tiers[detail_index]
@edit[:new][:num_tiers][detail_index] = tiers[detail_index].size
@edit[:new][:details].push(temp)
end
@edit[:new][:per_time_types] = ChargebackRateDetail::PER_TIME_TYPES.map { |x, y| [x, _(y)] }.to_h
if params[:pressed] == 'chargeback_rates_copy'
@rate.id = nil
@edit[:new][:description] = "copy of #{@rate.description}"
end
@edit[:rec_id] = @rate.id || nil
@edit[:key] = "cbrate_edit__#{@rate.id || "new"}"
@edit[:current] = copy_hash(@edit[:new])
session[:edit] = @edit
end
# Get variables from edit form
def cb_rate_get_form_vars
@edit[:new][:description] = params[:description] if params[:description]
@edit[:new][:rate_type] = params[:rate_type] if params[:rate_type]
if params[:currency]
@edit[:new][:currency] = params[:currency].to_i
rate_detail_currency = Currency.find(params[:currency])
@edit[:new][:code_currency] = "#{rate_detail_currency.symbol} [#{rate_detail_currency.full_name}]"
end
@edit[:new][:details].each_with_index do |detail, detail_index|
%i[per_time per_unit sub_metric].each do |measure|
key = "#{measure}_#{detail_index}".to_sym
detail[measure] = params[key] if params[key]
end
# Add currencies to chargeback_controller.rb
detail[:currency] = params[:currency].to_i if params[:currency]
# Save tiers into @edit
(0..@edit[:new][:num_tiers][detail_index].to_i - 1).each do |tier_index|
tier = @edit[:new][:tiers][detail_index][tier_index] || {}
%i[fixed_rate variable_rate start finish].each do |field|
key = "#{field}_#{detail_index}_#{tier_index}".to_sym
tier[field] = params[key] if params[key]
end
end
end
end
def cb_rate_set_record_vars
@rate_details = []
@rate_tiers = []
@edit[:new][:details].each_with_index do |detail, detail_index|
rate_detail = detail[:id] ? ChargebackRateDetail.find(detail[:id]) : ChargebackRateDetail.new
rate_detail.attributes = detail.slice(*ChargebackRateDetail::FORM_ATTRIBUTES)
rate_detail.sub_metric = detail[:sub_metric] if rate_detail.sub_metric
rate_detail_edit = @edit[:new][:details][detail_index]
# C: Record the currency selected in the edit view, in my chargeback_rate_details table
rate_detail.chargeback_rate_detail_currency_id = rate_detail_edit[:currency]
rate_detail.chargeback_rate_detail_measure_id = rate_detail_edit[:chargeback_rate_detail_measure_id]
rate_detail.chargeback_rate_id = @rate.id
# Save tiers into @sb
rate_tiers = []
@edit[:new][:tiers][detail_index].each do |tier|
rate_tier = tier[:id] ? ChargebackTier.find(tier[:id]) : ChargebackTier.new
tier[:start] = Float::INFINITY if tier[:start].blank?
tier[:finish] = Float::INFINITY if tier[:finish].blank?
rate_tier.attributes = tier.slice(*ChargebackTier::FORM_ATTRIBUTES)
rate_tier.chargeback_rate_detail_id = rate_detail.id
rate_tiers.push(rate_tier)
end
@rate_tiers[detail_index] = rate_tiers
@rate_details.push(rate_detail)
end
end
def new_rate_edit?
!params[:id].present? || params[:id] == 'new' || params[:pressed] == 'chargeback_rates_new'
end
def display_detail_errors(detail, errors)
errors.each { |field, msg| add_flash("'#{detail.chargeable_field.description}' #{field.to_s.humanize.downcase} #{msg}", :error) }
end
def add_row(i, pos, code_currency)
locals = {:code_currency => code_currency}
render :update do |page|
page << javascript_prologue
# Update the first row to change the colspan
page.replace("rate_detail_row_#{i}_0",
:partial => "tier_first_row",
:locals => locals)
# Insert the new tier after the last one
page.insert_html(:after,
"rate_detail_row_#{i}_#{pos}",
:partial => "tier_row",
:locals => locals)
page << javascript_for_miq_button_visibility(true)
end
end
def breadcrumbs_options
{
:breadcrumbs => [
{:title => _("Overview")},
{:title => _("Chargeback")},
{:title => _("Rates"), :url => controller_url},
],
}
end
toolbar :chargeback_rate, :chargeback_rates
menu_section :chargeback
end