Failed Conditions
Pull Request — experimental/3.1 (#2512)
by chihiro
12:14
created

OrderRepository::getNew()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
crap 1
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
25
namespace Eccube\Repository;
26
27
use Doctrine\ORM\QueryBuilder;
28
use Eccube\Annotation\Inject;
29
use Eccube\Annotation\Repository;
30
use Eccube\Doctrine\Query\Queries;
31
use Eccube\Util\Str;
32
33
/**
34
 * OrderRepository
35
 *
36
 * This class was generated by the Doctrine ORM. Add your own custom
37
 * repository methods below.
38
 *
39
 * @Repository
40
 */
41
class OrderRepository extends AbstractRepository
42
{
43
    /**
44
     * @Inject("config")
45
     * @var array
46
     */
47
    protected $appConfig;
48
49
    /**
50
     * @Inject("eccube.queries")
51
     * @var Queries
52
     */
53
    protected $queries;
54
55 7
    public function changeStatus($orderId, \Eccube\Entity\Master\OrderStatus $Status)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
56
    {
57
        $Order = $this
58 7
            ->find($orderId)
59 7
            ->setOrderStatus($Status)
60
        ;
61
62 7
        switch ($Status->getId()) {
63 7
            case '5': // 発送済へ
64 3
                $Order->setCommitDate(new \DateTime());
65 3
                break;
66 4
            case '6': // 入金済へ
67 3
                $Order->setPaymentDate(new \DateTime());
68 3
                break;
69
        }
70
71 7
        $em = $this->getEntityManager();
72 7
        $em->persist($Order);
73 7
        $em->flush();
74
    }
75
76
    /**
77
     *
78
     * @param  array        $searchData
0 ignored issues
show
introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
79
     * @return QueryBuilder
80
     */
81 19
    public function getQueryBuilderBySearchData($searchData)
82
    {
83 19
        $qb = $this->createQueryBuilder('o');
84
85 19
        $joinedCustomer = false;
86
87
        // order_id_start
88 19 View Code Duplication
        if (isset($searchData['order_id_start']) && Str::isNotBlank($searchData['order_id_start'])) {
89
            $qb
90 1
                ->andWhere('o.id >= :order_id_start')
91 1
                ->setParameter('order_id_start', $searchData['order_id_start']);
92
        }
93
94
        // order_id_end
95 19 View Code Duplication
        if (isset($searchData['order_id_end']) && Str::isNotBlank($searchData['order_id_end'])) {
96
            $qb
97 1
                ->andWhere('o.id <= :order_id_end')
98 1
                ->setParameter('order_id_end', $searchData['order_id_end']);
99
        }
100
101
        // status
102 19 View Code Duplication
        if (!empty($searchData['status']) && $searchData['status']) {
103
            $qb
104 1
                ->andWhere('o.OrderStatus = :status')
105 1
                ->setParameter('status', $searchData['status']);
106
        }
107
108
        // name
109 19 View Code Duplication
        if (isset($searchData['name']) && Str::isNotBlank($searchData['name'])) {
110
            $qb
111 1
                ->andWhere('CONCAT(o.name01, o.name02) LIKE :name')
112 1
                ->setParameter('name', '%' . $searchData['name'] . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
113
        }
114
115
        // kana
116 19 View Code Duplication
        if (isset($searchData['kana']) && Str::isNotBlank($searchData['kana'])) {
117
            $qb
118 1
                ->andWhere('CONCAT(o.kana01, o.kana02) LIKE :kana')
119 1
                ->setParameter('kana', '%' . $searchData['kana'] . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
120
        }
121
122
        // email
123 19 View Code Duplication
        if (isset($searchData['email']) && Str::isNotBlank($searchData['email'])) {
124
            $qb
125 1
                ->andWhere('o.email = :email')
126 1
                ->setParameter('email', $searchData['email']);
127
        }
128
129
        // tel
130 19 View Code Duplication
        if (isset($searchData['tel01']) && Str::isNotBlank($searchData['tel01'])) {
131
            $qb
132 1
                ->andWhere('o.tel01 = :tel01')
133 1
                ->setParameter('tel01', $searchData['tel01']);
134
        }
135 19 View Code Duplication
        if (isset($searchData['tel02']) && Str::isNotBlank($searchData['tel02'])) {
136
            $qb
137 1
                ->andWhere('o.tel02 = :tel02')
138 1
                ->setParameter('tel02', $searchData['tel02']);
139
        }
140 19 View Code Duplication
        if (isset($searchData['tel03']) && Str::isNotBlank($searchData['tel03'])) {
141
            $qb
142 1
                ->andWhere('o.tel03 = :tel03')
143 1
                ->setParameter('tel03', $searchData['tel03']);
144
        }
145
146
        // birth
147 19 View Code Duplication
        if (!empty($searchData['birth_start']) && $searchData['birth_start']) {
148 1
            if (!$joinedCustomer) {
149 1
                $qb->leftJoin('o.Customer', 'c');
150 1
                $joinedCustomer = true;
151
            }
152
153 1
            $date = $searchData['birth_start'];
154
            $qb
155 1
                ->andWhere('c.birth >= :birth_start')
156 1
                ->setParameter('birth_start', $date);
157
        }
158 19 View Code Duplication
        if (!empty($searchData['birth_end']) && $searchData['birth_end']) {
159 1
            if (!$joinedCustomer) {
160 1
                $qb->leftJoin('o.Customer', 'c');
161 1
                $joinedCustomer = true;
162
            }
163
164 1
            $date = clone $searchData['birth_end'];
165
            $date = $date
166 1
                ->modify('+1 days');
167
            $qb
168 1
                ->andWhere('c.birth < :birth_end')
169 1
                ->setParameter('birth_end', $date);
170
        }
171
172
        // sex
173 19
        if (!empty($searchData['sex']) && count($searchData['sex']) > 0) {
174 1
            if (!$joinedCustomer) {
175 1
                $qb->leftJoin('o.Customer', 'c');
176 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...
177
            }
178
179 1
            $sexs = array();
180 1
            foreach ($searchData['sex'] as $sex) {
181 1
                $sexs[] = $sex->getId();
182
            }
183
184
            $qb
185 1
                ->andWhere($qb->expr()->in('c.Sex', ':sexs'))
186 1
                ->setParameter('sexs', $sexs);
187
        }
188
189
        // payment
190 19 View Code Duplication
        if (!empty($searchData['payment']) && count($searchData['payment'])) {
191
            $payments = array();
192
            foreach ($searchData['payment'] as $payment) {
193
                $payments[] = $payment->getId();
194
            }
195
            $qb
196
                ->leftJoin('o.Payment', 'p')
197
                ->andWhere($qb->expr()->in('p.id', ':payments'))
198
                ->setParameter('payments', $payments);
199
        }
200
201
        // oreder_date
202 19 View Code Duplication
        if (!empty($searchData['order_date_start']) && $searchData['order_date_start']) {
203 1
            $date = $searchData['order_date_start'];
204
            $qb
205 1
                ->andWhere('o.create_date >= :order_date_start')
206 1
                ->setParameter('order_date_start', $date);
207
        }
208 19 View Code Duplication
        if (!empty($searchData['order_date_end']) && $searchData['order_date_end']) {
209 1
            $date = clone $searchData['order_date_end'];
210
            $date = $date
211 1
                ->modify('+1 days');
212
            $qb
213 1
                ->andWhere('o.create_date < :order_date_end')
214 1
                ->setParameter('order_date_end', $date);
215
        }
216
217
        // create_date
218 19 View Code Duplication
        if (!empty($searchData['update_date_start']) && $searchData['update_date_start']) {
219 1
            $date = $searchData['update_date_start'];
220
            $qb
221 1
                ->andWhere('o.update_date >= :update_date_start')
222 1
                ->setParameter('update_date_start', $date);
223
        }
224 19 View Code Duplication
        if (!empty($searchData['update_date_end']) && $searchData['update_date_end']) {
225 1
            $date = clone $searchData['update_date_end'];
226
            $date = $date
227 1
                ->modify('+1 days');
228
            $qb
229 1
                ->andWhere('o.update_date < :update_date_end')
230 1
                ->setParameter('update_date_end', $date);
231
        }
232
233
        // payment_total
234 19 View Code Duplication
        if (isset($searchData['payment_total_start']) && Str::isNotBlank($searchData['payment_total_start'])) {
235
            $qb
236 1
                ->andWhere('o.payment_total >= :payment_total_start')
237 1
                ->setParameter('payment_total_start', $searchData['payment_total_start']);
238
        }
239 19 View Code Duplication
        if (isset($searchData['payment_total_end']) && Str::isNotBlank($searchData['payment_total_end'])) {
240
            $qb
241 1
                ->andWhere('o.payment_total <= :payment_total_end')
242 1
                ->setParameter('payment_total_end', $searchData['payment_total_end']);
243
        }
244
245
        // buy_product_name
246 19 View Code Duplication
        if (isset($searchData['buy_product_name']) && Str::isNotBlank($searchData['buy_product_name'])) {
247
            $qb
248 1
                ->leftJoin('o.OrderDetails', 'od')
249 1
                ->andWhere('od.product_name LIKE :buy_product_name')
250 1
                ->setParameter('buy_product_name', '%' . $searchData['buy_product_name'] . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
251
        }
252
253
        // Order By
254 19
        $qb->addOrderBy('o.update_date', 'DESC');
255
256 19
        return $this->queries->customize(QueryKey::ORDER_SEARCH, $qb, $searchData);
257
    }
258
259
260
    /**
261
     *
262
     * @param  array        $searchData
0 ignored issues
show
introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
263
     * @return QueryBuilder
264
     */
265 30
    public function getQueryBuilderBySearchDataForAdmin($searchData)
266
    {
267 30
        $qb = $this->createQueryBuilder('o');
268
269
        // order_id_start
270 30 View Code Duplication
        if (isset($searchData['order_id_start']) && Str::isNotBlank($searchData['order_id_start'])) {
271
            $qb
272 1
                ->andWhere('o.id >= :order_id_start')
273 1
                ->setParameter('order_id_start', $searchData['order_id_start']);
274
        }
275
        // multi
276 30 View Code Duplication
        if (isset( $searchData['multi']) && Str::isNotBlank($searchData['multi'])) {
277 5
            $multi = preg_match('/^\d+$/', $searchData['multi']) ? $searchData['multi'] : null;
278
            $qb
279 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...
280 5
                           'o.kana01 LIKE :likemulti OR o.kana02 LIKE :likemulti OR o.company_name 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...
281 5
                ->setParameter('multi', $multi)
282 5
                ->setParameter('likemulti', '%' . $searchData['multi'] . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
283
        }
284
285
        // order_id_end
286 30 View Code Duplication
        if (isset($searchData['order_id_end']) && Str::isNotBlank($searchData['order_id_end'])) {
287
            $qb
288 1
                ->andWhere('o.id <= :order_id_end')
289 1
                ->setParameter('order_id_end', $searchData['order_id_end']);
290
        }
291
292
        // status
293 30
        $filterStatus = false;
294 30 View Code Duplication
        if (!empty($searchData['status']) && $searchData['status']) {
295
            $qb
296 2
                ->andWhere('o.OrderStatus = :status')
297 2
                ->setParameter('status', $searchData['status']);
298 2
            $filterStatus = true;
299
        }
300 30
        if (!empty($searchData['multi_status']) && count($searchData['multi_status'])) {
301
            $qb
302 1
                ->andWhere($qb->expr()->in('o.OrderStatus', ':multi_status'))
303 1
                ->setParameter('multi_status', $searchData['multi_status']->toArray());
304 1
            $filterStatus = true;
305
        }
306 30
        if (!$filterStatus) {
307
            // 購入処理中は検索対象から除外
308 27
            $OrderStatuses = $this->getEntityManager()
309 27
                ->getRepository('Eccube\Entity\Master\OrderStatus')
310 27
                ->findNotContainsBy(array('id' => $this->appConfig['order_processing']));
311 27
            $qb->andWhere($qb->expr()->in('o.OrderStatus', ':status'))
312 27
                ->setParameter('status', $OrderStatuses);
313
        }
314
315
        // name
316 30 View Code Duplication
        if (isset($searchData['name']) && Str::isNotBlank($searchData['name'])) {
317
            $qb
318 1
                ->andWhere('CONCAT(o.name01, o.name02) LIKE :name')
319 1
                ->setParameter('name', '%' . $searchData['name'] . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
320
        }
321
322
        // kana
323 30 View Code Duplication
        if (isset($searchData['kana']) && Str::isNotBlank($searchData['kana'])) {
324
            $qb
325 1
                ->andWhere('CONCAT(o.kana01, o.kana02) LIKE :kana')
326 1
                ->setParameter('kana', '%' . $searchData['kana'] . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
327
        }
328
329
        // email
330 30 View Code Duplication
        if (isset($searchData['email']) && Str::isNotBlank($searchData['email'])) {
331
            $qb
332 2
                ->andWhere('o.email like :email')
333 2
                ->setParameter('email', '%' . $searchData['email'] . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
334
        }
335
336
        // tel
337 30 View Code Duplication
        if (isset($searchData['tel']) && Str::isNotBlank($searchData['tel'])) {
338
            $qb
339 1
                ->andWhere('CONCAT(o.tel01, o.tel02, o.tel03) LIKE :tel')
340 1
                ->setParameter('tel', '%' . $searchData['tel'] . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
341
        }
342
343
        // sex
344 30
        if (!empty($searchData['sex']) && count($searchData['sex']) > 0) {
345
            $qb
346 2
                ->andWhere($qb->expr()->in('o.Sex', ':sex'))
347 2
                ->setParameter('sex', $searchData['sex']->toArray());
348
        }
349
350
        // payment
351 30 View Code Duplication
        if (!empty($searchData['payment']) && count($searchData['payment'])) {
352 1
            $payments = array();
353 1
            foreach ($searchData['payment'] as $payment) {
354 1
                $payments[] = $payment->getId();
355
            }
356
            $qb
357 1
                ->leftJoin('o.Payment', 'p')
358 1
                ->andWhere($qb->expr()->in('p.id', ':payments'))
359 1
                ->setParameter('payments', $payments);
360
        }
361
362
        // oreder_date
363 30 View Code Duplication
        if (!empty($searchData['order_date_start']) && $searchData['order_date_start']) {
364 1
            $date = $searchData['order_date_start'];
365
            $qb
366 1
                ->andWhere('o.order_date >= :order_date_start')
367 1
                ->setParameter('order_date_start', $date);
368
        }
369 30 View Code Duplication
        if (!empty($searchData['order_date_end']) && $searchData['order_date_end']) {
370 1
            $date = clone $searchData['order_date_end'];
371
            $date = $date
372 1
                ->modify('+1 days');
373
            $qb
374 1
                ->andWhere('o.order_date < :order_date_end')
375 1
                ->setParameter('order_date_end', $date);
376
        }
377
378
        // payment_date
379 30 View Code Duplication
        if (!empty($searchData['payment_date_start']) && $searchData['payment_date_start']) {
380 1
            $date = $searchData['payment_date_start'];
381
            $qb
382 1
                ->andWhere('o.payment_date >= :payment_date_start')
383 1
                ->setParameter('payment_date_start', $date);
384
        }
385 30 View Code Duplication
        if (!empty($searchData['payment_date_end']) && $searchData['payment_date_end']) {
386 1
            $date = clone $searchData['payment_date_end'];
387
            $date = $date
388 1
                ->modify('+1 days');
389
            $qb
390 1
                ->andWhere('o.payment_date < :payment_date_end')
391 1
                ->setParameter('payment_date_end', $date);
392
        }
393
394
        // commit_date
395 30 View Code Duplication
        if (!empty($searchData['commit_date_start']) && $searchData['commit_date_start']) {
396 1
            $date = $searchData['commit_date_start'];
397
            $qb
398 1
                ->andWhere('o.commit_date >= :commit_date_start')
399 1
                ->setParameter('commit_date_start', $date);
400
        }
401 30 View Code Duplication
        if (!empty($searchData['commit_date_end']) && $searchData['commit_date_end']) {
402 1
            $date = clone $searchData['commit_date_end'];
403
            $date = $date
404 1
                ->modify('+1 days');
405
            $qb
406 1
                ->andWhere('o.commit_date < :commit_date_end')
407 1
                ->setParameter('commit_date_end', $date);
408
        }
409
410
411
        // update_date
412 30 View Code Duplication
        if (!empty($searchData['update_date_start']) && $searchData['update_date_start']) {
413 1
            $date = $searchData['update_date_start'];
414
            $qb
415 1
                ->andWhere('o.update_date >= :update_date_start')
416 1
                ->setParameter('update_date_start', $date);
417
        }
418 30 View Code Duplication
        if (!empty($searchData['update_date_end']) && $searchData['update_date_end']) {
419 1
            $date = clone $searchData['update_date_end'];
420
            $date = $date
421 1
                ->modify('+1 days');
422
            $qb
423 1
                ->andWhere('o.update_date < :update_date_end')
424 1
                ->setParameter('update_date_end', $date);
425
        }
426
427
        // payment_total
428 30 View Code Duplication
        if (isset($searchData['payment_total_start']) && Str::isNotBlank($searchData['payment_total_start'])) {
429
            $qb
430 1
                ->andWhere('o.payment_total >= :payment_total_start')
431 1
                ->setParameter('payment_total_start', $searchData['payment_total_start']);
432
        }
433 30 View Code Duplication
        if (isset($searchData['payment_total_end']) && Str::isNotBlank($searchData['payment_total_end'])) {
434
            $qb
435 1
                ->andWhere('o.payment_total <= :payment_total_end')
436 1
                ->setParameter('payment_total_end', $searchData['payment_total_end']);
437
        }
438
439
        // buy_product_name
440 30 View Code Duplication
        if (isset($searchData['buy_product_name']) && Str::isNotBlank($searchData['buy_product_name'])) {
441
            $qb
442 1
                ->leftJoin('o.OrderDetails', 'od')
443 1
                ->andWhere('od.product_name LIKE :buy_product_name')
444 1
                ->setParameter('buy_product_name', '%' . $searchData['buy_product_name'] . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
445
        }
446
447
        // Order By
448 30
        $qb->orderBy('o.update_date', 'DESC');
449 30
        $qb->addorderBy('o.id', 'DESC');
450
451 30
        return $this->queries->customize(QueryKey::ORDER_SEARCH_ADMIN, $qb, $searchData);
452
    }
453
454
455
    /**
456
     * @param  \Eccube\Entity\Customer $Customer
457
     * @return QueryBuilder
458
     */
459 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...
460
    {
461 2
        $qb = $this->createQueryBuilder('o')
462 2
            ->where('o.Customer = :Customer')
463 2
            ->setParameter('Customer', $Customer);
464
465
        // Order By
466 2
        $qb->addOrderBy('o.id', 'DESC');
467
468 2
        return $this->queries->customize(QueryKey::ORDER_SEARCH_BY_CUSTOMER, $qb, ['customer' => $Customer]);
469
    }
470
471
    /**
472
     * 会員の合計購入金額を取得、回数を取得
473
     *
474
     * @param  \Eccube\Entity\Customer $Customer
475
     * @param  array $OrderStatuses
0 ignored issues
show
introduced by
Expected 19 spaces after parameter type; 1 found
Loading history...
476
     * @return array
477
     */
478 2
    public function getCustomerCount(\Eccube\Entity\Customer $Customer, array $OrderStatuses)
479
    {
480 2
        $result = $this->createQueryBuilder('o')
481 2
            ->select('COUNT(o.id) AS buy_times, SUM(o.total)  AS buy_total')
482 2
            ->where('o.Customer = :Customer')
483 2
            ->andWhere('o.OrderStatus in (:OrderStatuses)')
484 2
            ->setParameter('Customer', $Customer)
485 2
            ->setParameter('OrderStatuses', $OrderStatuses)
486 2
            ->groupBy('o.id')
487 2
            ->orderBy('o.id', 'ASC')
488 2
            ->getQuery()
489 2
            ->getResult();
490
491 2
        return $result;
492
    }
493
}
494