app/Plugin/lecastPriceSelectBlock41/Event.php line 57

Open in your IDE?
  1. <?php
  2. /*
  3.   * This file is part of the LecastTableList plugin
  4.   *
  5.   * Copyright (C) >=2020 lecast system.
  6.   * @author Tetsuji Shiro
  7.   *
  8.   * このプラグインは再販売禁止です。
  9.   */
  10. namespace Plugin\lecastPriceSelectBlock41;
  11. use Eccube\Event\EccubeEvents;
  12. use Eccube\Event\EventArgs;
  13. use Eccube\Entity\BaseInfo;
  14. use Eccube\Repository\BaseInfoRepository;
  15. use Eccube\Repository\TaxRuleRepository;
  16. use Symfony\Component\Form\Extension\Core\Type\TextType;
  17. use Doctrine\ORM\EntityManagerInterface;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. class Event implements EventSubscriberInterface
  20. {
  21.     /**
  22.      * @var EntityManagerInterface
  23.      */
  24.     private $entityManager;
  25.     /**
  26.      * @var BaseInfo
  27.      */
  28.     protected $BaseInfo;
  29.     /**
  30.      * @var TaxRuleRepository
  31.      */
  32.     protected $taxRuleRepository;
  33.     public function __construct(
  34.         EntityManagerInterface $entityManager,
  35.         BaseInfoRepository $baseInfoRepository,
  36.         TaxRuleRepository $taxRuleRepository
  37.     ) {
  38.         $this->entityManager $entityManager;
  39.         $this->BaseInfo $baseInfoRepository->get();
  40.         $this->taxRuleRepository $taxRuleRepository;
  41.     }
  42.     public static function getSubscribedEvents()
  43.     {
  44.         return [
  45.             EccubeEvents::FRONT_PRODUCT_INDEX_SEARCH => 'onFrontProductIndexSearch',
  46.             EccubeEvents::FRONT_PRODUCT_INDEX_INITIALIZE => 'onFrontProductIndexInitialize',
  47.         ];
  48.     }
  49.     public function onFrontProductIndexInitialize(EventArgs $event)
  50.     {
  51.         $builder $event->getArgument('builder');
  52.         $builder->add('price_range'TextType::class, [
  53.             ]);
  54.     }
  55.     public function onFrontProductIndexSearch(EventArgs $event)
  56.     {
  57.         $qb $event->getArgument('qb');
  58.         $searchData $event->getArgument('searchData');
  59.         if(empty($searchData['price_range'])){
  60.             return;
  61.         }
  62.         // 税率も計算する
  63.         $taxrule null;
  64.         if(!$this->BaseInfo->isOptionProductTaxRule()){
  65.             $taxrule $this->taxRuleRepository->getByRule();
  66.         }
  67.         if(!in_array('pc'$qb->getAllAliases())){
  68.             $qb->innerJoin('p.ProductClasses''pc');
  69.         }
  70.         $range explode('-'$searchData['price_range']);
  71.         $min_value $range[0];
  72.         $max_value $range[1];
  73.         if (!empty($searchData['price_range'])) {
  74.             if(!empty($taxrule)){// 全体税率
  75.                 $qb->andWhere('pc.price02 * :TaxRule >= :MinValue');
  76.                 $qb->setParameter("MinValue"$min_value);
  77.                 if($max_value){
  78.                     $qb->andWhere('pc.price02 * :TaxRule <= :MaxValue');
  79.                     $qb->setParameter("MaxValue"$max_value);
  80.                 }
  81.                 $qb->setParameter("TaxRule", ($taxrule->getTaxRate()+100)/100);
  82.             }else{// 個別税率
  83.                 $qb->leftJoin('Eccube\Entity\TaxRule''t''WITH''pc.id = t.ProductClass');
  84.                 $qb->andWhere('pc.price02 * ((t.tax_rate+100)/100) >= :MinValue');
  85.                 $qb->setParameter("MinValue"$min_value);
  86.                 if($max_value){
  87.                     $qb->andWhere('pc.price02 * ((t.tax_rate+100)/100) <= :MaxValue');
  88.                     $qb->setParameter("MaxValue"$max_value);
  89.                 }
  90.             }
  91.         }
  92.     }
  93. }