Failed Conditions
Pull Request — experimental/3.1 (#2486)
by
unknown
68:04 queued 25:47
created

ShoppingService::getShippingFormBuilder()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 13

Duplication

Lines 24
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 1
dl 24
loc 24
rs 8.9713
c 0
b 0
f 0
ccs 0
cts 12
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\DBAL\LockMode;
27
use Doctrine\ORM\EntityManager;
28
use Eccube\Annotation\Inject;
29
use Eccube\Annotation\Service;
30
use Eccube\Application;
31
use Eccube\Common\Constant;
32
use Eccube\Entity\BaseInfo;
33
use Eccube\Entity\Customer;
34
use Eccube\Entity\Delivery;
35
use Eccube\Entity\MailHistory;
36
use Eccube\Entity\Order;
37
use Eccube\Entity\OrderDetail;
38
use Eccube\Entity\Product;
39
use Eccube\Entity\ProductClass;
40
use Eccube\Entity\ShipmentItem;
41
use Eccube\Entity\Shipping;
42
use Eccube\Event\EccubeEvents;
43
use Eccube\Event\EventArgs;
44
use Eccube\Exception\CartException;
45
use Eccube\Exception\ShoppingException;
46
use Eccube\Form\Type\ShippingItemType;
47
use Eccube\Repository\CustomerAddressRepository;
48
use Eccube\Repository\DeliveryFeeRepository;
49
use Eccube\Repository\DeliveryRepository;
50
use Eccube\Repository\MailTemplateRepository;
51
use Eccube\Repository\Master\DeviceTypeRepository;
52
use Eccube\Repository\Master\OrderStatusRepository;
53
use Eccube\Repository\Master\PrefRepository;
54
use Eccube\Repository\OrderRepository;
55
use Eccube\Repository\PaymentRepository;
56
use Eccube\Repository\TaxRuleRepository;
57
use Eccube\Util\Str;
58
use Symfony\Component\EventDispatcher\EventDispatcher;
59
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
60
use Symfony\Component\Form\FormFactory;
61
use Symfony\Component\HttpFoundation\Session\Session;
62
63
/**
64
 * @Service
65
 */
66
class ShoppingService
67
{
68
    /**
69
     * @Inject(MailTemplateRepository::class)
70
     * @var MailTemplateRepository
71
     */
72
    protected $mailTemplateRepository;
73
74
    /**
75
     * @Inject(MailService::class)
76
     * @var MailService
77
     */
78
    protected $mailService;
79
80
    /**
81
     * @Inject("eccube.event.dispatcher")
82
     * @var EventDispatcher
83
     */
84
    protected $eventDispatcher;
85
86
    /**
87
     * @Inject("form.factory")
88
     * @var FormFactory
89
     */
90
    protected $formFactory;
91
92
    /**
93
     * @Inject(DeliveryFeeRepository::class)
94
     * @var DeliveryFeeRepository
95
     */
96
    protected $deliveryFeeRepository;
97
98
    /**
99
     * @Inject(TaxRuleRepository::class)
100
     * @var TaxRuleRepository
101
     */
102
    protected $taxRuleRepository;
103
104
    /**
105
     * @Inject(CustomerAddressRepository::class)
106
     * @var CustomerAddressRepository
107
     */
108
    protected $customerAddressRepository;
109
110
    /**
111
     * @Inject(DeliveryRepository::class)
112
     * @var DeliveryRepository
113
     */
114
    protected $deliveryRepository;
115
116
    /**
117
     * @Inject(OrderStatusRepository::class)
118
     * @var OrderStatusRepository
119
     */
120
    protected $orderStatusRepository;
121
122
    /**
123
     * @Inject(PaymentRepository::class)
124
     * @var PaymentRepository
125
     */
126
    protected $paymentRepository;
127
128
    /**
129
     * @Inject(DeviceTypeRepository::class)
130
     * @var DeviceTypeRepository
131
     */
132
    protected $deviceTypeRepository;
133
134
    /**
135
     * @Inject("orm.em")
136
     * @var EntityManager
137
     */
138
    protected $entityManager;
139
140
    /**
141
     * @Inject("config")
142
     * @var array
143
     */
144
    protected $appConfig;
145
146
    /**
147
     * @Inject(PrefRepository::class)
148
     * @var PrefRepository
149
     */
150
    protected $prefRepository;
151
152
    /**
153
     * @Inject("session")
154
     * @var Session
155
     */
156
    protected $session;
157
158
    /**
159
     * @Inject(OrderRepository::class)
160
     * @var OrderRepository
161
     */
162
    protected $orderRepository;
163
164
    /**
165
     * @Inject(BaseInfo::class)
166
     * @var BaseInfo
167
     */
168
    protected $BaseInfo;
169
170
    /**
171
     * @Inject(Application::class)
172
     * @var \Eccube\Application
173
     */
174
    public $app;
175
176
    /**
177
     * @Inject(CartService::class)
178
     * @var \Eccube\Service\CartService
179
     */
180
    protected $cartService;
181
182
    /**
183
     * @var \Eccube\Service\OrderService
184
     *
185
     * @deprecated
186
     */
187
    protected $orderService;
188
189
    /**
190
     * セッションにセットされた受注情報を取得
191
     *
192
     * @param null $status
193
     * @return null|object
194
     */
195 11
    public function getOrder($status = null)
196
    {
197
198
        // 受注データを取得
199 11
        $preOrderId = $this->cartService->getPreOrderId();
200 11
        if (!$preOrderId) {
201 11
            return null;
202
        }
203
204
        $condition = array(
205 5
            'pre_order_id' => $preOrderId,
206
        );
207
208 5
        if (!is_null($status)) {
209
            $condition += array(
210 5
                'OrderStatus' => $status,
211
            );
212
        }
213
214 5
        $Order = $this->orderRepository->findOneBy($condition);
215
216 5
        return $Order;
217
218
    }
219
220
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$sesisonKey" missing
Loading history...
221
     * 非会員情報を取得
222
     *
223
     * @param $sesisonKey
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
224
     * @return $Customer|null
0 ignored issues
show
Documentation introduced by
The doc-type $Customer|null could not be parsed: Unknown type name "$Customer" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
225
     */
226 4
    public function getNonMember($sesisonKey)
227
    {
228
229
        // 非会員でも一度会員登録されていればショッピング画面へ遷移
230 4
        $nonMember = $this->session->get($sesisonKey);
231 4
        if (is_null($nonMember)) {
232 1
            return null;
233
        }
234 3
        if (!array_key_exists('customer', $nonMember) || !array_key_exists('pref', $nonMember)) {
235
            return null;
236
        }
237
238 3
        $Customer = $nonMember['customer'];
239 3
        $Customer->setPref($this->prefRepository->find($nonMember['pref']));
240
241 3
        foreach ($Customer->getCustomerAddresses() as $CustomerAddress) {
242 3
            $Pref = $CustomerAddress->getPref();
243 3
            if ($Pref) {
244 3
                $CustomerAddress->setPref($this->prefRepository->find($Pref->getId()));
245
            }
246
        }
247
248 3
        return $Customer;
249
250
    }
251
252
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
253
     * 受注情報を作成
254
     *
255
     * @param $Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
256
     * @return \Eccube\Entity\Order
257
     */
258
    public function createOrder($Customer)
259
    {
260
        // ランダムなpre_order_idを作成
261 View Code Duplication
        do {
262
            $preOrderId = sha1(Str::random(32));
263
            $Order = $this->orderRepository->findOneBy(array(
264
                'pre_order_id' => $preOrderId,
265
                'OrderStatus' => $this->appConfig['order_processing'],
266
            ));
267
        } while ($Order);
268
269
        // 受注情報、受注明細情報、お届け先情報、配送商品情報を作成
270
        $Order = $this->registerPreOrder(
271
            $Customer,
272
            $preOrderId);
273
274
        $this->cartService->setPreOrderId($preOrderId);
275
        $this->cartService->save();
276
277
        return $Order;
278
    }
279
280
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
introduced by
Doc comment for parameter "$preOrderId" missing
Loading history...
281
     * 仮受注情報作成
282
     *
283
     * @param $Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
284
     * @param $preOrderId
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
285
     * @return mixed
286
     * @throws \Doctrine\ORM\NoResultException
287
     * @throws \Doctrine\ORM\NonUniqueResultException
288
     */
289
    public function registerPreOrder(Customer $Customer, $preOrderId)
290
    {
291
292
        $this->em = $this->entityManager;
293
294
        // 受注情報を作成
295
        $Order = $this->getNewOrder($Customer);
296
        $Order->setPreOrderId($preOrderId);
297
298
        $DeviceType = $this->deviceTypeRepository->find($this->app['mobile_detect.device_type']);
299
        $Order->setDeviceType($DeviceType);
300
301
        $this->entityManager->persist($Order);
302
303
        // 配送業者情報を取得
304
        $deliveries = $this->getDeliveriesCart();
305
306
        // お届け先情報を作成
307
        $Order = $this->getNewShipping($Order, $Customer, $deliveries);
308
309
        // 受注明細情報、配送商品情報を作成
310
        $Order = $this->getNewDetails($Order);
311
312
        // 小計
313
        $subTotal = $this->orderService->getSubTotal($Order);
0 ignored issues
show
Deprecated Code introduced by
The property Eccube\Service\ShoppingService::$orderService has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
Deprecated Code introduced by
The method Eccube\Service\OrderService::getSubTotal() has been deprecated with message: since 3.0.0, to be removed in 3.1

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

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

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
Deprecated Code introduced by
The method Eccube\Service\OrderService::getTotalTax() has been deprecated with message: since 3.0.0, to be removed in 3.1

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

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

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

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

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

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
Deprecated Code introduced by
The method Eccube\Service\OrderService::getProductTypes() has been deprecated with message: since 3.0.0, to be removed in 3.1

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

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

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
759
760
        // 配送料合計金額
761
        // TODO CalculateDeliveryFeeStrategy でセットする
762
        // $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($shippings));
763
764
        // 配送料無料条件(合計金額)
765 1
        $this->setDeliveryFreeAmount($Order);
766
767
        // 配送料無料条件(合計数量)
768 1
        $this->setDeliveryFreeQuantity($Order);
769
770
        // 合計金額の計算
771 1
        $this->calculatePrice($Order);
772
773 1
        return $Order;
774
775
    }
776
777
    /**
778
     * 配送料金の設定
779
     *
780
     * @param Shipping $Shipping
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
781
     * @param Delivery|null $Delivery
782
     */
783
    public function setShippingDeliveryFee(Shipping $Shipping, Delivery $Delivery = null)
784
    {
785
        // 配送料金の設定
786
        if (is_null($Delivery)) {
787
            $Delivery = $Shipping->getDelivery();
788
        }
789
        $deliveryFee = $this->deliveryFeeRepository->findOneBy(array('Delivery' => $Delivery, 'Pref' => $Shipping->getPref()));
790
791
        $Shipping->setDeliveryFee($deliveryFee);
792
        $Shipping->setDelivery($Delivery);
793
794
        // 商品ごとの配送料合計
795
        $productDeliveryFeeTotal = 0;
796
        if (!is_null($this->BaseInfo->getOptionProductDeliveryFee())) {
797
            $productDeliveryFeeTotal += $this->getProductDeliveryFee($Shipping);
798
        }
799
800
        $Shipping->setShippingDeliveryFee($deliveryFee->getFee() + $productDeliveryFeeTotal);
801
        $Shipping->setShippingDeliveryName($Delivery->getName());
802
    }
803
804
    /**
805
     * 配送料無料条件(合計金額)の条件を満たしていれば配送料金を0に設定
806
     *
807
     * @param Order $Order
808
     */
809 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...
810
    {
811
        // 配送料無料条件(合計金額)
812 2
        $deliveryFreeAmount = $this->BaseInfo->getDeliveryFreeAmount();
813 2
        if (!is_null($deliveryFreeAmount)) {
814
            // 合計金額が設定金額以上であれば送料無料
815 1
            if ($Order->getSubTotal() >= $deliveryFreeAmount) {
816 1
                $Order->setDeliveryFeeTotal(0);
817
                // お届け先情報の配送料も0にセット
818 1
                $shippings = $Order->getShippings();
819 1
                foreach ($shippings as $Shipping) {
820 1
                    $Shipping->setShippingDeliveryFee(0);
821
                }
822
            }
823
        }
824
    }
825
826
    /**
827
     * 配送料無料条件(合計数量)の条件を満たしていれば配送料金を0に設定
828
     *
829
     * @param Order $Order
830
     */
831 1 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...
832
    {
833
        // 配送料無料条件(合計数量)
834 1
        $deliveryFreeQuantity = $this->BaseInfo->getDeliveryFreeQuantity();
835 1
        if (!is_null($deliveryFreeQuantity)) {
836
            // 合計数量が設定数量以上であれば送料無料
837
            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...
838
                $Order->setDeliveryFeeTotal(0);
839
                // お届け先情報の配送料も0にセット
840
                $shippings = $Order->getShippings();
841
                foreach ($shippings as $Shipping) {
842
                    $Shipping->setShippingDeliveryFee(0);
843
                }
844
            }
845
        }
846
    }
847
848
849
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$em" missing
Loading history...
850
     * 商品公開ステータスチェック、在庫チェック、購入制限数チェックを行い、在庫情報をロックする
851
     *
852
     * @param $em トランザクション制御されているEntityManager
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
853
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 57 spaces after parameter type; 1 found
Loading history...
854
     * @return bool true : 成功、false : 失敗
855
     */
856 4
    public function isOrderProduct($em, \Eccube\Entity\Order $Order)
857
    {
858 4
        $orderDetails = $Order->getOrderDetails();
859
860 4
        foreach ($orderDetails as $orderDetail) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
861
862 4
            if (is_null($orderDetail->getProduct())) {
863
                // FIXME 配送明細を考慮する必要がある
864
                continue;
865
            }
866
867
            // 商品削除チェック
868 4
            if ($orderDetail->getProductClass()->getDelFlg()) {
869
                // @deprecated 3.1以降ではexceptionをthrowする
870
                // throw new ShoppingException('cart.product.delete');
871
                return false;
872
            }
873
874
            // 商品公開ステータスチェック
875 4
            if ($orderDetail->getProduct()->getStatus()->getId() != \Eccube\Entity\Master\Disp::DISPLAY_SHOW) {
876
                // 商品が非公開ならエラー
877
878
                // @deprecated 3.1以降ではexceptionをthrowする
879
                // throw new ShoppingException('cart.product.not.status');
880 1
                return false;
881
            }
882
883
            // 購入制限数チェック
884 3
            if (!is_null($orderDetail->getProductClass()->getSaleLimit())) {
885 2
                if ($orderDetail->getQuantity() > $orderDetail->getProductClass()->getSaleLimit()) {
886
                    // @deprecated 3.1以降ではexceptionをthrowする
887
                    // throw new ShoppingException('cart.over.sale_limit');
888 1
                    return false;
889
                }
890
            }
891
892
            // 購入数チェック
893 2
            if ($orderDetail->getQuantity() < 1) {
894
                // 購入数量が1未満ならエラー
895
896
                // @deprecated 3.1以降ではexceptionをthrowする
897
                // throw new ShoppingException('???');
898 2
                return false;
899
            }
900
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
901
        }
902
903
        // 在庫チェック
904 2
        foreach ($orderDetails as $orderDetail) {
905 2
            if (is_null($orderDetail->getProductClass())) {
906
                // FIXME 配送明細を考慮する必要がある
907
                continue;
908
            }
909
            // 在庫が無制限かチェックし、制限ありなら在庫数をチェック
910 2
            if ($orderDetail->getProductClass()->getStockUnlimited() == Constant::DISABLED) {
911
                // 在庫チェックあり
912
                // 在庫に対してロック(select ... for update)を実行
913 1
                $productStock = $em->getRepository('Eccube\Entity\ProductStock')->find(
914 1
                    $orderDetail->getProductClass()->getProductStock()->getId(), LockMode::PESSIMISTIC_WRITE
915
                );
916
                // 購入数量と在庫数をチェックして在庫がなければエラー
917 1
                if ($productStock->getStock() < 1) {
918
                    // @deprecated 3.1以降ではexceptionをthrowする
919
                    // throw new ShoppingException('cart.over.stock');
920
                    return false;
921 1
                } elseif ($orderDetail->getQuantity() > $productStock->getStock()) {
922
                    // @deprecated 3.1以降ではexceptionをthrowする
923
                    // throw new ShoppingException('cart.over.stock');
924 2
                    return false;
925
                }
926
            }
927
        }
928
929 1
        return true;
930
931
    }
