Class: Workarea::Reports::SalesByCategory

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

Instance Method Summary collapse

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

#aggregationObject



9
10
11
# File 'app/queries/workarea/reports/sales_by_category.rb', line 9

def aggregation
  [filter, project_used_fields, group_by_category]
end

#filterObject



13
14
15
16
17
18
19
20
# File 'app/queries/workarea/reports/sales_by_category.rb', line 13

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

#group_by_categoryObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/queries/workarea/reports/sales_by_category.rb', line 36

def group_by_category
  {
    '$group' => {
      '_id' => '$category_id',
      'orders' => { '$sum' => '$orders' },
      'units_sold' => { '$sum' => '$units_sold' },
      'merchandise' => { '$sum' => '$merchandise' },
      'discounts' => { '$sum' => '$discounts' },
      'tax' => { '$sum' => '$tax' },
      'revenue' => { '$sum' => '$revenue' }
    }
  }
end

#project_used_fieldsObject



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/queries/workarea/reports/sales_by_category.rb', line 22

def project_used_fields
  {
    '$project' => {
      'category_id' => 1,
      'orders' => 1,
      'units_sold' => 1,
      'merchandise' => 1,
      'discounts' => 1,
      'tax' => 1,
      'revenue' => 1
    }
  }
end