ShoppingService   D
last analyzed

Complexity

Total Complexity 106

Size/Duplication

Total Lines 1225
Duplicated Lines 9.88 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 95.51%

Importance

Changes 0
Metric Value
dl 121
loc 1225
ccs 404
cts 423
cp 0.9551
rs 4.4102
c 0
b 0
f 0
wmc 106
lcom 1
cbo 8

41 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B getOrder() 0 24 3
A getNonMember() 0 15 2
A createOrder() 0 21 2
A registerPreOrder() 0 69 3
A getNewOrder() 0 7 1
A newOrder() 0 6 1
B copyToOrderFromCustomer() 0 34 3
A getDeliveriesCart() 0 9 1
A getDeliveriesOrder() 0 9 1
A getDeliveries() 0 22 3
B getNewShipping() 0 24 3
A copyToShippingFromCustomer() 0 53 3
B getNewDetails() 0 24 2
B getNewOrderDetail() 0 28 3
B getNewShipmentItem() 0 54 7
A getShippingDeliveryFeeTotal() 0 10 2
A getProductDeliveryFee() 0 9 2
A getAmount() 21 21 1
A setShippingDeliveryFee() 0 20 3
A setDeliveryFreeAmount() 16 16 4
A setDeliveryFreeQuantity() 16 16 4
C isOrderProduct() 0 39 8
B setOrderUpdate() 0 32 4
A setOrderUpdateData() 0 8 1
B setStockUpdate() 0 25 3
A setCustomerUpdate() 0 17 2
B getPayments() 0 18 6
C getFormDeliveryDates() 0 44 7
A getFormPayments() 0 22 3
B getShippingForm() 26 26 1
B getShippingFormBuilder() 24 24 1
A setFormData() 0 18 3
A calculateDeliveryFee() 18 18 1
B processPurchase() 0 26 3
A isDiscount() 0 9 2
A setDiscount() 0 6 1
A calculatePrice() 0 16 2
A setOrderStatus() 0 17 1
A sendOrderMail() 0 23 1
A notifyComplete() 0 12 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like ShoppingService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ShoppingService, and based on these observations, apply Extract Interface, too.

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\Service;
25
26
use Doctrine\DBAL\LockMode;
27
use Eccube\Application;
28
use Eccube\Common\Constant;
29
use Eccube\Entity\Customer;
30
use Eccube\Entity\Delivery;
31
use Eccube\Entity\MailHistory;
32
use Eccube\Entity\Master\DeviceType;
33
use Eccube\Entity\Order;
34
use Eccube\Entity\OrderDetail;
35
use Eccube\Entity\Product;
36
use Eccube\Entity\ProductClass;
37
use Eccube\Entity\ShipmentItem;
38
use Eccube\Entity\Shipping;
39
use Eccube\Event\EccubeEvents;
40
use Eccube\Event\EventArgs;
41
use Eccube\Exception\CartException;
42
use Eccube\Exception\ShoppingException;
43
use Eccube\Util\Str;
44
45
46
class ShoppingService
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
47
{
48
    /** @var \Eccube\Application */
49
    public $app;
50
51
    /** @var \Eccube\Service\CartService */
52
    protected $cartService;
53
54
    /** @var \Eccube\Service\OrderService */
55
    protected $orderService;
56
57
    /** @var \Eccube\Entity\BaseInfo */
58
    protected $BaseInfo;
59
60
    /** @var  \Doctrine\ORM\EntityManager */
61
    protected $em;
62
63 61
    public function __construct(Application $app, $cartService, $orderService)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
64
    {
65 61
        $this->app = $app;
66 61
        $this->cartService = $cartService;
67 61
        $this->orderService = $orderService;
68 61
        $this->BaseInfo = $app['eccube.repository.base_info']->get();
69
    }
70
71
    /**
72
     * セッションにセットされた受注情報を取得
73
     *
74
     * @param null $status
75
     * @return null|object
76
     */
77 38
    public function getOrder($status = null)
78
    {
79
80
        // 受注データを取得
81 38
        $preOrderId = $this->cartService->getPreOrderId();
82 38
        if (!$preOrderId) {
83 33
            return null;
84
        }
85
86
        $condition = array(
87 33
            'pre_order_id' => $preOrderId,
88
        );
89
90 33
        if (!is_null($status)) {
91
            $condition += array(
92 30
                'OrderStatus' => $status,
93
            );
94
        }
95
96 33
        $Order = $this->app['eccube.repository.order']->findOneBy($condition);
97
98 33
        return $Order;
99
100
    }
101
102
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$sesisonKey" missing
Loading history...
103
     * 非会員情報を取得
104
     *
105
     * @param $sesisonKey
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
106
     * @return $Customer|null
0 ignored issues
show
Documentation introduced by
The doc-type $Customer|null could not be parsed: Unknown type name "$Customer" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
107
     */
108 14
    public function getNonMember($sesisonKey)
109
    {
110
111
        // 非会員でも一度会員登録されていればショッピング画面へ遷移
112 14
        $nonMember = $this->app['session']->get($sesisonKey);
113 14
        if (is_null($nonMember)) {
114 1
            return null;
115
        }
116
117 13
        $Customer = $nonMember['customer'];
118 13
        $Customer->setPref($this->app['eccube.repository.master.pref']->find($nonMember['pref']));
119
120 13
        return $Customer;
121
122
    }
123
124
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
125
     * 受注情報を作成
126
     *
127
     * @param $Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
128
     * @return \Eccube\Entity\Order
129
     */
130 40
    public function createOrder($Customer)
131
    {
132
        // ランダムなpre_order_idを作成
133
        do {
134 40
            $preOrderId = sha1(Str::random(32));
135 40
            $Order = $this->app['eccube.repository.order']->findOneBy(array(
136 40
                'pre_order_id' => $preOrderId,
137 40
                'OrderStatus' => $this->app['config']['order_processing'],
138
            ));
139 40
        } while ($Order);
140
141
        // 受注情報、受注明細情報、お届け先情報、配送商品情報を作成
142 40
        $Order = $this->registerPreOrder(
143
            $Customer,
144
            $preOrderId);
145
146 40
        $this->cartService->setPreOrderId($preOrderId);
147 40
        $this->cartService->save();
148
149 40
        return $Order;
150
    }
151
152
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$preOrderId" missing
Loading history...
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
153
     * 仮受注情報作成
154
     *
155
     * @param $Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
156
     * @param $preOrderId
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
157
     * @return mixed
158
     * @throws \Doctrine\ORM\NoResultException
159
     * @throws \Doctrine\ORM\NonUniqueResultException
160
     */
161 40
    public function registerPreOrder(Customer $Customer, $preOrderId)
162
    {
163
164 40
        $this->em = $this->app['orm.em'];
165
166
        // 受注情報を作成
167 40
        $Order = $this->getNewOrder($Customer);
168 40
        $Order->setPreOrderId($preOrderId);
169
170
        // device type
171 40
        if ($this->app['mobile_detect']->isMobile()) {
172
            $DeviceType = $this->app['eccube.repository.master.device_type']->find(DeviceType::DEVICE_TYPE_SP);
173
        } else {
174 40
            $DeviceType = $this->app['eccube.repository.master.device_type']->find(DeviceType::DEVICE_TYPE_PC);
175
        }
176 40
        $Order->setDeviceType($DeviceType);
177
178 40
        $this->em->persist($Order);
179
180
        // 配送業者情報を取得
181 40
        $deliveries = $this->getDeliveriesCart();
182
183
        // お届け先情報を作成
184 40
        $Order = $this->getNewShipping($Order, $Customer, $deliveries);
185
186
        // 受注明細情報、配送商品情報を作成
187 40
        $Order = $this->getNewDetails($Order);
188
189
        // 小計
190 40
        $subTotal = $this->orderService->getSubTotal($Order);
0 ignored issues
show
Deprecated Code introduced by
The method Eccube\Service\OrderService::getSubTotal() has been deprecated with message: since 3.0.0, to be removed in 3.1

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
191
192
        // 消費税のみの小計
193 40
        $tax = $this->orderService->getTotalTax($Order);
0 ignored issues
show
Deprecated Code introduced by
The method Eccube\Service\OrderService::getTotalTax() has been deprecated with message: since 3.0.0, to be removed in 3.1

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
194
195
        // 配送料合計金額
196 40
        $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($Order->getShippings()));
197
198
        // 小計
199 40
        $Order->setSubTotal($subTotal);
200
201
        // 配送料無料条件(合計金額)
202 40
        $this->setDeliveryFreeAmount($Order);
203
204
        // 配送料無料条件(合計数量)
205 40
        $this->setDeliveryFreeQuantity($Order);
206
207
        // 初期選択の支払い方法をセット
208 40
        $payments = $this->app['eccube.repository.payment']->findAllowedPayments($deliveries);
209 40
        $payments = $this->getPayments($payments, $subTotal);
210
211 40
        if (count($payments) > 0) {
212 40
            $payment = $payments[0];
213 40
            $Order->setPayment($payment);
214 40
            $Order->setPaymentMethod($payment->getMethod());
215 40
            $Order->setCharge($payment->getCharge());
216
        } else {
217
            $Order->setCharge(0);
218
        }
219
220 40
        $Order->setTax($tax);
221
222
        // 合計金額の計算
223 40
        $this->calculatePrice($Order);
224
225 40
        $this->em->flush();
226
227 40
        return $Order;
228
229
    }
