Completed
Pull Request — experimental/3.1 (#2479)
by chihiro
65:40 queued 24:05
created

ShoppingService::setShippingDeliveryFee()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 4
nop 2
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
ccs 0
cts 12
cp 0
crap 12
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\Customer;
33
use Eccube\Entity\Delivery;
34
use Eccube\Entity\MailHistory;
35
use Eccube\Entity\Order;
36
use Eccube\Entity\OrderDetail;
37
use Eccube\Entity\Product;
38
use Eccube\Entity\ProductClass;
39
use Eccube\Entity\ShipmentItem;
40
use Eccube\Entity\Shipping;
41
use Eccube\Event\EccubeEvents;
42
use Eccube\Event\EventArgs;
43
use Eccube\Exception\CartException;
44
use Eccube\Exception\ShoppingException;
45
use Eccube\Form\Type\ShippingItemType;
46
use Eccube\Repository\BaseInfoRepository;
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(BaseInfoRepository::class)
166
     * @var BaseInfoRepository
167
     */
168
    protected $baseInfoRepository;
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
        $BaseInfo = $this->baseInfoRepository->get();
470 3
        if ($BaseInfo->getOptionMultipleShipping() == Constant::ENABLED) {
471
            // 複数配送対応
472
473
            // 支払方法を取得
474 1
            $payments = $this->paymentRepository->findAllowedPayments($deliveries);
475
476 1
            if (count($productTypes) > 1) {
477
                // 商品種別が複数ある場合、配送対象となる配送業者を取得
478 1
                $deliveries = $this->deliveryRepository->findAllowedDeliveries($productTypes, $payments);
479
            }
480
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
481
        }
482
483 3
        return $deliveries;
484
485
    }
486
487
488
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$deliveries" missing
Loading history...
489
     * お届け先情報を作成
490
     *
491
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
492
     * @param Customer $Customer
0 ignored issues
show
introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
493
     * @param $deliveries
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
494
     * @return Order
495
     */
496
    public function getNewShipping(Order $Order, Customer $Customer, $deliveries)
497
    {
498
        $productTypes = array();
499
        foreach ($deliveries as $Delivery) {
500
            if (!in_array($Delivery->getProductType()->getId(), $productTypes)) {
501
                $Shipping = new Shipping();
502
503
                $this->copyToShippingFromCustomer($Shipping, $Customer)
504
                    ->setOrder($Order)
505
                    ->setDelFlg(Constant::DISABLED);
506
507
                // 配送料金の設定
508
                $this->setShippingDeliveryFee($Shipping, $Delivery);
509
510
                $this->entityManager->persist($Shipping);
511
512
                $Order->addShipping($Shipping);
513
514
                $productTypes[] = $Delivery->getProductType()->getId();
515
            }
516
        }
517
518
        return $Order;
519
    }
520
521
    /**
522
     * お届け先情報を作成
523
     *
524
     * @param \Eccube\Entity\Shipping $Shipping
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
525
     * @param \Eccube\Entity\Customer|null $Customer
526
     * @return \Eccube\Entity\Shipping
527
     */
528 1
    public function copyToShippingFromCustomer(Shipping $Shipping, Customer $Customer = null)
529
    {
530 1
        if (is_null($Customer)) {
531 1
            return $Shipping;
532
        }
533
534
        $CustomerAddress = $this->customerAddressRepository->findOneBy(
535
            array('Customer' => $Customer),
536
            array('id' => 'ASC')
537
        );
538
539
        if (!is_null($CustomerAddress)) {
540
            $Shipping
541
                ->setName01($CustomerAddress->getName01())
542
                ->setName02($CustomerAddress->getName02())
543
                ->setKana01($CustomerAddress->getKana01())
544
                ->setKana02($CustomerAddress->getKana02())
545
                ->setCompanyName($CustomerAddress->getCompanyName())
546
                ->setTel01($CustomerAddress->getTel01())
547
                ->setTel02($CustomerAddress->getTel02())
548
                ->setTel03($CustomerAddress->getTel03())
549
                ->setFax01($CustomerAddress->getFax01())
550
                ->setFax02($CustomerAddress->getFax02())
551
                ->setFax03($CustomerAddress->getFax03())
552
                ->setZip01($CustomerAddress->getZip01())
553
                ->setZip02($CustomerAddress->getZip02())
554
                ->setZipCode($CustomerAddress->getZip01().$CustomerAddress->getZip02())
555
                ->setPref($CustomerAddress->getPref())
556
                ->setAddr01($CustomerAddress->getAddr01())
557
                ->setAddr02($CustomerAddress->getAddr02());
558
        } else {
559
            $Shipping
560
                ->setName01($Customer->getName01())
561
                ->setName02($Customer->getName02())
562
                ->setKana01($Customer->getKana01())
563
                ->setKana02($Customer->getKana02())
564
                ->setCompanyName($Customer->getCompanyName())
565
                ->setTel01($Customer->getTel01())
566
                ->setTel02($Customer->getTel02())
567
                ->setTel03($Customer->getTel03())
568
                ->setFax01($Customer->getFax01())
569
                ->setFax02($Customer->getFax02())
570
                ->setFax03($Customer->getFax03())
571
                ->setZip01($Customer->getZip01())
572
                ->setZip02($Customer->getZip02())
573
                ->setZipCode($Customer->getZip01().$Customer->getZip02())
574
                ->setPref($Customer->getPref())
575
                ->setAddr01($Customer->getAddr01())
576
                ->setAddr02($Customer->getAddr02());
577
        }
578
579
        return $Shipping;
580
    }
