1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sales\Model\ResourceModel\Report\Order;
/**
* Order entity resource model with aggregation by created at
*
* @author Magento Core Team <core@magentocommerce.com>
*/
class Createdat extends \Magento\Sales\Model\ResourceModel\Report\AbstractReport
{
/**
* Model initialization
*
* @return void
*/
protected function _construct()
{
$this->_init('sales_order_aggregated_created', 'id');
}
/**
* Aggregate Orders data by order created at
*
* @param string|int|\DateTime|array|null $from
* @param string|int|\DateTime|array|null $to
* @return $this
*/
public function aggregate($from = null, $to = null)
{
return $this->_aggregateByField('created_at', $from, $to);
}
/**
* Aggregate Orders data by custom field
*
* @param string $aggregationField
* @param string|int|\DateTime|array|null $from
* @param string|int|\DateTime|array|null $to
* @return $this
* @throws \Exception
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/
protected function _aggregateByField($aggregationField, $from, $to)
{
$connection = $this->getConnection();
$connection->beginTransaction();
try {
if ($from !== null || $to !== null) {
$subSelect = $this->_getTableDateRangeSelect(
$this->getTable('sales_order'),
$aggregationField,
$aggregationField,
$from,
$to
);
} else {
$subSelect = null;
}
$this->_clearTableByDateRange($this->getMainTable(), $from, $to, $subSelect);
$periodExpr = $connection->getDatePartSql(
$this->getStoreTZOffsetQuery(
['o' => $this->getTable('sales_order')],
'o.' . $aggregationField,
$from,
$to
)
);
// Columns list
$columns = [
'period' => $periodExpr,
'store_id' => 'o.store_id',
'order_status' => 'o.status',
'orders_count' => new \Zend_Db_Expr('COUNT(o.entity_id)'),
'total_qty_ordered' => new \Zend_Db_Expr('SUM(oi.total_qty_ordered)'),
'total_qty_invoiced' => new \Zend_Db_Expr('SUM(oi.total_qty_invoiced)'),
'total_income_amount' => new \Zend_Db_Expr(
sprintf(
'SUM((%s - %s) * %s)',
$connection->getIfNullSql('o.base_grand_total', 0),
$connection->getIfNullSql('o.base_total_canceled', 0),
$connection->getIfNullSql('o.base_to_global_rate', 0)
)
),
'total_revenue_amount' => new \Zend_Db_Expr(
sprintf(
'SUM((%s - %s - %s - (%s - %s - %s)) * %s)',
$connection->getIfNullSql('o.base_total_invoiced', 0),
$connection->getIfNullSql('o.base_tax_invoiced', 0),
$connection->getIfNullSql('o.base_shipping_invoiced', 0),
$connection->getIfNullSql('o.base_total_refunded', 0),
$connection->getIfNullSql('o.base_tax_refunded', 0),
$connection->getIfNullSql('o.base_shipping_refunded', 0),
$connection->getIfNullSql('o.base_to_global_rate', 0)
)
),
'total_profit_amount' => new \Zend_Db_Expr(
sprintf(
'SUM((%s - %s - %s - %s - %s) * %s)',
$connection->getIfNullSql('o.base_total_paid', 0),
$connection->getIfNullSql('o.base_total_refunded', 0),
$connection->getIfNullSql('o.base_tax_invoiced', 0),
$connection->getIfNullSql('o.base_shipping_invoiced', 0),
$connection->getIfNullSql('o.base_total_invoiced_cost', 0),
$connection->getIfNullSql('o.base_to_global_rate', 0)
)
),
'total_invoiced_amount' => new \Zend_Db_Expr(
sprintf(
'SUM(%s * %s)',
$connection->getIfNullSql('o.base_total_invoiced', 0),
$connection->getIfNullSql('o.base_to_global_rate', 0)
)
),
'total_canceled_amount' => new \Zend_Db_Expr(
sprintf(
'SUM(%s * %s)',
$connection->getIfNullSql('o.base_total_canceled', 0),
$connection->getIfNullSql('o.base_to_global_rate', 0)
)
),
'total_paid_amount' => new \Zend_Db_Expr(
sprintf(
'SUM(%s * %s)',
$connection->getIfNullSql('o.base_total_paid', 0),
$connection->getIfNullSql('o.base_to_global_rate', 0)
)
),
'total_refunded_amount' => new \Zend_Db_Expr(
sprintf(
'SUM(%s * %s)',
$connection->getIfNullSql('o.base_total_refunded', 0),
$connection->getIfNullSql('o.base_to_global_rate', 0)
)
),
'total_tax_amount' => new \Zend_Db_Expr(
sprintf(
'SUM((%s - %s) * %s)',
$connection->getIfNullSql('o.base_tax_amount', 0),
$connection->getIfNullSql('o.base_tax_canceled', 0),
$connection->getIfNullSql('o.base_to_global_rate', 0)
)
),
'total_tax_amount_actual' => new \Zend_Db_Expr(
sprintf(
'SUM((%s -%s) * %s)',
$connection->getIfNullSql('o.base_tax_invoiced', 0),
$connection->getIfNullSql('o.base_tax_refunded', 0),
$connection->getIfNullSql('o.base_to_global_rate', 0)
)
),
'total_shipping_amount' => new \Zend_Db_Expr(
sprintf(
'SUM((%s - %s) * %s)',
$connection->getIfNullSql('o.base_shipping_amount', 0),
$connection->getIfNullSql('o.base_shipping_canceled', 0),
$connection->getIfNullSql('o.base_to_global_rate', 0)
)
),
'total_shipping_amount_actual' => new \Zend_Db_Expr(
sprintf(
'SUM((%s - %s) * %s)',
$connection->getIfNullSql('o.base_shipping_invoiced', 0),
$connection->getIfNullSql('o.base_shipping_refunded', 0),
$connection->getIfNullSql('o.base_to_global_rate', 0)
)
),
'total_discount_amount' => new \Zend_Db_Expr(
sprintf(
'SUM((ABS(%s) - %s) * %s)',
$connection->getIfNullSql('o.base_discount_amount', 0),
$connection->getIfNullSql('o.base_discount_canceled', 0),
$connection->getIfNullSql('o.base_to_global_rate', 0)
)
),
'total_discount_amount_actual' => new \Zend_Db_Expr(
sprintf(
'SUM((%s - %s) * %s)',
$connection->getIfNullSql('o.base_discount_invoiced', 0),
$connection->getIfNullSql('o.base_discount_refunded', 0),
$connection->getIfNullSql('o.base_to_global_rate', 0)
)
),
];
$select = $connection->select();
$selectOrderItem = $connection->select();
$qtyCanceledExpr = $connection->getIfNullSql('qty_canceled', 0);
$cols = [
'order_id' => 'order_id',
'total_qty_ordered' => new \Zend_Db_Expr("SUM(qty_ordered - {$qtyCanceledExpr})"),
'total_qty_invoiced' => new \Zend_Db_Expr('SUM(qty_invoiced)'),
];
$selectOrderItem->from(
$this->getTable('sales_order_item'),
$cols
)->where(
'parent_item_id IS NULL'
)->group(
'order_id'
);
$select->from(
['o' => $this->getTable('sales_order')],
$columns
)->join(
['oi' => $selectOrderItem],
'oi.order_id = o.entity_id',
[]
)->where(
'o.state NOT IN (?)',
[\Magento\Sales\Model\Order::STATE_PENDING_PAYMENT, \Magento\Sales\Model\Order::STATE_NEW]
);
if ($subSelect !== null) {
$select->having($this->_makeConditionFromDateRangeSelect($subSelect, 'period'));
}
$select->group([$periodExpr, 'o.store_id', 'o.status']);
$connection->query($select->insertFromSelect($this->getMainTable(), array_keys($columns)));
// setup all columns to select SUM() except period, store_id and order_status
foreach ($columns as $k => $v) {
$columns[$k] = new \Zend_Db_Expr('SUM(' . $k . ')');
}
$columns['period'] = 'period';
$columns['store_id'] = new \Zend_Db_Expr(\Magento\Store\Model\Store::DEFAULT_STORE_ID);
$columns['order_status'] = 'order_status';
$select->reset();
$select->from($this->getMainTable(), $columns)->where('store_id <> 0');
if ($subSelect !== null) {
$select->where($this->_makeConditionFromDateRangeSelect($subSelect, 'period'));
}
$select->group(['period', 'order_status']);
$connection->query($select->insertFromSelect($this->getMainTable(), array_keys($columns)));
$connection->commit();
} catch (\Exception $e) {
$connection->rollBack();
throw $e;
}
return $this;
}
}