Completed
Pull Request — master (#1261)
by Kentaro
33:04 queued 10s
created

ShoppingService::getAmount()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 24
ccs 3
cts 3
cp 1
rs 8.9714
cc 1
eloc 10
nc 1
nop 1
crap 1
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
namespace Eccube\Service;
25
26
use Doctrine\DBAL\LockMode;
27
use Eccube\Application;
28
use Eccube\Common\Constant;
29
use Eccube\Entity\Customer;
30
use Eccube\Entity\Delivery;
31
use Eccube\Entity\Order;
32
use Eccube\Entity\OrderDetail;
33
use Eccube\Entity\Product;
34
use Eccube\Entity\ProductClass;
35
use Eccube\Entity\ShipmentItem;
36
use Eccube\Entity\Shipping;
37
use Eccube\Util\Str;
38
39
class ShoppingService
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
40
{
41
    /** @var \Eccube\Application */
42
    public $app;
43
44
    /** @var \Eccube\Service\CartService */
45
    protected $cartService;
46
47
    /** @var \Eccube\Service\OrderService */
48
    protected $orderService;
49
50
    /** @var \Eccube\Entity\BaseInfo */
51
    protected $BaseInfo;
52
53
    /** @var  \Doctrine\ORM\EntityManager */
54
    protected $em;
55
56 29
    public function __construct(Application $app, $cartService, $orderService)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
57
    {
58 29
        $this->app = $app;
59 29
        $this->cartService = $cartService;
60 29
        $this->orderService = $orderService;
61
        $this->BaseInfo = $app['eccube.repository.base_info']->get();
62
    }
63
64
    /**
65
     * セッションにセットされた受注情報を取得
66
     *
67
     * @param null $status
68
     * @return null|object
69
     */
70 6
    public function getOrder($status = null)
71
    {
72
73
        // 受注データを取得
74
        $preOrderId = $this->cartService->getPreOrderId();
75
76
        $condition = array(
77
            'pre_order_id' => $preOrderId,
78 6
        );
79
80
        if (!is_null($status)) {
81
            $condition += array(
82
                'OrderStatus' => $status,
83 3
            );
84
        }
85
86
        $Order = $this->app['eccube.repository.order']->findOneBy($condition);
87
88 6
        return $Order;
89
90 3
    }
91
92
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$sesisonKey" missing
Loading history...
93
     * 非会員情報を取得
94
     *
95
     * @param $sesisonKey
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
96
     * @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...
97
     */
98 3
    public function getNonMember($sesisonKey)
99
    {
100
101
        // 非会員でも一度会員登録されていればショッピング画面へ遷移
102
        $nonMember = $this->app['session']->get($sesisonKey);
103
        if (is_null($nonMember)) {
104 1
            return null;
105
        }
106
107 3
        $Customer = $nonMember['customer'];
108
        $Customer->setPref($this->app['eccube.repository.master.pref']->find($nonMember['pref']));
109
110 3
        return $Customer;
111
112
    }
113
114
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
115
     * 受注情報を作成
116
     *
117
     * @param $Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
118
     * @return \Eccube\Entity\Order
119
     */
120 7
    public function createOrder($Customer)
121
    {
122
        // ランダムなpre_order_idを作成
123
        $preOrderId = sha1(Str::random(32));
124
125
        // 受注情報、受注明細情報、お届け先情報、配送商品情報を作成
126 7
        $Order = $this->registerPreOrder(
127
            $Customer,
128
            $preOrderId);
129
130
        $this->cartService->setPreOrderId($preOrderId);
131
        $this->cartService->save();
132
133 7
        return $Order;
134
    }
135
136
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
introduced by
Doc comment for parameter "$preOrderId" missing
Loading history...
137
     * 仮受注情報作成
138
     *
139
     * @param $Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
140
     * @param $preOrderId
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
141
     * @return mixed
142
     * @throws \Doctrine\ORM\NoResultException
143
     * @throws \Doctrine\ORM\NonUniqueResultException
144
     */
145 7
    public function registerPreOrder(Customer $Customer, $preOrderId)
146
    {
147
148
        $this->em = $this->app['orm.em'];
149
150
        // 受注情報を作成
151
        $Order = $this->getNewOrder($Customer);
152
        $Order->setPreOrderId($preOrderId);
153
154
        $this->em->persist($Order);
155
156
        // 配送業者情報を取得
157
        $deliveries = $this->getDeliveriesCart();
158
159
        // お届け先情報を作成
160
        $Order = $this->getNewShipping($Order, $Customer, $deliveries);
161
162
        // 受注明細情報、配送商品情報を作成
163
        $Order = $this->getNewDetails($Order);
164
165
        // 小計
166
        $subTotal = $this->orderService->getSubTotal($Order);
0 ignored issues
show
Deprecated Code introduced by
The method Eccube\Service\OrderService::getSubTotal() has been deprecated with message: since 3.0.0, to be removed in 3.1

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

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

Loading history...
167
168
        // 消費税のみの小計
169
        $tax = $this->orderService->getTotalTax($Order);
0 ignored issues
show
Deprecated Code introduced by
The method Eccube\Service\OrderService::getTotalTax() has been deprecated with message: since 3.0.0, to be removed in 3.1

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

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

Loading history...
170
171
        // 配送料合計金額
172
        $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($Order->getShippings()));
173
174
        // 小計
175
        $Order->setSubTotal($subTotal);
176
177
        // 配送料無料条件(合計金額)
178
        $this->setDeliveryFreeAmount($Order);
179
180
        // 配送料無料条件(合計数量)
181
        $this->setDeliveryFreeQuantity($Order);
182
183
        // 初期選択の支払い方法をセット
184
        $payments = $this->app['eccube.repository.payment']->findAllowedPayments($deliveries);
185
        $payments = $this->getPayments($payments, $subTotal);
186
187
        if (count($payments) > 0) {
188 7
            $payment = $payments[0];
189
            $Order->setPayment($payment);
190
            $Order->setPaymentMethod($payment->getMethod());
191
            $Order->setCharge($payment->getCharge());
192
        } else {
193
            $Order->setCharge(0);
194 7
        }
195
196
        $Order->setTax($tax);
197
198
        $total = $subTotal + $Order->getCharge() + $Order->getDeliveryFeeTotal();
199
200
        $Order->setTotal($total);
201
        $Order->setPaymentTotal($total);
202
203
        $this->em->flush();
204
205 7
        return $Order;
206
207 7
    }
208
209
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
210
     * 受注情報を作成
211
     * @param $Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
212
     * @return \Eccube\Entity\Order
213
     */
214 7
    public function getNewOrder(Customer $Customer)
215
    {
216
        $Order = $this->newOrder();
217
        $this->copyToOrderFromCustomer($Order, $Customer);
218
219 7
        return $Order;
220 7
    }
221
222
223
    /**
224
     * 受注情報を作成
225
     * @return \Eccube\Entity\Order
226
     */
227 7
    public function newOrder()
228
    {
229
        $OrderStatus = $this->app['eccube.repository.order_status']->find($this->app['config']['order_processing']);
230
        $Order = new \Eccube\Entity\Order($OrderStatus);
231 7
        return $Order;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
232
    }
233
234
    /**
235
     * 受注情報を作成
236
     *
237
     * @param \Eccube\Entity\Order $Order
0 ignored issues
show
introduced by
Expected 9 spaces after parameter type; 1 found
Loading history...
238
     * @param \Eccube\Entity\Customer|null $Customer
239
     * @return \Eccube\Entity\Order
240
     */
241 7
    public function copyToOrderFromCustomer(Order $Order, Customer $Customer = null)
242
    {
243
        if (is_null($Customer)) {
244
            return $Order;
245
        }
246
247
        if ($Customer->getId()) {
248
            $Order->setCustomer($Customer);
249
        }
250
        $Order
251
            ->setName01($Customer->getName01())
252
            ->setName02($Customer->getName02())
253
            ->setKana01($Customer->getKana01())
254
            ->setKana02($Customer->getKana02())
255
            ->setCompanyName($Customer->getCompanyName())
256
            ->setEmail($Customer->getEmail())
257
            ->setTel01($Customer->getTel01())
258
            ->setTel02($Customer->getTel02())
259
            ->setTel03($Customer->getTel03())
260
            ->setFax01($Customer->getFax01())
261
            ->setFax02($Customer->getFax02())
262
            ->setFax03($Customer->getFax03())
263
            ->setZip01($Customer->getZip01())
264
            ->setZip02($Customer->getZip02())
265
            ->setZipCode($Customer->getZip01() . $Customer->getZip02())
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
266
            ->setPref($Customer->getPref())
267
            ->setAddr01($Customer->getAddr01())
268
            ->setAddr02($Customer->getAddr02())
269
            ->setSex($Customer->getSex())
270
            ->setBirth($Customer->getBirth())
271
            ->setJob($Customer->getJob());
272
273 7
        return $Order;
274 7
    }
275
276
277
    /**
278
     * 配送業者情報を取得
279
     *
280
     * @return array
281
     */
282
    public function getDeliveriesCart()
283
    {
284
285
        // カートに保持されている商品種別を取得
286
        $productTypes = $this->cartService->getProductTypes();
287
288
        return $this->getDeliveries($productTypes);
289
290
    }
291
292
    /**
293
     * 配送業者情報を取得
294
     *
295
     * @param Order $Order
296
     * @return array
297
     */
298
    public function getDeliveriesOrder(Order $Order)
299
    {
300
301
        // 受注情報から商品種別を取得
302
        $productTypes = $this->orderService->getProductTypes($Order);
0 ignored issues
show
Deprecated Code introduced by
The method Eccube\Service\OrderService::getProductTypes() has been deprecated with message: since 3.0.0, to be removed in 3.1

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

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

Loading history...
303
304
        return $this->getDeliveries($productTypes);
305
306
    }
307
308
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$productTypes" missing
Loading history...
309
     * 配送業者情報を取得
310
     *
311
     * @param $productTypes
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
312
     * @return array
313
     */
314 10
    public function getDeliveries($productTypes)
315
    {
316
317
        // 商品種別に紐づく配送業者を取得
318
        $deliveries = $this->app['eccube.repository.delivery']->getDeliveries($productTypes);
319
320
        if ($this->BaseInfo->getOptionMultipleShipping() == Constant::ENABLED) {
321
            // 複数配送対応
322
323
            // 支払方法を取得
324
            $payments = $this->app['eccube.repository.payment']->findAllowedPayments($deliveries);
325
326
            if (count($productTypes) > 1) {
327
                // 商品種別が複数ある場合、配送対象となる配送業者を取得
328
                $deliveries = $this->app['eccube.repository.delivery']->findAllowedDeliveries($productTypes, $payments);
329
            }
330
331
        }
332
333 10
        return $deliveries;
334
335
    }
336
337
338
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$deliveries" missing
Loading history...
339
     * お届け先情報を作成
340
     *
341
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
342
     * @param Customer $Customer
0 ignored issues
show
introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
343
     * @param $deliveries
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
344
     * @return Order
345
     */
346 7
    public function getNewShipping(Order $Order, Customer $Customer, $deliveries)
347
    {
348 7
        $productTypes = array();
349
        foreach ($deliveries as $Delivery) {
350
            if (!in_array($Delivery->getProductType()->getId(), $productTypes)) {
351
                $Shipping = new Shipping();
352
353 7
                $this->copyToShippingFromCustomer($Shipping, $Customer)
354 7
                    ->setOrder($Order)
355
                    ->setDelFlg(Constant::DISABLED);
356
357
                // 配送料金の設定
358
                $this->setShippingDeliveryFee($Shipping, $Delivery);
359
360
                $this->em->persist($Shipping);
361
362
                $Order->addShipping($Shipping);
363
364
                $productTypes[] = $Delivery->getProductType()->getId();
365
            }
366
        }
367
368 7
        return $Order;
369 7
    }
370
371
    /**
372
     * お届け先情報を作成
373
     *
374
     * @param \Eccube\Entity\Shipping $Shipping
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
375
     * @param \Eccube\Entity\Customer|null $Customer
376
     * @return \Eccube\Entity\Shipping
377
     */
378 8
    public function copyToShippingFromCustomer(Shipping $Shipping, Customer $Customer = null)
379
    {
380
        if (is_null($Customer)) {
381 1
            return $Shipping;
382
        }
383
384 7
        $CustomerAddress = $this->app['eccube.repository.customer_address']->findOneBy(
385 7
            array('Customer' => $Customer),
386 7
            array('id' => 'ASC')
387
        );
388
389
        if (!is_null($CustomerAddress)) {
390
            $Shipping
391
                ->setName01($CustomerAddress->getName01())
392
                ->setName02($CustomerAddress->getName02())
393
                ->setKana01($CustomerAddress->getKana01())
394
                ->setKana02($CustomerAddress->getKana02())
395
                ->setCompanyName($CustomerAddress->getCompanyName())
396
                ->setTel01($CustomerAddress->getTel01())
397
                ->setTel02($CustomerAddress->getTel02())
398
                ->setTel03($CustomerAddress->getTel03())
399
                ->setFax01($CustomerAddress->getFax01())
400
                ->setFax02($CustomerAddress->getFax02())
401
                ->setFax03($CustomerAddress->getFax03())
402
                ->setZip01($CustomerAddress->getZip01())
403
                ->setZip02($CustomerAddress->getZip02())
404
                ->setZipCode($CustomerAddress->getZip01() . $CustomerAddress->getZip02())
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
405
                ->setPref($CustomerAddress->getPref())
406
                ->setAddr01($CustomerAddress->getAddr01())
407
                ->setAddr02($CustomerAddress->getAddr02());
408
        } else {
409
            $Shipping
410
                ->setName01($Customer->getName01())
411
                ->setName02($Customer->getName02())
412
                ->setKana01($Customer->getKana01())
413
                ->setKana02($Customer->getKana02())
414
                ->setCompanyName($Customer->getCompanyName())
415
                ->setTel01($Customer->getTel01())
416
                ->setTel02($Customer->getTel02())
417
                ->setTel03($Customer->getTel03())
418
                ->setFax01($Customer->getFax01())
419
                ->setFax02($Customer->getFax02())
420
                ->setFax03($Customer->getFax03())
421
                ->setZip01($Customer->getZip01())
422
                ->setZip02($Customer->getZip02())
423
                ->setZipCode($Customer->getZip01() . $Customer->getZip02())
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
424
                ->setPref($Customer->getPref())
425
                ->setAddr01($Customer->getAddr01())
426
                ->setAddr02($Customer->getAddr02());
427 5
        }
428
429 7
        return $Shipping;
430 8
    }
431
432
433
    /**
434
     * 受注明細情報、配送商品情報を作成
435
     *
436
     * @param Order $Order
437
     * @return Order
438
     */
439 7
    public function getNewDetails(Order $Order)
440
    {
441
442
        // 受注詳細, 配送商品
443
        foreach ($this->cartService->getCart()->getCartItems() as $item) {
444
            /* @var $ProductClass \Eccube\Entity\ProductClass */
445
            $ProductClass = $item->getObject();
446
            /* @var $Product \Eccube\Entity\Product */
447
            $Product = $ProductClass->getProduct();
448
449
            $quantity = $item->getQuantity();
450
451
            // 受注明細情報を作成
452
            $OrderDetail = $this->getNewOrderDetail($Product, $ProductClass, $quantity);
453
            $OrderDetail->setOrder($Order);
454
            $Order->addOrderDetail($OrderDetail);
455
456
            // 配送商品情報を作成
457
            $this->getNewShipmentItem($Order, $Product, $ProductClass, $quantity);
458
        }
459
460 7
        return $Order;
461
462 7
    }
463
464
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$quantity" missing
Loading history...
465
     * 受注明細情報を作成
466
     *
467
     * @param Product $Product
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
468
     * @param ProductClass $ProductClass
469
     * @param $quantity
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
470
     * @return \Eccube\Entity\OrderDetail
471
     */
472 7
    public function getNewOrderDetail(Product $Product, ProductClass $ProductClass, $quantity)
473
    {
474
        $OrderDetail = new OrderDetail();
475
        $TaxRule = $this->app['eccube.repository.tax_rule']->getByRule($Product, $ProductClass);
476 7
        $OrderDetail->setProduct($Product)
477 7
            ->setProductClass($ProductClass)
478
            ->setProductName($Product->getName())
479
            ->setProductCode($ProductClass->getCode())
480
            ->setPrice($ProductClass->getPrice02())
481 7
            ->setQuantity($quantity)
482
            ->setTaxRule($TaxRule->getCalcRule()->getId())
483
            ->setTaxRate($TaxRule->getTaxRate());
484
485
        $ClassCategory1 = $ProductClass->getClassCategory1();
486
        if (!is_null($ClassCategory1)) {
487
            $OrderDetail->setClasscategoryName1($ClassCategory1->getName());
488
            $OrderDetail->setClassName1($ClassCategory1->getClassName()->getName());
489
        }
490
        $ClassCategory2 = $ProductClass->getClassCategory2();
491
        if (!is_null($ClassCategory2)) {
492
            $OrderDetail->setClasscategoryName2($ClassCategory2->getName());
493
            $OrderDetail->setClassName2($ClassCategory2->getClassName()->getName());
494
        }
495
496
        $this->em->persist($OrderDetail);
497
498 7
        return $OrderDetail;
499 7
    }
500
501
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$quantity" missing
Loading history...
502
     * 配送商品情報を作成
503
     *
504
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
505
     * @param Product $Product
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
506
     * @param ProductClass $ProductClass
507
     * @param $quantity
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
508
     * @return \Eccube\Entity\ShipmentItem
509
     */
510 7
    public function getNewShipmentItem(Order $Order, Product $Product, ProductClass $ProductClass, $quantity)
511
    {
512
513
        $ShipmentItem = new ShipmentItem();
514
        $shippings = $Order->getShippings();
515
516
        // 選択された商品がどのお届け先情報と関連するかチェック
517 7
        $Shipping = null;
518
        foreach ($shippings as $s) {
519
            if ($s->getDelivery()->getProductType()->getId() == $ProductClass->getProductType()->getId()) {
520
                // 商品種別が同一のお届け先情報と関連させる
521 7
                $Shipping = $s;
522 7
                break;
523
            }
524 7
        }
525
526
        // 商品ごとの配送料合計
527 7
        $productDeliveryFeeTotal = 0;
528
        if (!is_null($this->BaseInfo->getOptionProductDeliveryFee())) {
529
            $productDeliveryFeeTotal = $ProductClass->getDeliveryFee() * $quantity;
530
        }
531
532
        $Shipping->setShippingDeliveryFee($Shipping->getShippingDeliveryFee() + $productDeliveryFeeTotal);
533
534 7
        $ShipmentItem->setShipping($Shipping)
535 7
            ->setOrder($Order)
536 7
            ->setProductClass($ProductClass)
537 7
            ->setProduct($Product)
538
            ->setProductName($Product->getName())
539
            ->setProductCode($ProductClass->getCode())
540
            ->setPrice($ProductClass->getPrice02())
541
            ->setQuantity($quantity);
542
543
        $ClassCategory1 = $ProductClass->getClassCategory1();
544
        if (!is_null($ClassCategory1)) {
545
            $ShipmentItem->setClasscategoryName1($ClassCategory1->getName());
546
            $ShipmentItem->setClassName1($ClassCategory1->getClassName()->getName());
547
        }
548
        $ClassCategory2 = $ProductClass->getClassCategory2();
549
        if (!is_null($ClassCategory2)) {
550
            $ShipmentItem->setClasscategoryName2($ClassCategory2->getName());
551
            $ShipmentItem->setClassName2($ClassCategory2->getClassName()->getName());
552
        }
553
        $Shipping->addShipmentItem($ShipmentItem);
554
        $this->em->persist($ShipmentItem);
555
556 7
        return $ShipmentItem;
557
558 7
    }
559
560
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$shippings" missing
Loading history...
561
     * お届け先ごとの送料合計を取得
562
     *
563
     * @param $shippings
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
564
     * @return int
565
     */
566 8
    public function getShippingDeliveryFeeTotal($shippings)
567
    {
568 8
        $deliveryFeeTotal = 0;
569
        foreach ($shippings as $Shipping) {
570
            $deliveryFeeTotal += $Shipping->getShippingDeliveryFee();
571
        }
572
573 8
        return $deliveryFeeTotal;
574
575
    }
576
577
    /**
578
     * 商品ごとの配送料を取得
579
     *
580
     * @param Shipping $Shipping
581
     * @return int
582
     */
583 8
    public function getProductDeliveryFee(Shipping $Shipping)
584
    {
585 8
        $productDeliveryFeeTotal = 0;
586
        $shipmentItems = $Shipping->getShipmentItems();
587
        foreach ($shipmentItems as $ShipmentItem) {
588
            $productDeliveryFeeTotal += $ShipmentItem->getProductClass()->getDeliveryFee() * $ShipmentItem->getQuantity();
589 7
        }
590 8
        return $productDeliveryFeeTotal;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
591 8
    }
592
593
    /**
594
     * 住所などの情報が変更された時に金額の再計算を行う
595
     *
596
     * @param Order $Order
597
     * @return Order
598
     */
599 1
    public function getAmount(Order $Order)
600
    {
601
602
        // 初期選択の配送業者をセット
603
        $shippings = $Order->getShippings();
604
605
        // 配送料合計金額
606
        $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($shippings));
607
608
        // 配送料無料条件(合計金額)
609
        $this->setDeliveryFreeAmount($Order);
610
611
        // 配送料無料条件(合計数量)
612
        $this->setDeliveryFreeQuantity($Order);
613
614
        $total = $Order->getSubTotal() + $Order->getCharge() + $Order->getDeliveryFeeTotal();
615
616
        $Order->setTotal($total);
617
        $Order->setPaymentTotal($total);
618
        $this->app['orm.em']->flush();
619
620 1
        return $Order;
621
622 1
    }
