Completed
Pull Request — master (#1504)
by Kentaro
60:07 queued 04:12
created

ShoppingService::getShippingFormBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 13

Duplication

Lines 23
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 23
loc 23
ccs 5
cts 5
cp 1
rs 9.0856
cc 1
eloc 13
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\Exception\CartException;
38
use Eccube\Util\Str;
39
40
41
class ShoppingService
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
42
{
43
    /** @var \Eccube\Application */
44
    public $app;
45
46
    /** @var \Eccube\Service\CartService */
47
    protected $cartService;
48
49
    /** @var \Eccube\Service\OrderService */
50
    protected $orderService;
51
52
    /** @var \Eccube\Entity\BaseInfo */
53
    protected $BaseInfo;
54
55
    /** @var  \Doctrine\ORM\EntityManager */
56
    protected $em;
57
58 44
    public function __construct(Application $app, $cartService, $orderService)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
59
    {
60 44
        $this->app = $app;
61 44
        $this->cartService = $cartService;
62 44
        $this->orderService = $orderService;
63
        $this->BaseInfo = $app['eccube.repository.base_info']->get();
64
    }
65
66
    /**
67
     * セッションにセットされた受注情報を取得
68
     *
69
     * @param null $status
70
     * @return null|object
71
     */
72 17
    public function getOrder($status = null)
73
    {
74
75
        // 受注データを取得
76
        $preOrderId = $this->cartService->getPreOrderId();
77 6
        if (!$preOrderId) {
78 10
            return null;
79
        }
80
81
        $condition = array(
82
            'pre_order_id' => $preOrderId,
83 17
        );
84
85
        if (!is_null($status)) {
86
            $condition += array(
87
                'OrderStatus' => $status,
88 14
            );
89
        }
90
91
        $Order = $this->app['eccube.repository.order']->findOneBy($condition);
92
93 17
        return $Order;
94
    }
95 3
96
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$sesisonKey" missing
Loading history...
97
     * 非会員情報を取得
98
     *
99
     * @param $sesisonKey
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
100
     * @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...
101
     */
102
    public function getNonMember($sesisonKey)
103 9
    {
104
105
        // 非会員でも一度会員登録されていればショッピング画面へ遷移
106
        $nonMember = $this->app['session']->get($sesisonKey);
107
        if (is_null($nonMember)) {
108
            return null;
109 1
        }
110
111
        $Customer = $nonMember['customer'];
112 9
        $Customer->setPref($this->app['eccube.repository.master.pref']->find($nonMember['pref']));
113
114
        return $Customer;
115 9
    }
116
117
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
118
     * 受注情報を作成
119
     *
120
     * @param $Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
121
     * @return \Eccube\Entity\Order
122
     */
123
    public function createOrder($Customer)
124
    {
125 16
        // ランダムなpre_order_idを作成
126
        $preOrderId = sha1(Str::random(32));
127
128
        // 受注情報、受注明細情報、お届け先情報、配送商品情報を作成
129
        $Order = $this->registerPreOrder(
130
            $Customer,
131 16
            $preOrderId);
132
133
        $this->cartService->setPreOrderId($preOrderId);
134
        $this->cartService->save();
135
136
        return $Order;
137
    }
138 16
139
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$preOrderId" missing
Loading history...
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
140
     * 仮受注情報作成
141
     *
142
     * @param $Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
143
     * @param $preOrderId
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
144
     * @return mixed
145
     * @throws \Doctrine\ORM\NoResultException
146
     * @throws \Doctrine\ORM\NonUniqueResultException
147
     */
148
    public function registerPreOrder(Customer $Customer, $preOrderId)
149
    {
150 16
        $this->em = $this->app['orm.em'];
151
152
        // 受注情報を作成
153
        $Order = $this->getNewOrder($Customer);
154
        $Order->setPreOrderId($preOrderId);
155
156
        $this->em->persist($Order);
157
158
        // 配送業者情報を取得
159
        $deliveries = $this->getDeliveriesCart();
160
161
        // お届け先情報を作成
162
        $Order = $this->getNewShipping($Order, $Customer, $deliveries);
163
164
        // 受注明細情報、配送商品情報を作成
165
        $Order = $this->getNewDetails($Order);
166
167
        // 小計
168
        $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...
169
170
        // 消費税のみの小計
171
        $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...
172
173
        // 配送料合計金額
174
        $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($Order->getShippings()));
175
176
        // 小計
177
        $Order->setSubTotal($subTotal);
178
179
        // 配送料無料条件(合計金額)
180
        $this->setDeliveryFreeAmount($Order);
181
182
        // 配送料無料条件(合計数量)
183
        $this->setDeliveryFreeQuantity($Order);
184
185
        // 初期選択の支払い方法をセット
186
        $payments = $this->app['eccube.repository.payment']->findAllowedPayments($deliveries);
187
        $payments = $this->getPayments($payments, $subTotal);
188
189
        if (count($payments) > 0) {
190
            $payment = $payments[0];
191
            $Order->setPayment($payment);
192
            $Order->setPaymentMethod($payment->getMethod());
193 16
            $Order->setCharge($payment->getCharge());
194
        } else {
195
            $Order->setCharge(0);
196
        }
197
198
        $Order->setTax($tax);
199 16
200
        $total = $subTotal + $Order->getCharge() + $Order->getDeliveryFeeTotal();
201
202
        $Order->setTotal($total);
203
        $Order->setPaymentTotal($total);
204
205
        $this->em->flush();
206
207
        return $Order;
208
    }
209
210 16
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
211
     * 受注情報を作成
212 16
     * @param $Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
213
     * @return \Eccube\Entity\Order
214
     */
215
    public function getNewOrder(Customer $Customer)
216
    {
217
        $Order = $this->newOrder();
218
        $this->copyToOrderFromCustomer($Order, $Customer);
219 16
220
        return $Order;
221
    }
222
223
224 16
    /**
225 16
     * 受注情報を作成
226
     * @return \Eccube\Entity\Order
227
     */
228
    public function newOrder()
229
    {
230
        $OrderStatus = $this->app['eccube.repository.order_status']->find($this->app['config']['order_processing']);
231
        $Order = new \Eccube\Entity\Order($OrderStatus);
232 16
        return $Order;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
233
    }
234
235
    /**
236 16
     * 受注情報を作成
237
     *
238
     * @param \Eccube\Entity\Order $Order
0 ignored issues
show
introduced by
Expected 9 spaces after parameter type; 1 found
Loading history...
239
     * @param \Eccube\Entity\Customer|null $Customer
240
     * @return \Eccube\Entity\Order
241
     */
242
    public function copyToOrderFromCustomer(Order $Order, Customer $Customer = null)
243
    {
244
        if (is_null($Customer)) {
245
            return $Order;
246 16
        }
247
248
        if ($Customer->getId()) {
249
            $Order->setCustomer($Customer);
250
        }
251
        $Order
252
            ->setName01($Customer->getName01())
253
            ->setName02($Customer->getName02())
254
            ->setKana01($Customer->getKana01())
255
            ->setKana02($Customer->getKana02())
256
            ->setCompanyName($Customer->getCompanyName())
257
            ->setEmail($Customer->getEmail())
258
            ->setTel01($Customer->getTel01())
259
            ->setTel02($Customer->getTel02())
260
            ->setTel03($Customer->getTel03())
261
            ->setFax01($Customer->getFax01())
262
            ->setFax02($Customer->getFax02())
263
            ->setFax03($Customer->getFax03())
264
            ->setZip01($Customer->getZip01())
265
            ->setZip02($Customer->getZip02())
266
            ->setZipCode($Customer->getZip01().$Customer->getZip02())
267
            ->setPref($Customer->getPref())
268
            ->setAddr01($Customer->getAddr01())
269
            ->setAddr02($Customer->getAddr02())
270
            ->setSex($Customer->getSex())
271
            ->setBirth($Customer->getBirth())
272
            ->setJob($Customer->getJob());
273
274
        return $Order;
275
    }
276
277
278 16
    /**
279 16
     * 配送業者情報を取得
280
     *
281
     * @return array
282
     */
283
    public function getDeliveriesCart()
284
    {
285
286
        // カートに保持されている商品種別を取得
287
        $productTypes = $this->cartService->getProductTypes();
288
289
        return $this->getDeliveries($productTypes);
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
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$productTypes" missing
Loading history...
308
     * 配送業者情報を取得
309
     *
310
     * @param $productTypes
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
311
     * @return array
312
     */
313
    public function getDeliveries($productTypes)
314
    {
315
316
        // 商品種別に紐づく配送業者を取得
317
        $deliveries = $this->app['eccube.repository.delivery']->getDeliveries($productTypes);
318
319 10
        if ($this->BaseInfo->getOptionMultipleShipping() == Constant::ENABLED) {
320
            // 複数配送対応
321
322
            // 支払方法を取得
323
            $payments = $this->app['eccube.repository.payment']->findAllowedPayments($deliveries);
324
325
            if (count($productTypes) > 1) {
326
                // 商品種別が複数ある場合、配送対象となる配送業者を取得
327
                $deliveries = $this->app['eccube.repository.delivery']->findAllowedDeliveries($productTypes, $payments);
328
            }
329
        }
330
331
        return $deliveries;
332
    }
333
334
335
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$deliveries" missing
Loading history...
336
     * お届け先情報を作成
337
     *
338 10
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
339
     * @param Customer $Customer
0 ignored issues
show
introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
340
     * @param $deliveries
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
341
     * @return Order
342
     */
343
    public function getNewShipping(Order $Order, Customer $Customer, $deliveries)
344
    {
345
        $productTypes = array();
346
        foreach ($deliveries as $Delivery) {
347
            if (!in_array($Delivery->getProductType()->getId(), $productTypes)) {
348
                $Shipping = new Shipping();
349
350
                $this->copyToShippingFromCustomer($Shipping, $Customer)
351 16
                    ->setOrder($Order)
352
                    ->setDelFlg(Constant::DISABLED);
353 16
354
                // 配送料金の設定
355
                $this->setShippingDeliveryFee($Shipping, $Delivery);
356
357
                $this->em->persist($Shipping);
358 16
359 16
                $Order->addShipping($Shipping);
360
361
                $productTypes[] = $Delivery->getProductType()->getId();
362
            }
363
        }
364
365
        return $Order;
366
    }
367
368
    /**
369
     * お届け先情報を作成
370
     *
371
     * @param \Eccube\Entity\Shipping $Shipping
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
372
     * @param \Eccube\Entity\Customer|null $Customer
373 16
     * @return \Eccube\Entity\Shipping
374 16
     */
375
    public function copyToShippingFromCustomer(Shipping $Shipping, Customer $Customer = null)
376
    {
377
        if (is_null($Customer)) {
378
            return $Shipping;
379
        }
380
381
        $CustomerAddress = $this->app['eccube.repository.customer_address']->findOneBy(
382
            array('Customer' => $Customer),
383 17
            array('id' => 'ASC')
384
        );
385
386 1
        if (!is_null($CustomerAddress)) {
387
            $Shipping
388
                ->setName01($CustomerAddress->getName01())
389 16
                ->setName02($CustomerAddress->getName02())
390 16
                ->setKana01($CustomerAddress->getKana01())
391 16
                ->setKana02($CustomerAddress->getKana02())
392
                ->setCompanyName($CustomerAddress->getCompanyName())
393
                ->setTel01($CustomerAddress->getTel01())
394
                ->setTel02($CustomerAddress->getTel02())
395
                ->setTel03($CustomerAddress->getTel03())
396
                ->setFax01($CustomerAddress->getFax01())
397
                ->setFax02($CustomerAddress->getFax02())
398
                ->setFax03($CustomerAddress->getFax03())
399
                ->setZip01($CustomerAddress->getZip01())
400
                ->setZip02($CustomerAddress->getZip02())
401
                ->setZipCode($CustomerAddress->getZip01().$CustomerAddress->getZip02())
402
                ->setPref($CustomerAddress->getPref())
403
                ->setAddr01($CustomerAddress->getAddr01())
404
                ->setAddr02($CustomerAddress->getAddr02());
405
        } else {
406
            $Shipping
407
                ->setName01($Customer->getName01())
408
                ->setName02($Customer->getName02())
409
                ->setKana01($Customer->getKana01())
410
                ->setKana02($Customer->getKana02())
411
                ->setCompanyName($Customer->getCompanyName())
412
                ->setTel01($Customer->getTel01())
413
                ->setTel02($Customer->getTel02())
414
                ->setTel03($Customer->getTel03())
415
                ->setFax01($Customer->getFax01())
416
                ->setFax02($Customer->getFax02())
417
                ->setFax03($Customer->getFax03())
418
                ->setZip01($Customer->getZip01())
419
                ->setZip02($Customer->getZip02())
420
                ->setZipCode($Customer->getZip01().$Customer->getZip02())
421
                ->setPref($Customer->getPref())
422
                ->setAddr01($Customer->getAddr01())
423
                ->setAddr02($Customer->getAddr02());
424
        }
425
426
        return $Shipping;
427
    }
428
429
430
    /**
431
     * 受注明細情報、配送商品情報を作成
432 14
     *
433
     * @param Order $Order
434 16
     * @return Order
435 17
     */
436
    public function getNewDetails(Order $Order)
437
    {
438
439
        // 受注詳細, 配送商品
440
        foreach ($this->cartService->getCart()->getCartItems() as $item) {
441
            /* @var $ProductClass \Eccube\Entity\ProductClass */
442
            $ProductClass = $item->getObject();
443
            /* @var $Product \Eccube\Entity\Product */
444 16
            $Product = $ProductClass->getProduct();
445
446
            $quantity = $item->getQuantity();
447
448
            // 受注明細情報を作成
449
            $OrderDetail = $this->getNewOrderDetail($Product, $ProductClass, $quantity);
450
            $OrderDetail->setOrder($Order);
451
            $Order->addOrderDetail($OrderDetail);
452
453
            // 配送商品情報を作成
454
            $this->getNewShipmentItem($Order, $Product, $ProductClass, $quantity);
455
        }
456
457
        return $Order;
458
    }
459
460
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$quantity" missing
Loading history...
461
     * 受注明細情報を作成
462
     *
463
     * @param Product $Product
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
464
     * @param ProductClass $ProductClass
465 16
     * @param $quantity
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
466
     * @return \Eccube\Entity\OrderDetail
467 16
     */
468
    public function getNewOrderDetail(Product $Product, ProductClass $ProductClass, $quantity)
469
    {
470
        $OrderDetail = new OrderDetail();
471
        $TaxRule = $this->app['eccube.repository.tax_rule']->getByRule($Product, $ProductClass);
472
        $OrderDetail->setProduct($Product)
473
            ->setProductClass($ProductClass)
474
            ->setProductName($Product->getName())
475
            ->setProductCode($ProductClass->getCode())
476
            ->setPrice($ProductClass->getPrice02())
477 16
            ->setQuantity($quantity)
478
            ->setTaxRule($TaxRule->getCalcRule()->getId())
479
            ->setTaxRate($TaxRule->getTaxRate());
480
481 16
        $ClassCategory1 = $ProductClass->getClassCategory1();
482 16
        if (!is_null($ClassCategory1)) {
483
            $OrderDetail->setClasscategoryName1($ClassCategory1->getName());
484
            $OrderDetail->setClassName1($ClassCategory1->getClassName()->getName());
485
        }
486 16
        $ClassCategory2 = $ProductClass->getClassCategory2();
487
        if (!is_null($ClassCategory2)) {
488
            $OrderDetail->setClasscategoryName2($ClassCategory2->getName());
489
            $OrderDetail->setClassName2($ClassCategory2->getClassName()->getName());
490
        }
491
492
        $this->em->persist($OrderDetail);
493
494
        return $OrderDetail;
495
    }
496
497
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$quantity" missing
Loading history...
498
     * 配送商品情報を作成
499
     *
500
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
501
     * @param Product $Product
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
502
     * @param ProductClass $ProductClass
503 16
     * @param $quantity
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
504 16
     * @return \Eccube\Entity\ShipmentItem
505
     */
506
    public function getNewShipmentItem(Order $Order, Product $Product, ProductClass $ProductClass, $quantity)
507
    {
508
        $ShipmentItem = new ShipmentItem();
509
        $shippings = $Order->getShippings();
510
511
        // 選択された商品がどのお届け先情報と関連するかチェック
512
        $Shipping = null;
513
        foreach ($shippings as $s) {
514
            if ($s->getDelivery()->getProductType()->getId() == $ProductClass->getProductType()->getId()) {
515 16
                // 商品種別が同一のお届け先情報と関連させる
516
                $Shipping = $s;
517
                break;
518
            }
519
        }
520
521
        if (is_null($Shipping)) {
522 16
            // お届け先情報と関連していない場合、エラー
523
            throw new CartException('shopping.delivery.not.producttype');
524
        }
525
526 16
        // 商品ごとの配送料合計
527 16
        $productDeliveryFeeTotal = 0;
528
        if (!is_null($this->BaseInfo->getOptionProductDeliveryFee())) {
529 16
            $productDeliveryFeeTotal = $ProductClass->getDeliveryFee() * $quantity;
530
        }
531
532
        $Shipping->setShippingDeliveryFee($Shipping->getShippingDeliveryFee() + $productDeliveryFeeTotal);
533
534
        $ShipmentItem->setShipping($Shipping)
535
            ->setOrder($Order)
536
            ->setProductClass($ProductClass)
537 16
            ->setProduct($Product)
538
            ->setProductName($Product->getName())
539
            ->setProductCode($ProductClass->getCode())
540
            ->setPrice($ProductClass->getPrice02())
541
            ->setQuantity($quantity);
542
543
        $ClassCategory1 = $ProductClass->getClassCategory1();
544 16
        if (!is_null($ClassCategory1)) {
545 16
            $ShipmentItem->setClasscategoryName1($ClassCategory1->getName());
546 16
            $ShipmentItem->setClassName1($ClassCategory1->getClassName()->getName());
547 16
        }
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
        return $ShipmentItem;
557
    }
558
559
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$shippings" missing
Loading history...
560
     * お届け先ごとの送料合計を取得
561
     *
562
     * @param $shippings
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
563
     * @return int
564
     */
565
    public function getShippingDeliveryFeeTotal($shippings)
566 16
    {
567
        $deliveryFeeTotal = 0;
568 16
        foreach ($shippings as $Shipping) {
569
            $deliveryFeeTotal += $Shipping->getShippingDeliveryFee();
570
        }
571
572
        return $deliveryFeeTotal;
573
    }
574
575
    /**
576 16
     * 商品ごとの配送料を取得
577
     *
578 16
     * @param Shipping $Shipping
579
     * @return int
580
     */
581
    public function getProductDeliveryFee(Shipping $Shipping)
582
    {
583 16
        $productDeliveryFeeTotal = 0;
584
        $shipmentItems = $Shipping->getShipmentItems();
585
        foreach ($shipmentItems as $ShipmentItem) {
586
            $productDeliveryFeeTotal += $ShipmentItem->getProductClass()->getDeliveryFee() * $ShipmentItem->getQuantity();
587
        }
588
        return $productDeliveryFeeTotal;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
589
    }
590
591
    /**
592
     * 住所などの情報が変更された時に金額の再計算を行う
593 14
     *
594
     * @param Order $Order
595 14
     * @return Order
596
     */
597
    public function getAmount(Order $Order)
598
    {
599 13
600 14
        // 初期選択の配送業者をセット
601 14
        $shippings = $Order->getShippings();
602
603
        // 配送料合計金額
604
        $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($shippings));
605
606
        // 配送料無料条件(合計金額)
607
        $this->setDeliveryFreeAmount($Order);
608
609 1
        // 配送料無料条件(合計数量)
610
        $this->setDeliveryFreeQuantity($Order);
611
612
        $total = $Order->getSubTotal() + $Order->getCharge() + $Order->getDeliveryFeeTotal();
613
614
        $Order->setTotal($total);
615
        $Order->setPaymentTotal($total);
616
        $this->app['orm.em']->flush();
617
618
        return $Order;
619
    }
620
621
    /**
622
     * 配送料金の設定
623
     *
624
     * @param Shipping $Shipping
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
625
     * @param Delivery|null $Delivery
626
     */
627
    public function setShippingDeliveryFee(Shipping $Shipping, Delivery $Delivery = null)
628
    {
629
        // 配送料金の設定
630 1
        if (is_null($Delivery)) {
631
            $Delivery = $Shipping->getDelivery();
632 1
        }
633
        $deliveryFee = $this->app['eccube.repository.delivery_fee']->findOneBy(array('Delivery' => $Delivery, 'Pref' => $Shipping->getPref()));
634
635
        $Shipping->setDeliveryFee($deliveryFee);
636
        $Shipping->setDelivery($Delivery);
637
638
        // 商品ごとの配送料合計
639
        $productDeliveryFeeTotal = 0;
640 15
        if (!is_null($this->BaseInfo->getOptionProductDeliveryFee())) {
641
            $productDeliveryFeeTotal += $this->getProductDeliveryFee($Shipping);
642
        }
643
644
        $Shipping->setShippingDeliveryFee($deliveryFee->getFee() + $productDeliveryFeeTotal);
645
        $Shipping->setShippingDeliveryName($Delivery->getName());
646
    }
647
648
    /**
649
     * 配送料無料条件(合計金額)の条件を満たしていれば配送料金を0に設定
650
     *
651
     * @param Order $Order
652 15
     */
653 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...
654
    {
655
        // 配送料無料条件(合計金額)
656
        $deliveryFreeAmount = $this->BaseInfo->getDeliveryFreeAmount();
657
        if (!is_null($deliveryFreeAmount)) {
658
            // 合計金額が設定金額以上であれば送料無料
659
            if ($Order->getSubTotal() >= $deliveryFreeAmount) {
660
                $Order->setDeliveryFeeTotal(0);
661
                // お届け先情報の配送料も0にセット
662
                $shippings = $Order->getShippings();
663
                foreach ($shippings as $Shipping) {
664
                    $Shipping->setShippingDeliveryFee(0);
665
                }
666
            }
667
        }
668
    }
669
670
    /**
671
     * 配送料無料条件(合計数量)の条件を満たしていれば配送料金を0に設定
672
     *
673
     * @param Order $Order
674
     */
675 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...
676
    {
677
        // 配送料無料条件(合計数量)
678
        $deliveryFreeQuantity = $this->BaseInfo->getDeliveryFreeQuantity();
679
        if (!is_null($deliveryFreeQuantity)) {
680
            // 合計数量が設定数量以上であれば送料無料
681
            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...
682
                $Order->setDeliveryFeeTotal(0);
683
                // お届け先情報の配送料も0にセット
684
                $shippings = $Order->getShippings();
685
                foreach ($shippings as $Shipping) {
686
                    $Shipping->setShippingDeliveryFee(0);
687
                }
688
            }
689
        }
690
    }
