Completed
Push — master ( 0096af...94ef0b )
by Kentaro
29:45
created

ShoppingService   D

Complexity

Total Complexity 86

Size/Duplication

Total Lines 955
Duplicated Lines 3.35 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 42.61%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 86
lcom 1
cbo 5
dl 32
loc 955
rs 4.4444
ccs 121
cts 284
cp 0.4261
c 3
b 0
f 0

30 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B getOrder() 0 24 3
A getNonMember() 0 15 2
A createOrder() 0 15 1
A registerPreOrder() 0 63 2
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
B getAmount() 0 24 1
A setShippingDeliveryFee() 0 20 3
A setDeliveryFreeAmount() 16 16 4
A setDeliveryFreeQuantity() 16 16 4
C isOrderProduct() 0 39 8
B setOrderUpdate() 0 42 4
B setStockUpdate() 0 25 3
A setCustomerUpdate() 0 17 2
B getPayments() 0 18 6
B getFormDeliveryDates() 0 38 6
A getFormPayments() 0 22 3
B getShippingForm() 0 26 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\Order;
32
use Eccube\Entity\OrderDetail;
33
use Eccube\Entity\Product;
34
use Eccube\Entity\ProductClass;
35
use Eccube\Entity\ShipmentItem;
36
use Eccube\Entity\Shipping;
37
use Eccube\Exception\CartException;
38
use Eccube\Util\Str;
39
40
41
class ShoppingService
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
42
{
43
    /** @var \Eccube\Application */
44
    public $app;
45
46
    /** @var \Eccube\Service\CartService */
47
    protected $cartService;
48
49
    /** @var \Eccube\Service\OrderService */
50
    protected $orderService;
51
52
    /** @var \Eccube\Entity\BaseInfo */
53
    protected $BaseInfo;
54
55
    /** @var  \Doctrine\ORM\EntityManager */
56
    protected $em;
57
58 44
    public function __construct(Application $app, $cartService, $orderService)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
59
    {
60 44
        $this->app = $app;
61 44
        $this->cartService = $cartService;
62 44
        $this->orderService = $orderService;
63
        $this->BaseInfo = $app['eccube.repository.base_info']->get();
64
    }
65
66
    /**
67
     * セッションにセットされた受注情報を取得
68
     *
69
     * @param null $status
70
     * @return null|object
71
     */
72 17
    public function getOrder($status = null)
73
    {
74
75
        // 受注データを取得
76
        $preOrderId = $this->cartService->getPreOrderId();
77 6
        if (!$preOrderId) {
78 10
            return null;
79
        }
80
81
        $condition = array(
82
            'pre_order_id' => $preOrderId,
83 17
        );
84
85
        if (!is_null($status)) {
86
            $condition += array(
87
                'OrderStatus' => $status,
88 14
            );
89
        }
90
91
        $Order = $this->app['eccube.repository.order']->findOneBy($condition);
92
93 17
        return $Order;
94
95 3
    }
96
97
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$sesisonKey" missing
Loading history...
98
     * 非会員情報を取得
99
     *
100
     * @param $sesisonKey
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
101
     * @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...
102
     */
103 9
    public function getNonMember($sesisonKey)
104
    {
105
106
        // 非会員でも一度会員登録されていればショッピング画面へ遷移
107
        $nonMember = $this->app['session']->get($sesisonKey);
108
        if (is_null($nonMember)) {
109 1
            return null;
110
        }
111
112 9
        $Customer = $nonMember['customer'];
113
        $Customer->setPref($this->app['eccube.repository.master.pref']->find($nonMember['pref']));
114
115 9
        return $Customer;
116
117
    }
118
119
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
120
     * 受注情報を作成
121
     *
122
     * @param $Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
123
     * @return \Eccube\Entity\Order
124
     */
125 16
    public function createOrder($Customer)
126
    {
127
        // ランダムなpre_order_idを作成
128
        $preOrderId = sha1(Str::random(32));
129
130
        // 受注情報、受注明細情報、お届け先情報、配送商品情報を作成
131 16
        $Order = $this->registerPreOrder(
132
            $Customer,
133
            $preOrderId);
134
135
        $this->cartService->setPreOrderId($preOrderId);
136
        $this->cartService->save();
137
138 16
        return $Order;
139
    }
140
141
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$preOrderId" missing
Loading history...
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
142
     * 仮受注情報作成
143
     *
144
     * @param $Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
145
     * @param $preOrderId
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
146
     * @return mixed
147
     * @throws \Doctrine\ORM\NoResultException
148
     * @throws \Doctrine\ORM\NonUniqueResultException
149
     */
150 16
    public function registerPreOrder(Customer $Customer, $preOrderId)
151
    {
152
153
        $this->em = $this->app['orm.em'];
154
155
        // 受注情報を作成
156
        $Order = $this->getNewOrder($Customer);
157
        $Order->setPreOrderId($preOrderId);
158
159
        $this->em->persist($Order);
160
161
        // 配送業者情報を取得
162
        $deliveries = $this->getDeliveriesCart();
163
164
        // お届け先情報を作成
165
        $Order = $this->getNewShipping($Order, $Customer, $deliveries);
166
167
        // 受注明細情報、配送商品情報を作成
168
        $Order = $this->getNewDetails($Order);
169
170
        // 小計
171
        $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...
172
173
        // 消費税のみの小計
174
        $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...
175
176
        // 配送料合計金額
177
        $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($Order->getShippings()));
178
179
        // 小計
180
        $Order->setSubTotal($subTotal);
181
182
        // 配送料無料条件(合計金額)
183
        $this->setDeliveryFreeAmount($Order);
184
185
        // 配送料無料条件(合計数量)
186
        $this->setDeliveryFreeQuantity($Order);
187
188
        // 初期選択の支払い方法をセット
189
        $payments = $this->app['eccube.repository.payment']->findAllowedPayments($deliveries);
190
        $payments = $this->getPayments($payments, $subTotal);
191
192
        if (count($payments) > 0) {
193 16
            $payment = $payments[0];
194
            $Order->setPayment($payment);
195
            $Order->setPaymentMethod($payment->getMethod());
196
            $Order->setCharge($payment->getCharge());
197
        } else {
198
            $Order->setCharge(0);
199 16
        }
200
201
        $Order->setTax($tax);
202
203
        $total = $subTotal + $Order->getCharge() + $Order->getDeliveryFeeTotal();
204
205
        $Order->setTotal($total);
206
        $Order->setPaymentTotal($total);
207
208
        $this->em->flush();
209
210 16
        return $Order;
211
212 16
    }