623
624
    /**
625
     * 配送料金の設定
626
     *
627
     * @param Shipping $Shipping
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
628
     * @param Delivery|null $Delivery
629
     */
630 7
    public function setShippingDeliveryFee(Shipping $Shipping, Delivery $Delivery = null)
631
    {
632
        // 配送料金の設定
633
        if (is_null($Delivery)) {
634
            $Delivery = $Shipping->getDelivery();
635
        }
636
        $deliveryFee = $this->app['eccube.repository.delivery_fee']->findOneBy(array('Delivery' => $Delivery, 'Pref' => $Shipping->getPref()));
637
638
        $Shipping->setDeliveryFee($deliveryFee);
639
        $Shipping->setDelivery($Delivery);
640
641
        // 商品ごとの配送料合計
642 7
        $productDeliveryFeeTotal = 0;
643
        if (!is_null($this->BaseInfo->getOptionProductDeliveryFee())) {
644
            $productDeliveryFeeTotal += $this->getProductDeliveryFee($Shipping);
645
        }
646
647
        $Shipping->setShippingDeliveryFee($deliveryFee->getFee() + $productDeliveryFeeTotal);
648
        $Shipping->setShippingDeliveryName($Delivery->getName());
649
650
    }
651
652
    /**
653
     * 配送料無料条件(合計金額)の条件を満たしていれば配送料金を0に設定
654
     *
655
     * @param Order $Order
656
     */
