Completed
Pull Request — experimental/3.1 (#2154)
by Kentaro
448:33 queued 441:10
created

EditController::searchCustomerById()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 59
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 34
CRAP Score 4.0027

Importance

Changes 0
Metric Value
cc 4
eloc 41
nc 4
nop 2
dl 0
loc 59
ccs 34
cts 36
cp 0.9444
crap 4.0027
rs 8.9846
c 0
b 0
f 0

How to fix   Long Method   

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
 * 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
namespace Eccube\Controller\Admin\Order;
25
26
use Doctrine\Common\Collections\ArrayCollection;
27
use Eccube\Application;
28
use Eccube\Common\Constant;
29
use Eccube\Controller\AbstractController;
30
use Eccube\Entity\ShipmentItem;
31
use Eccube\Event\EccubeEvents;
32
use Eccube\Event\EventArgs;
33
use Eccube\Form\Type\AddCartType;
34
use Eccube\Form\Type\Admin\OrderType;
35
use Eccube\Form\Type\Admin\SearchCustomerType;
36
use Eccube\Form\Type\Admin\SearchProductType;
37
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
38
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
39
use Symfony\Component\Form\FormError;
40
use Symfony\Component\HttpFoundation\Request;
41
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
42
43
/**
44
 * TODO 管理画面のルーティングは動的に行う.おそらくコントローラのディレクトリをフロント/管理で分ける必要がある
45
 *
46
 * @Route("/admin/order")
47
 */
48
class EditController extends AbstractController
49
{
50
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$id" missing
Loading history...
51
     * 受注登録/編集画面.
52
     *
53
     * @Route("/edit", name="admin_order_new")
54
     * @Route("/{id}/edit", requirements={"id" = "\d+"}, name="admin_order_edit")
55
     * @Template("Order/edit.twig")
56
     *
57
     * TODO templateアノテーションを利用するかどうか検討.http://symfony.com/doc/current/best_practices/controllers.html
58
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
59 17
    public function index(Application $app, Request $request, $id = null)
60
    {
61
        /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */
62 17
        $softDeleteFilter = $app['orm.em']->getFilters()->getFilter('soft_delete');
63 17
        $softDeleteFilter->setExcludes(array(
64 17
            'Eccube\Entity\ProductClass',
65
            'Eccube\Entity\Product',
66
        ));
67
68 17
        $TargetOrder = null;
0 ignored issues
show
Unused Code introduced by
$TargetOrder 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...
69 17
        $OriginOrder = null;
0 ignored issues
show
Unused Code introduced by
$OriginOrder 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...
70
71 17
        if (is_null($id)) {
72
            // 空のエンティティを作成.
73 7
            $TargetOrder = $this->newOrder();
74
        } else {
75 10
            $TargetOrder = $app['eccube.repository.order']->find($id);
76 10
            if (is_null($TargetOrder)) {
77
                throw new NotFoundHttpException();
78
            }
79
        }
80
81
        // 編集前の受注情報を保持
82 17
        $OriginOrder = clone $TargetOrder;
83 17
        $OriginalOrderDetails = new ArrayCollection();
84
85 17
        foreach ($TargetOrder->getOrderDetails() as $OrderDetail) {
86 17
            $OriginalOrderDetails->add($OrderDetail);
87
        }
88
89 17
        $builder = $app['form.factory']
90 17
            ->createBuilder(OrderType::class, $TargetOrder);
91
92 17
        $event = new EventArgs(
93
            array(
94 17
                'builder' => $builder,
95 17
                'OriginOrder' => $OriginOrder,
96 17
                'TargetOrder' => $TargetOrder,
97 17
                'OriginOrderDetails' => $OriginalOrderDetails,
98
            ),
99
            $request
100
        );
101 17
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_INDEX_INITIALIZE, $event);
102
103 17
        $form = $builder->getForm();
104 17
        $form->handleRequest($request);
105
106 17
        if ($form->isSubmitted()) {
107 11
            $event = new EventArgs(
108
                array(
109 11
                    'builder' => $builder,
110 11
                    'OriginOrder' => $OriginOrder,
111 11
                    'TargetOrder' => $TargetOrder,
112 11
                    'OriginOrderDetails' => $OriginalOrderDetails,
113
                ),
114
                $request
115
            );
116 11
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_INDEX_PROGRESS, $event);
117
118
            // 入力情報にもとづいて再計算.
119
            // TODO 購入フローのように、明細の自動生成をどこまで行うか検討する. 単純集計でよいような気がする
120
            // 集計は,この1行でいけるはず
121
            // プラグインで Strategy をセットしたりする
122
            // TODO 編集前のOrder情報が必要かもしれない
123 11
            $app['eccube.service.calculate']($TargetOrder, $TargetOrder->getCustomer())->calculate();
124
125
            // 登録ボタン押下
126 11
            switch ($request->get('mode')) {
127 11
                case 'register':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
128
129 11
                    log_info('受注登録開始', array($TargetOrder->getId()));
130
131
                    // TODO 在庫の有無や販売制限数のチェックなども行う必要があるため、完了処理もcaluclatorのように抽象化できないか検討する.
132 11
                    if ($TargetOrder->getTotal() > $app['config']['max_total_fee']) {
133
                        log_info('受注登録入力チェックエラー', array($TargetOrder->getId()));
134
                        $form['charge']->addError(new FormError('合計金額の上限を超えております。'));
135 11
                    } elseif ($form->isValid()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
136
137 11
                        $BaseInfo = $app['eccube.repository.base_info']->get();
138
139
                        // TODO 後続にある会員情報の更新のように、完了処理もcaluclatorのように抽象化できないか検討する.
140
                        // 受注日/発送日/入金日の更新.
141 11
                        $this->updateDate($app, $TargetOrder, $OriginOrder);
142
143
                        // 画面上で削除された明細は、受注明細で削除されているものをremove
144 11
                        foreach ($OriginalOrderDetails as $OrderDetail) {
145 7
                            if (false === $TargetOrder->getOrderDetails()->contains($OrderDetail)) {
146 11
                                $app['orm.em']->remove($OrderDetail);
147
                            }
148
                        }
149
150
                        // 複数配送の場合,
151 11
                        if ($BaseInfo->getOptionMultipleShipping() == Constant::ENABLED) {
152 4
                            foreach ($TargetOrder->getOrderDetails() as $OrderDetail) {
153 4
                                $OrderDetail->setOrder($TargetOrder);
154
                            }
155 4
                            $Shippings = $TargetOrder->getShippings();
156 4
                            foreach ($Shippings as $Shipping) {
157 4
                                $shipmentItems = $Shipping->getShipmentItems();
158 4
                                foreach ($shipmentItems as $ShipmentItem) {
159 4
                                    $ShipmentItem->setOrder($TargetOrder);
160 4
                                    $ShipmentItem->setShipping($Shipping);
161 4
                                    $app['orm.em']->persist($ShipmentItem);
162
                                }
163 4
                                $Shipping->setOrder($TargetOrder);
164 4
                                $app['orm.em']->persist($Shipping);
165
                            }
166
                        } else {
167
                            // 単一配送の場合, ShippimentItemsはOrderDetailの内容をコピーし、delete/insertで作り直す.
168
                            // TODO あまり本質的な処理ではないので簡略化したい.
169 7
                            $Shipping = $TargetOrder->getShippings()->first();
170 7
                            foreach ($Shipping->getShipmentItems() as $ShipmentItem) {
171 4
                                $Shipping->removeShipmentItem($ShipmentItem);
172 7
                                $app['orm.em']->remove($ShipmentItem);
173
                            }
174 7
                            foreach ($TargetOrder->getOrderDetails() as $OrderDetail) {
175 7
                                $OrderDetail->setOrder($TargetOrder);
176 7
                                if ($OrderDetail->getProduct()) {
177 7
                                    $ShipmentItem = new ShipmentItem();
178 7
                                    $ShipmentItem->copyProperties($OrderDetail);
179 7
                                    $ShipmentItem->setShipping($Shipping);
180 7
                                    $Shipping->addShipmentItem($ShipmentItem);
181
                                }
182
                            }
183
                        }
184
185 11
                        $app['orm.em']->persist($TargetOrder);
186 11
                        $app['orm.em']->flush();
187
188
                        // TODO 集計系に移動
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
189
//                        if ($Customer) {
190
//                            // 会員の場合、購入回数、購入金額などを更新
191
//                            $app['eccube.repository.customer']->updateBuyData($app, $Customer, $TargetOrder->getOrderStatus()->getId());
192
//                        }
193
194 11
                        $event = new EventArgs(
195
                            array(
196 11
                                'form' => $form,
197 11
                                'OriginOrder' => $OriginOrder,
198 11
                                'TargetOrder' => $TargetOrder,
199 11
                                'OriginOrderDetails' => $OriginalOrderDetails,
200
                                //'Customer' => $Customer,
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
201
                            ),
202
                            $request
203
                        );
204 11
                        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_INDEX_COMPLETE, $event);
205
206 11
                        $app->addSuccess('admin.order.save.complete', 'admin');
207
208 11
                        log_info('受注登録完了', array($TargetOrder->getId()));
209
210 11
                        return $app->redirect($app->url('admin_order_edit', array('id' => $TargetOrder->getId())));
211
                    }
212
213
                    break;
214
215
                case 'add_delivery':
216
                    // お届け先情報の新規追加
217
218
                    $form = $builder->getForm();
219
220
                    $Shipping = new \Eccube\Entity\Shipping();
221
                    $TargetOrder->addShipping($Shipping);
222
223
                    $Shipping->setOrder($TargetOrder);
224
225
                    $form->setData($TargetOrder);
226
227
                    break;
228
229
                default:
230
                    break;
231
            }
232
        }
