Completed
Push — support-coverage ( 9b57a3...b5dae6 )
by Kentaro
41:38
created

ShoppingService::newOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 4
cp 0
crap 2
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\ORM\EntityManager;
27
use Doctrine\ORM\EntityManagerInterface;
28
use Eccube\Annotation\Service;
29
use Eccube\Common\EccubeConfig;
30
use Eccube\Entity\BaseInfo;
31
use Eccube\Entity\Customer;
32
use Eccube\Entity\Delivery;
33
use Eccube\Entity\MailHistory;
34
use Eccube\Entity\Master\DeviceType;
35
use Eccube\Entity\Master\OrderStatus;
36
use Eccube\Entity\Order;
37
use Eccube\Entity\OrderItem;
38
use Eccube\Entity\Product;
39
use Eccube\Entity\ProductClass;
40
use Eccube\Entity\Shipping;
41
use Eccube\Event\EccubeEvents;
42
use Eccube\Event\EventArgs;
43
use Eccube\Exception\CartException;
44
use Eccube\Form\Type\ShippingItemType;
45
use Eccube\Repository\CustomerAddressRepository;
46
use Eccube\Repository\DeliveryFeeRepository;
47
use Eccube\Repository\DeliveryRepository;
48
use Eccube\Repository\DeliveryTimeRepository;
49
use Eccube\Repository\MailTemplateRepository;
50
use Eccube\Repository\Master\DeviceTypeRepository;
51
use Eccube\Repository\Master\OrderStatusRepository;
52
use Eccube\Repository\Master\PrefRepository;
53
use Eccube\Repository\OrderRepository;
54
use Eccube\Repository\PaymentRepository;
55
use Eccube\Repository\TaxRuleRepository;
56
use Eccube\Util\StringUtil;
57
use Symfony\Component\EventDispatcher\EventDispatcher;
58
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
59
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
60
use Symfony\Component\Form\FormFactory;
61
use Symfony\Component\Form\FormFactoryInterface;
62
use Symfony\Component\HttpFoundation\Session\Session;
63
use Symfony\Component\HttpFoundation\Session\SessionInterface;
64
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
65
66
/**
67
 * @Service
68
 */