691
692
693
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$em" missing
Loading history...
694
     * 商品公開ステータスチェック、在庫チェック、購入制限数チェックを行い、在庫情報をロックする
695
     *
696
     * @param $em トランザクション制御されているEntityManager
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
697
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 57 spaces after parameter type; 1 found
Loading history...
698
     * @return bool true : 成功、false : 失敗
699
     */
700
    public function isOrderProduct($em, \Eccube\Entity\Order $Order)
701
    {
702
        // 商品公開ステータスチェック
703
        $orderDetails = $Order->getOrderDetails();
704
705
        foreach ($orderDetails as $orderDetail) {
706
            if ($orderDetail->getProduct()->getStatus()->getId() != \Eccube\Entity\Master\Disp::DISPLAY_SHOW) {
707
                // 商品が非公開ならエラー
708
                return false;
709
            }
710
711
            // 購入制限数チェック
712
            if (!is_null($orderDetail->getProductClass()->getSaleLimit())) {
713 8
                if ($orderDetail->getQuantity() > $orderDetail->getProductClass()->getSaleLimit()) {
714
                    return false;
715
                }
716
            }
717
        }
718
719
        // 在庫チェック
720
        foreach ($orderDetails as $orderDetail) {
721 1
            // 在庫が無制限かチェックし、制限ありなら在庫数をチェック
722
            if ($orderDetail->getProductClass()->getStockUnlimited() == Constant::DISABLED) {
723
                // 在庫チェックあり
724
                // 在庫に対してロック(select ... for update)を実行
725
                $productStock = $em->getRepository('Eccube\Entity\ProductStock')->find(
726
                    $orderDetail->getProductClass()->getProductStock()->getId(), LockMode::PESSIMISTIC_WRITE
727 1
                );
728
                // 購入数量と在庫数をチェックして在庫がなければエラー
729
                if ($orderDetail->getQuantity() > $productStock->getStock()) {
730
                    return false;
731 2
                }
732
            }
733
        }
734
735
        return true;
736
    }
