Completed
Push — fix#1932 ( afb4fd...142bd3 )
by NOBU
33:32
created

EditController::calculate()   C

Complexity

Conditions 13
Paths 9

Size

Total Lines 72
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 51
CRAP Score 13

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 13
eloc 51
c 2
b 0
f 1
nc 9
nop 2
dl 0
loc 72
ccs 51
cts 51
cp 1
crap 13
rs 5.5073

How to fix   Long Method    Complexity   

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
            'Eccube\Entity\Product',
46
        ));
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
        } 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 17
            $OriginalOrderDetails->add($OrderDetail);
67
        }
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
            ),
79
            $request
80
        );
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
                ),
95
                $request
96
            );
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()) {
112 11
                        $BaseInfo = $app['eccube.repository.base_info']->get();
113
114
                        // お支払い方法の更新
115 11
                        $TargetOrder->setPaymentMethod($TargetOrder->getPayment()->getMethod());
116
117
                        // 配送業者・お届け時間の更新
118 11
                        $Shippings = $TargetOrder->getShippings();
119 11
                        foreach ($Shippings as $Shipping) {
120 11
                            $Shipping->setShippingDeliveryName($Shipping->getDelivery()->getName());
121 11
                            if (!is_null($Shipping->getDeliveryTime())) {
122 11
                                $Shipping->setShippingDeliveryTime($Shipping->getDeliveryTime()->getDeliveryTime());
123
                            } else {
124 11
                                $Shipping->setShippingDeliveryTime(null);
125
                            }
126
                        }
127
128
129
                        // 受注日/発送日/入金日の更新.
130 11
                        $this->updateDate($app, $TargetOrder, $OriginOrder);
131
132
                        // 受注明細で削除されているものをremove
133 11
                        foreach ($OriginalOrderDetails as $OrderDetail) {
134 7
                            if (false === $TargetOrder->getOrderDetails()->contains($OrderDetail)) {
135 11
                                $app['orm.em']->remove($OrderDetail);
136
                            }
137
                        }
138
139
140 11
                        if ($BaseInfo->getOptionMultipleShipping() == Constant::ENABLED) {
141 4
                            foreach ($TargetOrder->getOrderDetails() as $OrderDetail) {
142
                                /** @var $OrderDetail \Eccube\Entity\OrderDetail */
143 4
                                $OrderDetail->setOrder($TargetOrder);
144
                            }
145
146
                            /** @var \Eccube\Entity\Shipping $Shipping */
147 4 View Code Duplication
                            foreach ($Shippings as $Shipping) {
148 4
                                $shipmentItems = $Shipping->getShipmentItems();
149
                                /** @var \Eccube\Entity\ShipmentItem $ShipmentItem */
150 4
                                foreach ($shipmentItems as $ShipmentItem) {
151 4
                                    $ShipmentItem->setOrder($TargetOrder);
152 4
                                    $ShipmentItem->setShipping($Shipping);
153 4
                                    $app['orm.em']->persist($ShipmentItem);
154
                                }
155 4
                                $Shipping->setOrder($TargetOrder);
156 4
                                $app['orm.em']->persist($Shipping);
157
                            }
158
                        } else {
159 7
                            foreach ($TargetOrder->getOrderDetails() as $OrderDetail) {
160 7
                                $OrderDetail->setOrder($TargetOrder);
161
162 7
                                $NewShipmentItem = new ShipmentItem();
163 7
                                $NewShipmentItems[] = $NewShipmentItem->copyProperties($OrderDetail);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$NewShipmentItems was never initialized. Although not strictly required by PHP, it is generally a good practice to add $NewShipmentItems = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
164
                            }
165
166
                            // 配送商品の更新. delete/insert.
167 7
                            $Shippings = $TargetOrder->getShippings();
168 7 View Code Duplication
                            foreach ($Shippings as $Shipping) {
169 7
                                $ShipmentItems = $Shipping->getShipmentItems();
170 7
                                foreach ($ShipmentItems as $ShipmentItem) {
171 7
                                    $app['orm.em']->remove($ShipmentItem);
172
                                }
173 7
                                $ShipmentItems->clear();
174 7
                                foreach ($NewShipmentItems as $NewShipmentItem) {
0 ignored issues
show
Bug introduced by
The variable $NewShipmentItems does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
175 7
                                    $NewShipmentItem->setShipping($Shipping);
176 7
                                    $ShipmentItems->add($NewShipmentItem);
177
                                }
178
                            }
179
                        }
180
181 11
                        $Customer = $TargetOrder->getCustomer();
182 11
                        if ($Customer) {
183
                            // 受注情報の会員情報を更新
184 11
                            $TargetOrder->setSex($Customer->getSex());
185 11
                            $TargetOrder->setJob($Customer->getJob());
186 11
                            $TargetOrder->setBirth($Customer->getBirth());
187
                        }
188 11
                        $app['orm.em']->persist($TargetOrder);
189 11
                        $app['orm.em']->flush();
190
191 11
                        if ($Customer) {
192
                            // 会員の場合、購入回数、購入金額などを更新
193 11
                            $app['eccube.repository.customer']->updateBuyData($app, $Customer, $TargetOrder->getOrderStatus()->getId());
194
                        }
195
196 11
                        $event = new EventArgs(
197
                            array(
198 11
                                'form' => $form,
199 11
                                'OriginOrder' => $OriginOrder,
200 11
                                'TargetOrder' => $TargetOrder,
201 11
                                'OriginOrderDetails' => $OriginalOrderDetails,
202 11
                                'Customer' => $Customer,
203
                            ),
204
                            $request
205
                        );
206 11
                        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_INDEX_COMPLETE, $event);