213
214
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
215
     * 受注情報を作成
216
     * @param $Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
217
     * @return \Eccube\Entity\Order
218
     */
219 16
    public function getNewOrder(Customer $Customer)
220
    {
221
        $Order = $this->newOrder();
222
        $this->copyToOrderFromCustomer($Order, $Customer);
223
224 16
        return $Order;
225 16
    }
226
227
228
    /**
229
     * 受注情報を作成
230
     * @return \Eccube\Entity\Order
231
     */
232 16
    public function newOrder()
233
    {
234
        $OrderStatus = $this->app['eccube.repository.order_status']->find($this->app['config']['order_processing']);
235
        $Order = new \Eccube\Entity\Order($OrderStatus);
236 16
        return $Order;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
237
    }
238
239
    /**
240
     * 受注情報を作成
241
     *
242
     * @param \Eccube\Entity\Order $Order
0 ignored issues
show
introduced by
Expected 9 spaces after parameter type; 1 found
Loading history...
243
     * @param \Eccube\Entity\Customer|null $Customer
244
     * @return \Eccube\Entity\Order
245
     */
246 16
    public function copyToOrderFromCustomer(Order $Order, Customer $Customer = null)
247
    {
248
        if (is_null($Customer)) {
249
            return $Order;
250
        }
251
252
        if ($Customer->getId()) {
253
            $Order->setCustomer($Customer);
254
        }
255
        $Order
256
            ->setName01($Customer->getName01())
257
            ->setName02($Customer->getName02())
258
            ->setKana01($Customer->getKana01())
259
            ->setKana02($Customer->getKana02())
260
            ->setCompanyName($Customer->getCompanyName())
261
            ->setEmail($Customer->getEmail())
262
            ->setTel01($Customer->getTel01())
263
            ->setTel02($Customer->getTel02())
264
            ->setTel03($Customer->getTel03())
265
            ->setFax01($Customer->getFax01())
266
            ->setFax02($Customer->getFax02())
267
            ->setFax03($Customer->getFax03())
268
            ->setZip01($Customer->getZip01())
269
            ->setZip02($Customer->getZip02())
270
            ->setZipCode($Customer->getZip01() . $Customer->getZip02())
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
271
            ->setPref($Customer->getPref())
272
            ->setAddr01($Customer->getAddr01())
273
            ->setAddr02($Customer->getAddr02())
274
            ->setSex($Customer->getSex())
275
            ->setBirth($Customer->getBirth())
276
            ->setJob($Customer->getJob());
277
278 16
        return $Order;
279 16
    }