581
582
583
    /**
584
     * 受注明細情報、配送商品情報を作成
585
     *
586
     * @param Order $Order
587
     * @return Order
588
     */
589
    public function getNewDetails(Order $Order)
590
    {
591
592
        // 受注詳細, 配送商品
593
        foreach ($this->cartService->getCart()->getCartItems() as $item) {
594
            /* @var $ProductClass \Eccube\Entity\ProductClass */
595
            $ProductClass = $item->getObject();
596
            /* @var $Product \Eccube\Entity\Product */
597
            $Product = $ProductClass->getProduct();
598
599
            $quantity = $item->getQuantity();
600
601
            // 受注明細情報を作成
602
            $OrderDetail = $this->getNewOrderDetail($Product, $ProductClass, $quantity);
603
            $OrderDetail->setOrder($Order);
604
            $Order->addOrderDetail($OrderDetail);
605
606
            // 配送商品情報を作成
607
            $this->getNewShipmentItem($Order, $Product, $ProductClass, $quantity);
608
        }
609
610
        return $Order;
611
612
    }
613
614
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$quantity" missing
Loading history...
615
     * 受注明細情報を作成
616
     *
617
     * @param Product $Product
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
618
     * @param ProductClass $ProductClass
619
     * @param $quantity
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
620
     * @return \Eccube\Entity\OrderDetail
621
     */
622
    public function getNewOrderDetail(Product $Product, ProductClass $ProductClass, $quantity)
623
    {
624
        $OrderDetail = new OrderDetail();
625
        $TaxRule = $this->taxRuleRepository->getByRule($Product, $ProductClass);
626
        $OrderDetail->setProduct($Product)
627
            ->setProductClass($ProductClass)
628
            ->setProductName($Product->getName())
629
            ->setProductCode($ProductClass->getCode())
630
            ->setPrice($ProductClass->getPrice02())
631
            ->setQuantity($quantity)
632
            ->setTaxRule($TaxRule->getCalcRule()->getId())
633
            ->setTaxRate($TaxRule->getTaxRate());
634
635
        $ClassCategory1 = $ProductClass->getClassCategory1();
636
        if (!is_null($ClassCategory1)) {
637
            $OrderDetail->setClasscategoryName1($ClassCategory1->getName());
638
            $OrderDetail->setClassName1($ClassCategory1->getClassName()->getName());
639
        }
640
        $ClassCategory2 = $ProductClass->getClassCategory2();
641
        if (!is_null($ClassCategory2)) {
642
            $OrderDetail->setClasscategoryName2($ClassCategory2->getName());
643
            $OrderDetail->setClassName2($ClassCategory2->getClassName()->getName());
644
        }
645
646
        $this->entityManager->persist($OrderDetail);
647
648
        return $OrderDetail;
649
    }
650
651
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$quantity" missing
Loading history...
652
     * 配送商品情報を作成
653
     *
654
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
655
     * @param Product $Product
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
656
     * @param ProductClass $ProductClass
657
     * @param $quantity
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
658
     * @return \Eccube\Entity\ShipmentItem
659
     */
660
    public function getNewShipmentItem(Order $Order, Product $Product, ProductClass $ProductClass, $quantity)
