Failed Conditions
Push — master ( 3ed8ee...0f3867 )
by Kentaro
19:22
created

ShoppingService::isOrderProduct()   C

Complexity

Conditions 8
Paths 14

Size

Total Lines 39
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

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