207
208 11
                        $app->addSuccess('admin.order.save.complete', 'admin');
209
210 11
                        log_info('受注登録完了', array($TargetOrder->getId()));
211
212 11
                        return $app->redirect($app->url('admin_order_edit', array('id' => $TargetOrder->getId())));
213
                    }
214
215 2
                    break;
216
217
                case 'add_delivery':
218
                    // お届け先情報の新規追加
219
220
                    $form = $builder->getForm();
221
222
                    $Shipping = new \Eccube\Entity\Shipping();
223
                    $Shipping->setDelFlg(Constant::DISABLED);
224
225
                    $TargetOrder->addShipping($Shipping);
226
227
                    $Shipping->setOrder($TargetOrder);
228
229
                    $form->setData($TargetOrder);
230
231
                    break;
232
233
                default:
234 2
                    break;
235
            }
236
        }
237
238
        // 会員検索フォーム
239 8
        $builder = $app['form.factory']
240 8
            ->createBuilder('admin_search_customer');
241
242 8
        $event = new EventArgs(
243
            array(
244 8
                'builder' => $builder,
245 8
                'OriginOrder' => $OriginOrder,
246 8
                'TargetOrder' => $TargetOrder,
247 8
                'OriginOrderDetails' => $OriginalOrderDetails,
248
            ),
249
            $request
250
        );
251 8
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_CUSTOMER_INITIALIZE, $event);
252
253 8
        $searchCustomerModalForm = $builder->getForm();
254
255
        // 商品検索フォーム
256 8
        $builder = $app['form.factory']
257 8
            ->createBuilder('admin_search_product');
258
259 8
        $event = new EventArgs(
260
            array(
261 8
                'builder' => $builder,
262 8
                'OriginOrder' => $OriginOrder,
263 8
                'TargetOrder' => $TargetOrder,
264 8
                'OriginOrderDetails' => $OriginalOrderDetails,
265
            ),
266
            $request
267
        );
268 8
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_PRODUCT_INITIALIZE, $event);
269
270 8
        $searchProductModalForm = $builder->getForm();
271
272
        // 配送業者のお届け時間
273 8
        $times = array();
274 8
        $deliveries = $app['eccube.repository.delivery']->findAll();
275 8
        foreach ($deliveries as $Delivery) {
276 8
            $deliveryTiems = $Delivery->getDeliveryTimes();
277 8
            foreach ($deliveryTiems as $DeliveryTime) {
278 8
                $times[$Delivery->getId()][$DeliveryTime->getId()] = $DeliveryTime->getDeliveryTime();
279
            }
280
        }
