Failed Conditions
Push — master ( 51b8bf...a12a0c )
by Ryo
45:25
created

ShoppingService::setCustomerUpdate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 9.4285
cc 2
eloc 9
nc 2
nop 2
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 Eccube\Application;
28
use Eccube\Common\Constant;
29
use Eccube\Entity\Customer;
30
use Eccube\Entity\Delivery;
31
use Eccube\Entity\MailHistory;
32
use Eccube\Entity\Master\DeviceType;
33
use Eccube\Entity\Order;
34
use Eccube\Entity\OrderDetail;
35
use Eccube\Entity\Product;
36
use Eccube\Entity\ProductClass;
37
use Eccube\Entity\ShipmentItem;
38
use Eccube\Entity\Shipping;
39
use Eccube\Event\EccubeEvents;
40
use Eccube\Event\EventArgs;
41
use Eccube\Exception\CartException;
42
use Eccube\Exception\ShoppingException;
43
use Eccube\Util\Str;
44
45
46
class ShoppingService
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
47
{
48
    /** @var \Eccube\Application */
49
    public $app;
50
51
    /** @var \Eccube\Service\CartService */
52
    protected $cartService;
53
54
    /** @var \Eccube\Service\OrderService */
55
    protected $orderService;
56
57
    /** @var \Eccube\Entity\BaseInfo */
58
    protected $BaseInfo;
59
60
    /** @var  \Doctrine\ORM\EntityManager */
61
    protected $em;
62
63 113
    public function __construct(Application $app, $cartService, $orderService)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
64
    {
65 113
        $this->app = $app;
66 113
        $this->cartService = $cartService;
67 113
        $this->orderService = $orderService;
68 113
        $this->BaseInfo = $app['eccube.repository.base_info']->get();
69
    }
70
71
    /**
72
     * セッションにセットされた受注情報を取得
73
     *
74
     * @param null $status
75
     * @return null|object
76
     */
77 90
    public function getOrder($status = null)
78
    {
79
80
        // 受注データを取得
81 90
        $preOrderId = $this->cartService->getPreOrderId();
82 90
        if (!$preOrderId) {
83 85
            return null;
84
        }
85
86
        $condition = array(
87 78
            'pre_order_id' => $preOrderId,
88
        );
89
90 78
        if (!is_null($status)) {
91
            $condition += array(
92 75
                'OrderStatus' => $status,
93
            );
94
        }
95
96 78
        $Order = $this->app['eccube.repository.order']->findOneBy($condition);
97
98 78
        return $Order;
99
100
    }
101
102
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$sesisonKey" missing
Loading history...
103
     * 非会員情報を取得
104
     *
105
     * @param $sesisonKey
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
106
     * @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...
107
     */
108 32
    public function getNonMember($sesisonKey)
109
    {
110
111
        // 非会員でも一度会員登録されていればショッピング画面へ遷移
112 32
        $nonMember = $this->app['session']->get($sesisonKey);
113 32
        if (is_null($nonMember)) {
114 1
            return null;
115
        }
116
117 31
        $Customer = $nonMember['customer'];
118 31
        $Customer->setPref($this->app['eccube.repository.master.pref']->find($nonMember['pref']));
119
120 31
        return $Customer;
121
122
    }
123
124
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
125
     * 受注情報を作成
126
     *
127
     * @param $Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
128
     * @return \Eccube\Entity\Order
129
     */
130 92
    public function createOrder($Customer)
131
    {
132
        // ランダムなpre_order_idを作成
133
        do {
134 92
            $preOrderId = sha1(Str::random(32));
135 92
            $Order = $this->app['eccube.repository.order']->findOneBy(array(
136 92
                'pre_order_id' => $preOrderId,
137 92
                'OrderStatus' => $this->app['config']['order_processing'],
138
            ));
139 92
        } while ($Order);
140
141
        // 受注情報、受注明細情報、お届け先情報、配送商品情報を作成
142 92
        $Order = $this->registerPreOrder(
143
            $Customer,
144
            $preOrderId);
145
146 92
        $this->cartService->setPreOrderId($preOrderId);
147 92
        $this->cartService->save();
148
149 92
        return $Order;
150
    }
151
152
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$preOrderId" missing
Loading history...
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
153
     * 仮受注情報作成
154
     *
155
     * @param $Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
156
     * @param $preOrderId
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
157
     * @return mixed
158
     * @throws \Doctrine\ORM\NoResultException
159
     * @throws \Doctrine\ORM\NonUniqueResultException
160
     */
161 92
    public function registerPreOrder(Customer $Customer, $preOrderId)
162
    {
163
164 92
        $this->em = $this->app['orm.em'];
165
166
        // 受注情報を作成
167 92
        $Order = $this->getNewOrder($Customer);
168 92
        $Order->setPreOrderId($preOrderId);
169
170
        // device type
171 92
        if ($this->app['mobile_detect']->isMobile()) {
172
            $DeviceType = $this->app['eccube.repository.master.device_type']->find(DeviceType::DEVICE_TYPE_SP);
173
        } else {
174 92
            $DeviceType = $this->app['eccube.repository.master.device_type']->find(DeviceType::DEVICE_TYPE_PC);
175
        }
176 92
        $Order->setDeviceType($DeviceType);
177
178 92
        $this->em->persist($Order);
179
180
        // 配送業者情報を取得
181 92
        $deliveries = $this->getDeliveriesCart();
182
183
        // お届け先情報を作成
184 92
        $Order = $this->getNewShipping($Order, $Customer, $deliveries);
185
186
        // 受注明細情報、配送商品情報を作成
187 92
        $Order = $this->getNewDetails($Order);
188
189
        // 小計
190 92
        $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...
191
192
        // 消費税のみの小計
193 92
        $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...
194
195
        // 配送料合計金額
196 92
        $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($Order->getShippings()));
197
198
        // 小計
199 92
        $Order->setSubTotal($subTotal);
200
201
        // 配送料無料条件(合計金額)
202 92
        $this->setDeliveryFreeAmount($Order);
203
204
        // 配送料無料条件(合計数量)
205 92
        $this->setDeliveryFreeQuantity($Order);
206
207
        // 初期選択の支払い方法をセット
208 92
        $payments = $this->app['eccube.repository.payment']->findAllowedPayments($deliveries);
209 92
        $payments = $this->getPayments($payments, $subTotal);
210
211 92
        if (count($payments) > 0) {
212 92
            $payment = $payments[0];
213 92
            $Order->setPayment($payment);
214 92
            $Order->setPaymentMethod($payment->getMethod());
215 92
            $Order->setCharge($payment->getCharge());
216
        } else {
217
            $Order->setCharge(0);
218
        }
219
220 92
        $Order->setTax($tax);
221
222
        // 合計金額の計算
223 92
        $this->calculatePrice($Order);
224
225 92
        $this->em->flush();
226
227 92
        return $Order;
228
229
    }
