Class: Workarea::Reports::SalesBySku

Inherits:
Object
  • Object
show all
Includes:
Report
Defined in:
app/queries/workarea/reports/sales_by_sku.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



13
14
15
# File 'app/queries/workarea/reports/sales_by_sku.rb', line 13

def aggregation
  [filter, project_used_fields, group_by_sku]
end

#filterObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/queries/workarea/reports/sales_by_sku.rb', line 17

def filter
  result = {
    '$match' => {
      'reporting_on' => { '$gte' => starts_at, '$lte' => ends_at },
      '$or' => [
        { 'orders' => { '$gt' => 0 } },
        { 'units_sold' => { '$gt' => 0 } }
      ]
    }
  }

  result['$match']['sku'] = { '$in' => skus } if skus.present?
  result
end

#group_by_skuObject



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/queries/workarea/reports/sales_by_sku.rb', line 46

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

#project_used_fieldsObject



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/queries/workarea/reports/sales_by_sku.rb', line 32

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

#skusObject



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

def skus
  Array.wrap(params[:skus])
end