Failed Conditions
Pull Request — experimental/3.1 (#2532)
by Kentaro
36:10
created

ShoppingService::setShippingDeliveryFee()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 8
nop 2
dl 0
loc 22
ccs 0
cts 13
cp 0
crap 20
rs 8.9197
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
namespace Eccube\Service;
25
26
use Doctrine\DBAL\LockMode;
27
use Doctrine\ORM\EntityManager;
28
use Eccube\Annotation\Inject;
29
use Eccube\Annotation\Service;
30
use Eccube\Application;
31
use Eccube\Common\Constant;
32
use Eccube\Entity\BaseInfo;
33
use Eccube\Entity\Customer;
34
use Eccube\Entity\Delivery;
35
use Eccube\Entity\MailHistory;
36
use Eccube\Entity\Order;
37
use Eccube\Entity\OrderDetail;
38
use Eccube\Entity\Product;
39
use Eccube\Entity\ProductClass;
40
use Eccube\Entity\OrderItem;
41
use Eccube\Entity\Shipping;
42
use Eccube\Event\EccubeEvents;
43
use Eccube\Event\EventArgs;
44
use Eccube\Exception\CartException;
45
use Eccube\Exception\ShoppingException;
46
use Eccube\Form\Type\ShippingItemType;
47
use Eccube\Repository\CustomerAddressRepository;
48
use Eccube\Repository\DeliveryFeeRepository;
49
use Eccube\Repository\DeliveryRepository;
50
use Eccube\Repository\MailTemplateRepository;
51
use Eccube\Repository\Master\DeviceTypeRepository;
52
use Eccube\Repository\Master\OrderStatusRepository;
53
use Eccube\Repository\Master\PrefRepository;
54
use Eccube\Repository\OrderRepository;
55
use Eccube\Repository\PaymentRepository;
56
use Eccube\Repository\TaxRuleRepository;
57
use Eccube\Util\Str;
58
use Symfony\Component\EventDispatcher\EventDispatcher;
59
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
60
use Symfony\Component\Form\FormFactory;
61
use Symfony\Component\HttpFoundation\Session\Session;
62
63
/**
64
 * @Service
65
 */
66
class ShoppingService
67
{
68
    /**
69
     * @Inject(MailTemplateRepository::class)
70
     * @var MailTemplateRepository
71
     */
72
    protected $mailTemplateRepository;
73
74
    /**
75
     * @Inject(MailService::class)
76
     * @var MailService
77
     */
78
    protected $mailService;
79
80
    /**
81
     * @Inject("eccube.event.dispatcher")
82
     * @var EventDispatcher
83
     */
84
    protected $eventDispatcher;
85
86
    /**
87
     * @Inject("form.factory")
88
     * @var FormFactory
89
     */
90
    protected $formFactory;
91
92
    /**
93
     * @Inject(DeliveryFeeRepository::class)
94
     * @var DeliveryFeeRepository
95
     */
96
    protected $deliveryFeeRepository;
97
98
    /**
99
     * @Inject(TaxRuleRepository::class)
100
     * @var TaxRuleRepository
101
     */
102
    protected $taxRuleRepository;
103
104
    /**
105
     * @Inject(CustomerAddressRepository::class)
106
     * @var CustomerAddressRepository
107
     */
108
    protected $customerAddressRepository;
109
110
    /**
111
     * @Inject(DeliveryRepository::class)
112
     * @var DeliveryRepository
113
     */
114
    protected $deliveryRepository;
115
116
    /**
117
     * @Inject(OrderStatusRepository::class)
118
     * @var OrderStatusRepository
119
     */
120
    protected $orderStatusRepository;
121
122
    /**
123
     * @Inject(PaymentRepository::class)
124
     * @var PaymentRepository
125
     */
126
    protected $paymentRepository;
127
128
    /**
129
     * @Inject(DeviceTypeRepository::class)
130
     * @var DeviceTypeRepository
131
     */
132
    protected $deviceTypeRepository;
133
134
    /**
135
     * @Inject("orm.em")
136
     * @var EntityManager
137
     */
138
    protected $entityManager;
139
140
    /**
141
     * @Inject("config")
142
     * @var array
143
     */
144
    protected $appConfig;
145
146
    /**
147
     * @Inject(PrefRepository::class)
148
     * @var PrefRepository
149
     */
150
    protected $prefRepository;
151
152
    /**
153
     * @Inject("session")
154
     * @var Session
155
     */
156
    protected $session;
157
158
    /**
159
     * @Inject(OrderRepository::class)
160
     * @var OrderRepository
161
     */
162
    protected $orderRepository;
163
164
    /**
165
     * @Inject(BaseInfo::class)
166
     * @var BaseInfo
167
     */
168
    protected $BaseInfo;
169
170
    /**
171
     * @Inject(Application::class)
172
     * @var \Eccube\Application
173
     */
174
    public $app;
175
176
    /**
177
     * @Inject(CartService::class)
178
     * @var \Eccube\Service\CartService
179
     */
180
    protected $cartService;
181
182
    /**
183
     * @var \Eccube\Service\OrderService
184
     *
185
     * @deprecated
186
     */
187
    protected $orderService;
188
189
    /**
190
     * セッションにセットされた受注情報を取得
191
     *
192
     * @param null $status
193
     * @return null|object
194
     */
195 11
    public function getOrder($status = null)
196
    {
197
198
        // 受注データを取得
199 11
        $preOrderId = $this->cartService->getPreOrderId();
200 11
        if (!$preOrderId) {
201 11
            return null;
202
        }
203
204
        $condition = array(
205 7
            'pre_order_id' => $preOrderId,
206
        );
207
208 7
        if (!is_null($status)) {
209
            $condition += array(
210 7
                'OrderStatus' => $status,
211
            );
212
        }
213
214 7
        $Order = $this->orderRepository->findOneBy($condition);
215
216 7
        return $Order;
217
218
    }
219
220
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$sesisonKey" missing
Loading history...
221
     * 非会員情報を取得
222
     *
223
     * @param $sesisonKey
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
224
     * @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...
225
     */
226 4
    public function getNonMember($sesisonKey)
227
    {
228
229
        // 非会員でも一度会員登録されていればショッピング画面へ遷移
230 4
        $nonMember = $this->session->get($sesisonKey);
231 4
        if (is_null($nonMember)) {
232 1
            return null;
233
        }
234 3
        if (!array_key_exists('customer', $nonMember) || !array_key_exists('pref', $nonMember)) {
235
            return null;
236
        }
237
238 3
        $Customer = $nonMember['customer'];
239 3
        $Customer->setPref($this->prefRepository->find($nonMember['pref']));
240
241 3
        foreach ($Customer->getCustomerAddresses() as $CustomerAddress) {
242 3
            $Pref = $CustomerAddress->getPref();
243 3
            if ($Pref) {
244 3
                $CustomerAddress->setPref($this->prefRepository->find($Pref->getId()));
245
            }
246
        }
247
248 3
        return $Customer;
249
250
    }
251
252
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
253
     * 受注情報を作成
254
     *
255
     * @param $Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
256
     * @return \Eccube\Entity\Order
257
     */
258
    public function createOrder($Customer)
259
    {
260
        // ランダムなpre_order_idを作成
261 View Code Duplication
        do {
262
            $preOrderId = sha1(Str::random(32));
263
            $Order = $this->orderRepository->findOneBy(array(
264
                'pre_order_id' => $preOrderId,
265
                'OrderStatus' => $this->appConfig['order_processing'],
266
            ));
267
        } while ($Order);
268
269
        // 受注情報、受注明細情報、お届け先情報、配送商品情報を作成
270
        $Order = $this->registerPreOrder(
271
            $Customer,
272
            $preOrderId);
273
274
        $this->cartService->setPreOrderId($preOrderId);
275
        $this->cartService->save();
276
277
        return $Order;
278
    }
279
280
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
introduced by
Doc comment for parameter "$preOrderId" missing
Loading history...
281
     * 仮受注情報作成
282
     *
283
     * @param $Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
284
     * @param $preOrderId
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
285
     * @return mixed
286
     * @throws \Doctrine\ORM\NoResultException
287
     * @throws \Doctrine\ORM\NonUniqueResultException
288
     */
289
    public function registerPreOrder(Customer $Customer, $preOrderId)
290
    {
291
292
        $this->em = $this->entityManager;
293
294
        // 受注情報を作成
295
        $Order = $this->getNewOrder($Customer);
296
        $Order->setPreOrderId($preOrderId);
297
298
        $DeviceType = $this->deviceTypeRepository->find($this->app['mobile_detect.device_type']);
299
        $Order->setDeviceType($DeviceType);
300
301
        $this->entityManager->persist($Order);
302
303
        // 配送業者情報を取得
304
        $deliveries = $this->getDeliveriesCart();
305
306
        // お届け先情報を作成
307
        $Order = $this->getNewShipping($Order, $Customer, $deliveries);
308
309
        // 受注明細情報、配送商品情報を作成
310
        $Order = $this->getNewDetails($Order);
311
312
        // 小計
313
        $subTotal = $this->orderService->getSubTotal($Order);
0 ignored issues
show
Deprecated Code introduced by
The property Eccube\Service\ShoppingService::$orderService has been deprecated.

This property 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 property will be removed from the class and what other property to use instead.

Loading history...
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...
314
315
        // 消費税のみの小計
316
        $tax = $this->orderService->getTotalTax($Order);
0 ignored issues
show
Deprecated Code introduced by
The property Eccube\Service\ShoppingService::$orderService has been deprecated.

This property 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 property will be removed from the class and what other property to use instead.

Loading history...
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...
317
318
        // 配送料合計金額
319
        // TODO CalculateDeliveryFeeStrategy でセットする
320
        // $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($Order->getShippings()));
321
322
        // 小計
323
        $Order->setSubTotal($subTotal);
324
325
        // 配送料無料条件(合計金額)
326
        $this->setDeliveryFreeAmount($Order);
327
328
        // 配送料無料条件(合計数量)
329
        $this->setDeliveryFreeQuantity($Order);
330
331
        // 初期選択の支払い方法をセット
332
        $payments = $this->paymentRepository->findAllowedPayments($deliveries);
333
        $payments = $this->getPayments($payments, $subTotal);
334
335
        if (count($payments) > 0) {
336
            $payment = $payments[0];
337
            $Order->setPayment($payment);
338
            $Order->setPaymentMethod($payment->getMethod());
339
            $Order->setCharge($payment->getCharge());
340
        } else {
341
            $Order->setCharge(0);
342
        }
343
344
        $Order->setTax($tax);
345
346
        // 合計金額の計算
347
        $this->calculatePrice($Order);
348
349
        $this->entityManager->flush();
350
351
        return $Order;
352
353
    }
354
355
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
356
     * 受注情報を作成
357
     *
358
     * @param $Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
359
     * @return \Eccube\Entity\Order
360
     */
361
    public function getNewOrder(Customer $Customer)
362
    {
363
        $Order = $this->newOrder();
364
        $this->copyToOrderFromCustomer($Order, $Customer);
365
366
        return $Order;
367
    }
368
369
370
    /**
371
     * 受注情報を作成
372
     *
373
     * @return \Eccube\Entity\Order
374
     */
375
    public function newOrder()
376
    {
377
        $OrderStatus = $this->orderStatusRepository->find($this->appConfig['order_processing']);
378
        $Order = new \Eccube\Entity\Order($OrderStatus);
379
380
        return $Order;
381
    }
382
383
    /**
384
     * 受注情報を作成
385
     *
386
     * @param \Eccube\Entity\Order $Order
0 ignored issues
show
introduced by
Expected 9 spaces after parameter type; 1 found
Loading history...
387
     * @param \Eccube\Entity\Customer|null $Customer
388
     * @return \Eccube\Entity\Order
389
     */
390
    public function copyToOrderFromCustomer(Order $Order, Customer $Customer = null)
391
    {
392
        if (is_null($Customer)) {
393
            return $Order;
394
        }
395
396
        if ($Customer->getId()) {
397
            $Order->setCustomer($Customer);
398
        }
399
        $Order
400
            ->setName01($Customer->getName01())
401
            ->setName02($Customer->getName02())
402
            ->setKana01($Customer->getKana01())
403
            ->setKana02($Customer->getKana02())
404
            ->setCompanyName($Customer->getCompanyName())
405
            ->setEmail($Customer->getEmail())
406
            ->setTel01($Customer->getTel01())
407
            ->setTel02($Customer->getTel02())
408
            ->setTel03($Customer->getTel03())
409
            ->setFax01($Customer->getFax01())
410
            ->setFax02($Customer->getFax02())
411
            ->setFax03($Customer->getFax03())
412
            ->setZip01($Customer->getZip01())
413
            ->setZip02($Customer->getZip02())
414
            ->setZipCode($Customer->getZip01().$Customer->getZip02())
415
            ->setPref($Customer->getPref())
416
            ->setAddr01($Customer->getAddr01())
417
            ->setAddr02($Customer->getAddr02())
418
            ->setSex($Customer->getSex())
419
            ->setBirth($Customer->getBirth())
420
            ->setJob($Customer->getJob());
421
422
        return $Order;
423
    }
424
425
426
    /**
427
     * 配送業者情報を取得
428
     *
429
     * @return array
430
     */
431
    public function getDeliveriesCart()
432
    {
433
434
        // カートに保持されている商品種別を取得
435
        $productTypes = $this->cartService->getProductTypes();
0 ignored issues
show
Bug introduced by
The method getProductTypes() does not seem to exist on object<Eccube\Service\CartService>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
436
437
        return $this->getDeliveries($productTypes);
438
439
    }
440
441
    /**
442
     * 配送業者情報を取得
443
     *
444
     * @param Order $Order
445
     * @return array
446
     */
447
    public function getDeliveriesOrder(Order $Order)
448
    {
449
450
        // 受注情報から商品種別を取得
451
        $productTypes = $this->orderService->getProductTypes($Order);
0 ignored issues
show
Deprecated Code introduced by
The property Eccube\Service\ShoppingService::$orderService has been deprecated.

This property 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 property will be removed from the class and what other property to use instead.

Loading history...
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...
452
453
        return $this->getDeliveries($productTypes);
454
455
    }
456
457
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$productTypes" missing
Loading history...
458
     * 配送業者情報を取得
459
     *
460
     * @param $productTypes
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
461
     * @return array
462
     */
463 3
    public function getDeliveries($productTypes)
464
    {
465
466
        // 商品種別に紐づく配送業者を取得
467 3
        $deliveries = $this->deliveryRepository->getDeliveries($productTypes);
468
469 3
        if ($this->BaseInfo->getOptionMultipleShipping() == Constant::ENABLED) {
470
            // 複数配送対応
471
472
            // 支払方法を取得
473 1
            $payments = $this->paymentRepository->findAllowedPayments($deliveries);
474
475 1
            if (count($productTypes) > 1) {
476
                // 商品種別が複数ある場合、配送対象となる配送業者を取得
477 1
                $deliveries = $this->deliveryRepository->findAllowedDeliveries($productTypes, $payments);
478
            }
479
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
480
        }
481
482 3
        return $deliveries;
483
484
    }
485
486
487
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$deliveries" missing
Loading history...
488
     * お届け先情報を作成
489
     *
490
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
491
     * @param Customer $Customer
0 ignored issues
show
introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
492
     * @param $deliveries
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
493
     * @return Order
494
     */
495
    public function getNewShipping(Order $Order, Customer $Customer, $deliveries)
496
    {
497
        $productTypes = array();
498
        foreach ($deliveries as $Delivery) {
499
            if (!in_array($Delivery->getProductType()->getId(), $productTypes)) {
500
                $Shipping = new Shipping();
501
502
                $this->copyToShippingFromCustomer($Shipping, $Customer)
503
                    ->setOrder($Order)
504
                    ->setDelFlg(Constant::DISABLED);
505
506
                // 配送料金の設定
507
                $this->setShippingDeliveryFee($Shipping, $Delivery);
508
509
                $this->entityManager->persist($Shipping);
510
511
                $Order->addShipping($Shipping);
512
513
                $productTypes[] = $Delivery->getProductType()->getId();
514
            }
515
        }
516
517
        return $Order;
518
    }
519
520
    /**
521
     * お届け先情報を作成
522
     *
523
     * @param \Eccube\Entity\Shipping $Shipping
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
524
     * @param \Eccube\Entity\Customer|null $Customer
525
     * @return \Eccube\Entity\Shipping
526
     */
527 1
    public function copyToShippingFromCustomer(Shipping $Shipping, Customer $Customer = null)
528
    {
529 1
        if (is_null($Customer)) {
530 1
            return $Shipping;
531
        }
532
533
        $CustomerAddress = $this->customerAddressRepository->findOneBy(
534
            array('Customer' => $Customer),
535
            array('id' => 'ASC')
536
        );
537
538
        if (!is_null($CustomerAddress)) {
539
            $Shipping
540
                ->setName01($CustomerAddress->getName01())
541
                ->setName02($CustomerAddress->getName02())
542
                ->setKana01($CustomerAddress->getKana01())
543
                ->setKana02($CustomerAddress->getKana02())
544
                ->setCompanyName($CustomerAddress->getCompanyName())
545
                ->setTel01($CustomerAddress->getTel01())
546
                ->setTel02($CustomerAddress->getTel02())
547
                ->setTel03($CustomerAddress->getTel03())
548
                ->setFax01($CustomerAddress->getFax01())
549
                ->setFax02($CustomerAddress->getFax02())
550
                ->setFax03($CustomerAddress->getFax03())
551
                ->setZip01($CustomerAddress->getZip01())
552
                ->setZip02($CustomerAddress->getZip02())
553
                ->setZipCode($CustomerAddress->getZip01().$CustomerAddress->getZip02())
554
                ->setPref($CustomerAddress->getPref())
555
                ->setAddr01($CustomerAddress->getAddr01())
556
                ->setAddr02($CustomerAddress->getAddr02());
557
        } else {
558
            $Shipping
559
                ->setName01($Customer->getName01())
560
                ->setName02($Customer->getName02())
561
                ->setKana01($Customer->getKana01())
562
                ->setKana02($Customer->getKana02())
563
                ->setCompanyName($Customer->getCompanyName())
564
                ->setTel01($Customer->getTel01())
565
                ->setTel02($Customer->getTel02())
566
                ->setTel03($Customer->getTel03())
567
                ->setFax01($Customer->getFax01())
568
                ->setFax02($Customer->getFax02())
569
                ->setFax03($Customer->getFax03())
570
                ->setZip01($Customer->getZip01())
571
                ->setZip02($Customer->getZip02())
572
                ->setZipCode($Customer->getZip01().$Customer->getZip02())
573
                ->setPref($Customer->getPref())
574
                ->setAddr01($Customer->getAddr01())
575
                ->setAddr02($Customer->getAddr02());
576
        }
577
578
        return $Shipping;
579
    }
580
581
582
    /**
583
     * 受注明細情報、配送商品情報を作成
584
     *
585
     * @param Order $Order
586
     * @return Order
587
     */
588
    public function getNewDetails(Order $Order)
589
    {
590
591
        // 受注詳細, 配送商品
592
        foreach ($this->cartService->getCart()->getCartItems() as $item) {
593
            /* @var $ProductClass \Eccube\Entity\ProductClass */
594
            $ProductClass = $item->getObject();
595
            /* @var $Product \Eccube\Entity\Product */
596
            $Product = $ProductClass->getProduct();
597
598
            $quantity = $item->getQuantity();
599
600
            // 受注明細情報を作成
601
            $OrderDetail = $this->getNewOrderDetail($Product, $ProductClass, $quantity);
602
            $OrderDetail->setOrder($Order);
603
            $Order->addOrderDetail($OrderDetail);
604
605
            // 配送商品情報を作成
606
            $this->getNewOrderItem($Order, $Product, $ProductClass, $quantity);
607
        }
608
609
        return $Order;
610
611
    }
612
613
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$quantity" missing
Loading history...
614
     * 受注明細情報を作成
615
     *
616
     * @param Product $Product
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
617
     * @param ProductClass $ProductClass
618
     * @param $quantity
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
619
     * @return \Eccube\Entity\OrderDetail
620
     */
621
    public function getNewOrderDetail(Product $Product, ProductClass $ProductClass, $quantity)
622
    {
623
        $OrderDetail = new OrderDetail();
624
        $TaxRule = $this->taxRuleRepository->getByRule($Product, $ProductClass);
625
        $OrderDetail->setProduct($Product)
626
            ->setProductClass($ProductClass)
627
            ->setProductName($Product->getName())
628
            ->setProductCode($ProductClass->getCode())
629
            ->setPrice($ProductClass->getPrice02())
630
            ->setQuantity($quantity)
631
            ->setTaxRule($TaxRule->getRoundingType()->getId())
632
            ->setTaxRate($TaxRule->getTaxRate());
633
634
        $ClassCategory1 = $ProductClass->getClassCategory1();
635
        if (!is_null($ClassCategory1)) {
636
            $OrderDetail->setClasscategoryName1($ClassCategory1->getName());
637
            $OrderDetail->setClassName1($ClassCategory1->getClassName()->getName());
638
        }
639
        $ClassCategory2 = $ProductClass->getClassCategory2();
640
        if (!is_null($ClassCategory2)) {
641
            $OrderDetail->setClasscategoryName2($ClassCategory2->getName());
642
            $OrderDetail->setClassName2($ClassCategory2->getClassName()->getName());
643
        }
644
645
        $this->entityManager->persist($OrderDetail);
646
647
        return $OrderDetail;
648
    }
649
650
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$quantity" missing
Loading history...
651
     * 配送商品情報を作成
652
     *
653
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
654
     * @param Product $Product
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
655
     * @param ProductClass $ProductClass
656
     * @param $quantity
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
657
     * @return \Eccube\Entity\OrderItem
658
     */
659
    public function getNewOrderItem(Order $Order, Product $Product, ProductClass $ProductClass, $quantity)
660
    {
661
662
        $OrderItem = new OrderItem();
663
        $shippings = $Order->getShippings();
664
665
        // 選択された商品がどのお届け先情報と関連するかチェック
666
        $Shipping = null;
667
        foreach ($shippings as $s) {
668
            if ($s->getDelivery()->getProductType()->getId() == $ProductClass->getProductType()->getId()) {
669
                // 商品種別が同一のお届け先情報と関連させる
670
                $Shipping = $s;
671
                break;
672
            }
673
        }
674
675
        if (is_null($Shipping)) {
676
            // お届け先情報と関連していない場合、エラー
677
            throw new CartException('shopping.delivery.not.producttype');
678
        }
679
680
        // 商品ごとの配送料合計
681
        $productDeliveryFeeTotal = 0;
682
        if (!is_null($this->BaseInfo->getOptionProductDeliveryFee())) {
683
            $productDeliveryFeeTotal = $ProductClass->getDeliveryFee() * $quantity;
684
        }
685
686
        $Shipping->setShippingDeliveryFee($Shipping->getShippingDeliveryFee() + $productDeliveryFeeTotal);
687
688
        $OrderItem->setShipping($Shipping)
689
            ->setOrder($Order)
690
            ->setProductClass($ProductClass)
691
            ->setProduct($Product)
692
            ->setProductName($Product->getName())
693
            ->setProductCode($ProductClass->getCode())
694
            ->setPrice($ProductClass->getPrice02())
695
            ->setQuantity($quantity);
696
697
        $ClassCategory1 = $ProductClass->getClassCategory1();
698
        if (!is_null($ClassCategory1)) {
699
            $OrderItem->setClasscategoryName1($ClassCategory1->getName());
700
            $OrderItem->setClassName1($ClassCategory1->getClassName()->getName());
701
        }
702
        $ClassCategory2 = $ProductClass->getClassCategory2();
703
        if (!is_null($ClassCategory2)) {
704
            $OrderItem->setClasscategoryName2($ClassCategory2->getName());
705
            $OrderItem->setClassName2($ClassCategory2->getClassName()->getName());
706
        }
707
        $Shipping->addOrderItem($OrderItem);
708
        $this->entityManager->persist($OrderItem);
709
710
        return $OrderItem;
711
712
    }
713
714
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$shippings" missing
Loading history...
715
     * お届け先ごとの送料合計を取得
716
     *
717
     * @param $shippings
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
718
     * @return int
719
     */
720
    public function getShippingDeliveryFeeTotal($shippings)
721
    {
722
        $deliveryFeeTotal = 0;
723
        foreach ($shippings as $Shipping) {
724
            $deliveryFeeTotal += $Shipping->getShippingDeliveryFee();
725
        }
726
727
        return $deliveryFeeTotal;
728
729
    }
730
731
    /**
732
     * 商品ごとの配送料を取得
733
     *
734
     * @param Shipping $Shipping
735
     * @return int
736
     */
737
    public function getProductDeliveryFee(Shipping $Shipping)
738
    {
739
        $productDeliveryFeeTotal = 0;
740
        $OrderItems = $Shipping->getOrderItems();
741
        foreach ($OrderItems as $OrderItem) {
742
            $productDeliveryFeeTotal += $OrderItem->getProductClass()->getDeliveryFee() * $OrderItem->getQuantity();
743
        }
744
745
        return $productDeliveryFeeTotal;
746
    }
747
748
    /**
749
     * 住所などの情報が変更された時に金額の再計算を行う
750
     * @deprecated PurchaseFlowで行う
751
     * @param Order $Order
752
     * @return Order
753
     */
754 1
    public function getAmount(Order $Order)
755
    {
756
757
        // 初期選択の配送業者をセット
758 1
        $shippings = $Order->getShippings();
0 ignored issues
show
Unused Code introduced by
$shippings 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...
759
760
        // 配送料合計金額
761
        // TODO CalculateDeliveryFeeStrategy でセットする
762
        // $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($shippings));
763
764
        // 配送料無料条件(合計金額)
765 1
        $this->setDeliveryFreeAmount($Order);
766
767
        // 配送料無料条件(合計数量)
768 1
        $this->setDeliveryFreeQuantity($Order);
769
770
        // 合計金額の計算
771 1
        $this->calculatePrice($Order);
772
773 1
        return $Order;
774
775
    }
776
777
    /**
778
     * 配送料金の設定
779
     *
780
     * @param Shipping $Shipping
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
781
     * @param Delivery|null $Delivery
782
     */
783
    public function setShippingDeliveryFee(Shipping $Shipping, Delivery $Delivery = null)
784
    {
785
        // 配送料金の設定
786
        if (is_null($Delivery)) {
787
            $Delivery = $Shipping->getDelivery();
788
        }
789
        $deliveryFee = $this->deliveryFeeRepository->findOneBy(array('Delivery' => $Delivery, 'Pref' => $Shipping->getPref()));
790
        if ($deliveryFee) {
791
            $Shipping->setDeliveryFee($deliveryFee);
792
            $Shipping->setFeeId($deliveryFee->getId());
793
        }
794
        $Shipping->setDelivery($Delivery);
795
796
        // 商品ごとの配送料合計
797
        $productDeliveryFeeTotal = 0;
798
        if (!is_null($this->BaseInfo->getOptionProductDeliveryFee())) {
799
            $productDeliveryFeeTotal += $this->getProductDeliveryFee($Shipping);
800
        }
801
802
        $Shipping->setShippingDeliveryFee($deliveryFee->getFee() + $productDeliveryFeeTotal);
803
        $Shipping->setShippingDeliveryName($Delivery->getName());
804
    }
805
806
    /**
807
     * 配送料無料条件(合計金額)の条件を満たしていれば配送料金を0に設定
808
     *
809
     * @param Order $Order
810
     */
811 4 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...
812
    {
813
        // 配送料無料条件(合計金額)
814 4
        $deliveryFreeAmount = $this->BaseInfo->getDeliveryFreeAmount();
815 4
        if (!is_null($deliveryFreeAmount)) {
816
            // 合計金額が設定金額以上であれば送料無料
817 1
            if ($Order->getSubTotal() >= $deliveryFreeAmount) {
818 1
                $Order->setDeliveryFeeTotal(0);
819
                // お届け先情報の配送料も0にセット
820 1
                $shippings = $Order->getShippings();
821 1
                foreach ($shippings as $Shipping) {
822 1
                    $Shipping->setShippingDeliveryFee(0);
823
                }
824
            }
825
        }
826
    }
827
828
    /**
829
     * 配送料無料条件(合計数量)の条件を満たしていれば配送料金を0に設定
830
     *
831
     * @param Order $Order
832
     */
833 3 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...
834
    {
835
        // 配送料無料条件(合計数量)
836 3
        $deliveryFreeQuantity = $this->BaseInfo->getDeliveryFreeQuantity();
837 3
        if (!is_null($deliveryFreeQuantity)) {
838
            // 合計数量が設定数量以上であれば送料無料
839
            if ($this->orderService->getTotalQuantity($Order) >= $deliveryFreeQuantity) {
0 ignored issues
show
Deprecated Code introduced by
The property Eccube\Service\ShoppingService::$orderService has been deprecated.

This property 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 property will be removed from the class and what other property to use instead.

Loading history...
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...
840
                $Order->setDeliveryFeeTotal(0);
841
                // お届け先情報の配送料も0にセット
842
                $shippings = $Order->getShippings();
843
                foreach ($shippings as $Shipping) {
844
                    $Shipping->setShippingDeliveryFee(0);
845
                }
846
            }
847
        }
848
    }
849
850
851
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$em" missing
Loading history...
852
     * 商品公開ステータスチェック、在庫チェック、購入制限数チェックを行い、在庫情報をロックする
853
     *
854
     * @param $em トランザクション制御されているEntityManager
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
855
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 57 spaces after parameter type; 1 found
Loading history...
856
     * @return bool true : 成功、false : 失敗
857
     */
858 4
    public function isOrderProduct($em, \Eccube\Entity\Order $Order)
859
    {
860 4
        $orderDetails = $Order->getOrderDetails();
861
862
        /** @var OrderItem $orderDetail */
863 4
        foreach ($orderDetails as $orderDetail) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
864
865 4
            if (is_null($orderDetail->getProduct())) {
866
                // FIXME 配送明細を考慮する必要がある
867
                continue;
868
            }
869
870
            // 商品削除チェック
871 4
            if ($orderDetail->getProductClass()->isVisible() == false) {
872
                // @deprecated 3.1以降ではexceptionをthrowする
873
                // throw new ShoppingException('cart.product.delete');
874
                return false;
875
            }
876
877
            // 商品公開ステータスチェック
878 4
            if ($orderDetail->getProduct()->getStatus()->getId() != \Eccube\Entity\Master\ProductStatus::DISPLAY_SHOW) {
879
                // 商品が非公開ならエラー
880
881
                // @deprecated 3.1以降ではexceptionをthrowする
882
                // throw new ShoppingException('cart.product.not.status');
883 1
                return false;
884
            }
885
886
            // 購入制限数チェック
887 3
            if (!is_null($orderDetail->getProductClass()->getSaleLimit())) {
888 2
                if ($orderDetail->getQuantity() > $orderDetail->getProductClass()->getSaleLimit()) {
889
                    // @deprecated 3.1以降ではexceptionをthrowする
890
                    // throw new ShoppingException('cart.over.sale_limit');
891 1
                    return false;
892
                }
893
            }
894
895
            // 購入数チェック
896 2
            if ($orderDetail->getQuantity() < 1) {
897
                // 購入数量が1未満ならエラー
898
899
                // @deprecated 3.1以降ではexceptionをthrowする
900
                // throw new ShoppingException('???');
901 2
                return false;
902
            }
903
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
904
        }
905
906
        // 在庫チェック
907 2
        foreach ($orderDetails as $orderDetail) {
908 2
            if (is_null($orderDetail->getProductClass())) {
909
                // FIXME 配送明細を考慮する必要がある
910
                continue;
911
            }
912
            // 在庫が無制限かチェックし、制限ありなら在庫数をチェック
913 2
            if ($orderDetail->getProductClass()->getStockUnlimited() == Constant::DISABLED) {
914
                // 在庫チェックあり
915
                // 在庫に対してロック(select ... for update)を実行
916 1
                $productStock = $em->getRepository('Eccube\Entity\ProductStock')->find(
917 1
                    $orderDetail->getProductClass()->getProductStock()->getId(), LockMode::PESSIMISTIC_WRITE
918
                );
919
                // 購入数量と在庫数をチェックして在庫がなければエラー
920 1
                if ($productStock->getStock() < 1) {
921
                    // @deprecated 3.1以降ではexceptionをthrowする
922
                    // throw new ShoppingException('cart.over.stock');
923
                    return false;
924 1
                } elseif ($orderDetail->getQuantity() > $productStock->getStock()) {
925
                    // @deprecated 3.1以降ではexceptionをthrowする
926
                    // throw new ShoppingException('cart.over.stock');
927 2
                    return false;
928
                }
929
            }
930
        }
931
932 1
        return true;
933
934
    }
935
936
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$data" missing
Loading history...
937
     * 受注情報、お届け先情報の更新
938
     *
939
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 22 spaces after parameter type; 1 found
Loading history...
940
     * @param $data フォームデータ
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
941
     *
942
     * @deprecated since 3.0.5, to be removed in 3.1
943
     */
944
    public function setOrderUpdate(Order $Order, $data)
945
    {
946
        // 受注情報を更新
947
        $Order->setOrderDate(new \DateTime());
948
        $Order->setOrderStatus($this->orderStatusRepository->find($this->appConfig['order_new']));
949
        $Order->setMessage($data['message']);
950
        // お届け先情報を更新
951
        $shippings = $data['shippings'];
952
        foreach ($shippings as $Shipping) {
953
            $Delivery = $Shipping->getDelivery();
954
            $deliveryFee = $this->deliveryFeeRepository->findOneBy(array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
955
                'Delivery' => $Delivery,
956
                'Pref' => $Shipping->getPref()
957
            ));
958
            $deliveryTime = $Shipping->getDeliveryTime();
959
            if (!empty($deliveryTime)) {
960
                $Shipping->setShippingDeliveryTime($deliveryTime->getDeliveryTime());
961
                $Shipping->setTimeId($deliveryTime->getId());
962
            }
963
            $Shipping->setDeliveryFee($deliveryFee);
964
            // 商品ごとの配送料合計
965
            $productDeliveryFeeTotal = 0;
966
            if (!is_null($this->BaseInfo->getOptionProductDeliveryFee())) {
967
                $productDeliveryFeeTotal += $this->getProductDeliveryFee($Shipping);
968
            }
969
            $Shipping->setShippingDeliveryFee($deliveryFee->getFee() + $productDeliveryFeeTotal);
970
            $Shipping->setShippingDeliveryName($Delivery->getName());
971
        }
972
        // 配送料無料条件(合計金額)
973
        $this->setDeliveryFreeAmount($Order);
974
        // 配送料無料条件(合計数量)
975
        $this->setDeliveryFreeQuantity($Order);
976
    }