280
281
282
    /**
283
     * 配送業者情報を取得
284
     *
285
     * @return array
286
     */
287
    public function getDeliveriesCart()
288
    {
289
290
        // カートに保持されている商品種別を取得
291
        $productTypes = $this->cartService->getProductTypes();
292
293
        return $this->getDeliveries($productTypes);
294
295
    }
296
297
    /**
298
     * 配送業者情報を取得
299
     *
300
     * @param Order $Order
301
     * @return array
302
     */
303
    public function getDeliveriesOrder(Order $Order)
304
    {
305
306
        // 受注情報から商品種別を取得
307
        $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...
308
309
        return $this->getDeliveries($productTypes);
310
311
    }
312
313
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$productTypes" missing
Loading history...
314
     * 配送業者情報を取得
315
     *
316
     * @param $productTypes
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
317
     * @return array
318
     */
319 10
    public function getDeliveries($productTypes)
320
    {
321
322
        // 商品種別に紐づく配送業者を取得
323
        $deliveries = $this->app['eccube.repository.delivery']->getDeliveries($productTypes);
324
325
        if ($this->BaseInfo->getOptionMultipleShipping() == Constant::ENABLED) {
326
            // 複数配送対応
327
328
            // 支払方法を取得
329
            $payments = $this->app['eccube.repository.payment']->findAllowedPayments($deliveries);
330
331
            if (count($productTypes) > 1) {
332
                // 商品種別が複数ある場合、配送対象となる配送業者を取得
333
                $deliveries = $this->app['eccube.repository.delivery']->findAllowedDeliveries($productTypes, $payments);
334
            }
335
336
        }
337
338 10
        return $deliveries;
339
340
    }
341
342
343
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$deliveries" missing
Loading history...
344
     * お届け先情報を作成
345
     *
346
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
347
     * @param Customer $Customer
0 ignored issues
show
introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
348
     * @param $deliveries
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
349
     * @return Order
350
     */
351 16
    public function getNewShipping(Order $Order, Customer $Customer, $deliveries)
352
    {
353 16
        $productTypes = array();
354
        foreach ($deliveries as $Delivery) {
355
            if (!in_array($Delivery->getProductType()->getId(), $productTypes)) {
356
                $Shipping = new Shipping();
357
358 16
                $this->copyToShippingFromCustomer($Shipping, $Customer)
359 16
                    ->setOrder($Order)
360
                    ->setDelFlg(Constant::DISABLED);
361
362
                // 配送料金の設定
363
                $this->setShippingDeliveryFee($Shipping, $Delivery);
364
365
                $this->em->persist($Shipping);
366
367
                $Order->addShipping($Shipping);
368
369
                $productTypes[] = $Delivery->getProductType()->getId();
370
            }
371
        }
372
373 16
        return $Order;
374 16
    }
375
376
    /**
377
     * お届け先情報を作成
378
     *
379
     * @param \Eccube\Entity\Shipping $Shipping
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
380
     * @param \Eccube\Entity\Customer|null $Customer
381
     * @return \Eccube\Entity\Shipping
382
     */
383 17
    public function copyToShippingFromCustomer(Shipping $Shipping, Customer $Customer = null)