932
933
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$data" missing
Loading history...
934
     * 受注情報、お届け先情報の更新
935
     *
936
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 22 spaces after parameter type; 1 found
Loading history...
937
     * @param $data フォームデータ
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
938
     *
939
     * @deprecated since 3.0.5, to be removed in 3.1
940
     */
941
    public function setOrderUpdate(Order $Order, $data)
942
    {
943
        // 受注情報を更新
944
        $Order->setOrderDate(new \DateTime());
945
        $Order->setOrderStatus($this->orderStatusRepository->find($this->appConfig['order_new']));
946
        $Order->setMessage($data['message']);
947
        // お届け先情報を更新
948
        $shippings = $data['shippings'];
949
        foreach ($shippings as $Shipping) {
950
            $Delivery = $Shipping->getDelivery();
951
            $deliveryFee = $this->deliveryFeeRepository->findOneBy(array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
952
                'Delivery' => $Delivery,
953
                'Pref' => $Shipping->getPref()
954
            ));
955
            $deliveryTime = $Shipping->getDeliveryTime();
956
            if (!empty($deliveryTime)) {
957
                $Shipping->setShippingDeliveryTime($deliveryTime->getDeliveryTime());
958
            }
959
            $Shipping->setDeliveryFee($deliveryFee);
960
            // 商品ごとの配送料合計
961
            $productDeliveryFeeTotal = 0;
962
            if (!is_null($this->BaseInfo->getOptionProductDeliveryFee())) {
963
                $productDeliveryFeeTotal += $this->getProductDeliveryFee($Shipping);
964
            }
965
            $Shipping->setShippingDeliveryFee($deliveryFee->getFee() + $productDeliveryFeeTotal);
966
            $Shipping->setShippingDeliveryName($Delivery->getName());
967
        }