737
738
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$data" missing
Loading history...
739 1
     * 受注情報、お届け先情報の更新
740
     *
741
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 22 spaces after parameter type; 1 found
Loading history...
742
     * @param $data フォームデータ
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
743
     */
744 1
    public function setOrderUpdate(Order $Order, $data)
745
    {
746
        // 受注情報を更新
747 1
        $Order->setOrderDate(new \DateTime());
748
        $Order->setOrderStatus($this->app['eccube.repository.order_status']->find($this->app['config']['order_new']));
749 5
        $Order->setMessage($data['message']);
750
751 8
        // お届け先情報を更新
752
        $shippings = $data['shippings'];
753
        foreach ($shippings as $Shipping) {
754
            $Delivery = $Shipping->getDelivery();
755
756
            $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...
757
                'Delivery' => $Delivery,
758
                'Pref' => $Shipping->getPref()
759 5
            ));
760
761
            $deliveryTime = $Shipping->getDeliveryTime();
762
            if (!empty($deliveryTime)) {
763
                $Shipping->setShippingDeliveryTime($deliveryTime->getDeliveryTime());
764
            }
765
766
            $Shipping->setDeliveryFee($deliveryFee);
767 5
768
            // 商品ごとの配送料合計
769
            $productDeliveryFeeTotal = 0;