230
231
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
232
     * 受注情報を作成
233
     *
234
     * @param $Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
235
     * @return \Eccube\Entity\Order
236
     */
237 92
    public function getNewOrder(Customer $Customer)
238
    {
239 92
        $Order = $this->newOrder();
240 92
        $this->copyToOrderFromCustomer($Order, $Customer);
241
242 92
        return $Order;
243
    }
244
245
246
    /**
247
     * 受注情報を作成
248
     *
249
     * @return \Eccube\Entity\Order
250
     */
251 92
    public function newOrder()
252
    {
253 92
        $OrderStatus = $this->app['eccube.repository.order_status']->find($this->app['config']['order_processing']);
254 92
        $Order = new \Eccube\Entity\Order($OrderStatus);
255
256 92
        return $Order;
257
    }
258
259
    /**
260
     * 受注情報を作成
261
     *
262
     * @param \Eccube\Entity\Order $Order
0 ignored issues
show
introduced by
Expected 9 spaces after parameter type; 1 found
Loading history...
263
     * @param \Eccube\Entity\Customer|null $Customer
264
     * @return \Eccube\Entity\Order
265
     */
266 92
    public function copyToOrderFromCustomer(Order $Order, Customer $Customer = null)
267
    {
268 92
        if (is_null($Customer)) {
269
            return $Order;
270
        }
271
272 92
        if ($Customer->getId()) {
273 60
            $Order->setCustomer($Customer);
274
        }
275
        $Order
276 92
            ->setName01($Customer->getName01())
277 92
            ->setName02($Customer->getName02())
278 92
            ->setKana01($Customer->getKana01())
279 92
            ->setKana02($Customer->getKana02())
280 92
            ->setCompanyName($Customer->getCompanyName())
281 92
            ->setEmail($Customer->getEmail())
282 92
            ->setTel01($Customer->getTel01())
283 92
            ->setTel02($Customer->getTel02())
284 92
            ->setTel03($Customer->getTel03())
285 92
            ->setFax01($Customer->getFax01())
286 92
            ->setFax02($Customer->getFax02())
287 92
            ->setFax03($Customer->getFax03())
288 92
            ->setZip01($Customer->getZip01())
289 92
            ->setZip02($Customer->getZip02())
290 92
            ->setZipCode($Customer->getZip01().$Customer->getZip02())
291 92
            ->setPref($Customer->getPref())
292 92
            ->setAddr01($Customer->getAddr01())
293 92
            ->setAddr02($Customer->getAddr02())
294 92
            ->setSex($Customer->getSex())
295 92
            ->setBirth($Customer->getBirth())
296 92
            ->setJob($Customer->getJob());
297
298 92
        return $Order;
299
    }
300
301
302
    /**
303
     * 配送業者情報を取得
304
     *
305
     * @return array
306
     */
307 92
    public function getDeliveriesCart()
308
    {
309
310
        // カートに保持されている商品種別を取得
311 92
        $productTypes = $this->cartService->getProductTypes();
312
313 92
        return $this->getDeliveries($productTypes);
314
315
    }
316
317
    /**
318
     * 配送業者情報を取得
319
     *
320
     * @param Order $Order
321
     * @return array
322
     */
323 84
    public function getDeliveriesOrder(Order $Order)
324
    {
325
326
        // 受注情報から商品種別を取得
327 84
        $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...
328
329 84
        return $this->getDeliveries($productTypes);
330
331
    }
332
333
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$productTypes" missing
Loading history...
334
     * 配送業者情報を取得
335
     *
336
     * @param $productTypes
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
337
     * @return array
338
     */
339 95
    public function getDeliveries($productTypes)
340
    {
341
342
        // 商品種別に紐づく配送業者を取得
343 95
        $deliveries = $this->app['eccube.repository.delivery']->getDeliveries($productTypes);
344
345 95
        if ($this->BaseInfo->getOptionMultipleShipping() == Constant::ENABLED) {
346
            // 複数配送対応
347
348
            // 支払方法を取得
349 41
            $payments = $this->app['eccube.repository.payment']->findAllowedPayments($deliveries);
350
351 41
            if (count($productTypes) > 1) {
352
                // 商品種別が複数ある場合、配送対象となる配送業者を取得
353 6
                $deliveries = $this->app['eccube.repository.delivery']->findAllowedDeliveries($productTypes, $payments);
354
            }
355
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
356
        }
357
358 95
        return $deliveries;
359
360
    }
361
362
363
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$deliveries" missing
Loading history...
364
     * お届け先情報を作成