281
282 8
        return $app->render('Order/edit.twig', array(
283 8
            'form' => $form->createView(),
284 8
            'searchCustomerModalForm' => $searchCustomerModalForm->createView(),
285 8
            'searchProductModalForm' => $searchProductModalForm->createView(),
286 8
            'Order' => $TargetOrder,
287 8
            'id' => $id,
288 8
            'shippingDeliveryTimes' => $app['serializer']->serialize($times, 'json'),
289
        ));
290
    }
291
292
    /**
293
     * 顧客情報を検索する.
294
     *
295
     * @param  Application                                    $app
0 ignored issues
show
introduced by
Expected 1 spaces after parameter type; 36 found
Loading history...
296
     * @param  Request                                        $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 40 found
Loading history...
297
     * @return \Symfony\Component\HttpFoundation\JsonResponse
298
     */
299 3
    public function searchCustomer(Application $app, Request $request)
300
    {
301 3
        if ($request->isXmlHttpRequest()) {
302 3
            $app['monolog']->addDebug('search customer start.');
303
304
            $searchData = array(
305 3
                'multi' => $request->get('search_word'),
306
            );
307
308 3
            $qb = $app['eccube.repository.customer']->getQueryBuilderBySearchData($searchData);
309
310 3
            $event = new EventArgs(
311
                array(
312 3
                    'qb' => $qb,
313 3
                    'data' => $searchData,
314
                ),
315
                $request
316
            );
317 3
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_CUSTOMER_SEARCH, $event);
318
319 3
            $Customers = $qb->getQuery()->getResult();
320
321
322 3
            if (empty($Customers)) {
323
                $app['monolog']->addDebug('search customer not found.');
324
            }
325
326 3
            $data = array();
327
328 3
            $formatTel = '%s-%s-%s';
329 3
            $formatName = '%s%s(%s%s)';
330 3
            foreach ($Customers as $Customer) {
331 3
                $data[] = array(
332 3
                    'id' => $Customer->getId(),
333 3
                    'name' => sprintf($formatName, $Customer->getName01(), $Customer->getName02(), $Customer->getKana01(),
334 3
                        $Customer->getKana02()),
335 3
                    'tel' => sprintf($formatTel, $Customer->getTel01(), $Customer->getTel02(), $Customer->getTel03()),
336 3
                    'email' => $Customer->getEmail(),
337
                );
338
            }
339
340 3
            $event = new EventArgs(
341
                array(
342 3
                    'data' => $data,
343 3
                    'Customers' => $Customers,
344
                ),
345
                $request
346
            );
347 3
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_CUSTOMER_COMPLETE, $event);
348 3
            $data = $event->getArgument('data');
349
350 3
            return $app->json($data);
351
        }
352
    }
353
354
    /**
355
     * 顧客情報を検索する.
356
     *
357
     * @param  Application                                    $app
0 ignored issues
show
introduced by
Expected 1 spaces after parameter type; 36 found
Loading history...
358
     * @param  Request                                        $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 40 found
Loading history...
359
     * @return \Symfony\Component\HttpFoundation\JsonResponse
360
     */
361 3
    public function searchCustomerById(Application $app, Request $request)
