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