365
     *
366
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
367
     * @param Customer $Customer
0 ignored issues
show
introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
368
     * @param $deliveries
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
369
     * @return Order
370
     */
371 92
    public function getNewShipping(Order $Order, Customer $Customer, $deliveries)
372
    {
373 92
        $productTypes = array();
374 92
        foreach ($deliveries as $Delivery) {
375 92
            if (!in_array($Delivery->getProductType()->getId(), $productTypes)) {
376 92
                $Shipping = new Shipping();
377
378 92
                $this->copyToShippingFromCustomer($Shipping, $Customer)
379 92
                    ->setOrder($Order)
380 92
                    ->setDelFlg(Constant::DISABLED);
381
382
                // 配送料金の設定
383 92
                $this->setShippingDeliveryFee($Shipping, $Delivery);
384
385 92
                $this->em->persist($Shipping);
386
387 92
                $Order->addShipping($Shipping);
388
389 92
                $productTypes[] = $Delivery->getProductType()->getId();
390
            }
391
        }
392
393 92
        return $Order;
394
    }
395
396
    /**
397
     * お届け先情報を作成
398
     *
399
     * @param \Eccube\Entity\Shipping $Shipping
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
400
     * @param \Eccube\Entity\Customer|null $Customer
401
     * @return \Eccube\Entity\Shipping
402
     */
403 93
    public function copyToShippingFromCustomer(Shipping $Shipping, Customer $Customer = null)
404
    {
405 93
        if (is_null($Customer)) {
406 1
            return $Shipping;
407
        }
408
409 92
        $CustomerAddress = $this->app['eccube.repository.customer_address']->findOneBy(
410 92
            array('Customer' => $Customer),
411 92
            array('id' => 'ASC')
412
        );
413
414 92
        if (!is_null($CustomerAddress)) {
415
            $Shipping
416 60
                ->setName01($CustomerAddress->getName01())
417 60
                ->setName02($CustomerAddress->getName02())
418 60
                ->setKana01($CustomerAddress->getKana01())
419 60
                ->setKana02($CustomerAddress->getKana02())
420 60
                ->setCompanyName($CustomerAddress->getCompanyName())
421 60
                ->setTel01($CustomerAddress->getTel01())
422 60
                ->setTel02($CustomerAddress->getTel02())
423 60
                ->setTel03($CustomerAddress->getTel03())
424 60
                ->setFax01($CustomerAddress->getFax01())
425 60
                ->setFax02($CustomerAddress->getFax02())
426 60
                ->setFax03($CustomerAddress->getFax03())
427 60
                ->setZip01($CustomerAddress->getZip01())
428 60
                ->setZip02($CustomerAddress->getZip02())
429 60
                ->setZipCode($CustomerAddress->getZip01().$CustomerAddress->getZip02())
430 60
                ->setPref($CustomerAddress->getPref())
431 60
                ->setAddr01($CustomerAddress->getAddr01())
432 60
                ->setAddr02($CustomerAddress->getAddr02());
433
        } else {
434
            $Shipping
435 32
                ->setName01($Customer->getName01())
436 32
                ->setName02($Customer->getName02())
437 32
                ->setKana01($Customer->getKana01())
438 32
                ->setKana02($Customer->getKana02())
439 32
                ->setCompanyName($Customer->getCompanyName())
440 32
                ->setTel01($Customer->getTel01())
441 32
                ->setTel02($Customer->getTel02())
442 32
                ->setTel03($Customer->getTel03())
443 32
                ->setFax01($Customer->getFax01())
444 32
                ->setFax02($Customer->getFax02())
445 32
                ->setFax03($Customer->getFax03())
446 32
                ->setZip01($Customer->getZip01())
447 32
                ->setZip02($Customer->getZip02())
448 32
                ->setZipCode($Customer->getZip01().$Customer->getZip02())
449 32
                ->setPref($Customer->getPref())
450 32
                ->setAddr01($Customer->getAddr01())
451 32
                ->setAddr02($Customer->getAddr02());
452
        }
453
454 92
        return $Shipping;
455
    }
456
457
458
    /**
459
     * 受注明細情報、配送商品情報を作成
460
     *
461
     * @param Order $Order
462
     * @return Order
463
     */
464 92
    public function getNewDetails(Order $Order)
465
    {
466
467
        // 受注詳細, 配送商品
468 92
        foreach ($this->cartService->getCart()->getCartItems() as $item) {
469
            /* @var $ProductClass \Eccube\Entity\ProductClass */
470 92
            $ProductClass = $item->getObject();
471
            /* @var $Product \Eccube\Entity\Product */
472 92
            $Product = $ProductClass->getProduct();
473
474 92
            $quantity = $item->getQuantity();
475
476
            // 受注明細情報を作成
477 92
            $OrderDetail = $this->getNewOrderDetail($Product, $ProductClass, $quantity);
478 92
            $OrderDetail->setOrder($Order);
479 92
            $Order->addOrderDetail($OrderDetail);
480
481
            // 配送商品情報を作成
482 92
            $this->getNewShipmentItem($Order, $Product, $ProductClass, $quantity);
483
        }
484
485 92
        return $Order;
486
487
    }
488
489
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$quantity" missing
Loading history...
490
     * 受注明細情報を作成
491
     *
492
     * @param Product $Product
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
493
     * @param ProductClass $ProductClass
494
     * @param $quantity
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
495
     * @return \Eccube\Entity\OrderDetail
496
     */
497 92
    public function getNewOrderDetail(Product $Product, ProductClass $ProductClass, $quantity)