362
    {
363 3
        if ($request->isXmlHttpRequest()) {
364 3
            $app['monolog']->addDebug('search customer by id start.');
365
366
            /** @var $Customer \Eccube\Entity\Customer */
367 3
            $Customer = $app['eccube.repository.customer']
368 3
                ->find($request->get('id'));
369
370 3
            $event = new EventArgs(
371
                array(
372 3
                    'Customer' => $Customer,
373
                ),
374
                $request
375
            );
376 3
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_CUSTOMER_BY_ID_INITIALIZE, $event);
377
378 3
            if (is_null($Customer)) {
379
                $app['monolog']->addDebug('search customer by id not found.');
380
381
                return $app->json(array(), 404);
382
            }
383
384 3
            $app['monolog']->addDebug('search customer by id found.');
385
386
            $data = array(
387 3
                'id' => $Customer->getId(),
388 3
                'name01' => $Customer->getName01(),
389 3
                'name02' => $Customer->getName02(),
390 3
                'kana01' => $Customer->getKana01(),
391 3
                'kana02' => $Customer->getKana02(),
392 3
                'zip01' => $Customer->getZip01(),
393 3
                'zip02' => $Customer->getZip02(),
394 3
                'pref' => is_null($Customer->getPref()) ? null : $Customer->getPref()->getId(),
395 3
                'addr01' => $Customer->getAddr01(),
396 3
                'addr02' => $Customer->getAddr02(),
397 3
                'email' => $Customer->getEmail(),
398 3
                'tel01' => $Customer->getTel01(),
399 3
                'tel02' => $Customer->getTel02(),
400 3
                'tel03' => $Customer->getTel03(),
401 3
                'fax01' => $Customer->getFax01(),
402 3
                'fax02' => $Customer->getFax02(),
403 3
                'fax03' => $Customer->getFax03(),
404 3
                'company_name' => $Customer->getCompanyName(),
405
            );
406
407 3
            $event = new EventArgs(
408
                array(
409 3
                    'data' => $data,
410 3
                    'Customer' => $Customer,
411
                ),
412
                $request
413
            );
414 3
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_CUSTOMER_BY_ID_COMPLETE, $event);
415 3
            $data = $event->getArgument('data');
416
417 3
            return $app->json($data);
418
        }
419
    }
420
421 3
    public function searchProduct(Application $app, Request $request, $page_no = null)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
422
    {
423 3
        if ($request->isXmlHttpRequest()) {
424 3
            $app['monolog']->addDebug('search product start.');
425 3
            $page_count = $app['config']['default_page_count'];
426 3
            $session = $app['session'];
427
428 3
            if ('POST' === $request->getMethod()) {
429 3
                $page_no = 1;
430
431
                $searchData = array(
432 3
                    'name' => $request->get('id'),
433
                );
434
435 3
                if ($categoryId = $request->get('category_id')) {
436
                    $Category = $app['eccube.repository.category']->find($categoryId);
437
                    $searchData['category_id'] = $Category;
438
                }
439
440 3
                $session->set('eccube.admin.order.product.search', $searchData);
441 3
                $session->set('eccube.admin.order.product.search.page_no', $page_no);
442
            } else {
443
                $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...
444
                if (is_null($page_no)) {
445
                    $page_no = intval($session->get('eccube.admin.order.product.search.page_no'));
446
                } else {
447
                    $session->set('eccube.admin.order.product.search.page_no', $page_no);
448
                }
449
            }
450
451 3
            $qb = $app['eccube.repository.product']
452 3
                ->getQueryBuilderBySearchData($searchData);
453
454 3
            $event = new EventArgs(
455
                array(
456 3
                    'qb' => $qb,
457 3
                    'searchData' => $searchData,
458
                ),
459
                $request
460
            );
461 3
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_PRODUCT_SEARCH, $event);
462
463
            /** @var \Knp\Component\Pager\Pagination\SlidingPagination $pagination */
464 3
            $pagination = $app['paginator']()->paginate(
465
                $qb,
466
                $page_no,
467
                $page_count,
468 3
                array('wrap-queries' => true)
469
            );
470
471
            /** @var $Products \Eccube\Entity\Product[] */
472 3
            $Products = $pagination->getItems();
473
474 3
            if (empty($Products)) {
475 3
                $app['monolog']->addDebug('search product not found.');
476
            }
477
478 3
            $forms = array();
479 3
            foreach ($Products as $Product) {
480
                /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
481
                $builder = $app['form.factory']->createNamedBuilder('', 'add_cart', null, array(
482
                    'product' => $Product,
483
                ));
484
                $addCartForm = $builder->getForm();
485 3
                $forms[$Product->getId()] = $addCartForm->createView();
486
            }
487
488 3
            $event = new EventArgs(
489
                array(
490 3
                    'forms' => $forms,
491 3
                    'Products' => $Products,
492 3
                    'pagination' => $pagination,
493
                ),
494
                $request
495
            );
496 3
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_PRODUCT_COMPLETE, $event);
497
498 3
            return $app->render('Order/search_product.twig', array(
499 3
                'forms' => $forms,
500 3
                'Products' => $Products,
501 3
                'pagination' => $pagination,
502
            ));
