Class: Workarea::Reports::SalesBySku
- Inherits:
-
Object
- Object
- Workarea::Reports::SalesBySku
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
#add_average_price ⇒ Object
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'app/queries/workarea/reports/sales_by_sku.rb', line 65
def add_average_price
{
'$addFields' => {
'average_price' => {
'$cond' => {
'if' => { '$lt' => ['$units_sold', 1] },
'then' => 0,
'else' => {
'$divide' => [
{
'$trunc' => {
'$multiply' => [
{ '$divide' => [
{ '$sum' => ['$merchandise', '$discounts'] },
'$units_sold'
]
},
100
]
}
},
100
]
}
}
}
}
}
end
|
#aggregation ⇒ Object
13
14
15
|
# File 'app/queries/workarea/reports/sales_by_sku.rb', line 13
def aggregation
[filter, project_used_fields, group_by_sku, add_average_price]
end
|
#filter ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'app/queries/workarea/reports/sales_by_sku.rb', line 17
def filter
result = {
'$match' => {
'reporting_on' => { '$gte' => starts_at.utc, '$lte' => ends_at.utc },
'$or' => [
{ 'orders' => { '$gt' => 0 } },
{ 'units_sold' => { '$gt' => 0 } },
{ 'units_canceled' => { '$gt' => 0 } }
]
}
}
result['$match']['sku'] = { '$in' => skus } if skus.present?
result
end
|
#group_by_sku ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'app/queries/workarea/reports/sales_by_sku.rb', line 49
def group_by_sku
{
'$group' => {
'_id' => '$sku',
'orders' => { '$sum' => '$orders' },
'units_sold' => { '$sum' => '$units_sold' },
'units_canceled' => { '$sum' => '$units_canceled' },
'merchandise' => { '$sum' => '$merchandise' },
'discounts' => { '$sum' => '$discounts' },
'tax' => { '$sum' => '$tax' },
'refund' => { '$sum' => '$refund' },
'revenue' => { '$sum' => '$revenue' }
}
}
end
|
#project_used_fields ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'app/queries/workarea/reports/sales_by_sku.rb', line 33
def project_used_fields
{
'$project' => {
'sku' => 1,
'orders' => 1,
'units_sold' => 1,
'units_canceled' => 1,
'merchandise' => 1,
'discounts' => 1,
'tax' => 1,
'refund' => 1,
'revenue' => 1
}
}
end
|
#skus ⇒ Object
9
10
11
|
# File 'app/queries/workarea/reports/sales_by_sku.rb', line 9
def skus
Array.wrap(params[:skus])
end
|