Failed Conditions
Push — master ( d534e7...8e1a86 )
by Yangsin
285:02 queued 279:03
created

EditController   C

Complexity

Total Complexity 64

Size/Duplication

Total Lines 639
Duplicated Lines 3.44 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 88.19%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 22
loc 639
ccs 366
cts 415
cp 0.8819
rs 5.5458
wmc 64
lcom 1
cbo 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A searchCustomer() 0 54 4
B searchCustomerById() 0 59 4
C searchProduct() 0 85 7
A newOrder() 0 10 1
C calculate() 0 72 13
D updateDate() 0 42 10
F index() 22 270 25

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