1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Bundle\CoreBundle\Doctrine\ORM; |
13
|
|
|
|
14
|
|
|
use Sylius\Bundle\OrderBundle\Doctrine\ORM\OrderRepository as BaseOrderRepository; |
15
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
16
|
|
|
use Sylius\Component\Core\Model\CustomerInterface; |
17
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
18
|
|
|
use Sylius\Component\Core\Model\PromotionCouponInterface; |
19
|
|
|
use Sylius\Component\Core\Repository\OrderRepositoryInterface; |
20
|
|
|
|
21
|
|
|
class OrderRepository extends BaseOrderRepository implements OrderRepositoryInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
|
|
public function createListQueryBuilder() |
27
|
|
|
{ |
28
|
|
|
return $this->createQueryBuilder('o') |
29
|
|
|
->addSelect('channel') |
30
|
|
|
->leftJoin('o.channel', 'channel') |
31
|
|
|
->addSelect('customer') |
32
|
|
|
->leftJoin('o.customer', 'customer') |
33
|
|
|
->andWhere('o.state != :state') |
34
|
|
|
->setParameter('state', OrderInterface::STATE_CART) |
35
|
|
|
; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
public function createByCustomerQueryBuilder(CustomerInterface $customer) |
42
|
|
|
{ |
43
|
|
|
return $this->createQueryBuilder('o') |
44
|
|
|
->innerJoin('o.customer', 'customer') |
45
|
|
|
->andWhere('customer = :customer') |
46
|
|
|
->andWhere('o.state != :state') |
47
|
|
|
->setParameter('customer', $customer) |
48
|
|
|
->setParameter('state', OrderInterface::STATE_CART) |
49
|
|
|
; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function createByCustomerIdQueryBuilder($customerId) |
56
|
|
|
{ |
57
|
|
|
return $this->createQueryBuilder('o') |
58
|
|
|
->innerJoin('o.customer', 'customer') |
59
|
|
|
->andWhere('customer.id = :customerId') |
60
|
|
|
->andWhere('o.state != :state') |
61
|
|
|
->setParameter('customerId', $customerId) |
62
|
|
|
->setParameter('state', OrderInterface::STATE_CART) |
63
|
|
|
; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function findByCustomer(CustomerInterface $customer, array $sorting = []) |
70
|
|
|
{ |
71
|
|
|
$queryBuilder = $this->createByCustomerQueryBuilder($customer); |
72
|
|
|
$this->applySorting($queryBuilder, $sorting); |
73
|
|
|
|
74
|
|
|
return $queryBuilder |
75
|
|
|
->getQuery() |
76
|
|
|
->getResult() |
77
|
|
|
; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function findOneForPayment($id) |
84
|
|
|
{ |
85
|
|
|
return $this->createQueryBuilder('o') |
86
|
|
|
->leftJoin('o.payments', 'payments') |
87
|
|
|
->leftJoin('payments.method', 'paymentMethods') |
88
|
|
|
->addSelect('payments') |
89
|
|
|
->addSelect('paymentMethods') |
90
|
|
|
->andWhere('o.id = :id') |
91
|
|
|
->setParameter('id', $id) |
92
|
|
|
->getQuery() |
93
|
|
|
->getOneOrNullResult() |
94
|
|
|
; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
*/ |
100
|
|
|
public function countByCustomerAndCoupon(CustomerInterface $customer, PromotionCouponInterface $coupon) |
101
|
|
|
{ |
102
|
|
|
$count = $this->createQueryBuilder('o') |
103
|
|
|
->select('count(o.id)') |
104
|
|
|
->innerJoin('o.promotionCoupon', 'coupon') |
105
|
|
|
->andWhere('o.customer = :customer') |
106
|
|
|
->andWhere('coupon = :coupon') |
107
|
|
|
->andWhere('o.state != :cartState') |
108
|
|
|
->andWhere('o.state != :cancelledState') |
109
|
|
|
->setParameter('customer', $customer) |
110
|
|
|
->setParameter('coupon', $coupon) |
111
|
|
|
->setParameter('cartState', OrderInterface::STATE_CART) |
112
|
|
|
->setParameter('cancelledState', OrderInterface::STATE_CANCELLED) |
113
|
|
|
->getQuery() |
114
|
|
|
->getSingleScalarResult() |
115
|
|
|
; |
116
|
|
|
|
117
|
|
|
return (int) $count; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
|
|
public function createCheckoutsPaginator(array $criteria = null, array $sorting = null) |
124
|
|
|
{ |
125
|
|
|
$queryBuilder = $this->createQueryBuilder('o'); |
126
|
|
|
$queryBuilder |
127
|
|
|
->andWhere('o.state = :state') |
128
|
|
|
->setParameter('state', OrderInterface::STATE_CART) |
129
|
|
|
; |
130
|
|
|
|
131
|
|
|
if (!empty($criteria['createdAtFrom'])) { |
132
|
|
|
$queryBuilder |
133
|
|
|
->andWhere($queryBuilder->expr()->gte('o.createdAt', ':createdAtFrom')) |
134
|
|
|
->setParameter('createdAtFrom', $criteria['createdAtFrom']) |
135
|
|
|
; |
136
|
|
|
} |
137
|
|
|
if (!empty($criteria['createdAtTo'])) { |
138
|
|
|
$queryBuilder |
139
|
|
|
->andWhere($queryBuilder->expr()->lte('o.createdAt', ':createdAtTo')) |
140
|
|
|
->setParameter('createdAtTo', $criteria['createdAtTo']) |
141
|
|
|
; |
142
|
|
|
} |
143
|
|
|
if (!empty($criteria['channel'])) { |
144
|
|
|
$queryBuilder |
145
|
|
|
->andWhere($queryBuilder->expr()->eq('o.channel', ':channel')) |
146
|
|
|
->setParameter('channel', $criteria['channel']) |
147
|
|
|
; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
if (empty($sorting)) { |
151
|
|
|
if (!is_array($sorting)) { |
152
|
|
|
$sorting = []; |
153
|
|
|
} |
154
|
|
|
$sorting['updatedAt'] = 'desc'; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$this->applySorting($queryBuilder, $sorting); |
158
|
|
|
|
159
|
|
|
return $this->getPaginator($queryBuilder); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* {@inheritdoc} |
164
|
|
|
*/ |
165
|
|
|
public function countByCustomer(CustomerInterface $customer) |
166
|
|
|
{ |
167
|
|
|
$count = $this->createByCustomerQueryBuilder($customer) |
168
|
|
|
->select('count(o.id)') |
169
|
|
|
->getQuery() |
170
|
|
|
->getSingleScalarResult() |
171
|
|
|
; |
172
|
|
|
|
173
|
|
|
return (int) $count; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* {@inheritdoc} |
178
|
|
|
*/ |
179
|
|
|
public function findOneByNumberAndCustomer($number, CustomerInterface $customer) |
180
|
|
|
{ |
181
|
|
|
return $this->createQueryBuilder('o') |
182
|
|
|
->leftJoin('o.customer', 'customer') |
183
|
|
|
->andWhere('customer = :customer') |
184
|
|
|
->andWhere('o.number = :number') |
185
|
|
|
->setParameter('customer', $customer) |
186
|
|
|
->setParameter('number', $number) |
187
|
|
|
->getQuery() |
188
|
|
|
->getOneOrNullResult() |
189
|
|
|
; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* {@inheritdoc} |
194
|
|
|
*/ |
195
|
|
|
public function findCartByIdAndChannel($id, ChannelInterface $channel) |
196
|
|
|
{ |
197
|
|
|
return $this->createQueryBuilder('o') |
198
|
|
|
->where('o.id = :id') |
199
|
|
|
->andWhere('o.state = :state') |
200
|
|
|
->andWhere('o.channel = :channel') |
201
|
|
|
->setParameter('id', $id) |
202
|
|
|
->setParameter('state', OrderInterface::STATE_CART) |
203
|
|
|
->setParameter('channel', $channel) |
204
|
|
|
->getQuery() |
205
|
|
|
->getOneOrNullResult() |
206
|
|
|
; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* {@inheritDoc} |
211
|
|
|
*/ |
212
|
|
|
public function getTotalSalesForChannel(ChannelInterface $channel) |
213
|
|
|
{ |
214
|
|
|
$total = $this->createQueryBuilder('o') |
215
|
|
|
->select('SUM(o.total)') |
216
|
|
|
->andWhere('o.channel = :channel') |
217
|
|
|
->andWhere('o.state != :state') |
218
|
|
|
->setParameter('channel', $channel) |
219
|
|
|
->setParameter('state', OrderInterface::STATE_CART) |
220
|
|
|
->getQuery() |
221
|
|
|
->getSingleScalarResult() |
222
|
|
|
; |
223
|
|
|
|
224
|
|
|
return (int) $total; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* {@inheritDoc} |
229
|
|
|
*/ |
230
|
|
|
public function countByChannel(ChannelInterface $channel) |
231
|
|
|
{ |
232
|
|
|
$count = $this->createQueryBuilder('o') |
233
|
|
|
->select('COUNT(o.id)') |
234
|
|
|
->andWhere('o.channel = :channel') |
235
|
|
|
->andWhere('o.state != :state') |
236
|
|
|
->setParameter('channel', $channel) |
237
|
|
|
->setParameter('state', OrderInterface::STATE_CART) |
238
|
|
|
->getQuery() |
239
|
|
|
->getSingleScalarResult() |
240
|
|
|
; |
241
|
|
|
|
242
|
|
|
return (int) $count; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* {@inheritdoc} |
247
|
|
|
*/ |
248
|
|
|
public function findLatestInChannel($count, ChannelInterface $channel) |
249
|
|
|
{ |
250
|
|
|
return $this->createQueryBuilder('o') |
251
|
|
|
->andWhere('o.channel = :channel') |
252
|
|
|
->andWhere('o.state != :state') |
253
|
|
|
->setMaxResults($count) |
254
|
|
|
->orderBy('o.checkoutCompletedAt', 'desc') |
255
|
|
|
->setParameter('channel', $channel) |
256
|
|
|
->setParameter('state', OrderInterface::STATE_CART) |
257
|
|
|
->getQuery() |
258
|
|
|
->getResult() |
259
|
|
|
; |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|