657 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...
658
    {
659
        // 配送料無料条件(合計金額)
660
        $deliveryFreeAmount = $this->BaseInfo->getDeliveryFreeAmount();
661
        if (!is_null($deliveryFreeAmount)) {
662
            // 合計金額が設定金額以上であれば送料無料
663
            if ($Order->getSubTotal() >= $deliveryFreeAmount) {
664
                $Order->setDeliveryFeeTotal(0);
665
                // お届け先情報の配送料も0にセット
666
                $shippings = $Order->getShippings();
667
                foreach ($shippings as $Shipping) {
668
                    $Shipping->setShippingDeliveryFee(0);
669
                }
670
            }
671
        }
672
    }
673
674
    /**
675
     * 配送料無料条件(合計数量)の条件を満たしていれば配送料金を0に設定
676
     *
677
     * @param Order $Order
678
     */
679 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...
680
    {
681
        // 配送料無料条件(合計数量)
682
        $deliveryFreeQuantity = $this->BaseInfo->getDeliveryFreeQuantity();
683
        if (!is_null($deliveryFreeQuantity)) {
684
            // 合計数量が設定数量以上であれば送料無料
685
            if ($this->orderService->getTotalQuantity($Order) >= $deliveryFreeQuantity) {
0 ignored issues
show
Deprecated Code introduced by
The method Eccube\Service\OrderService::getTotalQuantity() has been deprecated with message: since 3.0.0, to be removed in 3.1

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

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

Loading history...
686
                $Order->setDeliveryFeeTotal(0);
687
                // お届け先情報の配送料も0にセット
688
                $shippings = $Order->getShippings();
689
                foreach ($shippings as $Shipping) {
690
                    $Shipping->setShippingDeliveryFee(0);
691
                }
692
            }
693
        }
694
    }