233
234
        // 会員検索フォーム
235 6
        $builder = $app['form.factory']
236 6
            ->createBuilder(SearchCustomerType::class);
237
238 6
        $event = new EventArgs(
239
            array(
240 6
                'builder' => $builder,
241 6
                'OriginOrder' => $OriginOrder,
242 6
                'TargetOrder' => $TargetOrder,
243 6
                'OriginOrderDetails' => $OriginalOrderDetails,
244
            ),
245
            $request
246
        );
247 6
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_CUSTOMER_INITIALIZE, $event);
248
249 6
        $searchCustomerModalForm = $builder->getForm();
250
251
        // 商品検索フォーム
252 6
        $builder = $app['form.factory']
253 6
            ->createBuilder(SearchProductType::class);
254
255 6
        $event = new EventArgs(
256
            array(
257 6
                'builder' => $builder,
258 6
                'OriginOrder' => $OriginOrder,
259 6
                'TargetOrder' => $TargetOrder,
260 6
                'OriginOrderDetails' => $OriginalOrderDetails,
261
            ),
262
            $request
263
        );
264 6
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_PRODUCT_INITIALIZE, $event);
265
266 6
        $searchProductModalForm = $builder->getForm();
267
268
        // 配送業者のお届け時間
269 6
        $times = array();