977
978
979
    /**
980
     * 受注情報の更新
981
     *
982
     * @param Order $Order 受注情報
983
     */
984 3
    public function setOrderUpdateData(Order $Order)
985
    {
986
        // 受注情報を更新
987 3
        $Order->setOrderDate(new \DateTime()); // XXX 後続の setOrderStatus でも時刻を更新している
988 3
        $OrderStatus = $this->orderStatusRepository->find($this->appConfig['order_new']);
989 3
        $this->setOrderStatus($Order, $OrderStatus);
990
991
    }
992
993
994
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$em" missing
Loading history...
995
     * 在庫情報の更新
996
     *
997
     * @param $em トランザクション制御されているEntityManager
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
998
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 57 spaces after parameter type; 1 found
Loading history...
999
     */
1000 3
    public function setStockUpdate($em, Order $Order)
1001
    {
1002
1003 3
        $orderDetails = $Order->getOrderDetails();
1004
1005
        // 在庫情報更新
1006 3
        foreach ($orderDetails as $orderDetail) {
1007 1
            if (is_null($orderDetail->getProductClass())) {
1008
                // FIXME 配送明細を考慮する必要がある
1009
                continue;
1010
            }
1011
            // 在庫が無制限かチェックし、制限ありなら在庫数を更新
1012 1
            if ($orderDetail->getProductClass()->getStockUnlimited() == Constant::DISABLED) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
1013
1014 1
                $productStock = $em->getRepository('Eccube\Entity\ProductStock')->find(
1015 1
                    $orderDetail->getProductClass()->getProductStock()->getId()
1016
                );
1017
1018
                // 在庫情報の在庫数を更新
1019 1
                $stock = $productStock->getStock() - $orderDetail->getQuantity();
1020 1
                $productStock->setStock($stock);
1021
1022
                // 商品規格情報の在庫数を更新
1023 1
                $orderDetail->getProductClass()->setStock($stock);
1024
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
1025
            }
1026
        }
