Completed
Push — master ( a12a0c...804439 )
by Yangsin
53:53 queued 02:17
created

ShoppingService   D

Complexity

Total Complexity 110

Size/Duplication

Total Lines 1259
Duplicated Lines 9.61 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 95.82%

Importance

Changes 0
Metric Value
dl 121
loc 1259
ccs 413
cts 431
cp 0.9582
rs 4.4102
c 0
b 0
f 0
wmc 110
lcom 1
cbo 8

41 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B getOrder() 0 24 3
A getNonMember() 0 15 2
A createOrder() 0 21 2
A registerPreOrder() 0 69 3
A getNewOrder() 0 7 1
A newOrder() 0 7 1
B copyToOrderFromCustomer() 0 34 3
A getDeliveriesCart() 0 9 1
A getDeliveriesOrder() 0 9 1
A getDeliveries() 0 22 3
B getNewShipping() 0 24 3
A copyToShippingFromCustomer() 0 53 3
B getNewDetails() 0 24 2
B getNewOrderDetail() 0 28 3
A getShippingDeliveryFeeTotal() 0 10 2
A getProductDeliveryFee() 0 10 2
A getAmount() 21 21 1
A setShippingDeliveryFee() 0 20 3
A setDeliveryFreeAmount() 16 16 4
A setDeliveryFreeQuantity() 16 16 4
B getNewShipmentItem() 0 54 7
C isOrderProduct() 0 69 12
B setOrderUpdate() 0 32 4
A setOrderUpdateData() 0 8 1
B setStockUpdate() 0 25 3
A setCustomerUpdate() 0 17 2
B getPayments() 0 18 6
C getFormDeliveryDates() 0 44 7
A getFormPayments() 0 22 3
B getShippingForm() 26 26 1
B getShippingFormBuilder() 24 24 1
A setFormData() 0 18 3
A calculateDeliveryFee() 18 18 1
B processPurchase() 0 26 3
A isDiscount() 0 9 2
A setDiscount() 0 6 1
A calculatePrice() 0 16 2
A setOrderStatus() 0 17 1
A sendOrderMail() 0 23 1
A notifyComplete() 0 12 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like ShoppingService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ShoppingService, and based on these observations, apply Extract Interface, too.

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 115
    public function __construct(Application $app, $cartService, $orderService)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
64
    {
65 115
        $this->app = $app;
66 115
        $this->cartService = $cartService;
67 115
        $this->orderService = $orderService;
68 115
        $this->BaseInfo = $app['eccube.repository.base_info']->get();
69
    }
70
71
    /**
72
     * セッションにセットされた受注情報を取得
73
     *
74
     * @param null $status
75
     * @return null|object
76
     */
77 92
    public function getOrder($status = null)
78
    {
79
80
        // 受注データを取得
81 92
        $preOrderId = $this->cartService->getPreOrderId();
82 92
        if (!$preOrderId) {
83 87
            return null;
84
        }
85
86
        $condition = array(
87 79
            'pre_order_id' => $preOrderId,
88
        );
89
90 79
        if (!is_null($status)) {
91
            $condition += array(
92 76
                'OrderStatus' => $status,
93
            );
94
        }
95
96 79
        $Order = $this->app['eccube.repository.order']->findOneBy($condition);
97
98 79
        return $Order;
99
100
    }
101
102
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$sesisonKey" missing
Loading history...
103
     * 非会員情報を取得
104
     *
105
     * @param $sesisonKey
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
106
     * @return $Customer|null
0 ignored issues
show
Documentation introduced by
The doc-type $Customer|null could not be parsed: Unknown type name "$Customer" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
107
     */
108 32
    public function getNonMember($sesisonKey)
109
    {
110
111
        // 非会員でも一度会員登録されていればショッピング画面へ遷移
112 32
        $nonMember = $this->app['session']->get($sesisonKey);
113 32
        if (is_null($nonMember)) {
114 1
            return null;
115
        }
116
117 31
        $Customer = $nonMember['customer'];
118 31
        $Customer->setPref($this->app['eccube.repository.master.pref']->find($nonMember['pref']));
119
120 31
        return $Customer;
121
122
    }