270 6
        $deliveries = $app['eccube.repository.delivery']->findAll();
271 6
        foreach ($deliveries as $Delivery) {
272 6
            $deliveryTiems = $Delivery->getDeliveryTimes();
273 6
            foreach ($deliveryTiems as $DeliveryTime) {
274 6
                $times[$Delivery->getId()][$DeliveryTime->getId()] = $DeliveryTime->getDeliveryTime();
275
            }
276
        }
277
278
        return [
279 6
            'form' => $form->createView(),
280 6
            'searchCustomerModalForm' => $searchCustomerModalForm->createView(),
281 6
            'searchProductModalForm' => $searchProductModalForm->createView(),
282 6
            'Order' => $TargetOrder,
283 6
            'id' => $id,
284 6
            'shippingDeliveryTimes' => $app['serializer']->serialize($times, 'json'),
285
        ];
286
    }
287
288
    /**
289
     * 顧客情報を検索する.
290
     *
291
     * @param Application $app
292
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
293
     * @return \Symfony\Component\HttpFoundation\JsonResponse
294
     */
295 5
    public function searchCustomer(Application $app, Request $request)
296
    {
297 5
        if ($request->isXmlHttpRequest()) {
298 5
            $app['monolog']->addDebug('search customer start.');
299
300
            $searchData = array(
301 5
                'multi' => $request->get('search_word'),
302
            );
303
304 5
            $qb = $app['eccube.repository.customer']->getQueryBuilderBySearchData($searchData);
305
306 5
            $event = new EventArgs(
307
                array(
308 5
                    'qb' => $qb,
309 5
                    'data' => $searchData,
310
                ),
311
                $request
312
            );
313 5
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_CUSTOMER_SEARCH, $event);
314
315 5
            $Customers = $qb->getQuery()->getResult();
316
317
318 5
            if (empty($Customers)) {
319
                $app['monolog']->addDebug('search customer not found.');
320
            }
321
322 5
            $data = array();
323
324 5
            $formatTel = '%s-%s-%s';
325 5
            $formatName = '%s%s(%s%s)';
326 5 View Code Duplication
            foreach ($Customers as $Customer) {
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...
327 5
                $data[] = array(
328 5
                    'id' => $Customer->getId(),
329 5
                    'name' => sprintf($formatName, $Customer->getName01(), $Customer->getName02(), $Customer->getKana01(),
330 5
                        $Customer->getKana02()),
331 5
                    'tel' => sprintf($formatTel, $Customer->getTel01(), $Customer->getTel02(), $Customer->getTel03()),
332 5
                    'email' => $Customer->getEmail(),
333
                );
334
            }
335
336 5
            $event = new EventArgs(
337
                array(
338 5
                    'data' => $data,
339 5
                    'Customers' => $Customers,
340
                ),
341
                $request
342
            );
343 5
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_CUSTOMER_COMPLETE, $event);
344 5
            $data = $event->getArgument('data');
