Class: Spree::Admin::ReportsController

Inherits:
BaseController show all
Defined in:
app/controllers/spree/admin/reports_controller.rb

Constant Summary collapse

AVAILABLE_REPORTS =
{
  :sales_total => { :name => I18n.t(:sales_total), :description => I18n.t(:sales_total_description) }
}

Instance Method Summary collapse

Methods included from Core::ControllerHelpers::Common

included

Methods included from Core::ControllerHelpers::Auth

#current_ability, included, #redirect_back_or_default, #store_location, #try_spree_current_user, #unauthorized

Instance Method Details

#indexObject



10
11
12
13
# File 'app/controllers/spree/admin/reports_controller.rb', line 10

def index
  @reports = AVAILABLE_REPORTS
  respond_with(@reports)
end

#sales_totalObject



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
# File 'app/controllers/spree/admin/reports_controller.rb', line 15

def sales_total
  params[:q] = {} unless params[:q]

  if params[:q][:created_at_gt].blank?
    params[:q][:created_at_gt] = Time.zone.now.beginning_of_month
  else
    params[:q][:created_at_gt] = Time.zone.parse(params[:q][:created_at_gt]).beginning_of_day rescue Time.zone.now.beginning_of_month
  end

  if params[:q] && !params[:q][:created_at_lt].blank?
    params[:q][:created_at_lt] = Time.zone.parse(params[:q][:created_at_lt]).end_of_day rescue ""
  end

  if params[:q].delete(:completed_at_not_null) == "1"
    params[:q][:completed_at_not_null] = true
  else
    params[:q][:completed_at_not_null] = false
  end

  params[:q][:s] ||= "created_at desc"

  @search = Order.complete.ransack(params[:q])
  @orders = @search.result

  @totals = {}
  @orders.each do |order|
    @totals[order.currency] = { :item_total => ::Money.new(0, order.currency), :adjustment_total => ::Money.new(0, order.currency), :sales_total => ::Money.new(0, order.currency) } unless @totals[order.currency]
    @totals[order.currency][:item_total] += order.display_item_total.money
    @totals[order.currency][:adjustment_total] += order.display_adjustment_total.money
    @totals[order.currency][:sales_total] += order.display_total.money
  end
end