498
    {
499 92
        $OrderDetail = new OrderDetail();
500 92
        $TaxRule = $this->app['eccube.repository.tax_rule']->getByRule($Product, $ProductClass);
501 92
        $OrderDetail->setProduct($Product)
502 92
            ->setProductClass($ProductClass)
503 92
            ->setProductName($Product->getName())
504 92
            ->setProductCode($ProductClass->getCode())
505 92
            ->setPrice($ProductClass->getPrice02())
506 92
            ->setQuantity($quantity)
507 92
            ->setTaxRule($TaxRule->getCalcRule()->getId())
508 92
            ->setTaxRate($TaxRule->getTaxRate());
509
510 92
        $ClassCategory1 = $ProductClass->getClassCategory1();
511 92
        if (!is_null($ClassCategory1)) {
512 92
            $OrderDetail->setClasscategoryName1($ClassCategory1->getName());
513 92
            $OrderDetail->setClassName1($ClassCategory1->getClassName()->getName());
514
        }
515 92
        $ClassCategory2 = $ProductClass->getClassCategory2();
516 92
        if (!is_null($ClassCategory2)) {
517 79
            $OrderDetail->setClasscategoryName2($ClassCategory2->getName());
518 79
            $OrderDetail->setClassName2($ClassCategory2->getClassName()->getName());
519
        }
520
521 92
        $this->em->persist($OrderDetail);
522
523 92
        return $OrderDetail;
524
    }
525
526
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$quantity" missing
Loading history...
527
     * 配送商品情報を作成
528
     *
529
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
530
     * @param Product $Product
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
531
     * @param ProductClass $ProductClass
532
     * @param $quantity
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
533
     * @return \Eccube\Entity\ShipmentItem
534
     */
535 92
    public function getNewShipmentItem(Order $Order, Product $Product, ProductClass $ProductClass, $quantity)
536
    {
537
538 92
        $ShipmentItem = new ShipmentItem();
539 92
        $shippings = $Order->getShippings();
540
541
        // 選択された商品がどのお届け先情報と関連するかチェック
542 92
        $Shipping = null;
543 92
        foreach ($shippings as $s) {
544 92
            if ($s->getDelivery()->getProductType()->getId() == $ProductClass->getProductType()->getId()) {
545
                // 商品種別が同一のお届け先情報と関連させる
546 92
                $Shipping = $s;
547 92
                break;
548
            }
549
        }
550
551 92
        if (is_null($Shipping)) {
552
            // お届け先情報と関連していない場合、エラー
553
            throw new CartException('shopping.delivery.not.producttype');
554
        }
555
556
        // 商品ごとの配送料合計
557 92
        $productDeliveryFeeTotal = 0;
558 92
        if (!is_null($this->BaseInfo->getOptionProductDeliveryFee())) {
559 92
            $productDeliveryFeeTotal = $ProductClass->getDeliveryFee() * $quantity;
560
        }
561
562 92
        $Shipping->setShippingDeliveryFee($Shipping->getShippingDeliveryFee() + $productDeliveryFeeTotal);
563
564 92
        $ShipmentItem->setShipping($Shipping)
565 92
            ->setOrder($Order)
566 92
            ->setProductClass($ProductClass)
567 92
            ->setProduct($Product)
568 92
            ->setProductName($Product->getName())
569 92
            ->setProductCode($ProductClass->getCode())
570 92
            ->setPrice($ProductClass->getPrice02())
571 92
            ->setQuantity($quantity);
572
573 92
        $ClassCategory1 = $ProductClass->getClassCategory1();
574 92
        if (!is_null($ClassCategory1)) {
575 92
            $ShipmentItem->setClasscategoryName1($ClassCategory1->getName());
576 92
            $ShipmentItem->setClassName1($ClassCategory1->getClassName()->getName());
577
        }
578 92
        $ClassCategory2 = $ProductClass->getClassCategory2();
579 92
        if (!is_null($ClassCategory2)) {
580 79
            $ShipmentItem->setClasscategoryName2($ClassCategory2->getName());
581 79
            $ShipmentItem->setClassName2($ClassCategory2->getClassName()->getName());
582
        }
583 92
        $Shipping->addShipmentItem($ShipmentItem);
584 92
        $this->em->persist($ShipmentItem);
585
586 92
        return $ShipmentItem;
587
588
    }
589
590
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$shippings" missing
Loading history...
591
     * お届け先ごとの送料合計を取得
592
     *
593
     * @param $shippings
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
594
     * @return int
595
     */
596 93
    public function getShippingDeliveryFeeTotal($shippings)
597
    {
598 93
        $deliveryFeeTotal = 0;
599 93
        foreach ($shippings as $Shipping) {
600 93
            $deliveryFeeTotal += $Shipping->getShippingDeliveryFee();
601
        }
602
603 93
        return $deliveryFeeTotal;
604
605
    }
606
607
    /**
608
     * 商品ごとの配送料を取得
609
     *
610
     * @param Shipping $Shipping
611
     * @return int
612
     */
613 92
    public function getProductDeliveryFee(Shipping $Shipping)
614
    {
615 92
        $productDeliveryFeeTotal = 0;
616 92
        $shipmentItems = $Shipping->getShipmentItems();
617 92
        foreach ($shipmentItems as $ShipmentItem) {
618 92
            $productDeliveryFeeTotal += $ShipmentItem->getProductClass()->getDeliveryFee() * $ShipmentItem->getQuantity();
619
        }
620
621 92
        return $productDeliveryFeeTotal;
622
    }
623
624
    /**
625
     * 住所などの情報が変更された時に金額の再計算を行う
626
     *
627
     * @param Order $Order
628
     * @return Order
629
     */