384
    {
385
        if (is_null($Customer)) {
386 1
            return $Shipping;
387
        }
388
389 16
        $CustomerAddress = $this->app['eccube.repository.customer_address']->findOneBy(
390 16
            array('Customer' => $Customer),
391 16
            array('id' => 'ASC')
392
        );
393
394
        if (!is_null($CustomerAddress)) {
395
            $Shipping
396
                ->setName01($CustomerAddress->getName01())
397
                ->setName02($CustomerAddress->getName02())
398
                ->setKana01($CustomerAddress->getKana01())
399
                ->setKana02($CustomerAddress->getKana02())
400
                ->setCompanyName($CustomerAddress->getCompanyName())
401
                ->setTel01($CustomerAddress->getTel01())
402
                ->setTel02($CustomerAddress->getTel02())
403
                ->setTel03($CustomerAddress->getTel03())
404
                ->setFax01($CustomerAddress->getFax01())
405
                ->setFax02($CustomerAddress->getFax02())
406
                ->setFax03($CustomerAddress->getFax03())
407
                ->setZip01($CustomerAddress->getZip01())
408
                ->setZip02($CustomerAddress->getZip02())
409
                ->setZipCode($CustomerAddress->getZip01() . $CustomerAddress->getZip02())
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
410
                ->setPref($CustomerAddress->getPref())
411
                ->setAddr01($CustomerAddress->getAddr01())
412
                ->setAddr02($CustomerAddress->getAddr02());
413
        } else {
414
            $Shipping
415
                ->setName01($Customer->getName01())
416
                ->setName02($Customer->getName02())
417
                ->setKana01($Customer->getKana01())
418
                ->setKana02($Customer->getKana02())
419
                ->setCompanyName($Customer->getCompanyName())
420
                ->setTel01($Customer->getTel01())
421
                ->setTel02($Customer->getTel02())
422
                ->setTel03($Customer->getTel03())
423
                ->setFax01($Customer->getFax01())
424
                ->setFax02($Customer->getFax02())
425
                ->setFax03($Customer->getFax03())
426
                ->setZip01($Customer->getZip01())
427
                ->setZip02($Customer->getZip02())
428
                ->setZipCode($Customer->getZip01() . $Customer->getZip02())
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
429
                ->setPref($Customer->getPref())
430
                ->setAddr01($Customer->getAddr01())
431
                ->setAddr02($Customer->getAddr02());
432 14
        }
433
434 16
        return $Shipping;
435 17
    }
436
437
438
    /**
439
     * 受注明細情報、配送商品情報を作成
440
     *
441
     * @param Order $Order
442
     * @return Order
443
     */
444 16
    public function getNewDetails(Order $Order)
445
    {
446
447
        // 受注詳細, 配送商品
448
        foreach ($this->cartService->getCart()->getCartItems() as $item) {
449
            /* @var $ProductClass \Eccube\Entity\ProductClass */
450
            $ProductClass = $item->getObject();
451
            /* @var $Product \Eccube\Entity\Product */
452
            $Product = $ProductClass->getProduct();
453
454
            $quantity = $item->getQuantity();
455
456
            // 受注明細情報を作成
457
            $OrderDetail = $this->getNewOrderDetail($Product, $ProductClass, $quantity);
458
            $OrderDetail->setOrder($Order);
459
            $Order->addOrderDetail($OrderDetail);
460
461
            // 配送商品情報を作成
462
            $this->getNewShipmentItem($Order, $Product, $ProductClass, $quantity);
463
        }
464
465 16
        return $Order;
466
467 16
    }
468
469
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$quantity" missing
Loading history...
470
     * 受注明細情報を作成
471
     *
472
     * @param Product $Product
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
473
     * @param ProductClass $ProductClass
474
     * @param $quantity
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
475
     * @return \Eccube\Entity\OrderDetail
476
     */
477 16
    public function getNewOrderDetail(Product $Product, ProductClass $ProductClass, $quantity)
478
    {
479
        $OrderDetail = new OrderDetail();
480
        $TaxRule = $this->app['eccube.repository.tax_rule']->getByRule($Product, $ProductClass);
481 16
        $OrderDetail->setProduct($Product)
482 16
            ->setProductClass($ProductClass)
483
            ->setProductName($Product->getName())
484
            ->setProductCode($ProductClass->getCode())
485
            ->setPrice($ProductClass->getPrice02())
486 16
            ->setQuantity($quantity)
487
            ->setTaxRule($TaxRule->getCalcRule()->getId())
488
            ->setTaxRate($TaxRule->getTaxRate());
489
490
        $ClassCategory1 = $ProductClass->getClassCategory1();
491
        if (!is_null($ClassCategory1)) {
492
            $OrderDetail->setClasscategoryName1($ClassCategory1->getName());
493
            $OrderDetail->setClassName1($ClassCategory1->getClassName()->getName());
494
        }
495
        $ClassCategory2 = $ProductClass->getClassCategory2();
496
        if (!is_null($ClassCategory2)) {
497
            $OrderDetail->setClasscategoryName2($ClassCategory2->getName());
498
            $OrderDetail->setClassName2($ClassCategory2->getClassName()->getName());
499
        }
500
501
        $this->em->persist($OrderDetail);
502
503 16
        return $OrderDetail;
504 16
    }
505
506
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$quantity" missing
Loading history...
507
     * 配送商品情報を作成
508
     *
509
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
510
     * @param Product $Product
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
511
     * @param ProductClass $ProductClass