695
696
697
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$em" missing
Loading history...
698
     * 商品公開ステータスチェック、在庫チェック、購入制限数チェックを行い、在庫情報をロックする
699
     *
700
     * @param $em トランザクション制御されているEntityManager
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
701
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 57 spaces after parameter type; 1 found
Loading history...
702
     * @return bool true : 成功、false : 失敗
703
     */
704 5
    public function isOrderProduct($em, \Eccube\Entity\Order $Order)
705
    {
706
        // 商品公開ステータスチェック
707
        $orderDetails = $Order->getOrderDetails();
708
709
        foreach ($orderDetails as $orderDetail) {
710
            if ($orderDetail->getProduct()->getStatus()->getId() != \Eccube\Entity\Master\Disp::DISPLAY_SHOW) {
711
                // 商品が非公開ならエラー
712 1
                return false;
713
            }
714
715
            // 購入制限数チェック
716
            if (!is_null($orderDetail->getProductClass()->getSaleLimit())) {
717
                if ($orderDetail->getQuantity() > $orderDetail->getProductClass()->getSaleLimit()) {
718 1
                    return false;
719
                }
720
            }
721
722 2
        }
723
724
        // 在庫チェック
725
        foreach ($orderDetails as $orderDetail) {
726
            // 在庫が無制限かチェックし、制限ありなら在庫数をチェック
727
            if ($orderDetail->getProductClass()->getStockUnlimited() == Constant::DISABLED) {
728
                // 在庫チェックあり
729
                // 在庫に対してロック(select ... for update)を実行
730 1
                $productStock = $em->getRepository('Eccube\Entity\ProductStock')->find(
731
                    $orderDetail->getProductClass()->getProductStock()->getId(), LockMode::PESSIMISTIC_WRITE
732
                );
733
                // 購入数量と在庫数をチェックして在庫がなければエラー
734
                if ($orderDetail->getQuantity() > $productStock->getStock()) {
735 1
                    return false;
736
                }
737
            }
738 1
        }
739
740 2
        return true;
741
742 5
    }