1027
1028
    }
1029
1030
1031
    /**
1032
     * 会員情報の更新
1033
     *
1034
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
1035
     * @param Customer $user ログインユーザ
0 ignored issues
show
introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
1036
     */
1037 2
    public function setCustomerUpdate(Order $Order, Customer $user)
1038
    {
1039
1040 2
        $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...
1041
1042
        // 顧客情報を更新
1043 2
        $now = new \DateTime();
1044 2
        $firstBuyDate = $user->getFirstBuyDate();
1045 2
        if (empty($firstBuyDate)) {
1046 2
            $user->setFirstBuyDate($now);
1047
        }
1048 2
        $user->setLastBuyDate($now);
1049
1050 2
        $user->setBuyTimes($user->getBuyTimes() + 1);
1051 2
        $user->setBuyTotal($user->getBuyTotal() + $Order->getTotal());
1052
1053
    }
1054
1055
1056
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$payments" missing
Loading history...
introduced by
Doc comment for parameter "$subTotal" missing
Loading history...
1057
     * 支払方法選択の表示設定
1058
     *
1059
     * @param $payments 支払選択肢情報
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
1060
     * @param $subTotal 小計
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
1061
     * @return array
1062
     */
1063 1
    public function getPayments($payments, $subTotal)
1064
    {
1065 1
        $pays = array();
1066 1
        foreach ($payments as $payment) {
1067
            // 支払方法の制限値内であれば表示
1068 1
            if (!is_null($payment)) {
1069 1
                $pay = $this->paymentRepository->find($payment['id']);
1070 1
                if (intval($pay->getRuleMin()) <= $subTotal) {
1071 1
                    if (is_null($pay->getRuleMax()) || $pay->getRuleMax() >= $subTotal) {
1072 1
                        $pays[] = $pay;
1073
                    }
1074
                }
1075
            }
1076
        }
1077
1078 1
        return $pays;
1079
1080
    }
