Failed Conditions
Push — experimental/3.1 ( 0c67c0...62371a )
by chihiro
51:52
created

EditController   F

Complexity

Total Complexity 49

Size/Duplication

Total Lines 653
Duplicated Lines 12.25 %

Coupling/Cohesion

Components 0
Dependencies 11

Test Coverage

Coverage 83.77%

Importance

Changes 0
Metric Value
dl 80
loc 653
ccs 253
cts 302
cp 0.8377
rs 3.903
c 0
b 0
f 0
wmc 49
lcom 0
cbo 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
D index() 19 217 14
A searchCustomer() 9 54 4
B searchCustomerHtml() 9 82 6
B searchCustomerById() 0 59 4
C searchProduct() 31 85 7
A newOrder() 0 9 1
B calculate() 12 38 3
D updateDate() 0 42 10

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like EditController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use EditController, and based on these observations, apply Extract Interface, too.

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\Master\DeviceType;
31
use Eccube\Entity\ShipmentItem;
32
use Eccube\Event\EccubeEvents;
33
use Eccube\Event\EventArgs;
34
use Eccube\Form\Type\AddCartType;
35
use Eccube\Form\Type\Admin\OrderType;
36
use Eccube\Form\Type\Admin\SearchCustomerType;
37
use Eccube\Form\Type\Admin\SearchProductType;
38
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
39
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
40
use Symfony\Component\Form\FormError;
41
use Symfony\Component\HttpFoundation\Request;
42
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
43
44
/**
45
 * TODO 管理画面のルーティングは動的に行う.おそらくコントローラのディレクトリをフロント/管理で分ける必要がある
46
 *
47
 * @Route("/admin/order")
48
 */
49
class EditController extends AbstractController
50
{
51
52
    /**
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...
53
     * 受注登録/編集画面.
54
     *
55
     * @Route("/edit", name="admin_order_new")
56
     * @Route("/{id}/edit", requirements={"id" = "\d+"}, name="admin_order_edit")
57
     * @Template("Order/edit.twig")
58
     *
59 17
     * TODO templateアノテーションを利用するかどうか検討.http://symfony.com/doc/current/best_practices/controllers.html
60
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
61
    public function index(Application $app, Request $request, $id = null)
62 17
    {
63 17
        /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */
64 17
        $softDeleteFilter = $app['orm.em']->getFilters()->getFilter('soft_delete');
65
        $softDeleteFilter->setExcludes(array(
66
            'Eccube\Entity\ProductClass',
67
            'Eccube\Entity\Product',
68 17
        ));
69 17
70
        $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...
71 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...
72
73 7
        if (is_null($id)) {
74
            // 空のエンティティを作成.
75 10
            $TargetOrder = $this->newOrder($app);
76 10
        } else {
77
            $TargetOrder = $app['eccube.repository.order']->find($id);
78
            if (is_null($TargetOrder)) {
79
                throw new NotFoundHttpException();
80
            }
81
        }
82 17
83 17
        // 編集前の受注情報を保持
84
        $OriginOrder = clone $TargetOrder;
85 17
        // $OriginalOrderDetails = new ArrayCollection();
86 17
        // 編集前のお届け先情報を保持
87
        $OriginalShippings = new ArrayCollection();
0 ignored issues
show
Unused Code introduced by
$OriginalShippings 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...
88
        // 編集前のお届け先のアイテム情報を保持
89 17
        $OriginalShipmentItems = new ArrayCollection();
90 17
91
        // 編集前の情報を保持
92 17
        foreach ($TargetOrder->getShipmentItems() as $tmpShipmentItem) {
93
            $OriginalShipmentItems->add($tmpShipmentItem);
94 17
        }
95 17
96 17
        $builder = $app['form.factory']
97 17
            ->createBuilder(OrderType::class, $TargetOrder);
98
99
        $event = new EventArgs(
100
            array(
101 17
                'builder' => $builder,
102
                'OriginOrder' => $OriginOrder,
103 17
                'TargetOrder' => $TargetOrder,
104 17
                // 'OriginOrderDetails' => $OriginalOrderDetails,
105
            ),
106 17
            $request
107 11
        );
108
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_INDEX_INITIALIZE, $event);
109 11
110 11
        $form = $builder->getForm();