345
346 5
            return $app->json($data);
347
        }
348
    }
349
350
    /**
351
     * 顧客情報を検索する.
352
     *
353
     * @param Application $app
354
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
355
     * @param integer $page_no
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
356
     * @return \Symfony\Component\HttpFoundation\JsonResponse
357
     */
358 1
    public function searchCustomerHtml(Application $app, Request $request, $page_no = null)
359
    {
360 1
        if ($request->isXmlHttpRequest()) {
361 1
            $app['monolog']->addDebug('search customer start.');
362 1
            $page_count = $app['config']['default_page_count'];
363 1
            $session = $app['session'];
364
365 1
            if ('POST' === $request->getMethod()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
366
367 1
                $page_no = 1;
368
369
                $searchData = array(
370 1
                    'multi' => $request->get('search_word'),
371
                );
372
373 1
                $session->set('eccube.admin.order.customer.search', $searchData);
374 1
                $session->set('eccube.admin.order.customer.search.page_no', $page_no);
375
            } else {
376
                $searchData = (array)$session->get('eccube.admin.order.customer.search');
0 ignored issues
show
Coding Style introduced by
As per coding-style, a cast statement should be followed by a single space.
Loading history...
377
                if (is_null($page_no)) {
378
                    $page_no = intval($session->get('eccube.admin.order.customer.search.page_no'));
379
                } else {
380
                    $session->set('eccube.admin.order.customer.search.page_no', $page_no);
381
                }
382
            }
383
384 1
            $qb = $app['eccube.repository.customer']->getQueryBuilderBySearchData($searchData);
385
386 1
            $event = new EventArgs(
387
                array(
388 1
                    'qb' => $qb,
389 1
                    'data' => $searchData,
390
                ),
391
                $request
392
            );
393 1
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_CUSTOMER_SEARCH, $event);
394
395
            /** @var \Knp\Component\Pager\Pagination\SlidingPagination $pagination */
396 1
            $pagination = $app['paginator']()->paginate(
397
                $qb,
398
                $page_no,
399
                $page_count,
400 1
                array('wrap-queries' => true)
401
            );
402
403
            /** @var $Customers \Eccube\Entity\Customer[] */
404 1
            $Customers = $pagination->getItems();
405
406 1
            if (empty($Customers)) {
407
                $app['monolog']->addDebug('search customer not found.');
408
            }
409
410 1
            $data = array();
411
412 1
            $formatTel = '%s-%s-%s';
413 1
            $formatName = '%s%s(%s%s)';
414 1 View Code Duplication
            foreach ($Customers as $Customer) {
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...
415 1
                $data[] = array(
416 1
                    'id' => $Customer->getId(),
417 1
                    'name' => sprintf($formatName, $Customer->getName01(), $Customer->getName02(), $Customer->getKana01(),
418 1
                        $Customer->getKana02()),
419 1
                    'tel' => sprintf($formatTel, $Customer->getTel01(), $Customer->getTel02(), $Customer->getTel03()),
420 1
                    'email' => $Customer->getEmail(),
421
                );
422
            }
423
424 1
            $event = new EventArgs(
425
                array(
426 1
                    'data' => $data,
427 1
                    'Customers' => $pagination,
428
                ),
429
                $request
430
            );
431 1
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_CUSTOMER_COMPLETE, $event);
432 1
            $data = $event->getArgument('data');
