Failed Conditions
Pull Request — experimental/3.1 (#2335)
by chihiro
50:55
created

EditController::searchProduct()   C

Complexity

Conditions 7
Paths 17

Size

Total Lines 85
Code Lines 53

Duplication

Lines 31
Ratio 36.47 %

Code Coverage

Tests 34
CRAP Score 7.2437

Importance

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