Class: Workarea::Reports::SalesOverTime

Inherits:
Object
  • Object
show all
Includes:
GroupByTime, Report
Defined in:
app/queries/workarea/reports/sales_over_time.rb

Instance Method Summary collapse

Methods included from GroupByTime

#group_by, #time_group_id

Methods included from Report

#cache_key, #count, #ends_at, #initialize, #limit, #more_results?, #results, #slug, #sort, #sort_by, #sort_direction, #sort_value, #starts_at

Instance Method Details

#add_aovObject



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/queries/workarea/reports/sales_over_time.rb', line 65

def add_aov
  {
    '$addFields' => {
      'aov' => {
        '$cond' => [
          { '$eq' => ['$orders', 0] },
          0,
          { '$divide' => ['$revenue', '$orders'] }
        ]
      }
    }
  }
end

#aggregationObject



10
11
12
# File 'app/queries/workarea/reports/sales_over_time.rb', line 10

def aggregation
  [filter, project_used_fields, group_by_time, add_aov]
end

#filterObject



14
15
16
17
18
19
20
21
22
23
24
# File 'app/queries/workarea/reports/sales_over_time.rb', line 14

def filter
  {
    '$match' => {
      'reporting_on' => { '$gte' => starts_at.utc, '$lte' => ends_at.utc },
      '$or' => [
        { 'orders' => { '$gt' => 0 } },
        { 'cancellations' => { '$gt' => 0 } }
      ]
    }
  }
end

#group_by_timeObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/queries/workarea/reports/sales_over_time.rb', line 45

def group_by_time
  {
    '$group' => {
      '_id' => time_group_id,
      'starts_at' => { '$min' => '$reporting_on' },
      'orders' => { '$sum' => '$orders' },
      'cancellations' => { '$sum' => '$cancellations' },
      'returning_orders' => { '$sum' => '$returning_orders' },
      'customers' => { '$sum' => '$customers' },
      'units_sold' => { '$sum' => '$units_sold' },
      'merchandise' => { '$sum' => '$merchandise' },
      'shipping' => { '$sum' => '$shipping' },
      'discounts' => { '$sum' => '$discounts' },
      'tax' => { '$sum' => '$tax' },
      'refund' => { '$sum' => '$refund' },
      'revenue' => { '$sum' => '$revenue' }
    }
  }
end

#project_used_fieldsObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/queries/workarea/reports/sales_over_time.rb', line 26

def project_used_fields
  {
    '$project' => {
      'reporting_on' => 1,
      'orders' => 1,
      'cancellations' => 1,
      'returning_orders' => 1,
      'customers' => 1,
      'units_sold' => 1,
      'merchandise' => 1,
      'shipping' => 1,
      'discounts' => 1,
      'tax' => 1,
      'refund' => 1,
      'revenue' => 1
    }
  }
end