123
124
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
125
     * 受注情報を作成
126
     *
127
     * @param $Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
128
     * @return \Eccube\Entity\Order
129
     */
130 94
    public function createOrder($Customer)
131
    {
132
        // ランダムなpre_order_idを作成
133
        do {
134 94
            $preOrderId = sha1(Str::random(32));
135 94
            $Order = $this->app['eccube.repository.order']->findOneBy(array(
136 94
                'pre_order_id' => $preOrderId,
137 94
                'OrderStatus' => $this->app['config']['order_processing'],
138
            ));
139 94
        } while ($Order);
140
141
        // 受注情報、受注明細情報、お届け先情報、配送商品情報を作成
142 94
        $Order = $this->registerPreOrder(
143
            $Customer,
144
            $preOrderId);
145
146 93
        $this->cartService->setPreOrderId($preOrderId);
147 93
        $this->cartService->save();
148
149 93
        return $Order;
150
    }
151
152
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$preOrderId" missing
Loading history...
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
153
     * 仮受注情報作成
154
     *
155
     * @param $Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
156
     * @param $preOrderId
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
157
     * @return mixed
158
     * @throws \Doctrine\ORM\NoResultException
159
     * @throws \Doctrine\ORM\NonUniqueResultException
160
     */
161 94
    public function registerPreOrder(Customer $Customer, $preOrderId)
162
    {
163
164 94
        $this->em = $this->app['orm.em'];
165
166
        // 受注情報を作成
167 94
        $Order = $this->getNewOrder($Customer);
168 94
        $Order->setPreOrderId($preOrderId);
169
170
        // device type
171 94
        if ($this->app['mobile_detect']->isMobile()) {
172
            $DeviceType = $this->app['eccube.repository.master.device_type']->find(DeviceType::DEVICE_TYPE_SP);
173
        } else {
174 94
            $DeviceType = $this->app['eccube.repository.master.device_type']->find(DeviceType::DEVICE_TYPE_PC);
175
        }
176 94
        $Order->setDeviceType($DeviceType);
177
178 94
        $this->em->persist($Order);
179
180
        // 配送業者情報を取得
181 94
        $deliveries = $this->getDeliveriesCart();
182
183
        // お届け先情報を作成
184 94
        $Order = $this->getNewShipping($Order, $Customer, $deliveries);
185
186
        // 受注明細情報、配送商品情報を作成
187 94
        $Order = $this->getNewDetails($Order);
188
189
        // 小計
190 93
        $subTotal = $this->orderService->getSubTotal($Order);
0 ignored issues
show
Deprecated Code introduced by
The method Eccube\Service\OrderService::getSubTotal() has been deprecated with message: since 3.0.0, to be removed in 3.1

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
191
192
        // 消費税のみの小計
193 93
        $tax = $this->orderService->getTotalTax($Order);
0 ignored issues
show
Deprecated Code introduced by
The method Eccube\Service\OrderService::getTotalTax() has been deprecated with message: since 3.0.0, to be removed in 3.1

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
194
195
        // 配送料合計金額
196 93
        $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($Order->getShippings()));
197
198
        // 小計
199 93
        $Order->setSubTotal($subTotal);
200
201
        // 配送料無料条件(合計金額)
202 93
        $this->setDeliveryFreeAmount($Order);
203
204
        // 配送料無料条件(合計数量)
205 93
        $this->setDeliveryFreeQuantity($Order);
206
207
        // 初期選択の支払い方法をセット
208 93
        $payments = $this->app['eccube.repository.payment']->findAllowedPayments($deliveries);
209 93
        $payments = $this->getPayments($payments, $subTotal);
210
211 93
        if (count($payments) > 0) {
212 93
            $payment = $payments[0];
213 93
            $Order->setPayment($payment);
214 93
            $Order->setPaymentMethod($payment->getMethod());
215 93
            $Order->setCharge($payment->getCharge());
216
        } else {
217
            $Order->setCharge(0);
218
        }