661
    {
662
663
        $ShipmentItem = new ShipmentItem();
664
        $shippings = $Order->getShippings();
665
        $BaseInfo = $this->baseInfoRepository->get();
666
667
        // 選択された商品がどのお届け先情報と関連するかチェック
668
        $Shipping = null;
669
        foreach ($shippings as $s) {
670
            if ($s->getDelivery()->getProductType()->getId() == $ProductClass->getProductType()->getId()) {
671
                // 商品種別が同一のお届け先情報と関連させる
672
                $Shipping = $s;
673
                break;
674
            }
675
        }
676
677
        if (is_null($Shipping)) {
678
            // お届け先情報と関連していない場合、エラー
679
            throw new CartException('shopping.delivery.not.producttype');
680
        }
681
682
        // 商品ごとの配送料合計
683
        $productDeliveryFeeTotal = 0;
684
        if (!is_null($BaseInfo->getOptionProductDeliveryFee())) {
685
            $productDeliveryFeeTotal = $ProductClass->getDeliveryFee() * $quantity;
686
        }
687
688
        $Shipping->setShippingDeliveryFee($Shipping->getShippingDeliveryFee() + $productDeliveryFeeTotal);
689
690
        $ShipmentItem->setShipping($Shipping)
691
            ->setOrder($Order)
692
            ->setProductClass($ProductClass)
693
            ->setProduct($Product)
694
            ->setProductName($Product->getName())
695
            ->setProductCode($ProductClass->getCode())
696
            ->setPrice($ProductClass->getPrice02())
697
            ->setQuantity($quantity);
698
699
        $ClassCategory1 = $ProductClass->getClassCategory1();
700
        if (!is_null($ClassCategory1)) {
701
            $ShipmentItem->setClasscategoryName1($ClassCategory1->getName());
702
            $ShipmentItem->setClassName1($ClassCategory1->getClassName()->getName());
703
        }
704
        $ClassCategory2 = $ProductClass->getClassCategory2();
705
        if (!is_null($ClassCategory2)) {
706
            $ShipmentItem->setClasscategoryName2($ClassCategory2->getName());
707
            $ShipmentItem->setClassName2($ClassCategory2->getClassName()->getName());
708
        }
709
        $Shipping->addShipmentItem($ShipmentItem);
710
        $this->entityManager->persist($ShipmentItem);
711
712
        return $ShipmentItem;
713
714
    }
715
716
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$shippings" missing
Loading history...
717
     * お届け先ごとの送料合計を取得
718
     *
719
     * @param $shippings
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
720
     * @return int
721
     */
722
    public function getShippingDeliveryFeeTotal($shippings)
723
    {
724
        $deliveryFeeTotal = 0;
725
        foreach ($shippings as $Shipping) {
726
            $deliveryFeeTotal += $Shipping->getShippingDeliveryFee();
727
        }
728
729
        return $deliveryFeeTotal;
730
731
    }
732
733
    /**
734
     * 商品ごとの配送料を取得
735
     *
736
     * @param Shipping $Shipping
737
     * @return int
738
     */
739
    public function getProductDeliveryFee(Shipping $Shipping)
740
    {
741
        $productDeliveryFeeTotal = 0;
742
        $shipmentItems = $Shipping->getShipmentItems();
743
        foreach ($shipmentItems as $ShipmentItem) {
744
            $productDeliveryFeeTotal += $ShipmentItem->getProductClass()->getDeliveryFee() * $ShipmentItem->getQuantity();
745
        }
746
747
        return $productDeliveryFeeTotal;
748
    }
749
750
    /**
751
     * 住所などの情報が変更された時に金額の再計算を行う
752
     * @deprecated PurchaseFlowで行う
753
     * @param Order $Order
754
     * @return Order
755
     */
756 1
    public function getAmount(Order $Order)
757
    {
758
759
        // 初期選択の配送業者をセット
760 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...
761
762
        // 配送料合計金額
763
        // TODO CalculateDeliveryFeeStrategy でセットする
764
        // $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($shippings));
765
766
        // 配送料無料条件(合計金額)
767 1
        $this->setDeliveryFreeAmount($Order);
768
769
        // 配送料無料条件(合計数量)
770 1
        $this->setDeliveryFreeQuantity($Order);
771
772
        // 合計金額の計算
773 1
        $this->calculatePrice($Order);
774
775 1
        return $Order;
776
777
    }
778
779
    /**
780
     * 配送料金の設定
781
     *
782
     * @param Shipping $Shipping
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
783
     * @param Delivery|null $Delivery
784
     */
785
    public function setShippingDeliveryFee(Shipping $Shipping, Delivery $Delivery = null)
786
    {
787
        // 配送料金の設定
788
        if (is_null($Delivery)) {
789
            $Delivery = $Shipping->getDelivery();
790
        }
791
        $deliveryFee = $this->deliveryFeeRepository->findOneBy(array('Delivery' => $Delivery, 'Pref' => $Shipping->getPref()));
792
793
        $Shipping->setDeliveryFee($deliveryFee);
794
        $Shipping->setDelivery($Delivery);
795
        $BaseInfo = $this->baseInfoRepository->get();
796
797
        // 商品ごとの配送料合計
798
        $productDeliveryFeeTotal = 0;
799
        if (!is_null($BaseInfo->getOptionProductDeliveryFee())) {
800
            $productDeliveryFeeTotal += $this->getProductDeliveryFee($Shipping);
801
        }
802
803
        $Shipping->setShippingDeliveryFee($deliveryFee->getFee() + $productDeliveryFeeTotal);
804
        $Shipping->setShippingDeliveryName($Delivery->getName());
805
    }
