ShoppingService::isDiscount()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
ccs 0
cts 4
cp 0
crap 6
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 119
    public function __construct(Application $app, $cartService, $orderService)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
64
    {
65 119
        $this->app = $app;
66 119
        $this->cartService = $cartService;
67 119
        $this->orderService = $orderService;
68 119
        $this->BaseInfo = $app['eccube.repository.base_info']->get();
69
    }
70
71
    /**
72
     * セッションにセットされた受注情報を取得
73
     *
74
     * @param null $status
75
     * @return null|object
76
     */
77 96
    public function getOrder($status = null)
78
    {
79
80
        // 受注データを取得
81 96
        $preOrderId = $this->cartService->getPreOrderId();
82 96
        if (!$preOrderId) {
83 91
            return null;
84
        }
85
86
        $condition = array(
87 82
            'pre_order_id' => $preOrderId,
88
        );
89
90 82
        if (!is_null($status)) {
91
            $condition += array(
92 79
                'OrderStatus' => $status,
93
            );
94
        }
95
96 82
        $Order = $this->app['eccube.repository.order']->findOneBy($condition);
97
98 82
        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 33
    public function getNonMember($sesisonKey)
109
    {
110
111
        // 非会員でも一度会員登録されていればショッピング画面へ遷移
112 33
        $nonMember = $this->app['session']->get($sesisonKey);
113 33
        if (is_null($nonMember)) {
114 1
            return null;
115
        }
116 32
        if (!array_key_exists('customer', $nonMember) || !array_key_exists('pref', $nonMember)) {
117
            return null;
118
        }
119
120 32
        $Customer = $nonMember['customer'];
121 32
        $Customer->setPref($this->app['eccube.repository.master.pref']->find($nonMember['pref']));
122
123 32
        return $Customer;
124
125
    }
126
127
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
128
     * 受注情報を作成
129
     *
130
     * @param $Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
131
     * @return \Eccube\Entity\Order
132
     */
133 98
    public function createOrder($Customer)
134
    {
135
        // ランダムなpre_order_idを作成
136
        do {
137 98
            $preOrderId = sha1(Str::random(32));
138 98
            $Order = $this->app['eccube.repository.order']->findOneBy(array(
139 98
                'pre_order_id' => $preOrderId,
140 98
                'OrderStatus' => $this->app['config']['order_processing'],
141
            ));
142 98
        } while ($Order);
143
144
        // 受注情報、受注明細情報、お届け先情報、配送商品情報を作成
145 98
        $Order = $this->registerPreOrder(
146
            $Customer,
147
            $preOrderId);
148
149 97
        $this->cartService->setPreOrderId($preOrderId);
150 97
        $this->cartService->save();
151
152 97
        return $Order;
153
    }
154
155
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
introduced by
Doc comment for parameter "$preOrderId" missing
Loading history...
156
     * 仮受注情報作成
157
     *
158
     * @param $Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
159
     * @param $preOrderId
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
160
     * @return mixed
161
     * @throws \Doctrine\ORM\NoResultException
162
     * @throws \Doctrine\ORM\NonUniqueResultException
163
     */
164 98
    public function registerPreOrder(Customer $Customer, $preOrderId)
165
    {
166
167 98
        $this->em = $this->app['orm.em'];
168
169
        // 受注情報を作成
170 98
        $Order = $this->getNewOrder($Customer);
171 98
        $Order->setPreOrderId($preOrderId);
172
173
        // device type
174 98
        if ($this->app['mobile_detect']->isMobile()) {
175
            $DeviceType = $this->app['eccube.repository.master.device_type']->find(DeviceType::DEVICE_TYPE_SP);
176
        } else {
177 98
            $DeviceType = $this->app['eccube.repository.master.device_type']->find(DeviceType::DEVICE_TYPE_PC);
178
        }
179 98
        $Order->setDeviceType($DeviceType);
180
181 98
        $this->em->persist($Order);
182
183
        // 配送業者情報を取得
184 98
        $deliveries = $this->getDeliveriesCart();
185
186
        // お届け先情報を作成
187 98
        $Order = $this->getNewShipping($Order, $Customer, $deliveries);
188
189
        // 受注明細情報、配送商品情報を作成
190 98
        $Order = $this->getNewDetails($Order);
191
192
        // 小計
193 97
        $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...
194
195
        // 消費税のみの小計
196 97
        $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...
197
198
        // 配送料合計金額
199 97
        $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($Order->getShippings()));
200
201
        // 小計
202 97
        $Order->setSubTotal($subTotal);
203
204
        // 配送料無料条件(合計金額)
205 97
        $this->setDeliveryFreeAmount($Order);
206
207
        // 配送料無料条件(合計数量)
208 97
        $this->setDeliveryFreeQuantity($Order);
209
210
        // 初期選択の支払い方法をセット
211 97
        $payments = $this->app['eccube.repository.payment']->findAllowedPayments($deliveries);
212 97
        $payments = $this->getPayments($payments, $subTotal);
213
214 97
        if (count($payments) > 0) {
215 96
            $payment = $payments[0];
216 96
            $Order->setPayment($payment);
217 96
            $Order->setPaymentMethod($payment->getMethod());
218 96
            $Order->setCharge($payment->getCharge());
219
        } else {
220 1
            $Order->setCharge(0);
221
        }
222
223 97
        $Order->setTax($tax);
224
225
        // 合計金額の計算
226 97
        $this->calculatePrice($Order);
227
228 97
        $this->em->flush();
229
230 97
        return $Order;
231
232
    }
233
234
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
235
     * 受注情報を作成
236
     *
237
     * @param $Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
238
     * @return \Eccube\Entity\Order