219
220 93
        $Order->setTax($tax);
221
222
        // 合計金額の計算
223 93
        $this->calculatePrice($Order);
224
225 93
        $this->em->flush();
226
227 93
        return $Order;
228
229
    }
230
231
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
232
     * 受注情報を作成
233
     *
234
     * @param $Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
235
     * @return \Eccube\Entity\Order
236
     */
237 94
    public function getNewOrder(Customer $Customer)
238
    {
239 94
        $Order = $this->newOrder();
240 94
        $this->copyToOrderFromCustomer($Order, $Customer);
241
242 94
        return $Order;
243
    }
244
245
246
    /**
247
     * 受注情報を作成
248
     *
249
     * @return \Eccube\Entity\Order
250
     */
251 94
    public function newOrder()
252
    {
253 94
        $OrderStatus = $this->app['eccube.repository.order_status']->find($this->app['config']['order_processing']);
254 94
        $Order = new \Eccube\Entity\Order($OrderStatus);
255
256 94
        return $Order;
257
    }
258
259
    /**
260
     * 受注情報を作成
261
     *
262
     * @param \Eccube\Entity\Order $Order
0 ignored issues
show
introduced by
Expected 9 spaces after parameter type; 1 found
Loading history...
263
     * @param \Eccube\Entity\Customer|null $Customer
264
     * @return \Eccube\Entity\Order
265
     */
266 94
    public function copyToOrderFromCustomer(Order $Order, Customer $Customer = null)
267
    {
268 94
        if (is_null($Customer)) {
269
            return $Order;
270
        }
271
272 94
        if ($Customer->getId()) {
273 62
            $Order->setCustomer($Customer);
274
        }
275
        $Order
276 94
            ->setName01($Customer->getName01())
277 94
            ->setName02($Customer->getName02())
278 94
            ->setKana01($Customer->getKana01())
279 94
            ->setKana02($Customer->getKana02())
280 94
            ->setCompanyName($Customer->getCompanyName())
281 94
            ->setEmail($Customer->getEmail())
282 94
            ->setTel01($Customer->getTel01())
283 94
            ->setTel02($Customer->getTel02())
284 94
            ->setTel03($Customer->getTel03())
285 94
            ->setFax01($Customer->getFax01())
286 94
            ->setFax02($Customer->getFax02())
287 94
            ->setFax03($Customer->getFax03())
288 94
            ->setZip01($Customer->getZip01())
289 94
            ->setZip02($Customer->getZip02())
290 94
            ->setZipCode($Customer->getZip01().$Customer->getZip02())
291 94
            ->setPref($Customer->getPref())
292 94
            ->setAddr01($Customer->getAddr01())
293 94
            ->setAddr02($Customer->getAddr02())
294 94
            ->setSex($Customer->getSex())
295 94
            ->setBirth($Customer->getBirth())
296 94
            ->setJob($Customer->getJob());
297
298 94
        return $Order;
299
    }
300
301
302
    /**
303
     * 配送業者情報を取得
304
     *
305
     * @return array
306
     */
307 94
    public function getDeliveriesCart()
308
    {
309
310
        // カートに保持されている商品種別を取得
311 94
        $productTypes = $this->cartService->getProductTypes();
312
313 94
        return $this->getDeliveries($productTypes);
314
315
    }
316
317
    /**
318
     * 配送業者情報を取得
319
     *
320
     * @param Order $Order
321
     * @return array
322
     */
323 85
    public function getDeliveriesOrder(Order $Order)
324
    {
325
326
        // 受注情報から商品種別を取得
327 85
        $productTypes = $this->orderService->getProductTypes($Order);
0 ignored issues
show
Deprecated Code introduced by
The method Eccube\Service\OrderService::getProductTypes() has been deprecated with message: since 3.0.0, to be removed in 3.1

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
328
329 85
        return $this->getDeliveries($productTypes);
330
331
    }
332
333
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$productTypes" missing
Loading history...
334
     * 配送業者情報を取得
