Class: Spree::Admin::ReportsController

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeReportsController

Returns a new instance of ReportsController.



19
20
21
22
# File 'app/controllers/spree/admin/reports_controller.rb', line 19

def initialize
  super 
  ReportsController.add_available_report!(:sales_total)
end

Class Method Details

.add_available_report!(report_key, report_description_key = nil) ⇒ Object



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

def add_available_report!(report_key, report_description_key = nil)
  if report_description_key.nil?
    report_description_key = "#{report_key}_description"
  end
  @@available_reports[report_key] = {name: Spree.t(report_key), description: Spree.t(report_description_key)}
end

.available_reportsObject



7
8
9
# File 'app/controllers/spree/admin/reports_controller.rb', line 7

def available_reports
  @@available_reports
end

Instance Method Details

#indexObject



24
25
26
# File 'app/controllers/spree/admin/reports_controller.rb', line 24

def index
  @reports = ReportsController.available_reports
end

#sales_totalObject



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

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

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

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

  params[:q][:s] ||= "completed_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