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(OrderStatusRepository::class) |
||
| 118 | * @var OrderStatusRepository |
||
| 119 | */ |
||
| 120 | protected $orderStatusRepository; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @Inject(PaymentRepository::class) |
||
| 124 | * @var PaymentRepository |
||
| 125 | */ |
||
| 126 | protected $paymentRepository; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @Inject(DeviceTypeRepository::class) |
||
| 130 | * @var DeviceTypeRepository |
||
| 131 | */ |
||
| 132 | protected $deviceTypeRepository; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @Inject("orm.em") |
||
| 136 | * @var EntityManager |
||
| 137 | */ |
||
| 138 | protected $entityManager; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @Inject("config") |
||
| 142 | * @var array |
||
| 143 | */ |
||
| 144 | protected $appConfig; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @Inject(PrefRepository::class) |
||
| 148 | * @var PrefRepository |
||
| 149 | */ |
||
| 150 | protected $prefRepository; |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @Inject("session") |
||
| 154 | * @var Session |
||
| 155 | */ |
||
| 156 | protected $session; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @Inject(OrderRepository::class) |
||
| 160 | * @var OrderRepository |
||
| 161 | */ |
||
| 162 | protected $orderRepository; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @Inject(BaseInfo::class) |
||
| 166 | * @var BaseInfo |
||
| 167 | */ |
||
| 168 | protected $BaseInfo; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @Inject(Application::class) |
||
| 172 | * @var \Eccube\Application |
||
| 173 | */ |
||
| 174 | public $app; |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @Inject(CartService::class) |
||
| 178 | * @var \Eccube\Service\CartService |
||
| 179 | */ |
||
| 180 | protected $cartService; |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @var \Eccube\Service\OrderService |
||
| 184 | * |
||
| 185 | * @deprecated |
||
| 186 | */ |
||
| 187 | protected $orderService; |
||
| 188 | |||
| 189 | /** |
||
| 190 | * セッションにセットされた受注情報を取得 |
||
| 191 | * |
||
| 192 | * @param null $status |
||
| 193 | * @return null|object |
||
| 194 | */ |
||
| 195 | public function getOrder($status = null) |
||
| 196 | { |
||
| 197 | |||
| 198 | // 受注データを取得 |
||
| 199 | $preOrderId = $this->cartService->getPreOrderId(); |
||
| 200 | if (!$preOrderId) { |
||
| 201 | return null; |
||
| 202 | } |
||
| 203 | |||
| 204 | $condition = array( |
||
| 205 | 'pre_order_id' => $preOrderId, |
||
| 206 | ); |
||
| 207 | |||
| 208 | if (!is_null($status)) { |
||
| 209 | $condition += array( |
||
| 210 | 'OrderStatus' => $status, |
||
| 211 | ); |
||
| 212 | } |
||
| 213 | |||
| 214 | $Order = $this->orderRepository->findOneBy($condition); |
||
| 215 | |||
| 216 | return $Order; |
||
| 217 | |||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
|
|
|||
| 221 | * 非会員情報を取得 |
||
| 222 | * |
||
| 223 | * @param $sesisonKey |
||
| 224 | * @return $Customer|null |
||
| 225 | */ |
||
| 226 | public function getNonMember($sesisonKey) |
||
| 227 | { |
||
| 228 | |||
| 229 | // 非会員でも一度会員登録されていればショッピング画面へ遷移 |
||
| 230 | $nonMember = $this->session->get($sesisonKey); |
||
| 231 | if (is_null($nonMember)) { |
||
| 232 | return null; |
||
| 233 | } |
||
| 234 | if (!array_key_exists('customer', $nonMember) || !array_key_exists('pref', $nonMember)) { |
||
| 235 | return null; |
||
| 236 | } |
||
| 237 | |||
| 238 | $Customer = $nonMember['customer']; |
||
| 239 | $Customer->setPref($this->prefRepository->find($nonMember['pref'])); |
||
| 240 | |||
| 241 | foreach ($Customer->getCustomerAddresses() as $CustomerAddress) { |
||
| 242 | $Pref = $CustomerAddress->getPref(); |
||
| 243 | if ($Pref) { |
||
| 244 | $CustomerAddress->setPref($this->prefRepository->find($Pref->getId())); |
||
| 245 | } |
||
| 246 | } |
||
| 247 | |||
| 248 | return $Customer; |
||
| 249 | |||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * 受注情報を作成 |
||
| 254 | * |
||
| 255 | * @param $Customer |
||
| 256 | * @return \Eccube\Entity\Order |
||
| 257 | */ |
||
| 258 | public function createOrder($Customer) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * 仮受注情報作成 |
||
| 282 | * |
||
| 283 | * @param $Customer |
||
| 284 | * @param $preOrderId |
||
| 285 | * @return mixed |
||
| 286 | * @throws \Doctrine\ORM\NoResultException |
||
| 287 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
| 288 | */ |
||
| 289 | public function registerPreOrder(Customer $Customer, $preOrderId) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * 受注情報を作成 |
||
| 357 | * |
||
| 358 | * @param $Customer |
||
| 359 | * @return \Eccube\Entity\Order |
||
| 360 | */ |
||
| 361 | public function getNewOrder(Customer $Customer) |
||
| 368 | |||
| 369 | |||
| 370 | /** |
||
| 371 | * 受注情報を作成 |
||
| 372 | * |
||
| 373 | * @return \Eccube\Entity\Order |
||
| 374 | */ |
||
| 375 | public function newOrder() |
||
| 382 | |||
| 383 | /** |
||
| 384 | * 受注情報を作成 |
||
| 385 | * |
||
| 386 | * @param \Eccube\Entity\Order $Order |
||
| 387 | * @param \Eccube\Entity\Customer|null $Customer |
||
| 388 | * @return \Eccube\Entity\Order |
||
| 389 | */ |
||
| 390 | public function copyToOrderFromCustomer(Order $Order, Customer $Customer = null) |
||
| 424 | |||
| 425 | |||
| 426 | /** |
||
| 427 | * 配送業者情報を取得 |
||
| 428 | * |
||
| 429 | * @return array |
||
| 430 | */ |
||
| 431 | public function getDeliveriesCart() |
||
| 440 | |||
| 441 | /** |
||
| 442 | * 配送業者情報を取得 |
||
| 443 | * |
||
| 444 | * @param Order $Order |
||
| 445 | * @return array |
||
| 446 | */ |
||
| 447 | public function getDeliveriesOrder(Order $Order) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * 配送業者情報を取得 |
||
| 459 | * |
||
| 460 | * @param $productTypes |
||
| 461 | * @return array |
||
| 462 | */ |
||
| 463 | public function getDeliveries($productTypes) |
||
| 464 | { |
||
| 465 | |||
| 466 | // 商品種別に紐づく配送業者を取得 |
||
| 467 | $deliveries = $this->deliveryRepository->getDeliveries($productTypes); |
||
| 468 | |||
| 469 | if ($this->BaseInfo->getOptionMultipleShipping() == Constant::ENABLED) { |
||
| 470 | // 複数配送対応 |
||
| 471 | |||
| 472 | // 支払方法を取得 |
||
| 473 | $payments = $this->paymentRepository->findAllowedPayments($deliveries); |
||
| 474 | |||
| 475 | if (count($productTypes) > 1) { |
||
| 476 | // 商品種別が複数ある場合、配送対象となる配送業者を取得 |
||
| 477 | $deliveries = $this->deliveryRepository->findAllowedDeliveries($productTypes, $payments); |
||
| 478 | } |
||
| 479 | |||
| 480 | } |
||
| 481 | |||
| 482 | return $deliveries; |
||
| 483 | |||
| 484 | } |
||
| 485 | |||
| 486 | |||
| 487 | /** |
||
| 488 | * お届け先情報を作成 |
||
| 489 | * |
||
| 490 | * @param Order $Order |
||
| 491 | * @param Customer $Customer |
||
| 492 | * @param $deliveries |
||
| 493 | * @return Order |
||
| 494 | */ |
||
| 495 | public function getNewShipping(Order $Order, Customer $Customer, $deliveries) |
||
| 519 | |||
| 520 | /** |
||
| 521 | * お届け先情報を作成 |
||
| 522 | * |
||
| 523 | * @param \Eccube\Entity\Shipping $Shipping |
||
| 524 | * @param \Eccube\Entity\Customer|null $Customer |
||
| 525 | * @return \Eccube\Entity\Shipping |
||
| 526 | */ |
||
| 527 | public function copyToShippingFromCustomer(Shipping $Shipping, Customer $Customer = null) |
||
| 528 | { |
||
| 529 | if (is_null($Customer)) { |
||
| 530 | return $Shipping; |
||
| 531 | } |
||
| 532 | |||
| 533 | $CustomerAddress = $this->customerAddressRepository->findOneBy( |
||
| 534 | array('Customer' => $Customer), |
||
| 535 | array('id' => 'ASC') |
||
| 536 | ); |
||
| 537 | |||
| 538 | if (!is_null($CustomerAddress)) { |
||
| 539 | $Shipping |
||
| 540 | ->setName01($CustomerAddress->getName01()) |
||
| 541 | ->setName02($CustomerAddress->getName02()) |
||
| 542 | ->setKana01($CustomerAddress->getKana01()) |
||
| 543 | ->setKana02($CustomerAddress->getKana02()) |
||
| 544 | ->setCompanyName($CustomerAddress->getCompanyName()) |
||
| 545 | ->setTel01($CustomerAddress->getTel01()) |
||
| 546 | ->setTel02($CustomerAddress->getTel02()) |
||
| 547 | ->setTel03($CustomerAddress->getTel03()) |
||
| 548 | ->setFax01($CustomerAddress->getFax01()) |
||
| 549 | ->setFax02($CustomerAddress->getFax02()) |
||
| 550 | ->setFax03($CustomerAddress->getFax03()) |
||
| 551 | ->setZip01($CustomerAddress->getZip01()) |
||
| 552 | ->setZip02($CustomerAddress->getZip02()) |
||
| 553 | ->setZipCode($CustomerAddress->getZip01().$CustomerAddress->getZip02()) |
||
| 554 | ->setPref($CustomerAddress->getPref()) |
||
| 555 | ->setAddr01($CustomerAddress->getAddr01()) |
||
| 556 | ->setAddr02($CustomerAddress->getAddr02()); |
||
| 557 | } else { |
||
| 558 | $Shipping |
||
| 559 | ->setName01($Customer->getName01()) |
||
| 560 | ->setName02($Customer->getName02()) |
||
| 561 | ->setKana01($Customer->getKana01()) |
||
| 562 | ->setKana02($Customer->getKana02()) |
||
| 563 | ->setCompanyName($Customer->getCompanyName()) |
||
| 564 | ->setTel01($Customer->getTel01()) |
||
| 565 | ->setTel02($Customer->getTel02()) |
||
| 566 | ->setTel03($Customer->getTel03()) |
||
| 567 | ->setFax01($Customer->getFax01()) |
||
| 568 | ->setFax02($Customer->getFax02()) |
||
| 569 | ->setFax03($Customer->getFax03()) |
||
| 570 | ->setZip01($Customer->getZip01()) |
||
| 571 | ->setZip02($Customer->getZip02()) |
||
| 572 | ->setZipCode($Customer->getZip01().$Customer->getZip02()) |
||
| 573 | ->setPref($Customer->getPref()) |
||
| 574 | ->setAddr01($Customer->getAddr01()) |
||
| 575 | ->setAddr02($Customer->getAddr02()); |
||
| 576 | } |
||
| 577 | |||
| 578 | return $Shipping; |
||
| 579 | } |
||
| 580 | |||
| 581 | |||
| 582 | /** |
||
| 583 | * 受注明細情報、配送商品情報を作成 |
||
| 584 | * |
||
| 585 | * @param Order $Order |
||
| 586 | * @return Order |
||
| 587 | */ |
||
| 588 | public function getNewDetails(Order $Order) |
||
| 612 | |||
| 613 | /** |
||
| 614 | * 受注明細情報を作成 |
||
| 615 | * |
||
| 616 | * @param Product $Product |
||
| 617 | * @param ProductClass $ProductClass |
||
| 618 | * @param $quantity |
||
| 619 | * @return \Eccube\Entity\OrderDetail |
||
| 620 | */ |
||
| 621 | public function getNewOrderDetail(Product $Product, ProductClass $ProductClass, $quantity) |
||
| 649 | |||
| 650 | /** |
||
| 651 | * 配送商品情報を作成 |
||
| 652 | * |
||
| 653 | * @param Order $Order |
||
| 654 | * @param Product $Product |
||
| 655 | * @param ProductClass $ProductClass |
||
| 656 | * @param $quantity |
||
| 657 | * @return \Eccube\Entity\OrderItem |
||
| 658 | */ |
||
| 659 | public function getNewOrderItem(Order $Order, Product $Product, ProductClass $ProductClass, $quantity) |
||
| 660 | { |
||
| 661 | |||
| 662 | $OrderItem = new OrderItem(); |
||
| 663 | $shippings = $Order->getShippings(); |
||
| 664 | |||
| 665 | // 選択された商品がどのお届け先情報と関連するかチェック |
||
| 666 | $Shipping = null; |
||
| 667 | foreach ($shippings as $s) { |
||
| 668 | if ($s->getDelivery()->getProductType()->getId() == $ProductClass->getProductType()->getId()) { |
||
| 669 | // 商品種別が同一のお届け先情報と関連させる |
||
| 670 | $Shipping = $s; |
||
| 671 | break; |
||
| 672 | } |
||
| 673 | } |
||
| 674 | |||
| 675 | if (is_null($Shipping)) { |
||
| 676 | // お届け先情報と関連していない場合、エラー |
||
| 677 | throw new CartException('shopping.delivery.not.producttype'); |
||
| 678 | } |
||
| 679 | |||
| 680 | // 商品ごとの配送料合計 |
||
| 681 | $productDeliveryFeeTotal = 0; |
||
| 682 | if (!is_null($this->BaseInfo->getOptionProductDeliveryFee())) { |
||
| 683 | $productDeliveryFeeTotal = $ProductClass->getDeliveryFee() * $quantity; |
||
| 684 | } |
||
| 685 | |||
| 686 | $Shipping->setShippingDeliveryFee($Shipping->getShippingDeliveryFee() + $productDeliveryFeeTotal); |
||
| 687 | |||
| 688 | $OrderItem->setShipping($Shipping) |
||
| 689 | ->setOrder($Order) |
||
| 690 | ->setProductClass($ProductClass) |
||
| 691 | ->setProduct($Product) |
||
| 692 | ->setProductName($Product->getName()) |
||
| 693 | ->setProductCode($ProductClass->getCode()) |
||
| 694 | ->setPrice($ProductClass->getPrice02()) |
||
| 695 | ->setQuantity($quantity); |
||
| 696 | |||
| 697 | $ClassCategory1 = $ProductClass->getClassCategory1(); |
||
| 698 | if (!is_null($ClassCategory1)) { |
||
| 699 | $OrderItem->setClasscategoryName1($ClassCategory1->getName()); |
||
| 700 | $OrderItem->setClassName1($ClassCategory1->getClassName()->getName()); |
||
| 701 | } |
||
| 702 | $ClassCategory2 = $ProductClass->getClassCategory2(); |
||
| 703 | if (!is_null($ClassCategory2)) { |
||
| 704 | $OrderItem->setClasscategoryName2($ClassCategory2->getName()); |
||
| 705 | $OrderItem->setClassName2($ClassCategory2->getClassName()->getName()); |
||
| 706 | } |
||
| 707 | $Shipping->addOrderItem($OrderItem); |
||
| 708 | $this->entityManager->persist($OrderItem); |
||
| 709 | |||
| 710 | return $OrderItem; |
||
| 711 | |||
| 712 | } |
||
| 713 | |||
| 714 | /** |
||
| 715 | * お届け先ごとの送料合計を取得 |
||
| 716 | * |
||
| 717 | * @param $shippings |
||
| 718 | * @return int |
||
| 719 | */ |
||
| 720 | public function getShippingDeliveryFeeTotal($shippings) |
||
| 730 | |||
| 731 | /** |
||
| 732 | * 商品ごとの配送料を取得 |
||
| 733 | * |
||
| 734 | * @param Shipping $Shipping |
||
| 735 | * @return int |
||
| 736 | */ |
||
| 737 | public function getProductDeliveryFee(Shipping $Shipping) |
||
| 747 | |||
| 748 | /** |
||
| 749 | * 住所などの情報が変更された時に金額の再計算を行う |
||
| 750 | * @deprecated PurchaseFlowで行う |
||
| 751 | * @param Order $Order |
||
| 752 | * @return Order |
||
| 753 | */ |
||
| 754 | public function getAmount(Order $Order) |
||
| 755 | { |
||
| 756 | |||
| 757 | // 初期選択の配送業者をセット |
||
| 758 | $shippings = $Order->getShippings(); |
||
| 759 | |||
| 760 | // 配送料合計金額 |
||
| 761 | // TODO CalculateDeliveryFeeStrategy でセットする |
||
| 762 | // $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($shippings)); |
||
| 763 | |||
| 764 | // 配送料無料条件(合計金額) |
||
| 765 | $this->setDeliveryFreeAmount($Order); |
||
| 766 | |||
| 767 | // 配送料無料条件(合計数量) |
||
| 768 | $this->setDeliveryFreeQuantity($Order); |
||
| 769 | |||
| 770 | // 合計金額の計算 |
||
| 771 | $this->calculatePrice($Order); |
||
| 772 | |||
| 773 | return $Order; |
||
| 774 | |||
| 775 | } |
||
| 776 | |||
| 777 | /** |
||
| 778 | * 配送料金の設定 |
||
| 779 | * |
||
| 780 | * @param Shipping $Shipping |
||
| 781 | * @param Delivery|null $Delivery |
||
| 782 | */ |
||
| 783 | public function setShippingDeliveryFee(Shipping $Shipping, Delivery $Delivery = null) |
||
| 805 | |||
| 806 | /** |
||
| 807 | * 配送料無料条件(合計金額)の条件を満たしていれば配送料金を0に設定 |
||
| 808 | * |
||
| 809 | * @param Order $Order |
||
| 810 | */ |
||
| 811 | View Code Duplication | public function setDeliveryFreeAmount(Order $Order) |
|
| 812 | { |
||
| 813 | // 配送料無料条件(合計金額) |
||
| 814 | $deliveryFreeAmount = $this->BaseInfo->getDeliveryFreeAmount(); |
||
| 815 | if (!is_null($deliveryFreeAmount)) { |
||
| 816 | // 合計金額が設定金額以上であれば送料無料 |
||
| 817 | if ($Order->getSubTotal() >= $deliveryFreeAmount) { |
||
| 818 | $Order->setDeliveryFeeTotal(0); |
||
| 819 | // お届け先情報の配送料も0にセット |
||
| 820 | $shippings = $Order->getShippings(); |
||
| 821 | foreach ($shippings as $Shipping) { |
||
| 822 | $Shipping->setShippingDeliveryFee(0); |
||
| 823 | } |
||
| 824 | } |
||
| 825 | } |
||
| 826 | } |
||
| 827 | |||
| 828 | /** |
||
| 829 | * 配送料無料条件(合計数量)の条件を満たしていれば配送料金を0に設定 |
||
| 830 | * |
||
| 831 | * @param Order $Order |
||
| 832 | */ |
||
| 833 | View Code Duplication | public function setDeliveryFreeQuantity(Order $Order) |
|
| 834 | { |
||
| 835 | // 配送料無料条件(合計数量) |
||
| 836 | $deliveryFreeQuantity = $this->BaseInfo->getDeliveryFreeQuantity(); |
||
| 837 | if (!is_null($deliveryFreeQuantity)) { |
||
| 838 | // 合計数量が設定数量以上であれば送料無料 |
||
| 839 | if ($this->orderService->getTotalQuantity($Order) >= $deliveryFreeQuantity) { |
||
| 840 | $Order->setDeliveryFeeTotal(0); |
||
| 841 | // お届け先情報の配送料も0にセット |
||
| 842 | $shippings = $Order->getShippings(); |
||
| 843 | foreach ($shippings as $Shipping) { |
||
| 844 | $Shipping->setShippingDeliveryFee(0); |
||
| 845 | } |
||
| 846 | } |
||
| 847 | } |
||
| 848 | } |
||
| 849 | |||
| 850 | |||
| 851 | /** |
||
| 852 | * 商品公開ステータスチェック、在庫チェック、購入制限数チェックを行い、在庫情報をロックする |
||
| 853 | * |
||
| 854 | * @param $em トランザクション制御されているEntityManager |
||
| 855 | * @param Order $Order 受注情報 |
||
| 856 | * @return bool true : 成功、false : 失敗 |
||
| 857 | */ |
||
| 858 | public function isOrderProduct($em, \Eccube\Entity\Order $Order) |
||
| 859 | { |
||
| 860 | $orderDetails = $Order->getOrderDetails(); |
||
| 861 | |||
| 862 | /** @var OrderItem $orderDetail */ |
||
| 863 | foreach ($orderDetails as $orderDetail) { |
||
| 864 | |||
| 865 | if (is_null($orderDetail->getProduct())) { |
||
| 866 | // FIXME 配送明細を考慮する必要がある |
||
| 867 | continue; |
||
| 868 | } |
||
| 869 | |||
| 870 | // 商品削除チェック |
||
| 871 | if ($orderDetail->getProductClass()->isVisible() == false) { |
||
| 872 | // @deprecated 3.1以降ではexceptionをthrowする |
||
| 873 | // throw new ShoppingException('cart.product.delete'); |
||
| 874 | return false; |
||
| 875 | } |
||
| 876 | |||
| 877 | // 商品公開ステータスチェック |
||
| 878 | if ($orderDetail->getProduct()->getStatus()->getId() != \Eccube\Entity\Master\ProductStatus::DISPLAY_SHOW) { |
||
| 879 | // 商品が非公開ならエラー |
||
| 880 | |||
| 881 | // @deprecated 3.1以降ではexceptionをthrowする |
||
| 882 | // throw new ShoppingException('cart.product.not.status'); |
||
| 883 | return false; |
||
| 884 | } |
||
| 885 | |||
| 886 | // 購入制限数チェック |
||
| 887 | if (!is_null($orderDetail->getProductClass()->getSaleLimit())) { |
||
| 888 | if ($orderDetail->getQuantity() > $orderDetail->getProductClass()->getSaleLimit()) { |
||
| 889 | // @deprecated 3.1以降ではexceptionをthrowする |
||
| 890 | // throw new ShoppingException('cart.over.sale_limit'); |
||
| 891 | return false; |
||
| 892 | } |
||
| 893 | } |
||
| 894 | |||
| 895 | // 購入数チェック |
||
| 896 | if ($orderDetail->getQuantity() < 1) { |
||
| 897 | // 購入数量が1未満ならエラー |
||
| 898 | |||
| 899 | // @deprecated 3.1以降ではexceptionをthrowする |
||
| 900 | // throw new ShoppingException('???'); |
||
| 901 | return false; |
||
| 902 | } |
||
| 903 | |||
| 904 | } |
||
| 905 | |||
| 906 | // 在庫チェック |
||
| 907 | foreach ($orderDetails as $orderDetail) { |
||
| 908 | if (is_null($orderDetail->getProductClass())) { |
||
| 909 | // FIXME 配送明細を考慮する必要がある |
||
| 910 | continue; |
||
| 911 | } |
||
| 912 | // 在庫が無制限かチェックし、制限ありなら在庫数をチェック |
||
| 913 | if ($orderDetail->getProductClass()->getStockUnlimited() == Constant::DISABLED) { |
||
| 914 | // 在庫チェックあり |
||
| 915 | // 在庫に対してロック(select ... for update)を実行 |
||
| 916 | $productStock = $em->getRepository('Eccube\Entity\ProductStock')->find( |
||
| 917 | $orderDetail->getProductClass()->getProductStock()->getId(), LockMode::PESSIMISTIC_WRITE |
||
| 918 | ); |
||
| 919 | // 購入数量と在庫数をチェックして在庫がなければエラー |
||
| 920 | if ($productStock->getStock() < 1) { |
||
| 921 | // @deprecated 3.1以降ではexceptionをthrowする |
||
| 922 | // throw new ShoppingException('cart.over.stock'); |
||
| 923 | return false; |
||
| 924 | } elseif ($orderDetail->getQuantity() > $productStock->getStock()) { |
||
| 925 | // @deprecated 3.1以降ではexceptionをthrowする |
||
| 926 | // throw new ShoppingException('cart.over.stock'); |
||
| 927 | return false; |
||
| 928 | } |
||
| 929 | } |
||
| 930 | } |
||
| 931 | |||
| 932 | return true; |
||
| 933 | |||
| 934 | } |
||
| 935 | |||
| 936 | /** |
||
| 937 | * 受注情報、お届け先情報の更新 |
||
| 938 | * |
||
| 939 | * @param Order $Order 受注情報 |
||
| 940 | * @param $data フォームデータ |
||
| 941 | * |
||
| 942 | * @deprecated since 3.0.5, to be removed in 3.1 |
||
| 943 | */ |
||
| 944 | public function setOrderUpdate(Order $Order, $data) |
||
| 977 | |||
| 978 | |||
| 979 | /** |
||
| 980 | * 受注情報の更新 |
||
| 981 | * |
||
| 982 | * @param Order $Order 受注情報 |
||
| 983 | */ |
||
| 984 | public function setOrderUpdateData(Order $Order) |
||
| 985 | { |
||
| 986 | // 受注情報を更新 |
||
| 987 | $Order->setOrderDate(new \DateTime()); // XXX 後続の setOrderStatus でも時刻を更新している |
||
| 988 | $OrderStatus = $this->orderStatusRepository->find($this->appConfig['order_new']); |
||
| 989 | $this->setOrderStatus($Order, $OrderStatus); |
||
| 990 | |||
| 991 | } |
||
| 992 | |||
| 993 | |||
| 994 | /** |
||
| 995 | * 在庫情報の更新 |
||
| 996 | * |
||
| 997 | * @param $em トランザクション制御されているEntityManager |
||
| 998 | * @param Order $Order 受注情報 |
||
| 999 | */ |
||
| 1000 | public function setStockUpdate($em, Order $Order) |
||
| 1001 | { |
||
| 1002 | |||
| 1003 | $orderDetails = $Order->getOrderDetails(); |
||
| 1004 | |||
| 1005 | // 在庫情報更新 |
||
| 1006 | foreach ($orderDetails as $orderDetail) { |
||
| 1007 | if (is_null($orderDetail->getProductClass())) { |
||
| 1008 | // FIXME 配送明細を考慮する必要がある |
||
| 1009 | continue; |
||
| 1010 | } |
||
| 1011 | // 在庫が無制限かチェックし、制限ありなら在庫数を更新 |
||
| 1012 | if ($orderDetail->getProductClass()->getStockUnlimited() == Constant::DISABLED) { |
||
| 1013 | |||
| 1014 | $productStock = $em->getRepository('Eccube\Entity\ProductStock')->find( |
||
| 1015 | $orderDetail->getProductClass()->getProductStock()->getId() |
||
| 1016 | ); |
||
| 1017 | |||
| 1018 | // 在庫情報の在庫数を更新 |
||
| 1019 | $stock = $productStock->getStock() - $orderDetail->getQuantity(); |
||
| 1020 | $productStock->setStock($stock); |
||
| 1021 | |||
| 1022 | // 商品規格情報の在庫数を更新 |
||
| 1023 | $orderDetail->getProductClass()->setStock($stock); |
||
| 1024 | |||
| 1025 | } |
||
| 1026 | } |
||
| 1027 | |||
| 1028 | } |
||
| 1029 | |||
| 1030 | |||
| 1031 | /** |
||
| 1032 | * 会員情報の更新 |
||
| 1033 | * |
||
| 1034 | * @param Order $Order 受注情報 |
||
| 1035 | * @param Customer $user ログインユーザ |
||
| 1036 | */ |
||
| 1037 | public function setCustomerUpdate(Order $Order, Customer $user) |
||
| 1038 | { |
||
| 1039 | |||
| 1040 | $orderDetails = $Order->getOrderDetails(); |
||
| 1041 | |||
| 1042 | // 顧客情報を更新 |
||
| 1043 | $now = new \DateTime(); |
||
| 1044 | $firstBuyDate = $user->getFirstBuyDate(); |
||
| 1045 | if (empty($firstBuyDate)) { |
||
| 1046 | $user->setFirstBuyDate($now); |
||
| 1047 | } |
||
| 1048 | $user->setLastBuyDate($now); |
||
| 1049 | |||
| 1050 | $user->setBuyTimes($user->getBuyTimes() + 1); |
||
| 1051 | $user->setBuyTotal($user->getBuyTotal() + $Order->getTotal()); |
||
| 1052 | |||
| 1053 | } |
||
| 1054 | |||
| 1055 | |||
| 1056 | /** |
||
| 1057 | * 支払方法選択の表示設定 |
||
| 1058 | * |
||
| 1059 | * @param $payments 支払選択肢情報 |
||
| 1060 | * @param $subTotal 小計 |
||
| 1061 | * @return array |
||
| 1062 | */ |
||
| 1063 | public function getPayments($payments, $subTotal) |
||
| 1064 | { |
||
| 1065 | $pays = array(); |
||
| 1066 | foreach ($payments as $payment) { |
||
| 1067 | // 支払方法の制限値内であれば表示 |
||
| 1068 | if (!is_null($payment)) { |
||
| 1069 | $pay = $this->paymentRepository->find($payment['id']); |
||
| 1070 | if (intval($pay->getRuleMin()) <= $subTotal) { |
||
| 1071 | if (is_null($pay->getRuleMax()) || $pay->getRuleMax() >= $subTotal) { |
||
| 1072 | $pays[] = $pay; |
||
| 1073 | } |
||
| 1074 | } |
||
| 1075 | } |
||
| 1076 | } |
||
| 1077 | |||
| 1078 | return $pays; |
||
| 1079 | |||
| 1080 | } |
||
| 1081 | |||
| 1082 | /** |
||
| 1083 | * お届け日を取得 |
||
| 1084 | * |
||
| 1085 | * @param Order $Order |
||
| 1086 | * @return array |
||
| 1087 | */ |
||
| 1088 | public function getFormDeliveryDates(Order $Order) |
||
| 1089 | { |
||
| 1090 | |||
| 1091 | // お届け日の設定 |
||
| 1092 | $minDate = 0; |
||
| 1093 | $deliveryDateFlag = false; |
||
| 1094 | |||
| 1095 | // 配送時に最大となる商品日数を取得 |
||
| 1096 | foreach ($Order->getOrderDetails() as $detail) { |
||
| 1097 | $deliveryDate = $detail->getProductClass()->getDeliveryDate(); |
||
| 1098 | if (!is_null($deliveryDate)) { |
||
| 1099 | if ($deliveryDate->getValue() < 0) { |
||
| 1100 | // 配送日数がマイナスの場合はお取り寄せなのでスキップする |
||
| 1101 | $deliveryDateFlag = false; |
||
| 1102 | break; |
||
| 1103 | } |
||
| 1104 | |||
| 1105 | if ($minDate < $deliveryDate->getValue()) { |
||
| 1106 | $minDate = $deliveryDate->getValue(); |
||
| 1107 | } |
||
| 1108 | // 配送日数が設定されている |
||
| 1109 | $deliveryDateFlag = true; |
||
| 1110 | } |
||
| 1111 | } |
||
| 1112 | |||
| 1113 | // 配達最大日数期間を設定 |
||
| 1114 | $deliveryDates = array(); |
||
| 1115 | |||
| 1116 | // 配送日数が設定されている |
||
| 1117 | View Code Duplication | if ($deliveryDateFlag) { |
|
| 1118 | $period = new \DatePeriod ( |
||
| 1119 | new \DateTime($minDate.' day'), |
||
| 1120 | new \DateInterval('P1D'), |
||
| 1121 | new \DateTime($minDate + $this->appConfig['deliv_date_end_max'].' day') |
||
| 1122 | ); |
||
| 1123 | |||
| 1124 | foreach ($period as $day) { |
||
| 1125 | $deliveryDates[$day->format('Y/m/d')] = $day->format('Y/m/d'); |
||
| 1126 | } |
||
| 1127 | } |
||
| 1128 | |||
| 1129 | return $deliveryDates; |
||
| 1130 | |||
| 1131 | } |
||
| 1132 | |||
| 1133 | /** |
||
| 1134 | * 支払方法を取得 |
||
| 1135 | * |
||
| 1136 | * @param $deliveries |
||
| 1137 | * @param Order $Order |
||
| 1138 | * @return array |
||
| 1139 | */ |
||
| 1140 | public function getFormPayments($deliveries, Order $Order) |
||
| 1161 | |||
| 1162 | /** |
||
| 1163 | * お届け先ごとにFormを作成 |
||
| 1164 | * |
||
| 1165 | * @param Order $Order |
||
| 1166 | * @return \Symfony\Component\Form\Form |
||
| 1167 | * @deprecated since 3.0, to be removed in 3.1 |
||
| 1168 | */ |
||
| 1169 | View Code Duplication | public function getShippingForm(Order $Order) |
|
| 1195 | |||
| 1196 | /** |
||
| 1197 | * お届け先ごとにFormBuilderを作成 |
||
| 1198 | * |
||
| 1199 | * @param Order $Order |
||
| 1200 | * @return \Symfony\Component\Form\FormBuilderInterface |
||
| 1201 | * |
||
| 1202 | * @deprecated 利用している箇所なし |
||
| 1203 | */ |
||
| 1204 | View Code Duplication | public function getShippingFormBuilder(Order $Order) |
|
| 1228 | |||
| 1229 | |||
| 1230 | /** |
||
| 1231 | * フォームデータを更新 |
||
| 1232 | * |
||
| 1233 | * @param Order $Order |
||
| 1234 | * @param array $data |
||
| 1235 | * |
||
| 1236 | * @deprecated |
||
| 1237 | */ |
||
| 1238 | public function setFormData(Order $Order, array $data) |
||
| 1239 | { |
||
| 1240 | |||
| 1241 | // お問い合わせ |
||
| 1242 | $Order->setMessage($data['message']); |
||
| 1243 | |||
| 1244 | // お届け先情報を更新 |
||
| 1245 | $shippings = $data['shippings']; |
||
| 1246 | foreach ($shippings as $Shipping) { |
||
| 1247 | |||
| 1248 | $deliveryTime = $Shipping->getDeliveryTime(); |
||
| 1249 | if (!empty($deliveryTime)) { |
||
| 1250 | $Shipping->setShippingDeliveryTime($deliveryTime->getDeliveryTime()); |
||
| 1251 | $Shipping->setTimeId($deliveryTime->getId()); |
||
| 1252 | } |
||
| 1253 | |||
| 1254 | } |
||
| 1255 | |||
| 1256 | } |
||
| 1257 | |||
| 1258 | /** |
||
| 1259 | * 配送料の合計金額を計算 |
||
| 1260 | * |
||
| 1261 | * @param Order $Order |
||
| 1262 | * @return Order |
||
| 1263 | */ |
||
| 1264 | public function calculateDeliveryFee(Order $Order) |
||
| 1265 | { |
||
| 1266 | |||
| 1267 | // 配送業者を取得 |
||
| 1268 | $shippings = $Order->getShippings(); |
||
| 1269 | |||
| 1270 | // 配送料合計金額 |
||
| 1271 | // TODO CalculateDeliveryFeeStrategy でセットする |
||
| 1272 | // $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($shippings)); |
||
| 1273 | |||
| 1274 | // 配送料無料条件(合計金額) |
||
| 1275 | $this->setDeliveryFreeAmount($Order); |
||
| 1276 | |||
| 1277 | // 配送料無料条件(合計数量) |
||
| 1278 | $this->setDeliveryFreeQuantity($Order); |
||
| 1279 | |||
| 1280 | return $Order; |
||
| 1281 | |||
| 1282 | } |
||
| 1283 | |||
| 1284 | |||
| 1285 | /** |
||
| 1286 | * 購入処理を行う |
||
| 1287 | * |
||
| 1288 | * @param Order $Order |
||
| 1289 | * @throws ShoppingException |
||
| 1290 | */ |
||
| 1291 | public function processPurchase(Order $Order) |
||
| 1292 | { |
||
| 1293 | |||
| 1294 | $em = $this->entityManager; |
||
| 1295 | |||
| 1296 | // TODO PurchaseFlowでやる |
||
| 1297 | // // 合計金額の再計算 |
||
| 1298 | // $this->calculatePrice($Order); |
||
| 1299 | // |
||
| 1300 | // // 商品公開ステータスチェック、商品制限数チェック、在庫チェック |
||
| 1301 | // $check = $this->isOrderProduct($em, $Order); |
||
| 1302 | // if (!$check) { |
||
| 1303 | // throw new ShoppingException('front.shopping.stock.error'); |
||
| 1304 | // } |
||
| 1305 | |||
| 1306 | // 受注情報、配送情報を更新 |
||
| 1307 | $Order = $this->calculateDeliveryFee($Order); |
||
| 1308 | $this->setOrderUpdateData($Order); |
||
| 1309 | // 在庫情報を更新 |
||
| 1310 | $this->setStockUpdate($em, $Order); |
||
| 1311 | |||
| 1312 | if ($this->app->isGranted('ROLE_USER')) { |
||
| 1313 | // 会員の場合、購入金額を更新 |
||
| 1314 | $this->setCustomerUpdate($Order, $this->app->user()); |
||
| 1315 | } |
||
| 1316 | |||
| 1317 | } |
||
| 1318 | |||
| 1319 | |||
| 1320 | /** |
||
| 1321 | * 値引き可能かチェック |
||
| 1322 | * |
||
| 1323 | * @param Order $Order |
||
| 1324 | * @param $discount |
||
| 1325 | * @return bool |
||
| 1326 | */ |
||
| 1327 | public function isDiscount(Order $Order, $discount) |
||
| 1336 | |||
| 1337 | |||
| 1338 | /** |
||
| 1339 | * 値引き金額をセット |
||
| 1340 | * |
||
| 1341 | * @param Order $Order |
||
| 1342 | * @param $discount |
||
| 1343 | */ |
||
| 1344 | public function setDiscount(Order $Order, $discount) |
||
| 1350 | |||
| 1351 | |||
| 1352 | /** |
||
| 1353 | * 合計金額を計算 |
||
| 1354 | * |
||
| 1355 | * @param Order $Order |
||
| 1356 | * @return Order |
||
| 1357 | */ |
||
| 1358 | public function calculatePrice(Order $Order) |
||
| 1359 | { |
||
| 1360 | |||
| 1361 | $total = $Order->getTotalPrice(); |
||
| 1362 | |||
| 1363 | if ($total < 0) { |
||
| 1364 | // 合計金額がマイナスの場合、0を設定し、discountは値引きされた額のみセット |
||
| 1365 | $total = 0; |
||
| 1366 | } |
||
| 1367 | |||
| 1368 | $Order->setTotal($total); |
||
| 1369 | $Order->setPaymentTotal($total); |
||
| 1370 | |||
| 1371 | return $Order; |
||
| 1372 | |||
| 1373 | } |
||
| 1374 | |||
| 1375 | /** |
||
| 1376 | * 受注ステータスをセット |
||
| 1377 | * |
||
| 1378 | * @param Order $Order |
||
| 1379 | * @param $status |
||
| 1380 | * @return Order |
||
| 1381 | */ |
||
| 1382 | public function setOrderStatus(Order $Order, $status) |
||
| 1383 | { |
||
| 1384 | |||
| 1385 | $Order->setOrderDate(new \DateTime()); |
||
| 1386 | $Order->setOrderStatus($this->orderStatusRepository->find($status)); |
||
| 1387 | |||
| 1388 | $event = new EventArgs( |
||
| 1389 | array( |
||
| 1390 | 'Order' => $Order, |
||
| 1391 | ), |
||
| 1392 | null |
||
| 1393 | ); |
||
| 1394 | $this->eventDispatcher->dispatch(EccubeEvents::SERVICE_SHOPPING_ORDER_STATUS, $event); |
||
| 1395 | |||
| 1396 | return $Order; |
||
| 1397 | |||
| 1398 | } |
||
| 1399 | |||
| 1400 | /** |
||
| 1401 | * 受注メール送信を行う |
||
| 1402 | * |
||
| 1403 | * @param Order $Order |
||
| 1404 | * @return MailHistory |
||
| 1405 | */ |
||
| 1406 | public function sendOrderMail(Order $Order) |
||
| 1407 | { |
||
| 1408 | |||
| 1409 | // メール送信 |
||
| 1410 | $message = $this->mailService->sendOrderMail($Order); |
||
| 1411 | |||
| 1412 | // 送信履歴を保存. |
||
| 1413 | $MailHistory = new MailHistory(); |
||
| 1414 | $MailHistory |
||
| 1415 | ->setSubject($message->getSubject()) |
||
| 1416 | ->setMailBody($message->getBody()) |
||
| 1417 | ->setSendDate(new \DateTime()) |
||
| 1418 | ->setOrder($Order); |
||
| 1419 | |||
| 1420 | $this->entityManager->persist($MailHistory); |
||
| 1421 | $this->entityManager->flush($MailHistory); |
||
| 1422 | |||
| 1423 | return $MailHistory; |
||
| 1424 | |||
| 1425 | } |
||
| 1426 | |||
| 1427 | |||
| 1428 | /** |
||
| 1429 | * 受注処理完了通知 |
||
| 1430 | * |
||
| 1431 | * @param Order $Order |
||
| 1432 | */ |
||
| 1433 | public function notifyComplete(Order $Order) |
||
| 1445 | |||
| 1446 | |||
| 1447 | } |
||
| 1448 |