806
807
    /**
808
     * 配送料無料条件(合計金額)の条件を満たしていれば配送料金を0に設定
809
     *
810
     * @param Order $Order
811
     */
812 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...
813
    {
814
        // 配送料無料条件(合計金額)
815 4
        $BaseInfo = $this->baseInfoRepository->get();
816 4
        $deliveryFreeAmount = $BaseInfo->getDeliveryFreeAmount();
817 4
        if (!is_null($deliveryFreeAmount)) {
818
            // 合計金額が設定金額以上であれば送料無料
819 1
            if ($Order->getSubTotal() >= $deliveryFreeAmount) {
820 1
                $Order->setDeliveryFeeTotal(0);
821
                // お届け先情報の配送料も0にセット
822 1
                $shippings = $Order->getShippings();
823 1
                foreach ($shippings as $Shipping) {
824 1
                    $Shipping->setShippingDeliveryFee(0);
825
                }
826
            }
827
        }
828
    }
829
830
    /**
831
     * 配送料無料条件(合計数量)の条件を満たしていれば配送料金を0に設定
832
     *
833
     * @param Order $Order
834
     */
835 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...
836
    {
837
        // 配送料無料条件(合計数量)
838 3
        $BaseInfo = $this->baseInfoRepository->get();
839 3
        $deliveryFreeQuantity = $BaseInfo->getDeliveryFreeQuantity();
840 3
        if (!is_null($deliveryFreeQuantity)) {
841
            // 合計数量が設定数量以上であれば送料無料
842
            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...
843
                $Order->setDeliveryFeeTotal(0);
844
                // お届け先情報の配送料も0にセット
845
                $shippings = $Order->getShippings();
846
                foreach ($shippings as $Shipping) {
847
                    $Shipping->setShippingDeliveryFee(0);
848
                }
849
            }
850
        }
851
    }
852
853
854
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$em" missing
Loading history...
855
     * 商品公開ステータスチェック、在庫チェック、購入制限数チェックを行い、在庫情報をロックする
856
     *
857
     * @param $em トランザクション制御されているEntityManager
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
858
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 57 spaces after parameter type; 1 found
Loading history...
859
     * @return bool true : 成功、false : 失敗
860
     */
861 4
    public function isOrderProduct($em, \Eccube\Entity\Order $Order)
862
    {
863 4
        $orderDetails = $Order->getOrderDetails();
864
865 4
        foreach ($orderDetails as $orderDetail) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
866
867 4
            if (is_null($orderDetail->getProduct())) {
868
                // FIXME 配送明細を考慮する必要がある
869
                continue;
870
            }
871
872
            // 商品削除チェック
873 4
            if ($orderDetail->getProductClass()->getDelFlg()) {
874
                // @deprecated 3.1以降ではexceptionをthrowする
875
                // throw new ShoppingException('cart.product.delete');
876
                return false;
877
            }
878
879
            // 商品公開ステータスチェック
880 4
            if ($orderDetail->getProduct()->getStatus()->getId() != \Eccube\Entity\Master\Disp::DISPLAY_SHOW) {
881
                // 商品が非公開ならエラー
882
883
                // @deprecated 3.1以降ではexceptionをthrowする
884
                // throw new ShoppingException('cart.product.not.status');
885 1
                return false;
886
            }
887
888
            // 購入制限数チェック
889 3
            if (!is_null($orderDetail->getProductClass()->getSaleLimit())) {
890 2
                if ($orderDetail->getQuantity() > $orderDetail->getProductClass()->getSaleLimit()) {
891
                    // @deprecated 3.1以降ではexceptionをthrowする
892
                    // throw new ShoppingException('cart.over.sale_limit');
893 1
                    return false;
894
                }
895
            }
896
897
            // 購入数チェック
898 2
            if ($orderDetail->getQuantity() < 1) {
899
                // 購入数量が1未満ならエラー
900
901
                // @deprecated 3.1以降ではexceptionをthrowする
902
                // throw new ShoppingException('???');
903 2
                return false;
904
            }
905
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
906
        }
907
908
        // 在庫チェック