230
231
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
232
     * 受注情報を作成
233
     * @param $Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
234
     * @return \Eccube\Entity\Order
235
     */
236 40
    public function getNewOrder(Customer $Customer)
237
    {
238 40
        $Order = $this->newOrder();
239 40
        $this->copyToOrderFromCustomer($Order, $Customer);
240
241 40
        return $Order;
242
    }
243
244
245
    /**
246
     * 受注情報を作成
247
     * @return \Eccube\Entity\Order
248
     */
249 40
    public function newOrder()
250
    {
251 40
        $OrderStatus = $this->app['eccube.repository.order_status']->find($this->app['config']['order_processing']);
252 40
        $Order = new \Eccube\Entity\Order($OrderStatus);
253 40
        return $Order;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
254
    }
255
256
    /**
257
     * 受注情報を作成
258
     *
259
     * @param \Eccube\Entity\Order $Order
0 ignored issues
show
introduced by
Expected 9 spaces after parameter type; 1 found
Loading history...
260
     * @param \Eccube\Entity\Customer|null $Customer
261
     * @return \Eccube\Entity\Order
262
     */
263 40
    public function copyToOrderFromCustomer(Order $Order, Customer $Customer = null)
264
    {
265 40
        if (is_null($Customer)) {
266
            return $Order;
267
        }
268
269 40
        if ($Customer->getId()) {
270 26
            $Order->setCustomer($Customer);
271
        }
272
        $Order
273 40
            ->setName01($Customer->getName01())
274 40
            ->setName02($Customer->getName02())
275 40
            ->setKana01($Customer->getKana01())
276 40
            ->setKana02($Customer->getKana02())
277 40
            ->setCompanyName($Customer->getCompanyName())
278 40
            ->setEmail($Customer->getEmail())
279 40
            ->setTel01($Customer->getTel01())
280 40
            ->setTel02($Customer->getTel02())
281 40
            ->setTel03($Customer->getTel03())
282 40
            ->setFax01($Customer->getFax01())
283 40
            ->setFax02($Customer->getFax02())
284 40
            ->setFax03($Customer->getFax03())
285 40
            ->setZip01($Customer->getZip01())
286 40
            ->setZip02($Customer->getZip02())
287 40
            ->setZipCode($Customer->getZip01().$Customer->getZip02())
288 40
            ->setPref($Customer->getPref())
289 40
            ->setAddr01($Customer->getAddr01())
290 40
            ->setAddr02($Customer->getAddr02())
291 40
            ->setSex($Customer->getSex())
292 40
            ->setBirth($Customer->getBirth())
293 40
            ->setJob($Customer->getJob());
294
295 40
        return $Order;
296
    }
297
298
299
    /**
300
     * 配送業者情報を取得
301
     *
302
     * @return array
303
     */