111 11
        $form->handleRequest($request);
112 11
113
        if ($form->isSubmitted()) {
114
            $event = new EventArgs(
115
                array(
116 11
                    'builder' => $builder,
117
                    'OriginOrder' => $OriginOrder,
118
                    'TargetOrder' => $TargetOrder,
119
                    // 'OriginOrderDetails' => $OriginalOrderDetails,
120
                ),
121
                $request
122
            );
123 11
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_INDEX_PROGRESS, $event);
124
125
            // FIXME 税額計算は CalculateService で処理する. ここはテストを通すための暫定処理
126 11
            // see EditControllerTest::testOrderProcessingWithTax
127 11
            // $OrderDetails = $TargetOrder->getOrderDetails();
128
            // $taxtotal = 0;
129 11
            // foreach ($OrderDetails as $OrderDetail) {
130
            //     $tax = $app['eccube.service.tax_rule']
131
            //         ->calcTax($OrderDetail->getPrice(), $OrderDetail->getTaxRate(), $OrderDetail->getTaxRule());
132 11
            //     $OrderDetail->setPriceIncTax($OrderDetail->getPrice() + $tax);
133
134
            //     $taxtotal += $tax * $OrderDetail->getQuantity();
135 11
            // }
136
            // $TargetOrder->setTax($taxtotal);
137 11
138
            // 入力情報にもとづいて再計算.
139
            // TODO 購入フローのように、明細の自動生成をどこまで行うか検討する. 単純集計でよいような気がする
140
            // 集計は,この1行でいけるはず
141 11
            // プラグインで Strategy をセットしたりする
142
            // TODO 編集前のOrder情報が必要かもしれない
143
            // TODO 手数料, 値引きの集計は未実装
144 11
            $app['eccube.service.calculate']($TargetOrder, $TargetOrder->getCustomer())->calculate();
145 7
146 11
            // 登録ボタン押下
147
            switch ($request->get('mode')) {
148
                case 'register':
149
                    log_info('受注登録開始', array($TargetOrder->getId()));
150
151 11
                    // TODO 在庫の有無や販売制限数のチェックなども行う必要があるため、完了処理もcaluclatorのように抽象化できないか検討する.
152 4
                    if ($TargetOrder->getTotal() > $app['config']['max_total_fee']) {
153 4
                        log_info('受注登録入力チェックエラー', array($TargetOrder->getId()));
154
                        $form['charge']->addError(new FormError('合計金額の上限を超えております。'));
155 4
                    } elseif ($form->isValid()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
156 4
157 4
                        $BaseInfo = $app['eccube.repository.base_info']->get();
0 ignored issues
show
Unused Code introduced by
$BaseInfo 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...
158 4
159 4
                        // TODO 後続にある会員情報の更新のように、完了処理もcaluclatorのように抽象化できないか検討する.
160 4
                        // 受注日/発送日/入金日の更新.
161 4
                        $this->updateDate($app, $TargetOrder, $OriginOrder);
162
163 4
                        // 画面上で削除された明細をremove
164 4
                        foreach ($OriginalShipmentItems as $ShipmentItem) {
165
                            if (false === $TargetOrder->getShipmentItems()->contains($ShipmentItem)) {
166
                                $ShipmentItem->setOrder(null);
167
                            }
168
                        }
169 7
170 7
                        foreach ($TargetOrder->getShipmentItems() as $ShipmentItem) {
171 4
                            $ShipmentItem->setOrder($TargetOrder);
172 7
                        }
173
174 7
                        // TODO 手数料, 値引きの集計は CalculateService で
175 7
                        $TargetOrder->setDeliveryFeeTotal($TargetOrder->calculateDeliveryFeeTotal()); // FIXME
176 7
                        $app['orm.em']->persist($TargetOrder);
177 7
                        $app['orm.em']->flush();
178 7
179 7
                        // TODO 集計系に移動
180 7
//                        if ($Customer) {
181
//                            // 会員の場合、購入回数、購入金額などを更新
182
//                            $app['eccube.repository.customer']->updateBuyData($app, $Customer, $TargetOrder->getOrderStatus()->getId());
183
//                        }
184
185 11
                        $event = new EventArgs(
186 11
                            array(
187
                                'form' => $form,
188
                                'OriginOrder' => $OriginOrder,
189
                                'TargetOrder' => $TargetOrder,
190
                                // 'OriginOrderDetails' => $OriginalOrderDetails,
191
                                //'Customer' => $Customer,
192
                            ),
193
                            $request
194 11
                        );
195
                        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_INDEX_COMPLETE, $event);
196 11
197 11
                        $app->addSuccess('admin.order.save.complete', 'admin');
198 11
199 11
                        log_info('受注登録完了', array($TargetOrder->getId()));
200
201
                        return $app->redirect($app->url('admin_order_edit', array('id' => $TargetOrder->getId())));
202
                    }