239
     */
240 98
    public function getNewOrder(Customer $Customer)
241
    {
242 98
        $Order = $this->newOrder();
243 98
        $this->copyToOrderFromCustomer($Order, $Customer);
244
245 98
        return $Order;
246
    }
247
248
249
    /**
250
     * 受注情報を作成
251
     *
252
     * @return \Eccube\Entity\Order
253
     */
254 98
    public function newOrder()
255
    {
256 98
        $OrderStatus = $this->app['eccube.repository.order_status']->find($this->app['config']['order_processing']);
257 98
        $Order = new \Eccube\Entity\Order($OrderStatus);
258
259 98
        return $Order;
260
    }
261
262
    /**
263
     * 受注情報を作成
264
     *
265
     * @param \Eccube\Entity\Order $Order
0 ignored issues
show
introduced by
Expected 9 spaces after parameter type; 1 found
Loading history...
266
     * @param \Eccube\Entity\Customer|null $Customer
267
     * @return \Eccube\Entity\Order
268
     */
269 98
    public function copyToOrderFromCustomer(Order $Order, Customer $Customer = null)
270
    {
271 98
        if (is_null($Customer)) {
272
            return $Order;
273
        }
274
275 98
        if ($Customer->getId()) {
276 65
            $Order->setCustomer($Customer);
277
        }
278
        $Order
279 98
            ->setName01($Customer->getName01())
280 98
            ->setName02($Customer->getName02())
281 98
            ->setKana01($Customer->getKana01())
282 98
            ->setKana02($Customer->getKana02())
283 98
            ->setCompanyName($Customer->getCompanyName())
284 98
            ->setEmail($Customer->getEmail())
285 98
            ->setTel01($Customer->getTel01())
286 98
            ->setTel02($Customer->getTel02())
287 98
            ->setTel03($Customer->getTel03())
288 98
            ->setFax01($Customer->getFax01())
289 98
            ->setFax02($Customer->getFax02())
290 98
            ->setFax03($Customer->getFax03())
291 98
            ->setZip01($Customer->getZip01())
292 98
            ->setZip02($Customer->getZip02())
293 98
            ->setZipCode($Customer->getZip01().$Customer->getZip02())
294 98
            ->setPref($Customer->getPref())
295 98
            ->setAddr01($Customer->getAddr01())
296 98
            ->setAddr02($Customer->getAddr02())
297 98
            ->setSex($Customer->getSex())
298 98
            ->setBirth($Customer->getBirth())
299 98
            ->setJob($Customer->getJob());
300
301 98
        return $Order;
302
    }
303
304
305
    /**
306
     * 配送業者情報を取得
307
     *
308
     * @return array
309
     */
310 98
    public function getDeliveriesCart()
311
    {
312
313
        // カートに保持されている商品種別を取得
314 98
        $productTypes = $this->cartService->getProductTypes();
315
316 98
        return $this->getDeliveries($productTypes);
317
318
    }
319
320
    /**
321
     * 配送業者情報を取得
322
     *
323
     * @param Order $Order
324
     * @return array
325
     */
326 89
    public function getDeliveriesOrder(Order $Order)
327
    {
328
329
        // 受注情報から商品種別を取得
330 89
        $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...
331
332 89
        return $this->getDeliveries($productTypes);
333
334
    }
335
336
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$productTypes" missing
Loading history...
337
     * 配送業者情報を取得
338
     *
339
     * @param $productTypes
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
340
     * @return array
341
     */
342 101
    public function getDeliveries($productTypes)
343
    {
344
345
        // 商品種別に紐づく配送業者を取得
346 101
        $deliveries = $this->app['eccube.repository.delivery']->getDeliveries($productTypes);
347
348 101
        if ($this->BaseInfo->getOptionMultipleShipping() == Constant::ENABLED) {
349
            // 複数配送対応
350
351
            // 支払方法を取得
352 42
            $payments = $this->app['eccube.repository.payment']->findAllowedPayments($deliveries);
353
354 42
            if (count($productTypes) > 1) {
355
                // 商品種別が複数ある場合、配送対象となる配送業者を取得
356 6
                $deliveries = $this->app['eccube.repository.delivery']->findAllowedDeliveries($productTypes, $payments);
357
            }
358
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
359
        }
360
361 101
        return $deliveries;
362
363
    }
364
365
366
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$deliveries" missing
Loading history...
367
     * お届け先情報を作成
368
     *
369
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
370
     * @param Customer $Customer
0 ignored issues
show
introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
371
     * @param $deliveries
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
372
     * @return Order
373
     */
374 98
    public function getNewShipping(Order $Order, Customer $Customer, $deliveries)
375
    {
376 98
        $productTypes = array();
377 98
        foreach ($deliveries as $Delivery) {
378 97
            if (!in_array($Delivery->getProductType()->getId(), $productTypes)) {
379 97
                $Shipping = new Shipping();
380
381 97
                $this->copyToShippingFromCustomer($Shipping, $Customer)
382 97
                    ->setOrder($Order)
383 97
                    ->setDelFlg(Constant::DISABLED);
384
385
                // 配送料金の設定
386 97
                $this->setShippingDeliveryFee($Shipping, $Delivery);
387
388 97
                $this->em->persist($Shipping);
389
390 97
                $Order->addShipping($Shipping);
391
392 98
                $productTypes[] = $Delivery->getProductType()->getId();
393
            }
394
        }
395
396 98
        return $Order;
397
    }
398
399
    /**
400
     * お届け先情報を作成
401
     *
402
     * @param \Eccube\Entity\Shipping $Shipping
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
403
     * @param \Eccube\Entity\Customer|null $Customer
404
     * @return \Eccube\Entity\Shipping
405
     */