770
            if (!is_null($this->BaseInfo->getOptionProductDeliveryFee())) {
771
                $productDeliveryFeeTotal += $this->getProductDeliveryFee($Shipping);
772 5
            }
773
774 5
            $Shipping->setShippingDeliveryFee($deliveryFee->getFee() + $productDeliveryFeeTotal);
775
            $Shipping->setShippingDeliveryName($Delivery->getName());
776
        }
777
778 5
        // 配送料無料条件(合計金額)
779
        $this->setDeliveryFreeAmount($Order);
780
781
        // 配送料無料条件(合計数量)
782
        $this->setDeliveryFreeQuantity($Order);
783
    }
784
785 5
786
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$em" missing
Loading history...
787
     * 在庫情報の更新
788
     *
789
     * @param $em トランザクション制御されているEntityManager
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
790
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 57 spaces after parameter type; 1 found
Loading history...
791
     */
792
    public function setStockUpdate($em, Order $Order)
793
    {
794
        $orderDetails = $Order->getOrderDetails();
795
796
        // 在庫情報更新
797
        foreach ($orderDetails as $orderDetail) {
798
            // 在庫が無制限かチェックし、制限ありなら在庫数を更新
799
            if ($orderDetail->getProductClass()->getStockUnlimited() == Constant::DISABLED) {
800
                $productStock = $em->getRepository('Eccube\Entity\ProductStock')->find(
801
                    $orderDetail->getProductClass()->getProductStock()->getId()
802
                );
803
804
                // 在庫情報の在庫数を更新
805
                $stock = $productStock->getStock() - $orderDetail->getQuantity();
806
                $productStock->setStock($stock);
807
808
                // 商品規格情報の在庫数を更新
809 1
                $orderDetail->getProductClass()->setStock($stock);
810
            }
811
        }
812
    }
