Class: Workarea::Reports::SalesByProduct
- Inherits:
-
Object
- Object
- Workarea::Reports::SalesByProduct
show all
- Includes:
- Report
- Defined in:
- app/queries/workarea/reports/sales_by_product.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'app/queries/workarea/reports/sales_by_product.rb', line 58
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
9
10
11
|
# File 'app/queries/workarea/reports/sales_by_product.rb', line 9
def aggregation
[filter, project_used_fields, group_by_product, add_average_price]
end
|
#filter ⇒ Object
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'app/queries/workarea/reports/sales_by_product.rb', line 13
def filter
{
'$match' => {
'reporting_on' => { '$gte' => starts_at.utc, '$lte' => ends_at.utc },
'$or' => [
{ 'orders' => { '$gt' => 0 } },
{ 'units_sold' => { '$gt' => 0 } },
{ 'units_canceled' => { '$gt' => 0 } }
]
}
}
end
|
#group_by_product ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'app/queries/workarea/reports/sales_by_product.rb', line 42
def group_by_product
{
'$group' => {
'_id' => '$product_id',
'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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'app/queries/workarea/reports/sales_by_product.rb', line 26
def project_used_fields
{
'$project' => {
'product_id' => 1,
'orders' => 1,
'units_sold' => 1,
'units_canceled' => 1,
'merchandise' => 1,
'discounts' => 1,
'tax' => 1,
'refund' => 1,
'revenue' => 1
}
}
end
|