503
        }
504
    }
505
506 7
    protected function newOrder()
507
    {
508 7
        $Order = new \Eccube\Entity\Order();
509 7
        $Shipping = new \Eccube\Entity\Shipping();
510 7
        $Shipping->setDelFlg(0);
511 7
        $Order->addShipping($Shipping);
512 7
        $Shipping->setOrder($Order);
513
514 7
        return $Order;
515
    }
516
517
    /**
518
     * フォームからの入直内容に基づいて、受注情報の再計算を行う
519
     *
520
     * @param $app
521
     * @param $Order
522
     */
523 11
    protected function calculate($app, \Eccube\Entity\Order $Order)
524
    {
525 11
        $taxtotal = 0;
526 11
        $subtotal = 0;
527
528
        // 受注明細データの税・小計を再計算
529
        /** @var $OrderDetails \Eccube\Entity\OrderDetail[] */
530 11
        $OrderDetails = $Order->getOrderDetails();
531 11
        foreach ($OrderDetails as $OrderDetail) {
532
            // 新規登録の場合は, 入力されたproduct_id/produc_class_idから明細にセットする.
533 11
            if (!$OrderDetail->getId()) {
534 7
                $TaxRule = $app['eccube.repository.tax_rule']->getByRule($OrderDetail->getProduct(),
535 7
                    $OrderDetail->getProductClass());
536 7
                $OrderDetail->setTaxRule($TaxRule->getId());
537 7
                $OrderDetail->setProductName($OrderDetail->getProduct()->getName());
538 7
                $OrderDetail->setProductCode($OrderDetail->getProductClass()->getCode());
539 7
                $OrderDetail->setClassName1($OrderDetail->getProductClass()->hasClassCategory1()
540 7
                    ? $OrderDetail->getProductClass()->getClassCategory1()->getClassName()->getName()
541 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...
542 7
                $OrderDetail->setClassName2($OrderDetail->getProductClass()->hasClassCategory2()
543 2
                    ? $OrderDetail->getProductClass()->getClassCategory2()->getClassName()->getName()
544 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...
545 7
                $OrderDetail->setClassCategoryName1($OrderDetail->getProductClass()->hasClassCategory1()
546 7
                    ? $OrderDetail->getProductClass()->getClassCategory1()->getName()
547 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...
548 7
                $OrderDetail->setClassCategoryName2($OrderDetail->getProductClass()->hasClassCategory2()
549 2
                    ? $OrderDetail->getProductClass()->getClassCategory2()->getName()
550 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...
551
            }
552
553
            // 税
554 11
            $tax = $app['eccube.service.tax_rule']
555 11
                ->calcTax($OrderDetail->getPrice(), $OrderDetail->getTaxRate(), $OrderDetail->getTaxRule());
556 11
            $OrderDetail->setPriceIncTax($OrderDetail->getPrice() + $tax);
557
558 11
            $taxtotal += $tax * $OrderDetail->getQuantity();
559
560
            // 小計
561 11
            $subtotal += $OrderDetail->getTotalPrice();
562
        }
563
564 11
        $shippings = $Order->getShippings();
565
        /** @var \Eccube\Entity\Shipping $Shipping */
566 11
        foreach ($shippings as $Shipping) {
567 11
            $shipmentItems = $Shipping->getShipmentItems();
568 11
            $Shipping->setDelFlg(Constant::DISABLED);
569
            /** @var \Eccube\Entity\ShipmentItem $ShipmentItem */
570 11
            foreach ($shipmentItems as $ShipmentItem) {
571 8
                $ShipmentItem->setProductName($ShipmentItem->getProduct()->getName());
572 8
                $ShipmentItem->setProductCode($ShipmentItem->getProductClass()->getCode());
573 8
                $ShipmentItem->setClassName1($ShipmentItem->getProductClass()->hasClassCategory1()
574 8
                    ? $ShipmentItem->getProductClass()->getClassCategory1()->getClassName()->getName()
575 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...
576 8
                $ShipmentItem->setClassName2($ShipmentItem->getProductClass()->hasClassCategory2()
577 1
                    ? $ShipmentItem->getProductClass()->getClassCategory2()->getClassName()->getName()
578 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...
579 8
                $ShipmentItem->setClassCategoryName1($ShipmentItem->getProductClass()->hasClassCategory1()
580 8
                    ? $ShipmentItem->getProductClass()->getClassCategory1()->getName()
581 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...
582 8
                $ShipmentItem->setClassCategoryName2($ShipmentItem->getProductClass()->hasClassCategory2()
583 1
                    ? $ShipmentItem->getProductClass()->getClassCategory2()->getName()
584 11
                    : 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...
585
            }
586
        }
587
588
        // 受注データの税・小計・合計を再計算
589 11
        $Order->setTax($taxtotal);
590 11
        $Order->setSubtotal($subtotal);
591 11
        $Order->setTotal($subtotal + $Order->getCharge() + $Order->getDeliveryFeeTotal() - $Order->getDiscount());
592
        // お支払い合計は、totalと同一金額(2系ではtotal - point)
593 11
        $Order->setPaymentTotal($Order->getTotal());
594
    }
595
596
    /**
597
     * 受注ステータスに応じて, 受注日/入金日/発送日を更新する,
598
     * 発送済ステータスが設定された場合は, お届け先情報の発送日も更新を行う.
599
     *
600
     * 編集の場合
601
     * - 受注ステータスが他のステータスから発送済へ変更された場合に発送日を更新
602
     * - 受注ステータスが他のステータスから入金済へ変更された場合に入金日を更新
603
     *
604
     * 新規登録の場合
605
     * - 受注日を更新
606
     * - 受注ステータスが発送済に設定された場合に発送日を更新
607
     * - 受注ステータスが入金済に設定された場合に入金日を更新
608
     *
609
     *
610
     * @param $app
611
     * @param $TargetOrder
612
     * @param $OriginOrder
613
     */
614 11
    protected function updateDate($app, $TargetOrder, $OriginOrder)
615
    {
616 11
        $dateTime = new \DateTime();
617
618
        // 編集
619 11
        if ($TargetOrder->getId()) {
620
            // 発送済
621 7
            if ($TargetOrder->getOrderStatus()->getId() == $app['config']['order_deliv']) {
622
                // 編集前と異なる場合のみ更新
623
                if ($TargetOrder->getOrderStatus()->getId() != $OriginOrder->getOrderStatus()->getId()) {
624
                    $TargetOrder->setCommitDate($dateTime);
625
                    // お届け先情報の発送日も更新する.
626
                    $Shippings = $TargetOrder->getShippings();
627
                    foreach ($Shippings as $Shipping) {
628
                        $Shipping->setShippingCommitDate($dateTime);
629
                    }
630
                }
631
                // 入金済
632 7
            } elseif ($TargetOrder->getOrderStatus()->getId() == $app['config']['order_pre_end']) {
633
                // 編集前と異なる場合のみ更新
634
                if ($TargetOrder->getOrderStatus()->getId() != $OriginOrder->getOrderStatus()->getId()) {
635 7
                    $TargetOrder->setPaymentDate($dateTime);
636
                }
637
            }
638
            // 新規
639
        } else {
640
            // 発送済
641 4
            if ($TargetOrder->getOrderStatus()->getId() == $app['config']['order_deliv']) {
642
                $TargetOrder->setCommitDate($dateTime);
643
                // お届け先情報の発送日も更新する.
644
                $Shippings = $TargetOrder->getShippings();
645
                foreach ($Shippings as $Shipping) {
646
                    $Shipping->setShippingCommitDate($dateTime);
647
                }
648
                // 入金済
649 4
            } elseif ($TargetOrder->getOrderStatus()->getId() == $app['config']['order_pre_end']) {
650
                $TargetOrder->setPaymentDate($dateTime);
651
            }
652
            // 受注日時
653 4
            $TargetOrder->setOrderDate($dateTime);
654
        }
655
    }
656
}
657