Completed
Pull Request — master (#2053)
by
unknown
42:47
created

EditController::updateDate()   D

Complexity

Conditions 10
Paths 9

Size

Total Lines 42
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 26.6375

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 42
ccs 9
cts 20
cp 0.45
rs 4.8196
cc 10
eloc 21
nc 9
nop 3
crap 26.6375

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