406 98
    public function copyToShippingFromCustomer(Shipping $Shipping, Customer $Customer = null)
407
    {
408 98
        if (is_null($Customer)) {
409 1
            return $Shipping;
410
        }
411
412 97
        $CustomerAddress = $this->app['eccube.repository.customer_address']->findOneBy(
413 97
            array('Customer' => $Customer),
414 97
            array('id' => 'ASC')
415
        );
416
417 97
        if (!is_null($CustomerAddress)) {
418
            $Shipping
419 64
                ->setName01($CustomerAddress->getName01())
420 64
                ->setName02($CustomerAddress->getName02())
421 64
                ->setKana01($CustomerAddress->getKana01())
422 64
                ->setKana02($CustomerAddress->getKana02())
423 64
                ->setCompanyName($CustomerAddress->getCompanyName())
424 64
                ->setTel01($CustomerAddress->getTel01())
425 64
                ->setTel02($CustomerAddress->getTel02())
426 64
                ->setTel03($CustomerAddress->getTel03())
427 64
                ->setFax01($CustomerAddress->getFax01())
428 64
                ->setFax02($CustomerAddress->getFax02())
429 64
                ->setFax03($CustomerAddress->getFax03())
430 64
                ->setZip01($CustomerAddress->getZip01())
431 64
                ->setZip02($CustomerAddress->getZip02())
432 64
                ->setZipCode($CustomerAddress->getZip01().$CustomerAddress->getZip02())
433 64
                ->setPref($CustomerAddress->getPref())
434 64
                ->setAddr01($CustomerAddress->getAddr01())
435 64
                ->setAddr02($CustomerAddress->getAddr02());
436
        } else {
437
            $Shipping
438 33
                ->setName01($Customer->getName01())
439 33
                ->setName02($Customer->getName02())
440 33
                ->setKana01($Customer->getKana01())
441 33
                ->setKana02($Customer->getKana02())
442 33
                ->setCompanyName($Customer->getCompanyName())
443 33
                ->setTel01($Customer->getTel01())
444 33
                ->setTel02($Customer->getTel02())
445 33
                ->setTel03($Customer->getTel03())
446 33
                ->setFax01($Customer->getFax01())
447 33
                ->setFax02($Customer->getFax02())
448 33
                ->setFax03($Customer->getFax03())
449 33
                ->setZip01($Customer->getZip01())
450 33
                ->setZip02($Customer->getZip02())
451 33
                ->setZipCode($Customer->getZip01().$Customer->getZip02())
452 33
                ->setPref($Customer->getPref())
453 33
                ->setAddr01($Customer->getAddr01())
454 33
                ->setAddr02($Customer->getAddr02());
455
        }
456
457 97
        return $Shipping;
458
    }
459
460
461
    /**
462
     * 受注明細情報、配送商品情報を作成
463
     *
464
     * @param Order $Order
465
     * @return Order
466
     */
467 98
    public function getNewDetails(Order $Order)
468
    {
469
470
        // 受注詳細, 配送商品
471 98
        foreach ($this->cartService->getCart()->getCartItems() as $item) {
472
            /* @var $ProductClass \Eccube\Entity\ProductClass */
473 98
            $ProductClass = $item->getObject();
474
            /* @var $Product \Eccube\Entity\Product */
475 98
            $Product = $ProductClass->getProduct();
476
477 98
            $quantity = $item->getQuantity();
478
479
            // 受注明細情報を作成
480 98
            $OrderDetail = $this->getNewOrderDetail($Product, $ProductClass, $quantity);
481 98
            $OrderDetail->setOrder($Order);
482 98
            $Order->addOrderDetail($OrderDetail);
483
484
            // 配送商品情報を作成
485 98
            $this->getNewShipmentItem($Order, $Product, $ProductClass, $quantity);
486
        }
487
488 97
        return $Order;
489
490
    }
491
492
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$quantity" missing
Loading history...
493
     * 受注明細情報を作成
494
     *
495
     * @param Product $Product
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
496
     * @param ProductClass $ProductClass
497
     * @param $quantity
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
498
     * @return \Eccube\Entity\OrderDetail
499
     */
500 98
    public function getNewOrderDetail(Product $Product, ProductClass $ProductClass, $quantity)
501
    {
502 98
        $OrderDetail = new OrderDetail();
503 98
        $TaxRule = $this->app['eccube.repository.tax_rule']->getByRule($Product, $ProductClass);
504 98
        $OrderDetail->setProduct($Product)
505 98
            ->setProductClass($ProductClass)
506 98
            ->setProductName($Product->getName())
507 98
            ->setProductCode($ProductClass->getCode())
508 98
            ->setPrice($ProductClass->getPrice02())
509 98
            ->setQuantity($quantity)
510 98
            ->setTaxRule($TaxRule->getCalcRule()->getId())
511 98
            ->setTaxRate($TaxRule->getTaxRate());
512
513 98
        $ClassCategory1 = $ProductClass->getClassCategory1();
514 98
        if (!is_null($ClassCategory1)) {
515 98
            $OrderDetail->setClasscategoryName1($ClassCategory1->getName());
516 98
            $OrderDetail->setClassName1($ClassCategory1->getClassName()->getName());
517
        }
518 98
        $ClassCategory2 = $ProductClass->getClassCategory2();
519 98
        if (!is_null($ClassCategory2)) {
520 86
            $OrderDetail->setClasscategoryName2($ClassCategory2->getName());
521 86
            $OrderDetail->setClassName2($ClassCategory2->getClassName()->getName());
522
        }
523
524 98
        $this->em->persist($OrderDetail);
525
526 98
        return $OrderDetail;
527
    }