630 43 View Code Duplication
    public function getAmount(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...
631
    {
632
633
        // 初期選択の配送業者をセット
634 43
        $shippings = $Order->getShippings();
635
636
        // 配送料合計金額
637 43
        $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($shippings));
638
639
        // 配送料無料条件(合計金額)
640 43
        $this->setDeliveryFreeAmount($Order);
641
642
        // 配送料無料条件(合計数量)
643 43
        $this->setDeliveryFreeQuantity($Order);
644
645
        // 合計金額の計算
646 43
        $this->calculatePrice($Order);
647
648 43
        return $Order;
649
650
    }
651
652
    /**
653
     * 配送料金の設定
654
     *
655
     * @param Shipping $Shipping
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
656
     * @param Delivery|null $Delivery
657
     */
658 92
    public function setShippingDeliveryFee(Shipping $Shipping, Delivery $Delivery = null)
659
    {
660
        // 配送料金の設定
661 92
        if (is_null($Delivery)) {
662 33
            $Delivery = $Shipping->getDelivery();
663
        }
664 92
        $deliveryFee = $this->app['eccube.repository.delivery_fee']->findOneBy(array('Delivery' => $Delivery, 'Pref' => $Shipping->getPref()));
665
666 92
        $Shipping->setDeliveryFee($deliveryFee);
667 92
        $Shipping->setDelivery($Delivery);
668
669
        // 商品ごとの配送料合計
670 92
        $productDeliveryFeeTotal = 0;
671 92
        if (!is_null($this->BaseInfo->getOptionProductDeliveryFee())) {
672 92
            $productDeliveryFeeTotal += $this->getProductDeliveryFee($Shipping);
673
        }
674
675 92
        $Shipping->setShippingDeliveryFee($deliveryFee->getFee() + $productDeliveryFeeTotal);
676 92
        $Shipping->setShippingDeliveryName($Delivery->getName());
677
    }
678
679
    /**
680
     * 配送料無料条件(合計金額)の条件を満たしていれば配送料金を0に設定
681
     *
682
     * @param Order $Order
683
     */
684 94 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...
685
    {
686
        // 配送料無料条件(合計金額)
687 94
        $deliveryFreeAmount = $this->BaseInfo->getDeliveryFreeAmount();
688 94
        if (!is_null($deliveryFreeAmount)) {
689
            // 合計金額が設定金額以上であれば送料無料
690 1
            if ($Order->getSubTotal() >= $deliveryFreeAmount) {
691 1
                $Order->setDeliveryFeeTotal(0);
692
                // お届け先情報の配送料も0にセット
693 1
                $shippings = $Order->getShippings();
694 1
                foreach ($shippings as $Shipping) {
695 1
                    $Shipping->setShippingDeliveryFee(0);
696
                }
697
            }
698
        }
699
    }
700
701
    /**
702
     * 配送料無料条件(合計数量)の条件を満たしていれば配送料金を0に設定
703
     *
704
     * @param Order $Order
705
     */
706 94 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...
707
    {
708
        // 配送料無料条件(合計数量)
709 94
        $deliveryFreeQuantity = $this->BaseInfo->getDeliveryFreeQuantity();
710 94
        if (!is_null($deliveryFreeQuantity)) {
711
            // 合計数量が設定数量以上であれば送料無料
712 1
            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...
713 1
                $Order->setDeliveryFeeTotal(0);
714
                // お届け先情報の配送料も0にセット
715 1
                $shippings = $Order->getShippings();
716 1
                foreach ($shippings as $Shipping) {
717 1
                    $Shipping->setShippingDeliveryFee(0);
718
                }
719
            }
720
        }
721
    }
722
723
724
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$em" missing
Loading history...
725
     * 商品公開ステータスチェック、在庫チェック、購入制限数チェックを行い、在庫情報をロックする
726
     *
727
     * @param $em トランザクション制御されているEntityManager
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
728
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 57 spaces after parameter type; 1 found
Loading history...
729
     * @return bool true : 成功、false : 失敗
730
     */
731 26
    public function isOrderProduct($em, \Eccube\Entity\Order $Order)
732
    {
733 26
        $orderDetails = $Order->getOrderDetails();
734
735 26
        foreach ($orderDetails as $orderDetail) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
736
737
            // 商品削除チェック
738 26
            if ($orderDetail->getProductClass()->getDelFlg()) {
739
                throw new ShoppingException('cart.product.delete');
740
            }
741
742
            // 商品公開ステータスチェック
743 26
            if ($orderDetail->getProduct()->getStatus()->getId() != \Eccube\Entity\Master\Disp::DISPLAY_SHOW) {
744
                // 商品が非公開ならエラー
745 1
                throw new ShoppingException('cart.product.not.status');
746
            }
747
748
            // 購入制限数チェック
749 25
            if (!is_null($orderDetail->getProductClass()->getSaleLimit())) {
750 2
                if ($orderDetail->getQuantity() > $orderDetail->getProductClass()->getSaleLimit()) {
751 1
                    throw new ShoppingException('cart.over.sale_limit');
752
                }
753
            }
754
755
            // 購入数チェック
756 24
            if ($orderDetail->getQuantity() < 1) {
757
                // 購入数量が1未満ならエラー
758 24
                return false;
759
            }
760
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
761
        }
762
763
        // 在庫チェック
764 24
        foreach ($orderDetails as $orderDetail) {
765
            // 在庫が無制限かチェックし、制限ありなら在庫数をチェック
766 24
            if ($orderDetail->getProductClass()->getStockUnlimited() == Constant::DISABLED) {
767
                // 在庫チェックあり
768
                // 在庫に対してロック(select ... for update)を実行
769 14
                $productStock = $em->getRepository('Eccube\Entity\ProductStock')->find(
770 14
                    $orderDetail->getProductClass()->getProductStock()->getId(), LockMode::PESSIMISTIC_WRITE
771
                );
772
                // 購入数量と在庫数をチェックして在庫がなければエラー
773 14
                if ($productStock->getStock() < 1) {
774
                    return false;
775 14
                } elseif ($orderDetail->getQuantity() > $productStock->getStock()) {
776 24
                    throw new ShoppingException('cart.over.stock');
777
                }
778
            }
779
        }