813
814
815
    /**
816
     * 会員情報の更新
817
     *
818
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
819 1
     * @param Customer $user ログインユーザ
0 ignored issues
show
introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
820
     */
821
    public function setCustomerUpdate(Order $Order, Customer $user)
822
    {
823
        $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...
824
825
        // 顧客情報を更新
826
        $now = new \DateTime();
827
        $firstBuyDate = $user->getFirstBuyDate();
828
        if (empty($firstBuyDate)) {
829
            $user->setFirstBuyDate($now);
830
        }
831
        $user->setLastBuyDate($now);
832
833
        $user->setBuyTimes($user->getBuyTimes() + 1);
834
        $user->setBuyTotal($user->getBuyTotal() + $Order->getTotal());
835
    }
836
837
838
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$payments" missing
Loading history...
introduced by
Doc comment for parameter "$subTotal" missing
Loading history...
839
     * 支払方法選択の表示設定
840
     *
841
     * @param $payments 支払選択肢情報
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
842 3
     * @param $subTotal 小計
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
843
     * @return array
844
     */
845
    public function getPayments($payments, $subTotal)
846
    {
847
        $pays = array();
848
        foreach ($payments as $payment) {
849
            // 支払方法の制限値内であれば表示
850 3
            if (!is_null($payment)) {
851
                $pay = $this->app['eccube.repository.payment']->find($payment['id']);
852
                if (intval($pay->getRuleMin()) <= $subTotal) {
853
                    if (is_null($pay->getRuleMax()) || $pay->getRuleMax() >= $subTotal) {
854
                        $pays[] = $pay;
855
                    }
856
                }
857
            }
858
        }
859
860
        return $pays;
861
    }
