1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Odiseo\SyliusReportPlugin\Filter; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Doctrine\ORM\EntityManager; |
7
|
|
|
use Doctrine\ORM\QueryBuilder; |
8
|
|
|
use Exception; |
9
|
|
|
use Sylius\Component\Core\Model\AddressInterface; |
10
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
11
|
|
|
use Sylius\Component\Core\Model\Customer; |
12
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Odiseo Team <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class QueryFilter |
18
|
|
|
{ |
19
|
|
|
/** @var EntityManager */ |
20
|
|
|
protected $em; |
21
|
|
|
|
22
|
|
|
/** @var QueryBuilder */ |
23
|
|
|
protected $qb; |
24
|
|
|
|
25
|
|
|
/** @var array */ |
26
|
|
|
protected $joins = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param EntityManager $entityManager |
30
|
|
|
*/ |
31
|
|
|
public function __construct(EntityManager $entityManager) |
32
|
|
|
{ |
33
|
|
|
$this->em = $entityManager; |
34
|
|
|
|
35
|
|
|
$this->qb = $this->em->createQueryBuilder(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return QueryBuilder |
40
|
|
|
*/ |
41
|
|
|
public function getQueryBuilder() |
42
|
|
|
{ |
43
|
|
|
return $this->qb; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return EntityManager |
48
|
|
|
*/ |
49
|
|
|
public function getEntityManager() |
50
|
|
|
{ |
51
|
|
|
return $this->em; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function reset() |
55
|
|
|
{ |
56
|
|
|
$this->qb = $this->em->createQueryBuilder(); |
57
|
|
|
$this->joins = []; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param QueryBuilder $qb |
62
|
|
|
* @param array $configuration |
63
|
|
|
* @param string $dateField |
64
|
|
|
* |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
|
|
protected function getGroupByParts(QueryBuilder $qb, array $configuration = [], $dateField = 'checkoutCompletedAt') |
68
|
|
|
{ |
69
|
|
|
if (false === strpos($dateField, '.')) { |
70
|
|
|
$rootAlias = $qb->getRootAliases()[0]; |
71
|
|
|
$dateF = $rootAlias.'.'.$dateField; |
72
|
|
|
} else { |
73
|
|
|
$dateF = $dateField; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$selectPeriod = ''; |
77
|
|
|
$selectGroupBy = ''; |
78
|
|
|
foreach ($configuration['groupBy'] as $groupByElement) { |
79
|
|
|
if (strlen($selectPeriod) > 0) { |
80
|
|
|
$selectPeriod .= ', '; |
81
|
|
|
$selectGroupBy .= ','; |
82
|
|
|
} |
83
|
|
|
$salias = ucfirst(strtolower($groupByElement)).'Date'; |
84
|
|
|
$selectPeriod .= $groupByElement.'('.$dateF.') as '.$salias; |
85
|
|
|
|
86
|
|
|
$selectGroupBy .= $salias; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return [$selectPeriod, $selectGroupBy]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $join |
94
|
|
|
* @param string $alias |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function addLeftJoin($join, $alias): string |
99
|
|
|
{ |
100
|
|
|
if (!isset($this->joins[$join])) { |
101
|
|
|
$this->joins[$join] = $alias; |
102
|
|
|
|
103
|
|
|
$this->qb->leftJoin($join, $alias); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $this->joins[$join]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param array $configuration |
111
|
|
|
* @param string $dateField |
112
|
|
|
* @param string $rootAlias |
113
|
|
|
* @throws Exception |
114
|
|
|
*/ |
115
|
|
|
public function addTimePeriod(array $configuration = [], $dateField = 'checkoutCompletedAt', $rootAlias = null): void |
116
|
|
|
{ |
117
|
|
|
if (false === strpos($dateField, '.')) { |
118
|
|
|
if (!$rootAlias) { |
119
|
|
|
$rootAlias = $this->qb->getRootAliases()[0]; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$dateF = $rootAlias.'.'.$dateField; |
123
|
|
|
} else { |
124
|
|
|
$dateF = $dateField; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$groupByParts = $this->getGroupByParts($this->qb, $configuration, $dateField); |
128
|
|
|
|
129
|
|
|
/** @var DateTime $startDateTime */ |
130
|
|
|
$startDateTime = $configuration['timePeriod']['start']; |
131
|
|
|
/** @var DateTime $endDateTime */ |
132
|
|
|
$endDateTime = $configuration['timePeriod']['end']?:new DateTime(); |
133
|
|
|
|
134
|
|
|
if ($groupByParts[0] && $groupByParts[1]) { |
135
|
|
|
$this->qb |
136
|
|
|
->addSelect($groupByParts[0]) |
137
|
|
|
->andWhere($this->qb->expr()->gte($dateF, ':from')) |
138
|
|
|
->andWhere($this->qb->expr()->lte($dateF, ':to')) |
139
|
|
|
->setParameter('from', $startDateTime->format('Y-m-d H:i:s')) |
140
|
|
|
->setParameter('to', $endDateTime->format('Y-m-d H:i:s')) |
141
|
|
|
->groupBy($groupByParts[1]) |
142
|
|
|
->orderBy('date,' . $groupByParts[1]) |
143
|
|
|
; |
144
|
|
|
} else { |
145
|
|
|
$this->qb |
146
|
|
|
->andWhere($this->qb->expr()->gte($dateF, ':from')) |
147
|
|
|
->andWhere($this->qb->expr()->lte($dateF, ':to')) |
148
|
|
|
->setParameter('from', $startDateTime->format('Y-m-d H:i:s')) |
149
|
|
|
->setParameter('to', $endDateTime->format('Y-m-d H:i:s')) |
150
|
|
|
->orderBy('date') |
151
|
|
|
; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param array $configuration |
157
|
|
|
* @param string $field |
158
|
|
|
* @param string $rootAlias |
159
|
|
|
*/ |
160
|
|
|
public function addChannel(array $configuration = [], $field = null, $rootAlias = null): void |
161
|
|
|
{ |
162
|
|
|
if (isset($configuration['channel']) && count($configuration['channel']) > 0) { |
163
|
|
|
$storeIds = []; |
164
|
|
|
|
165
|
|
|
if ($configuration['channel'] instanceof ChannelInterface) { |
166
|
|
|
$storeIds[] = $configuration['channel']->getId(); |
167
|
|
|
} elseif (is_array($configuration['channel']) && !in_array(0, $configuration['channel'])) { |
168
|
|
|
$storeIds = $configuration['channel']; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
if (!(count($storeIds) > 0)) { |
172
|
|
|
return; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
if (!$field) { |
176
|
|
|
if (!$rootAlias) { |
177
|
|
|
$rootAlias = $this->qb->getRootAliases()[0]; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$field = $rootAlias.'.channel'; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$this->qb |
184
|
|
|
->andWhere($this->qb->expr()->in($field, $storeIds)) |
185
|
|
|
; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param array $configuration |
191
|
|
|
* @param string $rootAlias |
192
|
|
|
*/ |
193
|
|
|
public function addUserGender(array $configuration = [], $rootAlias = null): void |
194
|
|
|
{ |
195
|
|
|
if (isset($configuration['userGender']) && count($configuration['userGender']) > 0) { |
196
|
|
|
$cAlias = $rootAlias; |
197
|
|
|
if (!$rootAlias) { |
198
|
|
|
$rootAlias = $cAlias = $this->qb->getRootAliases()[0]; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
if (!$this->hasRootEntity(Customer::class)) { |
202
|
|
|
$cAlias = $this->addLeftJoin($rootAlias.'.customer', 'c'); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$this->qb |
206
|
|
|
->andWhere($this->qb->expr()->in($cAlias.'.gender', $configuration['userGender'])) |
207
|
|
|
; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @param array $configuration |
213
|
|
|
* @param string $addressType |
214
|
|
|
* @param string $rootAlias |
215
|
|
|
*/ |
216
|
|
|
public function addUserCountry(array $configuration = [], string $addressType = 'shipping', $rootAlias = null): void |
217
|
|
|
{ |
218
|
|
|
$type = 'user'.ucfirst($addressType).'Country'; |
219
|
|
|
|
220
|
|
|
if (isset($configuration[$type]) && count($configuration[$type]) > 0) { |
221
|
|
|
$cAlias = $rootAlias; |
222
|
|
|
if (!$rootAlias) { |
223
|
|
|
$rootAlias = $cAlias = $this->qb->getRootAliases()[0]; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
if (!$this->hasRootEntity(Customer::class)) { |
227
|
|
|
$cAlias = $this->addLeftJoin($rootAlias.'.customer', 'c'); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
$caAlias = $this->addLeftJoin($cAlias.'.addresses', 'c'.substr($addressType, 0, 1).'a'); |
231
|
|
|
|
232
|
|
|
$this->qb |
233
|
|
|
->andWhere($this->qb->expr()->in($caAlias.'.countryCode', $configuration[$type])) |
234
|
|
|
; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @param array $configuration |
240
|
|
|
* @param string $addressType |
241
|
|
|
* @param string $rootAlias |
242
|
|
|
*/ |
243
|
|
|
public function addUserProvince(array $configuration = [], string $addressType = 'shipping', $rootAlias = null): void |
244
|
|
|
{ |
245
|
|
|
$type = 'user'.ucfirst($addressType).'Province'; |
246
|
|
|
|
247
|
|
|
if (isset($configuration[$type]) && count($configuration[$type]) > 0) { |
248
|
|
|
$provinces = $configuration[$type]->map(function (AddressInterface $address) { |
249
|
|
|
return $address->getProvinceCode() ?: $address->getProvinceName(); |
250
|
|
|
})->toArray(); |
251
|
|
|
|
252
|
|
|
$cAlias = $rootAlias; |
253
|
|
|
if (!$rootAlias) { |
254
|
|
|
$rootAlias = $cAlias = $this->qb->getRootAliases()[0]; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
if (!$this->hasRootEntity(Customer::class)) { |
258
|
|
|
$cAlias = $this->addLeftJoin($rootAlias.'.customer', 'c'); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
$caAlias = $this->addLeftJoin($cAlias.'.addresses', 'c'.substr($addressType, 0, 1).'a'); |
262
|
|
|
|
263
|
|
|
$this->qb |
264
|
|
|
->andWhere($this->qb->expr()->orX( |
265
|
|
|
$this->qb->expr()->in($caAlias.'.provinceCode', $provinces), |
266
|
|
|
$this->qb->expr()->in($caAlias.'.provinceName', $provinces) |
267
|
|
|
)) |
268
|
|
|
; |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @param array $configuration |
274
|
|
|
* @param string $addressType |
275
|
|
|
* @param string $rootAlias |
276
|
|
|
*/ |
277
|
|
|
public function addUserCity(array $configuration = [], string $addressType = 'shipping', $rootAlias = null): void |
278
|
|
|
{ |
279
|
|
|
$type = 'user'.ucfirst($addressType).'City'; |
280
|
|
|
|
281
|
|
|
if (isset($configuration[$type]) && count($configuration[$type]) > 0) { |
282
|
|
|
$cities = $configuration[$type]->map(function (AddressInterface $address) { |
283
|
|
|
return $address->getCity(); |
284
|
|
|
})->toArray(); |
285
|
|
|
|
286
|
|
|
$cAlias = $rootAlias; |
287
|
|
|
if (!$rootAlias) { |
288
|
|
|
$rootAlias = $cAlias = $this->qb->getRootAliases()[0]; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
if (!$this->hasRootEntity(Customer::class)) { |
292
|
|
|
$cAlias = $this->addLeftJoin($rootAlias.'.customer', 'c'); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
$caAlias = $this->addLeftJoin($cAlias.'.addresses', 'c'.substr($addressType, 0, 1).'a'); |
296
|
|
|
|
297
|
|
|
$this->qb |
298
|
|
|
->andWhere($this->qb->expr()->in($caAlias.'.city', $cities)) |
299
|
|
|
; |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @param array $configuration |
305
|
|
|
* @param string $addressType |
306
|
|
|
* @param string $rootAlias |
307
|
|
|
*/ |
308
|
|
|
public function addUserPostcode(array $configuration = [], string $addressType = 'shipping', $rootAlias = null): void |
309
|
|
|
{ |
310
|
|
|
$type = 'user'.ucfirst($addressType).'Postcode'; |
311
|
|
|
|
312
|
|
|
if (isset($configuration[$type]) && count($configuration[$type]) > 0) { |
313
|
|
|
$codes = $configuration[$type]->map(function (AddressInterface $address) { |
314
|
|
|
return $address->getPostcode(); |
315
|
|
|
})->toArray(); |
316
|
|
|
|
317
|
|
|
$cAlias = $rootAlias; |
318
|
|
|
if (!$rootAlias) { |
319
|
|
|
$rootAlias = $cAlias = $this->qb->getRootAliases()[0]; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
if (!$this->hasRootEntity(Customer::class)) { |
323
|
|
|
$cAlias = $this->addLeftJoin($rootAlias.'.customer', 'c'); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
$caAlias = $this->addLeftJoin($cAlias.'.addresses', 'c'.substr($addressType, 0, 1).'2a'); |
327
|
|
|
|
328
|
|
|
$this->qb |
329
|
|
|
->andWhere($this->qb->expr()->in($caAlias.'.postcode', $codes)) |
330
|
|
|
; |
331
|
|
|
} |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* @param array $configuration |
336
|
|
|
* @param string $field |
337
|
|
|
*/ |
338
|
|
|
public function addProduct(array $configuration = [], string $field = 'p.id'): void |
339
|
|
|
{ |
340
|
|
|
if (isset($configuration['product']) && count($configuration['product']) > 0) { |
341
|
|
|
$products = $configuration['product']->map(function (ProductInterface $product) { |
342
|
|
|
return $product->getId(); |
343
|
|
|
})->toArray(); |
344
|
|
|
|
345
|
|
|
$this->qb |
346
|
|
|
->andWhere($this->qb->expr()->in($field, $products)) |
347
|
|
|
; |
348
|
|
|
} |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @param array $configuration |
353
|
|
|
* @param string $field |
354
|
|
|
*/ |
355
|
|
|
public function addProductCategory(array $configuration = [], string $field = 'pt.taxon'): void |
356
|
|
|
{ |
357
|
|
|
if (isset($configuration['productCategory']) && count($configuration['productCategory']) > 0) { |
358
|
|
|
$this->qb |
359
|
|
|
->andWhere($this->qb->expr()->in($field, $configuration['productCategory'])) |
360
|
|
|
; |
361
|
|
|
} |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* @param string $rootEntityClassname |
366
|
|
|
* |
367
|
|
|
* @return bool |
368
|
|
|
*/ |
369
|
|
|
protected function hasRootEntity($rootEntityClassname): bool |
370
|
|
|
{ |
371
|
|
|
return in_array($rootEntityClassname, $this->qb->getRootEntities()); |
372
|
|
|
} |
373
|
|
|
} |
374
|
|
|
|