1081
1082
    /**
1083
     * お届け日を取得
1084
     *
1085
     * @param Order $Order
1086
     * @return array
1087
     */
1088 2
    public function getFormDeliveryDates(Order $Order)
1089
    {
1090
1091
        // お届け日の設定
1092 2
        $minDate = 0;
1093 2
        $deliveryDateFlag = false;
1094
1095
        // 配送時に最大となる商品日数を取得
1096 2
        foreach ($Order->getOrderDetails() as $detail) {
1097 2
            $deliveryDate = $detail->getProductClass()->getDeliveryDate();
1098 2
            if (!is_null($deliveryDate)) {
1099 2
                if ($deliveryDate->getValue() < 0) {
1100
                    // 配送日数がマイナスの場合はお取り寄せなのでスキップする
1101 1
                    $deliveryDateFlag = false;
1102 1
                    break;
1103
                }
1104
1105 1
                if ($minDate < $deliveryDate->getValue()) {
1106
                    $minDate = $deliveryDate->getValue();
1107
                }
1108
                // 配送日数が設定されている
1109 1
                $deliveryDateFlag = true;
1110
            }
1111
        }
1112
1113
        // 配達最大日数期間を設定
1114 2
        $deliveryDates = array();
1115
1116
        // 配送日数が設定されている
1117 2 View Code Duplication
        if ($deliveryDateFlag) {
1118 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...
1119 1
                new \DateTime($minDate.' day'),
1120 1
                new \DateInterval('P1D'),
1121 1
                new \DateTime($minDate + $this->appConfig['deliv_date_end_max'].' day')
1122
            );
1123
1124 1
            foreach ($period as $day) {
1125 1
                $deliveryDates[$day->format('Y/m/d')] = $day->format('Y/m/d');
1126
            }
1127
        }
