app/Plugin/PinpointSaleDx/EventSubscriber/MypageEventSubscriber.php line 58

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright(c) 2019 SYSTEM_KD
  4.  * Date: 2019/08/24
  5.  */
  6. namespace Plugin\PinpointSaleDx\EventSubscriber;
  7. use Eccube\Entity\Order;
  8. use Eccube\Entity\OrderItem;
  9. use Eccube\Event\TemplateEvent;
  10. use Plugin\PinpointSaleDx\Config\ConfigSetting;
  11. use Plugin\PinpointSaleDx\Config\ConfigSupportTrait;
  12. use Plugin\PinpointSaleDx\Service\PinpointSaleDxService;
  13. use Plugin\PinpointSaleDx\Service\PlgConfigService\ConfigService;
  14. use Plugin\PinpointSaleDx\Service\PurchaseFlow\Processor\PinpointSaleDxDiscountProcessor;
  15. use Plugin\PinpointSaleDx\Service\TwigRenderService\TwigRenderService;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class MypageEventSubscriber implements EventSubscriberInterface
  18. {
  19.     /** @var TwigRenderService */
  20.     protected $twigRenderService;
  21.     /** @var PinpointSaleDxService */
  22.     protected $pinpointSaleService;
  23.     /** @var ConfigService */
  24.     protected $configService;
  25.     use ConfigSupportTrait;
  26.     /**
  27.      * MypageEventSubscriber constructor.
  28.      * @param TwigRenderService $twigRenderService
  29.      * @param PinpointSaleDxService $pinpointSaleService
  30.      * @param ConfigService $configService
  31.      */
  32.     public function __construct(
  33.         TwigRenderService $twigRenderService,
  34.         PinpointSaleDxService $pinpointSaleService,
  35.         ConfigService $configService
  36.     )
  37.     {
  38.         $this->twigRenderService $twigRenderService;
  39.         $this->pinpointSaleService $pinpointSaleService;
  40.         $this->configService $configService;
  41.     }
  42.     /**
  43.      * 注文履歴詳細 テンプレート
  44.      *
  45.      * @param TemplateEvent $event
  46.      * @throws \Exception
  47.      */
  48.     public function onTemplateMypageHistory(TemplateEvent $event)
  49.     {
  50.         // タイムセール名称設定
  51.         $this->setDiscountName($event);
  52.         if (!$this->configService->isKeyBool(ConfigSetting::SETTING_KEY_HISTORY_VIEW)) {
  53.             return;
  54.         }
  55.         $this->twigRenderService->initRenderService($event);
  56.         // 値引き額
  57.         $pinpointDiscounts = [];
  58.         // 現在の値引き額
  59.         $nowPinpointDiscounts = [];
  60.         // タイムセール変更状態
  61.         $pinpointDiscountChange false;
  62.         /** @var Order $order */
  63.         $order $event->getParameter('Order');
  64.         /** @var OrderItem $orderItem */
  65.         foreach ($order->getItems() as $orderItem) {
  66.             // 受注よりタイムセール値引き額取得
  67.             if ($orderItem->getProcessorName() == PinpointSaleDxDiscountProcessor::class) {
  68.                 $shipping_id $orderItem->getShipping()->getId();
  69.                 $product_class_id $orderItem->getProductClass()->getId();
  70.                 $key $shipping_id '-' $product_class_id;
  71.                 $pinpointDiscounts[$key] = ($orderItem->getPriceIncTax() * $orderItem->getQuantity());
  72.             }
  73.         }
  74.         foreach ($order->getItems() as $orderItem) {
  75.             // 現在のタイムセール値引き額取得
  76.             if ($orderItem->isProduct()) {
  77.                 $shipping_id $orderItem->getShipping()->getId();
  78.                 $product_class_id $orderItem->getProductClass()->getId();
  79.                 $key $shipping_id '-' $product_class_id;
  80.                 $pinpointSaleItem $this->pinpointSaleService->getPinpointSaleDxItem($orderItem->getProductClass());
  81.                 if ($pinpointSaleItem && $pinpointSaleItem->isActive()) {
  82.                     // セール価格あり
  83.                     $discountPrice $pinpointSaleItem->getDiscountPriceIncTax();
  84.                     $discountPriceSubtotal = -* ($discountPrice $orderItem->getQuantity());
  85.                     if (isset($pinpointDiscounts[$key])
  86.                         && $pinpointDiscounts[$key] == $discountPriceSubtotal) {
  87.                         // 購入時点と同じ状態のため表示しない
  88.                         continue;
  89.                     }
  90.                     if ($discountPriceSubtotal 0) {
  91.                         // 値引ではなく増額のため表示しない
  92.                         continue;
  93.                     }
  94.                     // タイムセール状態に変更有りマーク
  95.                     $pinpointDiscountChange true;
  96.                 } else {
  97.                     // セール外
  98.                     $discountPriceSubtotal 0;
  99.                 }
  100.                 $nowPinpointDiscounts[$key] = $discountPriceSubtotal;
  101.             }
  102.         }
  103.         // 受注のタイムセール値引情報
  104.         $event->setParameter('pinpointDiscounts'$pinpointDiscounts);
  105.         // 現在のタイムセール値引情報
  106.         $event->setParameter('nowPinpointDiscounts'$nowPinpointDiscounts);
  107.         if ($this->configService->isKeyBool(ConfigSetting::SETTING_KEY_HISTORY_VIEW)) {
  108.             /* 表示制御 */
  109.             // タイムセールレコード追加
  110.             $childes[] = $this->twigRenderService
  111.                 ->eachChildBuilder()
  112.                 ->findThis()
  113.                 ->find('.ec-imageGrid__content')
  114.                 ->targetFindAndIndexKey('#pinpoint_sale_history_'"pinpointSaleIndex")
  115.                 ->setInsertModeAppend();
  116.             $this->twigRenderService
  117.                 ->eachBuilder()
  118.                 ->find('.ec-orderDelivery__item')
  119.                 ->setEachIndexKey('pinpointSaleIndex')
  120.                 ->each($childes);
  121.         }
  122.         // タイムセール変更状態
  123.         if ($pinpointDiscountChange) {
  124.             // 変更状態通知表示
  125.             $this->twigRenderService
  126.                 ->insertBuilder()
  127.                 ->find('.ec-orderRole__summary')
  128.                 ->eq(0)
  129.                 ->setTargetId('pinpoint_sale_message')
  130.                 ->setInsertModeAppend();
  131.         }
  132.         $this->twigRenderService->addSupportSnippet('@PinpointSaleDx/default/Mypage/history_ex.twig');
  133.         $event->addAsset('@PinpointSaleDx/default/Mypage/history_ex_css.twig');
  134.     }
  135.     /**
  136.      * Returns an array of event names this subscriber wants to listen to.
  137.      *
  138.      * The array keys are event names and the value can be:
  139.      *
  140.      *  * The method name to call (priority defaults to 0)
  141.      *  * An array composed of the method name to call and the priority
  142.      *  * An array of arrays composed of the method names to call and respective
  143.      *    priorities, or 0 if unset
  144.      *
  145.      * For instance:
  146.      *
  147.      *  * ['eventName' => 'methodName']
  148.      *  * ['eventName' => ['methodName', $priority]]
  149.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  150.      *
  151.      * @return array The event names to listen to
  152.      */
  153.     public static function getSubscribedEvents()
  154.     {
  155.         return [
  156.             'Mypage/history.twig' => ['onTemplateMypageHistory'],
  157.         ];
  158.     }
  159. }