528
529
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$quantity" missing
Loading history...
530
     * 配送商品情報を作成
531
     *
532
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
533
     * @param Product $Product
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
534
     * @param ProductClass $ProductClass
535
     * @param $quantity
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
536
     * @return \Eccube\Entity\ShipmentItem
537
     */
538 98
    public function getNewShipmentItem(Order $Order, Product $Product, ProductClass $ProductClass, $quantity)
539
    {
540
541 98
        $ShipmentItem = new ShipmentItem();
542 98
        $shippings = $Order->getShippings();
543
544
        // 選択された商品がどのお届け先情報と関連するかチェック
545 98
        $Shipping = null;
546 98
        foreach ($shippings as $s) {
547 97
            if ($s->getDelivery()->getProductType()->getId() == $ProductClass->getProductType()->getId()) {
548
                // 商品種別が同一のお届け先情報と関連させる
549 97
                $Shipping = $s;
550 98
                break;
551
            }
552
        }
553
554 98
        if (is_null($Shipping)) {
555
            // お届け先情報と関連していない場合、エラー
556 1
            throw new CartException('shopping.delivery.not.producttype');
557
        }
558
559
        // 商品ごとの配送料合計
560 97
        $productDeliveryFeeTotal = 0;
561 97
        if ($this->BaseInfo->getOptionProductDeliveryFee() === Constant::ENABLED) {
562
            $productDeliveryFeeTotal = $ProductClass->getDeliveryFee() * $quantity;
563
        }
564
565 97
        $Shipping->setShippingDeliveryFee($Shipping->getShippingDeliveryFee() + $productDeliveryFeeTotal);
566
567 97
        $ShipmentItem->setShipping($Shipping)
568 97
            ->setOrder($Order)
569 97
            ->setProductClass($ProductClass)
570 97
            ->setProduct($Product)
571 97
            ->setProductName($Product->getName())
572 97
            ->setProductCode($ProductClass->getCode())
573 97
            ->setPrice($ProductClass->getPrice02())
574 97
            ->setQuantity($quantity);
575
576 97
        $ClassCategory1 = $ProductClass->getClassCategory1();
577 97
        if (!is_null($ClassCategory1)) {
578 97
            $ShipmentItem->setClasscategoryName1($ClassCategory1->getName());
579 97
            $ShipmentItem->setClassName1($ClassCategory1->getClassName()->getName());
580
        }
581 97
        $ClassCategory2 = $ProductClass->getClassCategory2();
582 97
        if (!is_null($ClassCategory2)) {
583 86
            $ShipmentItem->setClasscategoryName2($ClassCategory2->getName());
584 86
            $ShipmentItem->setClassName2($ClassCategory2->getClassName()->getName());
585
        }
586 97
        $Shipping->addShipmentItem($ShipmentItem);
587 97
        $this->em->persist($ShipmentItem);
588
589 97
        return $ShipmentItem;
590
591
    }
592
593
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$shippings" missing
Loading history...
594
     * お届け先ごとの送料合計を取得
595
     *
596
     * @param $shippings
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
597
     * @return int
598
     */
599 98
    public function getShippingDeliveryFeeTotal($shippings)
600
    {
601 98
        $deliveryFeeTotal = 0;
602 98
        foreach ($shippings as $Shipping) {
603 98
            $deliveryFeeTotal += $Shipping->getShippingDeliveryFee();
604
        }
605
606 98
        return $deliveryFeeTotal;
607
608
    }
609
610
    /**
611
     * 商品ごとの配送料を取得
612
     *
613
     * @param Shipping $Shipping
614
     * @return int
615
     */
616
    public function getProductDeliveryFee(Shipping $Shipping)
617
    {
618
        $productDeliveryFeeTotal = 0;
619
        $shipmentItems = $Shipping->getShipmentItems();
620
        foreach ($shipmentItems as $ShipmentItem) {
621
            $productDeliveryFeeTotal += $ShipmentItem->getProductClass()->getDeliveryFee() * $ShipmentItem->getQuantity();
622
        }
623
624
        return $productDeliveryFeeTotal;
625
    }
626
627
    /**
628
     * 住所などの情報が変更された時に金額の再計算を行う
629
     *
630
     * @param Order $Order
631
     * @return Order
632
     */
633 43 View Code Duplication
    public function getAmount(Order $Order)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
634
    {
635
636
        // 初期選択の配送業者をセット
637 43
        $shippings = $Order->getShippings();
638
639
        // 配送料合計金額
640 43
        $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($shippings));
641
642
        // 配送料無料条件(合計金額)
643 43
        $this->setDeliveryFreeAmount($Order);
644
645
        // 配送料無料条件(合計数量)
646 43
        $this->setDeliveryFreeQuantity($Order);
647
648
        // 合計金額の計算
649 43
        $this->calculatePrice($Order);
650
651 43
        return $Order;
652
653
    }
654
655
    /**
656
     * 配送料金の設定
657
     *
658
     * @param Shipping $Shipping
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
659
     * @param Delivery|null $Delivery
660
     */
661 97
    public function setShippingDeliveryFee(Shipping $Shipping, Delivery $Delivery = null)