968
        // 配送料無料条件(合計金額)
969
        $this->setDeliveryFreeAmount($Order);
970
        // 配送料無料条件(合計数量)
971
        $this->setDeliveryFreeQuantity($Order);
972
    }
973
974
975
    /**
976
     * 受注情報の更新
977
     *
978
     * @param Order $Order 受注情報
979
     */
980 1
    public function setOrderUpdateData(Order $Order)
981
    {
982
        // 受注情報を更新
983 1
        $Order->setOrderDate(new \DateTime()); // XXX 後続の setOrderStatus でも時刻を更新している
984 1
        $OrderStatus = $this->orderStatusRepository->find($this->appConfig['order_new']);
985 1
        $this->setOrderStatus($Order, $OrderStatus);
986
987
    }
988
989
990
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$em" missing
Loading history...
991
     * 在庫情報の更新
992
     *
993
     * @param $em トランザクション制御されているEntityManager
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
994
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 57 spaces after parameter type; 1 found
Loading history...
995
     */
996 1
    public function setStockUpdate($em, Order $Order)
997
    {
998
999 1
        $orderDetails = $Order->getOrderDetails();
1000
1001
        // 在庫情報更新
1002 1
        foreach ($orderDetails as $orderDetail) {
1003 1
            if (is_null($orderDetail->getProductClass())) {
1004
                // FIXME 配送明細を考慮する必要がある
1005
                continue;
1006
            }
1007
            // 在庫が無制限かチェックし、制限ありなら在庫数を更新
1008 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...
1009
1010 1
                $productStock = $em->getRepository('Eccube\Entity\ProductStock')->find(
1011 1
                    $orderDetail->getProductClass()->getProductStock()->getId()
1012
                );
1013
1014
                // 在庫情報の在庫数を更新
1015 1
                $stock = $productStock->getStock() - $orderDetail->getQuantity();
1016 1
                $productStock->setStock($stock);
1017
1018
                // 商品規格情報の在庫数を更新
1019 1
                $orderDetail->getProductClass()->setStock($stock);
1020
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
1021
            }
1022
        }
