Failed Conditions
Pull Request — experimental/3.1 (#2304)
by Kiyotaka
39:27
created

OrderRepository::setApplication()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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