662
    {
663
        // 配送料金の設定
664 97
        if (is_null($Delivery)) {
665 33
            $Delivery = $Shipping->getDelivery();
666
        }
667 97
        $deliveryFee = $this->app['eccube.repository.delivery_fee']->findOneBy(array('Delivery' => $Delivery, 'Pref' => $Shipping->getPref()));
668
669 97
        $Shipping->setDeliveryFee($deliveryFee);
670 97
        $Shipping->setDelivery($Delivery);
671
672
        // 商品ごとの配送料合計
673 97
        $productDeliveryFeeTotal = 0;
674 97
        if ($this->BaseInfo->getOptionProductDeliveryFee() === Constant::ENABLED) {
675
            $productDeliveryFeeTotal += $this->getProductDeliveryFee($Shipping);
676
        }
677
678 97
        $Shipping->setShippingDeliveryFee($deliveryFee->getFee() + $productDeliveryFeeTotal);
679 97
        $Shipping->setShippingDeliveryName($Delivery->getName());
680
    }
681
682
    /**
683
     * 配送料無料条件(合計金額)の条件を満たしていれば配送料金を0に設定
684
     *
685
     * @param Order $Order
686
     */
687 99 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...
688
    {
689
        // 配送料無料条件(合計金額)
690 99
        $deliveryFreeAmount = $this->BaseInfo->getDeliveryFreeAmount();
691 99
        if (!is_null($deliveryFreeAmount)) {
692
            // 合計金額が設定金額以上であれば送料無料
693 1
            if ($Order->getSubTotal() >= $deliveryFreeAmount) {
694 1
                $Order->setDeliveryFeeTotal(0);
695
                // お届け先情報の配送料も0にセット
696 1
                $shippings = $Order->getShippings();
697 1
                foreach ($shippings as $Shipping) {
698 1
                    $Shipping->setShippingDeliveryFee(0);
699
                }
700
            }
701
        }
702
    }
703
704
    /**
705
     * 配送料無料条件(合計数量)の条件を満たしていれば配送料金を0に設定
706
     *
707
     * @param Order $Order
708
     */
709 99 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...
710
    {
711
        // 配送料無料条件(合計数量)
712 99
        $deliveryFreeQuantity = $this->BaseInfo->getDeliveryFreeQuantity();
713 99
        if (!is_null($deliveryFreeQuantity)) {
714
            // 合計数量が設定数量以上であれば送料無料
715 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...
716 1
                $Order->setDeliveryFeeTotal(0);
717
                // お届け先情報の配送料も0にセット
718 1
                $shippings = $Order->getShippings();
719 1
                foreach ($shippings as $Shipping) {
720 1
                    $Shipping->setShippingDeliveryFee(0);
721
                }
722
            }
723
        }
724
    }
725
726
727
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$em" missing
Loading history...
728
     * 商品公開ステータスチェック、在庫チェック、購入制限数チェックを行い、在庫情報をロックする
729
     *
730
     * @param $em トランザクション制御されているEntityManager
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
731
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 57 spaces after parameter type; 1 found
Loading history...
732
     * @return bool true : 成功、false : 失敗
733
     */
734 27
    public function isOrderProduct($em, \Eccube\Entity\Order $Order)
735
    {
736 27
        $orderDetails = $Order->getOrderDetails();
737
738 27
        foreach ($orderDetails as $orderDetail) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
739
740
            // 商品削除チェック
741 27
            if ($orderDetail->getProductClass()->getDelFlg()) {
742
                // @deprecated 3.1以降ではexceptionをthrowする
743
                // throw new ShoppingException('cart.product.delete');
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
744
                return false;
745
            }
746
747
            // 商品公開ステータスチェック
748 27
            if ($orderDetail->getProduct()->getStatus()->getId() != \Eccube\Entity\Master\Disp::DISPLAY_SHOW) {
749
                // 商品が非公開ならエラー
750
751
                // @deprecated 3.1以降ではexceptionをthrowする
752
                // throw new ShoppingException('cart.product.not.status');
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
753 1
                return false;
754
            }
755
756
            // 購入制限数チェック
757 26
            if (!is_null($orderDetail->getProductClass()->getSaleLimit())) {
758 2
                if ($orderDetail->getQuantity() > $orderDetail->getProductClass()->getSaleLimit()) {
759
                    // @deprecated 3.1以降ではexceptionをthrowする
760
                    // throw new ShoppingException('cart.over.sale_limit');
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
761 1
                    return false;
762
                }
763
            }
764
765
            // 購入数チェック
766 25
            if ($orderDetail->getQuantity() < 1) {
767
                // 購入数量が1未満ならエラー
768
769
                // @deprecated 3.1以降ではexceptionをthrowする
770
                // throw new ShoppingException('???');
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
771 25
                return false;
772
            }
773
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
774
        }
775
776
        // 在庫チェック
777 25
        foreach ($orderDetails as $orderDetail) {
778
            // 在庫が無制限かチェックし、制限ありなら在庫数をチェック
779 25
            if ($orderDetail->getProductClass()->getStockUnlimited() == Constant::DISABLED) {
780
                // 在庫チェックあり
781
                // 在庫に対してロック(select ... for update)を実行
782 15
                $productStock = $em->getRepository('Eccube\Entity\ProductStock')->find(
783 15
                    $orderDetail->getProductClass()->getProductStock()->getId(), LockMode::PESSIMISTIC_WRITE
784
                );
785
                // 購入数量と在庫数をチェックして在庫がなければエラー
786 15
                if ($productStock->getStock() < 1) {
787
                    // @deprecated 3.1以降ではexceptionをthrowする
788
                    // throw new ShoppingException('cart.over.stock');
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
789
                    return false;
790 15
                } elseif ($orderDetail->getQuantity() > $productStock->getStock()) {
791
                    // @deprecated 3.1以降ではexceptionをthrowする
792
                    // throw new ShoppingException('cart.over.stock');
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
793 25
                    return false;
794
                }
795
            }
796
        }
797
798 24
        return true;
799
800
    }
801
802
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$data" missing
Loading history...
803
     * 受注情報、お届け先情報の更新