203
204 11
                    break;
205
206 11 View Code Duplication
                case 'add_delivery':
207
                    // お届け先情報の新規追加
208 11
209
                    $form = $builder->getForm();
210 11
211
                    $Shipping = new \Eccube\Entity\Shipping();
212
                    $TargetOrder->addShipping($Shipping);
213
214
                    $Shipping->setOrder($TargetOrder);
215
216
                    $form->setData($TargetOrder);
217
218
                    break;
219
220
                default:
221
                    break;
222
            }
223
        }
224
225
        // 会員検索フォーム
226
        $builder = $app['form.factory']
227
            ->createBuilder(SearchCustomerType::class);
228
229
        $event = new EventArgs(
230
            array(
231
                'builder' => $builder,
232
                'OriginOrder' => $OriginOrder,
233
                'TargetOrder' => $TargetOrder,
234
                // 'OriginOrderDetails' => $OriginalOrderDetails,
235 6
            ),
236 6
            $request
237
        );
238 6
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_CUSTOMER_INITIALIZE, $event);
239
240 6
        $searchCustomerModalForm = $builder->getForm();
241 6
242 6
        // 商品検索フォーム
243 6
        $builder = $app['form.factory']
244
            ->createBuilder(SearchProductType::class);
245
246
        $event = new EventArgs(
247 6
            array(
248
                'builder' => $builder,
249 6
                'OriginOrder' => $OriginOrder,
250
                'TargetOrder' => $TargetOrder,
251
                // 'OriginOrderDetails' => $OriginalOrderDetails,
252 6
            ),
253 6
            $request
254
        );
255 6
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_PRODUCT_INITIALIZE, $event);
256
257 6
        $searchProductModalForm = $builder->getForm();
258 6
259 6
        // 配送業者のお届け時間
260 6
        $times = array();
261
        $deliveries = $app['eccube.repository.delivery']->findAll();
262 View Code Duplication
        foreach ($deliveries as $Delivery) {
263
            $deliveryTiems = $Delivery->getDeliveryTimes();
264 6
            foreach ($deliveryTiems as $DeliveryTime) {
265
                $times[$Delivery->getId()][$DeliveryTime->getId()] = $DeliveryTime->getDeliveryTime();
266 6
            }
267
        }
268
269 6
        return [
270 6
            'form' => $form->createView(),
271 6
            'searchCustomerModalForm' => $searchCustomerModalForm->createView(),
272 6
            'searchProductModalForm' => $searchProductModalForm->createView(),
273 6
            'Order' => $TargetOrder,
274 6
            'id' => $id,
275
            'shippingDeliveryTimes' => $app['serializer']->serialize($times, 'json'),
276
        ];
277
    }
278
279 6
    /**
280 6
     * 顧客情報を検索する.
281 6
     *
282 6
     * @param Application $app
283 6
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
284 6
     * @return \Symfony\Component\HttpFoundation\JsonResponse
285
     */