433
434 1
            return $app->render('Order/search_customer.twig', array(
435 1
                'data' => $data,
436 1
                'pagination' => $pagination,
437
            ));
438
        }
439
    }
440
441
    /**
442
     * 顧客情報を検索する.
443
     *
444
     * @param Application $app
445
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
446
     * @return \Symfony\Component\HttpFoundation\JsonResponse
447
     */
448 3
    public function searchCustomerById(Application $app, Request $request)
449
    {
450 3
        if ($request->isXmlHttpRequest()) {
451 3
            $app['monolog']->addDebug('search customer by id start.');
452
453
            /** @var $Customer \Eccube\Entity\Customer */
454 3
            $Customer = $app['eccube.repository.customer']
455 3
                ->find($request->get('id'));
456
457 3
            $event = new EventArgs(
458
                array(
459 3
                    'Customer' => $Customer,
460
                ),
461
                $request
462
            );
463 3
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_CUSTOMER_BY_ID_INITIALIZE, $event);
464
465 3
            if (is_null($Customer)) {
466
                $app['monolog']->addDebug('search customer by id not found.');
467
468
                return $app->json(array(), 404);
469
            }
470
471 3
            $app['monolog']->addDebug('search customer by id found.');
472
473
            $data = array(
474 3
                'id' => $Customer->getId(),
475 3
                'name01' => $Customer->getName01(),
476 3
                'name02' => $Customer->getName02(),
477 3
                'kana01' => $Customer->getKana01(),
478 3
                'kana02' => $Customer->getKana02(),
479 3
                'zip01' => $Customer->getZip01(),
480 3
                'zip02' => $Customer->getZip02(),
481 3
                'pref' => is_null($Customer->getPref()) ? null : $Customer->getPref()->getId(),
482 3
                'addr01' => $Customer->getAddr01(),
483 3
                'addr02' => $Customer->getAddr02(),
484 3
                'email' => $Customer->getEmail(),
485 3
                'tel01' => $Customer->getTel01(),
486 3
                'tel02' => $Customer->getTel02(),
487 3
                'tel03' => $Customer->getTel03(),
488 3
                'fax01' => $Customer->getFax01(),
489 3
                'fax02' => $Customer->getFax02(),
490 3
                'fax03' => $Customer->getFax03(),
491 3
                'company_name' => $Customer->getCompanyName(),
492
            );
493
494 3
            $event = new EventArgs(
495
                array(
496 3
                    'data' => $data,
497 3
                    'Customer' => $Customer,
498
                ),
499
                $request
500
            );
501 3
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_CUSTOMER_BY_ID_COMPLETE, $event);
502 3
            $data = $event->getArgument('data');
503
504 3
            return $app->json($data);
505
        }
506
    }
507
508 3
    public function searchProduct(Application $app, Request $request, $page_no = null)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
