Class: SpreeReports::Reports::SoldProducts

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#excluded_user_ids, #without_excluded_orders

Constructor Details

#initialize(params) ⇒ SoldProducts

Returns a new instance of SoldProducts.



8
9
10
11
12
13
# File 'lib/spree_reports/reports/sold_products.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/sold_products.rb', line 6

def currencies
  @currencies
end

#currencyObject

Returns the value of attribute currency.



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

def currency
  @currency
end

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

#date_startObject

Returns the value of attribute date_start.



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

def date_start
  @date_start
end

#monthsObject

Returns the value of attribute months.



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

def months
  @months
end

#ordersObject

Returns the value of attribute orders.



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

def orders
  @orders
end

#paramsObject

Returns the value of attribute params.



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

def params
  @params
end

#storeObject

Returns the value of attribute store.



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

def store
  @store
end

#storesObject

Returns the value of attribute stores.



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

def stores
  @stores
end

Instance Method Details

#build_responseObject



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/spree_reports/reports/sold_products.rb', line 52

def build_response
  @data = @data_tmp.map do |item|
    {
      variant_id: item[0],
      variant_name: item[2],
      variant_slug: item[3],
      variant_available_on: item[4],
      quantity: item[1]
    }
  end
end

#csv_filenameObject



89
90
91
# File 'lib/spree_reports/reports/sold_products.rb', line 89

def csv_filename
  "sold_products_#{@months}-months.csv"
end

#get_dataObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/spree_reports/reports/sold_products.rb', line 27

def get_data
  @orders = Spree::Order.complete.where(payment_state: 'paid')
  @orders = @orders.where("completed_at >= ?", @date_start) if @date_start
  @orders = @orders.where(currency: @currency) if @currencies.size > 1
  @orders = @orders.where(store_id: @store) if @stores.size > 2 && @store != "all"
  @orders = without_excluded_orders(@orders)
  
  order_ids = @orders.pluck(:id)
  line_items = Spree::LineItem.where(order_id: order_ids)
  variant_ids_and_quantity = line_items.group(:variant_id).sum(:quantity).sort_by { |k,v| v }.reverse
  
  variants = Spree::Variant.all
  variant_names = variants.all.map { |v| [v.id, [variant_name_with_option_values(v), v.slug, v.available_on] ] }.to_h
  
  @data_tmp = variant_ids_and_quantity.map do |id, quantity|
    [
      id,
      quantity,
      variant_names[id][0],
      variant_names[id][1],
      variant_names[id][2]
    ]
  end
end

#setup_paramsObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/spree_reports/reports/sold_products.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]
    
  @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       
end

#to_csvObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/spree_reports/reports/sold_products.rb', line 71

def to_csv
    
  CSV.generate(headers: true, col_sep: ";") do |csv|
    csv << %w{ variant_id variant_name variant_slug quantity }

    @data.each do |item|
      csv << [
        item[:variant_id],
        item[:variant_name],
        item[:variant_slug],
        item[:quantity]
      ]
    end

  end
    
end

#variant_name_with_option_values(v) ⇒ Object



64
65
66
67
68
69
# File 'lib/spree_reports/reports/sold_products.rb', line 64

def variant_name_with_option_values(v)
  s = v.name
  option_values = v.option_values.map { |o| o.presentation }
  s += " (#{option_values.join(", ")})" if option_values.any?
  s
end