Completed
Push — master ( 5cfee9...046c10 )
by chihiro
468:11 queued 456:50
created

EditController::searchCustomerById()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 59
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 39
CRAP Score 4.0058

Importance

Changes 0
Metric Value
dl 0
loc 59
ccs 39
cts 42
cp 0.9286
rs 8.9846
c 0
b 0
f 0
cc 4
eloc 41
nc 4
nop 2
crap 4.0058

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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