304 40
    public function getDeliveriesCart()
305
    {
306
307
        // カートに保持されている商品種別を取得
308 40
        $productTypes = $this->cartService->getProductTypes();
309
310 40
        return $this->getDeliveries($productTypes);
311
312
    }
313
314
    /**
315
     * 配送業者情報を取得
316
     *
317
     * @param Order $Order
318
     * @return array
319
     */
320 32
    public function getDeliveriesOrder(Order $Order)
321
    {
322
323
        // 受注情報から商品種別を取得
324 32
        $productTypes = $this->orderService->getProductTypes($Order);
0 ignored issues
show
Deprecated Code introduced by
The method Eccube\Service\OrderService::getProductTypes() has been deprecated with message: since 3.0.0, to be removed in 3.1

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
325
326 32
        return $this->getDeliveries($productTypes);
327
328
    }
329
330
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$productTypes" missing
Loading history...
331
     * 配送業者情報を取得
332
     *
333
     * @param $productTypes
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
334
     * @return array
335
     */
336 43
    public function getDeliveries($productTypes)
337
    {
338
339
        // 商品種別に紐づく配送業者を取得
340 43
        $deliveries = $this->app['eccube.repository.delivery']->getDeliveries($productTypes);
341
342 43
        if ($this->BaseInfo->getOptionMultipleShipping() == Constant::ENABLED) {
343
            // 複数配送対応
344
345
            // 支払方法を取得
346 7
            $payments = $this->app['eccube.repository.payment']->findAllowedPayments($deliveries);
347
348 7
            if (count($productTypes) > 1) {
349
                // 商品種別が複数ある場合、配送対象となる配送業者を取得
350 1
                $deliveries = $this->app['eccube.repository.delivery']->findAllowedDeliveries($productTypes, $payments);
351
            }
352
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
353
        }
354
355 43
        return $deliveries;
356
357
    }
358
359
360
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$deliveries" missing
Loading history...
361
     * お届け先情報を作成
362
     *
363
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
364
     * @param Customer $Customer
0 ignored issues
show
introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
365
     * @param $deliveries
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
366
     * @return Order
367
     */
368 40
    public function getNewShipping(Order $Order, Customer $Customer, $deliveries)
369
    {
370 40
        $productTypes = array();
371 40
        foreach ($deliveries as $Delivery) {
372 40
            if (!in_array($Delivery->getProductType()->getId(), $productTypes)) {
373 40
                $Shipping = new Shipping();
374
375 40
                $this->copyToShippingFromCustomer($Shipping, $Customer)
376 40
                    ->setOrder($Order)
377 40
                    ->setDelFlg(Constant::DISABLED);
378
379
                // 配送料金の設定
380 40
                $this->setShippingDeliveryFee($Shipping, $Delivery);
381
382 40
                $this->em->persist($Shipping);
383
384 40
                $Order->addShipping($Shipping);
385
386 40
                $productTypes[] = $Delivery->getProductType()->getId();
387
            }
388
        }
389
390 40
        return $Order;
391
    }
392
393
    /**
394
     * お届け先情報を作成
395
     *
396
     * @param \Eccube\Entity\Shipping $Shipping
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
397
     * @param \Eccube\Entity\Customer|null $Customer
398
     * @return \Eccube\Entity\Shipping
399
     */
400 41
    public function copyToShippingFromCustomer(Shipping $Shipping, Customer $Customer = null)
401
    {
402 41
        if (is_null($Customer)) {
403 1
            return $Shipping;
404
        }
405
406 40
        $CustomerAddress = $this->app['eccube.repository.customer_address']->findOneBy(
407 40
            array('Customer' => $Customer),
408 40
            array('id' => 'ASC')
409
        );
410
411 40
        if (!is_null($CustomerAddress)) {
412
            $Shipping
413 26
                ->setName01($CustomerAddress->getName01())
414 26
                ->setName02($CustomerAddress->getName02())
415 26
                ->setKana01($CustomerAddress->getKana01())
416 26
                ->setKana02($CustomerAddress->getKana02())
417 26
                ->setCompanyName($CustomerAddress->getCompanyName())
418 26
                ->setTel01($CustomerAddress->getTel01())
419 26
                ->setTel02($CustomerAddress->getTel02())
420 26
                ->setTel03($CustomerAddress->getTel03())
421 26
                ->setFax01($CustomerAddress->getFax01())
422 26
                ->setFax02($CustomerAddress->getFax02())
423 26
                ->setFax03($CustomerAddress->getFax03())
424 26
                ->setZip01($CustomerAddress->getZip01())
425 26
                ->setZip02($CustomerAddress->getZip02())
426 26
                ->setZipCode($CustomerAddress->getZip01().$CustomerAddress->getZip02())
427 26
                ->setPref($CustomerAddress->getPref())
428 26
                ->setAddr01($CustomerAddress->getAddr01())
429 26
                ->setAddr02($CustomerAddress->getAddr02());
430
        } else {
431
            $Shipping
432 14
                ->setName01($Customer->getName01())
433 14
                ->setName02($Customer->getName02())
434 14
                ->setKana01($Customer->getKana01())
435 14
                ->setKana02($Customer->getKana02())
436 14
                ->setCompanyName($Customer->getCompanyName())
437 14
                ->setTel01($Customer->getTel01())
438 14
                ->setTel02($Customer->getTel02())
439 14
                ->setTel03($Customer->getTel03())
440 14
                ->setFax01($Customer->getFax01())
441 14
                ->setFax02($Customer->getFax02())
442 14
                ->setFax03($Customer->getFax03())
443 14
                ->setZip01($Customer->getZip01())
444 14
                ->setZip02($Customer->getZip02())
445 14
                ->setZipCode($Customer->getZip01().$Customer->getZip02())
446 14
                ->setPref($Customer->getPref())
447 14
                ->setAddr01($Customer->getAddr01())
448 14
                ->setAddr02($Customer->getAddr02());
449
        }
450
451 40
        return $Shipping;
452
    }
453
454
455
    /**
456
     * 受注明細情報、配送商品情報を作成
457
     *
458
     * @param Order $Order
459
     * @return Order
460
     */