743
744
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$data" missing
Loading history...
745
     * 受注情報、お届け先情報の更新
746
     *
747
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 22 spaces after parameter type; 1 found
Loading history...
748
     * @param $data フォームデータ
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
749
     */
750 2
    public function setOrderUpdate(Order $Order, $data)
751
    {
752
        // 受注情報を更新
753
        $Order->setOrderDate(new \DateTime());
754
        $Order->setOrderStatus($this->app['eccube.repository.order_status']->find($this->app['config']['order_new']));
755
        $Order->setMessage($data['message']);
756
757
        // お届け先情報を更新
758 2
        $shippings = $data['shippings'];
759
        foreach ($shippings as $Shipping) {
760
761
            $Delivery = $Shipping->getDelivery();
762
763 2
            $deliveryFee = $this->app['eccube.repository.delivery_fee']->findOneBy(array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
764
                'Delivery' => $Delivery,
765 2
                'Pref' => $Shipping->getPref()
766
            ));
767
768
            $deliveryTime = $Shipping->getDeliveryTime();
769 2
            if (!empty($deliveryTime)) {
770
                $Shipping->setShippingDeliveryTime($deliveryTime->getDeliveryTime());
771
            }
772
773
            $Shipping->setDeliveryFee($deliveryFee);
774
775
            // 商品ごとの配送料合計
776 2
            $productDeliveryFeeTotal = 0;
777
            if (!is_null($this->BaseInfo->getOptionProductDeliveryFee())) {
778
                $productDeliveryFeeTotal += $this->getProductDeliveryFee($Shipping);
779
            }
780
781
            $Shipping->setShippingDeliveryFee($deliveryFee->getFee() + $productDeliveryFeeTotal);
782
            $Shipping->setShippingDeliveryName($Delivery->getName());
783
        }