335
     *
336
     * @param $productTypes
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
337
     * @return array
338
     */
339 97
    public function getDeliveries($productTypes)
340
    {
341
342
        // 商品種別に紐づく配送業者を取得
343 97
        $deliveries = $this->app['eccube.repository.delivery']->getDeliveries($productTypes);
344
345 97
        if ($this->BaseInfo->getOptionMultipleShipping() == Constant::ENABLED) {
346
            // 複数配送対応
347
348
            // 支払方法を取得
349 42
            $payments = $this->app['eccube.repository.payment']->findAllowedPayments($deliveries);
350
351 42
            if (count($productTypes) > 1) {
352
                // 商品種別が複数ある場合、配送対象となる配送業者を取得
353 6
                $deliveries = $this->app['eccube.repository.delivery']->findAllowedDeliveries($productTypes, $payments);
354
            }
355
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
356
        }
357
358 97
        return $deliveries;
359
360
    }
361
362
363
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$deliveries" missing
Loading history...
364
     * お届け先情報を作成
365
     *
366
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
367
     * @param Customer $Customer
0 ignored issues
show
introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
368
     * @param $deliveries
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
369
     * @return Order
370
     */
371 94
    public function getNewShipping(Order $Order, Customer $Customer, $deliveries)
372
    {
373 94
        $productTypes = array();
374 94
        foreach ($deliveries as $Delivery) {
375 93
            if (!in_array($Delivery->getProductType()->getId(), $productTypes)) {
376 93
                $Shipping = new Shipping();
377
378 93
                $this->copyToShippingFromCustomer($Shipping, $Customer)
379 93
                    ->setOrder($Order)
380 93
                    ->setDelFlg(Constant::DISABLED);
381
382
                // 配送料金の設定
383 93
                $this->setShippingDeliveryFee($Shipping, $Delivery);
384
385 93
                $this->em->persist($Shipping);
386
387 93
                $Order->addShipping($Shipping);
388
389 94
                $productTypes[] = $Delivery->getProductType()->getId();
390
            }
391
        }
392
393 94
        return $Order;
394
    }
395
396
    /**
397
     * お届け先情報を作成
398
     *
399
     * @param \Eccube\Entity\Shipping $Shipping
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
400
     * @param \Eccube\Entity\Customer|null $Customer
401
     * @return \Eccube\Entity\Shipping
402
     */
403 94
    public function copyToShippingFromCustomer(Shipping $Shipping, Customer $Customer = null)
404
    {
405 94
        if (is_null($Customer)) {
406 1
            return $Shipping;
407
        }
408
409 93
        $CustomerAddress = $this->app['eccube.repository.customer_address']->findOneBy(
410 93
            array('Customer' => $Customer),
411 93
            array('id' => 'ASC')
412
        );
413
414 93
        if (!is_null($CustomerAddress)) {
415
            $Shipping
416 61
                ->setName01($CustomerAddress->getName01())
417 61
                ->setName02($CustomerAddress->getName02())
418 61
                ->setKana01($CustomerAddress->getKana01())
419 61
                ->setKana02($CustomerAddress->getKana02())
420 61
                ->setCompanyName($CustomerAddress->getCompanyName())
421 61
                ->setTel01($CustomerAddress->getTel01())
422 61
                ->setTel02($CustomerAddress->getTel02())
423 61
                ->setTel03($CustomerAddress->getTel03())
424 61
                ->setFax01($CustomerAddress->getFax01())
425 61
                ->setFax02($CustomerAddress->getFax02())
426 61
                ->setFax03($CustomerAddress->getFax03())
427 61
                ->setZip01($CustomerAddress->getZip01())
428 61
                ->setZip02($CustomerAddress->getZip02())
429 61
                ->setZipCode($CustomerAddress->getZip01().$CustomerAddress->getZip02())
430 61
                ->setPref($CustomerAddress->getPref())
431 61
                ->setAddr01($CustomerAddress->getAddr01())
432 61
                ->setAddr02($CustomerAddress->getAddr02());
433
        } else {
434
            $Shipping
435 32
                ->setName01($Customer->getName01())
436 32
                ->setName02($Customer->getName02())
437 32
                ->setKana01($Customer->getKana01())
438 32
                ->setKana02($Customer->getKana02())
439 32
                ->setCompanyName($Customer->getCompanyName())
440 32
                ->setTel01($Customer->getTel01())
441 32
                ->setTel02($Customer->getTel02())
442 32
                ->setTel03($Customer->getTel03())
443 32
                ->setFax01($Customer->getFax01())
444 32
                ->setFax02($Customer->getFax02())
445 32
                ->setFax03($Customer->getFax03())
446 32
                ->setZip01($Customer->getZip01())
447 32
                ->setZip02($Customer->getZip02())
448 32
                ->setZipCode($Customer->getZip01().$Customer->getZip02())
449 32
                ->setPref($Customer->getPref())
450 32
                ->setAddr01($Customer->getAddr01())
451 32
                ->setAddr02($Customer->getAddr02());
452
        }
453
454 93
        return $Shipping;
455
    }