461 40
    public function getNewDetails(Order $Order)
462
    {
463
464
        // 受注詳細, 配送商品
465 40
        foreach ($this->cartService->getCart()->getCartItems() as $item) {
466
            /* @var $ProductClass \Eccube\Entity\ProductClass */
467 40
            $ProductClass = $item->getObject();
468
            /* @var $Product \Eccube\Entity\Product */
469 40
            $Product = $ProductClass->getProduct();
470
471 40
            $quantity = $item->getQuantity();
472
473
            // 受注明細情報を作成
474 40
            $OrderDetail = $this->getNewOrderDetail($Product, $ProductClass, $quantity);
475 40
            $OrderDetail->setOrder($Order);
476 40
            $Order->addOrderDetail($OrderDetail);
477
478
            // 配送商品情報を作成
479 40
            $this->getNewShipmentItem($Order, $Product, $ProductClass, $quantity);
480
        }
481
482 40
        return $Order;
483
484
    }
485
486
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$quantity" missing
Loading history...
487
     * 受注明細情報を作成
488
     *
489
     * @param Product $Product
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
490
     * @param ProductClass $ProductClass
491
     * @param $quantity
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
492
     * @return \Eccube\Entity\OrderDetail
493
     */
494 40
    public function getNewOrderDetail(Product $Product, ProductClass $ProductClass, $quantity)
495
    {
496 40
        $OrderDetail = new OrderDetail();
497 40
        $TaxRule = $this->app['eccube.repository.tax_rule']->getByRule($Product, $ProductClass);
498 40
        $OrderDetail->setProduct($Product)
499 40
            ->setProductClass($ProductClass)
500 40
            ->setProductName($Product->getName())
501 40
            ->setProductCode($ProductClass->getCode())
502 40
            ->setPrice($ProductClass->getPrice02())
503 40
            ->setQuantity($quantity)
504 40
            ->setTaxRule($TaxRule->getCalcRule()->getId())
505 40
            ->setTaxRate($TaxRule->getTaxRate());
506
507 40
        $ClassCategory1 = $ProductClass->getClassCategory1();
508 40
        if (!is_null($ClassCategory1)) {
509 40
            $OrderDetail->setClasscategoryName1($ClassCategory1->getName());
510 40
            $OrderDetail->setClassName1($ClassCategory1->getClassName()->getName());
511
        }
512 40
        $ClassCategory2 = $ProductClass->getClassCategory2();
513 40
        if (!is_null($ClassCategory2)) {
514 40
            $OrderDetail->setClasscategoryName2($ClassCategory2->getName());
515 40
            $OrderDetail->setClassName2($ClassCategory2->getClassName()->getName());
516
        }
517
518 40
        $this->em->persist($OrderDetail);
519
520 40
        return $OrderDetail;
521
    }
522
523
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$quantity" missing
Loading history...
524
     * 配送商品情報を作成
525
     *
526
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
527
     * @param Product $Product
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
528
     * @param ProductClass $ProductClass
529
     * @param $quantity
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
530
     * @return \Eccube\Entity\ShipmentItem
531
     */
532 40
    public function getNewShipmentItem(Order $Order, Product $Product, ProductClass $ProductClass, $quantity)
533
    {
534
535 40
        $ShipmentItem = new ShipmentItem();
536 40
        $shippings = $Order->getShippings();
537
538
        // 選択された商品がどのお届け先情報と関連するかチェック
539 40
        $Shipping = null;
540 40
        foreach ($shippings as $s) {
541 40
            if ($s->getDelivery()->getProductType()->getId() == $ProductClass->getProductType()->getId()) {
542
                // 商品種別が同一のお届け先情報と関連させる
543 40
                $Shipping = $s;
544 40
                break;
545
            }
546
        }
547
548 40
        if (is_null($Shipping)) {
549
            // お届け先情報と関連していない場合、エラー
550
            throw new CartException('shopping.delivery.not.producttype');
551
        }
552
553
        // 商品ごとの配送料合計
554 40
        $productDeliveryFeeTotal = 0;
555 40
        if (!is_null($this->BaseInfo->getOptionProductDeliveryFee())) {
556 40
            $productDeliveryFeeTotal = $ProductClass->getDeliveryFee() * $quantity;
557
        }
558
559 40
        $Shipping->setShippingDeliveryFee($Shipping->getShippingDeliveryFee() + $productDeliveryFeeTotal);
560
561 40
        $ShipmentItem->setShipping($Shipping)
562 40
            ->setOrder($Order)
563 40
            ->setProductClass($ProductClass)
564 40
            ->setProduct($Product)
565 40
            ->setProductName($Product->getName())
566 40
            ->setProductCode($ProductClass->getCode())
567 40
            ->setPrice($ProductClass->getPrice02())
568 40
            ->setQuantity($quantity);
569
570 40
        $ClassCategory1 = $ProductClass->getClassCategory1();
571 40
        if (!is_null($ClassCategory1)) {
572 40
            $ShipmentItem->setClasscategoryName1($ClassCategory1->getName());
573 40
            $ShipmentItem->setClassName1($ClassCategory1->getClassName()->getName());
574
        }
575 40
        $ClassCategory2 = $ProductClass->getClassCategory2();
576 40
        if (!is_null($ClassCategory2)) {
577 40
            $ShipmentItem->setClasscategoryName2($ClassCategory2->getName());
578 40
            $ShipmentItem->setClassName2($ClassCategory2->getClassName()->getName());
579
        }
580 40
        $Shipping->addShipmentItem($ShipmentItem);
581 40
        $this->em->persist($ShipmentItem);
582
583 40
        return $ShipmentItem;
584
585
    }
586
587
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$shippings" missing
Loading history...
588
     * お届け先ごとの送料合計を取得
589
     *
590
     * @param $shippings
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
591
     * @return int
592
     */
593 41
    public function getShippingDeliveryFeeTotal($shippings)
594
    {
595 41
        $deliveryFeeTotal = 0;
596 41
        foreach ($shippings as $Shipping) {
597 41
            $deliveryFeeTotal += $Shipping->getShippingDeliveryFee();
598
        }
599
600 41
        return $deliveryFeeTotal;
601
602
    }