1128
1129 2
        return $deliveryDates;
1130
1131
    }
1132
1133
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$deliveries" missing
Loading history...
1134
     * 支払方法を取得
1135
     *
1136
     * @param $deliveries
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
1137
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
1138
     * @return array
1139
     */
1140
    public function getFormPayments($deliveries, Order $Order)
1141
    {
1142
1143
        $productTypes = $this->orderService->getProductTypes($Order);
0 ignored issues
show
Deprecated Code introduced by
The property Eccube\Service\ShoppingService::$orderService has been deprecated.

This property 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 property will be removed from the class and what other property to use instead.

Loading history...
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...
1144
        if ($this->BaseInfo->getOptionMultipleShipping() == Constant::ENABLED && count($productTypes) > 1) {
1145
            // 複数配送時の支払方法
1146
1147
            $payments = $this->paymentRepository->findAllowedPayments($deliveries);
1148
        } else {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
1149
1150
            // 配送業者をセット
1151
            $shippings = $Order->getShippings();
1152
            $Shipping = $shippings[0];
1153
            $payments = $this->paymentRepository->findPayments($Shipping->getDelivery(), true);
1154
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
1155
        }
1156
        $payments = $this->getPayments($payments, $Order->getSubTotal());
1157
1158
        return $payments;
1159
1160
    }
