Class: SpreeReports::Reports::SoldProducts
- Defined in:
- lib/spree_reports/reports/sold_products.rb
Instance Attribute Summary collapse
-
#currencies ⇒ Object
Returns the value of attribute currencies.
-
#currency ⇒ Object
Returns the value of attribute currency.
-
#data ⇒ Object
Returns the value of attribute data.
-
#date_start ⇒ Object
Returns the value of attribute date_start.
-
#months ⇒ Object
Returns the value of attribute months.
-
#orders ⇒ Object
Returns the value of attribute orders.
-
#params ⇒ Object
Returns the value of attribute params.
-
#store ⇒ Object
Returns the value of attribute store.
-
#stores ⇒ Object
Returns the value of attribute stores.
Instance Method Summary collapse
- #build_response ⇒ Object
- #csv_filename ⇒ Object
- #get_data ⇒ Object
-
#initialize(params) ⇒ SoldProducts
constructor
A new instance of SoldProducts.
- #setup_params ⇒ Object
- #to_csv ⇒ Object
- #variant_name_with_option_values(v) ⇒ Object
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
#currencies ⇒ Object
Returns the value of attribute currencies.
6 7 8 |
# File 'lib/spree_reports/reports/sold_products.rb', line 6 def currencies @currencies end |
#currency ⇒ Object
Returns the value of attribute currency.
6 7 8 |
# File 'lib/spree_reports/reports/sold_products.rb', line 6 def currency @currency end |
#data ⇒ Object
Returns the value of attribute data.
5 6 7 |
# File 'lib/spree_reports/reports/sold_products.rb', line 5 def data @data end |
#date_start ⇒ Object
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 |
#months ⇒ Object
Returns the value of attribute months.
6 7 8 |
# File 'lib/spree_reports/reports/sold_products.rb', line 6 def months @months end |
#orders ⇒ Object
Returns the value of attribute orders.
5 6 7 |
# File 'lib/spree_reports/reports/sold_products.rb', line 5 def orders @orders end |
#params ⇒ Object
Returns the value of attribute params.
5 6 7 |
# File 'lib/spree_reports/reports/sold_products.rb', line 5 def params @params end |
#store ⇒ Object
Returns the value of attribute store.
6 7 8 |
# File 'lib/spree_reports/reports/sold_products.rb', line 6 def store @store end |
#stores ⇒ Object
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_response ⇒ Object
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_filename ⇒ Object
89 90 91 |
# File 'lib/spree_reports/reports/sold_products.rb', line 89 def csv_filename "sold_products_#{@months}-months.csv" end |
#get_data ⇒ Object
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_params ⇒ Object
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_csv ⇒ Object
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 |