603
604
    /**
605
     * 商品ごとの配送料を取得
606
     *
607
     * @param Shipping $Shipping
608
     * @return int
609
     */
610 40
    public function getProductDeliveryFee(Shipping $Shipping)
611
    {
612 40
        $productDeliveryFeeTotal = 0;
613 40
        $shipmentItems = $Shipping->getShipmentItems();
614 40
        foreach ($shipmentItems as $ShipmentItem) {
615 40
            $productDeliveryFeeTotal += $ShipmentItem->getProductClass()->getDeliveryFee() * $ShipmentItem->getQuantity();
616
        }
617 40
        return $productDeliveryFeeTotal;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
618
    }
619
620
    /**
621
     * 住所などの情報が変更された時に金額の再計算を行う
622
     *
623
     * @param Order $Order
624
     * @return Order
625
     */
626 10 View Code Duplication
    public function getAmount(Order $Order)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
627
    {
628
629
        // 初期選択の配送業者をセット
630 10
        $shippings = $Order->getShippings();
631
632
        // 配送料合計金額
633 10
        $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($shippings));
634
635
        // 配送料無料条件(合計金額)
636 10
        $this->setDeliveryFreeAmount($Order);
637
638
        // 配送料無料条件(合計数量)
639 10
        $this->setDeliveryFreeQuantity($Order);
640
641
        // 合計金額の計算
642 10
        $this->calculatePrice($Order);
643
644 10
        return $Order;
645
646
    }
647
648
    /**
649
     * 配送料金の設定
650
     *
651
     * @param Shipping $Shipping
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
652
     * @param Delivery|null $Delivery
653
     */
654 40
    public function setShippingDeliveryFee(Shipping $Shipping, Delivery $Delivery = null)
655
    {
656
        // 配送料金の設定
657 40
        if (is_null($Delivery)) {
658 5
            $Delivery = $Shipping->getDelivery();
659
        }
660 40
        $deliveryFee = $this->app['eccube.repository.delivery_fee']->findOneBy(array('Delivery' => $Delivery, 'Pref' => $Shipping->getPref()));
661
662 40
        $Shipping->setDeliveryFee($deliveryFee);
663 40
        $Shipping->setDelivery($Delivery);
664
665
        // 商品ごとの配送料合計
666 40
        $productDeliveryFeeTotal = 0;
667 40
        if (!is_null($this->BaseInfo->getOptionProductDeliveryFee())) {
668 40
            $productDeliveryFeeTotal += $this->getProductDeliveryFee($Shipping);
669
        }
670
671 40
        $Shipping->setShippingDeliveryFee($deliveryFee->getFee() + $productDeliveryFeeTotal);
672 40
        $Shipping->setShippingDeliveryName($Delivery->getName());
673
    }
674
675
    /**
676
     * 配送料無料条件(合計金額)の条件を満たしていれば配送料金を0に設定
677
     *
678
     * @param Order $Order
679
     */
680 42 View Code Duplication
    public function setDeliveryFreeAmount(Order $Order)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
681
    {
682
        // 配送料無料条件(合計金額)
683 42
        $deliveryFreeAmount = $this->BaseInfo->getDeliveryFreeAmount();
684 42
        if (!is_null($deliveryFreeAmount)) {
685
            // 合計金額が設定金額以上であれば送料無料
686 1
            if ($Order->getSubTotal() >= $deliveryFreeAmount) {
687 1
                $Order->setDeliveryFeeTotal(0);
688
                // お届け先情報の配送料も0にセット
689 1
                $shippings = $Order->getShippings();
690 1
                foreach ($shippings as $Shipping) {
691 1
                    $Shipping->setShippingDeliveryFee(0);
692
                }
693
            }
694
        }
695
    }
696
697
    /**
698
     * 配送料無料条件(合計数量)の条件を満たしていれば配送料金を0に設定
699
     *
700
     * @param Order $Order
701
     */
702 42 View Code Duplication
    public function setDeliveryFreeQuantity(Order $Order)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
703
    {
704
        // 配送料無料条件(合計数量)
705 42
        $deliveryFreeQuantity = $this->BaseInfo->getDeliveryFreeQuantity();
706 42
        if (!is_null($deliveryFreeQuantity)) {
707
            // 合計数量が設定数量以上であれば送料無料
708 1
            if ($this->orderService->getTotalQuantity($Order) >= $deliveryFreeQuantity) {
0 ignored issues
show
Deprecated Code introduced by
The method Eccube\Service\OrderService::getTotalQuantity() has been deprecated with message: since 3.0.0, to be removed in 3.1

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
709 1
                $Order->setDeliveryFeeTotal(0);
710
                // お届け先情報の配送料も0にセット
711 1
                $shippings = $Order->getShippings();
712 1
                foreach ($shippings as $Shipping) {
713 1
                    $Shipping->setShippingDeliveryFee(0);
714
                }
715
            }
716
        }
717
    }
718
719
720
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$em" missing
Loading history...
721
     * 商品公開ステータスチェック、在庫チェック、購入制限数チェックを行い、在庫情報をロックする
722
     *
723
     * @param $em トランザクション制御されているEntityManager
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
724
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 57 spaces after parameter type; 1 found
Loading history...
725
     * @return bool true : 成功、false : 失敗
726
     */
727 13
    public function isOrderProduct($em, \Eccube\Entity\Order $Order)