1161
1162
    /**
1163
     * お届け先ごとにFormを作成
1164
     *
1165
     * @param Order $Order
1166
     * @return \Symfony\Component\Form\Form
1167
     * @deprecated since 3.0, to be removed in 3.1
1168
     */
1169 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...
1170
    {
1171
        $message = $Order->getMessage();
1172
1173
        $deliveries = $this->getDeliveriesOrder($Order);
1174
1175
        // 配送業者の支払方法を取得
1176
        $payments = $this->getFormPayments($deliveries, $Order);
1177
1178
        $builder = $this->formFactory->createBuilder('shopping', null, array(
1179
            'payments' => $payments,
1180
            'payment' => $Order->getPayment(),
1181
            'message' => $message,
1182
        ));
1183
1184
        $builder
1185
            ->add('shippings', CollectionType::class, array(
1186
                'entry_type' => ShippingItemType::class,
1187
                'data' => $Order->getShippings(),
1188
            ));
1189
1190
        $form = $builder->getForm();
1191
1192
        return $form;
1193
1194
    }
1195
1196
    /**
1197
     * お届け先ごとにFormBuilderを作成
1198
     *
1199
     * @param Order $Order
1200
     * @return \Symfony\Component\Form\FormBuilderInterface
1201
     *
1202
     * @deprecated 利用している箇所なし
1203
     */