509
    {
510 3
        if ($request->isXmlHttpRequest()) {
511 3
            $app['monolog']->addDebug('search product start.');
512 3
            $page_count = $app['config']['default_page_count'];
513 3
            $session = $app['session'];
514
515 3
            if ('POST' === $request->getMethod()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
516
517 3
                $page_no = 1;
518
519
                $searchData = array(
520 3
                    'id' => $request->get('id'),
521
                );
522
523 3
                if ($categoryId = $request->get('category_id')) {
524
                    $Category = $app['eccube.repository.category']->find($categoryId);
525
                    $searchData['category_id'] = $Category;
526
                }
527
528 3
                $session->set('eccube.admin.order.product.search', $searchData);
529 3
                $session->set('eccube.admin.order.product.search.page_no', $page_no);
530
            } else {
531
                $searchData = (array)$session->get('eccube.admin.order.product.search');
0 ignored issues
show
Coding Style introduced by
As per coding-style, a cast statement should be followed by a single space.
Loading history...
532
                if (is_null($page_no)) {
533
                    $page_no = intval($session->get('eccube.admin.order.product.search.page_no'));
534
                } else {
535
                    $session->set('eccube.admin.order.product.search.page_no', $page_no);
536
                }
537
            }
538
539 3
            $qb = $app['eccube.repository.product']
540 3
                ->getQueryBuilderBySearchDataForAdmin($searchData);
541
542 3
            $event = new EventArgs(
543
                array(
544 3
                    'qb' => $qb,
545 3
                    'searchData' => $searchData,
546
                ),
547
                $request
548
            );
549 3
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_PRODUCT_SEARCH, $event);
550
551
            /** @var \Knp\Component\Pager\Pagination\SlidingPagination $pagination */
552 3
            $pagination = $app['paginator']()->paginate(
553
                $qb,
554
                $page_no,
555
                $page_count,
556 3
                array('wrap-queries' => true)
557
            );
558
559
            /** @var $Products \Eccube\Entity\Product[] */
560 3
            $Products = $pagination->getItems();
561
562 3
            if (empty($Products)) {
563
                $app['monolog']->addDebug('search product not found.');
564
            }
565
566 3
            $forms = array();
567 3
            foreach ($Products as $Product) {
568
                /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
569 3
                $builder = $app['form.factory']->createNamedBuilder('', AddCartType::class, null, array(
570 3
                    'product' => $Product,
571
                ));
572 3
                $addCartForm = $builder->getForm();
573 3
                $forms[$Product->getId()] = $addCartForm->createView();
574
            }
575
576 3
            $event = new EventArgs(
577
                array(
578 3
                    'forms' => $forms,
579 3
                    'Products' => $Products,
580 3
                    'pagination' => $pagination,
581
                ),
582
                $request
583
            );
584 3
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_PRODUCT_COMPLETE, $event);
585
586 3
            return $app->render('Order/search_product.twig', array(
587 3
                'forms' => $forms,
588 3
                'Products' => $Products,
589 3
                'pagination' => $pagination,
590
            ));
591
        }
592
    }
593
594 7
    protected function newOrder()
595
    {
596 7
        $Order = new \Eccube\Entity\Order();
597 7
        $Shipping = new \Eccube\Entity\Shipping();
598 7
        $Order->addShipping($Shipping);
599 7
        $Shipping->setOrder($Order);
600
601 7
        return $Order;
602
    }
603
604
    /**
605
     * フォームからの入直内容に基づいて、受注情報の再計算を行う
606
     *
607
     * @param $app
608
     * @param $Order
609
     */
610
    protected function calculate($app, \Eccube\Entity\Order $Order)