728
    {
729
        // 商品公開ステータスチェック
730 13
        $orderDetails = $Order->getOrderDetails();
731
732 13
        foreach ($orderDetails as $orderDetail) {
733 13
            if ($orderDetail->getProduct()->getStatus()->getId() != \Eccube\Entity\Master\Disp::DISPLAY_SHOW) {
734
                // 商品が非公開ならエラー
735 1
                return false;
736
            }
737
738
            // 購入制限数チェック
739 12
            if (!is_null($orderDetail->getProductClass()->getSaleLimit())) {
740 2
                if ($orderDetail->getQuantity() > $orderDetail->getProductClass()->getSaleLimit()) {
741 12
                    return false;
742
                }
743
            }
744
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
745
        }
746
747
        // 在庫チェック
748 11
        foreach ($orderDetails as $orderDetail) {
749
            // 在庫が無制限かチェックし、制限ありなら在庫数をチェック
750 11
            if ($orderDetail->getProductClass()->getStockUnlimited() == Constant::DISABLED) {
751
                // 在庫チェックあり
752
                // 在庫に対してロック(select ... for update)を実行
753 1
                $productStock = $em->getRepository('Eccube\Entity\ProductStock')->find(
754 1
                    $orderDetail->getProductClass()->getProductStock()->getId(), LockMode::PESSIMISTIC_WRITE
755
                );
756
                // 購入数量と在庫数をチェックして在庫がなければエラー
757 1
                if ($orderDetail->getQuantity() > $productStock->getStock()) {
758 11
                    return false;
759
                }
760
            }
761
        }
762
763 10
        return true;
764
765
    }
766
767
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$data" missing
Loading history...
768
     * 受注情報、お届け先情報の更新
769
     *
770
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 22 spaces after parameter type; 1 found
Loading history...
771
     * @param $data フォームデータ
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
772
     *
773
     * @deprecated since 3.0.5, to be removed in 3.1
774
     */
775
    public function setOrderUpdate(Order $Order, $data)
776
    {
777
        // 受注情報を更新
778
        $Order->setOrderDate(new \DateTime());
779
        $Order->setOrderStatus($this->app['eccube.repository.order_status']->find($this->app['config']['order_new']));
780
        $Order->setMessage($data['message']);
781
        // お届け先情報を更新
782
        $shippings = $data['shippings'];
783
        foreach ($shippings as $Shipping) {
784
            $Delivery = $Shipping->getDelivery();
785
            $deliveryFee = $this->app['eccube.repository.delivery_fee']->findOneBy(array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
786
                'Delivery' => $Delivery,
787
                'Pref' => $Shipping->getPref()
788
            ));
789
            $deliveryTime = $Shipping->getDeliveryTime();
790
            if (!empty($deliveryTime)) {
791
                $Shipping->setShippingDeliveryTime($deliveryTime->getDeliveryTime());
792
            }
793
            $Shipping->setDeliveryFee($deliveryFee);
794
            // 商品ごとの配送料合計
795
            $productDeliveryFeeTotal = 0;
796
            if (!is_null($this->BaseInfo->getOptionProductDeliveryFee())) {
797
                $productDeliveryFeeTotal += $this->getProductDeliveryFee($Shipping);
798
            }
799
            $Shipping->setShippingDeliveryFee($deliveryFee->getFee() + $productDeliveryFeeTotal);
800
            $Shipping->setShippingDeliveryName($Delivery->getName());
801
        }
802
        // 配送料無料条件(合計金額)
803
        $this->setDeliveryFreeAmount($Order);
804
        // 配送料無料条件(合計数量)
805
        $this->setDeliveryFreeQuantity($Order);
806
    }
807
808
809
    /**
810
     * 受注情報の更新
811
     *
812
     * @param Order $Order 受注情報
813
     */
814 10
    public function setOrderUpdateData(Order $Order)
815
    {
816
        // 受注情報を更新
817 10
        $Order->setOrderDate(new \DateTime());
818 10
        $OrderStatus = $this->app['eccube.repository.order_status']->find($this->app['config']['order_new']);
819 10
        $this->setOrderStatus($Order, $OrderStatus);
820
821
    }
822
823
824
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$em" missing
Loading history...
825
     * 在庫情報の更新
826
     *
827
     * @param $em トランザクション制御されているEntityManager
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
828
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 57 spaces after parameter type; 1 found
Loading history...
829
     */
830 10
    public function setStockUpdate($em, Order $Order)
831
    {
832
833 10
        $orderDetails = $Order->getOrderDetails();
834
835
        // 在庫情報更新
836 10
        foreach ($orderDetails as $orderDetail) {
837
            // 在庫が無制限かチェックし、制限ありなら在庫数を更新
838 10
            if ($orderDetail->getProductClass()->getStockUnlimited() == Constant::DISABLED) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
839
840 1
                $productStock = $em->getRepository('Eccube\Entity\ProductStock')->find(
841 1
                    $orderDetail->getProductClass()->getProductStock()->getId()
842
                );
843
844
                // 在庫情報の在庫数を更新
845 1
                $stock = $productStock->getStock() - $orderDetail->getQuantity();
846 1
                $productStock->setStock($stock);
847
848
                // 商品規格情報の在庫数を更新
849 10
                $orderDetail->getProductClass()->setStock($stock);
850
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
851
            }
852
        }
853
854
    }
855
856
857
    /**
858
     * 会員情報の更新
859
     *
860
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
861
     * @param Customer $user ログインユーザ
0 ignored issues
show
introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
862
     */
863 6
    public function setCustomerUpdate(Order $Order, Customer $user)
864
    {
865
866 6
        $orderDetails = $Order->getOrderDetails();
0 ignored issues
show
Unused Code introduced by
$orderDetails 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...
867
868
        // 顧客情報を更新
869 6
        $now = new \DateTime();
870 6
        $firstBuyDate = $user->getFirstBuyDate();
871 6
        if (empty($firstBuyDate)) {
872 6
            $user->setFirstBuyDate($now);
873
        }
874 6
        $user->setLastBuyDate($now);
875
876 6
        $user->setBuyTimes($user->getBuyTimes() + 1);
877 6
        $user->setBuyTotal($user->getBuyTotal() + $Order->getTotal());
878
879
    }
880
881
882
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$payments" missing
Loading history...
introduced by
Doc comment for parameter "$subTotal" missing
Loading history...
883
     * 支払方法選択の表示設定
884
     *
885
     * @param $payments 支払選択肢情報
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
886
     * @param $subTotal 小計
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
887
     * @return array
888
     */
889 43
    public function getPayments($payments, $subTotal)