512
     * @param $quantity
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
513
     * @return \Eccube\Entity\ShipmentItem
514
     */
515 16
    public function getNewShipmentItem(Order $Order, Product $Product, ProductClass $ProductClass, $quantity)
516
    {
517
518
        $ShipmentItem = new ShipmentItem();
519
        $shippings = $Order->getShippings();
520
521
        // 選択された商品がどのお届け先情報と関連するかチェック
522 16
        $Shipping = null;
523
        foreach ($shippings as $s) {
524
            if ($s->getDelivery()->getProductType()->getId() == $ProductClass->getProductType()->getId()) {
525
                // 商品種別が同一のお届け先情報と関連させる
526 16
                $Shipping = $s;
527 16
                break;
528
            }
529 16
        }
530
531
        if (is_null($Shipping)) {
532
            // お届け先情報と関連していない場合、エラー
533
            throw new CartException('shopping.delivery.not.producttype');
534
        }
535
536
        // 商品ごとの配送料合計
537 16
        $productDeliveryFeeTotal = 0;
538
        if (!is_null($this->BaseInfo->getOptionProductDeliveryFee())) {
539
            $productDeliveryFeeTotal = $ProductClass->getDeliveryFee() * $quantity;
540
        }
541
542
        $Shipping->setShippingDeliveryFee($Shipping->getShippingDeliveryFee() + $productDeliveryFeeTotal);
543
544 16
        $ShipmentItem->setShipping($Shipping)
545 16
            ->setOrder($Order)
546 16
            ->setProductClass($ProductClass)
547 16
            ->setProduct($Product)
548
            ->setProductName($Product->getName())
549
            ->setProductCode($ProductClass->getCode())
550
            ->setPrice($ProductClass->getPrice02())
551
            ->setQuantity($quantity);
552
553
        $ClassCategory1 = $ProductClass->getClassCategory1();
554
        if (!is_null($ClassCategory1)) {
555
            $ShipmentItem->setClasscategoryName1($ClassCategory1->getName());
556
            $ShipmentItem->setClassName1($ClassCategory1->getClassName()->getName());
557
        }
558
        $ClassCategory2 = $ProductClass->getClassCategory2();
559
        if (!is_null($ClassCategory2)) {
560
            $ShipmentItem->setClasscategoryName2($ClassCategory2->getName());
561
            $ShipmentItem->setClassName2($ClassCategory2->getClassName()->getName());
562
        }
563
        $Shipping->addShipmentItem($ShipmentItem);
564
        $this->em->persist($ShipmentItem);
565
566 16
        return $ShipmentItem;
567
568 16
    }
569
570
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$shippings" missing
Loading history...
571
     * お届け先ごとの送料合計を取得
572
     *
573
     * @param $shippings
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
574
     * @return int
575
     */
576 16
    public function getShippingDeliveryFeeTotal($shippings)
577
    {
578 16
        $deliveryFeeTotal = 0;
579
        foreach ($shippings as $Shipping) {
580
            $deliveryFeeTotal += $Shipping->getShippingDeliveryFee();
581
        }
582
583 16
        return $deliveryFeeTotal;
584
585
    }
586
587
    /**
588
     * 商品ごとの配送料を取得
589
     *
590
     * @param Shipping $Shipping
591
     * @return int
592
     */
593 14
    public function getProductDeliveryFee(Shipping $Shipping)
594
    {
595 14
        $productDeliveryFeeTotal = 0;
596
        $shipmentItems = $Shipping->getShipmentItems();
597
        foreach ($shipmentItems as $ShipmentItem) {
598
            $productDeliveryFeeTotal += $ShipmentItem->getProductClass()->getDeliveryFee() * $ShipmentItem->getQuantity();
599 13
        }
600 14
        return $productDeliveryFeeTotal;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
601 14
    }
602
603
    /**
604
     * 住所などの情報が変更された時に金額の再計算を行う
605
     *
606
     * @param Order $Order
607
     * @return Order
608
     */
609 1
    public function getAmount(Order $Order)
610
    {
611
612
        // 初期選択の配送業者をセット
613
        $shippings = $Order->getShippings();
614
615
        // 配送料合計金額
616
        $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($shippings));
617
618
        // 配送料無料条件(合計金額)
619
        $this->setDeliveryFreeAmount($Order);
620
621
        // 配送料無料条件(合計数量)
622
        $this->setDeliveryFreeQuantity($Order);
623
624
        $total = $Order->getSubTotal() + $Order->getCharge() + $Order->getDeliveryFeeTotal();
