Completed
Push — fix#1932 ( 142bd3...e67a3b )
by NOBU
40:00
created

EditController::searchCustomer()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 54
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 4.0007

Importance

Changes 0
Metric Value
cc 4
eloc 33
nc 5
nop 2
dl 0
loc 54
rs 9.0306
c 0
b 0
f 0
ccs 27
cts 28
cp 0.9643
crap 4.0007

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
            '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()) {
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
                            } else {
125 11
                                $Shipping->setShippingDeliveryTime(null);
126
                            }
127
                        }
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 11
                                $app['orm.em']->remove($OrderDetail);
137
                            }
138
                        }
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
                            }
146
147
                            /** @var \Eccube\Entity\Shipping $Shipping */
148 4 View Code Duplication
                            foreach ($Shippings as $Shipping) {
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
                                }
156 4
                                $Shipping->setOrder($TargetOrder);
157 4
                                $app['orm.em']->persist($Shipping);
158
                            }
159
                        } 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
                            }
183
                            // 配送商品の更新. delete/insert.
184 7
                            $Shippings = $TargetOrder->getShippings();
185 7 View Code Duplication
                            foreach ($Shippings as $Shipping) {
186 7
                                $ShipmentItems = $Shipping->getShipmentItems();
187 7
                                foreach ($ShipmentItems as $ShipmentItem) {
188 7
                                    $app['orm.em']->remove($ShipmentItem);
189
                                }
190 7
                                $ShipmentItems->clear();
191 7
                                foreach ($NewShipmentItems as $NewShipmentItem) {
192 7
                                    $NewShipmentItem->setShipping($Shipping);
193 7
                                    $ShipmentItems->add($NewShipmentItem);
194
                                }
195
                            }
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
                        }
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
                        }
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
                            ),
222
                            $request
223
                        );
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
                    $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 2
                    break;
253
            }
254
        }
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
            ),
267
            $request
268
        );
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
            ),
284
            $request
285
        );
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
            }
298
        }
299
300 8
        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
        ));
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
            );
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
                ),
333
                $request
334
            );
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
            }
357
358 3
            $event = new EventArgs(
359
                array(
360 3
                    'data' => $data,
361 3
                    'Customers' => $Customers,
362
                ),
363
                $request
364
            );
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 3
    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
                ),
392
                $request
393
            );
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
            );
424
425 3
            $event = new EventArgs(
426
                array(
427 3
                    'data' => $data,
428 3
                    'Customer' => $Customer,
429
                ),
430
                $request
431
            );
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 3
            return $app->json($data);
436
        }
437
    }
438
439 3
    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 3
            $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
                );
453
454 3
                if ($categoryId = $request->get('category_id')) {
455
                    $Category = $app['eccube.repository.category']->find($categoryId);
456
                    $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
            } 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 3
            $event = new EventArgs(
474
                array(
475 3
                    'qb' => $qb,
476 3
                    'searchData' => $searchData,
477
                ),
478
                $request
479
            );
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
                $qb,
485
                $page_no,
486
                $page_count,
487 3
                array('wrap-queries' => true)
488
            );
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
            }
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 3
                $forms[$Product->getId()] = $addCartForm->createView();
505
            }
506
507 3
            $event = new EventArgs(
508
                array(
509 3
                    'forms' => $forms,
510 3
                    'Products' => $Products,
511 3
                    'pagination' => $pagination,
512
                ),
513
                $request
514
            );
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
            ));
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 5
                    ? $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 5
                    ? $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
            }
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
        }
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 5
                    ? $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 5
                    ? $ShipmentItem->getProductClass()->getClassCategory2()->getName()
603 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...
604
            }
605
        }
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
    }
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 7
                    $TargetOrder->setPaymentDate($dateTime);
655
                }
656
            }
657
            // 新規
658
        } 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
    }
675
}
676