784
785
        // 配送料無料条件(合計金額)
786
        $this->setDeliveryFreeAmount($Order);
787
788
        // 配送料無料条件(合計数量)
789
        $this->setDeliveryFreeQuantity($Order);
790
791
    }
792
793
794
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$em" missing
Loading history...
795
     * 在庫情報の更新
796
     *
797
     * @param $em トランザクション制御されているEntityManager
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
798
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 57 spaces after parameter type; 1 found
Loading history...
799
     */
800 1
    public function setStockUpdate($em, Order $Order)
801
    {
802
803
        $orderDetails = $Order->getOrderDetails();
804
805
        // 在庫情報更新
806
        foreach ($orderDetails as $orderDetail) {
807
            // 在庫が無制限かチェックし、制限ありなら在庫数を更新
808
            if ($orderDetail->getProductClass()->getStockUnlimited() == Constant::DISABLED) {
809
810 1
                $productStock = $em->getRepository('Eccube\Entity\ProductStock')->find(
811
                    $orderDetail->getProductClass()->getProductStock()->getId()
812
                );
813
814
                // 在庫情報の在庫数を更新
815
                $stock = $productStock->getStock() - $orderDetail->getQuantity();
816
                $productStock->setStock($stock);
817
818
                // 商品規格情報の在庫数を更新
819
                $orderDetail->getProductClass()->setStock($stock);
820
821
            }
822
        }
823
824
    }
