app/Plugin/SheebRental42/Event.php line 228

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Customize of EC-CUBE by Kenji Nakanishi
  4.  *
  5.  * Copyright(c) 2021 Kenji Nakanishi. All Rights Reserved.
  6.  *
  7.  * https://www.facebook.com/web.kenji.nakanishi
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Plugin\SheebRental42;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Eccube\Common\EccubeConfig;
  15. use Eccube\Event\EccubeEvents;
  16. use Eccube\Event\EventArgs;
  17. use Eccube\Event\TemplateEvent;
  18. use Eccube\Service\PurchaseFlow\Processor\StockReduceProcessor;
  19. use Eccube\Service\PurchaseFlow\PurchaseContext;
  20. use Plugin\SheebRental42\Entity\Config;
  21. use Plugin\SheebRental42\Entity\Holiday;
  22. use Plugin\SheebRental42\Repository\ConfigRepository;
  23. use Plugin\SheebRental42\Repository\HolidayRepository;
  24. use Plugin\SheebRental42\Service\StockService;
  25. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  26. use Symfony\Component\Form\FormFactoryInterface;
  27. use Symfony\Component\Workflow\Event\Event as WorkflowEvent;
  28. class Event implements EventSubscriberInterface
  29. {
  30.     /**
  31.      * @return array
  32.      */
  33.     public static function getSubscribedEvents()
  34.     {
  35.         return [
  36.             '@admin/index.twig' => ['onRenderAdminHome'10],
  37.             '@admin/Product/product_class.twig' => ['onRenderProductClassEdit'10],
  38.             '@admin/Order/index.twig' => ['onRenderOrderArchive'10],
  39.             '@admin/Order/edit.twig' => ['onRenderOrderEdit'10],
  40.             '@admin/Order/shipping.twig' => ['onRenderOrderShipping'10],
  41.             'Product/detail.twig' => ['onRenderProductDetail'10],
  42.             'Shopping/index.twig' => ['onRenderShoppingIndex'10],
  43.             'Shopping/confirm.twig' => ['onRenderShoppingConfirm'10],
  44.             'Mypage/index.twig' => ['onRenderMypageHistoryArchive'10],
  45.             'Mypage/history.twig' => ['onRenderMypageHistory'10],
  46.             // 管理画面 Order登録・更新
  47.             EccubeEvents::ADMIN_ORDER_EDIT_INDEX_PROGRESS => ['updateOrder'10],
  48.             // StateMachine 打ち消し
  49.             'workflow.order.transition.back_to_in_progress' => ['rollbackStock'10],
  50.             'workflow.order.transition.cancel' => ['commitStock'10],
  51.         ];
  52.     }
  53.     /**
  54.      * @var EccubeConfig
  55.      */
  56.     private $eccubeConfig;
  57.     /**
  58.      * @var FormFactoryInterface
  59.      */
  60.     private $factory;
  61.     /**
  62.      * @var EntityManagerInterface
  63.      */
  64.     private $em;
  65.     /**
  66.      * @var ConfigRepository
  67.      */
  68.     private $RentalConfigReposigory;
  69.     /**
  70.      * @var HolidayRepository
  71.      */
  72.     private $RentalHolidayRepository;
  73.     /**
  74.      * @var StockService
  75.      */
  76.     private $stockService;
  77.     /**
  78.      * @var StockReduceProcessor
  79.      */
  80.     private $stockReduceProcessor;
  81.     public function __construct(
  82.         EccubeConfig $eccubeConfig,
  83.         FormFactoryInterface $factory,
  84.         EntityManagerInterface $em,
  85.         StockService $stockService,
  86.         StockReduceProcessor $stockReduceProcessor
  87.     ) {
  88.         $this->eccubeConfig $eccubeConfig;
  89.         $this->factory $factory;
  90.         $this->em $em;
  91.         $this->RentalConfigReposigory $this->em->getRepository(Config::class);
  92.         $this->RentalHolidayRepository $this->em->getRepository(Holiday::class);
  93.         $this->stockService $stockService;
  94.         $this->stockReduceProcessor $stockReduceProcessor;
  95.     }
  96.     public function updateOrder(EventArgs $event)
  97.     {
  98.         /** @var \Eccube\Entity\Order */
  99.         $OriginOrder $event->getArgument('OriginOrder');
  100.         /** @var \Eccube\Entity\Order */
  101.         $TargetOrder $event->getArgument('TargetOrder');
  102.         /** @var \Eccube\Entity\Master\OrderStatus */
  103.         $OriginStatus $OriginOrder->getOrderStatus();
  104.         /** @var \Eccube\Entity\Master\OrderStatus */
  105.         $TargetStatus $TargetOrder->getOrderStatus();
  106.         if (!PluginManager::inPluginPurchase($TargetOrder$this->em)) {
  107.             // レンタルプラグインの受注でなければ処理しない
  108.             return;
  109.         }
  110.         // レンタルステータスが null になっているものは 出荷準備中にする
  111.         foreach ($TargetOrder->getShippings() as $Shipping) {
  112.             if ($Shipping->getRentalStatus() !== PluginManager::ORDER_STATUS_PREPARE) continue;
  113.             $Shipping->setRentalStatus(PluginManager::ORDER_STATUS_PREPARE);
  114.         }
  115.         if (empty($OriginStatus)) {
  116.             // 新規登録時には終了
  117.             return;
  118.         }
  119.         if ($OriginStatus->getId() === $TargetStatus->getId()) {
  120.             // Statusに変更がなければ終了
  121.             return;
  122.         }
  123.         // OrderStatusに連動してレンタル状況を変更
  124.         foreach ($TargetOrder->getShippings() as $Shipping) {
  125.             $this->stockService->interlockStatus($Shipping$TargetStatus);
  126.         }
  127.     }
  128.     /**
  129.      * 在庫を戻す. StateMachineの打ち消し
  130.      *
  131.      * @param Event $event
  132.      */
  133.     public function rollbackStock(WorkflowEvent $event)
  134.     {
  135.         /* @var Order $Order */
  136.         $Order $event->getSubject()->getOrder();
  137.         if (PluginManager::inPluginPurchase($Order$this->em)) {
  138.             $this->stockReduceProcessor->rollback($Order, new PurchaseContext());
  139.         }
  140.     }
  141.     /**
  142.      * 在庫を減らす. StateMachineの打ち消し
  143.      *
  144.      * @param Event $event
  145.      *
  146.      * @throws PurchaseFlow\PurchaseException
  147.      */
  148.     public function commitStock(WorkflowEvent $event)
  149.     {
  150.         /* @var Order $Order */
  151.         $Order $event->getSubject()->getOrder();
  152.         if (PluginManager::inPluginPurchase($Order$this->em)) {
  153.             $this->stockReduceProcessor->prepare($Order, new PurchaseContext());
  154.         }
  155.     }
  156.     /**
  157.      * 管理画面 HOME画面
  158.      * ・異常値チェック
  159.      */
  160.     public function onRenderAdminHome(TemplateEvent $templateEvent)
  161.     {
  162.         $this->stockService->updateAlert();
  163. //        $templateEvent->addSnippet('@SheebRental42/Admin/index.twig');
  164.         $templateEvent->addSnippet('@SheebRental42/Admin/alert.twig');
  165.     }
  166.     /**
  167.      * 管理画面 商品管理 規格設定画面
  168.      * ・レンタル設定要素追加
  169.      */
  170.     public function onRenderProductClassEdit(TemplateEvent $templateEvent)
  171.     {
  172.         $templateEvent->addSnippet('@SheebRental42/Admin/Product/product_class.twig');
  173.     }
  174.     /**
  175.      * 管理画面 HOME画面
  176.      * ・異常値チェック
  177.      */
  178.     public function onRenderOrderArchive(TemplateEvent $templateEvent)
  179.     {
  180.         $this->stockService->updateAlert();
  181.         $templateEvent->addSnippet('@SheebRental42/Admin/alert.twig');
  182.         $templateEvent->addSnippet('@SheebRental42/Admin/Order/index.twig');
  183.     }
  184.     /**
  185.      * 管理画面 受注管理 詳細画面
  186.      * ・受注要素追加
  187.      */
  188.     public function onRenderOrderEdit(TemplateEvent $templateEvent)
  189.     {
  190.         $templateEvent->addSnippet('@SheebRental42/Parts/datepick_snippet.twig');
  191.         $templateEvent->addAsset('@SheebRental42/Parts/datepick_asset.twig');
  192.         $templateEvent->addSnippet('@SheebRental42/Admin/Order/edit.twig');
  193.     }
  194.     /**
  195.      * 管理画面 受注管理 複数配送時 配送先編集画面
  196.      * ・受注要素追加
  197.      */
  198.     public function onRenderOrderShipping(TemplateEvent $templateEvent)
  199.     {
  200.         $templateEvent->addSnippet('@SheebRental42/Parts/datepick_snippet.twig');
  201.         $templateEvent->addAsset('@SheebRental42/Parts/datepick_asset.twig');
  202.         $templateEvent->addSnippet('@SheebRental42/Admin/Order/shipping.twig');
  203.     }
  204.     /**
  205.      * フロント画面 商品詳細
  206.      */
  207.     public function onRenderProductDetail(TemplateEvent $templateEvent)
  208.     {
  209.         $templateEvent->addSnippet('@SheebRental42/Product/detail.twig');
  210.         $templateEvent->addAsset('@SheebRental42/Product/detail_assets.twig');
  211.     }
  212.     /**
  213.      * フロント画面 注文手続き画面
  214.      */
  215.     public function onRenderShoppingIndex(TemplateEvent $templateEvent)
  216.     {
  217.         if (defined('EXIST_SHEEB_RENTAL')) {
  218.             $Order $templateEvent->getParameter('Order');
  219.             $Config $this->RentalConfigReposigory->get();
  220.             $fnValidDate = function (\DateTime $datetime\DateTime $min_date\DateTime $max_date) {
  221.                 if ($datetime $min_date) {
  222.                     return null;
  223.                 }
  224.                 if ($datetime $max_date) {
  225.                     return null;
  226.                 }
  227.                 return $datetime->format('Y-m-d');
  228.             };
  229.             $params = [];
  230.             /* @var $Shipping Shipping */
  231.             foreach ($Order->getShippings() as $idx => $Shipping) {
  232.                 // --- カレンダー選択可能範囲 ---
  233.                 [$min_start_days$max_end_days] = $this->stockService->getSelectableDateRangeByOrderItems(
  234.                     $Shipping->getOrderItems()
  235.                 );
  236.                 $params[$idx]['min_date'] = $min_start_days;
  237.                 $params[$idx]['max_date'] = $max_end_days;
  238.                 $start_date = (new \DateTime())->modify("+{$min_start_days} days");
  239.                 $start_date->setTime(00);
  240.                 $end_date = (new \DateTime())->modify("+{$max_end_days} days");
  241.                 $end_date->setTime(2359);
  242.                 $period StockService::createPeriod($start_date$end_date);
  243.                 // --- 営業日管理 ---
  244.                 [$start_disable_days$end_disable_days] = $this->stockService->getHolidays($period$Config);
  245.                 // --- 在庫管理 ---
  246.                 [$calendar$settings] = $this->stockService->getMergedStockCalendar($Shipping$period);
  247.                 $params[$idx]['start_calendar'] = $this->stockService->mergeDisableCalendar($calendar$start_disable_days);
  248.                 $params[$idx]['end_calendar'] = $this->stockService->mergeDisableCalendar($calendar$end_disable_days);
  249.                 $params[$idx]['period_start'] = $fnValidDate($Shipping->getPeriodStart(), $start_date$end_date);
  250.                 $params[$idx]['period_end'] = $fnValidDate($Shipping->getPeriodEnd(), $start_date$end_date);
  251.                 $params[$idx]['minRentalDays'] = $settings['minRentalDays'];
  252.                 $params[$idx]['maxRentalDays'] = $settings['maxRentalDays'];
  253.             }
  254.             // 最短利用開始可能日数をセット
  255.             $templateEvent->setParameter('args'json_encode(['params' => $params], JSON_UNESCAPED_UNICODE));
  256.             $templateEvent->addSnippet('@SheebRental42/Shopping/index.twig');
  257.             $templateEvent->addAsset('@SheebRental42/Shopping/index_assets.twig');
  258.         } else {
  259.             $templateEvent->addSnippet('@SheebRental42/Shopping/index_not_rental.twig');
  260.         }
  261.     }
  262.     /**
  263.      * フロント画面 注文確認画面
  264.      */
  265.     public function onRenderShoppingConfirm(TemplateEvent $templateEvent)
  266.     {
  267.         if (defined('EXIST_SHEEB_RENTAL')) {
  268.             // --- 送料や手数料計算の条件が、プラグインから他の明細を追加するとズレるのを修正 ---
  269.             $source $templateEvent->getSource();
  270.             // 送料
  271.             $source str_replace(
  272.                 '{% for item in shipping.order_items if item.isDeliveryFee %}',
  273.                 '{% for item in shipping.order_items if item.processor_name == "Eccube\\\\Service\\\\PurchaseFlow\\\\Processor\\\\DeliveryFeePreprocessor" %}',
  274.                 $source
  275.             );
  276.             // 手数料
  277.             $source str_replace(
  278.                 '{% for item in Order.order_items if item.isCharge %}',
  279.                 '{% for item in Order.order_items if item.processor_name == "Eccube\\\\Service\\\\PurchaseFlow\\\\Processor\\\\PaymentChargePreprocessor" %}',
  280.                 $source
  281.             );
  282.             $templateEvent->setSource($source);
  283.             // --- テンプレート追加 ---
  284.             $templateEvent->addSnippet('@SheebRental42/Shopping/confirm.twig');
  285.         }
  286.     }
  287.     /**
  288.      * マイページ購入履歴一覧
  289.      * ・レンタル状況追加
  290.      */
  291.     public function onRenderMypageHistoryArchive(TemplateEvent $templateEvent)
  292.     {
  293.         $templateEvent->addSnippet('@SheebRental42/Mypage/index.twig');
  294.     }
  295.     /**
  296.      * マイページ購入履歴詳細
  297.      * ・レンタル要素追加
  298.      */
  299.     public function onRenderMypageHistory(TemplateEvent $templateEvent)
  300.     {
  301.         if (PluginManager::inPluginPurchase($templateEvent->getParameter('Order'), $this->em)) {
  302.             $templateEvent->addSnippet('@SheebRental42/Mypage/history.twig');
  303.         }
  304.     }
  305. }