app/Plugin/EccubePaymentLite42/Service/SaveRegularOrderService.php line 66

Open in your IDE?
  1. <?php
  2. namespace Plugin\EccubePaymentLite42\Service;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Eccube\Entity\Master\SaleType;
  5. use Eccube\Entity\Order;
  6. use Eccube\Repository\Master\SaleTypeRepository;
  7. use Eccube\Service\PurchaseFlow\Processor\PointProcessor;
  8. use Plugin\EccubePaymentLite42\Entity\Config;
  9. use Plugin\EccubePaymentLite42\Entity\RegularCycle;
  10. use Plugin\EccubePaymentLite42\Entity\RegularOrder;
  11. use Plugin\EccubePaymentLite42\Entity\RegularOrderItem;
  12. use Plugin\EccubePaymentLite42\Entity\RegularShipping;
  13. use Plugin\EccubePaymentLite42\Entity\RegularStatus;
  14. use Plugin\EccubePaymentLite42\Repository\ConfigRepository;
  15. use Plugin\EccubePaymentLite42\Repository\RegularCycleRepository;
  16. use Plugin\EccubePaymentLite42\Repository\RegularStatusRepository;
  17. use Symfony\Component\HttpFoundation\RequestStack;
  18. class SaveRegularOrderService
  19. {
  20.     /**
  21.      * @var EntityManagerInterface
  22.      */
  23.     private $entityManager;
  24.     /**
  25.      * @var RegularStatusRepository
  26.      */
  27.     private $regularStatusRepository;
  28.     /**
  29.      * @var ConfigRepository
  30.      */
  31.     private $configRepository;
  32.     /**
  33.      * @var RegularCycleRepository
  34.      */
  35.     private $regularCycleRepository;
  36.     /**
  37.      * @var SessionInterface
  38.      */
  39.     private $session;
  40.     /**
  41.      * @var CalculateNextDeliveryDateService
  42.      */
  43.     private $calculateNextDeliveryDateService;
  44.     /**
  45.      * @var SaleTypeRepository
  46.      */
  47.     private $saleTypeRepository;
  48.     public function __construct(
  49.         EntityManagerInterface $entityManager,
  50.         RegularStatusRepository $regularStatusRepository,
  51.         ConfigRepository $configRepository,
  52.         RegularCycleRepository $regularCycleRepository,
  53.         SaleTypeRepository $saleTypeRepository,
  54.         RequestStack $requestStack,
  55.         CalculateNextDeliveryDateService $calculateNextDeliveryDateService
  56.     ) {
  57.         $this->entityManager $entityManager;
  58.         $this->regularStatusRepository $regularStatusRepository;
  59.         $this->configRepository $configRepository;
  60.         $this->saleTypeRepository $saleTypeRepository;
  61.         $this->regularCycleRepository $regularCycleRepository;
  62.         $this->session $requestStack->getSession();
  63.         $this->calculateNextDeliveryDateService $calculateNextDeliveryDateService;
  64.     }
  65.     public function setRegularCycleIdInSession($regularCycleId)
  66.     {
  67.         $this->session->set(
  68.             'regular_cycle_id',
  69.             $regularCycleId
  70.         );
  71.     }
  72.     public function save(Order $Order)
  73.     {
  74.         // 一度しか処理が行われないように、セッションを削除している。
  75.         if (is_null($this->session->get('regular_cycle_id'))) {
  76.             return;
  77.         }
  78.         $regularCycleId $this->session->get('regular_cycle_id');
  79.         $this->session->remove('regular_cycle_id');
  80.         /** @var Config $Config */
  81.         $Config $this->configRepository->find(1);
  82.         $saleTypeId $Order->getShippings()[0]->getDelivery()->getSaleType()->getId();
  83.         /** @var SaleType $SaleType */
  84.         $SaleType $this->saleTypeRepository->find($saleTypeId);
  85.         if ($SaleType->getName() !== '定期商品') {
  86.             return;
  87.         }
  88.         // 配送予定日が入力されていない場合、受注日+初回配送予定日の日数で登録
  89.         foreach ($Order->getShippings() as $Shipping) {
  90.             if (is_null($Shipping->getShippingDeliveryDate())) {
  91.                 $num $Config->getFirstDeliveryDays();
  92.                 $Shipping->setShippingDeliveryDate(new \DateTime('+'.$num.' day'));
  93.             }
  94.         }
  95.         $RegularOrder = new RegularOrder();
  96.         /** @var RegularStatus $RegularStatus */
  97.         $RegularStatus $this->
  98.             regularStatusRepository
  99.             ->find(RegularStatus::CONTINUE);
  100.         // RegularOrder
  101.         $RegularOrder->copyProperties($Order, [
  102.             'id',
  103.             'create_date',
  104.             'update_date',
  105.         ]);
  106.         /** @var RegularCycle $RegularCycle */
  107.         $RegularCycle $this
  108.             ->regularCycleRepository
  109.             ->find($regularCycleId);
  110.         $RegularOrder
  111.             ->setTransCode($Order->getTransCode())
  112.             ->setRegularOrderCount(1)
  113.             ->setRegularSkipFlag(0)
  114.             ->setRegularStatus($RegularStatus)
  115.             ->setRegularCycle($RegularCycle);
  116.         // RegularShipping
  117.         $Shippings $Order->getShippings();
  118.         $shippingList = [];
  119.         foreach ($Shippings as $Shipping) {
  120.             $RegularShipping = new RegularShipping();
  121.             $RegularShipping->copyProperties($Shipping, ['id']);
  122.             $nextDeliveryDate $this->calculateNextDeliveryDateService
  123.                 ->calc($RegularOrder->getRegularCycle(), $this->getDiffShippingDeliveryDate($Shipping->getShippingDeliveryDate()));
  124.             // お届け予定日が入力されている場合は、現在日付との差分の日付を取得する。
  125.             $RegularShipping
  126.                 ->setRegularOrder($RegularOrder)
  127.                 ->setNextDeliveryDate($nextDeliveryDate)
  128.             ;
  129.             $RegularOrder->addRegularShipping($RegularShipping);
  130.             $shippingList[$Shipping->getId()] = $RegularShipping;
  131.         }
  132.         // RegularOrderItem
  133.         $OrderItems $Order->getOrderItems();
  134.         foreach ($OrderItems as $OrderItem) {
  135.             if ($OrderItem->getProcessorName() == PointProcessor::class) {
  136.                 continue;
  137.             }
  138.             $RegularOrderItem = new RegularOrderItem();
  139.             $RegularOrderItem->copyProperties($OrderItem, ['id']);
  140.             $RegularOrderItem
  141.                 ->setRegularOrder($RegularOrder);
  142.             $RegularOrder->addRegularOrderItem($RegularOrderItem);
  143.             $Shipping $OrderItem->getShipping();
  144.             if ($Shipping) {
  145.                 $RegularShipping $shippingList[$Shipping->getId()];
  146.                 $RegularOrderItem
  147.                     ->setRegularShipping($RegularShipping);
  148.                 $RegularShipping->addRegularOrderItem($RegularOrderItem);
  149.             }
  150.         }
  151.         // remove use point
  152.         $this->removePointRegularOrder($Order$RegularOrder);
  153.         // end remove use point
  154.         $this->entityManager->persist($RegularOrder);
  155.         $this->entityManager->flush();
  156.         // 受注テーブルに定期受注IDを登録
  157.         $Order->setRegularOrder($RegularOrder);
  158.         $this->entityManager->persist($Order);
  159.         $this->entityManager->flush();
  160.     }
  161.     private function getDiffShippingDeliveryDate(\DateTime $shippingDeliveryDate): int
  162.     {
  163.         $today = new \DateTime('today');
  164.         $interval $today->diff($shippingDeliveryDate);
  165.         return (int) $interval->format('%a');
  166.     }
  167.     /**
  168.      * Remove point in RegularOrder
  169.      */
  170.     public function removePointRegularOrder(Order $OrderRegularOrder $RegularOrder)
  171.     {
  172.         /** @var OrderItem $OrderItem */
  173.         $OrderItems $Order->getItems();
  174.         $discountPrices 0;
  175.         foreach ($OrderItems as $OrderItemKey => $OrderItem) {
  176.             if ($OrderItem->isPoint()) {
  177.                 $discountPrices $discountPrices $OrderItem->getPrice();
  178.             }
  179.         }
  180.         $RegularOrder->setDiscount($RegularOrder->getDiscount() - $discountPrices)
  181.                      ->setTotal($RegularOrder->getTotal() + $discountPrices)
  182.                      ->setPaymentTotal($RegularOrder->getPaymentTotal() + $discountPrices)
  183.                      ->setUsePoint(0);
  184.     }
  185. }