1023
1024
    }
1025
1026
1027
    /**
1028
     * 会員情報の更新
1029
     *
1030
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
1031
     * @param Customer $user ログインユーザ
0 ignored issues
show
introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
1032
     */
1033 1
    public function setCustomerUpdate(Order $Order, Customer $user)
1034
    {
1035
1036 1
        $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...
1037
1038
        // 顧客情報を更新
1039 1
        $now = new \DateTime();
1040 1
        $firstBuyDate = $user->getFirstBuyDate();
1041 1
        if (empty($firstBuyDate)) {
1042 1
            $user->setFirstBuyDate($now);
1043
        }
1044 1
        $user->setLastBuyDate($now);
1045
1046 1
        $user->setBuyTimes($user->getBuyTimes() + 1);
1047 1
        $user->setBuyTotal($user->getBuyTotal() + $Order->getTotal());
1048
1049
    }
1050
1051
1052
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$payments" missing
Loading history...
introduced by
Doc comment for parameter "$subTotal" missing
Loading history...
1053
     * 支払方法選択の表示設定
1054
     *
1055
     * @param $payments 支払選択肢情報
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
1056
     * @param $subTotal 小計
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
1057
     * @return array
1058
     */
1059 1
    public function getPayments($payments, $subTotal)
1060
    {
1061 1
        $pays = array();
1062 1
        foreach ($payments as $payment) {
1063
            // 支払方法の制限値内であれば表示
1064 1
            if (!is_null($payment)) {
1065 1
                $pay = $this->paymentRepository->find($payment['id']);
1066 1
                if (intval($pay->getRuleMin()) <= $subTotal) {
1067 1
                    if (is_null($pay->getRuleMax()) || $pay->getRuleMax() >= $subTotal) {
1068 1
                        $pays[] = $pay;
1069
                    }
1070
                }
1071
            }
1072
        }
1073
1074 1
        return $pays;
1075
1076
    }