69
class ShoppingService
70
{
71
    /**
72
     * @var MailTemplateRepository
73
     */
74
    protected $mailTemplateRepository;
75
76
    /**
77
     * @var MailService
78
     */
79
    protected $mailService;
80
81
    /**
82
     * @var EventDispatcher
83
     */
84
    protected $eventDispatcher;
85
86
    /**
87
     * @var FormFactory
88
     */
89
    protected $formFactory;
90
91
    /**
92
     * @var DeliveryFeeRepository
93
     */
94
    protected $deliveryFeeRepository;
95
96
    /**
97
     * @var TaxRuleRepository
98
     */
99
    protected $taxRuleRepository;
100
101
    /**
102
     * @var CustomerAddressRepository
103
     */
104
    protected $customerAddressRepository;
105
106
    /**
107
     * @var DeliveryRepository
108
     */
109
    protected $deliveryRepository;
110
111
    /**
112
     * @var DeliveryTimeRepository
113
     */
114
    protected $deliveryTimeRepository;
115
116
    /**
117
     * @var OrderStatusRepository
118
     */
119
    protected $orderStatusRepository;
120
121
    /**
122
     * @var PaymentRepository
123
     */
124
    protected $paymentRepository;
125
126
    /**
127
     * @var DeviceTypeRepository
128
     */
129
    protected $deviceTypeRepository;
130
131
    /**
132
     * @var EntityManager
133
     */
134
    protected $entityManager;
135
136
    /**
137
     * @var EccubeConfig
138
     */
139
    protected $eccubeConfig;
140
141
    /**
142
     * @var PrefRepository
143
     */
144
    protected $prefRepository;
145
146
    /**
147
     * @var Session
148
     */
149
    protected $session;
150
151
    /**
152
     * @var OrderRepository
153
     */
154
    protected $orderRepository;
155
156
    /**
157
     * @var BaseInfo
158
     */
159
    protected $BaseInfo;
160
161
    /**
162
     * @var \Eccube\Service\CartService
163
     */
164
    protected $cartService;
165
166
    /**
167
     * @var \Eccube\Service\OrderService
168
     *
169
     * @deprecated
170
     */
171
    protected $orderService;
172
173
    /**
174
     * @var AuthorizationCheckerInterface
175
     */
176
    protected $authorizationChecker;
177
178
    /**
179
     * ShoppingService constructor.
180
     *
181
     * @param MailTemplateRepository $mailTemplateRepository
0 ignored issues
show
introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
182
     * @param MailService $mailService
0 ignored issues
show
introduced by
Expected 19 spaces after parameter type; 1 found
Loading history...
183
     * @param EventDispatcher $eventDispatcher
0 ignored issues
show
introduced by
Expected 15 spaces after parameter type; 1 found
Loading history...
184
     * @param FormFactory $formFactory
0 ignored issues
show
introduced by
Expected 19 spaces after parameter type; 1 found
Loading history...
185
     * @param DeliveryFeeRepository $deliveryFeeRepository
0 ignored issues
show
introduced by
Expected 9 spaces after parameter type; 1 found
Loading history...
186
     * @param TaxRuleRepository $taxRuleRepository
0 ignored issues
show
introduced by
Expected 13 spaces after parameter type; 1 found
Loading history...
187
     * @param CustomerAddressRepository $customerAddressRepository
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
188
     * @param DeliveryRepository $deliveryRepository
0 ignored issues
show
introduced by
Expected 12 spaces after parameter type; 1 found
Loading history...
189
     * @param DeliveryTimeRepository $deliveryTimeRepository
0 ignored issues
show
introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
190
     * @param OrderStatusRepository $orderStatusRepository
0 ignored issues
show
introduced by
Expected 9 spaces after parameter type; 1 found
Loading history...
191
     * @param PaymentRepository $paymentRepository
0 ignored issues
show
introduced by
Expected 13 spaces after parameter type; 1 found
Loading history...
192
     * @param DeviceTypeRepository $deviceTypeRepository
0 ignored issues
show
introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
193
     * @param EntityManager $entityManager
0 ignored issues
show
introduced by
Expected 17 spaces after parameter type; 1 found
Loading history...
194
     * @param EccubeConfig $eccubeConfig
0 ignored issues
show
introduced by
Expected 18 spaces after parameter type; 1 found
Loading history...
195
     * @param PrefRepository $prefRepository
0 ignored issues
show
introduced by
Expected 16 spaces after parameter type; 1 found
Loading history...
196
     * @param Session $session
0 ignored issues
show
introduced by
Expected 23 spaces after parameter type; 1 found
Loading history...
197
     * @param OrderRepository $orderRepository
0 ignored issues
show
introduced by
Expected 15 spaces after parameter type; 1 found
Loading history...
198
     * @param CartService $cartService
0 ignored issues
show
introduced by
Expected 19 spaces after parameter type; 1 found
Loading history...
199
     * @param OrderService $orderService
0 ignored issues
show
introduced by
Expected 18 spaces after parameter type; 1 found
Loading history...
200
     * @param BaseInfo $BaseInfo
0 ignored issues
show
introduced by
Expected 22 spaces after parameter type; 1 found
Loading history...
201
     * @param AuthorizationCheckerInterface $authorizationChecker
202
     */
203 36
    public function __construct(
204
        MailTemplateRepository $mailTemplateRepository,
205
        MailService $mailService,
206
        EventDispatcherInterface $eventDispatcher,
207
        FormFactoryInterface $formFactory,
208
        DeliveryFeeRepository $deliveryFeeRepository,
209
        TaxRuleRepository $taxRuleRepository,
210
        CustomerAddressRepository $customerAddressRepository,
211
        DeliveryRepository $deliveryRepository,
212
        DeliveryTimeRepository $deliveryTimeRepository,
213
        OrderStatusRepository $orderStatusRepository,
214
        PaymentRepository $paymentRepository,
215
        DeviceTypeRepository $deviceTypeRepository,
216
        EntityManagerInterface $entityManager,
217
        EccubeConfig $eccubeConfig,
218
        PrefRepository $prefRepository,
219
        SessionInterface $session,
220
        OrderRepository $orderRepository,
221
        CartService $cartService,
222
        OrderService $orderService,
223
        BaseInfo $BaseInfo,
224
        AuthorizationCheckerInterface $authorizationChecker
225
    ) {
226 36
        $this->mailTemplateRepository = $mailTemplateRepository;
227 36
        $this->mailService = $mailService;
228 36
        $this->eventDispatcher = $eventDispatcher;
0 ignored issues
show
Documentation Bug introduced by
$eventDispatcher is of type object<Symfony\Component...entDispatcherInterface>, but the property $eventDispatcher was declared to be of type object<Symfony\Component...atcher\EventDispatcher>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
229 36
        $this->formFactory = $formFactory;
0 ignored issues
show
Documentation Bug introduced by
$formFactory is of type object<Symfony\Component...m\FormFactoryInterface>, but the property $formFactory was declared to be of type object<Symfony\Component\Form\FormFactory>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
230 36
        $this->deliveryFeeRepository = $deliveryFeeRepository;
231 36
        $this->taxRuleRepository = $taxRuleRepository;
232 36
        $this->customerAddressRepository = $customerAddressRepository;
233 36
        $this->deliveryRepository = $deliveryRepository;
234 36
        $this->deliveryTimeRepository = $deliveryTimeRepository;
235 36
        $this->orderStatusRepository = $orderStatusRepository;
236 36
        $this->paymentRepository = $paymentRepository;
237 36
        $this->deviceTypeRepository = $deviceTypeRepository;
238 36
        $this->entityManager = $entityManager;
0 ignored issues
show
Documentation Bug introduced by
$entityManager is of type object<Doctrine\ORM\EntityManagerInterface>, but the property $entityManager was declared to be of type object<Doctrine\ORM\EntityManager>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
239 36
        $this->eccubeConfig = $eccubeConfig;
240 36
        $this->prefRepository = $prefRepository;
241 36
        $this->session = $session;
0 ignored issues
show
Documentation Bug introduced by
$session is of type object<Symfony\Component...ssion\SessionInterface>, but the property $session was declared to be of type object<Symfony\Component...dation\Session\Session>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
242 36
        $this->orderRepository = $orderRepository;
243 36
        $this->cartService = $cartService;
244 36
        $this->orderService = $orderService;
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...
245 36
        $this->BaseInfo = $BaseInfo;
246 36
        $this->authorizationChecker = $authorizationChecker;
247
    }
248
249
    /**
250
     * セッションにセットされた受注情報を取得
251
     *
252
     * @param null $status
253
     *
254
     * @return null|object
255
     */
256 26
    public function getOrder($status = null)
257
    {
258
        // 受注データを取得
259 26
        $preOrderId = $this->cartService->getPreOrderId();
260 26
        if (!$preOrderId) {
261 26
            return null;
262
        }
263
264
        $condition = [
265 13
            'pre_order_id' => $preOrderId,
266
        ];
267
268 13
        if (!is_null($status)) {
269
            $condition += [
270 13
                'OrderStatus' => $status,
271
            ];
272
        }
273
274 13
        $Order = $this->orderRepository->findOneBy($condition);
275
276 13
        return $Order;
277
    }
278
279
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$sesisonKey" missing
Loading history...
280
     * 非会員情報を取得
281
     *
282
     * @param $sesisonKey
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
283
     *
284
     * @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...
285
     */
286
    public function getNonMember($sesisonKey)
287
    {
288
        // 非会員でも一度会員登録されていればショッピング画面へ遷移
289
        $nonMember = $this->session->get($sesisonKey);
290
        if (is_null($nonMember)) {
291
            return null;
292
        }
293
        if (!array_key_exists('customer', $nonMember) || !array_key_exists('pref', $nonMember)) {
294
            return null;
295
        }
296
297
        $Customer = $nonMember['customer'];
298
        $Customer->setPref($this->prefRepository->find($nonMember['pref']));
299
300
        foreach ($Customer->getCustomerAddresses() as $CustomerAddress) {
301
            $Pref = $CustomerAddress->getPref();
302
            if ($Pref) {
303
                $CustomerAddress->setPref($this->prefRepository->find($Pref->getId()));
304
            }
305
        }
306
307
        return $Customer;
308
    }
309
310
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
311
     * 受注情報を作成
312
     *
313
     * @param $Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
314
     *
315
     * @return \Eccube\Entity\Order
316
     */
317
    public function createOrder($Customer)
318
    {
319
        // ランダムなpre_order_idを作成
320 View Code Duplication
        do {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
321
            $preOrderId = sha1(StringUtil::random(32));
322
            $Order = $this->orderRepository->findOneBy([
323
                'pre_order_id' => $preOrderId,
324
                'OrderStatus' => OrderStatus::PROCESSING,
325
            ]);
326
        } while ($Order);
327
328
        // 受注情報、受注明細情報、お届け先情報、配送商品情報を作成
329
        $Order = $this->registerPreOrder(
330
            $Customer,
331
            $preOrderId);
332
333
        $this->cartService->setPreOrderId($preOrderId);
334
        $this->cartService->save();
335
336
        return $Order;
337
    }
338
339
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
introduced by
Doc comment for parameter "$preOrderId" missing
Loading history...
340
     * 仮受注情報作成
341
     *
342
     * @param $Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
343
     * @param $preOrderId
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
344
     *
345
     * @return mixed
346
     *
347
     * @throws \Doctrine\ORM\NoResultException
348
     * @throws \Doctrine\ORM\NonUniqueResultException
349
     */
350
    public function registerPreOrder(Customer $Customer, $preOrderId)
351
    {
352
        $this->em = $this->entityManager;
0 ignored issues
show
Bug introduced by
The property em does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
353
354
        // 受注情報を作成
355
        $Order = $this->getNewOrder($Customer);
356
        $Order->setPreOrderId($preOrderId);
357
358
        $mobileDetect = new \Mobile_Detect();
359
        $DeviceType = $this->deviceTypeRepository->find($mobileDetect->isMobile() ? DeviceType::DEVICE_TYPE_SP : DeviceType::DEVICE_TYPE_PC);
360
        $Order->setDeviceType($DeviceType);
361
362
        $this->entityManager->persist($Order);
363
364
        // 配送業者情報を取得
365
        $deliveries = $this->getDeliveriesCart();
366
367
        // お届け先情報を作成
368
        $Order = $this->getNewShipping($Order, $Customer, $deliveries);
369
370
        // 受注明細情報、配送商品情報を作成
371
        $Order = $this->getNewDetails($Order);
372
373
        // 小計
374
        $subTotal = $this->orderService->getSubTotal($Order);
0 ignored issues
show
Bug introduced by
The method getSubTotal() does not seem to exist on object<Eccube\Service\OrderService>.

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...
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...
375
376
        // 消費税のみの小計
377
        $tax = $this->orderService->getTotalTax($Order);
0 ignored issues
show
Bug introduced by
The method getTotalTax() does not seem to exist on object<Eccube\Service\OrderService>.

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...
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...
378
379
        // 配送料合計金額
380
        // TODO CalculateDeliveryFeeStrategy でセットする
381
        // $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($Order->getShippings()));
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
382
383
        // 小計
384
        $Order->setSubTotal($subTotal);
385
386
        // 配送料無料条件(合計金額)
387
        $this->setDeliveryFreeAmount($Order);
388
389
        // 配送料無料条件(合計数量)
390
        $this->setDeliveryFreeQuantity($Order);
391
392
        // 初期選択の支払い方法をセット
393
        $payments = $this->paymentRepository->findAllowedPayments($deliveries);
394
        $payments = $this->getPayments($payments, $subTotal);
395
396
        if (count($payments) > 0) {
397
            $payment = $payments[0];
398
            $Order->setPayment($payment);
399
            $Order->setPaymentMethod($payment->getMethod());
400
            $Order->setCharge($payment->getCharge());
401
        } else {
402
            $Order->setCharge(0);
403
        }
404
405
        $Order->setTax($tax);
406
407
        // 合計金額の計算
408
        $this->calculatePrice($Order);
409
410
        $this->entityManager->flush();
411
412
        return $Order;
413
    }
414
415
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
416
     * 受注情報を作成
417
     *
418
     * @param $Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
419
     *
420
     * @return \Eccube\Entity\Order
421
     */
422
    public function getNewOrder(Customer $Customer)
423
    {
424
        $Order = $this->newOrder();
425
        $this->copyToOrderFromCustomer($Order, $Customer);
426
427
        return $Order;
428
    }
429
430
    /**
431
     * 受注情報を作成
432
     *
433
     * @return \Eccube\Entity\Order
434
     */
435
    public function newOrder()
436
    {
437
        $OrderStatus = $this->orderStatusRepository->find(OrderStatus::PROCESSING);
438
        $Order = new \Eccube\Entity\Order($OrderStatus);
439
440
        return $Order;
441
    }
442
443
    /**
444
     * 受注情報を作成
445
     *
446
     * @param \Eccube\Entity\Order $Order
0 ignored issues
show
introduced by
Expected 9 spaces after parameter type; 1 found
Loading history...
447
     * @param \Eccube\Entity\Customer|null $Customer
448
     *
449
     * @return \Eccube\Entity\Order
450
     */
451
    public function copyToOrderFromCustomer(Order $Order, Customer $Customer = null)
452
    {
453
        if (is_null($Customer)) {
454
            return $Order;
455
        }
456
457
        if ($Customer->getId()) {
458
            $Order->setCustomer($Customer);
459
        }
460
        $Order
461
            ->setName01($Customer->getName01())
462
            ->setName02($Customer->getName02())
463
            ->setKana01($Customer->getKana01())
464
            ->setKana02($Customer->getKana02())
465
            ->setCompanyName($Customer->getCompanyName())
466
            ->setEmail($Customer->getEmail())
467
            ->setTel01($Customer->getTel01())
468
            ->setTel02($Customer->getTel02())
469
            ->setTel03($Customer->getTel03())
470
            ->setFax01($Customer->getFax01())
471
            ->setFax02($Customer->getFax02())
472
            ->setFax03($Customer->getFax03())
473
            ->setZip01($Customer->getZip01())
474
            ->setZip02($Customer->getZip02())
475
            ->setZipCode($Customer->getZip01().$Customer->getZip02())
476
            ->setPref($Customer->getPref())
477
            ->setAddr01($Customer->getAddr01())
478
            ->setAddr02($Customer->getAddr02())
479
            ->setSex($Customer->getSex())
480
            ->setBirth($Customer->getBirth())
481
            ->setJob($Customer->getJob());
482
483
        return $Order;
484
    }
485
486
    /**
487
     * 配送業者情報を取得
488
     *
489
     * @return array
490
     */
491
    public function getDeliveriesCart()
492
    {
493
        // カートに保持されている販売種別を取得
494
        $saleTypes = $this->cartService->getSaleTypes();
0 ignored issues
show
Bug introduced by
The method getSaleTypes() 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...
495
496
        return $this->getDeliveries($saleTypes);
497
    }
498
499
    /**
500
     * 配送業者情報を取得
501
     *
502
     * @param Order $Order
503
     *
504
     * @return array
505
     */
506
    public function getDeliveriesOrder(Order $Order)
507
    {
508
        // 受注情報から販売種別を取得
509
        $saleTypes = $this->orderService->getSaleTypes($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::getSaleTypes() 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...
510
511
        return $this->getDeliveries($saleTypes);
512
    }
513
514
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$saleTypes" missing
Loading history...
515
     * 配送業者情報を取得
516
     *
517
     * @param $saleTypes
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
518
     *
519
     * @return array
520
     */
521
    public function getDeliveries($saleTypes)
522
    {
523
        // 販売種別に紐づく配送業者を取得
524
        $deliveries = $this->deliveryRepository->getDeliveries($saleTypes);
525
526
        if ($this->BaseInfo->isOptionMultipleShipping()) {
527
            // 複数配送対応
528
529
            // 支払方法を取得
530
            $payments = $this->paymentRepository->findAllowedPayments($deliveries);
531
532
            if (count($saleTypes) > 1) {
533
                // 販売種別が複数ある場合、配送対象となる配送業者を取得
534
                $deliveries = $this->deliveryRepository->findAllowedDeliveries($saleTypes, $payments);
535
            }
536
        }
537
538
        return $deliveries;
539
    }
540
541
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$deliveries" missing
Loading history...
542
     * お届け先情報を作成
543
     *
544
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
545
     * @param Customer $Customer
0 ignored issues
show
introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
546
     * @param $deliveries
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
547
     *
548
     * @return Order
549
     */
550
    public function getNewShipping(Order $Order, Customer $Customer, $deliveries)
551
    {
552
        $saleTypes = [];
553
        foreach ($deliveries as $Delivery) {
554
            if (!in_array($Delivery->getSaleType()->getId(), $saleTypes)) {
555
                $Shipping = new Shipping();
556
557
                $this->copyToShippingFromCustomer($Shipping, $Customer)
558
                    ->setOrder($Order);
559
560
                // 配送料金の設定
561
                $this->setShippingDeliveryFee($Shipping, $Delivery);
562
563
                $this->entityManager->persist($Shipping);
564
565
                $Order->addShipping($Shipping);
566
567
                $saleTypes[] = $Delivery->getProductType()->getId();
568
            }
569
        }
570
571
        return $Order;
572
    }
573
574
    /**
575
     * お届け先情報を作成
576
     *
577
     * @param \Eccube\Entity\Shipping $Shipping
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
578
     * @param \Eccube\Entity\Customer|null $Customer
579
     *
580
     * @return \Eccube\Entity\Shipping
581
     */
582
    public function copyToShippingFromCustomer(Shipping $Shipping, Customer $Customer = null)
583
    {
584
        if (is_null($Customer)) {
585
            return $Shipping;
586
        }
587
588
        $CustomerAddress = $this->customerAddressRepository->findOneBy(
589
            ['Customer' => $Customer],
590
            ['id' => 'ASC']
591
        );
592
593
        if (!is_null($CustomerAddress)) {
594
            $Shipping
595
                ->setName01($CustomerAddress->getName01())
596
                ->setName02($CustomerAddress->getName02())
597
                ->setKana01($CustomerAddress->getKana01())
598
                ->setKana02($CustomerAddress->getKana02())
599
                ->setCompanyName($CustomerAddress->getCompanyName())
600
                ->setTel01($CustomerAddress->getTel01())
601
                ->setTel02($CustomerAddress->getTel02())
602
                ->setTel03($CustomerAddress->getTel03())
603
                ->setFax01($CustomerAddress->getFax01())
604
                ->setFax02($CustomerAddress->getFax02())
605
                ->setFax03($CustomerAddress->getFax03())
606
                ->setZip01($CustomerAddress->getZip01())
607
                ->setZip02($CustomerAddress->getZip02())
608
                ->setZipCode($CustomerAddress->getZip01().$CustomerAddress->getZip02())
609
                ->setPref($CustomerAddress->getPref())
610
                ->setAddr01($CustomerAddress->getAddr01())
611
                ->setAddr02($CustomerAddress->getAddr02());
612
        } else {
613
            $Shipping
614
                ->setName01($Customer->getName01())
615
                ->setName02($Customer->getName02())
616
                ->setKana01($Customer->getKana01())
617
                ->setKana02($Customer->getKana02())
618
                ->setCompanyName($Customer->getCompanyName())
619
                ->setTel01($Customer->getTel01())
620
                ->setTel02($Customer->getTel02())
621
                ->setTel03($Customer->getTel03())
622
                ->setFax01($Customer->getFax01())
623
                ->setFax02($Customer->getFax02())
624
                ->setFax03($Customer->getFax03())
625
                ->setZip01($Customer->getZip01())
626
                ->setZip02($Customer->getZip02())
627
                ->setZipCode($Customer->getZip01().$Customer->getZip02())
628
                ->setPref($Customer->getPref())
629
                ->setAddr01($Customer->getAddr01())
630
                ->setAddr02($Customer->getAddr02());
631
        }
632
633
        return $Shipping;
634
    }
635
636
    /**
637
     * 受注明細情報、配送商品情報を作成
638
     *
639
     * @param Order $Order
640
     *
641
     * @return Order
642
     */
643
    public function getNewDetails(Order $Order)
644
    {
645
        // 受注詳細, 配送商品
646
        foreach ($this->cartService->getCart()->getCartItems() as $item) {
647
            /* @var $ProductClass \Eccube\Entity\ProductClass */
648
            $ProductClass = $item->getProductClass();
649
            /* @var $Product \Eccube\Entity\Product */
650
            $Product = $ProductClass->getProduct();
651
652
            $quantity = $item->getQuantity();
653
654
            // 配送商品情報を作成
655
            $this->getNewOrderItem($Order, $Product, $ProductClass, $quantity);
656
        }
657
658
        return $Order;
659
    }
660
661
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$quantity" missing
Loading history...
662
     * 配送商品情報を作成
663
     *
664
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
665
     * @param Product $Product
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
666
     * @param ProductClass $ProductClass
667
     * @param $quantity
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
668
     *
669
     * @return \Eccube\Entity\OrderItem
670
     */
671
    public function getNewOrderItem(Order $Order, Product $Product, ProductClass $ProductClass, $quantity)
672
    {
673
        $OrderItem = new OrderItem();
674
        $shippings = $Order->getShippings();
675
676
        // 選択された商品がどのお届け先情報と関連するかチェック
677
        $Shipping = null;
678
        foreach ($shippings as $s) {
679
            if ($s->getDelivery()->getSaleType()->getId() == $ProductClass->getSaleType()->getId()) {
680
                // 販売種別が同一のお届け先情報と関連させる
681
                $Shipping = $s;
682
                break;
683
            }
684
        }
685
686
        if (is_null($Shipping)) {
687
            // お届け先情報と関連していない場合、エラー
688
            throw new CartException('shopping.delivery.not.saletype');
689
        }
690
691
        // 商品ごとの配送料合計
692
        $productDeliveryFeeTotal = 0;
693
        if ($this->BaseInfo->isOptionProductDeliveryFee()) {
694
            $productDeliveryFeeTotal = $ProductClass->getDeliveryFee() * $quantity;
695
        }
696
697
        $Shipping->setShippingDeliveryFee($Shipping->getShippingDeliveryFee() + $productDeliveryFeeTotal);
698
699
        $OrderItem->setShipping($Shipping)
700
            ->setOrder($Order)
701
            ->setProductClass($ProductClass)
702
            ->setProduct($Product)
703
            ->setProductName($Product->getName())
704
            ->setProductCode($ProductClass->getCode())
705
            ->setPrice($ProductClass->getPrice02())
706
            ->setQuantity($quantity);
707
708
        $ClassCategory1 = $ProductClass->getClassCategory1();
709
        if (!is_null($ClassCategory1)) {
710
            $OrderItem->setClasscategoryName1($ClassCategory1->getName());
711
            $OrderItem->setClassName1($ClassCategory1->getClassName()->getName());
712
        }
713
        $ClassCategory2 = $ProductClass->getClassCategory2();
714
        if (!is_null($ClassCategory2)) {
715
            $OrderItem->setClasscategoryName2($ClassCategory2->getName());
716
            $OrderItem->setClassName2($ClassCategory2->getClassName()->getName());
717
        }
718
        $Shipping->addOrderItem($OrderItem);
719
        $this->entityManager->persist($OrderItem);
720
721
        return $OrderItem;
722
    }
723
724
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$shippings" missing
Loading history...
725
     * お届け先ごとの送料合計を取得
726
     *
727
     * @param $shippings
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
728
     *
729
     * @return int
730
     */
731
    public function getShippingDeliveryFeeTotal($shippings)
732
    {
733
        $deliveryFeeTotal = 0;
734
        foreach ($shippings as $Shipping) {
735
            $deliveryFeeTotal += $Shipping->getShippingDeliveryFee();
736
        }
737
738
        return $deliveryFeeTotal;
739
    }
740
741
    /**
742
     * 商品ごとの配送料を取得
743
     *
744
     * @param Shipping $Shipping
745
     *
746
     * @return int
747
     */
748
    public function getProductDeliveryFee(Shipping $Shipping)
749
    {
750
        $productDeliveryFeeTotal = 0;
751
        $OrderItems = $Shipping->getOrderItems();
752
        foreach ($OrderItems as $OrderItem) {
753
            $productDeliveryFeeTotal += $OrderItem->getProductClass()->getDeliveryFee() * $OrderItem->getQuantity();
754
        }
755
756
        return $productDeliveryFeeTotal;
757
    }
758
759
    /**
760
     * 住所などの情報が変更された時に金額の再計算を行う
761
     *
762
     * @deprecated PurchaseFlowで行う
763
     *
764
     * @param Order $Order
765
     *
766
     * @return Order
767
     */
768
    public function getAmount(Order $Order)
769
    {
770
        // 初期選択の配送業者をセット
771
        $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...
772
773
        // 配送料合計金額
774
        // TODO CalculateDeliveryFeeStrategy でセットする
775
        // $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($shippings));
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
776
777
        // 配送料無料条件(合計金額)
778
        $this->setDeliveryFreeAmount($Order);
779
780
        // 配送料無料条件(合計数量)
781
        $this->setDeliveryFreeQuantity($Order);
782
783
        // 合計金額の計算
784
        $this->calculatePrice($Order);
785
786
        return $Order;
787
    }
788
789
    /**
790
     * 配送料金の設定
791
     *
792
     * @param Shipping $Shipping
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
793
     * @param Delivery|null $Delivery
794
     */
795
    public function setShippingDeliveryFee(Shipping $Shipping, Delivery $Delivery = null)
796
    {
797
        // 配送料金の設定
798
        if (is_null($Delivery)) {
799
            $Delivery = $Shipping->getDelivery();
800
        }
801
        $deliveryFee = $this->deliveryFeeRepository->findOneBy(['Delivery' => $Delivery, 'Pref' => $Shipping->getPref()]);
802
        if ($deliveryFee) {
803
            $Shipping->setFeeId($deliveryFee->getId());
804
        }
805
        $Shipping->setDelivery($Delivery);
806
807
        // 商品ごとの配送料合計
808
        $productDeliveryFeeTotal = 0;
809
        if ($this->BaseInfo->isOptionProductDeliveryFee()) {
810
            $productDeliveryFeeTotal += $this->getProductDeliveryFee($Shipping);
811
        }
812
813
        $Shipping->setShippingDeliveryFee($deliveryFee->getFee() + $productDeliveryFeeTotal);
814
        $Shipping->setShippingDeliveryName($Delivery->getName());
815
    }
816
817
    /**
818
     * 配送料無料条件(合計金額)の条件を満たしていれば配送料金を0に設定
819
     *
820
     * @param Order $Order
821
     */
822 2 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...
823
    {
824
        // 配送料無料条件(合計金額)
825 2
        $deliveryFreeAmount = $this->BaseInfo->getDeliveryFreeAmount();
826 2
        if (!is_null($deliveryFreeAmount)) {
827
            // 合計金額が設定金額以上であれば送料無料
828
            if ($Order->getSubTotal() >= $deliveryFreeAmount) {
829
                $Order->setDeliveryFeeTotal(0);
830
                // お届け先情報の配送料も0にセット
831
                $shippings = $Order->getShippings();
832
                foreach ($shippings as $Shipping) {
833
                    $Shipping->setShippingDeliveryFee(0);
834
                }
835
            }
836
        }
837
    }
838
839
    /**
840
     * 配送料無料条件(合計数量)の条件を満たしていれば配送料金を0に設定
841
     *
842
     * @param Order $Order
843
     */
844 2 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...
845
    {
846
        // 配送料無料条件(合計数量)
847 2
        $deliveryFreeQuantity = $this->BaseInfo->getDeliveryFreeQuantity();
848 2
        if (!is_null($deliveryFreeQuantity)) {
849
            // 合計数量が設定数量以上であれば送料無料
850
            if ($this->orderService->getTotalQuantity($Order) >= $deliveryFreeQuantity) {
0 ignored issues
show
Bug introduced by
The method getTotalQuantity() does not seem to exist on object<Eccube\Service\OrderService>.

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...
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...
851
                $Order->setDeliveryFeeTotal(0);
852
                // お届け先情報の配送料も0にセット
853
                $shippings = $Order->getShippings();
854
                foreach ($shippings as $Shipping) {
855
                    $Shipping->setShippingDeliveryFee(0);
856
                }
857
            }
858
        }
859
    }
860
861
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$data" missing
Loading history...
862
     * 受注情報、お届け先情報の更新
863
     *
864
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 22 spaces after parameter type; 1 found
Loading history...
865
     * @param $data フォームデータ
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
866
     *
867
     * @deprecated since 3.0.5, to be removed in 3.1
868
     */
869
    public function setOrderUpdate(Order $Order, $data)
870
    {
871
        // 受注情報を更新
872
        $Order->setOrderDate(new \DateTime());
873
        $Order->setOrderStatus($this->orderStatusRepository->find(OrderStatus::NEW));
874
        $Order->setMessage($data['message']);
875
        // お届け先情報を更新
876
        $shippings = $data['shippings'];
877
        foreach ($shippings as $Shipping) {
878
            $Delivery = $Shipping->getDelivery();
879
            $deliveryFee = $this->deliveryFeeRepository->findOneBy([
880
                'Delivery' => $Delivery,
881
                'Pref' => $Shipping->getPref(),
882
            ]);
883
            $deliveryTime = $Shipping->getDeliveryTime();
884
            if (!empty($deliveryTime)) {
885
                $Shipping->setShippingDeliveryTime($deliveryTime->getDeliveryTime());
886
                $Shipping->setTimeId($deliveryTime->getId());
887
            }
888
            $Shipping->setDeliveryFee($deliveryFee);
889
            // 商品ごとの配送料合計
890
            $productDeliveryFeeTotal = 0;
891
            if ($this->BaseInfo->isOptionProductDeliveryFee()) {
892
                $productDeliveryFeeTotal += $this->getProductDeliveryFee($Shipping);
893
            }
894
            $Shipping->setShippingDeliveryFee($deliveryFee->getFee() + $productDeliveryFeeTotal);
895
            $Shipping->setShippingDeliveryName($Delivery->getName());
896
        }
897
        // 配送料無料条件(合計金額)
898
        $this->setDeliveryFreeAmount($Order);
899
        // 配送料無料条件(合計数量)
900
        $this->setDeliveryFreeQuantity($Order);
901
    }
902
903
    /**
904
     * 受注情報の更新
905
     *
906
     * @param Order $Order 受注情報
907
     */
908 2
    public function setOrderUpdateData(Order $Order)
909
    {
910
        // 受注情報を更新
911 2
        $Order->setOrderDate(new \DateTime()); // XXX 後続の setOrderStatus でも時刻を更新している
912 2
        $OrderStatus = $this->orderStatusRepository->find(OrderStatus::NEW);
913 2
        $this->setOrderStatus($Order, $OrderStatus);
914
    }
915
916
    /**
917
     * 会員情報の更新
918
     *
919
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
920
     * @param Customer $user ログインユーザ
0 ignored issues
show
introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
921
     */
922 1
    public function setCustomerUpdate(Order $Order, Customer $user)
923
    {
924
        // 顧客情報を更新
925 1
        $now = new \DateTime();
926 1
        $firstBuyDate = $user->getFirstBuyDate();
927 1
        if (empty($firstBuyDate)) {
928 1
            $user->setFirstBuyDate($now);
929
        }
930 1
        $user->setLastBuyDate($now);
931
932 1
        $user->setBuyTimes($user->getBuyTimes() + 1);
933 1
        $user->setBuyTotal($user->getBuyTotal() + $Order->getTotal());
934
    }
935
936
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$payments" missing
Loading history...
introduced by
Doc comment for parameter "$subTotal" missing
Loading history...
937
     * 支払方法選択の表示設定
938
     *
939
     * @param $payments 支払選択肢情報
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
940
     * @param $subTotal 小計
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
941
     *
942
     * @return array
943
     */
944
    public function getPayments($payments, $subTotal)
945
    {
946
        $pays = [];
947
        foreach ($payments as $payment) {
948
            // 支払方法の制限値内であれば表示
949
            if (!is_null($payment)) {
950
                $pay = $this->paymentRepository->find($payment['id']);
951
                if (intval($pay->getRuleMin()) <= $subTotal) {
952
                    if (is_null($pay->getRuleMax()) || $pay->getRuleMax() >= $subTotal) {
953
                        $pays[] = $pay;
954
                    }
955
                }
956
            }
957
        }
958
959
        return $pays;
960
    }
961
962
    /**
963
     * お届け日を取得
964
     *
965
     * @param Order $Order
966
     *
967
     * @return array
968
     */
969
    public function getFormDeliveryDurations(Order $Order)
970
    {
971
        // お届け日の設定
972
        $minDate = 0;
973
        $deliveryDurationFlag = false;
974
975
        // 配送時に最大となる商品日数を取得
976 View Code Duplication
        foreach ($Order->getOrderItems() as $item) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
977
            if (!$item->isProduct()) {
978
                continue;
979
            }
980
            $ProductClass = $item->getProductClass();
981
            $deliveryDuration = $ProductClass->getDeliveryDuration();
982
            if (!is_null($deliveryDuration)) {
983
                if ($deliveryDuration->getDuration() < 0) {
984
                    // 配送日数がマイナスの場合はお取り寄せなのでスキップする
985
                    $deliveryDurationFlag = false;
986
                    break;
987
                }
988
989
                if ($minDate < $deliveryDuration->getDuration()) {
990
                    $minDate = $deliveryDuration->getDuration();
991
                }
992
                // 配送日数が設定されている
993
                $deliveryDurationFlag = true;
994
            }
995
        }
996
997
        // 配達最大日数期間を設定
998
        $deliveryDurations = [];
999
1000
        // 配送日数が設定されている
1001 View Code Duplication
        if ($deliveryDurationFlag) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
1002
            $period = new \DatePeriod(
1003
                new \DateTime($minDate.' day'),
1004
                new \DateInterval('P1D'),
1005
                new \DateTime($minDate + $this->eccubeConfig['eccube_deliv_date_end_max'].' day')
1006
            );
1007
1008
            foreach ($period as $day) {
1009
                $deliveryDurations[$day->format('Y/m/d')] = $day->format('Y/m/d');
1010
            }
1011
        }
1012
1013
        return $deliveryDurations;
1014
    }