286
    public function searchCustomer(Application $app, Request $request)
287
    {
288
        if ($request->isXmlHttpRequest()) {
289
            $app['monolog']->addDebug('search customer start.');
290
291
            $searchData = array(
292
                'multi' => $request->get('search_word'),
293
            );
294
295 5
            $qb = $app['eccube.repository.customer']->getQueryBuilderBySearchData($searchData);
296
297 5
            $event = new EventArgs(
298 5
                array(
299
                    'qb' => $qb,
300
                    'data' => $searchData,
301 5
                ),
302
                $request
303
            );
304 5
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_CUSTOMER_SEARCH, $event);
305
306 5
            $Customers = $qb->getQuery()->getResult();
307
308 5
309 5
            if (empty($Customers)) {
310
                $app['monolog']->addDebug('search customer not found.');
311
            }
312
313 5
            $data = array();
314
315 5
            $formatTel = '%s-%s-%s';
316
            $formatName = '%s%s(%s%s)';
317 View Code Duplication
            foreach ($Customers as $Customer) {
318 5
                $data[] = array(
319
                    'id' => $Customer->getId(),
320
                    'name' => sprintf($formatName, $Customer->getName01(), $Customer->getName02(), $Customer->getKana01(),
321
                        $Customer->getKana02()),
322 5
                    'tel' => sprintf($formatTel, $Customer->getTel01(), $Customer->getTel02(), $Customer->getTel03()),
323
                    'email' => $Customer->getEmail(),
324 5
                );
325 5
            }
326 5
327 5
            $event = new EventArgs(
328 5
                array(
329 5
                    'data' => $data,
330 5
                    'Customers' => $Customers,
331 5
                ),
332 5
                $request
333
            );
334
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_CUSTOMER_COMPLETE, $event);
335
            $data = $event->getArgument('data');
336 5
337
            return $app->json($data);
338 5
        }
339 5
    }
340
341
    /**
342
     * 顧客情報を検索する.
343 5
     *
344 5
     * @param Application $app
345
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
346 5
     * @param integer $page_no
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
347
     * @return \Symfony\Component\HttpFoundation\JsonResponse
348
     */
349
    public function searchCustomerHtml(Application $app, Request $request, $page_no = null)
350
    {
351
        if ($request->isXmlHttpRequest()) {
352
            $app['monolog']->addDebug('search customer start.');
353
            $page_count = $app['config']['default_page_count'];
354
            $session = $app['session'];
355
356
            if ('POST' === $request->getMethod()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
357
358 1
                $page_no = 1;
359
360 1
                $searchData = array(
361 1
                    'multi' => $request->get('search_word'),
362 1
                );
363 1
364
                $session->set('eccube.admin.order.customer.search', $searchData);
365 1
                $session->set('eccube.admin.order.customer.search.page_no', $page_no);
366
            } else {
367 1
                $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...
368
                if (is_null($page_no)) {
369
                    $page_no = intval($session->get('eccube.admin.order.customer.search.page_no'));
370 1
                } else {
371
                    $session->set('eccube.admin.order.customer.search.page_no', $page_no);
372
                }
373 1
            }
374 1
375
            $qb = $app['eccube.repository.customer']->getQueryBuilderBySearchData($searchData);
376
377
            $event = new EventArgs(
378
                array(
379
                    'qb' => $qb,
380
                    'data' => $searchData,
381
                ),
382
                $request
383
            );
384 1
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_CUSTOMER_SEARCH, $event);
385
386 1
            /** @var \Knp\Component\Pager\Pagination\SlidingPagination $pagination */
387
            $pagination = $app['paginator']()->paginate(
388 1
                $qb,
389 1
                $page_no,
390
                $page_count,
391
                array('wrap-queries' => true)
392
            );
393 1
394
            /** @var $Customers \Eccube\Entity\Customer[] */
395
            $Customers = $pagination->getItems();
396 1
397
            if (empty($Customers)) {
398
                $app['monolog']->addDebug('search customer not found.');
399
            }
400 1
401
            $data = array();
402
403
            $formatTel = '%s-%s-%s';
404 1
            $formatName = '%s%s(%s%s)';
405 View Code Duplication
            foreach ($Customers as $Customer) {
406 1
                $data[] = array(
407
                    'id' => $Customer->getId(),
408
                    'name' => sprintf($formatName, $Customer->getName01(), $Customer->getName02(), $Customer->getKana01(),
409
                        $Customer->getKana02()),
410 1
                    'tel' => sprintf($formatTel, $Customer->getTel01(), $Customer->getTel02(), $Customer->getTel03()),
411
                    'email' => $Customer->getEmail(),
412 1
                );
413 1
            }
414 1
415 1
            $event = new EventArgs(
416 1
                array(
417 1
                    'data' => $data,
418 1
                    'Customers' => $pagination,
419 1
                ),
420 1
                $request
421
            );
422
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_CUSTOMER_COMPLETE, $event);
423
            $data = $event->getArgument('data');
