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
|
# File 'app/controllers/deposit/collection_controller.rb', line 55
def account_invoices
cached_options_for_klient = options_for_klient
searcher = lambda do |search_key, _offset, _limit|
account = begin
KillBillClient::Model::Account.find_by_id(search_key, false, false,
cached_options_for_klient)
rescue StandardError
nil
end
if account.nil?
[]
else
account.invoices(cached_options_for_klient)
end
end
= lambda do |invoice, column|
[
invoice.invoice_number.to_i,
invoice.invoice_date,
invoice.amount,
invoice.balance,
nil
][column]
end
formatter = lambda do |invoice|
[
invoice.invoice_number,
invoice.invoice_date,
view_context.humanized_money_with_symbol(Money.from_amount(invoice.amount.to_f, invoice.currency)),
view_context.humanized_money_with_symbol(Money.from_amount(invoice.balance.to_f, invoice.currency)),
nil
]
end
begin
search_key = (params[:search] || {})[:value].presence
offset = (params[:start] || 0).to_i
limit = (params[:length] || 50).to_i
pages = searcher.call(search_key, offset, limit)
rescue StandardError => e
error = e.to_s
end
json = {
draw: (params[:draw] || 0).to_i,
recordsTotal: pages.nil? ? 0 : limit,
recordsFiltered: pages.nil? ? 0 : limit,
data: []
}
json[:error] = error unless error.nil?
pages ||= []
ordering = (params[:order] || {})[:'0'] || {}
ordering_column = (ordering[:column] || 0).to_i
ordering_dir = ordering[:dir] || 'asc'
unless search_key.nil?
pages.sort! do |a, b|
a = .call(a, ordering_column)
b = .call(b, ordering_column)
sort = a <=> b
sort.nil? ? -1 : sort
end
end
pages.reverse! if (ordering_dir == 'desc' && limit >= 0) || (ordering_dir == 'asc' && limit.negative?)
pages.each { |page| json[:data] << formatter.call(page) }
respond_to do |format|
format.json { render json: json }
end
end
|