Failed Conditions
Pull Request — master (#1665)
by k-yamamura
74:32
created

ShoppingService::getFormDeliveryDates()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 44
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 7

Importance

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