780
781 23
        return true;
782
783
    }
784
785
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$data" missing
Loading history...
786
     * 受注情報、お届け先情報の更新
787
     *
788
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 22 spaces after parameter type; 1 found
Loading history...
789
     * @param $data フォームデータ
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
790
     *
791
     * @deprecated since 3.0.5, to be removed in 3.1
792
     */
793
    public function setOrderUpdate(Order $Order, $data)
794
    {
795
        // 受注情報を更新
796
        $Order->setOrderDate(new \DateTime());
797
        $Order->setOrderStatus($this->app['eccube.repository.order_status']->find($this->app['config']['order_new']));
798
        $Order->setMessage($data['message']);
799
        // お届け先情報を更新
800
        $shippings = $data['shippings'];
801
        foreach ($shippings as $Shipping) {
802
            $Delivery = $Shipping->getDelivery();
803
            $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...
804
                'Delivery' => $Delivery,
805
                'Pref' => $Shipping->getPref()
806
            ));
807
            $deliveryTime = $Shipping->getDeliveryTime();
808
            if (!empty($deliveryTime)) {
809
                $Shipping->setShippingDeliveryTime($deliveryTime->getDeliveryTime());
810
            }
811
            $Shipping->setDeliveryFee($deliveryFee);
812
            // 商品ごとの配送料合計
813
            $productDeliveryFeeTotal = 0;
814
            if (!is_null($this->BaseInfo->getOptionProductDeliveryFee())) {
815
                $productDeliveryFeeTotal += $this->getProductDeliveryFee($Shipping);
816
            }
817
            $Shipping->setShippingDeliveryFee($deliveryFee->getFee() + $productDeliveryFeeTotal);
818
            $Shipping->setShippingDeliveryName($Delivery->getName());
819
        }
820
        // 配送料無料条件(合計金額)
821
        $this->setDeliveryFreeAmount($Order);
822
        // 配送料無料条件(合計数量)
823
        $this->setDeliveryFreeQuantity($Order);
824
    }
825
826
827
    /**
828
     * 受注情報の更新
829
     *
830
     * @param Order $Order 受注情報
831
     */
832 23
    public function setOrderUpdateData(Order $Order)
833
    {
834
        // 受注情報を更新
835 23
        $Order->setOrderDate(new \DateTime());
836 23
        $OrderStatus = $this->app['eccube.repository.order_status']->find($this->app['config']['order_new']);
837 23
        $this->setOrderStatus($Order, $OrderStatus);
838
839
    }
840
841
842
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$em" missing
Loading history...
843
     * 在庫情報の更新
844
     *
845
     * @param $em トランザクション制御されているEntityManager
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
846
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 57 spaces after parameter type; 1 found
Loading history...
847
     */
848 23
    public function setStockUpdate($em, Order $Order)
849
    {
850
851 23
        $orderDetails = $Order->getOrderDetails();
852
853
        // 在庫情報更新
854 23
        foreach ($orderDetails as $orderDetail) {
855
            // 在庫が無制限かチェックし、制限ありなら在庫数を更新
856 23
            if ($orderDetail->getProductClass()->getStockUnlimited() == Constant::DISABLED) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
857
858 14
                $productStock = $em->getRepository('Eccube\Entity\ProductStock')->find(
859 14
                    $orderDetail->getProductClass()->getProductStock()->getId()
860
                );
861
862
                // 在庫情報の在庫数を更新
863 14
                $stock = $productStock->getStock() - $orderDetail->getQuantity();
864 14
                $productStock->setStock($stock);
865
866
                // 商品規格情報の在庫数を更新
867 23
                $orderDetail->getProductClass()->setStock($stock);
868
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
869
            }
870
        }
871
872
    }
873
874
875
    /**
876
     * 会員情報の更新
877
     *
878
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
879
     * @param Customer $user ログインユーザ
0 ignored issues
show
introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
880
     */
881 15
    public function setCustomerUpdate(Order $Order, Customer $user)
882
    {
883
884 15
        $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...
885
886
        // 顧客情報を更新
887 15
        $now = new \DateTime();
888 15
        $firstBuyDate = $user->getFirstBuyDate();
889 15
        if (empty($firstBuyDate)) {
890 15
            $user->setFirstBuyDate($now);
891
        }
892 15
        $user->setLastBuyDate($now);
893
894 15
        $user->setBuyTimes($user->getBuyTimes() + 1);
895 15
        $user->setBuyTotal($user->getBuyTotal() + $Order->getTotal());
896
897
    }
898
899
900
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$payments" missing
Loading history...
introduced by
Doc comment for parameter "$subTotal" missing
Loading history...
901
     * 支払方法選択の表示設定
902
     *
903
     * @param $payments 支払選択肢情報
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
904
     * @param $subTotal 小計
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
905
     * @return array
906
     */
907 95
    public function getPayments($payments, $subTotal)
908
    {
909 95
        $pays = array();
910 95
        foreach ($payments as $payment) {
911
            // 支払方法の制限値内であれば表示
912 95
            if (!is_null($payment)) {
913 95
                $pay = $this->app['eccube.repository.payment']->find($payment['id']);
914 95
                if (intval($pay->getRuleMin()) <= $subTotal) {
915 95
                    if (is_null($pay->getRuleMax()) || $pay->getRuleMax() >= $subTotal) {
916 95
                        $pays[] = $pay;
917
                    }
918
                }
919
            }
920
        }
921
922 95
        return $pays;
923
924
    }