456
457
458
    /**
459
     * 受注明細情報、配送商品情報を作成
460
     *
461
     * @param Order $Order
462
     * @return Order
463
     */
464 94
    public function getNewDetails(Order $Order)
465
    {
466
467
        // 受注詳細, 配送商品
468 94
        foreach ($this->cartService->getCart()->getCartItems() as $item) {
469
            /* @var $ProductClass \Eccube\Entity\ProductClass */
470 94
            $ProductClass = $item->getObject();
471
            /* @var $Product \Eccube\Entity\Product */
472 94
            $Product = $ProductClass->getProduct();
473
474 94
            $quantity = $item->getQuantity();
475
476
            // 受注明細情報を作成
477 94
            $OrderDetail = $this->getNewOrderDetail($Product, $ProductClass, $quantity);
478 94
            $OrderDetail->setOrder($Order);
479 94
            $Order->addOrderDetail($OrderDetail);
480
481
            // 配送商品情報を作成
482 94
            $this->getNewShipmentItem($Order, $Product, $ProductClass, $quantity);
483
        }
484
485 93
        return $Order;
486
487
    }
488
489
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$quantity" missing
Loading history...
490
     * 受注明細情報を作成
491
     *
492
     * @param Product $Product
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
493
     * @param ProductClass $ProductClass
494
     * @param $quantity
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
495
     * @return \Eccube\Entity\OrderDetail
496
     */
497 94
    public function getNewOrderDetail(Product $Product, ProductClass $ProductClass, $quantity)
498
    {
499 94
        $OrderDetail = new OrderDetail();
500 94
        $TaxRule = $this->app['eccube.repository.tax_rule']->getByRule($Product, $ProductClass);
501 94
        $OrderDetail->setProduct($Product)
502 94
            ->setProductClass($ProductClass)
503 94
            ->setProductName($Product->getName())
504 94
            ->setProductCode($ProductClass->getCode())
505 94
            ->setPrice($ProductClass->getPrice02())
506 94
            ->setQuantity($quantity)
507 94
            ->setTaxRule($TaxRule->getCalcRule()->getId())
508 94
            ->setTaxRate($TaxRule->getTaxRate());
509
510 94
        $ClassCategory1 = $ProductClass->getClassCategory1();
511 94
        if (!is_null($ClassCategory1)) {
512 94
            $OrderDetail->setClasscategoryName1($ClassCategory1->getName());
513 94
            $OrderDetail->setClassName1($ClassCategory1->getClassName()->getName());
514
        }
515 94
        $ClassCategory2 = $ProductClass->getClassCategory2();
516 94
        if (!is_null($ClassCategory2)) {
517 84
            $OrderDetail->setClasscategoryName2($ClassCategory2->getName());
518 84
            $OrderDetail->setClassName2($ClassCategory2->getClassName()->getName());
519
        }
520
521 94
        $this->em->persist($OrderDetail);
522
523 94
        return $OrderDetail;
524
    }