862
863
    /**
864
     * お届け日を取得
865
     *
866
     * @param Order $Order
867
     * @return array
868 10
     */
869
    public function getFormDeliveryDates(Order $Order)
870 10
    {
871
872
        // お届け日の設定
873
        $minDate = 0;
874
        $deliveryDateFlag = false;
875
876
        // 配送時に最大となる商品日数を取得
877
        foreach ($Order->getOrderDetails() as $detail) {
878
            $deliveryDate = $detail->getProductClass()->getDeliveryDate();
879
            if (!is_null($deliveryDate)) {
880
                if ($minDate < $deliveryDate->getValue()) {
881
                    $minDate = $deliveryDate->getValue();
882
                }
883 10
                // 配送日数が設定されている
884
                $deliveryDateFlag = true;
885
            }
886
        }
887
888
        // 配達最大日数期間を設定
889
        $deliveryDates = array();
890
891
        // 配送日数が設定されている
892
        if ($deliveryDateFlag) {
893 4
            $period = new \DatePeriod(
894
                new \DateTime($minDate.' day'),
895
                new \DateInterval('P1D'),
896
                new \DateTime($minDate + $this->app['config']['deliv_date_end_max'].' day')
897 4
            );
898 4
899
            foreach ($period as $day) {
900
                $deliveryDates[$day->format('Y/m/d')] = $day->format('Y/m/d');
901 4
            }
902
        }
903
904
        return $deliveryDates;
905
    }