1077
1078
    /**
1079
     * お届け日を取得
1080
     *
1081
     * @param Order $Order
1082
     * @return array
1083
     */
1084 2
    public function getFormDeliveryDates(Order $Order)
1085
    {
1086
1087
        // お届け日の設定
1088 2
        $minDate = 0;
1089 2
        $deliveryDateFlag = false;
1090
1091
        // 配送時に最大となる商品日数を取得
1092 2
        foreach ($Order->getOrderDetails() as $detail) {
1093 2
            $deliveryDate = $detail->getProductClass()->getDeliveryDate();
1094 2
            if (!is_null($deliveryDate)) {
1095 2
                if ($deliveryDate->getValue() < 0) {
1096
                    // 配送日数がマイナスの場合はお取り寄せなのでスキップする
1097 1
                    $deliveryDateFlag = false;
1098 1
                    break;
1099
                }
1100
1101 1
                if ($minDate < $deliveryDate->getValue()) {
1102
                    $minDate = $deliveryDate->getValue();
1103
                }
1104
                // 配送日数が設定されている
1105 1
                $deliveryDateFlag = true;
1106
            }
1107
        }
1108
1109
        // 配達最大日数期間を設定
1110 2
        $deliveryDates = array();
1111
1112
        // 配送日数が設定されている
1113 2 View Code Duplication
        if ($deliveryDateFlag) {
1114 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...
1115 1
                new \DateTime($minDate.' day'),
1116 1
                new \DateInterval('P1D'),
1117 1
                new \DateTime($minDate + $this->appConfig['deliv_date_end_max'].' day')
1118
            );
1119
1120 1
            foreach ($period as $day) {
1121 1
                $deliveryDates[$day->format('Y/m/d')] = $day->format('Y/m/d');
1122
            }
1123
        }