1204 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...
1205
    {
1206
        $message = $Order->getMessage();
1207
1208
        $deliveries = $this->getDeliveriesOrder($Order);
1209
1210
        // 配送業者の支払方法を取得
1211
        $payments = $this->getFormPayments($deliveries, $Order);
1212
1213
        $builder = $this->formFactory->createBuilder('shopping', null, array(
1214
            'payments' => $payments,
1215
            'payment' => $Order->getPayment(),
1216
            'message' => $message,
1217
        ));
1218
1219
        $builder
1220
            ->add('shippings', CollectionType::class, array(
1221
                'entry_type' => ShippingItemType::class,
1222
                'data' => $Order->getShippings(),
1223
            ));
1224
1225
        return $builder;
1226
1227
    }
1228
1229
1230
    /**
1231
     * フォームデータを更新
1232
     *
1233
     * @param Order $Order
1234
     * @param array $data
1235
     *
1236
     * @deprecated
1237
     */
1238 1
    public function setFormData(Order $Order, array $data)
1239
    {
1240
1241
        // お問い合わせ
1242 1
        $Order->setMessage($data['message']);
1243
1244
        // お届け先情報を更新
1245 1
        $shippings = $data['shippings'];
1246 1
        foreach ($shippings as $Shipping) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
1247
1248 1
            $deliveryTime = $Shipping->getDeliveryTime();
1249 1
            if (!empty($deliveryTime)) {
1250
                $Shipping->setShippingDeliveryTime($deliveryTime->getDeliveryTime());
1251 1
                $Shipping->setTimeId($deliveryTime->getId());
1252
            }
1253
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
1254
        }