525
526
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$quantity" missing
Loading history...
527
     * 配送商品情報を作成
528
     *
529
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
530
     * @param Product $Product
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
531
     * @param ProductClass $ProductClass
532
     * @param $quantity
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
533
     * @return \Eccube\Entity\ShipmentItem
534
     */
535 94
    public function getNewShipmentItem(Order $Order, Product $Product, ProductClass $ProductClass, $quantity)
536
    {
537
538 94
        $ShipmentItem = new ShipmentItem();
539 94
        $shippings = $Order->getShippings();
540
541
        // 選択された商品がどのお届け先情報と関連するかチェック
542 94
        $Shipping = null;
543 94
        foreach ($shippings as $s) {
544 93
            if ($s->getDelivery()->getProductType()->getId() == $ProductClass->getProductType()->getId()) {
545
                // 商品種別が同一のお届け先情報と関連させる
546 93
                $Shipping = $s;
547 94
                break;
548
            }
549
        }
550
551 94
        if (is_null($Shipping)) {
552
            // お届け先情報と関連していない場合、エラー
553 1
            throw new CartException('shopping.delivery.not.producttype');
554
        }
555
556
        // 商品ごとの配送料合計
557 93
        $productDeliveryFeeTotal = 0;
558 93
        if (!is_null($this->BaseInfo->getOptionProductDeliveryFee())) {
559 93
            $productDeliveryFeeTotal = $ProductClass->getDeliveryFee() * $quantity;
560
        }
561
562 93
        $Shipping->setShippingDeliveryFee($Shipping->getShippingDeliveryFee() + $productDeliveryFeeTotal);
563
564 93
        $ShipmentItem->setShipping($Shipping)
565 93
            ->setOrder($Order)
566 93
            ->setProductClass($ProductClass)
567 93
            ->setProduct($Product)
568 93
            ->setProductName($Product->getName())
569 93
            ->setProductCode($ProductClass->getCode())
570 93
            ->setPrice($ProductClass->getPrice02())
571 93
            ->setQuantity($quantity);
572
573 93
        $ClassCategory1 = $ProductClass->getClassCategory1();
574 93
        if (!is_null($ClassCategory1)) {
575 93
            $ShipmentItem->setClasscategoryName1($ClassCategory1->getName());
576 93
            $ShipmentItem->setClassName1($ClassCategory1->getClassName()->getName());
577
        }
578 93
        $ClassCategory2 = $ProductClass->getClassCategory2();
579 93
        if (!is_null($ClassCategory2)) {
580 83
            $ShipmentItem->setClasscategoryName2($ClassCategory2->getName());
581 83
            $ShipmentItem->setClassName2($ClassCategory2->getClassName()->getName());
582
        }
583 93
        $Shipping->addShipmentItem($ShipmentItem);
584 93
        $this->em->persist($ShipmentItem);
585
586 93
        return $ShipmentItem;
587
588
    }
589
590
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$shippings" missing
Loading history...
591
     * お届け先ごとの送料合計を取得
592
     *
593
     * @param $shippings
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
594
     * @return int
595
     */
596 94
    public function getShippingDeliveryFeeTotal($shippings)
597
    {
598 94
        $deliveryFeeTotal = 0;
599 94
        foreach ($shippings as $Shipping) {
600 94
            $deliveryFeeTotal += $Shipping->getShippingDeliveryFee();
601
        }
602
603 94
        return $deliveryFeeTotal;
604
605
    }
606
607
    /**
608
     * 商品ごとの配送料を取得
609
     *
610
     * @param Shipping $Shipping
611
     * @return int
612
     */
613 93
    public function getProductDeliveryFee(Shipping $Shipping)
614
    {
615 93
        $productDeliveryFeeTotal = 0;
616 93
        $shipmentItems = $Shipping->getShipmentItems();
617 93
        foreach ($shipmentItems as $ShipmentItem) {
618 93
            $productDeliveryFeeTotal += $ShipmentItem->getProductClass()->getDeliveryFee() * $ShipmentItem->getQuantity();
619
        }
620
621 93
        return $productDeliveryFeeTotal;
622
    }