625
626
        $Order->setTotal($total);
627
        $Order->setPaymentTotal($total);
628
        $this->app['orm.em']->flush();
629
630 1
        return $Order;
631
632 1
    }
633
634
    /**
635
     * 配送料金の設定
636
     *
637
     * @param Shipping $Shipping
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
638
     * @param Delivery|null $Delivery
639
     */
640 15
    public function setShippingDeliveryFee(Shipping $Shipping, Delivery $Delivery = null)
641
    {
642
        // 配送料金の設定
643
        if (is_null($Delivery)) {
644
            $Delivery = $Shipping->getDelivery();
645
        }
646
        $deliveryFee = $this->app['eccube.repository.delivery_fee']->findOneBy(array('Delivery' => $Delivery, 'Pref' => $Shipping->getPref()));
647
648
        $Shipping->setDeliveryFee($deliveryFee);
649
        $Shipping->setDelivery($Delivery);
650
651
        // 商品ごとの配送料合計
652 15
        $productDeliveryFeeTotal = 0;
653
        if (!is_null($this->BaseInfo->getOptionProductDeliveryFee())) {
654
            $productDeliveryFeeTotal += $this->getProductDeliveryFee($Shipping);
655
        }
656
657
        $Shipping->setShippingDeliveryFee($deliveryFee->getFee() + $productDeliveryFeeTotal);
658
        $Shipping->setShippingDeliveryName($Delivery->getName());
659
    }
660
661
    /**
662
     * 配送料無料条件(合計金額)の条件を満たしていれば配送料金を0に設定
663
     *
664
     * @param Order $Order
665
     */
666 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...
667
    {
668
        // 配送料無料条件(合計金額)
669
        $deliveryFreeAmount = $this->BaseInfo->getDeliveryFreeAmount();
670
        if (!is_null($deliveryFreeAmount)) {
671
            // 合計金額が設定金額以上であれば送料無料
672
            if ($Order->getSubTotal() >= $deliveryFreeAmount) {
673
                $Order->setDeliveryFeeTotal(0);
674
                // お届け先情報の配送料も0にセット
675
                $shippings = $Order->getShippings();
676
                foreach ($shippings as $Shipping) {
677
                    $Shipping->setShippingDeliveryFee(0);
678
                }
679
            }
680
        }
681
    }
682
683
    /**
684
     * 配送料無料条件(合計数量)の条件を満たしていれば配送料金を0に設定
685
     *
686
     * @param Order $Order
687
     */
688 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...
689
    {
690
        // 配送料無料条件(合計数量)
691
        $deliveryFreeQuantity = $this->BaseInfo->getDeliveryFreeQuantity();
692
        if (!is_null($deliveryFreeQuantity)) {
693
            // 合計数量が設定数量以上であれば送料無料
694
            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...
695
                $Order->setDeliveryFeeTotal(0);
696
                // お届け先情報の配送料も0にセット
697
                $shippings = $Order->getShippings();
698
                foreach ($shippings as $Shipping) {
699
                    $Shipping->setShippingDeliveryFee(0);
700
                }
701
            }
702
        }
703
    }
704
705
706
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$em" missing
Loading history...
707
     * 商品公開ステータスチェック、在庫チェック、購入制限数チェックを行い、在庫情報をロックする
708
     *
709
     * @param $em トランザクション制御されているEntityManager
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
710
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 57 spaces after parameter type; 1 found
Loading history...
711
     * @return bool true : 成功、false : 失敗
712
     */
713 8
    public function isOrderProduct($em, \Eccube\Entity\Order $Order)
714
    {
715
        // 商品公開ステータスチェック
716
        $orderDetails = $Order->getOrderDetails();
717
718
        foreach ($orderDetails as $orderDetail) {
719
            if ($orderDetail->getProduct()->getStatus()->getId() != \Eccube\Entity\Master\Disp::DISPLAY_SHOW) {
720
                // 商品が非公開ならエラー
721 1
                return false;
722
            }
723
724
            // 購入制限数チェック
725
            if (!is_null($orderDetail->getProductClass()->getSaleLimit())) {
726
                if ($orderDetail->getQuantity() > $orderDetail->getProductClass()->getSaleLimit()) {
727 1
                    return false;
728
                }
729
            }
730
731 2
        }
732
733
        // 在庫チェック
734
        foreach ($orderDetails as $orderDetail) {
735
            // 在庫が無制限かチェックし、制限ありなら在庫数をチェック
736
            if ($orderDetail->getProductClass()->getStockUnlimited() == Constant::DISABLED) {
737
                // 在庫チェックあり
738
                // 在庫に対してロック(select ... for update)を実行
739 1
                $productStock = $em->getRepository('Eccube\Entity\ProductStock')->find(
740
                    $orderDetail->getProductClass()->getProductStock()->getId(), LockMode::PESSIMISTIC_WRITE
741
                );
742
                // 購入数量と在庫数をチェックして在庫がなければエラー
743
                if ($orderDetail->getQuantity() > $productStock->getStock()) {
744 1
                    return false;
745
                }
746
            }
747 1
        }
748
749 5
        return true;
750
751 8
    }
