Failed Conditions
Push — dev/plugin-misc ( f22d99...066a4d )
by Kiyotaka
10:54 queued 03:43
created

OrderRepository::getCustomerCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A OrderRepository::countByOrderStatus() 0 9 1
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
     */
45
    public function __construct(RegistryInterface $registry, Queries $queries)
46
    {
47
        parent::__construct($registry, Order::class);
48
        $this->queries = $queries;
49
    }
50
51
    /**
52
     * @param int $orderId
53
     * @param OrderStatus $Status
54
     */
55
    public function changeStatus($orderId, \Eccube\Entity\Master\OrderStatus $Status)
56
    {
57
        $Order = $this
58
            ->find($orderId)
59
            ->setOrderStatus($Status)
60
        ;
61
62
        switch ($Status->getId()) {
63
            case '6': // 入金済へ
64
                $Order->setPaymentDate(new \DateTime());
65
                break;
66
        }
67
68
        $em = $this->getEntityManager();
69
        $em->persist($Order);
70
        $em->flush();
71
    }
72
73
    /**
74
     * @param  array        $searchData
75
     *
76
     * @return QueryBuilder
77
     */
78
    public function getQueryBuilderBySearchDataForAdmin($searchData)
79
    {
80
        $qb = $this->createQueryBuilder('o')
81
            ->select('o, s')
82
            ->addSelect('oi', 'pref')
83
            ->leftJoin('o.OrderItems', 'oi')
84
            ->leftJoin('o.Pref', 'pref')
85
            ->innerJoin('o.Shippings', 's');
86
87
        // order_id_start
88 View Code Duplication
        if (isset($searchData['order_id']) && StringUtil::isNotBlank($searchData['order_id'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
89
            $qb
90
                ->andWhere('o.id = :order_id')
91
                ->setParameter('order_id', $searchData['order_id']);
92
        }
93
94
        // order_no
95 View Code Duplication
        if (isset($searchData['order_no']) && StringUtil::isNotBlank($searchData['order_no'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
96
            $qb
97
                ->andWhere('o.order_no = :order_no')
98
                ->setParameter('order_no', $searchData['order_no']);
99
        }
100
101
        // order_id_start
102 View Code Duplication
        if (isset($searchData['order_id_start']) && StringUtil::isNotBlank($searchData['order_id_start'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
103
            $qb
104
                ->andWhere('o.id >= :order_id_start')
105
                ->setParameter('order_id_start', $searchData['order_id_start']);
106
        }
107
        // multi
108 View Code Duplication
        if (isset($searchData['multi']) && StringUtil::isNotBlank($searchData['multi'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
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
                            'o.kana01 LIKE :likemulti OR o.kana02 LIKE :likemulti OR o.company_name LIKE :likemulti OR '.
113
                            'o.order_no LIKE :likemulti')
114
                ->setParameter('multi', $multi)
115
                ->setParameter('likemulti', '%'.$searchData['multi'].'%');
116
        }
117
118
        // order_id_end
119 View Code Duplication
        if (isset($searchData['order_id_end']) && StringUtil::isNotBlank($searchData['order_id_end'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
120
            $qb
121
                ->andWhere('o.id <= :order_id_end')
122
                ->setParameter('order_id_end', $searchData['order_id_end']);
123
        }
124
125
        // status
126
        $filterStatus = false;
127 View Code Duplication
        if (!empty($searchData['status']) && count($searchData['status'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
128
            $qb
129
                ->andWhere($qb->expr()->in('o.OrderStatus', ':status'))
130
                ->setParameter('status', $searchData['status']);
131
            $filterStatus = true;
132
        }
133
134
        if (!$filterStatus) {
135
            // 購入処理中は検索対象から除外
136
            $OrderStatuses = $this->getEntityManager()
137
                ->getRepository('Eccube\Entity\Master\OrderStatus')
138
                ->findNotContainsBy(['id' => OrderStatus::PROCESSING]);
139
            $qb->andWhere($qb->expr()->in('o.OrderStatus', ':status'))
140
                ->setParameter('status', $OrderStatuses);
141
        }
142
143
        // company_name
144 View Code Duplication
        if (isset($searchData['company_name']) && StringUtil::isNotBlank($searchData['company_name'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
145
            $qb
146
                ->andWhere('o.company_name LIKE :company_name')
147
                ->setParameter('company_name', '%'.$searchData['company_name'].'%');
148
        }
149
150
        // name
151 View Code Duplication
        if (isset($searchData['name']) && StringUtil::isNotBlank($searchData['name'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
152
            $qb
153
                ->andWhere('CONCAT(o.name01, o.name02) LIKE :name')
154
                ->setParameter('name', '%'.$searchData['name'].'%');
155
        }
156
157
        // kana
158 View Code Duplication
        if (isset($searchData['kana']) && StringUtil::isNotBlank($searchData['kana'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
159
            $qb
160
                ->andWhere('CONCAT(o.kana01, o.kana02) LIKE :kana')
161
                ->setParameter('kana', '%'.$searchData['kana'].'%');
162
        }
163
164
        // email
165 View Code Duplication
        if (isset($searchData['email']) && StringUtil::isNotBlank($searchData['email'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
166
            $qb
167
                ->andWhere('o.email like :email')
168
                ->setParameter('email', '%'.$searchData['email'].'%');
169
        }
170
171
        // tel
172 View Code Duplication
        if (isset($searchData['phone_number']) && StringUtil::isNotBlank($searchData['phone_number'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
173
            $tel = preg_replace('/[^0-9]/ ', '', $searchData['phone_number']);
174
            $qb
175
                ->andWhere('o.phone_number LIKE :phone_number')
176
                ->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'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
188
            $payments = [];
189
            foreach ($searchData['payment'] as $payment) {
190
                $payments[] = $payment->getId();
191
            }
192
            $qb
193
                ->leftJoin('o.Payment', 'p')
194
                ->andWhere($qb->expr()->in('p.id', ':payments'))
195
                ->setParameter('payments', $payments);
196
        }
197
198
        // oreder_date
199 View Code Duplication
        if (!empty($searchData['order_date_start']) && $searchData['order_date_start']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
200
            $date = $searchData['order_date_start'];
201
            $qb
202
                ->andWhere('o.order_date >= :order_date_start')
203
                ->setParameter('order_date_start', $date);
204
        }
205 View Code Duplication
        if (!empty($searchData['order_date_end']) && $searchData['order_date_end']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
206
            $date = clone $searchData['order_date_end'];
207
            $date = $date
208
                ->modify('+1 days');
209
            $qb
210
                ->andWhere('o.order_date < :order_date_end')
211
                ->setParameter('order_date_end', $date);
212
        }
213
214
        // payment_date
215 View Code Duplication
        if (!empty($searchData['payment_date_start']) && $searchData['payment_date_start']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
216
            $date = $searchData['payment_date_start'];
217
            $qb
218
                ->andWhere('o.payment_date >= :payment_date_start')
219
                ->setParameter('payment_date_start', $date);
220
        }
221 View Code Duplication
        if (!empty($searchData['payment_date_end']) && $searchData['payment_date_end']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
222
            $date = clone $searchData['payment_date_end'];
223
            $date = $date
224
                ->modify('+1 days');
225
            $qb
226
                ->andWhere('o.payment_date < :payment_date_end')
227
                ->setParameter('payment_date_end', $date);
228
        }
229
230
        // update_date
231 View Code Duplication
        if (!empty($searchData['update_date_start']) && $searchData['update_date_start']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
232
            $date = $searchData['update_date_start'];
233
            $qb
234
                ->andWhere('o.update_date >= :update_date_start')
235
                ->setParameter('update_date_start', $date);
236
        }
237 View Code Duplication
        if (!empty($searchData['update_date_end']) && $searchData['update_date_end']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
238
            $date = clone $searchData['update_date_end'];
239
            $date = $date
240
                ->modify('+1 days');
241
            $qb
242
                ->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'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
248
            $qb
249
                ->andWhere('o.payment_total >= :payment_total_start')
250
                ->setParameter('payment_total_start', $searchData['payment_total_start']);
251
        }
252 View Code Duplication
        if (isset($searchData['payment_total_end']) && StringUtil::isNotBlank($searchData['payment_total_end'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
253
            $qb
254
                ->andWhere('o.payment_total <= :payment_total_end')
255
                ->setParameter('payment_total_end', $searchData['payment_total_end']);
256
        }
257
258
        // buy_product_name
259 View Code Duplication
        if (isset($searchData['buy_product_name']) && StringUtil::isNotBlank($searchData['buy_product_name'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
260
            $qb
261
                ->andWhere('oi.product_name LIKE :buy_product_name')
262
                ->setParameter('buy_product_name', '%'.$searchData['buy_product_name'].'%');
263
        }
264
265
        // 発送メール送信/未送信.
266
        if (isset($searchData['shipping_mail']) && $count = count($searchData['shipping_mail'])) {
267
            // 送信済/未送信両方にチェックされている場合は検索条件に追加しない
268
            if ($count < 2) {
269
                $checked = current($searchData['shipping_mail']);
270
                if ($checked == Shipping::SHIPPING_MAIL_UNSENT) {
271
                    // 未送信
272
                    $qb
273
                        ->andWhere('s.mail_send_date IS NULL');
274
                } elseif ($checked == Shipping::SHIPPING_MAIL_SENT) {
275
                    // 送信
276
                    $qb
277
                        ->andWhere('s.mail_send_date IS NOT NULL');
278
                }
279
            }
280
        }
281
282
        // 送り状番号.
283
        if (!empty($searchData['tracking_number'])) {
284
            $qb
285
                ->andWhere('s.tracking_number = :tracking_number')
286
                ->setParameter('tracking_number', $searchData['tracking_number']);
287
        }
288
289
        // お届け予定日(Shipping.delivery_date)
290 View Code Duplication
        if (!empty($searchData['shipping_delivery_date_start']) && $searchData['shipping_delivery_date_start']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
291
            $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
        }
296 View Code Duplication
        if (!empty($searchData['shipping_delivery_date_end']) && $searchData['shipping_delivery_date_end']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
297
            $date = clone $searchData['shipping_delivery_date_end'];
298
            $date = $date
299
                ->modify('+1 days');
300
            $qb
301
                ->andWhere('s.shipping_delivery_date < :shipping_delivery_date_end')
302
                ->setParameter('shipping_delivery_date_end', $date);
303
        }
304
305
        // Order By
306
        $qb->orderBy('o.update_date', 'DESC');
307
        $qb->addorderBy('o.id', 'DESC');
308
309
        return $this->queries->customize(QueryKey::ORDER_SEARCH_ADMIN, $qb, $searchData);
310
    }
311
312
    /**
313
     * @param  \Eccube\Entity\Customer $Customer
314
     *
315
     * @return QueryBuilder
316
     */
317
    public function getQueryBuilderByCustomer(\Eccube\Entity\Customer $Customer)
318
    {
319
        $qb = $this->createQueryBuilder('o')
320
            ->where('o.Customer = :Customer')
321
            ->setParameter('Customer', $Customer);
322
323
        // Order By
324
        $qb->addOrderBy('o.id', 'DESC');
325
326
        return $this->queries->customize(QueryKey::ORDER_SEARCH_BY_CUSTOMER, $qb, ['customer' => $Customer]);
327
    }
328
329
    /**
330
     * ステータスごとの受注件数を取得する.
331
     *
332
     * @param integer $OrderStatusOrId
333
     *
334
     * @return int
335
     *
336
     * @throws \Doctrine\ORM\NoResultException
337
     * @throws \Doctrine\ORM\NonUniqueResultException
338
     */
339
    public function countByOrderStatus($OrderStatusOrId)
340
    {
341
        return (int) $this->createQueryBuilder('o')
342
            ->select('COALESCE(COUNT(o.id), 0)')
343
            ->where('o.OrderStatus = :OrderStatus')
344
            ->setParameter('OrderStatus', $OrderStatusOrId)
345
            ->getQuery()
346
            ->getSingleScalarResult();
347
    }
348
349
    /**
350
     * 会員の購入金額, 購入回数, 初回購入日, 最終購入費を更新する
351
     *
352
     * @param Customer $Customer
353
     * @param array $OrderStatuses
354
     */
355
    public function updateOrderSummary(Customer $Customer, array $OrderStatuses = [OrderStatus::NEW, OrderStatus::PAID, OrderStatus::DELIVERED, OrderStatus::IN_PROGRESS])
356
    {
357
        try {
358
            $result = $this->createQueryBuilder('o')
359
                ->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')
360
                ->where('o.Customer = :Customer')
361
                ->andWhere('o.OrderStatus in (:OrderStatuses)')
362
                ->setParameter('Customer', $Customer)
363
                ->setParameter('OrderStatuses', $OrderStatuses)
364
                ->groupBy('o.Customer')
365
                ->getQuery()
366
                ->getSingleResult();
367
        } catch (NoResultException $e) {
368
            // 受注データが存在しなければ初期化
369
            $Customer->setFirstBuyDate(null);
370
            $Customer->setLastBuyDate(null);
371
            $Customer->setBuyTimes(0);
372
            $Customer->setBuyTotal(0);
373
374
            return;
375
        }
376
377
        $FirstOrder = $this->find(['id' => $result['first_order_id']]);
378
        $LastOrder = $this->find(['id' => $result['last_order_id']]);
379
380
        $Customer->setBuyTimes($result['buy_times']);
381
        $Customer->setBuyTotal($result['buy_total']);
382
        $Customer->setFirstBuyDate($FirstOrder->getOrderDate());
383
        $Customer->setLastBuyDate($LastOrder->getOrderDate());
384
    }
385
}
386