825
826
827
    /**
828
     * 会員情報の更新
829
     *
830
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
831
     * @param Customer $user ログインユーザ
0 ignored issues
show
introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
832
     */
833 1
    public function setCustomerUpdate(Order $Order, Customer $user)
834
    {
835
836
        $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...
837
838
        // 顧客情報を更新
839
        $now = new \DateTime();
840
        $firstBuyDate = $user->getFirstBuyDate();
841 1
        if (empty($firstBuyDate)) {
842
            $user->setFirstBuyDate($now);
843
        }
844
        $user->setLastBuyDate($now);
845
846
        $user->setBuyTimes($user->getBuyTimes() + 1);
847
        $user->setBuyTotal($user->getBuyTotal() + $Order->getTotal());
848
849
    }
850
851
852
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$payments" missing
Loading history...
introduced by
Doc comment for parameter "$subTotal" missing
Loading history...
853
     * 支払方法選択の表示設定
854
     *
855
     * @param $payments 支払選択肢情報
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
856
     * @param $subTotal 小計
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
857
     * @return array
858
     */
859 10
    public function getPayments($payments, $subTotal)
860
    {
861 10
        $pays = array();
862
        foreach ($payments as $payment) {
863
            // 支払方法の制限値内であれば表示
864
            if (!is_null($payment)) {
865
                $pay = $this->app['eccube.repository.payment']->find($payment['id']);
866
                if (intval($pay->getRuleMin()) <= $subTotal) {
867
                    if (is_null($pay->getRuleMax()) || $pay->getRuleMax() >= $subTotal) {
868
                        $pays[] = $pay;
869
                    }
870
                }
871
            }
872
        }
873
874 10
        return $pays;
875
876
    }