1124
1125 2
        return $deliveryDates;
1126
1127
    }
1128
1129
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$deliveries" missing
Loading history...
1130
     * 支払方法を取得
1131
     *
1132
     * @param $deliveries
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
1133
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
1134
     * @return array
1135
     */
1136
    public function getFormPayments($deliveries, Order $Order)
1137
    {
1138
1139
        $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...
1140
        if ($this->BaseInfo->getOptionMultipleShipping() == Constant::ENABLED && count($productTypes) > 1) {
1141
            // 複数配送時の支払方法
1142
1143
            $payments = $this->paymentRepository->findAllowedPayments($deliveries);
1144
        } else {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
1145
1146
            // 配送業者をセット
1147
            $shippings = $Order->getShippings();
1148
            $Shipping = $shippings[0];
1149
            $payments = $this->paymentRepository->findPayments($Shipping->getDelivery(), true);
1150
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
1151
        }
1152
        $payments = $this->getPayments($payments, $Order->getSubTotal());
1153
1154
        return $payments;
1155
1156
    }
1157
1158
    /**
1159
     * お届け先ごとにFormを作成
1160
     *
1161
     * @param Order $Order
1162
     * @return \Symfony\Component\Form\Form
1163
     * @deprecated since 3.0, to be removed in 3.1
1164
     */