623
624
    /**
625
     * 住所などの情報が変更された時に金額の再計算を行う
626
     *
627
     * @param Order $Order
628
     * @return Order
629
     */
630 43 View Code Duplication
    public function getAmount(Order $Order)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
631
    {
632
633
        // 初期選択の配送業者をセット
634 43
        $shippings = $Order->getShippings();
635
636
        // 配送料合計金額
637 43
        $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($shippings));
638
639
        // 配送料無料条件(合計金額)
640 43
        $this->setDeliveryFreeAmount($Order);
641
642
        // 配送料無料条件(合計数量)
643 43
        $this->setDeliveryFreeQuantity($Order);
644
645
        // 合計金額の計算
646 43
        $this->calculatePrice($Order);
647
648 43
        return $Order;
649
650
    }
651
652
    /**
653
     * 配送料金の設定
654
     *
655
     * @param Shipping $Shipping
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
656
     * @param Delivery|null $Delivery
657
     */
658 93
    public function setShippingDeliveryFee(Shipping $Shipping, Delivery $Delivery = null)
659
    {
660
        // 配送料金の設定
661 93
        if (is_null($Delivery)) {
662 33
            $Delivery = $Shipping->getDelivery();
663
        }
664 93
        $deliveryFee = $this->app['eccube.repository.delivery_fee']->findOneBy(array('Delivery' => $Delivery, 'Pref' => $Shipping->getPref()));
665
666 93
        $Shipping->setDeliveryFee($deliveryFee);
667 93
        $Shipping->setDelivery($Delivery);
668
669
        // 商品ごとの配送料合計
670 93
        $productDeliveryFeeTotal = 0;
671 93
        if (!is_null($this->BaseInfo->getOptionProductDeliveryFee())) {
672 93
            $productDeliveryFeeTotal += $this->getProductDeliveryFee($Shipping);
673
        }
674
675 93
        $Shipping->setShippingDeliveryFee($deliveryFee->getFee() + $productDeliveryFeeTotal);
676 93
        $Shipping->setShippingDeliveryName($Delivery->getName());
677
    }
678
679
    /**
680
     * 配送料無料条件(合計金額)の条件を満たしていれば配送料金を0に設定
681
     *
682
     * @param Order $Order
683
     */
684 95 View Code Duplication
    public function setDeliveryFreeAmount(Order $Order)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
685
    {
686
        // 配送料無料条件(合計金額)
687 95
        $deliveryFreeAmount = $this->BaseInfo->getDeliveryFreeAmount();
688 95
        if (!is_null($deliveryFreeAmount)) {
689
            // 合計金額が設定金額以上であれば送料無料
690 1
            if ($Order->getSubTotal() >= $deliveryFreeAmount) {
691 1
                $Order->setDeliveryFeeTotal(0);
692
                // お届け先情報の配送料も0にセット
693 1
                $shippings = $Order->getShippings();
694 1
                foreach ($shippings as $Shipping) {
695 1
                    $Shipping->setShippingDeliveryFee(0);
696
                }
697
            }
698
        }
699
    }
700
701
    /**
702
     * 配送料無料条件(合計数量)の条件を満たしていれば配送料金を0に設定
703
     *
704
     * @param Order $Order
705
     */
706 95 View Code Duplication
    public function setDeliveryFreeQuantity(Order $Order)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
707
    {
708
        // 配送料無料条件(合計数量)
709 95
        $deliveryFreeQuantity = $this->BaseInfo->getDeliveryFreeQuantity();
710 95
        if (!is_null($deliveryFreeQuantity)) {
711
            // 合計数量が設定数量以上であれば送料無料
712 1
            if ($this->orderService->getTotalQuantity($Order) >= $deliveryFreeQuantity) {
0 ignored issues
show
Deprecated Code introduced by
The method Eccube\Service\OrderService::getTotalQuantity() has been deprecated with message: since 3.0.0, to be removed in 3.1

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

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