752
753
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$data" missing
Loading history...
754
     * 受注情報、お届け先情報の更新
755
     *
756
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 22 spaces after parameter type; 1 found
Loading history...
757
     * @param $data フォームデータ
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
758
     */
759 5
    public function setOrderUpdate(Order $Order, $data)
760
    {
761
        // 受注情報を更新
762
        $Order->setOrderDate(new \DateTime());
763
        $Order->setOrderStatus($this->app['eccube.repository.order_status']->find($this->app['config']['order_new']));
764
        $Order->setMessage($data['message']);
765
766
        // お届け先情報を更新
767 5
        $shippings = $data['shippings'];
768
        foreach ($shippings as $Shipping) {
769
770
            $Delivery = $Shipping->getDelivery();
771
772 5
            $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...
773
                'Delivery' => $Delivery,
774 5
                'Pref' => $Shipping->getPref()
775
            ));
776
777
            $deliveryTime = $Shipping->getDeliveryTime();
778 5
            if (!empty($deliveryTime)) {
779
                $Shipping->setShippingDeliveryTime($deliveryTime->getDeliveryTime());
780
            }
781
782
            $Shipping->setDeliveryFee($deliveryFee);
783
784
            // 商品ごとの配送料合計
785 5
            $productDeliveryFeeTotal = 0;
786
            if (!is_null($this->BaseInfo->getOptionProductDeliveryFee())) {
787
                $productDeliveryFeeTotal += $this->getProductDeliveryFee($Shipping);
788
            }
789
790
            $Shipping->setShippingDeliveryFee($deliveryFee->getFee() + $productDeliveryFeeTotal);
791
            $Shipping->setShippingDeliveryName($Delivery->getName());
792
        }
793
794
        // 配送料無料条件(合計金額)
795
        $this->setDeliveryFreeAmount($Order);
796
797
        // 配送料無料条件(合計数量)
798
        $this->setDeliveryFreeQuantity($Order);
799
800
    }
801
802
803
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$em" missing
Loading history...
804
     * 在庫情報の更新
805
     *
806
     * @param $em トランザクション制御されているEntityManager
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
807
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 57 spaces after parameter type; 1 found
Loading history...
808
     */
809 1
    public function setStockUpdate($em, Order $Order)
810
    {
811
812
        $orderDetails = $Order->getOrderDetails();
813
814
        // 在庫情報更新
815
        foreach ($orderDetails as $orderDetail) {
816
            // 在庫が無制限かチェックし、制限ありなら在庫数を更新
817
            if ($orderDetail->getProductClass()->getStockUnlimited() == Constant::DISABLED) {
818
819 1
                $productStock = $em->getRepository('Eccube\Entity\ProductStock')->find(
820
                    $orderDetail->getProductClass()->getProductStock()->getId()
821
                );
822
823
                // 在庫情報の在庫数を更新
824
                $stock = $productStock->getStock() - $orderDetail->getQuantity();
825
                $productStock->setStock($stock);
826
827
                // 商品規格情報の在庫数を更新
828
                $orderDetail->getProductClass()->setStock($stock);
829
830
            }
831
        }
832
833
    }
834
835
836
    /**
837
     * 会員情報の更新
838
     *
839
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
840
     * @param Customer $user ログインユーザ
0 ignored issues
show
introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
841
     */
842 3
    public function setCustomerUpdate(Order $Order, Customer $user)
843
    {
844
845
        $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...
846
847
        // 顧客情報を更新
848
        $now = new \DateTime();
849
        $firstBuyDate = $user->getFirstBuyDate();
850 3
        if (empty($firstBuyDate)) {
851
            $user->setFirstBuyDate($now);
852
        }
853
        $user->setLastBuyDate($now);
854
855
        $user->setBuyTimes($user->getBuyTimes() + 1);
856
        $user->setBuyTotal($user->getBuyTotal() + $Order->getTotal());
857
858
    }
