Class: SpreeReports::Reports::OrdersByPeriod

Inherits:
Base
  • Object
show all
Defined in:
lib/spree_reports/reports/orders_by_period.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#excluded_user_ids, #without_excluded_orders

Constructor Details

#initialize(params) ⇒ OrdersByPeriod

Returns a new instance of OrdersByPeriod.



8
9
10
11
12
13
# File 'lib/spree_reports/reports/orders_by_period.rb', line 8

def initialize(params)
  @params = params
  setup_params
  get_data
  build_response
end

Instance Attribute Details

#currenciesObject

Returns the value of attribute currencies.



6
7
8
# File 'lib/spree_reports/reports/orders_by_period.rb', line 6

def currencies
  @currencies
end

#currencyObject

Returns the value of attribute currency.



6
7
8
# File 'lib/spree_reports/reports/orders_by_period.rb', line 6

def currency
  @currency
end

#dataObject

Returns the value of attribute data.



5
6
7
# File 'lib/spree_reports/reports/orders_by_period.rb', line 5

def data
  @data
end

#date_startObject

Returns the value of attribute date_start.



6
7
8
# File 'lib/spree_reports/reports/orders_by_period.rb', line 6

def date_start
  @date_start
end

#group_byObject

Returns the value of attribute group_by.



6
7
8
# File 'lib/spree_reports/reports/orders_by_period.rb', line 6

def group_by
  @group_by
end

#group_by_listObject

Returns the value of attribute group_by_list.



6
7
8
# File 'lib/spree_reports/reports/orders_by_period.rb', line 6

def group_by_list
  @group_by_list
end

#monthsObject

Returns the value of attribute months.



6
7
8
# File 'lib/spree_reports/reports/orders_by_period.rb', line 6

def months
  @months
end

#paramsObject

Returns the value of attribute params.



5
6
7
# File 'lib/spree_reports/reports/orders_by_period.rb', line 5

def params
  @params
end

#stateObject

Returns the value of attribute state.



6
7
8
# File 'lib/spree_reports/reports/orders_by_period.rb', line 6

def state
  @state
end

#statesObject

Returns the value of attribute states.



6
7
8
# File 'lib/spree_reports/reports/orders_by_period.rb', line 6

def states
  @states
end

#storeObject

Returns the value of attribute store.



6
7
8
# File 'lib/spree_reports/reports/orders_by_period.rb', line 6

def store
  @store
end

#storesObject

Returns the value of attribute stores.



6
7
8
# File 'lib/spree_reports/reports/orders_by_period.rb', line 6

def stores
  @stores
end

Instance Method Details

#build_responseObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/spree_reports/reports/orders_by_period.rb', line 100

def build_response
  @data = @sales_count.map do |k, v|
    {
      date: k,
      date_formatted: SpreeReports::Helper.date_formatted(k, @group_by),
      count: v,
      total: @sales_total[k].to_f,
      item_total: @sales_item_total[k].to_f,
      avg_total: SpreeReports::Helper.round(SpreeReports::Helper.divide(@sales_total[k].to_f, v)),
      adjustment_total: @sales_adjustment_total[k].to_f,
      shipment_total: @sales_shipment_total[k].to_f,
      promo_total: @sales_promo_total[k].to_f,
      included_tax_total: @sales_included_tax_total[k].to_f,
      item_count_total: @sales_item_count_total[k].to_i,
      items_per_order: SpreeReports::Helper.round(SpreeReports::Helper.divide(@sales_item_count_total[k].to_f, v.to_f))
    }
  end
end

#csv_filenameObject



145
146
147
# File 'lib/spree_reports/reports/orders_by_period.rb', line 145

def csv_filename
  "orders_per_period_#{@group_by}_#{@months}-months_#{@state}.csv"
end

#get_dataObject



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
# File 'lib/spree_reports/reports/orders_by_period.rb', line 50

