EditController::calculate()   B
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 33
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 4
nop 2
dl 0
loc 33
ccs 17
cts 17
cp 1
crap 3
rs 8.8571
c 0
b 0
f 0
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
        foreach ($TargetOrder->getOrderDetails() as $OrderDetail) {
67 17
            $OriginalOrderDetails->add($OrderDetail);
68
        }
69
70 17
        $builder = $app['form.factory']
71 17
            ->createBuilder('order', $TargetOrder);
72
73 17
        $event = new EventArgs(
74
            array(
75 17
                'builder' => $builder,
76 17
                'OriginOrder' => $OriginOrder,
77 17
                'TargetOrder' => $TargetOrder,
78 17
                'OriginOrderDetails' => $OriginalOrderDetails,
79
            ),
80
            $request
81
        );
82 17
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_INDEX_INITIALIZE, $event);
83
84 17
        $form = $builder->getForm();
85
86 17
        if ('POST' === $request->getMethod()) {
87 11
            $form->handleRequest($request);
88
89 11
            $event = new EventArgs(
90
                array(
91 11
                    'builder' => $builder,
92 11
                    'OriginOrder' => $OriginOrder,
93 11
                    'TargetOrder' => $TargetOrder,
94 11
                    'OriginOrderDetails' => $OriginalOrderDetails,
95
                ),
96
                $request
97
            );
98 11
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_INDEX_PROGRESS, $event);
99
100
            // 入力情報にもとづいて再計算.
101 11
            $this->calculate($app, $TargetOrder);
102
103
            // 登録ボタン押下
104 11
            switch ($request->get('mode')) {
105 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...
106
107 11
                    log_info('受注登録開始', array($TargetOrder->getId()));
108
109 11
                    if ($TargetOrder->getTotal() > $app['config']['max_total_fee']) {
110
                        log_info('受注登録入力チェックエラー', array($TargetOrder->getId()));
111
                        $form['charge']->addError(new FormError('合計金額の上限を超えております。'));
112 11
                    } elseif ($form->isValid()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
113
114 11
                        $BaseInfo = $app['eccube.repository.base_info']->get();
115
116
                        // お支払い方法の更新
117 11
                        $TargetOrder->setPaymentMethod($TargetOrder->getPayment()->getMethod());
118
119
                        // 配送業者・お届け時間の更新
120 11
                        $Shippings = $TargetOrder->getShippings();
121 11
                        foreach ($Shippings as $Shipping) {
122 11
                            $Shipping->setShippingDeliveryName($Shipping->getDelivery()->getName());
123 11
                            if (!is_null($Shipping->getDeliveryTime())) {
124 11
                                $Shipping->setShippingDeliveryTime($Shipping->getDeliveryTime()->getDeliveryTime());
125
                            } else {
126 11
                                $Shipping->setShippingDeliveryTime(null);
127
                            }
128
                        }
129
130
131
                        // 受注日/発送日/入金日の更新.
132 11
                        $this->updateDate($app, $TargetOrder, $OriginOrder);
133
134
                        // 受注明細で削除されているものをremove
135 11
                        foreach ($OriginalOrderDetails as $OrderDetail) {
136 7
                            if (false === $TargetOrder->getOrderDetails()->contains($OrderDetail)) {
137 11
                                $app['orm.em']->remove($OrderDetail);
138
                            }
139
                        }
140
141
142 11
                        if ($BaseInfo->getOptionMultipleShipping() == Constant::ENABLED) {
143 4
                            foreach ($TargetOrder->getOrderDetails() as $OrderDetail) {
144
                                /** @var $OrderDetail \Eccube\Entity\OrderDetail */
145 4
                                $OrderDetail->setOrder($TargetOrder);
146
                            }
147
148
                            /** @var \Eccube\Entity\Shipping $Shipping */
149 4 View Code Duplication
                            foreach ($Shippings as $Shipping) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150 4
                                $shipmentItems = $Shipping->getShipmentItems();
151
                                /** @var \Eccube\Entity\ShipmentItem $ShipmentItem */
152 4
                                foreach ($shipmentItems as $ShipmentItem) {
153 4
                                    $ShipmentItem->setOrder($TargetOrder);
154 4
                                    $ShipmentItem->setShipping($Shipping);
155 4
                                    $app['orm.em']->persist($ShipmentItem);
156
                                }
157 4
                                $Shipping->setOrder($TargetOrder);
158 4
                                $app['orm.em']->persist($Shipping);
159
                            }
160
                        } else {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
161
162 7
                            $NewShipmentItems = new ArrayCollection();
163
164 7
                            foreach ($TargetOrder->getOrderDetails() as $OrderDetail) {
165
                                /** @var $OrderDetail \Eccube\Entity\OrderDetail */
166 7
                                $OrderDetail->setOrder($TargetOrder);
167
168 7
                                $NewShipmentItem = new ShipmentItem();
169
                                $NewShipmentItem
170 7
                                    ->setProduct($OrderDetail->getProduct())
171 7
                                    ->setProductClass($OrderDetail->getProductClass())
172 7
                                    ->setProductName($OrderDetail->getProduct()->getName())
173 7
                                    ->setProductCode($OrderDetail->getProductClass()->getCode())
174 7
                                    ->setClassCategoryName1($OrderDetail->getClassCategoryName1())
175 7
                                    ->setClassCategoryName2($OrderDetail->getClassCategoryName2())
176 7
                                    ->setClassName1($OrderDetail->getClassName1())
177 7
                                    ->setClassName2($OrderDetail->getClassName2())
178 7
                                    ->setPrice($OrderDetail->getPrice())
179 7
                                    ->setQuantity($OrderDetail->getQuantity())
180 7
                                    ->setOrder($TargetOrder);
181 7
                                $NewShipmentItems[] = $NewShipmentItem;
182
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
183
                            }
184
                            // 配送商品の更新. delete/insert.
185 7
                            $Shippings = $TargetOrder->getShippings();
186 7 View Code Duplication
                            foreach ($Shippings as $Shipping) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

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