424 1
425
            return $app->render('Order/search_customer.twig', array(
426 1
                'data' => $data,
427 1
                'pagination' => $pagination,
428
            ));
429
        }
430
    }
431 1
432 1
    /**
433
     * 顧客情報を検索する.
434 1
     *
435 1
     * @param Application $app
436 1
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
437
     * @return \Symfony\Component\HttpFoundation\JsonResponse
438
     */
439
    public function searchCustomerById(Application $app, Request $request)
440
    {
441
        if ($request->isXmlHttpRequest()) {
442
            $app['monolog']->addDebug('search customer by id start.');
443
444
            /** @var $Customer \Eccube\Entity\Customer */
445
            $Customer = $app['eccube.repository.customer']
446
                ->find($request->get('id'));
447
448 3
            $event = new EventArgs(
449
                array(
450 3
                    'Customer' => $Customer,
451 3
                ),
452
                $request
453
            );
454 3
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_CUSTOMER_BY_ID_INITIALIZE, $event);
455 3
456
            if (is_null($Customer)) {
457 3
                $app['monolog']->addDebug('search customer by id not found.');
458
459 3
                return $app->json(array(), 404);
460
            }
461
462
            $app['monolog']->addDebug('search customer by id found.');
463 3
464
            $data = array(
465 3
                'id' => $Customer->getId(),
466
                'name01' => $Customer->getName01(),
467
                'name02' => $Customer->getName02(),
468
                'kana01' => $Customer->getKana01(),
469
                'kana02' => $Customer->getKana02(),
470
                'zip01' => $Customer->getZip01(),
471 3
                'zip02' => $Customer->getZip02(),
472
                'pref' => is_null($Customer->getPref()) ? null : $Customer->getPref()->getId(),
473
                'addr01' => $Customer->getAddr01(),
474 3
                'addr02' => $Customer->getAddr02(),
475 3
                'email' => $Customer->getEmail(),
476 3
                'tel01' => $Customer->getTel01(),
477 3
                'tel02' => $Customer->getTel02(),
478 3
                'tel03' => $Customer->getTel03(),
479 3
                'fax01' => $Customer->getFax01(),
480 3
                'fax02' => $Customer->getFax02(),
481 3
                'fax03' => $Customer->getFax03(),
482 3
                'company_name' => $Customer->getCompanyName(),
483 3
            );
484 3
485 3
            $event = new EventArgs(
486 3
                array(
487 3
                    'data' => $data,
488 3
                    'Customer' => $Customer,
489 3
                ),
490 3
                $request
491 3
            );
492
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_CUSTOMER_BY_ID_COMPLETE, $event);
493
            $data = $event->getArgument('data');
494 3
495
            return $app->json($data);
496 3
        }
497 3
    }
498
499
    public function searchProduct(Application $app, Request $request, $page_no = null)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