925
926
    /**
927
     * お届け日を取得
928
     *
929
     * @param Order $Order
930
     * @return array
931
     */
932 86
    public function getFormDeliveryDates(Order $Order)
933
    {
934
935
        // お届け日の設定
936 86
        $minDate = 0;
937 86
        $deliveryDateFlag = false;
938
939
        // 配送時に最大となる商品日数を取得
940 86
        foreach ($Order->getOrderDetails() as $detail) {
941 86
            $deliveryDate = $detail->getProductClass()->getDeliveryDate();
942 86
            if (!is_null($deliveryDate)) {
943 42
                if ($deliveryDate->getValue() < 0) {
944
                    // 配送日数がマイナスの場合はお取り寄せなのでスキップする
945 9
                    $deliveryDateFlag = false;
946 9
                    break;
947
                }
948
949 35
                if ($minDate < $deliveryDate->getValue()) {
950 33
                    $minDate = $deliveryDate->getValue();
951
                }
952
                // 配送日数が設定されている
953 86
                $deliveryDateFlag = true;
954
            }
955
        }
956
957
        // 配達最大日数期間を設定
958 86
        $deliveryDates = array();
959
960
        // 配送日数が設定されている
961 86
        if ($deliveryDateFlag) {
962 33
            $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...
963 33
                new \DateTime($minDate.' day'),
964 33
                new \DateInterval('P1D'),
965 33
                new \DateTime($minDate + $this->app['config']['deliv_date_end_max'].' day')
966
            );
967
968 33
            foreach ($period as $day) {
969 33
                $deliveryDates[$day->format('Y/m/d')] = $day->format('Y/m/d');
970
            }
971
        }
972
973 86
        return $deliveryDates;
974
975
    }
976
977
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$deliveries" missing
Loading history...
978
     * 支払方法を取得
979
     *
980
     * @param $deliveries
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
981
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
982
     * @return array
983
     */
984 86
    public function getFormPayments($deliveries, Order $Order)
985
    {
986
987 86
        $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...
988
989 86
        if ($this->BaseInfo->getOptionMultipleShipping() == Constant::ENABLED && count($productTypes) > 1) {
990
            // 複数配送時の支払方法
991
992 5
            $payments = $this->app['eccube.repository.payment']->findAllowedPayments($deliveries);
993
        } else {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
994
995
            // 配送業者をセット
996 81
            $shippings = $Order->getShippings();
997 81
            $Shipping = $shippings[0];
998 81
            $payments = $this->app['eccube.repository.payment']->findPayments($Shipping->getDelivery(), true);
999
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
1000
        }
1001 86
        $payments = $this->getPayments($payments, $Order->getSubTotal());
1002
1003 86
        return $payments;
1004
1005
    }
1006
1007
    /**
1008
     * お届け先ごとにFormを作成
1009
     *
1010
     * @param Order $Order
1011
     * @return \Symfony\Component\Form\Form
1012
     * @deprecated since 3.0, to be removed in 3.1
1013
     */
1014 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...
1015
    {
1016
        $message = $Order->getMessage();
1017
1018
        $deliveries = $this->getDeliveriesOrder($Order);
1019
1020
        // 配送業者の支払方法を取得
1021
        $payments = $this->getFormPayments($deliveries, $Order);
1022
1023
        $builder = $this->app['form.factory']->createBuilder('shopping', null, array(
1024
            'payments' => $payments,
1025
            'payment' => $Order->getPayment(),
1026
            'message' => $message,
1027
        ));
1028
1029
        $builder
1030
            ->add('shippings', 'collection', array(
1031
                'type' => 'shipping_item',
1032
                'data' => $Order->getShippings(),
1033
            ));
1034
1035
        $form = $builder->getForm();
1036
1037
        return $form;
1038
1039
    }
1040
1041
    /**
1042
     * お届け先ごとにFormBuilderを作成
1043
     *
1044
     * @param Order $Order
1045
     * @return \Symfony\Component\Form\FormBuilderInterface
1046
     */
1047 84 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...
1048
    {
1049 84
        $message = $Order->getMessage();
1050
1051 84
        $deliveries = $this->getDeliveriesOrder($Order);
1052
1053
        // 配送業者の支払方法を取得
1054 84
        $payments = $this->getFormPayments($deliveries, $Order);
1055
1056 84
        $builder = $this->app['form.factory']->createBuilder('shopping', null, array(
1057 84
            'payments' => $payments,
1058 84
            'payment' => $Order->getPayment(),
1059 84
            'message' => $message,
1060
        ));
1061
1062
        $builder
1063 84
            ->add('shippings', 'collection', array(
1064 84
                'type' => 'shipping_item',
1065 84
                'data' => $Order->getShippings(),
1066
            ));
1067
1068 84
        return $builder;
1069
1070
    }
1071
1072
1073
    /**
1074
     * フォームデータを更新
1075
     *
1076
     * @param Order $Order
1077
     * @param array $data
1078
     */
1079 23
    public function setFormData(Order $Order, array $data)
1080
    {
1081
1082
        // お問い合わせ
1083 23
        $Order->setMessage($data['message']);
1084
1085
        // お届け先情報を更新
1086 23
        $shippings = $data['shippings'];
1087 23
        foreach ($shippings as $Shipping) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
1088
1089 23
            $deliveryTime = $Shipping->getDeliveryTime();
1090 23
            if (!empty($deliveryTime)) {
1091 23
                $Shipping->setShippingDeliveryTime($deliveryTime->getDeliveryTime());
1092
            }
1093
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
1094
        }
1095
1096
    }
