Failed Conditions
Push — master ( 746c21...bb3715 )
by Kentaro
35:35
created

ShoppingService::getDeliveriesCart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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