500
    {
501 3
        if ($request->isXmlHttpRequest()) {
502 3
            $app['monolog']->addDebug('search product start.');
503
            $page_count = $app['config']['default_page_count'];
504 3
            $session = $app['session'];
505
506 View Code Duplication
            if ('POST' === $request->getMethod()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
507
508 3
                $page_no = 1;
509
510 3
                $searchData = array(
511 3
                    'id' => $request->get('id'),
512 3
                );
513 3
514
                if ($categoryId = $request->get('category_id')) {
515 3
                    $Category = $app['eccube.repository.category']->find($categoryId);
516
                    $searchData['category_id'] = $Category;
517 3
                }
518
519
                $session->set('eccube.admin.order.product.search', $searchData);
520 3
                $session->set('eccube.admin.order.product.search.page_no', $page_no);
521
            } else {
522
                $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...
523 3
                if (is_null($page_no)) {
524
                    $page_no = intval($session->get('eccube.admin.order.product.search.page_no'));
525
                } else {
526
                    $session->set('eccube.admin.order.product.search.page_no', $page_no);
527
                }
528 3
            }
529 3
530
            $qb = $app['eccube.repository.product']
531
                ->getQueryBuilderBySearchDataForAdmin($searchData);
532
533
            $event = new EventArgs(
534
                array(
535
                    'qb' => $qb,
536
                    'searchData' => $searchData,
537
                ),
538
                $request
539 3
            );
540 3
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_PRODUCT_SEARCH, $event);
541
542 3
            /** @var \Knp\Component\Pager\Pagination\SlidingPagination $pagination */
543
            $pagination = $app['paginator']()->paginate(
544 3
                $qb,
545 3
                $page_no,
546
                $page_count,
547
                array('wrap-queries' => true)
548
            );
549 3
550
            /** @var $Products \Eccube\Entity\Product[] */
551
            $Products = $pagination->getItems();
552 3
553
            if (empty($Products)) {
554
                $app['monolog']->addDebug('search product not found.');
555
            }
556 3
557
            $forms = array();
558 View Code Duplication
            foreach ($Products as $Product) {
559
                /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
560 3
                $builder = $app['form.factory']->createNamedBuilder('', AddCartType::class, null, array(
561
                    'product' => $Product,
562 3
                ));
563
                $addCartForm = $builder->getForm();
564
                $forms[$Product->getId()] = $addCartForm->createView();
565
            }
566 3
567 3
            $event = new EventArgs(
568
                array(
569 3
                    'forms' => $forms,
570 3
                    'Products' => $Products,
571
                    'pagination' => $pagination,
572 3
                ),
573 3
                $request
574
            );
575
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_PRODUCT_COMPLETE, $event);
576 3
577
            return $app->render('Order/search_product.twig', array(
578 3
                'forms' => $forms,
579 3
                'Products' => $Products,
580 3
                'pagination' => $pagination,
581
            ));
582
        }
583
    }
584 3
585
    protected function newOrder(Application $app)
586 3
    {
587 3
        $Order = new \Eccube\Entity\Order();
588 3
        // device type
589 3
        $DeviceType = $app['eccube.repository.master.device_type']->find(DeviceType::DEVICE_TYPE_ADMIN);
590
        $Order->setDeviceType($DeviceType);
591
592
        return $Order;
593
    }
594 7
595
    /**
596 7
     * フォームからの入直内容に基づいて、受注情報の再計算を行う
597 7
     *
598 7
     * @param $app
599 7
     * @param $Order
600
     */
601 7
    protected function calculate($app, \Eccube\Entity\Order $Order)
602
    {
603
        $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...
604
        $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...
605
606
        // 受注明細データの税・小計を再計算
607
        /** @var $OrderDetails \Eccube\Entity\OrderDetail[] */
608
        $OrderDetails = $Order->getOrderDetails();
609 View Code Duplication
        foreach ($OrderDetails as $OrderDetail) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
610
611
            // 税
612
            $tax = $app['eccube.service.tax_rule']
613
                ->calcTax($OrderDetail->getPrice(), $OrderDetail->getTaxRate(), $OrderDetail->getTaxRule());
614
            $OrderDetail->setPriceIncTax($OrderDetail->getPrice() + $tax);
615
616
            // $taxtotal += $tax * $OrderDetail->getQuantity();
617
618
            // // 小計
619
            // $subtotal += $OrderDetail->getTotalPrice();
620
        }
