<?php
/*
* This file is part of the LecastTableList plugin
*
* Copyright (C) >=2020 lecast system.
* @author Tetsuji Shiro
*
* このプラグインは再販売禁止です。
*/
namespace Plugin\lecastPriceSelectBlock41;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Eccube\Entity\BaseInfo;
use Eccube\Repository\BaseInfoRepository;
use Eccube\Repository\TaxRuleRepository;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class Event implements EventSubscriberInterface
{
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var BaseInfo
*/
protected $BaseInfo;
/**
* @var TaxRuleRepository
*/
protected $taxRuleRepository;
public function __construct(
EntityManagerInterface $entityManager,
BaseInfoRepository $baseInfoRepository,
TaxRuleRepository $taxRuleRepository
) {
$this->entityManager = $entityManager;
$this->BaseInfo = $baseInfoRepository->get();
$this->taxRuleRepository = $taxRuleRepository;
}
public static function getSubscribedEvents()
{
return [
EccubeEvents::FRONT_PRODUCT_INDEX_SEARCH => 'onFrontProductIndexSearch',
EccubeEvents::FRONT_PRODUCT_INDEX_INITIALIZE => 'onFrontProductIndexInitialize',
];
}
public function onFrontProductIndexInitialize(EventArgs $event)
{
$builder = $event->getArgument('builder');
$builder->add('price_range', TextType::class, [
]);
}
public function onFrontProductIndexSearch(EventArgs $event)
{
$qb = $event->getArgument('qb');
$searchData = $event->getArgument('searchData');
if(empty($searchData['price_range'])){
return;
}
// 税率も計算する
$taxrule = null;
if(!$this->BaseInfo->isOptionProductTaxRule()){
$taxrule = $this->taxRuleRepository->getByRule();
}
if(!in_array('pc', $qb->getAllAliases())){
$qb->innerJoin('p.ProductClasses', 'pc');
}
$range = explode('-', $searchData['price_range']);
$min_value = $range[0];
$max_value = $range[1];
if (!empty($searchData['price_range'])) {
if(!empty($taxrule)){// 全体税率
$qb->andWhere('pc.price02 * :TaxRule >= :MinValue');
$qb->setParameter("MinValue", $min_value);
if($max_value){
$qb->andWhere('pc.price02 * :TaxRule <= :MaxValue');
$qb->setParameter("MaxValue", $max_value);
}
$qb->setParameter("TaxRule", ($taxrule->getTaxRate()+100)/100);
}else{// 個別税率
$qb->leftJoin('Eccube\Entity\TaxRule', 't', 'WITH', 'pc.id = t.ProductClass');
$qb->andWhere('pc.price02 * ((t.tax_rate+100)/100) >= :MinValue');
$qb->setParameter("MinValue", $min_value);
if($max_value){
$qb->andWhere('pc.price02 * ((t.tax_rate+100)/100) <= :MaxValue');
$qb->setParameter("MaxValue", $max_value);
}
}
}
}
}