Completed
Pull Request — 4.0 (#3658)
by Kentaro
06:26
created

getQueryBuilderBySearchDataForAdmin()   F

Complexity

Conditions 58
Paths > 20000

Size

Total Lines 233

Duplication

Lines 142
Ratio 60.94 %

Code Coverage

Tests 109
CRAP Score 59.0765

Importance

Changes 0
Metric Value
cc 58
nc 503316480
nop 1
dl 142
loc 233
ccs 109
cts 117
cp 0.9316
crap 59.0765
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Entity\Shipping;
23
use Eccube\Util\StringUtil;
24
use Symfony\Bridge\Doctrine\RegistryInterface;
25
26
/**
27
 * OrderRepository
28
 *
29
 * This class was generated by the Doctrine ORM. Add your own custom
30
 * repository methods below.
31
 */
32
class OrderRepository extends AbstractRepository
33
{
34
    /**
35
     * @var Queries
36
     */
37
    protected $queries;
38
39
    /**
40
     * OrderRepository constructor.
41
     *
42
     * @param RegistryInterface $registry
43
     * @param Queries $queries
44 832
     */
45
    public function __construct(RegistryInterface $registry, Queries $queries)
46 832
    {
47 832
        parent::__construct($registry, Order::class);
48
        $this->queries = $queries;
49
    }
50
51
    /**
52
     * @param int $orderId
53
     * @param OrderStatus $Status
54 19
     */
55
    public function changeStatus($orderId, \Eccube\Entity\Master\OrderStatus $Status)
56
    {
57 19
        $Order = $this
58 19
            ->find($orderId)
59
            ->setOrderStatus($Status)
60
        ;
61 19
62 19
        switch ($Status->getId()) {
63 3
            case '6': // 入金済へ
64 3
                $Order->setPaymentDate(new \DateTime());
65
                break;
66
        }
67 19
68 19
        $em = $this->getEntityManager();
69 19
        $em->persist($Order);
70
        $em->flush();
71
    }
72
73
    /**
74
     * @param  array        $searchData
75
     *
76
     * @return QueryBuilder
77 17
     */
78
    public function getQueryBuilderBySearchDataForAdmin($searchData)
79 17
    {
80
        $qb = $this->createQueryBuilder('o')
81 17
            ->select('o, s')
82
            ->addSelect('oi', 'p')
83
            ->leftJoin('o.OrderItems', 'oi')
84 17
            ->leftJoin('o.Pref', 'p')
85
            ->innerJoin('o.Shippings', 's');
86 1
87 1
        // order_id_start
88 View Code Duplication
        if (isset($searchData['order_id']) && StringUtil::isNotBlank($searchData['order_id'])) {
89
            $qb
90
                ->andWhere('o.id = :order_id')
91 17
                ->setParameter('order_id', $searchData['order_id']);
92
        }
93 1
94 1
        // order_no
95 View Code Duplication
        if (isset($searchData['order_no']) && StringUtil::isNotBlank($searchData['order_no'])) {
96
            $qb
97
                ->andWhere('o.order_no = :order_no')
98 17
                ->setParameter('order_no', $searchData['order_no']);
99
        }
100 1
101 1
        // order_id_start
102 View Code Duplication
        if (isset($searchData['order_id_start']) && StringUtil::isNotBlank($searchData['order_id_start'])) {
103
            $qb
104
                ->andWhere('o.id >= :order_id_start')
105 17
                ->setParameter('order_id_start', $searchData['order_id_start']);
106
        }
107 1
        // multi
108 1 View Code Duplication
        if (isset($searchData['multi']) && StringUtil::isNotBlank($searchData['multi'])) {
109
            $multi = preg_match('/^\d{0,10}$/', $searchData['multi']) ? $searchData['multi'] : null;
110
            $qb
111
                ->andWhere('o.id = :multi OR o.name01 LIKE :likemulti OR o.name02 LIKE :likemulti OR '.
112 17
                            'o.kana01 LIKE :likemulti OR o.kana02 LIKE :likemulti OR o.company_name LIKE :likemulti OR '.
113
                            'o.order_no LIKE :likemulti')
114 1
                ->setParameter('multi', $multi)
115 1
                ->setParameter('likemulti', '%'.$searchData['multi'].'%');
116
        }
117
118
        // order_id_end
119 17 View Code Duplication
        if (isset($searchData['order_id_end']) && StringUtil::isNotBlank($searchData['order_id_end'])) {
120
            $qb
121 1
                ->andWhere('o.id <= :order_id_end')
122 1
                ->setParameter('order_id_end', $searchData['order_id_end']);
123
        }
124
125
        // status
126 17
        $filterStatus = false;
127 View Code Duplication
        if (!empty($searchData['status']) && count($searchData['status'])) {
128 1
            $qb
129 1
                ->andWhere($qb->expr()->in('o.OrderStatus', ':status'))
130
                ->setParameter('status', $searchData['status']);
131
            $filterStatus = true;
132
        }
133 17
134 1
        if (!$filterStatus) {
135 1
            // 購入処理中は検索対象から除外
136 1
            $OrderStatuses = $this->getEntityManager()
137
                ->getRepository('Eccube\Entity\Master\OrderStatus')
138
                ->findNotContainsBy(['id' => OrderStatus::PROCESSING]);
139 1
            $qb->andWhere($qb->expr()->in('o.OrderStatus', ':status'))
140
                ->setParameter('status', $OrderStatuses);
141 1
        }
142 1
143
        // company_name
144 17 View Code Duplication
        if (isset($searchData['company_name']) && StringUtil::isNotBlank($searchData['company_name'])) {
145 1
            $qb
146 1
                ->andWhere('o.company_name LIKE :company_name')
147 1
                ->setParameter('company_name', '%'.$searchData['company_name'].'%');
148
        }
149
150 1
        // name
151 View Code Duplication
        if (isset($searchData['name']) && StringUtil::isNotBlank($searchData['name'])) {
152 1
            $qb
153
                ->andWhere('CONCAT(o.name01, o.name02) LIKE :name')
154 1
                ->setParameter('name', '%'.$searchData['name'].'%');
155 1
        }
156
157
        // kana
158 View Code Duplication
        if (isset($searchData['kana']) && StringUtil::isNotBlank($searchData['kana'])) {
159 17
            $qb
160 1
                ->andWhere('CONCAT(o.kana01, o.kana02) LIKE :kana')
161 1
                ->setParameter('kana', '%'.$searchData['kana'].'%');
162 1
        }
163
164
        // email
165 1 View Code Duplication
        if (isset($searchData['email']) && StringUtil::isNotBlank($searchData['email'])) {
166 1
            $qb
167 1
                ->andWhere('o.email like :email')
168
                ->setParameter('email', '%'.$searchData['email'].'%');
169
        }
170
171 1
        // tel
172 1 View Code Duplication
        if (isset($searchData['phone_number']) && StringUtil::isNotBlank($searchData['phone_number'])) {
173
            $tel = preg_replace('/[^0-9]/ ', '', $searchData['phone_number']);
174
            $qb
175
                ->andWhere('o.phone_number LIKE :phone_number')
176 17
                ->setParameter('phone_number', '%'.$tel.'%');
177
        }
178
179
        // sex
180
        if (!empty($searchData['sex']) && count($searchData['sex']) > 0) {
181
            $qb
182
                ->andWhere($qb->expr()->in('o.Sex', ':sex'))
183
                ->setParameter('sex', $searchData['sex']->toArray());
184
        }
185
186
        // payment
187 View Code Duplication
        if (!empty($searchData['payment']) && count($searchData['payment'])) {
188 17
            $payments = [];
189 1
            foreach ($searchData['payment'] as $payment) {
190
                $payments[] = $payment->getId();
191 1
            }
192 1
            $qb
193
                ->leftJoin('o.Payment', 'p')
194 17
                ->andWhere($qb->expr()->in('p.id', ':payments'))
195 1
                ->setParameter('payments', $payments);
196
        }
197 1
198
        // oreder_date
199 1 View Code Duplication
        if (!empty($searchData['order_date_start']) && $searchData['order_date_start']) {
200 1
            $date = $searchData['order_date_start'];
201
            $qb
202
                ->andWhere('o.order_date >= :order_date_start')
203
                ->setParameter('order_date_start', $date);
204 17
        }
205 1 View Code Duplication
        if (!empty($searchData['order_date_end']) && $searchData['order_date_end']) {
206
            $date = clone $searchData['order_date_end'];
207 1
            $date = $date
208 1
                ->modify('+1 days');
209
            $qb
210 17
                ->andWhere('o.order_date < :order_date_end')
211 1
                ->setParameter('order_date_end', $date);
212
        }
213 1
214
        // payment_date
215 1 View Code Duplication
        if (!empty($searchData['payment_date_start']) && $searchData['payment_date_start']) {
216 1
            $date = $searchData['payment_date_start'];
217
            $qb
218
                ->andWhere('o.payment_date >= :payment_date_start')
219
                ->setParameter('payment_date_start', $date);
220 17
        }
221 View Code Duplication
        if (!empty($searchData['payment_date_end']) && $searchData['payment_date_end']) {
222 1
            $date = clone $searchData['payment_date_end'];
223 1
            $date = $date
224
                ->modify('+1 days');
225 17
            $qb
226
                ->andWhere('o.payment_date < :payment_date_end')
227 1
                ->setParameter('payment_date_end', $date);
228 1
        }
229
230
        // update_date
231 View Code Duplication
        if (!empty($searchData['update_date_start']) && $searchData['update_date_start']) {
232 17
            $date = $searchData['update_date_start'];
233
            $qb
234 1
                ->andWhere('o.update_date >= :update_date_start')
235 1
                ->setParameter('update_date_start', $date);
236 1
        }
237 View Code Duplication
        if (!empty($searchData['update_date_end']) && $searchData['update_date_end']) {
238
            $date = clone $searchData['update_date_end'];
239
            $date = $date
240 17
                ->modify('+1 days');
241
            $qb
242 17
                ->andWhere('o.update_date < :update_date_end')
243
                ->setParameter('update_date_end', $date);
244
        }
245
246
        // payment_total
247 View Code Duplication
        if (isset($searchData['payment_total_start']) && StringUtil::isNotBlank($searchData['payment_total_start'])) {
248
            $qb
249
                ->andWhere('o.payment_total >= :payment_total_start')
250 34
                ->setParameter('payment_total_start', $searchData['payment_total_start']);
251
        }
252 34 View Code Duplication
        if (isset($searchData['payment_total_end']) && StringUtil::isNotBlank($searchData['payment_total_end'])) {
253 34
            $qb
254 34
                ->andWhere('o.payment_total <= :payment_total_end')
255
                ->setParameter('payment_total_end', $searchData['payment_total_end']);
256
        }
257 34
258
        // buy_product_name
259 View Code Duplication
        if (isset($searchData['buy_product_name']) && StringUtil::isNotBlank($searchData['buy_product_name'])) {
260
            $qb
261
                ->andWhere('oi.product_name LIKE :buy_product_name')
262
                ->setParameter('buy_product_name', '%'.$searchData['buy_product_name'].'%');
263
        }
264 34
265
        // 発送メール送信/未送信.
266 2
        if (isset($searchData['shipping_mail']) && $count = count($searchData['shipping_mail'])) {
267 2
            // 送信済/未送信両方にチェックされている場合は検索条件に追加しない
268
            if ($count < 2) {
269
                $checked = current($searchData['shipping_mail']);
270
                if ($checked == Shipping::SHIPPING_MAIL_UNSENT) {
271 34
                    // 未送信
272
                    $qb
273 1
                        ->andWhere('s.mail_send_date IS NULL');
274 1
                } elseif ($checked == Shipping::SHIPPING_MAIL_SENT) {
275
                    // 送信
276
                    $qb
277 34
                        ->andWhere('s.mail_send_date IS NOT NULL');
278 5
                }
279
            }
280 5
        }
281
282 5
        // 送り状番号.
283 5
        if (!empty($searchData['tracking_number'])) {
284 5
            $qb
285
                ->andWhere('s.tracking_number = :tracking_number')
286
                ->setParameter('tracking_number', $searchData['tracking_number']);
287
        }
288 34
289
        // お届け予定日(Shipping.delivery_date)
290 1 View Code Duplication
        if (!empty($searchData['shipping_delivery_date_start']) && $searchData['shipping_delivery_date_start']) {
291 1
            $date = $searchData['shipping_delivery_date_start'];
292
            $qb
293
                ->andWhere('s.shipping_delivery_date >= :shipping_delivery_date_start')
294
                ->setParameter('shipping_delivery_date_start', $date);
295 34
        }
296 34 View Code Duplication
        if (!empty($searchData['shipping_delivery_date_end']) && $searchData['shipping_delivery_date_end']) {
297
            $date = clone $searchData['shipping_delivery_date_end'];
298 1
            $date = $date
299 1
                ->modify('+1 days');
300 1
            $qb
301
                ->andWhere('s.shipping_delivery_date < :shipping_delivery_date_end')
302
                ->setParameter('shipping_delivery_date_end', $date);
303 34
        }
304
305 33
        // Order By
306 33
        $qb->orderBy('o.update_date', 'DESC');
307 33
        $qb->addorderBy('o.id', 'DESC');
308 33
309 33
        return $this->queries->customize(QueryKey::ORDER_SEARCH_ADMIN, $qb, $searchData);
310
    }
311
312
    /**
313 34
     * @param  \Eccube\Entity\Customer $Customer
314
     *
315 1
     * @return QueryBuilder
316 1
     */
317
    public function getQueryBuilderByCustomer(\Eccube\Entity\Customer $Customer)
318
    {
319
        $qb = $this->createQueryBuilder('o')
320 34
            ->where('o.Customer = :Customer')
321
            ->setParameter('Customer', $Customer);
322 1
323 1
        // Order By
324
        $qb->addOrderBy('o.id', 'DESC');
325
326
        return $this->queries->customize(QueryKey::ORDER_SEARCH_BY_CUSTOMER, $qb, ['customer' => $Customer]);
327 34
    }
328
329 1
    /**
330 1
     * 会員の合計購入金額を取得、回数を取得
331
     *
332
     * @param  \Eccube\Entity\Customer $Customer
333
     * @param  array $OrderStatuses
334 34
     *
335
     * @return array
336 3
     */
337 3
    public function getCustomerCount(\Eccube\Entity\Customer $Customer, array $OrderStatuses)
338
    {
339
        $result = $this->createQueryBuilder('o')
340
            ->select('COUNT(o.id) AS buy_times, SUM(o.total) AS buy_total, MAX(o.id) AS order_id')
341 34
            ->where('o.Customer = :Customer')
342 1
            ->andWhere('o.OrderStatus in (:OrderStatuses)')
343
            ->setParameter('Customer', $Customer)
344 1
            ->setParameter('OrderStatuses', $OrderStatuses)
345 1
            ->groupBy('o.Customer')
346
            ->getQuery()
347
            ->getResult();
348
349 34
        return $result;
350
    }
351 1
352 1
    /**
353
     * 会員が保持する最新の購入処理中の Order を取得する.
354
     *
355
     * @param Customer
356 34
     *
357
     * @return Order
358
     */
359
    public function getExistsOrdersByCustomer(\Eccube\Entity\Customer $Customer)
360
    {
361
        $qb = $this->createQueryBuilder('o');
362
        $Order = $qb
363
            ->select('o')
364
            ->where('o.Customer = :Customer')
365
            ->setParameter('Customer', $Customer)
366
            ->orderBy('o.id', 'DESC')
367
            ->getQuery()
368 34
            ->setMaxResults(1)
369 1
            ->getOneOrNullResult();
370
371 1
        if ($Order && $Order->getOrderStatus()->getId() == OrderStatus::PROCESSING) {
372 1
            return $Order;
373
        }
374 34
375 1
        return null;
376
    }
377 1
378
    /**
379 1
     * ステータスごとの受注件数を取得する.
380 1
     *
381
     * @param integer $OrderStatusOrId
382
     *
383
     * @return int
384 34
     *
385 1
     * @throws \Doctrine\ORM\NoResultException
386
     * @throws \Doctrine\ORM\NonUniqueResultException
387 1
     */
388 1
    public function countByOrderStatus($OrderStatusOrId)
389
    {
390 34
        return (int) $this->createQueryBuilder('o')
391 1
            ->select('COALESCE(COUNT(o.id), 0)')
392
            ->where('o.OrderStatus = :OrderStatus')
393 1
            ->setParameter('OrderStatus', $OrderStatusOrId)
394
            ->getQuery()
395 1
            ->getSingleScalarResult();
396 1
    }
397
398
    /**
399
     * 会員の購入金額, 購入回数, 初回購入日, 最終購入費を更新する
400 34
     *
401 1
     * @param Customer $Customer
402
     * @param array $OrderStatuses
403 1
     */
404 1
    public function updateOrderSummary(Customer $Customer, array $OrderStatuses = [OrderStatus::NEW, OrderStatus::PAID, OrderStatus::DELIVERED, OrderStatus::IN_PROGRESS])
405
    {
406 34
        try {
407 1
            $result = $this->createQueryBuilder('o')
408
                ->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')
409 1
                ->where('o.Customer = :Customer')
410
                ->andWhere('o.OrderStatus in (:OrderStatuses)')
411 1
                ->setParameter('Customer', $Customer)
412 1
                ->setParameter('OrderStatuses', $OrderStatuses)
413
                ->groupBy('o.Customer')
414
                ->getQuery()
415
                ->getSingleResult();
416 34
        } catch (NoResultException $e) {
417
            // 受注データが存在しなければ初期化
418 1
            $Customer->setFirstBuyDate(null);
419 1
            $Customer->setLastBuyDate(null);
420
            $Customer->setBuyTimes(0);
421 34
            $Customer->setBuyTotal(0);
422
423 1
            return;
424 1
        }
425
426
        $FirstOrder = $this->find(['id' => $result['first_order_id']]);
427
        $LastOrder = $this->find(['id' => $result['last_order_id']]);
428 34
429
        $Customer->setBuyTimes($result['buy_times']);
430 1
        $Customer->setBuyTotal($result['buy_total']);
431 1
        $Customer->setFirstBuyDate($FirstOrder->getOrderDate());
432 1
        $Customer->setLastBuyDate($LastOrder->getOrderDate());
433
    }
434
}
435