909 2
        foreach ($orderDetails as $orderDetail) {
910 2
            if (is_null($orderDetail->getProductClass())) {
911
                // FIXME 配送明細を考慮する必要がある
912
                continue;
913
            }
914
            // 在庫が無制限かチェックし、制限ありなら在庫数をチェック
915 2
            if ($orderDetail->getProductClass()->getStockUnlimited() == Constant::DISABLED) {
916
                // 在庫チェックあり
917
                // 在庫に対してロック(select ... for update)を実行
918 1
                $productStock = $em->getRepository('Eccube\Entity\ProductStock')->find(
919 1
                    $orderDetail->getProductClass()->getProductStock()->getId(), LockMode::PESSIMISTIC_WRITE
920
                );
921
                // 購入数量と在庫数をチェックして在庫がなければエラー
922 1
                if ($productStock->getStock() < 1) {
923
                    // @deprecated 3.1以降ではexceptionをthrowする
924
                    // throw new ShoppingException('cart.over.stock');
925
                    return false;
926 1
                } elseif ($orderDetail->getQuantity() > $productStock->getStock()) {
927
                    // @deprecated 3.1以降ではexceptionをthrowする
928
                    // throw new ShoppingException('cart.over.stock');
929 2
                    return false;
930
                }
931
            }
932
        }
933
934 1
        return true;
935
936
    }
937
938
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$data" missing
Loading history...
939
     * 受注情報、お届け先情報の更新
940
     *
941
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 22 spaces after parameter type; 1 found
Loading history...
942
     * @param $data フォームデータ
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
943
     *
944
     * @deprecated since 3.0.5, to be removed in 3.1
945
     */
946
    public function setOrderUpdate(Order $Order, $data)
947
    {
948
        // 受注情報を更新
949
        $Order->setOrderDate(new \DateTime());
950
        $Order->setOrderStatus($this->orderStatusRepository->find($this->appConfig['order_new']));
951
        $Order->setMessage($data['message']);
952
        // お届け先情報を更新
953
        $shippings = $data['shippings'];
954
        $BaseInfo = $this->baseInfoRepository->get();
955
        foreach ($shippings as $Shipping) {
956
            $Delivery = $Shipping->getDelivery();
957
            $deliveryFee = $this->deliveryFeeRepository->findOneBy(array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
958
                'Delivery' => $Delivery,
959
                'Pref' => $Shipping->getPref()
960
            ));
961
            $deliveryTime = $Shipping->getDeliveryTime();
962
            if (!empty($deliveryTime)) {
963
                $Shipping->setShippingDeliveryTime($deliveryTime->getDeliveryTime());
964
            }
965
            $Shipping->setDeliveryFee($deliveryFee);
966
            // 商品ごとの配送料合計
967
            $productDeliveryFeeTotal = 0;
968
            if (!is_null($BaseInfo->getOptionProductDeliveryFee())) {
969
                $productDeliveryFeeTotal += $this->getProductDeliveryFee($Shipping);
970
            }
971
            $Shipping->setShippingDeliveryFee($deliveryFee->getFee() + $productDeliveryFeeTotal);
972
            $Shipping->setShippingDeliveryName($Delivery->getName());
973
        }
974
        // 配送料無料条件(合計金額)
975
        $this->setDeliveryFreeAmount($Order);
976
        // 配送料無料条件(合計数量)
977
        $this->setDeliveryFreeQuantity($Order);
978
    }
979
980
981
    /**
982
     * 受注情報の更新
983
     *
984
     * @param Order $Order 受注情報
985
     */
986 3
    public function setOrderUpdateData(Order $Order)
987
    {
988
        // 受注情報を更新
989 3
        $Order->setOrderDate(new \DateTime()); // XXX 後続の setOrderStatus でも時刻を更新している
990 3
        $OrderStatus = $this->orderStatusRepository->find($this->appConfig['order_new']);
991 3
        $this->setOrderStatus($Order, $OrderStatus);
992
993
    }
994
995
996
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$em" missing
Loading history...
997
     * 在庫情報の更新
998
     *
999
     * @param $em トランザクション制御されているEntityManager
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
1000
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 57 spaces after parameter type; 1 found
Loading history...
1001
     */
1002 3
    public function setStockUpdate($em, Order $Order)
1003
    {
1004
1005 3
        $orderDetails = $Order->getOrderDetails();
1006
1007
        // 在庫情報更新
1008 3
        foreach ($orderDetails as $orderDetail) {
1009 1
            if (is_null($orderDetail->getProductClass())) {
1010
                // FIXME 配送明細を考慮する必要がある
1011
                continue;
1012
            }
1013
            // 在庫が無制限かチェックし、制限ありなら在庫数を更新
1014 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...
1015
1016 1
                $productStock = $em->getRepository('Eccube\Entity\ProductStock')->find(
1017 1
                    $orderDetail->getProductClass()->getProductStock()->getId()
1018
                );
1019
1020
                // 在庫情報の在庫数を更新
1021 1
                $stock = $productStock->getStock() - $orderDetail->getQuantity();
1022 1
                $productStock->setStock($stock);
1023
1024
                // 商品規格情報の在庫数を更新
1025 1
                $orderDetail->getProductClass()->setStock($stock);
1026
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
1027
            }
1028
        }