1015
1016
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$deliveries" missing
Loading history...
1017
     * 支払方法を取得
1018
     *
1019
     * @param $deliveries
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
1020
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
1021
     *
1022
     * @return array
1023
     */
1024
    public function getFormPayments($deliveries, Order $Order)
1025
    {
1026
        $saleTypes = $this->orderService->getSaleTypes($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::getSaleTypes() 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...
1027
        if ($this->BaseInfo->isOptionMultipleShipping() && count($saleTypes) > 1) {
1028
            // 複数配送時の支払方法
1029
1030
            $payments = $this->paymentRepository->findAllowedPayments($deliveries);
1031
        } else {
1032
            // 配送業者をセット
1033
            $shippings = $Order->getShippings();
1034
            $Shipping = $shippings[0];
1035
            $payments = $this->paymentRepository->findPayments($Shipping->getDelivery(), true);
1036
        }
1037
        $payments = $this->getPayments($payments, $Order->getSubTotal());
1038
1039
        return $payments;
1040
    }
1041
1042
    /**
1043
     * お届け先ごとにFormを作成
1044
     *
1045
     * @param Order $Order
1046
     *
1047
     * @return \Symfony\Component\Form\Form
1048
     *
1049
     * @deprecated since 3.0, to be removed in 3.1
1050
     */
1051 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...
1052
    {
1053
        $message = $Order->getMessage();
1054
1055
        $deliveries = $this->getDeliveriesOrder($Order);
1056
1057
        // 配送業者の支払方法を取得
1058
        $payments = $this->getFormPayments($deliveries, $Order);
1059
1060
        $builder = $this->formFactory->createBuilder('shopping', null, [
1061
            'payments' => $payments,
1062
            'payment' => $Order->getPayment(),
1063
            'message' => $message,
1064
        ]);
1065
1066
        $builder
1067
            ->add('shippings', CollectionType::class, [
1068
                'entry_type' => ShippingItemType::class,
1069
                'data' => $Order->getShippings(),
1070
            ]);
1071
1072
        $form = $builder->getForm();
1073
1074
        return $form;
1075
    }
1076
1077
    /**
1078
     * お届け先ごとにFormBuilderを作成
1079
     *
1080
     * @param Order $Order
1081
     *
1082
     * @return \Symfony\Component\Form\FormBuilderInterface
1083
     *
1084
     * @deprecated 利用している箇所なし
1085
     */
1086 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...
1087
    {
1088
        $message = $Order->getMessage();
1089
1090
        $deliveries = $this->getDeliveriesOrder($Order);
1091
1092
        // 配送業者の支払方法を取得
1093
        $payments = $this->getFormPayments($deliveries, $Order);
1094
1095
        $builder = $this->formFactory->createBuilder('shopping', null, [
1096
            'payments' => $payments,
1097
            'payment' => $Order->getPayment(),
1098
            'message' => $message,
1099
        ]);
1100
1101
        $builder
1102
            ->add('shippings', CollectionType::class, [
1103
                'entry_type' => ShippingItemType::class,
1104
                'data' => $Order->getShippings(),
1105
            ]);
1106
1107
        return $builder;
1108
    }
1109
1110
    /**
1111
     * フォームデータを更新
1112
     *
1113
     * @param Order $Order
1114
     * @param array $data
1115
     *
1116
     * @deprecated
1117
     */
1118
    public function setFormData(Order $Order, array $data)
1119
    {
1120
        // お問い合わせ
1121
        $Order->setMessage($data['message']);
1122
1123
        // お届け先情報を更新
1124
        $shippings = $data['shippings'];
1125
        foreach ($shippings as $Shipping) {
1126
            $timeId = $Shipping->getTimeId();
1127
            $deliveryTime = null;
1128
            if ($timeId) {
1129
                $deliveryTime = $this->deliveryTimeRepository->find($timeId);
1130
            }
1131
            if ($deliveryTime) {
1132
                $Shipping->setShippingDeliveryTime($deliveryTime->getDeliveryTime());
1133
                $Shipping->setTimeId($timeId);
1134
            }
1135
        }
1136
    }
1137
1138
    /**
1139
     * 配送料の合計金額を計算
1140
     *
1141
     * @param Order $Order
1142
     *
1143
     * @return Order
1144
     */
1145 2
    public function calculateDeliveryFee(Order $Order)
1146
    {
1147
        // 配送業者を取得
1148 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...
1149
1150
        // 配送料合計金額
1151
        // TODO CalculateDeliveryFeeStrategy でセットする
1152
        // $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($shippings));
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1153
1154
        // 配送料無料条件(合計金額)
1155 2
        $this->setDeliveryFreeAmount($Order);
1156
1157
        // 配送料無料条件(合計数量)
1158 2
        $this->setDeliveryFreeQuantity($Order);
1159
1160 2
        return $Order;
1161
    }
1162
1163
    /**
1164
     * 購入処理を行う
1165
     *
1166
     * @param Order $Order
1167
     *
1168
     * @deprecated PurchaseFlow::purchase() を使用してください
1169
     */
1170 2
    public function processPurchase(Order $Order)
1171
    {
1172
        // 受注情報、配送情報を更新
1173 2
        $Order = $this->calculateDeliveryFee($Order);
1174 2
        $this->setOrderUpdateData($Order);
1175
1176 2
        if ($this->authorizationChecker->isGranted('ROLE_USER')) {
1177 1
            $this->setCustomerUpdate($Order, $Order->getCustomer());
1178
        }
1179
    }
1180
1181
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$discount" missing
Loading history...
1182
     * 値引き可能かチェック
1183
     *
1184
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
1185
     * @param       $discount
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
1186
     *
1187
     * @return bool
1188
     */
1189
    public function isDiscount(Order $Order, $discount)
1190
    {
1191
        if ($Order->getTotal() < $discount) {
1192
            return false;
1193
        }
1194
1195
        return true;
1196
    }
1197
1198
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$discount" missing
Loading history...
1199
     * 値引き金額をセット
1200
     *
1201
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
1202
     * @param $discount
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
1203
     */
1204
    public function setDiscount(Order $Order, $discount)
1205
    {
1206
        $Order->setDiscount($Order->getDiscount() + $discount);
1207
    }
1208
1209
    /**
1210
     * 合計金額を計算
1211
     *
1212
     * @param Order $Order
1213
     *
1214
     * @return Order
1215
     */
1216
    public function calculatePrice(Order $Order)
1217
    {
1218
        $total = $Order->getTotalPrice();
1219
1220
        if ($total < 0) {
1221
            // 合計金額がマイナスの場合、0を設定し、discountは値引きされた額のみセット
1222
            $total = 0;
1223
        }
1224
1225
        $Order->setTotal($total);
1226
        $Order->setPaymentTotal($total);
1227
1228
        return $Order;
1229
    }
1230
1231
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$status" missing
Loading history...
1232
     * 受注ステータスをセット
1233
     *
1234
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
1235
     * @param $status
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
1236
     *
1237
     * @return Order
1238
     */
1239 2
    public function setOrderStatus(Order $Order, $status)
1240
    {
1241 2
        $Order->setOrderDate(new \DateTime());
1242 2
        $Order->setOrderStatus($this->orderStatusRepository->find($status));
1243
1244 2
        $event = new EventArgs(
1245
            [
1246 2
                'Order' => $Order,
1247
            ],
1248 2
            null
1249
        );
1250 2
        $this->eventDispatcher->dispatch(EccubeEvents::SERVICE_SHOPPING_ORDER_STATUS, $event);
1251
1252 2
        return $Order;
1253
    }
1254
1255
    /**
1256
     * 受注メール送信を行う
1257
     *
1258
     * @param Order $Order
1259
     *
1260
     * @return MailHistory
1261
     */
1262 2
    public function sendOrderMail(Order $Order)
1263
    {
1264
        // メール送信
1265 2
        $message = $this->mailService->sendOrderMail($Order);
1266
1267
        // 送信履歴を保存.
1268 2
        $MailHistory = new MailHistory();
1269
        $MailHistory
1270 2
            ->setMailSubject($message->getSubject())
1271 2
            ->setMailBody($message->getBody())
1272 2
            ->setSendDate(new \DateTime())
1273 2
            ->setOrder($Order);
1274
1275 2
        $this->entityManager->persist($MailHistory);
1276 2
        $this->entityManager->flush($MailHistory);
1277
1278 2
        return $MailHistory;
1279
    }
1280
1281
    /**
1282
     * 受注処理完了通知
1283
     *
1284
     * @param Order $Order
1285
     */
1286
    public function notifyComplete(Order $Order)
1287
    {
1288
        $event = new EventArgs(
1289
            [
1290
                'Order' => $Order,
1291
            ],
1292
            null
1293
        );
1294
        $this->eventDispatcher->dispatch(EccubeEvents::SERVICE_SHOPPING_NOTIFY_COMPLETE, $event);
1295
    }
1296
}
1297