804
     *
805
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 22 spaces after parameter type; 1 found
Loading history...
806
     * @param $data フォームデータ
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
807
     *
808
     * @deprecated since 3.0.5, to be removed in 3.1
809
     */
810
    public function setOrderUpdate(Order $Order, $data)
811
    {
812
        // 受注情報を更新
813
        $Order->setOrderDate(new \DateTime());
814
        $Order->setOrderStatus($this->app['eccube.repository.order_status']->find($this->app['config']['order_new']));
815
        $Order->setMessage($data['message']);
816
        // お届け先情報を更新
817
        $shippings = $data['shippings'];
818
        foreach ($shippings as $Shipping) {
819
            $Delivery = $Shipping->getDelivery();
820
            $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...
821
                'Delivery' => $Delivery,
822
                'Pref' => $Shipping->getPref()
823
            ));
824
            $deliveryTime = $Shipping->getDeliveryTime();
825
            if (!empty($deliveryTime)) {
826
                $Shipping->setShippingDeliveryTime($deliveryTime->getDeliveryTime());
827
            }
828
            $Shipping->setDeliveryFee($deliveryFee);
829
            // 商品ごとの配送料合計
830
            $productDeliveryFeeTotal = 0;
831
            if ($this->BaseInfo->getOptionProductDeliveryFee() === Constant::ENABLED) {
832
                $productDeliveryFeeTotal += $this->getProductDeliveryFee($Shipping);
833
            }
834
            $Shipping->setShippingDeliveryFee($deliveryFee->getFee() + $productDeliveryFeeTotal);
835
            $Shipping->setShippingDeliveryName($Delivery->getName());
836
        }
837
        // 配送料無料条件(合計金額)
838
        $this->setDeliveryFreeAmount($Order);
839
        // 配送料無料条件(合計数量)
840
        $this->setDeliveryFreeQuantity($Order);
841
    }
842
843
844
    /**
845
     * 受注情報の更新
846
     *
847
     * @param Order $Order 受注情報
848
     */
849 24
    public function setOrderUpdateData(Order $Order)
850
    {
851
        // 受注情報を更新
852 24
        $Order->setOrderDate(new \DateTime());
853 24
        $OrderStatus = $this->app['eccube.repository.order_status']->find($this->app['config']['order_new']);
854 24
        $this->setOrderStatus($Order, $OrderStatus);
855
856
    }
857
858
859
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$em" missing
Loading history...
860
     * 在庫情報の更新
861
     *
862
     * @param $em トランザクション制御されているEntityManager
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
863
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 57 spaces after parameter type; 1 found
Loading history...
864
     */
865 24
    public function setStockUpdate($em, Order $Order)
866
    {
867
868 24
        $orderDetails = $Order->getOrderDetails();
869
870
        // 在庫情報更新
871 24
        foreach ($orderDetails as $orderDetail) {
872
            // 在庫が無制限かチェックし、制限ありなら在庫数を更新
873 24
            if ($orderDetail->getProductClass()->getStockUnlimited() == Constant::DISABLED) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
874
875 15
                $productStock = $em->getRepository('Eccube\Entity\ProductStock')->find(
876 15
                    $orderDetail->getProductClass()->getProductStock()->getId()
877
                );
878
879
                // 在庫情報の在庫数を更新
880 15
                $stock = $productStock->getStock() - $orderDetail->getQuantity();
881 15
                $productStock->setStock($stock);
882
883
                // 商品規格情報の在庫数を更新
884 24
                $orderDetail->getProductClass()->setStock($stock);
885
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
886
            }
887
        }
888
889
    }
890
891
892
    /**
893
     * 会員情報の更新
894
     *
895
     * @param Order $Order 受注情報
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
896
     * @param Customer $user ログインユーザ
0 ignored issues
show
introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
897
     */
898 16
    public function setCustomerUpdate(Order $Order, Customer $user)
899
    {
900
901 16
        $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...
902
903
        // 顧客情報を更新
904 16
        $now = new \DateTime();
905 16
        $firstBuyDate = $user->getFirstBuyDate();
906 16
        if (empty($firstBuyDate)) {
907 16
            $user->setFirstBuyDate($now);
908
        }
909 16
        $user->setLastBuyDate($now);
910
911 16
        $user->setBuyTimes($user->getBuyTimes() + 1);
912 16
        $user->setBuyTotal($user->getBuyTotal() + $Order->getTotal());
913
914
    }
915
916
917
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$payments" missing
Loading history...
introduced by
Doc comment for parameter "$subTotal" missing
Loading history...
918
     * 支払方法選択の表示設定
919
     *
920
     * @param $payments 支払選択肢情報
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
921
     * @param $subTotal 小計
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
922
     * @return array
923
     */
924 100
    public function getPayments($payments, $subTotal)
925
    {
926 100
        $pays = array();
927 100
        foreach ($payments as $payment) {
928
            // 支払方法の制限値内であれば表示
929 100
            if (!is_null($payment)) {
930 100
                $pay = $this->app['eccube.repository.payment']->find($payment['id']);
931 100
                if (intval($pay->getRuleMin()) <= $subTotal) {
932 100
                    if (is_null($pay->getRuleMax()) || $pay->getRuleMax() >= $subTotal) {
933 100
                        $pays[] = $pay;
934
                    }
935
                }
936
            }
937
        }
938
939 100
        return $pays;
940
941
    }
942
943
    /**
944
     * お届け日を取得
945
     *
946
     * @param Order $Order
947
     * @return array
948
     */
949 91
    public function getFormDeliveryDates(Order $Order)