890
    {
891 43
        $pays = array();
892 43
        foreach ($payments as $payment) {
893
            // 支払方法の制限値内であれば表示
894 43
            if (!is_null($payment)) {
895 43
                $pay = $this->app['eccube.repository.payment']->find($payment['id']);
896 43
                if (intval($pay->getRuleMin()) <= $subTotal) {
897 43
                    if (is_null($pay->getRuleMax()) || $pay->getRuleMax() >= $subTotal) {
898 43
                        $pays[] = $pay;
899
                    }
900
                }
901
            }
902
        }
903
904 43
        return $pays;
905
906
    }
907
908
    /**
909
     * お届け日を取得
910
     *
911
     * @param Order $Order
912
     * @return array
913
     */
914 34
    public function getFormDeliveryDates(Order $Order)
915
    {
916
917
        // お届け日の設定
918 34
        $minDate = 0;
919 34
        $deliveryDateFlag = false;
920
921
        // 配送時に最大となる商品日数を取得
922 34
        foreach ($Order->getOrderDetails() as $detail) {
923 34
            $deliveryDate = $detail->getProductClass()->getDeliveryDate();
924 34
            if (!is_null($deliveryDate)) {
925 2
                if ($deliveryDate->getValue() < 0) {
926
                    // 配送日数がマイナスの場合はお取り寄せなのでスキップする
927 1
                    $deliveryDateFlag = false;
928 1
                    break;
929
                }
930
931 1
                if ($minDate < $deliveryDate->getValue()) {
932
                    $minDate = $deliveryDate->getValue();
933
                }
934
                // 配送日数が設定されている
935 34
                $deliveryDateFlag = true;
936
            }
937
        }
938
939
        // 配達最大日数期間を設定
940 34
        $deliveryDates = array();
941
942
        // 配送日数が設定されている
943 34
        if ($deliveryDateFlag) {
944 1
            $period = new \DatePeriod (
0 ignored issues
show
introduced by
Use parentheses when instantiating classes
Loading history...
Coding Style introduced by
Space before opening parenthesis of function call prohibited
Loading history...
945 1
                new \DateTime($minDate.' day'),
946 1
                new \DateInterval('P1D'),
947 1
                new \DateTime($minDate + $this->app['config']['deliv_date_end_max'].' day')
948
            );
949
950 1
            foreach ($period as $day) {
951 1
                $deliveryDates[$day->format('Y/m/d')] = $day->format('Y/m/d');
952
            }
953
        }
954
955 34
        return $deliveryDates;
956
957
    }
958
959
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$deliveries" missing
Loading history...
960
     * 支払方法を取得
961
     *
962
     * @param $deliveries
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
963
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
964
     * @return array
965
     */
966 34
    public function getFormPayments($deliveries, Order $Order)
967
    {
968
969 34
        $productTypes = $this->orderService->getProductTypes($Order);
0 ignored issues
show
Deprecated Code introduced by
The method Eccube\Service\OrderService::getProductTypes() has been deprecated with message: since 3.0.0, to be removed in 3.1

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
970
971 34
        if ($this->BaseInfo->getOptionMultipleShipping() == Constant::ENABLED && count($productTypes) > 1) {
972
            // 複数配送時の支払方法
973
974
            $payments = $this->app['eccube.repository.payment']->findAllowedPayments($deliveries);
975
        } else {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
976
977
            // 配送業者をセット
978 34
            $shippings = $Order->getShippings();
979 34
            $Shipping = $shippings[0];
980 34
            $payments = $this->app['eccube.repository.payment']->findPayments($Shipping->getDelivery(), true);
981
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
982
        }
983 34
        $payments = $this->getPayments($payments, $Order->getSubTotal());
984
985 34
        return $payments;
986
987
    }
988
989
    /**
990
     * お届け先ごとにFormを作成
991
     *
992
     * @param Order $Order
993
     * @return \Symfony\Component\Form\Form
994
     * @deprecated since 3.0, to be removed in 3.1
995
     */
996 View Code Duplication
    public function getShippingForm(Order $Order)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
997
    {
998
        $message = $Order->getMessage();
999
1000
        $deliveries = $this->getDeliveriesOrder($Order);
1001
1002
        // 配送業者の支払方法を取得
1003
        $payments = $this->getFormPayments($deliveries, $Order);
1004
1005
        $builder = $this->app['form.factory']->createBuilder('shopping', null, array(
1006
            'payments' => $payments,
1007
            'payment' => $Order->getPayment(),
1008
            'message' => $message,
1009
        ));
1010
1011
        $builder
1012
            ->add('shippings', 'collection', array(
1013
                'type' => 'shipping_item',
1014
                'data' => $Order->getShippings(),
1015
            ));
1016
1017
        $form = $builder->getForm();
1018
1019
        return $form;
1020
1021
    }
1022
1023
    /**
1024
     * お届け先ごとにFormBuilderを作成
1025
     *
1026
     * @param Order $Order
1027
     * @return \Symfony\Component\Form\FormBuilderInterface
1028
     */
1029 32 View Code Duplication
    public function getShippingFormBuilder(Order $Order)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
1030
    {
1031 32
        $message = $Order->getMessage();
1032
1033 32
        $deliveries = $this->getDeliveriesOrder($Order);
1034
1035
        // 配送業者の支払方法を取得
1036 32
        $payments = $this->getFormPayments($deliveries, $Order);
1037
1038 32
        $builder = $this->app['form.factory']->createBuilder('shopping', null, array(
1039 32
            'payments' => $payments,
1040 32
            'payment' => $Order->getPayment(),
1041 32
            'message' => $message,
1042
        ));
1043
1044
        $builder
1045 32
            ->add('shippings', 'collection', array(
1046 32
                'type' => 'shipping_item',
1047 32
                'data' => $Order->getShippings(),
1048
            ));
1049
1050 32
        return $builder;
1051
1052
    }
1053
1054
1055
    /**
1056
     * フォームデータを更新
1057
     *
1058
     * @param Order $Order
1059
     * @param array $data
1060
     */
1061 10
    public function setFormData(Order $Order, array $data)