1255
1256
    }
1257
1258
    /**
1259
     * 配送料の合計金額を計算
1260
     *
1261
     * @param Order $Order
1262
     * @return Order
1263
     */
1264 2
    public function calculateDeliveryFee(Order $Order)
1265
    {
1266
1267
        // 配送業者を取得
1268 2
        $shippings = $Order->getShippings();
0 ignored issues
show
Unused Code introduced by
$shippings 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...
1269
1270
        // 配送料合計金額
1271
        // TODO CalculateDeliveryFeeStrategy でセットする
1272
        // $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($shippings));
1273
1274
        // 配送料無料条件(合計金額)
1275 2
        $this->setDeliveryFreeAmount($Order);
1276
1277
        // 配送料無料条件(合計数量)
1278 2
        $this->setDeliveryFreeQuantity($Order);
1279
1280 2
        return $Order;
1281
1282
    }
1283
1284
1285
    /**
1286
     * 購入処理を行う
1287
     *
1288
     * @param Order $Order
1289
     * @throws ShoppingException
1290
     */
1291 2
    public function processPurchase(Order $Order)
1292
    {
1293
1294 2
        $em = $this->entityManager;
1295
1296
        // TODO PurchaseFlowでやる
1297
//        // 合計金額の再計算
1298
//        $this->calculatePrice($Order);
1299
//
1300
//        // 商品公開ステータスチェック、商品制限数チェック、在庫チェック
1301
//        $check = $this->isOrderProduct($em, $Order);
1302
//        if (!$check) {
1303
//            throw new ShoppingException('front.shopping.stock.error');
1304
//        }
1305
1306
        // 受注情報、配送情報を更新
1307 2
        $Order = $this->calculateDeliveryFee($Order);
1308 2
        $this->setOrderUpdateData($Order);
1309
        // 在庫情報を更新
1310 2
        $this->setStockUpdate($em, $Order);
1311
1312 2
        if ($this->app->isGranted('ROLE_USER')) {
1313
            // 会員の場合、購入金額を更新
1314 1
            $this->setCustomerUpdate($Order, $this->app->user());
1315
        }
1316
1317
    }
1318
1319
1320
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$discount" missing
Loading history...
1321
     * 値引き可能かチェック
1322
     *
1323
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
1324
     * @param       $discount
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
1325
     * @return bool
1326
     */
1327
    public function isDiscount(Order $Order, $discount)
1328
    {
1329
1330
        if ($Order->getTotal() < $discount) {
1331
            return false;
1332
        }
1333
1334
        return true;
1335
    }
1336
1337
1338
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$discount" missing
Loading history...
1339
     * 値引き金額をセット
1340
     *
1341
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
1342
     * @param $discount
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
1343
     */
1344
    public function setDiscount(Order $Order, $discount)
1345
    {
1346
1347
        $Order->setDiscount($Order->getDiscount() + $discount);
1348
1349
    }
1350
1351
1352
    /**
1353
     * 合計金額を計算
1354
     *
1355
     * @param Order $Order
1356
     * @return Order
1357
     */
1358 1
    public function calculatePrice(Order $Order)
1359
    {
1360
1361 1
        $total = $Order->getTotalPrice();
1362
1363 1
        if ($total < 0) {
1364
            // 合計金額がマイナスの場合、0を設定し、discountは値引きされた額のみセット
1365
            $total = 0;
1366
        }
1367
1368 1
        $Order->setTotal($total);
1369 1
        $Order->setPaymentTotal($total);
1370
1371 1
        return $Order;
1372
1373
    }
1374
1375
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$status" missing
Loading history...
1376
     * 受注ステータスをセット
1377
     *
1378
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
1379
     * @param $status
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
1380
     * @return Order
1381
     */
1382 3
    public function setOrderStatus(Order $Order, $status)
1383
    {
1384
1385 3
        $Order->setOrderDate(new \DateTime());
1386 3
        $Order->setOrderStatus($this->orderStatusRepository->find($status));
1387
1388 3
        $event = new EventArgs(
1389
            array(
1390 3
                'Order' => $Order,
1391
            ),
1392 3
            null
1393
        );
1394 3
        $this->eventDispatcher->dispatch(EccubeEvents::SERVICE_SHOPPING_ORDER_STATUS, $event);
1395
1396 3
        return $Order;
1397
1398
    }
1399
1400
    /**
1401
     * 受注メール送信を行う
1402
     *
1403
     * @param Order $Order
1404
     * @return MailHistory
1405
     */
1406 2
    public function sendOrderMail(Order $Order)
1407
    {
1408
1409
        // メール送信
1410 2
        $message = $this->mailService->sendOrderMail($Order);
1411
1412
        // 送信履歴を保存.
1413 2
        $MailHistory = new MailHistory();
1414
        $MailHistory
1415 2
            ->setSubject($message->getSubject())
1416 2
            ->setMailBody($message->getBody())
1417 2
            ->setSendDate(new \DateTime())
1418 2
            ->setOrder($Order);
1419
1420 2
        $this->entityManager->persist($MailHistory);
1421 2
        $this->entityManager->flush($MailHistory);
1422
1423 2
        return $MailHistory;
1424
1425
    }
1426
1427
1428
    /**
1429
     * 受注処理完了通知
1430
     *
1431
     * @param Order $Order
1432
     */
1433
    public function notifyComplete(Order $Order)
1434
    {
1435
1436
        $event = new EventArgs(
1437
            array(
1438
                'Order' => $Order,
1439
            ),
1440
            null
1441
        );
1442
        $this->eventDispatcher->dispatch(EccubeEvents::SERVICE_SHOPPING_NOTIFY_COMPLETE, $event);
1443
1444
    }
1445
1446
1447
}
1448