950
    {
951
952
        // お届け日の設定
953 91
        $minDate = 0;
954 91
        $deliveryDateFlag = false;
955
956
        // 配送時に最大となる商品日数を取得
957 91
        foreach ($Order->getOrderDetails() as $detail) {
958 91
            $deliveryDate = $detail->getProductClass()->getDeliveryDate();
959 91
            if (!is_null($deliveryDate)) {
960 43
                if ($deliveryDate->getValue() < 0) {
961
                    // 配送日数がマイナスの場合はお取り寄せなのでスキップする
962 9
                    $deliveryDateFlag = false;
963 9
                    break;
964
                }
965
966 38
                if ($minDate < $deliveryDate->getValue()) {
967 36
                    $minDate = $deliveryDate->getValue();
968
                }
969
                // 配送日数が設定されている
970 91
                $deliveryDateFlag = true;
971
            }
972
        }
973
974
        // 配達最大日数期間を設定
975 91
        $deliveryDates = array();
976
977
        // 配送日数が設定されている
978 91
        if ($deliveryDateFlag) {
979 34
            $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...
980 34
                new \DateTime($minDate.' day'),
981 34
                new \DateInterval('P1D'),
982 34
                new \DateTime($minDate + $this->app['config']['deliv_date_end_max'].' day')
983
            );
984
985 34
            foreach ($period as $day) {
986 34
                $deliveryDates[$day->format('Y/m/d')] = $day->format('Y/m/d');
987
            }
988
        }
989
990 91
        return $deliveryDates;
991
992
    }
993
994
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$deliveries" missing
Loading history...
995
     * 支払方法を取得
996
     *
997
     * @param $deliveries
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
998
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
999
     * @return array
1000
     */
1001 91
    public function getFormPayments($deliveries, Order $Order)
1002
    {
1003 91
        $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...
1004
1005 91
        if ($this->BaseInfo->getOptionMultipleShipping() == Constant::ENABLED && count($productTypes) > 1) {
1006
            // 複数配送時の支払方法
1007 5
            $payments = $this->app['eccube.repository.payment']->findAllowedPayments($deliveries);
1008
        } else {
1009
            // 配送業者をセット
1010 86
            $shippings = $Order->getShippings();
1011 86
            $payments = array();
1012 86
            foreach ($shippings as $Shipping) {
1013 86
                $paymentsShip = $this->app['eccube.repository.payment']->findPayments($Shipping->getDelivery(), true);
1014 86
                if (!$payments) {
1015 86
                    $payments = $paymentsShip;
1016
                } else {
1017 86
                    $payments = array_intersect($payments, $paymentsShip);
1018
                }
1019
            }
1020
        }
1021 91
        $payments = $this->getPayments($payments, $Order->getSubTotal());
1022
1023 91
        return $payments;
1024
    }
1025
1026
    /**
1027
     * お届け先ごとにFormを作成
1028
     *
1029
     * @param Order $Order
1030
     * @return \Symfony\Component\Form\Form
1031
     * @deprecated since 3.0, to be removed in 3.1
1032
     */
1033 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...
1034
    {
1035
        $message = $Order->getMessage();
1036
1037
        $deliveries = $this->getDeliveriesOrder($Order);
1038
1039
        // 配送業者の支払方法を取得
1040
        $payments = $this->getFormPayments($deliveries, $Order);
1041
1042
        $builder = $this->app['form.factory']->createBuilder('shopping', null, array(
1043
            'payments' => $payments,
1044
            'payment' => $Order->getPayment(),
1045
            'message' => $message,
1046
        ));
1047
1048
        $builder
1049
            ->add('shippings', 'collection', array(
1050
                'type' => 'shipping_item',
1051
                'data' => $Order->getShippings(),
1052
            ));
1053
1054
        $form = $builder->getForm();
1055
1056
        return $form;
1057
1058
    }
1059
1060
    /**
1061
     * お届け先ごとにFormBuilderを作成
1062
     *
1063
     * @param Order $Order
1064
     * @return \Symfony\Component\Form\FormBuilderInterface
1065
     */
1066 89 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...
1067
    {
1068 89
        $message = $Order->getMessage();
1069
1070 89
        $deliveries = $this->getDeliveriesOrder($Order);
1071
1072
        // 配送業者の支払方法を取得
1073 89
        $payments = $this->getFormPayments($deliveries, $Order);
1074
1075 89
        $builder = $this->app['form.factory']->createBuilder('shopping', null, array(
1076 89
            'payments' => $payments,
1077 89
            'payment' => $Order->getPayment(),
1078 89
            'message' => $message,
1079
        ));
1080
1081
        $builder
1082 89
            ->add('shippings', 'collection', array(
1083 89
                'type' => 'shipping_item',
1084 89
                'data' => $Order->getShippings(),
1085
            ));
1086
1087 89
        return $builder;
1088
1089
    }
1090
1091
1092
    /**
1093
     * フォームデータを更新
1094
     *
1095
     * @param Order $Order
1096
     * @param array $data
1097
     */
1098 24
    public function setFormData(Order $Order, array $data)
1099
    {
1100
1101
        // お問い合わせ
1102 24
        $Order->setMessage($data['message']);
1103
1104
        // お届け先情報を更新
1105 24
        $shippings = $data['shippings'];
1106 24
        foreach ($shippings as $Shipping) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
1107
1108 24
            $deliveryTime = $Shipping->getDeliveryTime();
1109 24
            if (!empty($deliveryTime)) {
1110 24
                $Shipping->setShippingDeliveryTime($deliveryTime->getDeliveryTime());
1111
            }
1112
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
1113
        }
1114
1115
    }