1165 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...
1166
    {
1167
        $message = $Order->getMessage();
1168
1169
        $deliveries = $this->getDeliveriesOrder($Order);
1170
1171
        // 配送業者の支払方法を取得
1172
        $payments = $this->getFormPayments($deliveries, $Order);
1173
1174
        $builder = $this->formFactory->createBuilder('shopping', null, array(
1175
            'payments' => $payments,
1176
            'payment' => $Order->getPayment(),
1177
            'message' => $message,
1178
        ));
1179
1180
        $builder
1181
            ->add('shippings', CollectionType::class, array(
1182
                'entry_type' => ShippingItemType::class,
1183
                'data' => $Order->getShippings(),
1184
            ));
1185
1186
        $form = $builder->getForm();
1187
1188
        return $form;
1189
1190
    }
1191
1192
    /**
1193
     * お届け先ごとにFormBuilderを作成
1194
     *
1195
     * @param Order $Order
1196
     * @return \Symfony\Component\Form\FormBuilderInterface
1197
     *
1198
     * @deprecated 利用している箇所なし
1199
     */
1200 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...
1201
    {
1202
        $message = $Order->getMessage();
1203
1204
        $deliveries = $this->getDeliveriesOrder($Order);
1205
1206
        // 配送業者の支払方法を取得
1207
        $payments = $this->getFormPayments($deliveries, $Order);
1208
1209
        $builder = $this->formFactory->createBuilder('shopping', null, array(
1210
            'payments' => $payments,
1211
            'payment' => $Order->getPayment(),
1212
            'message' => $message,
1213
        ));
1214
1215
        $builder
1216
            ->add('shippings', CollectionType::class, array(
1217
                'entry_type' => ShippingItemType::class,
1218
                'data' => $Order->getShippings(),
1219
            ));
1220
1221
        return $builder;
1222
1223
    }
1224
1225
1226
    /**
1227
     * フォームデータを更新
1228
     *
1229
     * @param Order $Order
1230
     * @param array $data
1231
     *
1232
     * @deprecated
1233
     */
1234 1
    public function setFormData(Order $Order, array $data)
1235
    {
1236
1237
        // お問い合わせ
1238 1
        $Order->setMessage($data['message']);
1239
1240
        // お届け先情報を更新
1241 1
        $shippings = $data['shippings'];
1242 1
        foreach ($shippings as $Shipping) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
1243
1244 1
            $deliveryTime = $Shipping->getDeliveryTime();
1245 1
            if (!empty($deliveryTime)) {
1246 1
                $Shipping->setShippingDeliveryTime($deliveryTime->getDeliveryTime());
1247
            }
1248
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
1249
        }
1250
1251
    }
1252
1253
    /**
1254
     * 配送料の合計金額を計算
1255
     *
1256
     * @param Order $Order
1257
     * @return Order
1258
     */
1259
    public function calculateDeliveryFee(Order $Order)
1260
    {
1261
1262
        // 配送業者を取得
1263
        $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...
1264
1265
        // 配送料合計金額
1266
        // TODO CalculateDeliveryFeeStrategy でセットする
1267
        // $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($shippings));
1268
1269
        // 配送料無料条件(合計金額)
1270
        $this->setDeliveryFreeAmount($Order);
1271
1272
        // 配送料無料条件(合計数量)
1273
        $this->setDeliveryFreeQuantity($Order);
1274
1275
        return $Order;
1276
1277
    }
1278
1279
1280
    /**
1281
     * 購入処理を行う
1282
     *
1283
     * @param Order $Order
1284
     * @throws ShoppingException
1285
     */
1286
    public function processPurchase(Order $Order)