1029
1030
    }
1031
1032
1033
    /**
1034
     * 会員情報の更新
1035
     *
1036
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
1037
     * @param Customer $user ログインユーザ
0 ignored issues
show
introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
1038
     */
1039 2
    public function setCustomerUpdate(Order $Order, Customer $user)
1040
    {
1041
1042 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...
1043
1044
        // 顧客情報を更新
1045 2
        $now = new \DateTime();
1046 2
        $firstBuyDate = $user->getFirstBuyDate();
1047 2
        if (empty($firstBuyDate)) {
1048 2
            $user->setFirstBuyDate($now);
1049
        }
1050 2
        $user->setLastBuyDate($now);
1051
1052 2
        $user->setBuyTimes($user->getBuyTimes() + 1);
1053 2
        $user->setBuyTotal($user->getBuyTotal() + $Order->getTotal());
1054
1055
    }
1056
1057
1058
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$payments" missing
Loading history...
introduced by
Doc comment for parameter "$subTotal" missing
Loading history...
1059
     * 支払方法選択の表示設定
1060
     *
1061
     * @param $payments 支払選択肢情報
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
1062
     * @param $subTotal 小計
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
1063
     * @return array
1064
     */
1065 1
    public function getPayments($payments, $subTotal)
1066
    {
1067 1
        $pays = array();
1068 1
        foreach ($payments as $payment) {
1069
            // 支払方法の制限値内であれば表示
1070 1
            if (!is_null($payment)) {
1071 1
                $pay = $this->paymentRepository->find($payment['id']);
1072 1
                if (intval($pay->getRuleMin()) <= $subTotal) {
1073 1
                    if (is_null($pay->getRuleMax()) || $pay->getRuleMax() >= $subTotal) {
1074 1
                        $pays[] = $pay;
1075
                    }
1076
                }
1077
            }
1078
        }
1079
1080 1
        return $pays;
1081
1082
    }
1083
1084
    /**
1085
     * お届け日を取得
1086
     *
1087
     * @param Order $Order
1088
     * @return array
1089
     */
1090 2
    public function getFormDeliveryDates(Order $Order)
1091
    {
1092
1093
        // お届け日の設定
1094 2
        $minDate = 0;
1095 2
        $deliveryDateFlag = false;
1096
1097
        // 配送時に最大となる商品日数を取得
1098 2
        foreach ($Order->getOrderDetails() as $detail) {
1099 2
            $deliveryDate = $detail->getProductClass()->getDeliveryDate();
1100 2
            if (!is_null($deliveryDate)) {
1101 2
                if ($deliveryDate->getValue() < 0) {
1102
                    // 配送日数がマイナスの場合はお取り寄せなのでスキップする
1103 1
                    $deliveryDateFlag = false;
1104 1
                    break;
1105
                }
1106
1107 1
                if ($minDate < $deliveryDate->getValue()) {
1108
                    $minDate = $deliveryDate->getValue();
1109
                }
1110
                // 配送日数が設定されている
1111 1
                $deliveryDateFlag = true;
1112
            }
1113
        }
1114
1115
        // 配達最大日数期間を設定
1116 2
        $deliveryDates = array();
1117
1118
        // 配送日数が設定されている
1119 2 View Code Duplication
        if ($deliveryDateFlag) {
1120 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...
1121 1
                new \DateTime($minDate.' day'),
1122 1
                new \DateInterval('P1D'),
1123 1
                new \DateTime($minDate + $this->appConfig['deliv_date_end_max'].' day')
1124
            );
1125
1126 1
            foreach ($period as $day) {
1127 1
                $deliveryDates[$day->format('Y/m/d')] = $day->format('Y/m/d');
1128
            }
1129
        }
1130
1131 2
        return $deliveryDates;
1132
1133
    }
1134
1135
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$deliveries" missing
Loading history...
1136
     * 支払方法を取得
1137
     *
1138
     * @param $deliveries
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
1139
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
1140
     * @return array
1141
     */
1142
    public function getFormPayments($deliveries, Order $Order)