906
907
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$deliveries" missing
Loading history...
908 1
     * 支払方法を取得
909
     *
910
     * @param $deliveries
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
911
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
912
     * @return array
913 4
     */
914
    public function getFormPayments($deliveries, Order $Order)
915
    {
916 4
        $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...
917
918
        if ($this->BaseInfo->getOptionMultipleShipping() == Constant::ENABLED && count($productTypes) > 1) {
919
            // 複数配送時の支払方法
920
921
            $payments = $this->app['eccube.repository.payment']->findAllowedPayments($deliveries);
922
        } else {
923
924
            // 配送業者をセット
925
            $shippings = $Order->getShippings();
926
            $Shipping = $shippings[0];
927
            $payments = $this->app['eccube.repository.payment']->findPayments($Shipping->getDelivery(), true);
928 4
        }
929
        $payments = $this->getPayments($payments, $Order->getSubTotal());
930 4
931
        return $payments;
932
    }
933
934
    /**
935
     * お届け先ごとにFormを作成
936
     *
937
     * @param Order $Order
938
     * @return \Symfony\Component\Form\Form
939 5
     * @deprecated since 3.0, to be removed in 3.1
940
     */
941 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...
942
    {
943
        $message = $Order->getMessage();
944
945
        $deliveries = $this->getDeliveriesOrder($Order);
946
947
        // 配送業者の支払方法を取得
948
        $payments = $this->getFormPayments($deliveries, $Order);
949
950
        $builder = $this->app['form.factory']->createBuilder('shopping', null, array(
951
            'payments' => $payments,
952
            'payment' => $Order->getPayment(),
953
            'message' => $message,
954
        ));
955
956
        $builder
957
            ->add('shippings', 'collection', array(
958 5
                'type' => 'shipping_item',
959
                'data' => $Order->getShippings(),
960 5
            ));
961
962
        $form = $builder->getForm();
963
964
        return $form;
965
    }
966
967
    /**
968 3
     * お届け先ごとにFormBuilderを作成
969
     *
970
     * @param Order $Order
971
     * @return \Symfony\Component\Form\FormBuilderInterface
972
     */
973 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...
974
    {
975
        $message = $Order->getMessage();
976
977
        $deliveries = $this->getDeliveriesOrder($Order);
978
979 3
        // 配送業者の支払方法を取得
980
        $payments = $this->getFormPayments($deliveries, $Order);
981
982
        $builder = $this->app['form.factory']->createBuilder('shopping', null, array(
983
            'payments' => $payments,
984
            'payment' => $Order->getPayment(),
985 3
            'message' => $message,
986 3
        ));
987
988
        $builder
989
            ->add('shippings', 'collection', array(
990
                'type' => 'shipping_item',
991 3
                'data' => $Order->getShippings(),
992
            ));
993 3
994
        return $builder;
995
    }
996
}
997