877
878
    /**
879
     * お届け日を取得
880
     *
881
     * @param Order $Order
882
     * @return array
883
     */
884 2
    public function getFormDeliveryDates(Order $Order)
885
    {
886
887
        // お届け日の設定
888 2
        $minDate = 0;
889 2
        $deliveryDateFlag = false;
890
891
        // 配送時に最大となる商品日数を取得
892 2
        foreach ($Order->getOrderDetails() as $detail) {
893
            $deliveryDate = $detail->getProductClass()->getDeliveryDate();
894
            if (!is_null($deliveryDate)) {
895
                if ($minDate < $deliveryDate->getValue()) {
896
                    $minDate = $deliveryDate->getValue();
897
                }
898
                // 配送日数が設定されている
899 1
                $deliveryDateFlag = true;
900
            }
901
        }
902
903
        // 配達最大日数期間を設定
904 2
        $deliveryDates = array();
905
906
        // 配送日数が設定されている
907 2
        if ($deliveryDateFlag) {
908
            $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...
909
                new \DateTime($minDate . ' day'),
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
910
                new \DateInterval('P1D'),
911
                new \DateTime($minDate + $this->app['config']['deliv_date_end_max'] . ' day')
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
912
            );
913
914
            foreach ($period as $day) {
915
                $deliveryDates[$day->format('Y/m/d')] = $day->format('Y/m/d');
916
            }
917
        }
918
919 2
        return $deliveryDates;
920
921 2
    }
922
923
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$deliveries" missing
Loading history...
924
     * 支払方法を取得
925
     *
926
     * @param $deliveries
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
927
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
928
     * @return array
929
     */
930 3
    public function getFormPayments($deliveries, Order $Order)
931
    {
932
933
        $productTypes = $this->orderService->getProductTypes($Order);
0 ignored issues
show
Deprecated Code introduced by
The method Eccube\Service\OrderService::getProductTypes() has been deprecated with message: since 3.0.0, to be removed in 3.1

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

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

Loading history...
934
935
        if ($this->BaseInfo->getOptionMultipleShipping() == Constant::ENABLED && count($productTypes) > 1) {
936
            // 複数配送時の支払方法
937
938
            $payments = $this->app['eccube.repository.payment']->findAllowedPayments($deliveries);
939
        } else {
940
941
            // 配送業者をセット
942
            $shippings = $Order->getShippings();
943
            $Shipping = $shippings[0];
944
            $payments = $this->app['eccube.repository.payment']->findPayments($Shipping->getDelivery(), true);
945
946
        }
947
        $payments = $this->getPayments($payments, $Order->getSubTotal());
948
949 3
        return $payments;
950
951 3
    }
952
953
    /**
954
     * お届け先ごとにFormを作成
955
     *
956
     * @param Order $Order
957
     * @return \Symfony\Component\Form\Form
958
     */
959 1
    public function getShippingForm(Order $Order)
960
    {
961
        $message = $Order->getMessage();
962
963
        $deliveries = $this->getDeliveriesOrder($Order);
964
965
        // 配送業者の支払方法を取得
966
        $payments = $this->getFormPayments($deliveries, $Order);
967
968
        $builder = $this->app['form.factory']->createBuilder('shopping', null, array(
969
            'payments' => $payments,
970 1
            'payment' => $Order->getPayment(),
971
            'message' => $message,
972
        ));
973
974
        $builder
975
            ->add('shippings', 'collection', array(
976 1
                'type' => 'shipping_item',
977 1
                'data' => $Order->getShippings(),
978
            ));
979
980
        $form = $builder->getForm();
981
982 1
        return $form;
983
984 1
    }
985
986
}
987