1143
    {
1144
1145
        $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...
1146
        $BaseInfo = $this->baseInfoRepository->get();
1147
        if ($BaseInfo->getOptionMultipleShipping() == Constant::ENABLED && count($productTypes) > 1) {
1148
            // 複数配送時の支払方法
1149
1150
            $payments = $this->paymentRepository->findAllowedPayments($deliveries);
1151
        } else {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
1152
1153
            // 配送業者をセット
1154
            $shippings = $Order->getShippings();
1155
            $Shipping = $shippings[0];
1156
            $payments = $this->paymentRepository->findPayments($Shipping->getDelivery(), true);
1157
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
1158
        }
1159
        $payments = $this->getPayments($payments, $Order->getSubTotal());
1160
1161
        return $payments;
1162
1163
    }
1164
1165
    /**
1166
     * お届け先ごとにFormを作成
1167
     *
1168
     * @param Order $Order
1169
     * @return \Symfony\Component\Form\Form
1170
     * @deprecated since 3.0, to be removed in 3.1
1171
     */
1172 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...
1173
    {
1174
        $message = $Order->getMessage();
1175
1176
        $deliveries = $this->getDeliveriesOrder($Order);
1177
1178
        // 配送業者の支払方法を取得
1179
        $payments = $this->getFormPayments($deliveries, $Order);
1180
1181
        $builder = $this->formFactory->createBuilder('shopping', null, array(
1182
            'payments' => $payments,
1183
            'payment' => $Order->getPayment(),
1184
            'message' => $message,
1185
        ));
1186
1187
        $builder
1188
            ->add('shippings', CollectionType::class, array(
1189
                'entry_type' => ShippingItemType::class,
1190
                'data' => $Order->getShippings(),
1191
            ));
1192
1193
        $form = $builder->getForm();
1194
1195
        return $form;
1196
1197
    }
1198
1199
    /**
1200
     * お届け先ごとにFormBuilderを作成
1201
     *
1202
     * @param Order $Order
1203
     * @return \Symfony\Component\Form\FormBuilderInterface
1204
     *
1205
     * @deprecated 利用している箇所なし
1206
     */
1207 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...
1208
    {
1209
        $message = $Order->getMessage();
1210
1211
        $deliveries = $this->getDeliveriesOrder($Order);
1212
1213
        // 配送業者の支払方法を取得
1214
        $payments = $this->getFormPayments($deliveries, $Order);
1215
1216
        $builder = $this->formFactory->createBuilder('shopping', null, array(
1217
            'payments' => $payments,
1218
            'payment' => $Order->getPayment(),
1219
            'message' => $message,
1220
        ));
1221
1222
        $builder
1223
            ->add('shippings', CollectionType::class, array(
1224
                'entry_type' => ShippingItemType::class,
1225
                'data' => $Order->getShippings(),
1226
            ));
1227
1228
        return $builder;
1229
1230
    }
1231
1232
1233
    /**
1234
     * フォームデータを更新
1235
     *
1236
     * @param Order $Order
1237
     * @param array $data
1238
     *
1239
     * @deprecated
1240
     */
1241 1
    public function setFormData(Order $Order, array $data)
1242
    {
1243
1244
        // お問い合わせ
1245 1
        $Order->setMessage($data['message']);
1246
1247
        // お届け先情報を更新
1248 1
        $shippings = $data['shippings'];
1249 1
        foreach ($shippings as $Shipping) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
1250
1251 1
            $deliveryTime = $Shipping->getDeliveryTime();
1252 1
            if (!empty($deliveryTime)) {
1253 1
                $Shipping->setShippingDeliveryTime($deliveryTime->getDeliveryTime());
1254
            }
1255
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
1256
        }
1257
1258
    }
1259
1260
    /**
1261
     * 配送料の合計金額を計算
1262
     *
1263
     * @param Order $Order
1264
     * @return Order
1265
     */
1266 2
    public function calculateDeliveryFee(Order $Order)
1267
    {
1268
1269
        // 配送業者を取得
1270 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...
1271
1272
        // 配送料合計金額
1273
        // TODO CalculateDeliveryFeeStrategy でセットする
1274
        // $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($shippings));
1275
1276
        // 配送料無料条件(合計金額)
1277 2
        $this->setDeliveryFreeAmount($Order);
1278
1279
        // 配送料無料条件(合計数量)
1280 2
        $this->setDeliveryFreeQuantity($Order);
1281
1282 2
        return $Order;
1283
1284
    }
1285
1286
1287
    /**
1288
     * 購入処理を行う
1289
     *
1290
     * @param Order $Order
1291
     * @throws ShoppingException
1292
     */
1293 2
    public function processPurchase(Order $Order)
