app/Plugin/CouponPro42/Service/PurchaseFlow/Processor/CouponItemProcessor.php line 62

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the CouponPro42 Plugin
  4.  *
  5.  * Copyright (C) 2022 Diezon.
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Plugin\CouponPro42\Service\PurchaseFlow\Processor;
  11. use Eccube\Annotation\CartFlow;
  12. use Eccube\Annotation\ShoppingFlow;
  13. use Eccube\Annotation\OrderFlow;
  14. use Eccube\Entity\BaseInfo;
  15. use Eccube\Entity\ItemHolderInterface;
  16. use Eccube\Entity\Master\OrderStatus;
  17. use Eccube\Entity\Order;
  18. use Eccube\Repository\BaseInfoRepository;
  19. use Eccube\Service\PurchaseFlow\ItemHolderPreprocessor;
  20. use Eccube\Service\PurchaseFlow\ProcessResult;
  21. use Eccube\Service\PurchaseFlow\PurchaseContext;
  22. use Eccube\Service\PurchaseFlow\PurchaseProcessor;
  23. use Plugin\CouponPro42\Common\Constant;
  24. use Plugin\CouponPro42\Entity\Coupon;
  25. use Plugin\CouponPro42\Entity\Master\CouponKind;
  26. use Plugin\CouponPro42\Entity\OrderCoupon;
  27. use Plugin\CouponPro42\Repository\CouponRepository;
  28. use Plugin\CouponPro42\Service\CouponHelper;
  29. use Doctrine\ORM\EntityManagerInterface;
  30. /**
  31.  * 購入フローにおけるクーポン処理.
  32.  * @ShoppingFlow
  33.  */
  34. class CouponItemProcessor implements ItemHolderPreprocessor
  35. {
  36.     /**
  37.      * @var EntityManagerInterface
  38.      */
  39.     protected $entityManager;
  40.     /**
  41.      * @var CouponHelper
  42.      */
  43.     protected $couponHelper;
  44.     /**
  45.      * @var BaseInfo
  46.      */
  47.     protected $BaseInfo;
  48.     /**
  49.      * CouponItemProcessor constructor.
  50.      * @param EntityManagerInterface $entityManager
  51.      * @param CouponRepository $couponRepository
  52.      * @param CouponHelper $couponHelper
  53.      * @param BaseInfoRepository $baseInfoRepository
  54.      */
  55.     public function __construct(EntityManagerInterface $entityManager,
  56.                                 CouponRepository $couponRepository,
  57.                                 CouponHelper $couponHelper,
  58.                                 BaseInfoRepository $baseInfoRepository)
  59.     {
  60.         $this->entityManager $entityManager;
  61.         $this->couponRepository $couponRepository;
  62.         $this->couponHelper $couponHelper;
  63.         $this->BaseInfo $baseInfoRepository->get();
  64.     }
  65.     /**
  66.      * {@inheritdoc}
  67.      */
  68.     public function process(ItemHolderInterface $itemHolderPurchaseContext $context)
  69.     {
  70.         if (!$itemHolder instanceof Order) {
  71.             return;
  72.         }
  73.         /** @var Order $Order */
  74.         $Order $itemHolder;
  75.         /** @var Coupon $Coupon */
  76.         $Coupon $this->couponRepository->findOneBy([
  77.             'coupon_code' => $Order->getCouponCode(),
  78.             'deleted' => false
  79.         ]);
  80.         $this->couponHelper->removeCouponDiscountItem($Order);
  81.         if ($Coupon && $this->couponHelper->isCouponEnabled($Order$Coupon)) {
  82.             // 購入フロー実行時、クーポン利用がある場合は割引明細を追加
  83.             $this->createOrderCoupon($Order$Coupon);
  84.             $this->couponHelper->addCouponDiscountItem($Order$Coupon);
  85.             // 購入フロー実行時、クーポン適用時、(商品代金-値引き額)に対して、送料無料(金額)を割った場合、送料を加算する.
  86.             $this->updateShippingFeeItem($Order);
  87.         }
  88.     }
  89.     /*
  90.      * Helper methods
  91.      */
  92.     /**
  93.      * @param Order $Order
  94.      * @param Coupon $Coupon
  95.      */
  96.     private function createOrderCoupon(Order $OrderCoupon $Coupon): void
  97.     {
  98.         $OrderCoupon = new OrderCoupon();
  99.         $OrderCoupon->setCoupon($Coupon)
  100.                     ->setOrder($Order)
  101.                     ->setCouponName($Coupon->getCouponName())
  102.                     ->setCouponKind($Coupon->getCouponKind())
  103.                     ->setCouponCode($Coupon->getCouponCode())
  104.                     ->setDiscountRate($Coupon->getDiscountRate());
  105.         $this->entityManager->persist($OrderCoupon);
  106.         $Order->setOrderCoupon($OrderCoupon);
  107.     }
  108.     /**
  109.      * (商品代金-値引き額)に対して、送料無料(金額)を割った場合、送料を加算する.
  110.      * @param Order $Order
  111.      */
  112.     private function updateShippingFeeItem(Order $Order) {
  113.         if (!$this->BaseInfo->getDeliveryFreeAmount() || $Order->getSubtotal() < $this->BaseInfo->getDeliveryFreeAmount()) {
  114.             return;
  115.         }
  116.         // 複数配送を考慮しない
  117.         if ($Order->isMultiple()) {
  118.             return;
  119.         }
  120.         $total 0;
  121.         $quantity 0;
  122.         foreach ($Order->getProductOrderItems() as $OrderItem) {
  123.             $total += $OrderItem->getTotalPrice();
  124.             $quantity += $OrderItem->getQuantity();
  125.         }
  126.         // 送料無料(個数)を超えている場合は処理を行わない
  127.         if ($this->BaseInfo->getDeliveryFreeQuantity()) {
  128.             if ($quantity >= $this->BaseInfo->getDeliveryFreeQuantity()) {
  129.                 return;
  130.             }
  131.         }
  132.         $totalAfterDiscount $total;
  133.         foreach ($Order->getTaxFreeDiscountItems() as $OrderItem) {
  134.             if ($OrderItem->getProcessorName() == CouponItemProcessor::class) {
  135.                 $totalAfterDiscount += $OrderItem->getTotalPrice();
  136.             }
  137.         }
  138.         $isDeliveryPaid $totalAfterDiscount $this->BaseInfo->getDeliveryFreeAmount();
  139.         if ($isDeliveryPaid) {
  140.             foreach ($Order->getItems() as $item) {
  141.                 if ($item->isDeliveryFee()) {
  142.                     $item->setQuantity(1);
  143.                 }
  144.             }
  145.         }
  146.     }
  147. }