Failed Conditions
Push — experimental/sf ( c0fd88...ff71e4 )
by
unknown
269:51 queued 261:57
created

OrderRepository::updateOrderSummary()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 30
ccs 22
cts 22
cp 1
crap 2
rs 9.44
c 0
b 0
f 0
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\NoResultException;
17
use Doctrine\ORM\QueryBuilder;
18
use Eccube\Doctrine\Query\Queries;
19
use Eccube\Entity\Customer;
20
use Eccube\Entity\Master\OrderStatus;
21
use Eccube\Entity\Order;
22
use Eccube\Util\StringUtil;
23
use Symfony\Bridge\Doctrine\RegistryInterface;
24
25
/**
26
 * OrderRepository
27
 *
28
 * This class was generated by the Doctrine ORM. Add your own custom
29
 * repository methods below.
30
 */
31
class OrderRepository extends AbstractRepository
32
{
33
    /**
34
     * @var Queries
35
     */
36
    protected $queries;
37
38
    /**
39
     * OrderRepository constructor.
40
     *
41
     * @param RegistryInterface $registry
42
     * @param Queries $queries
0 ignored issues
show
introduced by
Expected 11 spaces after parameter type; 1 found
Loading history...
43
     */
44 774
    public function __construct(RegistryInterface $registry, Queries $queries)
45
    {
46 774
        parent::__construct($registry, Order::class);
47 774
        $this->queries = $queries;
48
    }
49
50
    /**
51
     * @param int $orderId
0 ignored issues
show
introduced by
Expected 9 spaces after parameter type; 1 found
Loading history...
52
     * @param OrderStatus $Status
53
     */
54 6
    public function changeStatus($orderId, \Eccube\Entity\Master\OrderStatus $Status)
55
    {
56
        $Order = $this
57 6
            ->find($orderId)
58 6
            ->setOrderStatus($Status)
59
        ;
60
61 6
        switch ($Status->getId()) {
62 6
            case '6': // 入金済へ
63 3
                $Order->setPaymentDate(new \DateTime());
64 3
                break;
65
        }
66
67 6
        $em = $this->getEntityManager();
68 6
        $em->persist($Order);
69 6
        $em->flush();
70
    }
71
72
    /**
73
     * @param array $searchData
74
     *
75
     * @return QueryBuilder
76
     */
77 17
    public function getQueryBuilderBySearchData($searchData)
78
    {
79 17
        $qb = $this->createQueryBuilder('o');
80
81 17
        $joinedCustomer = false;
82
83
        // order_id_start
84 17 View Code Duplication
        if (isset($searchData['order_id_start']) && StringUtil::isNotBlank($searchData['order_id_start'])) {
85
            $qb
86 1
                ->andWhere('o.id >= :order_id_start')
87 1
                ->setParameter('order_id_start', $searchData['order_id_start']);
88
        }
89
90
        // order_id_end
91 17 View Code Duplication
        if (isset($searchData['order_id_end']) && StringUtil::isNotBlank($searchData['order_id_end'])) {
92
            $qb
93 1
                ->andWhere('o.id <= :order_id_end')
94 1
                ->setParameter('order_id_end', $searchData['order_id_end']);
95
        }
96
97
        // status
98 17
        if (!empty($searchData['status']) && $searchData['status']) {
99
            $qb
100 1
                ->andWhere('o.OrderStatus = :status')
101 1
                ->setParameter('status', $searchData['status']);
102
        }
103
104
        // name
105 17 View Code Duplication
        if (isset($searchData['name']) && StringUtil::isNotBlank($searchData['name'])) {
106
            $qb
107 1
                ->andWhere('CONCAT(o.name01, o.name02) LIKE :name')
108 1
                ->setParameter('name', '%'.$searchData['name'].'%');
109
        }
110
111
        // kana
112 17 View Code Duplication
        if (isset($searchData['kana']) && StringUtil::isNotBlank($searchData['kana'])) {
113
            $qb
114 1
                ->andWhere('CONCAT(o.kana01, o.kana02) LIKE :kana')
115 1
                ->setParameter('kana', '%'.$searchData['kana'].'%');
116
        }
117
118
        // email
119 17 View Code Duplication
        if (isset($searchData['email']) && StringUtil::isNotBlank($searchData['email'])) {
120
            $qb
121 1
                ->andWhere('o.email = :email')
122 1
                ->setParameter('email', $searchData['email']);
123
        }
124
125
        // tel
126 17 View Code Duplication
        if (isset($searchData['phone_number']) && StringUtil::isNotBlank($searchData['phone_number'])) {
127
            $qb
128 1
                ->andWhere('o.phone_number = :phone_number')
129 1
                ->setParameter('phone_number', $searchData['phone_number']);
130
        }
131
132
        // birth
133 17 View Code Duplication
        if (!empty($searchData['birth_start']) && $searchData['birth_start']) {
134 1
            if (!$joinedCustomer) {
135 1
                $qb->leftJoin('o.Customer', 'c');
136 1
                $joinedCustomer = true;
137
            }
138
139 1
            $date = $searchData['birth_start'];
140
            $qb
141 1
                ->andWhere('c.birth >= :birth_start')
142 1
                ->setParameter('birth_start', $date);
143
        }
144 17 View Code Duplication
        if (!empty($searchData['birth_end']) && $searchData['birth_end']) {
145 1
            if (!$joinedCustomer) {
146 1
                $qb->leftJoin('o.Customer', 'c');
147 1
                $joinedCustomer = true;
148
            }
149
150 1
            $date = clone $searchData['birth_end'];
151
            $date = $date
152 1
                ->modify('+1 days');
153
            $qb
154 1
                ->andWhere('c.birth < :birth_end')
155 1
                ->setParameter('birth_end', $date);
156
        }
157
158
        // sex
159 17
        if (!empty($searchData['sex']) && count($searchData['sex']) > 0) {
160 1
            if (!$joinedCustomer) {
161 1
                $qb->leftJoin('o.Customer', 'c');
162 1
                $joinedCustomer = true;
0 ignored issues
show
Unused Code introduced by
$joinedCustomer is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
163
            }
164
165 1
            $sexs = [];
166 1
            foreach ($searchData['sex'] as $sex) {
167 1
                $sexs[] = $sex->getId();
168
            }
169
170
            $qb
171 1
                ->andWhere($qb->expr()->in('c.Sex', ':sexs'))
172 1
                ->setParameter('sexs', $sexs);
173
        }
174
175
        // payment
176 17 View Code Duplication
        if (!empty($searchData['payment']) && count($searchData['payment'])) {
177
            $payments = [];
178
            foreach ($searchData['payment'] as $payment) {
179
                $payments[] = $payment->getId();
180
            }
181
            $qb
182
                ->leftJoin('o.Payment', 'p')
183
                ->andWhere($qb->expr()->in('p.id', ':payments'))
184
                ->setParameter('payments', $payments);
185
        }
186
187
        // oreder_date
188 17 View Code Duplication
        if (!empty($searchData['order_date_start']) && $searchData['order_date_start']) {
189 1
            $date = $searchData['order_date_start'];
190
            $qb
191 1
                ->andWhere('o.create_date >= :order_date_start')
192 1
                ->setParameter('order_date_start', $date);
193
        }
194 17 View Code Duplication
        if (!empty($searchData['order_date_end']) && $searchData['order_date_end']) {
195 1
            $date = clone $searchData['order_date_end'];
196
            $date = $date
197 1
                ->modify('+1 days');
198
            $qb
199 1
                ->andWhere('o.create_date < :order_date_end')
200 1
                ->setParameter('order_date_end', $date);
201
        }
202
203
        // create_date
204 17 View Code Duplication
        if (!empty($searchData['update_date_start']) && $searchData['update_date_start']) {
205 1
            $date = $searchData['update_date_start'];
206
            $qb
207 1
                ->andWhere('o.update_date >= :update_date_start')
208 1
                ->setParameter('update_date_start', $date);
209
        }
210 17 View Code Duplication
        if (!empty($searchData['update_date_end']) && $searchData['update_date_end']) {
211 1
            $date = clone $searchData['update_date_end'];
212
            $date = $date
213 1
                ->modify('+1 days');
214
            $qb
215 1
                ->andWhere('o.update_date < :update_date_end')
216 1
                ->setParameter('update_date_end', $date);
217
        }
218
219
        // payment_total
220 17 View Code Duplication
        if (isset($searchData['payment_total_start']) && StringUtil::isNotBlank($searchData['payment_total_start'])) {
221
            $qb
222 1
                ->andWhere('o.payment_total >= :payment_total_start')
223 1
                ->setParameter('payment_total_start', $searchData['payment_total_start']);
224
        }
225 17 View Code Duplication
        if (isset($searchData['payment_total_end']) && StringUtil::isNotBlank($searchData['payment_total_end'])) {
226
            $qb
227 1
                ->andWhere('o.payment_total <= :payment_total_end')
228 1
                ->setParameter('payment_total_end', $searchData['payment_total_end']);
229
        }
230
231
        // buy_product_name
232 17 View Code Duplication
        if (isset($searchData['buy_product_name']) && StringUtil::isNotBlank($searchData['buy_product_name'])) {
233
            $qb
234 1
                ->leftJoin('o.OrderItems', 'oi')
235 1
                ->andWhere('oi.product_name LIKE :buy_product_name')
236 1
                ->setParameter('buy_product_name', '%'.$searchData['buy_product_name'].'%');
237
        }
238
239
        // Order By
240 17
        $qb->addOrderBy('o.update_date', 'DESC');
241
242 17
        return $this->queries->customize(QueryKey::ORDER_SEARCH, $qb, $searchData);
243
    }
244
245
    /**
246
     * @param  array        $searchData
0 ignored issues
show
introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
247
     *
248
     * @return QueryBuilder
249
     */
250 32
    public function getQueryBuilderBySearchDataForAdmin($searchData)
251
    {
252 32
        $qb = $this->createQueryBuilder('o')
253 32
            ->select('o, s')
254 32
            ->innerJoin('o.Shippings', 's');
255
256
        // order_id_start
257 32 View Code Duplication
        if (isset($searchData['order_id']) && StringUtil::isNotBlank($searchData['order_id'])) {
258
            $qb
259
                ->andWhere('o.id = :order_id')
260
                ->setParameter('order_id', $searchData['order_id']);
261
        }
262
263
        // order_no
264 32 View Code Duplication
        if (isset($searchData['order_no']) && StringUtil::isNotBlank($searchData['order_no'])) {
265
            $qb
266 2
                ->andWhere('o.order_no = :order_no')
267 2
                ->setParameter('order_no', $searchData['order_no']);
268
        }
269
270
        // order_id_start
271 32 View Code Duplication
        if (isset($searchData['order_id_start']) && StringUtil::isNotBlank($searchData['order_id_start'])) {
272
            $qb
273 1
                ->andWhere('o.id >= :order_id_start')
274 1
                ->setParameter('order_id_start', $searchData['order_id_start']);
275
        }
276
        // multi
277 32 View Code Duplication
        if (isset($searchData['multi']) && StringUtil::isNotBlank($searchData['multi'])) {
278 5
            $multi = preg_match('/^\d{0,10}$/', $searchData['multi']) ? $searchData['multi'] : null;
279
            $qb
280 5
                ->andWhere('o.id = :multi OR o.name01 LIKE :likemulti OR o.name02 LIKE :likemulti OR '.
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
281
                           'o.kana01 LIKE :likemulti OR o.kana02 LIKE :likemulti OR o.company_name LIKE :likemulti OR '.
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
282 5
                           'o.order_no LIKE :likemulti')
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 16 spaces, but found 27.
Loading history...
283 5
                ->setParameter('multi', $multi)
284 5
                ->setParameter('likemulti', '%'.$searchData['multi'].'%');
285
        }
286
287
        // order_id_end
288 32 View Code Duplication
        if (isset($searchData['order_id_end']) && StringUtil::isNotBlank($searchData['order_id_end'])) {
289
            $qb
290 1
                ->andWhere('o.id <= :order_id_end')
291 1
                ->setParameter('order_id_end', $searchData['order_id_end']);
292
        }
293
294
        // status
295 32
        $filterStatus = false;
296 32 View Code Duplication
        if (!empty($searchData['status']) && count($searchData['status'])) {
297
            $qb
298 1
                ->andWhere($qb->expr()->in('o.OrderStatus', ':status'))
299 1
                ->setParameter('status', $searchData['status']);
300 1
            $filterStatus = true;
301
        }
302
303 32
        if (!$filterStatus) {
304
            // 購入処理中は検索対象から除外
305 31
            $OrderStatuses = $this->getEntityManager()
306 31
                ->getRepository('Eccube\Entity\Master\OrderStatus')
307 31
                ->findNotContainsBy(['id' => OrderStatus::PROCESSING]);
308 31
            $qb->andWhere($qb->expr()->in('o.OrderStatus', ':status'))
309 31
                ->setParameter('status', $OrderStatuses);
310
        }
311
312
        // company_name
313 32 View Code Duplication
        if (isset($searchData['company_name']) && StringUtil::isNotBlank($searchData['company_name'])) {
314
            $qb
315 1
                ->andWhere('o.company_name LIKE :company_name')
316 1
                ->setParameter('company_name', '%'.$searchData['company_name'].'%');
317
        }
318
319
        // name
320 32 View Code Duplication
        if (isset($searchData['name']) && StringUtil::isNotBlank($searchData['name'])) {
321
            $qb
322 1
                ->andWhere('CONCAT(o.name01, o.name02) LIKE :name')
323 1
                ->setParameter('name', '%'.$searchData['name'].'%');
324
        }
325
326
        // kana
327 32 View Code Duplication
        if (isset($searchData['kana']) && StringUtil::isNotBlank($searchData['kana'])) {
328
            $qb
329 1
                ->andWhere('CONCAT(o.kana01, o.kana02) LIKE :kana')
330 1
                ->setParameter('kana', '%'.$searchData['kana'].'%');
331
        }
332
333
        // email
334 32 View Code Duplication
        if (isset($searchData['email']) && StringUtil::isNotBlank($searchData['email'])) {
335
            $qb
336 3
                ->andWhere('o.email like :email')
337 3
                ->setParameter('email', '%'.$searchData['email'].'%');
338
        }
339
340
        // tel
341 32 View Code Duplication
        if (isset($searchData['phone_number']) && StringUtil::isNotBlank($searchData['phone_number'])) {
342 1
            $tel = preg_replace('/[^0-9]/ ', '', $searchData['phone_number']);
343
            $qb
344 1
                ->andWhere('o.phone_number LIKE :phone_number')
345 1
                ->setParameter('phone_number', '%'.$tel.'%');
346
        }
347
348
        // sex
349 32
        if (!empty($searchData['sex']) && count($searchData['sex']) > 0) {
350
            $qb
351 1
                ->andWhere($qb->expr()->in('o.Sex', ':sex'))
352 1
                ->setParameter('sex', $searchData['sex']->toArray());
353
        }
354
355
        // payment
356 32 View Code Duplication
        if (!empty($searchData['payment']) && count($searchData['payment'])) {
357
            $payments = [];
358
            foreach ($searchData['payment'] as $payment) {
359
                $payments[] = $payment->getId();
360
            }
361
            $qb
362
                ->leftJoin('o.Payment', 'p')
363
                ->andWhere($qb->expr()->in('p.id', ':payments'))
364
                ->setParameter('payments', $payments);
365
        }
366
367
        // oreder_date
368 32 View Code Duplication
        if (!empty($searchData['order_date_start']) && $searchData['order_date_start']) {
369 1
            $date = $searchData['order_date_start'];
370
            $qb
371 1
                ->andWhere('o.order_date >= :order_date_start')
372 1
                ->setParameter('order_date_start', $date);
373
        }
374 32 View Code Duplication
        if (!empty($searchData['order_date_end']) && $searchData['order_date_end']) {
375 1
            $date = clone $searchData['order_date_end'];
376
            $date = $date
377 1
                ->modify('+1 days');
378
            $qb
379 1
                ->andWhere('o.order_date < :order_date_end')
380 1
                ->setParameter('order_date_end', $date);
381
        }
382
383
        // payment_date
384 32 View Code Duplication
        if (!empty($searchData['payment_date_start']) && $searchData['payment_date_start']) {
385 1
            $date = $searchData['payment_date_start'];
386
            $qb
387 1
                ->andWhere('o.payment_date >= :payment_date_start')
388 1
                ->setParameter('payment_date_start', $date);
389
        }
390 32 View Code Duplication
        if (!empty($searchData['payment_date_end']) && $searchData['payment_date_end']) {
391 1
            $date = clone $searchData['payment_date_end'];
392
            $date = $date
393 1
                ->modify('+1 days');
394
            $qb
395 1
                ->andWhere('o.payment_date < :payment_date_end')
396 1
                ->setParameter('payment_date_end', $date);
397
        }
398
399
        // update_date
400 32 View Code Duplication
        if (!empty($searchData['update_date_start']) && $searchData['update_date_start']) {
401 1
            $date = $searchData['update_date_start'];
402
            $qb
403 1
                ->andWhere('o.update_date >= :update_date_start')
404 1
                ->setParameter('update_date_start', $date);
405
        }
406 32 View Code Duplication
        if (!empty($searchData['update_date_end']) && $searchData['update_date_end']) {
407 1
            $date = clone $searchData['update_date_end'];
408
            $date = $date
409 1
                ->modify('+1 days');
410
            $qb
411 1
                ->andWhere('o.update_date < :update_date_end')
412 1
                ->setParameter('update_date_end', $date);
413
        }
414
415
        // payment_total
416 32 View Code Duplication
        if (isset($searchData['payment_total_start']) && StringUtil::isNotBlank($searchData['payment_total_start'])) {
417
            $qb
418 1
                ->andWhere('o.payment_total >= :payment_total_start')
419 1
                ->setParameter('payment_total_start', $searchData['payment_total_start']);
420
        }
421 32 View Code Duplication
        if (isset($searchData['payment_total_end']) && StringUtil::isNotBlank($searchData['payment_total_end'])) {
422
            $qb
423 1
                ->andWhere('o.payment_total <= :payment_total_end')
424 1
                ->setParameter('payment_total_end', $searchData['payment_total_end']);
425
        }
426
427
        // buy_product_name
428 32 View Code Duplication
        if (isset($searchData['buy_product_name']) && StringUtil::isNotBlank($searchData['buy_product_name'])) {
429
            $qb
430 1
                ->leftJoin('o.OrderItems', 'oi')
431 1
                ->andWhere('oi.product_name LIKE :buy_product_name')
432 1
                ->setParameter('buy_product_name', '%'.$searchData['buy_product_name'].'%');
433
        }
434
435
        // 発送メール送信済かどうか.
436 32
        if (isset($searchData['shipping_mail_send']) && $searchData['shipping_mail_send']) {
437
            $qb
438
                ->andWhere('s.mail_send_date IS NOT NULL');
439
        }
440
441
        // 送り状番号.
442 32
        if (!empty($searchData['tracking_number'])) {
443
            $qb
444 1
                ->andWhere('s.tracking_number = :tracking_number')
445 1
                ->setParameter('tracking_number', $searchData['tracking_number']);
446
        }
447
448
        // お届け予定日(Shipping.delivery_date)
449 32 View Code Duplication
        if (!empty($searchData['shipping_delivery_date_start']) && $searchData['shipping_delivery_date_start']) {
450
            $date = $searchData['shipping_delivery_date_start'];
451
            $qb
452
                ->andWhere('s.shipping_delivery_date >= :shipping_delivery_date_start')
453
                ->setParameter('shipping_delivery_date_start', $date);
454
        }
455 32 View Code Duplication
        if (!empty($searchData['shipping_delivery_date_end']) && $searchData['shipping_delivery_date_end']) {
456
            $date = clone $searchData['shipping_delivery_date_end'];
457
            $date = $date
458
                ->modify('+1 days');
459
            $qb
460
                ->andWhere('s.shipping_delivery_date < :shipping_delivery_date_end')
461
                ->setParameter('shipping_delivery_date_end', $date);
462
        }
463
464
        // Order By
465 32
        $qb->orderBy('o.update_date', 'DESC');
466 32
        $qb->addorderBy('o.id', 'DESC');
467
468 32
        return $this->queries->customize(QueryKey::ORDER_SEARCH_ADMIN, $qb, $searchData);
469
    }
470
471
    /**
472
     * @param  \Eccube\Entity\Customer $Customer
473
     *
474
     * @return QueryBuilder
475
     */
476 2 View Code Duplication
    public function getQueryBuilderByCustomer(\Eccube\Entity\Customer $Customer)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
477
    {
478 2
        $qb = $this->createQueryBuilder('o')
479 2
            ->where('o.Customer = :Customer')
480 2
            ->setParameter('Customer', $Customer);
481
482
        // Order By
483 2
        $qb->addOrderBy('o.id', 'DESC');
484
485 2
        return $this->queries->customize(QueryKey::ORDER_SEARCH_BY_CUSTOMER, $qb, ['customer' => $Customer]);
486
    }
487
488
    /**
489
     * 会員の合計購入金額を取得、回数を取得
490
     *
491
     * @param  \Eccube\Entity\Customer $Customer
492
     * @param  array $OrderStatuses
0 ignored issues
show
introduced by
Expected 19 spaces after parameter type; 1 found
Loading history...
493
     *
494
     * @return array
495
     */
496 1
    public function getCustomerCount(\Eccube\Entity\Customer $Customer, array $OrderStatuses)
497
    {
498 1
        $result = $this->createQueryBuilder('o')
499 1
            ->select('COUNT(o.id) AS buy_times, SUM(o.total) AS buy_total, MAX(o.id) AS order_id')
500 1
            ->where('o.Customer = :Customer')
501 1
            ->andWhere('o.OrderStatus in (:OrderStatuses)')
502 1
            ->setParameter('Customer', $Customer)
503 1
            ->setParameter('OrderStatuses', $OrderStatuses)
504 1
            ->groupBy('o.Customer')
505 1
            ->getQuery()
506 1
            ->getResult();
507
508 1
        return $result;
509
    }
510
511
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
512
     * 会員が保持する最新の購入処理中の Order を取得する.
513
     *
514
     * @param Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
515
     *
516
     * @return Order
517
     */
518 2
    public function getExistsOrdersByCustomer(\Eccube\Entity\Customer $Customer)
519
    {
520 2
        $qb = $this->createQueryBuilder('o');
521
        $Order = $qb
522 2
            ->select('o')
523 2
            ->where('o.Customer = :Customer')
524 2
            ->setParameter('Customer', $Customer)
525 2
            ->orderBy('o.id', 'DESC')
526 2
            ->getQuery()
527 2
            ->setMaxResults(1)
528 2
            ->getOneOrNullResult();
529
530 2
        if ($Order && $Order->getOrderStatus()->getId() == OrderStatus::PROCESSING) {
531 1
            return $Order;
532
        }
533
534 1
        return null;
535
    }
536
537
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$OrderStatusOrId" missing
Loading history...
538
     * ステータスごとの受注件数を取得する.
539
     *
540
     * @param $OrderStatusOrId
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
541
     *
542
     * @return int
543
     *
544
     * @throws \Doctrine\ORM\NoResultException
545
     * @throws \Doctrine\ORM\NonUniqueResultException
546
     */
547 16
    public function countByOrderStatus($OrderStatusOrId)
548
    {
549 16
        return (int) $this->createQueryBuilder('o')
550 16
            ->select('COALESCE(COUNT(o.id), 0)')
551 16
            ->where('o.OrderStatus = :OrderStatus')
552 16
            ->setParameter('OrderStatus', $OrderStatusOrId)
553 16
            ->getQuery()
554 16
            ->getSingleScalarResult();
555
    }
556
557
    /**
558
     * 会員の購入金額, 購入回数, 初回購入日, 最終購入費を更新する
559
     *
560
     * @param Customer $Customer
561
     * @param array $OrderStatuses
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
562
     */
563 6
    public function updateOrderSummary(Customer $Customer, array $OrderStatuses = [OrderStatus::NEW, OrderStatus::PAID, OrderStatus::DELIVERED, OrderStatus::IN_PROGRESS])
564
    {
565
        try {
566 6
            $result = $this->createQueryBuilder('o')
567 6
                ->select('COUNT(o.id) AS buy_times, SUM(o.total) AS buy_total, MIN(o.id) AS first_order_id, MAX(o.id) AS last_order_id')
568 6
                ->where('o.Customer = :Customer')
569 6
                ->andWhere('o.OrderStatus in (:OrderStatuses)')
570 6
                ->setParameter('Customer', $Customer)
571 6
                ->setParameter('OrderStatuses', $OrderStatuses)
572 6
                ->groupBy('o.Customer')
573 6
                ->getQuery()
574 6
                ->getSingleResult();
575 1
        } catch (NoResultException $e) {
576
            // 受注データが存在しなければ初期化
577 1
            $Customer->setFirstBuyDate(null);
578 1
            $Customer->setLastBuyDate(null);
579 1
            $Customer->setBuyTimes(0);
580 1
            $Customer->setBuyTotal(0);
581
582 1
            return;
583
        }
584
585 6
        $FirstOrder = $this->find(['id' => $result['first_order_id']]);
586 6
        $LastOrder = $this->find(['id' => $result['last_order_id']]);
587
588 6
        $Customer->setBuyTimes($result['buy_times']);
589 6
        $Customer->setBuyTotal($result['buy_total']);
590 6
        $Customer->setFirstBuyDate($FirstOrder->getOrderDate());
591 6
        $Customer->setLastBuyDate($LastOrder->getOrderDate());
592
    }
593
}
594