Failed Conditions
Push — master ( 9c9d2b...a2b736 )
by chihiro
43:30
created

ShoppingService::getFormPayments()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

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