1116
1117
    /**
1118
     * 配送料の合計金額を計算
1119
     *
1120
     * @param Order $Order
1121
     * @return Order
1122
     */
1123 23 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...
1124
    {
1125
1126
        // 配送業者を取得
1127 23
        $shippings = $Order->getShippings();
1128
1129
        // 配送料合計金額
1130 23
        $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($shippings));
1131
1132
        // 配送料無料条件(合計金額)
1133 23
        $this->setDeliveryFreeAmount($Order);
1134
1135
        // 配送料無料条件(合計数量)
1136 23
        $this->setDeliveryFreeQuantity($Order);
1137
1138 23
        return $Order;
1139
1140
    }
1141
1142
1143
    /**
1144
     * 購入処理を行う
1145
     *
1146
     * @param Order $Order
1147
     * @throws ShoppingException
1148
     */
1149 23
    public function processPurchase(Order $Order)
1150
    {
1151
1152 23
        $em = $this->app['orm.em'];
1153
1154
        // 合計金額の再計算
1155 23
        $this->calculatePrice($Order);
1156
1157
        // 商品公開ステータスチェック、商品制限数チェック、在庫チェック
1158 23
        $check = $this->isOrderProduct($em, $Order);
1159 23
        if (!$check) {
1160
            throw new ShoppingException('front.shopping.stock.error');
1161
        }
1162
1163
        // 受注情報、配送情報を更新
1164 23
        $Order = $this->calculateDeliveryFee($Order);
1165 23
        $this->setOrderUpdateData($Order);
1166
        // 在庫情報を更新
1167 23
        $this->setStockUpdate($em, $Order);
1168
1169 23
        if ($this->app->isGranted('ROLE_USER')) {
1170
            // 会員の場合、購入金額を更新
1171 15
            $this->setCustomerUpdate($Order, $this->app->user());
1172
        }
1173
1174
    }
1175
1176
1177
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$discount" missing
Loading history...
1178
     * 値引き可能かチェック
1179
     *
1180
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
1181
     * @param       $discount
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
1182
     * @return bool
1183
     */
1184
    public function isDiscount(Order $Order, $discount)
1185
    {
1186
1187
        if ($Order->getTotal() < $discount) {
1188
            return false;
1189
        }
1190
1191
        return true;
1192
    }
1193
1194
1195
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$discount" missing
Loading history...
1196
     * 値引き金額をセット
1197
     *
1198
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
1199
     * @param $discount
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
1200
     */
1201
    public function setDiscount(Order $Order, $discount)
1202
    {
1203
1204
        $Order->setDiscount($Order->getDiscount() + $discount);
1205
1206
    }
1207
1208
1209
    /**
1210
     * 合計金額を計算
1211
     *
1212
     * @param Order $Order
1213
     * @return Order
1214
     */
1215 98
    public function calculatePrice(Order $Order)
1216
    {
1217
1218 98
        $total = $Order->getTotalPrice();
1219
1220 98
        if ($total < 0) {
1221
            // 合計金額がマイナスの場合、0を設定し、discountは値引きされた額のみセット
1222
            $total = 0;
1223
        }
1224
1225 98
        $Order->setTotal($total);
1226 98
        $Order->setPaymentTotal($total);
1227
1228 98
        return $Order;
1229
1230
    }
1231
1232
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$status" missing
Loading history...
1233
     * 受注ステータスをセット
1234
     *
1235
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
1236
     * @param $status
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
1237
     * @return Order
1238
     */
1239 24
    public function setOrderStatus(Order $Order, $status)
1240
    {
1241
1242 24
        $Order->setOrderDate(new \DateTime());
1243 24
        $Order->setOrderStatus($this->app['eccube.repository.order_status']->find($status));
1244
1245 24
        $event = new EventArgs(
1246
            array(
1247 24
                'Order' => $Order,
1248
            ),
1249 24
            null
1250
        );
1251 24
        $this->app['eccube.event.dispatcher']->dispatch(EccubeEvents::SERVICE_SHOPPING_ORDER_STATUS, $event);
1252
1253 24
        return $Order;
1254
1255
    }
1256
1257
    /**
1258
     * 受注メール送信を行う
1259
     *
1260
     * @param Order $Order
1261
     * @return MailHistory
1262
     */
1263 23
    public function sendOrderMail(Order $Order)
1264
    {
1265
1266
        // メール送信
1267 23
        $message = $this->app['eccube.service.mail']->sendOrderMail($Order);
1268
1269
        // 送信履歴を保存.
1270 23
        $MailTemplate = $this->app['eccube.repository.mail_template']->find(1);
1271
1272 23
        $MailHistory = new MailHistory();
1273
        $MailHistory
1274 23
            ->setSubject($message->getSubject())
1275 23
            ->setMailBody($message->getBody())
1276 23
            ->setMailTemplate($MailTemplate)
1277 23
            ->setSendDate(new \DateTime())
1278 23
            ->setOrder($Order);
1279
1280 23
        $this->app['orm.em']->persist($MailHistory);
1281 23
        $this->app['orm.em']->flush($MailHistory);
1282
1283 23
        return $MailHistory;
1284
1285
    }
1286
1287
1288
    /**
1289
     * 受注処理完了通知
1290
     *
1291
     * @param Order $Order
1292
     */
1293
    public function notifyComplete(Order $Order)
1294
    {
1295
1296
        $event = new EventArgs(
1297
            array(
1298
                'Order' => $Order,
1299
            ),
1300
            null
1301
        );
1302
        $this->app['eccube.event.dispatcher']->dispatch(EccubeEvents::SERVICE_SHOPPING_NOTIFY_COMPLETE, $event);
1303
1304
    }
1305
1306
1307
}
1308