611
    {
612
        $taxtotal = 0;
0 ignored issues
show
Unused Code introduced by
$taxtotal 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...
613
        $subtotal = 0;
0 ignored issues
show
Unused Code introduced by
$subtotal 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...
614
615
        // 受注明細データの税・小計を再計算
616
        /** @var $OrderDetails \Eccube\Entity\OrderDetail[] */
617
        $OrderDetails = $Order->getOrderDetails();
618 View Code Duplication
        foreach ($OrderDetails as $OrderDetail) {
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...
Coding Style introduced by
Blank line found at start of control structure
Loading history...
619
620
            // 税
621
            $tax = $app['eccube.service.tax_rule']
622
                ->calcTax($OrderDetail->getPrice(), $OrderDetail->getTaxRate(), $OrderDetail->getTaxRule());
623
            $OrderDetail->setPriceIncTax($OrderDetail->getPrice() + $tax);
624
625
            // $taxtotal += $tax * $OrderDetail->getQuantity();
0 ignored issues
show
Unused Code Comprehensibility introduced by
44% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
626
627
            // // 小計
628
            // $subtotal += $OrderDetail->getTotalPrice();
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
629
        }
630
631
        $shippings = $Order->getShippings();
632
        /** @var \Eccube\Entity\Shipping $Shipping */
633
        foreach ($shippings as $Shipping) {
634
            $Shipping->setDelFlg(Constant::DISABLED);
635
        }
636
637
        // // 受注データの税・小計・合計を再計算
638
        // $Order->setTax($taxtotal);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
639
        // $Order->setSubtotal($subtotal);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
640
        // $Order->setTotal($subtotal + $Order->getCharge() + $Order->getDeliveryFeeTotal() - $Order->getDiscount());
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
641
        // // お支払い合計は、totalと同一金額(2系ではtotal - point)
642
        // $Order->setPaymentTotal($Order->getTotal());
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
643
644
        // 集計は,この1行でいけるはず
645
        // プラグインで Strategy をセットしたりする
646
        $app['eccube.service.calculate']($Order, $Order->getCustomer())->calculate();
647
    }
648
649
    /**
650
     * 受注ステータスに応じて, 受注日/入金日/発送日を更新する,
651
     * 発送済ステータスが設定された場合は, お届け先情報の発送日も更新を行う.
652
     *
653
     * 編集の場合
654
     * - 受注ステータスが他のステータスから発送済へ変更された場合に発送日を更新
655
     * - 受注ステータスが他のステータスから入金済へ変更された場合に入金日を更新
656
     *
657
     * 新規登録の場合
658
     * - 受注日を更新
659
     * - 受注ステータスが発送済に設定された場合に発送日を更新
660
     * - 受注ステータスが入金済に設定された場合に入金日を更新
661
     *
662
     *
663
     * @param $app
664
     * @param $TargetOrder
665
     * @param $OriginOrder
666
     */
667 11
    protected function updateDate($app, $TargetOrder, $OriginOrder)
668
    {
669 11
        $dateTime = new \DateTime();
670
671
        // 編集
672 11
        if ($TargetOrder->getId()) {
673
            // 発送済
674 7
            if ($TargetOrder->getOrderStatus()->getId() == $app['config']['order_deliv']) {
675
                // 編集前と異なる場合のみ更新
676
                if ($TargetOrder->getOrderStatus()->getId() != $OriginOrder->getOrderStatus()->getId()) {
677
                    $TargetOrder->setCommitDate($dateTime);
678
                    // お届け先情報の発送日も更新する.
679
                    $Shippings = $TargetOrder->getShippings();
680
                    foreach ($Shippings as $Shipping) {
681
                        $Shipping->setShippingCommitDate($dateTime);
682
                    }
683
                }
684
                // 入金済
685 7
            } elseif ($TargetOrder->getOrderStatus()->getId() == $app['config']['order_pre_end']) {
686
                // 編集前と異なる場合のみ更新
687
                if ($TargetOrder->getOrderStatus()->getId() != $OriginOrder->getOrderStatus()->getId()) {
688 7
                    $TargetOrder->setPaymentDate($dateTime);
689
                }
690
            }
691
            // 新規
692
        } else {
693
            // 発送済
694 4
            if ($TargetOrder->getOrderStatus()->getId() == $app['config']['order_deliv']) {
695
                $TargetOrder->setCommitDate($dateTime);
696
                // お届け先情報の発送日も更新する.
697
                $Shippings = $TargetOrder->getShippings();
698
                foreach ($Shippings as $Shipping) {
699
                    $Shipping->setShippingCommitDate($dateTime);
700
                }
701
                // 入金済
702 4
            } elseif ($TargetOrder->getOrderStatus()->getId() == $app['config']['order_pre_end']) {
703
                $TargetOrder->setPaymentDate($dateTime);
704
            }
705
            // 受注日時
706 4
            $TargetOrder->setOrderDate($dateTime);
707
        }
708
    }
709
}
710