Я пытаюсь создать фильтр для моего дохода_контроллера show action , но он работает не очень хорошо.
Это то, что я сделал в приведенном ниже коде, но он выбирает только end_datetime при фильтрации по дате.
дохода_контроллер.rb
То, что я пытаюсь сделать в действии show
, - это мой поиск по умолчанию с 30 днями, а если параметры заполнены датой, используйте выбранную дату и время.
module Admin
module Statistic
class RevenuesController < BaseController
def show
@revenues_reports = Currency.all.map do |currency|
puts params
start_datetime = if params[:created_at].nil? then
30.days.ago
else
params[:created_at].slice(0..15)
end
end_datetime = if params[:created_at].nil? then
Time.now
else
params[:created_at].slice(0..15)
end
get_revenue_report_for_currency(currency, start_datetime, end_datetime)
end
end
def get_revenue_report_for_currency(currency, start_datetime, end_datetime)
puts "start: #{start_datetime}" # I added this to see from the log the start_datetime it picks
puts "end: #{end_datetime}" # I added this to see from the log the end_datetime it picks
total_withdraw_fees_charged = Withdraw.where(:currency => currency.id, :created_at => start_datetime..end_datetime).sum(:fee)
total_withdraw_network_fees_charged = 0.3 * total_withdraw_fees_charged
net_income_from_withdraws = total_withdraw_fees_charged - total_withdraw_network_fees_charged
total_sell_order_fees_charged = Order.where(:type => 'OrderAsk', :bid => currency.id, :created_at => start_datetime..end_datetime).sum(:fee)
total_buy_order_fees_charged = Order.where(:type => 'OrderBid', :ask => currency.id, :created_at => start_datetime..end_datetime).sum(:fee)
total_trading_fees = total_sell_order_fees_charged + total_buy_order_fees_charged
total_fees = net_income_from_withdraws + total_trading_fees
{
currency: currency.code.upcase,
total_withdraw_fees_charged: total_withdraw_fees_charged,
total_withdraw_network_fees_charged: total_withdraw_network_fees_charged,
net_income_from_withdraws: net_income_from_withdraws,
total_sell_order_fees_charged: total_sell_order_fees_charged,
total_buy_order_fees_charged: total_buy_order_fees_charged,
total_trading_fees: total_trading_fees,
total_fees: total_fees
}
end
end
end
end
show.html.erb
.panel.panel-primary
.panel-heading
h4.panel-title = t('admin.statistic.filter')
.panel-body
= form_tag(admin_statistic_revenue_path, :method => 'get', class: 'form-horizontal') do |f|
.row
.col.col-xs-3
label[for="#"]
| Start Date:
input.form-control.created_at.date_time_filter.from[name="[created_at]" type="#" value=""]
.col.col-xs-3
label[for="#"]
| End Date:
input.form-control.created_at.date_time_filter.to[name="[created_at]" type="#" value=""]
= button_tag "Search", :class => 'btn btn-info', :name => nil
URL параметров браузера
http://localhost:3000/admin/statistic/revenue?utf8=%E2%9C%93&%5Bcreated_at%5D=2018-08-01+00%3A00&%5Bcreated_at%5D=2018-10-05+00%3A00
development_logs