859
860
861
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$payments" missing
Loading history...
introduced by
Doc comment for parameter "$subTotal" missing
Loading history...
862
     * 支払方法選択の表示設定
863
     *
864
     * @param $payments 支払選択肢情報
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
865
     * @param $subTotal 小計
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
866
     * @return array
867
     */
868 10
    public function getPayments($payments, $subTotal)
869
    {
870 10
        $pays = array();
871
        foreach ($payments as $payment) {
872
            // 支払方法の制限値内であれば表示
873
            if (!is_null($payment)) {
874
                $pay = $this->app['eccube.repository.payment']->find($payment['id']);
875
                if (intval($pay->getRuleMin()) <= $subTotal) {
876
                    if (is_null($pay->getRuleMax()) || $pay->getRuleMax() >= $subTotal) {
877
                        $pays[] = $pay;
878
                    }
879
                }
880
            }
881
        }
882
883 10
        return $pays;
884
885
    }
886
887
    /**
888
     * お届け日を取得
889
     *
890
     * @param Order $Order
891
     * @return array
892
     */
893 4
    public function getFormDeliveryDates(Order $Order)
894
    {
895
896
        // お届け日の設定
897 4
        $minDate = 0;
898 4
        $deliveryDateFlag = false;
899
900
        // 配送時に最大となる商品日数を取得
901 4
        foreach ($Order->getOrderDetails() as $detail) {
902
            $deliveryDate = $detail->getProductClass()->getDeliveryDate();
903
            if (!is_null($deliveryDate)) {
904
                if ($minDate < $deliveryDate->getValue()) {
905
                    $minDate = $deliveryDate->getValue();
906
                }
907
                // 配送日数が設定されている
908 1
                $deliveryDateFlag = true;
909
            }
910
        }
911
912
        // 配達最大日数期間を設定
913 4
        $deliveryDates = array();
914
915
        // 配送日数が設定されている
916 4
        if ($deliveryDateFlag) {
917
            $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...
918
                new \DateTime($minDate . ' day'),
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
919
                new \DateInterval('P1D'),
920
                new \DateTime($minDate + $this->app['config']['deliv_date_end_max'] . ' day')
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
921
            );
922
923
            foreach ($period as $day) {
924
                $deliveryDates[$day->format('Y/m/d')] = $day->format('Y/m/d');
925
            }
926
        }
927
928 4
        return $deliveryDates;
929
930 4
    }
931
932
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$deliveries" missing
Loading history...
933
     * 支払方法を取得
934
     *
935
     * @param $deliveries
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
936
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
937
     * @return array
938
     */
939 5
    public function getFormPayments($deliveries, Order $Order)
940
    {
941
942
        $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...
943
944
        if ($this->BaseInfo->getOptionMultipleShipping() == Constant::ENABLED && count($productTypes) > 1) {
945
            // 複数配送時の支払方法
946
947
            $payments = $this->app['eccube.repository.payment']->findAllowedPayments($deliveries);
948
        } else {
949
950
            // 配送業者をセット
951
            $shippings = $Order->getShippings();
952
            $Shipping = $shippings[0];
953
            $payments = $this->app['eccube.repository.payment']->findPayments($Shipping->getDelivery(), true);
954
955
        }
956
        $payments = $this->getPayments($payments, $Order->getSubTotal());
957
958 5
        return $payments;
959
960 5
    }
961
962
    /**
963
     * お届け先ごとにFormを作成
964
     *
965
     * @param Order $Order
966
     * @return \Symfony\Component\Form\Form
967
     */
968 3
    public function getShippingForm(Order $Order)
969
    {
970
        $message = $Order->getMessage();
971
972
        $deliveries = $this->getDeliveriesOrder($Order);
973
974
        // 配送業者の支払方法を取得
975
        $payments = $this->getFormPayments($deliveries, $Order);
976
977
        $builder = $this->app['form.factory']->createBuilder('shopping', null, array(
978
            'payments' => $payments,
979 3
            'payment' => $Order->getPayment(),
980
            'message' => $message,
981
        ));
982
983
        $builder
984
            ->add('shippings', 'collection', array(
985 3
                'type' => 'shipping_item',
986 3
                'data' => $Order->getShippings(),
987
            ));
988
989
        $form = $builder->getForm();
990
991 3
        return $form;
992
993 3
    }
994
995
}
996