1062
    {
1063
1064
        // お問い合わせ
1065 10
        $Order->setMessage($data['message']);
1066
1067
        // お届け先情報を更新
1068 10
        $shippings = $data['shippings'];
1069 10
        foreach ($shippings as $Shipping) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
1070
1071 10
            $deliveryTime = $Shipping->getDeliveryTime();
1072 10
            if (!empty($deliveryTime)) {
1073 10
                $Shipping->setShippingDeliveryTime($deliveryTime->getDeliveryTime());
1074
            }
1075
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
1076
        }
1077
1078
    }
1079
1080
    /**
1081
     * 配送料の合計金額を計算
1082
     *
1083
     * @param Order $Order
1084
     * @return Order
1085
     */
1086 9 View Code Duplication
    public function calculateDeliveryFee(Order $Order)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
1087
    {
1088
1089
        // 配送業者を取得
1090 9
        $shippings = $Order->getShippings();
1091
1092
        // 配送料合計金額
1093 9
        $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($shippings));
1094
1095
        // 配送料無料条件(合計金額)
1096 9
        $this->setDeliveryFreeAmount($Order);
1097
1098
        // 配送料無料条件(合計数量)
1099 9
        $this->setDeliveryFreeQuantity($Order);
1100
1101 9
        return $Order;
1102
1103
    }
1104
1105
1106
    /**
1107
     * 購入処理を行う
1108
     *
1109
     * @param Order $Order
1110
     * @throws ShoppingException
1111
     */
1112 9
    public function processPurchase(Order $Order)
1113
    {
1114
1115 9
        $em = $this->app['orm.em'];
1116
1117
        // 合計金額の再計算
1118 9
        $this->calculatePrice($Order);
1119
1120
        // 商品公開ステータスチェック、商品制限数チェック、在庫チェック
1121 9
        $check = $this->isOrderProduct($em, $Order);
1122 9
        if (!$check) {
1123
            throw new ShoppingException('front.shopping.stock.error');
1124
        }
1125
1126
        // 受注情報、配送情報を更新
1127 9
        $Order = $this->calculateDeliveryFee($Order);
1128 9
        $this->setOrderUpdateData($Order);
1129
        // 在庫情報を更新
1130 9
        $this->setStockUpdate($em, $Order);
1131
1132 9
        if ($this->app->isGranted('ROLE_USER')) {
1133
            // 会員の場合、購入金額を更新
1134 5
            $this->setCustomerUpdate($Order, $this->app->user());
1135
        }
1136
1137
    }
1138
1139
1140
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$discount" missing
Loading history...
1141
     * 値引き可能かチェック
1142
     *
1143
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
1144
     * @param       $discount
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
1145
     * @return bool
1146
     */
1147
    public function isDiscount(Order $Order, $discount)
1148
    {
1149
1150
        if ($Order->getTotal() < $discount) {
1151
            return false;
1152
        }
1153
1154
        return true;
1155
    }
1156
1157
1158
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$discount" missing
Loading history...
1159
     * 値引き金額をセット
1160
     *
1161
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
1162
     * @param $discount
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
1163
     */
1164
    public function setDiscount(Order $Order, $discount)
1165
    {
1166
1167
        $Order->setDiscount($Order->getDiscount() + $discount);
1168
1169
    }
1170
1171
1172
    /**
1173
     * 合計金額を計算
1174
     *
1175
     * @param Order $Order
1176
     * @return Order
1177
     */
1178 41
    public function calculatePrice(Order $Order)
1179
    {
1180
1181 41
        $total = $Order->getTotalPrice();
1182
1183 41
        if ($total < 0) {
1184
            // 合計金額がマイナスの場合、0を設定し、discountは値引きされた額のみセット
1185
            $total = 0;
1186
        }
1187
1188 41
        $Order->setTotal($total);
1189 41
        $Order->setPaymentTotal($total);
1190
1191 41
        return $Order;
1192
1193
    }
1194
1195
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$status" missing
Loading history...
1196
     * 受注ステータスをセット
1197
     *
1198
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
1199
     * @param $status
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
1200
     * @return Order
1201
     */
1202 10
    public function setOrderStatus(Order $Order, $status)
1203
    {
1204
1205 10
        $Order->setOrderDate(new \DateTime());
1206 10
        $Order->setOrderStatus($this->app['eccube.repository.order_status']->find($status));
1207
1208 10
        $event = new EventArgs(
1209
            array(
1210 10
                'Order' => $Order,
1211
            ),
1212 10
            null
1213
        );
1214 10
        $this->app['eccube.event.dispatcher']->dispatch(EccubeEvents::SERVICE_SHOPPING_ORDER_STATUS, $event);
1215
1216 10
        return $Order;
1217
1218
    }
1219
1220
    /**
1221
     * 受注メール送信を行う
1222
     *
1223
     * @param Order $Order
1224
     * @return MailHistory
1225
     */
1226 9
    public function sendOrderMail(Order $Order)
1227
    {
1228
1229
        // メール送信
1230 9
        $message = $this->app['eccube.service.mail']->sendOrderMail($Order);
1231
1232
        // 送信履歴を保存.
1233 9
        $MailTemplate = $this->app['eccube.repository.mail_template']->find(1);
1234
1235 9
        $MailHistory = new MailHistory();
1236
        $MailHistory
1237 9
            ->setSubject($message->getSubject())
1238 9
            ->setMailBody($message->getBody())
1239 9
            ->setMailTemplate($MailTemplate)
1240 9
            ->setSendDate(new \DateTime())
1241 9
            ->setOrder($Order);
1242
1243 9
        $this->app['orm.em']->persist($MailHistory);
1244 9
        $this->app['orm.em']->flush($MailHistory);
1245
1246 9
        return $MailHistory;
1247
1248
    }
1249
1250
1251
    /**
1252
     * 受注処理完了通知
1253
     *
1254
     * @param Order $Order
1255
     */
1256
    public function notifyComplete(Order $Order)
1257
    {
1258
1259
        $event = new EventArgs(
1260
            array(
1261
                'Order' => $Order,
1262
            ),
1263
            null
1264
        );
1265
        $this->app['eccube.event.dispatcher']->dispatch(EccubeEvents::SERVICE_SHOPPING_NOTIFY_COMPLETE, $event);
1266
1267
    }
1268
1269
1270
}
1271