1097
1098
    /**
1099
     * 配送料の合計金額を計算
1100
     *
1101
     * @param Order $Order
1102
     * @return Order
1103
     */
1104 22 View Code Duplication
    public function calculateDeliveryFee(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...
1105
    {
1106
1107
        // 配送業者を取得
1108 22
        $shippings = $Order->getShippings();
1109
1110
        // 配送料合計金額
1111 22
        $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($shippings));
1112
1113
        // 配送料無料条件(合計金額)
1114 22
        $this->setDeliveryFreeAmount($Order);
1115
1116
        // 配送料無料条件(合計数量)
1117 22
        $this->setDeliveryFreeQuantity($Order);
1118
1119 22
        return $Order;
1120
1121
    }
1122
1123
1124
    /**
1125
     * 購入処理を行う
1126
     *
1127
     * @param Order $Order
1128
     * @throws ShoppingException
1129
     */
1130 22
    public function processPurchase(Order $Order)
1131
    {
1132
1133 22
        $em = $this->app['orm.em'];
1134
1135
        // 合計金額の再計算
1136 22
        $this->calculatePrice($Order);
1137
1138
        // 商品公開ステータスチェック、商品制限数チェック、在庫チェック
1139 22
        $check = $this->isOrderProduct($em, $Order);
1140 22
        if (!$check) {
1141
            throw new ShoppingException('front.shopping.stock.error');
1142
        }
1143
1144
        // 受注情報、配送情報を更新
1145 22
        $Order = $this->calculateDeliveryFee($Order);
1146 22
        $this->setOrderUpdateData($Order);
1147
        // 在庫情報を更新
1148 22
        $this->setStockUpdate($em, $Order);
1149
1150 22
        if ($this->app->isGranted('ROLE_USER')) {
1151
            // 会員の場合、購入金額を更新
1152 14
            $this->setCustomerUpdate($Order, $this->app->user());
1153
        }
1154
1155
    }
1156
1157
1158
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$discount" missing
Loading history...
1159
     * 値引き可能かチェック
1160
     *
1161
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
1162
     * @param       $discount
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
1163
     * @return bool
1164
     */
1165
    public function isDiscount(Order $Order, $discount)
1166
    {
1167
1168
        if ($Order->getTotal() < $discount) {
1169
            return false;
1170
        }
1171
1172
        return true;
1173
    }
1174
1175
1176
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$discount" missing
Loading history...
1177
     * 値引き金額をセット
1178
     *
1179
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
1180
     * @param $discount
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
1181
     */
1182
    public function setDiscount(Order $Order, $discount)
1183
    {
1184
1185
        $Order->setDiscount($Order->getDiscount() + $discount);
1186
1187
    }
1188
1189
1190
    /**
1191
     * 合計金額を計算
1192
     *
1193
     * @param Order $Order
1194
     * @return Order
1195
     */
1196 93
    public function calculatePrice(Order $Order)
1197
    {
1198
1199 93
        $total = $Order->getTotalPrice();
1200
1201 93
        if ($total < 0) {
1202
            // 合計金額がマイナスの場合、0を設定し、discountは値引きされた額のみセット
1203
            $total = 0;
1204
        }
1205
1206 93
        $Order->setTotal($total);
1207 93
        $Order->setPaymentTotal($total);
1208
1209 93
        return $Order;
1210
1211
    }
1212
1213
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$status" missing
Loading history...
1214
     * 受注ステータスをセット
1215
     *
1216
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
1217
     * @param $status
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
1218
     * @return Order
1219
     */
1220 23
    public function setOrderStatus(Order $Order, $status)
1221
    {
1222
1223 23
        $Order->setOrderDate(new \DateTime());
1224 23
        $Order->setOrderStatus($this->app['eccube.repository.order_status']->find($status));
1225
1226 23
        $event = new EventArgs(
1227
            array(
1228 23
                'Order' => $Order,
1229
            ),
1230 23
            null
1231
        );
1232 23
        $this->app['eccube.event.dispatcher']->dispatch(EccubeEvents::SERVICE_SHOPPING_ORDER_STATUS, $event);
1233
1234 23
        return $Order;
1235
1236
    }
1237
1238
    /**
1239
     * 受注メール送信を行う
1240
     *
1241
     * @param Order $Order
1242
     * @return MailHistory
1243
     */
1244 22
    public function sendOrderMail(Order $Order)
1245
    {
1246
1247
        // メール送信
1248 22
        $message = $this->app['eccube.service.mail']->sendOrderMail($Order);
1249
1250
        // 送信履歴を保存.
1251 22
        $MailTemplate = $this->app['eccube.repository.mail_template']->find(1);
1252
1253 22
        $MailHistory = new MailHistory();
1254
        $MailHistory
1255 22
            ->setSubject($message->getSubject())
1256 22
            ->setMailBody($message->getBody())
1257 22
            ->setMailTemplate($MailTemplate)
1258 22
            ->setSendDate(new \DateTime())
1259 22
            ->setOrder($Order);
1260
1261 22
        $this->app['orm.em']->persist($MailHistory);
1262 22
        $this->app['orm.em']->flush($MailHistory);
1263
1264 22
        return $MailHistory;
1265
1266
    }
1267
1268
1269
    /**
1270
     * 受注処理完了通知
1271
     *
1272
     * @param Order $Order
1273
     */
1274
    public function notifyComplete(Order $Order)
1275
    {
1276
1277
        $event = new EventArgs(
1278
            array(
1279
                'Order' => $Order,
1280
            ),
1281
            null
1282
        );
1283
        $this->app['eccube.event.dispatcher']->dispatch(EccubeEvents::SERVICE_SHOPPING_NOTIFY_COMPLETE, $event);
1284
1285
    }
1286
1287
1288
}
1289