1294
    {
1295
1296 2
        $em = $this->entityManager;
1297
1298
        // TODO PurchaseFlowでやる
1299
//        // 合計金額の再計算
1300
//        $this->calculatePrice($Order);
1301
//
1302
//        // 商品公開ステータスチェック、商品制限数チェック、在庫チェック
1303
//        $check = $this->isOrderProduct($em, $Order);
1304
//        if (!$check) {
1305
//            throw new ShoppingException('front.shopping.stock.error');
1306
//        }
1307
1308
        // 受注情報、配送情報を更新
1309 2
        $Order = $this->calculateDeliveryFee($Order);
1310 2
        $this->setOrderUpdateData($Order);
1311
        // 在庫情報を更新
1312 2
        $this->setStockUpdate($em, $Order);
1313
1314 2
        if ($this->app->isGranted('ROLE_USER')) {
1315
            // 会員の場合、購入金額を更新
1316 1
            $this->setCustomerUpdate($Order, $this->app->user());
1317
        }
1318
1319
    }
1320
1321
1322
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$discount" missing
Loading history...
1323
     * 値引き可能かチェック
1324
     *
1325
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
1326
     * @param       $discount
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
1327
     * @return bool
1328
     */
1329
    public function isDiscount(Order $Order, $discount)
1330
    {
1331
1332
        if ($Order->getTotal() < $discount) {
1333
            return false;
1334
        }
1335
1336
        return true;
1337
    }
1338
1339
1340
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$discount" missing
Loading history...
1341
     * 値引き金額をセット
1342
     *
1343
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
1344
     * @param $discount
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
1345
     */
1346
    public function setDiscount(Order $Order, $discount)
1347
    {
1348
1349
        $Order->setDiscount($Order->getDiscount() + $discount);
1350
1351
    }
1352
1353
1354
    /**
1355
     * 合計金額を計算
1356
     *
1357
     * @param Order $Order
1358
     * @return Order
1359
     */
1360 1
    public function calculatePrice(Order $Order)
1361
    {
1362
1363 1
        $total = $Order->getTotalPrice();
1364
1365 1
        if ($total < 0) {
1366
            // 合計金額がマイナスの場合、0を設定し、discountは値引きされた額のみセット
1367
            $total = 0;
1368
        }
1369
1370 1
        $Order->setTotal($total);
1371 1
        $Order->setPaymentTotal($total);
1372
1373 1
        return $Order;
1374
1375
    }
1376
1377
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$status" missing
Loading history...
1378
     * 受注ステータスをセット
1379
     *
1380
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
1381
     * @param $status
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
1382
     * @return Order
1383
     */
1384 3
    public function setOrderStatus(Order $Order, $status)
1385
    {
1386
1387 3
        $Order->setOrderDate(new \DateTime());
1388 3
        $Order->setOrderStatus($this->orderStatusRepository->find($status));
1389
1390 3
        $event = new EventArgs(
1391
            array(
1392 3
                'Order' => $Order,
1393
            ),
1394 3
            null
1395
        );
1396 3
        $this->eventDispatcher->dispatch(EccubeEvents::SERVICE_SHOPPING_ORDER_STATUS, $event);
1397
1398 3
        return $Order;
1399
1400
    }
1401
1402
    /**
1403
     * 受注メール送信を行う
1404
     *
1405
     * @param Order $Order
1406
     * @return MailHistory
1407
     */
1408 2
    public function sendOrderMail(Order $Order)
1409
    {
1410
1411
        // メール送信
1412 2
        $message = $this->mailService->sendOrderMail($Order);
1413
1414
        // 送信履歴を保存.
1415 2
        $MailTemplate = $this->mailTemplateRepository->find(1);
1416
1417 2
        $MailHistory = new MailHistory();
1418
        $MailHistory
1419 2
            ->setSubject($message->getSubject())
1420 2
            ->setMailBody($message->getBody())
1421 2
            ->setMailTemplate($MailTemplate)
1422 2
            ->setSendDate(new \DateTime())
1423 2
            ->setOrder($Order);
1424
1425 2
        $this->entityManager->persist($MailHistory);
1426 2
        $this->entityManager->flush($MailHistory);
1427
1428 2
        return $MailHistory;
1429
1430
    }
1431
1432
1433
    /**
1434
     * 受注処理完了通知
1435
     *
1436
     * @param Order $Order
1437
     */
1438
    public function notifyComplete(Order $Order)
1439
    {
1440
1441
        $event = new EventArgs(
1442
            array(
1443
                'Order' => $Order,
1444
            ),
1445
            null
1446
        );
1447
        $this->eventDispatcher->dispatch(EccubeEvents::SERVICE_SHOPPING_NOTIFY_COMPLETE, $event);
1448
1449
    }
1450
1451
1452
}
1453