Class: ShopifyDashboardPlus::SalesReport

Inherits:
Report
  • Object
show all
Defined in:
lib/shopify_dashboard_plus/report.rb

Constant Summary

Constants included from ApplicationHelpers

ApplicationHelpers::DESIRED_FIELDS

Instance Method Summary collapse

Methods inherited from Report

#initialize

Methods included from ApplicationHelpers

#close_connection, #connected?, #date_range_valid?, #date_today, #days, #display_as_currency, #get_average_revenue, #get_daily_revenues, #get_date_range, #get_detailed_revenue_metrics, #get_host, #get_list_of_orders, #get_total_revenue, #hash_to_graph_format, #open_connection, #order_parameters_paginate, #set_connection, #shop_name, #strip_protocol

Constructor Details

This class inherits a constructor from ShopifyDashboardPlus::Report

Instance Method Details

#currencies_per_saleObject



60
61
62
63
64
65
66
67
# File 'lib/shopify_dashboard_plus/report.rb', line 60

def currencies_per_sale
  currencies = Hash.new(0)

  @orders.each do |order|
    currencies[order.currency] += 1 if order.attributes['currency']
  end
  currencies
end

#number_of_salesObject



56
57
58
# File 'lib/shopify_dashboard_plus/report.rb', line 56

def number_of_sales
  @orders.length
end

#sales_per_countryObject



16
17
18
19
20
21
22
23
# File 'lib/shopify_dashboard_plus/report.rb', line 16

def sales_per_country
  sales_per_country = Hash.new(0)

  @orders.each do |order|
    sales_per_country[order.billing_address.country] += 1 if order.attributes['billing_address']
  end
  sales_per_country
end

#sales_per_customerObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/shopify_dashboard_plus/report.rb', line 35

def sales_per_customer
  customer_sales = []
  @orders.each do |order|
    order.line_items.each do |item|
      customer_name = "#{order.customer.first_name} #{order.customer.last_name} (#{order.customer.email})"
      customer_sales.push({ :name => item.title,
                            :data => [customer_name, item.price.to_f]})
    end
  end
  hash_to_graph_format(customer_sales)
end

#sales_per_price_pointObject



47
48
49
50
51
52
53
54
# File 'lib/shopify_dashboard_plus/report.rb', line 47

def sales_per_price_point
  sales_per_price_point = Hash.new(0)

  @line_items.each do |item|
    sales_per_price_point[item.price] += 1
  end
  sales_per_price_point.sort_by{ |x,y| x.to_f }.to_h rescue {}
end

#sales_per_productObject



25
26
27
28
29
30
31
32
33
# File 'lib/shopify_dashboard_plus/report.rb', line 25

def sales_per_product
  sales_per_product = Hash.new(0)

  @line_items.each do |item|
    sales_per_product[item.title] += 1
  end

  sales_per_product
end

#to_hObject



69
70
71
72
73
74
75
76
77
78
# File 'lib/shopify_dashboard_plus/report.rb', line 69

def to_h
  {
    :currencies_per_sale => currencies_per_sale,
    :sales_per_country => sales_per_country,
    :sales_per_price => sales_per_price_point,
    :sales_per_product => sales_per_product,
    :sales_per_customer => sales_per_customer,
    :number_of_sales => number_of_sales
  }
end