621
622
        $shippings = $Order->getShippings();
623
        /** @var \Eccube\Entity\Shipping $Shipping */
624
        foreach ($shippings as $Shipping) {
625
            $Shipping->setDelFlg(Constant::DISABLED);
626
        }
627
628
        // // 受注データの税・小計・合計を再計算
629
        // $Order->setTax($taxtotal);
630
        // $Order->setSubtotal($subtotal);
631
        // $Order->setTotal($subtotal + $Order->getCharge() + $Order->getDeliveryFeeTotal() - $Order->getDiscount());
632
        // // お支払い合計は、totalと同一金額(2系ではtotal - point)
633
        // $Order->setPaymentTotal($Order->getTotal());
634
635
        // 集計は,この1行でいけるはず
636
        // プラグインで Strategy をセットしたりする
637
        $app['eccube.service.calculate']($Order, $Order->getCustomer())->calculate();
638
    }
639
640
    /**
641
     * 受注ステータスに応じて, 受注日/入金日/発送日を更新する,
642
     * 発送済ステータスが設定された場合は, お届け先情報の発送日も更新を行う.
643
     *
644
     * 編集の場合
645
     * - 受注ステータスが他のステータスから発送済へ変更された場合に発送日を更新
646
     * - 受注ステータスが他のステータスから入金済へ変更された場合に入金日を更新
647
     *
648
     * 新規登録の場合
649
     * - 受注日を更新
650
     * - 受注ステータスが発送済に設定された場合に発送日を更新
651
     * - 受注ステータスが入金済に設定された場合に入金日を更新
652
     *
653
     * @param $app
654
     * @param $TargetOrder
655
     * @param $OriginOrder
656
     *
657
     * TODO Service へ移動する
658
     */
659
    protected function updateDate($app, $TargetOrder, $OriginOrder)
660
    {
661
        $dateTime = new \DateTime();
662
663
        // 編集
664
        if ($TargetOrder->getId()) {
665
            // 発送済
666
            if ($TargetOrder->getOrderStatus()->getId() == $app['config']['order_deliv']) {
667 11
                // 編集前と異なる場合のみ更新
668
                if ($TargetOrder->getOrderStatus()->getId() != $OriginOrder->getOrderStatus()->getId()) {
669 11
                    $TargetOrder->setCommitDate($dateTime);
670
                    // お届け先情報の発送日も更新する.
671
                    $Shippings = $TargetOrder->getShippings();
672 11
                    foreach ($Shippings as $Shipping) {
673
                        $Shipping->setShippingCommitDate($dateTime);
674 7
                    }
675
                }
676
                // 入金済
677
            } elseif ($TargetOrder->getOrderStatus()->getId() == $app['config']['order_pre_end']) {
678
                // 編集前と異なる場合のみ更新
679
                if ($TargetOrder->getOrderStatus()->getId() != $OriginOrder->getOrderStatus()->getId()) {
680
                    $TargetOrder->setPaymentDate($dateTime);
681
                }
682
            }
683
            // 新規
684
        } else {
685 7
            // 発送済
686
            if ($TargetOrder->getOrderStatus()->getId() == $app['config']['order_deliv']) {
687
                $TargetOrder->setCommitDate($dateTime);
688 7
                // お届け先情報の発送日も更新する.
689
                $Shippings = $TargetOrder->getShippings();
690
                foreach ($Shippings as $Shipping) {
691
                    $Shipping->setShippingCommitDate($dateTime);
692
                }
693
                // 入金済
694 4
            } elseif ($TargetOrder->getOrderStatus()->getId() == $app['config']['order_pre_end']) {
695
                $TargetOrder->setPaymentDate($dateTime);
696
            }
697
            // 受注日時
698
            $TargetOrder->setOrderDate($dateTime);
699
        }
700
    }
701
}
702