Failed Conditions
Push — experimental/sf ( ef9148...a8c20b )
by Ryo
1419:13 queued 1408:28
created

src/Eccube/Repository/OrderRepository.php (2 issues)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Repository;
15
16
use Doctrine\ORM\QueryBuilder;
17
use Eccube\Doctrine\Query\Queries;
18
use Eccube\Entity\Order;
19
use Eccube\Entity\Master\OrderStatus;
20
use Eccube\Util\StringUtil;
21
use Symfony\Bridge\Doctrine\RegistryInterface;
22
23
/**
24
 * OrderRepository
25
 *
26
 * This class was generated by the Doctrine ORM. Add your own custom
27
 * repository methods below.
28
 */
29
class OrderRepository extends AbstractRepository
30
{
31
    /**
32
     * @var Queries
33
     */
34
    protected $queries;
35
36
    /**
37
     * OrderRepository constructor.
38
     *
39
     * @param RegistryInterface $registry
40
     * @param Queries $queries
41
     */
42 755
    public function __construct(RegistryInterface $registry, Queries $queries)
43
    {
44 755
        parent::__construct($registry, Order::class);
45 755
        $this->queries = $queries;
46
    }
47
48
    /**
49
     * @param int $orderId
50
     * @param OrderStatus $Status
51
     */
52 7
    public function changeStatus($orderId, \Eccube\Entity\Master\OrderStatus $Status)
53
    {
54
        $Order = $this
55 7
            ->find($orderId)
56 7
            ->setOrderStatus($Status)
57
        ;
58
59 7
        switch ($Status->getId()) {
60 7
            case '5': // 発送済へ
61 1
                $Order->setShippingDate(new \DateTime());
62 1
                break;
63 6
            case '6': // 入金済へ
64 3
                $Order->setPaymentDate(new \DateTime());
65 3
                break;
66
        }
67
68 7
        $em = $this->getEntityManager();
69 7
        $em->persist($Order);
70 7
        $em->flush();
71
    }
72
73
    /**
74
     * @param array $searchData
75
     *
76
     * @return QueryBuilder
77
     */
78 17
    public function getQueryBuilderBySearchData($searchData)
79
    {
80 17
        $qb = $this->createQueryBuilder('o');
81
82 17
        $joinedCustomer = false;
83
84
        // order_id_start
85 17 View Code Duplication
        if (isset($searchData['order_id_start']) && StringUtil::isNotBlank($searchData['order_id_start'])) {
86
            $qb
87 1
                ->andWhere('o.id >= :order_id_start')
88 1
                ->setParameter('order_id_start', $searchData['order_id_start']);
89
        }
90
91
        // order_id_end
92 17 View Code Duplication
        if (isset($searchData['order_id_end']) && StringUtil::isNotBlank($searchData['order_id_end'])) {
93
            $qb
94 1
                ->andWhere('o.id <= :order_id_end')
95 1
                ->setParameter('order_id_end', $searchData['order_id_end']);
96
        }
97
98
        // status
99 17
        if (!empty($searchData['status']) && $searchData['status']) {
100
            $qb
101 1
                ->andWhere('o.OrderStatus = :status')
102 1
                ->setParameter('status', $searchData['status']);
103
        }
104
105
        // name
106 17 View Code Duplication
        if (isset($searchData['name']) && StringUtil::isNotBlank($searchData['name'])) {
107
            $qb
108 1
                ->andWhere('CONCAT(o.name01, o.name02) LIKE :name')
109 1
                ->setParameter('name', '%'.$searchData['name'].'%');
110
        }
111
112
        // kana
113 17 View Code Duplication
        if (isset($searchData['kana']) && StringUtil::isNotBlank($searchData['kana'])) {
114
            $qb
115 1
                ->andWhere('CONCAT(o.kana01, o.kana02) LIKE :kana')
116 1
                ->setParameter('kana', '%'.$searchData['kana'].'%');
117
        }
118
119
        // email
120 17 View Code Duplication
        if (isset($searchData['email']) && StringUtil::isNotBlank($searchData['email'])) {
121
            $qb
122 1
                ->andWhere('o.email = :email')
123 1
                ->setParameter('email', $searchData['email']);
124
        }
125
126
        // tel
127 17 View Code Duplication
        if (isset($searchData['phone_number']) && StringUtil::isNotBlank($searchData['phone_number'])) {
128
            $qb
129 1
                ->andWhere('o.phone_number = :phone_number')
130 1
                ->setParameter('phone_number', $searchData['phone_number']);
131
        }
132
133
        // birth
134 17 View Code Duplication
        if (!empty($searchData['birth_start']) && $searchData['birth_start']) {
135 1
            if (!$joinedCustomer) {
136 1
                $qb->leftJoin('o.Customer', 'c');
137 1
                $joinedCustomer = true;
138
            }
139
140 1
            $date = $searchData['birth_start'];
141
            $qb
142 1
                ->andWhere('c.birth >= :birth_start')
143 1
                ->setParameter('birth_start', $date);
144
        }
145 17 View Code Duplication
        if (!empty($searchData['birth_end']) && $searchData['birth_end']) {
146 1
            if (!$joinedCustomer) {
147 1
                $qb->leftJoin('o.Customer', 'c');
148 1
                $joinedCustomer = true;
149
            }
150
151 1
            $date = clone $searchData['birth_end'];
152
            $date = $date
153 1
                ->modify('+1 days');
154
            $qb
155 1
                ->andWhere('c.birth < :birth_end')
156 1
                ->setParameter('birth_end', $date);
157
        }
158
159
        // sex
160 17
        if (!empty($searchData['sex']) && count($searchData['sex']) > 0) {
161 1
            if (!$joinedCustomer) {
162 1
                $qb->leftJoin('o.Customer', 'c');
163 1
                $joinedCustomer = true;
164
            }
165
166 1
            $sexs = [];
167 1
            foreach ($searchData['sex'] as $sex) {
168 1
                $sexs[] = $sex->getId();
169
            }
170
171
            $qb
172 1
                ->andWhere($qb->expr()->in('c.Sex', ':sexs'))
173 1
                ->setParameter('sexs', $sexs);
174
        }
175
176
        // payment
177 17 View Code Duplication
        if (!empty($searchData['payment']) && count($searchData['payment'])) {
178
            $payments = [];
179
            foreach ($searchData['payment'] as $payment) {
180
                $payments[] = $payment->getId();
181
            }
182
            $qb
183
                ->leftJoin('o.Payment', 'p')
184
                ->andWhere($qb->expr()->in('p.id', ':payments'))
185
                ->setParameter('payments', $payments);
186
        }
187
188
        // oreder_date
189 17 View Code Duplication
        if (!empty($searchData['order_date_start']) && $searchData['order_date_start']) {
190 1
            $date = $searchData['order_date_start'];
191
            $qb
192 1
                ->andWhere('o.create_date >= :order_date_start')
193 1
                ->setParameter('order_date_start', $date);
194
        }
195 17 View Code Duplication
        if (!empty($searchData['order_date_end']) && $searchData['order_date_end']) {
196 1
            $date = clone $searchData['order_date_end'];
197
            $date = $date
198 1
                ->modify('+1 days');
199
            $qb
200 1
                ->andWhere('o.create_date < :order_date_end')
201 1
                ->setParameter('order_date_end', $date);
202
        }
203
204
        // create_date
205 17 View Code Duplication
        if (!empty($searchData['update_date_start']) && $searchData['update_date_start']) {
206 1
            $date = $searchData['update_date_start'];
207
            $qb
208 1
                ->andWhere('o.update_date >= :update_date_start')
209 1
                ->setParameter('update_date_start', $date);
210
        }
211 17 View Code Duplication
        if (!empty($searchData['update_date_end']) && $searchData['update_date_end']) {
212 1
            $date = clone $searchData['update_date_end'];
213
            $date = $date
214 1
                ->modify('+1 days');
215
            $qb
216 1
                ->andWhere('o.update_date < :update_date_end')
217 1
                ->setParameter('update_date_end', $date);
218
        }
219
220
        // payment_total
221 17 View Code Duplication
        if (isset($searchData['payment_total_start']) && StringUtil::isNotBlank($searchData['payment_total_start'])) {
222
            $qb
223 1
                ->andWhere('o.payment_total >= :payment_total_start')
224 1
                ->setParameter('payment_total_start', $searchData['payment_total_start']);
225
        }
226 17 View Code Duplication
        if (isset($searchData['payment_total_end']) && StringUtil::isNotBlank($searchData['payment_total_end'])) {
227
            $qb
228 1
                ->andWhere('o.payment_total <= :payment_total_end')
229 1
                ->setParameter('payment_total_end', $searchData['payment_total_end']);
230
        }
231
232
        // buy_product_name
233 17 View Code Duplication
        if (isset($searchData['buy_product_name']) && StringUtil::isNotBlank($searchData['buy_product_name'])) {
234
            $qb
235 1
                ->leftJoin('o.OrderItems', 'oi')
236 1
                ->andWhere('oi.product_name LIKE :buy_product_name')
237 1
                ->setParameter('buy_product_name', '%'.$searchData['buy_product_name'].'%');
238
        }
239
240
        // Order By
241 17
        $qb->addOrderBy('o.update_date', 'DESC');
242
243 17
        return $this->queries->customize(QueryKey::ORDER_SEARCH, $qb, $searchData);
244
    }
245
246
    /**
247
     * @param  array        $searchData
248
     *
249
     * @return QueryBuilder
250
     */
251 32
    public function getQueryBuilderBySearchDataForAdmin($searchData)
252
    {
253 32
        $qb = $this->createQueryBuilder('o')
254 32
            ->select('o, s')
255 32
            ->innerJoin('o.Shippings', 's');
256
257
        // order_id_start
258 32 View Code Duplication
        if (isset($searchData['order_id']) && StringUtil::isNotBlank($searchData['order_id'])) {
259
            $qb
260
                ->andWhere('o.id = :order_id')
261
                ->setParameter('order_id', $searchData['order_id']);
262
        }
263
264
        // order_no
265 32 View Code Duplication
        if (isset($searchData['order_no']) && StringUtil::isNotBlank($searchData['order_no'])) {
266
            $qb
267 2
                ->andWhere('o.order_no = :order_no')
268 2
                ->setParameter('order_no', $searchData['order_no']);
269
        }
270
271
        // order_id_start
272 32 View Code Duplication
        if (isset($searchData['order_id_start']) && StringUtil::isNotBlank($searchData['order_id_start'])) {
273
            $qb
274 1
                ->andWhere('o.id >= :order_id_start')
275 1
                ->setParameter('order_id_start', $searchData['order_id_start']);
276
        }
277
        // multi
278 32 View Code Duplication
        if (isset($searchData['multi']) && StringUtil::isNotBlank($searchData['multi'])) {
279 5
            $multi = preg_match('/^\d{0,10}$/', $searchData['multi']) ? $searchData['multi'] : null;
280
            $qb
281 5
                ->andWhere('o.id = :multi OR o.name01 LIKE :likemulti OR o.name02 LIKE :likemulti OR '.
282
                           'o.kana01 LIKE :likemulti OR o.kana02 LIKE :likemulti OR o.company_name LIKE :likemulti OR '.
283 5
                           'o.order_no LIKE :likemulti')
284 5
                ->setParameter('multi', $multi)
285 5
                ->setParameter('likemulti', '%'.$searchData['multi'].'%');
286
        }
287
288
        // order_id_end
289 32 View Code Duplication
        if (isset($searchData['order_id_end']) && StringUtil::isNotBlank($searchData['order_id_end'])) {
290
            $qb
291 1
                ->andWhere('o.id <= :order_id_end')
292 1
                ->setParameter('order_id_end', $searchData['order_id_end']);
293
        }
294
295
        // status
296 32
        $filterStatus = false;
297 32 View Code Duplication
        if (!empty($searchData['status']) && count($searchData['status'])) {
298
            $qb
299 1
                ->andWhere($qb->expr()->in('o.OrderStatus', ':status'))
300 1
                ->setParameter('status', $searchData['status']);
301 1
            $filterStatus = true;
302
        }
303
304 32
        if (!$filterStatus) {
305
            // 購入処理中は検索対象から除外
306 31
            $OrderStatuses = $this->getEntityManager()
307 31
                ->getRepository('Eccube\Entity\Master\OrderStatus')
308 31
                ->findNotContainsBy(['id' => OrderStatus::PROCESSING]);
309 31
            $qb->andWhere($qb->expr()->in('o.OrderStatus', ':status'))
310 31
                ->setParameter('status', $OrderStatuses);
311
        }
312
313
        // company_name
314 32 View Code Duplication
        if (isset($searchData['company_name']) && StringUtil::isNotBlank($searchData['company_name'])) {
315
            $qb
316 1
                ->andWhere('o.company_name LIKE :company_name')
317 1
                ->setParameter('company_name', '%'.$searchData['company_name'].'%');
318
        }
319
320
        // name
321 32 View Code Duplication
        if (isset($searchData['name']) && StringUtil::isNotBlank($searchData['name'])) {
322
            $qb
323 1
                ->andWhere('CONCAT(o.name01, o.name02) LIKE :name')
324 1
                ->setParameter('name', '%'.$searchData['name'].'%');
325
        }
326
327
        // kana
328 32 View Code Duplication
        if (isset($searchData['kana']) && StringUtil::isNotBlank($searchData['kana'])) {
329
            $qb
330 1
                ->andWhere('CONCAT(o.kana01, o.kana02) LIKE :kana')
331 1
                ->setParameter('kana', '%'.$searchData['kana'].'%');
332
        }
333
334
        // email
335 32 View Code Duplication
        if (isset($searchData['email']) && StringUtil::isNotBlank($searchData['email'])) {
336
            $qb
337 3
                ->andWhere('o.email like :email')
338 3
                ->setParameter('email', '%'.$searchData['email'].'%');
339
        }
340
341
        // tel
342 32 View Code Duplication
        if (isset($searchData['phone_number']) && StringUtil::isNotBlank($searchData['phone_number'])) {
343 1
            $tel = preg_replace('/[^0-9]/ ', '', $searchData['phone_number']);
344
            $qb
345 1
                ->andWhere('o.phone_number LIKE :phone_number')
346 1
                ->setParameter('phone_number', '%'.$tel.'%');
347
        }
348
349
        // sex
350 32
        if (!empty($searchData['sex']) && count($searchData['sex']) > 0) {
351
            $qb
352 1
                ->andWhere($qb->expr()->in('o.Sex', ':sex'))
353 1
                ->setParameter('sex', $searchData['sex']->toArray());
354
        }
355
356
        // payment
357 32 View Code Duplication
        if (!empty($searchData['payment']) && count($searchData['payment'])) {
358
            $payments = [];
359
            foreach ($searchData['payment'] as $payment) {
360
                $payments[] = $payment->getId();
361
            }
362
            $qb
363
                ->leftJoin('o.Payment', 'p')
364
                ->andWhere($qb->expr()->in('p.id', ':payments'))
365
                ->setParameter('payments', $payments);
366
        }
367
368
        // oreder_date
369 32 View Code Duplication
        if (!empty($searchData['order_date_start']) && $searchData['order_date_start']) {
370 1
            $date = $searchData['order_date_start'];
371
            $qb
372 1
                ->andWhere('o.order_date >= :order_date_start')
373 1
                ->setParameter('order_date_start', $date);
374
        }
375 32 View Code Duplication
        if (!empty($searchData['order_date_end']) && $searchData['order_date_end']) {
376 1
            $date = clone $searchData['order_date_end'];
377
            $date = $date
378 1
                ->modify('+1 days');
379
            $qb
380 1
                ->andWhere('o.order_date < :order_date_end')
381 1
                ->setParameter('order_date_end', $date);
382
        }
383
384
        // payment_date
385 32 View Code Duplication
        if (!empty($searchData['payment_date_start']) && $searchData['payment_date_start']) {
386 1
            $date = $searchData['payment_date_start'];
387
            $qb
388 1
                ->andWhere('o.payment_date >= :payment_date_start')
389 1
                ->setParameter('payment_date_start', $date);
390
        }
391 32 View Code Duplication
        if (!empty($searchData['payment_date_end']) && $searchData['payment_date_end']) {
392 1
            $date = clone $searchData['payment_date_end'];
393
            $date = $date
394 1
                ->modify('+1 days');
395
            $qb
396 1
                ->andWhere('o.payment_date < :payment_date_end')
397 1
                ->setParameter('payment_date_end', $date);
398
        }
399
400
        // update_date
401 32 View Code Duplication
        if (!empty($searchData['update_date_start']) && $searchData['update_date_start']) {
402 1
            $date = $searchData['update_date_start'];
403
            $qb
404 1
                ->andWhere('o.update_date >= :update_date_start')
405 1
                ->setParameter('update_date_start', $date);
406
        }
407 32 View Code Duplication
        if (!empty($searchData['update_date_end']) && $searchData['update_date_end']) {
408 1
            $date = clone $searchData['update_date_end'];
409
            $date = $date
410 1
                ->modify('+1 days');
411
            $qb
412 1
                ->andWhere('o.update_date < :update_date_end')
413 1
                ->setParameter('update_date_end', $date);
414
        }
415
416
        // payment_total
417 32 View Code Duplication
        if (isset($searchData['payment_total_start']) && StringUtil::isNotBlank($searchData['payment_total_start'])) {
418
            $qb
419 1
                ->andWhere('o.payment_total >= :payment_total_start')
420 1
                ->setParameter('payment_total_start', $searchData['payment_total_start']);
421
        }
422 32 View Code Duplication
        if (isset($searchData['payment_total_end']) && StringUtil::isNotBlank($searchData['payment_total_end'])) {
423
            $qb
424 1
                ->andWhere('o.payment_total <= :payment_total_end')
425 1
                ->setParameter('payment_total_end', $searchData['payment_total_end']);
426
        }
427
428
        // buy_product_name
429 32 View Code Duplication
        if (isset($searchData['buy_product_name']) && StringUtil::isNotBlank($searchData['buy_product_name'])) {
430
            $qb
431 1
                ->leftJoin('o.OrderItems', 'oi')
432 1
                ->andWhere('oi.product_name LIKE :buy_product_name')
433 1
                ->setParameter('buy_product_name', '%'.$searchData['buy_product_name'].'%');
434
        }
435
436
        // 発送メール送信済かどうか.
437 32
        if (isset($searchData['shipping_mail_send']) && $searchData['shipping_mail_send']) {
438
            $qb
439
                ->andWhere('s.mail_send_date IS NOT NULL');
440
        }
441
442
        // 送り状番号.
443 32
        if (!empty($searchData['tracking_number'])) {
444
            $qb
445 1
                ->andWhere('s.tracking_number = :tracking_number')
446 1
                ->setParameter('tracking_number', $searchData['tracking_number']);
447
        }
448
449
        // お届け予定日(Shipping.delivery_date)
450 32 View Code Duplication
        if (!empty($searchData['shipping_delivery_date_start']) && $searchData['shipping_delivery_date_start']) {
451
            $date = $searchData['shipping_delivery_date_start'];
452
            $qb
453
                ->andWhere('s.shipping_delivery_date >= :shipping_delivery_date_start')
454
                ->setParameter('shipping_delivery_date_start', $date);
455
        }
456 32 View Code Duplication
        if (!empty($searchData['shipping_delivery_date_end']) && $searchData['shipping_delivery_date_end']) {
457
            $date = clone $searchData['shipping_delivery_date_end'];
458
            $date = $date
459
                ->modify('+1 days');
460
            $qb
461
                ->andWhere('s.shipping_delivery_date < :shipping_delivery_date_end')
462
                ->setParameter('shipping_delivery_date_end', $date);
463
        }
464
465
        // Order By
466 32
        $qb->orderBy('o.update_date', 'DESC');
467 32
        $qb->addorderBy('o.id', 'DESC');
468
469 32
        return $this->queries->customize(QueryKey::ORDER_SEARCH_ADMIN, $qb, $searchData);
470
    }
471
472
    /**
473
     * @param  \Eccube\Entity\Customer $Customer
474
     *
475
     * @return QueryBuilder
476
     */
477 2 View Code Duplication
    public function getQueryBuilderByCustomer(\Eccube\Entity\Customer $Customer)
478
    {
479 2
        $qb = $this->createQueryBuilder('o')
480 2
            ->where('o.Customer = :Customer')
481 2
            ->setParameter('Customer', $Customer);
482
483
        // Order By
484 2
        $qb->addOrderBy('o.id', 'DESC');
485
486 2
        return $this->queries->customize(QueryKey::ORDER_SEARCH_BY_CUSTOMER, $qb, ['customer' => $Customer]);
487
    }
488
489
    /**
490
     * 会員の合計購入金額を取得、回数を取得
491
     *
492
     * @param  \Eccube\Entity\Customer $Customer
493
     * @param  array $OrderStatuses
494
     *
495
     * @return array
496
     */
497 1
    public function getCustomerCount(\Eccube\Entity\Customer $Customer, array $OrderStatuses)
498
    {
499 1
        $result = $this->createQueryBuilder('o')
500 1
            ->select('COUNT(o.id) AS buy_times, SUM(o.total)  AS buy_total')
501 1
            ->where('o.Customer = :Customer')
502 1
            ->andWhere('o.OrderStatus in (:OrderStatuses)')
503 1
            ->setParameter('Customer', $Customer)
504 1
            ->setParameter('OrderStatuses', $OrderStatuses)
505 1
            ->groupBy('o.Customer')
506 1
            ->getQuery()
507 1
            ->getResult();
508
509 1
        return $result;
510
    }
511
512
    /**
513
     * 会員が保持する最新の購入処理中の Order を取得する.
514
     *
515
     * @param Customer
516
     *
517
     * @return Order
518
     */
519 2
    public function getExistsOrdersByCustomer(\Eccube\Entity\Customer $Customer)
520
    {
521 2
        $qb = $this->createQueryBuilder('o');
522
        $Order = $qb
523 2
            ->select('o')
524 2
            ->where('o.Customer = :Customer')
525 2
            ->setParameter('Customer', $Customer)
526 2
            ->orderBy('o.id', 'DESC')
527 2
            ->getQuery()
528 2
            ->setMaxResults(1)
529 2
            ->getOneOrNullResult();
530
531 2
        if ($Order && $Order->getOrderStatus()->getId() == OrderStatus::PROCESSING) {
532 1
            return $Order;
533
        }
534
535 1
        return null;
536
    }
537
538
    /**
0 ignored issues
show
Doc comment for parameter "$OrderStatusOrId" missing
Loading history...
539
     * ステータスごとの受注件数を取得する.
540
     *
541
     * @param $OrderStatusOrId
0 ignored issues
show
Missing parameter name
Loading history...
542
     *
543
     * @return int
544
     *
545
     * @throws \Doctrine\ORM\NoResultException
546
     * @throws \Doctrine\ORM\NonUniqueResultException
547
     */
548 16
    public function countByOrderStatus($OrderStatusOrId)
549
    {
550 16
        return (int) $this->createQueryBuilder('o')
551 16
            ->select('COALESCE(COUNT(o.id), 0)')
552 16
            ->where('o.OrderStatus = :OrderStatus')
553 16
            ->setParameter('OrderStatus', $OrderStatusOrId)
554 16
            ->getQuery()
555 16
            ->getSingleScalarResult();
556
    }
557
}
558