def get_data
  
  # select by state
  
  if @state == "complete_paid"
    date_column = :completed_at
    @sales = Spree::Order.complete.where(payment_state: 'paid')
  elsif @state == "complete"
    date_column = :completed_at
    @sales = Spree::Order.complete
  elsif @state == "incomplete"
    date_column = :created_at
    @sales = Spree::Order.incomplete
  elsif @state == "canceled"
    date_column = :canceled_at
    @sales = Spree::Order.where.not(canceled_at: nil)
  else
    date_column = :created_at
    @sales = Spree::Order.where(state: @state)
  end
    
  @sales = @sales.where("#{date_column.to_s} >= ?", @date_start) if @date_start
  @sales = @sales.where(currency: @currency) if @currencies.size > 1
  @sales = @sales.where(store_id: @store) if @stores.size > 2 && @store != "all"        
  @sales = without_excluded_orders(@sales)
  
  # group by

  if @group_by == :year
    @sales = @sales.group_by_year(date_column, time_zone: SpreeReports.time_zone)
  elsif @group_by == :month
    @sales = @sales.group_by_month(date_column, time_zone: SpreeReports.time_zone)
  elsif @group_by == :week
    # %W => week start: monday, %U => week start: sunday
    @sales = @sales.group_by_week(date_column, time_zone: SpreeReports.time_zone)
  else
    @sales = @sales.group_by_day(date_column, time_zone: SpreeReports.time_zone)
  end
  
  @sales_count = @sales.count
  @sales_total = @sales.sum(:total)
  @sales_item_total = @sales.sum(:item_total)
  @sales_adjustment_total = @sales.sum(:adjustment_total)
  @sales_shipment_total = @sales.sum(:shipment_total)
  @sales_promo_total = @sales.sum(:promo_total)
  @sales_included_tax_total = @sales.sum(:included_tax_total)
  @sales_item_count_total = @sales.sum(:item_count)
  
end

#setup_paramsObject



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
# File 'lib/spree_reports/reports/orders_by_period.rb', line 15

def setup_params
  @currencies = Spree::Order.select('currency').distinct.map { |c| c.currency }  
  @currency = @currencies.include?(params[:currency]) ? params[:currency] : @currencies.first
  
  @stores = Spree::Store.all.map { |store| [store.name, store.id] }
  @stores << ["all", "all"]
  @store = @stores.map{ |s| s[1].to_s }.include?(params[:store]) ? params[:store] : @stores.first[1]
    
  @group_by_list = [:day, :week, :month, :year]
  @group_by = @group_by_list.include?(params[:group_by].try(:to_sym)) ? params[:group_by].to_sym : :month
    
  # states
  @states = %w{complete_paid complete incomplete cart address delivery payment confirm canceled}
  @state = @states.include?(params[:state]) ? params[:state] : "complete_paid"
  
  # ******************************************************************************************************
  # MONTHS
    
  @months = SpreeReports.report_months.include?(params[:months]) ? params[:months] : SpreeReports.default_months.to_s
  @date_start = (@months != "all") ? (Time.now - (@months.to_i).months) : nil
    
  if @date_start
    if @group_by == :year
      @date_start = @date_start.beginning_of_year
    elsif @group_by == :month
      @date_start = @date_start.beginning_of_month
    elsif @group_by == :week
      @date_start = @date_start.beginning_of_week
    else
      @date_start = @date_start.beginning_of_day
    end
  end
          
end

#to_csvObject



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
# File 'lib/spree_reports/reports/orders_by_period.rb', line 119

def to_csv
    
  CSV.generate(headers: true, col_sep: ";") do |csv|
    csv << %w{date date_formatted count item_count_total items_per_order avg_total total item_total adjustment_total shipment_total promo_total included_tax_total }

    @data.each do |item|
      csv << [
        item[:date],
        item[:date_formatted],
        item[:count],
        item[:item_count_total],
        item[:items_per_order],
        item[:avg_total],
        item[:total],
        item[:item_total],
        item[:adjustment_total],
        item[:shipment_total],
        item[:promo_total],
        item[:included_tax_total]
      ]
    end

  end
    
end