@@ -66,6 +66,11 @@ class Collection extends \Magento\Customer\Model\Resource\Customer\Collection
66
66
*/
67
67
protected $ _quoteItemFactory ;
68
68
69
+ /**
70
+ * @var \Magento\Sales\Model\Resource\Order\Collection
71
+ */
72
+ protected $ orderResource ;
73
+
69
74
/**
70
75
* @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
71
76
* @param \Psr\Log\LoggerInterface $logger
@@ -79,6 +84,7 @@ class Collection extends \Magento\Customer\Model\Resource\Customer\Collection
79
84
* @param \Magento\Framework\Object\Copy\Config $fieldsetConfig
80
85
* @param \Magento\Quote\Model\QuoteRepository $quoteRepository
81
86
* @param \Magento\Quote\Model\Resource\Quote\Item\CollectionFactory $quoteItemFactory
87
+ * @param \Magento\Sales\Model\Resource\Order\Collection $orderResource
82
88
* @param mixed $connection
83
89
* @param string $modelName
84
90
*
@@ -97,6 +103,7 @@ public function __construct(
97
103
\Magento \Framework \Object \Copy \Config $ fieldsetConfig ,
98
104
\Magento \Quote \Model \QuoteRepository $ quoteRepository ,
99
105
\Magento \Quote \Model \Resource \Quote \Item \CollectionFactory $ quoteItemFactory ,
106
+ \Magento \Sales \Model \Resource \Order \Collection $ orderResource ,
100
107
$ connection = null ,
101
108
$ modelName = self ::CUSTOMER_MODEL_NAME
102
109
) {
@@ -114,6 +121,7 @@ public function __construct(
114
121
$ connection ,
115
122
$ modelName
116
123
);
124
+ $ this ->orderResource = $ orderResource ;
117
125
$ this ->quoteRepository = $ quoteRepository ;
118
126
$ this ->_quoteItemFactory = $ quoteItemFactory ;
119
127
}
@@ -152,90 +160,6 @@ public function addCustomerName()
152
160
return $ this ;
153
161
}
154
162
155
- /**
156
- * Order for each customer
157
- *
158
- * @param string $fromDate
159
- * @param string $toDate
160
- * @return $this
161
- */
162
- public function joinOrders ($ fromDate = '' , $ toDate = '' )
163
- {
164
- if ($ fromDate != '' && $ toDate != '' ) {
165
- $ dateFilter = " AND orders.created_at BETWEEN ' {$ fromDate }' AND ' {$ toDate }' " ;
166
- } else {
167
- $ dateFilter = '' ;
168
- }
169
-
170
- $ this ->getSelect ()->joinLeft (
171
- ['orders ' => $ this ->getTable ('sales_order ' )],
172
- "orders.customer_id = e.entity_id " . $ dateFilter ,
173
- []
174
- );
175
-
176
- return $ this ;
177
- }
178
-
179
- /**
180
- * Add orders count
181
- *
182
- * @return $this
183
- */
184
- public function addOrdersCount ()
185
- {
186
- $ this ->getSelect ()->columns (
187
- ["orders_count " => "COUNT(orders.entity_id) " ]
188
- )->where (
189
- 'orders.state <> ? ' ,
190
- \Magento \Sales \Model \Order::STATE_CANCELED
191
- )->group (
192
- "e.entity_id "
193
- );
194
-
195
- return $ this ;
196
- }
197
-
198
- /**
199
- * Order summary info for each customer such as orders_count, orders_avg_amount, orders_total_amount
200
- *
201
- * @param int $storeId
202
- * @return $this
203
- */
204
- public function addSumAvgTotals ($ storeId = 0 )
205
- {
206
- $ adapter = $ this ->getConnection ();
207
- $ baseSubtotalRefunded = $ adapter ->getIfNullSql ('orders.base_subtotal_refunded ' , 0 );
208
- $ baseSubtotalCanceled = $ adapter ->getIfNullSql ('orders.base_subtotal_canceled ' , 0 );
209
-
210
- /**
211
- * calculate average and total amount
212
- */
213
- $ expr = $ storeId ==
214
- 0 ?
215
- "(orders.base_subtotal - {$ baseSubtotalCanceled } - {$ baseSubtotalRefunded }) * orders.base_to_global_rate " :
216
- "orders.base_subtotal - {$ baseSubtotalCanceled } - {$ baseSubtotalRefunded }" ;
217
-
218
- $ this ->getSelect ()->columns (
219
- ["orders_avg_amount " => "AVG( {$ expr }) " ]
220
- )->columns (
221
- ["orders_sum_amount " => "SUM( {$ expr }) " ]
222
- );
223
-
224
- return $ this ;
225
- }
226
-
227
- /**
228
- * Order by total amount
229
- *
230
- * @param string $dir
231
- * @return $this
232
- */
233
- public function orderByTotalAmount ($ dir = self ::SORT_ORDER_DESC )
234
- {
235
- $ this ->getSelect ()->order ("orders_sum_amount {$ dir }" );
236
- return $ this ;
237
- }
238
-
239
163
/**
240
164
* Add order statistics
241
165
*
@@ -259,17 +183,17 @@ protected function _addOrdersStatistics()
259
183
$ customerIds = $ this ->getColumnValues ($ this ->getResource ()->getIdFieldName ());
260
184
261
185
if ($ this ->_addOrderStatistics && !empty ($ customerIds )) {
262
- $ adapter = $ this ->getConnection ();
186
+ $ adapter = $ this ->orderResource -> getConnection ();
263
187
$ baseSubtotalRefunded = $ adapter ->getIfNullSql ('orders.base_subtotal_refunded ' , 0 );
264
188
$ baseSubtotalCanceled = $ adapter ->getIfNullSql ('orders.base_subtotal_canceled ' , 0 );
265
189
266
190
$ totalExpr = $ this ->_addOrderStatFilter ?
267
191
"(orders.base_subtotal- {$ baseSubtotalCanceled }- {$ baseSubtotalRefunded })*orders.base_to_global_rate " :
268
192
"orders.base_subtotal- {$ baseSubtotalCanceled }- {$ baseSubtotalRefunded }" ;
269
193
270
- $ select = $ this ->getConnection ()->select ();
194
+ $ select = $ this ->orderResource -> getConnection ()->select ();
271
195
$ select ->from (
272
- ['orders ' => $ this ->getTable ('sales_order ' )],
196
+ ['orders ' => $ this ->orderResource -> getTable ('sales_order ' )],
273
197
[
274
198
'orders_avg_amount ' => "AVG( {$ totalExpr }) " ,
275
199
'orders_sum_amount ' => "SUM( {$ totalExpr }) " ,
@@ -286,7 +210,7 @@ protected function _addOrdersStatistics()
286
210
'orders.customer_id '
287
211
);
288
212
289
- foreach ($ this ->getConnection ()->fetchAll ($ select ) as $ ordersInfo ) {
213
+ foreach ($ this ->orderResource -> getConnection ()->fetchAll ($ select ) as $ ordersInfo ) {
290
214
$ this ->getItemById ($ ordersInfo ['customer_id ' ])->addData ($ ordersInfo );
291
215
}
292
216
}
0 commit comments