1287
    {
1288
1289
        $em = $this->entityManager;
1290
1291
        // TODO PurchaseFlowでやる
1292
//        // 合計金額の再計算
1293
//        $this->calculatePrice($Order);
1294
//
1295
//        // 商品公開ステータスチェック、商品制限数チェック、在庫チェック
1296
//        $check = $this->isOrderProduct($em, $Order);
1297
//        if (!$check) {
1298
//            throw new ShoppingException('front.shopping.stock.error');
1299
//        }
1300
1301
        // 受注情報、配送情報を更新
1302
        $Order = $this->calculateDeliveryFee($Order);
1303
        $this->setOrderUpdateData($Order);
1304
        // 在庫情報を更新
1305
        $this->setStockUpdate($em, $Order);
1306
1307
        if ($this->app->isGranted('ROLE_USER')) {
1308
            // 会員の場合、購入金額を更新
1309
            $this->setCustomerUpdate($Order, $this->app->user());
1310
        }
1311
1312
    }
1313
1314
1315
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$discount" missing
Loading history...
1316
     * 値引き可能かチェック
1317
     *
1318
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
1319
     * @param       $discount
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
1320
     * @return bool
1321
     */
1322
    public function isDiscount(Order $Order, $discount)
1323
    {
1324
1325
        if ($Order->getTotal() < $discount) {
1326
            return false;
1327
        }
1328
1329
        return true;
1330
    }
1331
1332
1333
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$discount" missing
Loading history...
1334
     * 値引き金額をセット
1335
     *
1336
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
1337
     * @param $discount
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
1338
     */
1339
    public function setDiscount(Order $Order, $discount)
1340
    {
1341
1342
        $Order->setDiscount($Order->getDiscount() + $discount);
1343
1344
    }
1345
1346
1347
    /**
1348
     * 合計金額を計算
1349
     *
1350
     * @param Order $Order
1351
     * @return Order
1352
     */
1353 1
    public function calculatePrice(Order $Order)
1354
    {
1355
1356 1
        $total = $Order->getTotalPrice();
1357
1358 1
        if ($total < 0) {
1359
            // 合計金額がマイナスの場合、0を設定し、discountは値引きされた額のみセット
1360
            $total = 0;
1361
        }
1362
1363 1
        $Order->setTotal($total);
1364 1
        $Order->setPaymentTotal($total);
1365
1366 1
        return $Order;
1367
1368
    }
1369
1370
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$status" missing
Loading history...
1371
     * 受注ステータスをセット
1372
     *
1373
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
1374
     * @param $status
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
1375
     * @return Order
1376
     */
1377 1
    public function setOrderStatus(Order $Order, $status)
1378
    {
1379
1380 1
        $Order->setOrderDate(new \DateTime());
1381 1
        $Order->setOrderStatus($this->orderStatusRepository->find($status));
1382
1383 1
        $event = new EventArgs(
1384
            array(
1385 1
                'Order' => $Order,
1386
            ),
1387 1
            null
1388
        );
1389 1
        $this->eventDispatcher->dispatch(EccubeEvents::SERVICE_SHOPPING_ORDER_STATUS, $event);
1390
1391 1
        return $Order;
1392
1393
    }
1394
1395
    /**
1396
     * 受注メール送信を行う
1397
     *
1398
     * @param Order $Order
1399
     * @return MailHistory
1400
     */
1401
    public function sendOrderMail(Order $Order)
1402
    {
1403
1404
        // メール送信
1405
        $message = $this->mailService->sendOrderMail($Order);
1406
1407
        // 送信履歴を保存.
1408
        $MailTemplate = $this->mailTemplateRepository->find(1);
1409
1410
        $MailHistory = new MailHistory();
1411
        $MailHistory
1412
            ->setSubject($message->getSubject())
1413
            ->setMailBody($message->getBody())
1414
            ->setMailTemplate($MailTemplate)
1415
            ->setSendDate(new \DateTime())
1416
            ->setOrder($Order);
1417
1418
        $this->entityManager->persist($MailHistory);
1419
        $this->entityManager->flush($MailHistory);
1420
1421
        return $MailHistory;
1422
1423
    }
1424
1425
1426
    /**
1427
     * 受注処理完了通知
1428
     *
1429
     * @param Order $Order
1430
     */
1431
    public function notifyComplete(Order $Order)
1432
    {
1433
1434
        $event = new EventArgs(
1435
            array(
1436
                'Order' => $Order,
1437
            ),
1438
            null
1439
        );
1440
        $this->eventDispatcher->dispatch(EccubeEvents::SERVICE_SHOPPING_NOTIFY_COMPLETE, $event);
1441
1442
    }
1443
1444
1445
}
1446