app/Plugin/CustomerClassPrice42/Doctrine/EventListener/CustomerClassPriceEventListener.php line 76

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of CustomerClassPrice
  4.  *
  5.  * Copyright(c) Akira Kurozumi <info@a-zumi.net>
  6.  *
  7.  * https://a-zumi.net
  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\CustomerClassPrice42\Doctrine\EventListener;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Doctrine\ORM\Event\PreUpdateEventArgs;
  15. use Doctrine\Persistence\Event\LifecycleEventArgs;
  16. use Eccube\Entity\Customer;
  17. use Eccube\Entity\ProductClass;
  18. use Eccube\Service\TaxRuleService;
  19. use Plugin\CustomerClassPrice42\Entity\CustomerClassPrice;
  20. use Plugin\CustomerClassPrice42\Service\DiscountHelper;
  21. use Symfony\Component\Security\Core\Security;
  22. class CustomerClassPriceEventListener
  23. {
  24.     /**
  25.      * @var EntityManagerInterface
  26.      */
  27.     private EntityManagerInterface $entityManager;
  28.     /**
  29.      * @var TaxRuleService
  30.      */
  31.     private TaxRuleService $taxRuleService;
  32.     /**
  33.      * @var DiscountHelper
  34.      */
  35.     private DiscountHelper $discountHelper;
  36.     /**
  37.      * @var Security
  38.      */
  39.     private Security $security;
  40.     public function __construct(
  41.         EntityManagerInterface $entityManager,
  42.         TaxRuleService $taxRuleService,
  43.         DiscountHelper $discountHelper,
  44.         Security $security
  45.     ) {
  46.         $this->entityManager $entityManager;
  47.         $this->taxRuleService $taxRuleService;
  48.         $this->discountHelper $discountHelper;
  49.         $this->security $security;
  50.     }
  51.     /**
  52.      * @param LifecycleEventArgs $args
  53.      *
  54.      * @return void
  55.      */
  56.     public function postLoad(LifecycleEventArgs $args): void
  57.     {
  58.         $entity $args->getObject();
  59.         /*
  60.          * 非会員設定部分
  61.          */
  62.         if ($this->supportsNonmember($entity)) {
  63.             global $kernel;
  64.             $container $kernel->getContainer();
  65.             $twig $container->get('twig');
  66.             $globals $twig->getGlobals();
  67.             $nonmemberFlag $globals['nonmember_flag'];
  68.             $nonmemberRatio intval($globals['nonmember_ratio']);
  69.             if ($nonmemberFlag) {
  70.                 //error_log( print_r($nonmemberRatio, true) . "\n", 3, "/home/liquor-sato/temp/log.txt");
  71.                 $entity->setPrice02IncTax(
  72.                     $this->taxRuleService->getPriceIncTax(
  73.                         $this->discountHelper->calculatePriceNonmember($entity->getPrice02(), $nonmemberRatio),
  74.                         $entity->getProduct(),
  75.                         $entity
  76.                     )
  77.                 );
  78.                 // 商品一覧や商品詳細で税抜の販売価格を割引価格に設定
  79.                 $entity->setPrice02($this->discountHelper->calculatePriceNonmember($entity->getPrice02(), $nonmemberRatio));
  80.             }
  81.         }
  82.         if (false === $this->supports($entity)) {
  83.             return;
  84.         }
  85.         /** @var Customer $customer */
  86.         $customer $this->security->getUser();
  87.         $customerClass $customer->getPlgCcpCustomerClass();
  88.         // 割引率が設定されている特定会員だったら割引価格セット
  89.         if ($customerClass->getDiscountRate()) {
  90.             $entity->setPrice02IncTax(
  91.                 $this->taxRuleService->getPriceIncTax(
  92.                     $this->discountHelper->calculatePrice($entity->getPrice02(), $customer),
  93.                     $entity->getProduct(),
  94.                     $entity
  95.                 )
  96.             );
  97.             // 商品一覧や商品詳細で税抜の販売価格を割引価格に設定
  98.             $entity->setPrice02($this->discountHelper->calculatePrice($entity->getPrice02(), $customer));
  99.         }
  100.         $customerClassPrice $this->entityManager->getRepository(CustomerClassPrice::class)->findOneBy([
  101.             'customerClass' => $customerClass,
  102.             'productClass' => $entity,
  103.         ]);
  104.         // 特定会員価格が設定されていたら価格をセット
  105.         if ($customerClassPrice instanceof CustomerClassPrice) {
  106.             if ($customerClassPrice->getPrice()) {
  107.                 $entity->setPrice02IncTax(
  108.                     $this->taxRuleService->getPriceIncTax(
  109.                         $customerClassPrice->getPrice(),
  110.                         $entity->getProduct(),
  111.                         $entity
  112.                     )
  113.                 );
  114.                 // 商品一覧や商品詳細で税抜の販売価格を特定会員価格に設定
  115.                 $entity->setPrice02($customerClassPrice->getPrice());
  116.             }
  117.         }
  118.     }
  119.     /**
  120.      * @param PreUpdateEventArgs $args
  121.      *
  122.      * @return void
  123.      */
  124.     public function preUpdate(PreUpdateEventArgs $args): void
  125.     {
  126.         $entity $args->getObject();
  127.         //if (false === $this->supports($entity)) {
  128.         //    return;
  129.         //}
  130.         if (false === $this->supports($entity) && false === $this->supportsNonmember($entity)) {
  131.             return;
  132.         }
  133.         // 特定会員価格に設定された販売価格をもとに戻す
  134.         if ($args->hasChangedField('price02')) {
  135.             $price02 $args->getOldValue('price02');
  136.             $entity->setPrice02($price02);
  137.         }
  138.     }
  139.     /**
  140.      * @param $entity
  141.      *
  142.      * @return bool
  143.      */
  144.     protected function supports($entity): bool
  145.     {
  146.         if (!$entity instanceof ProductClass) {
  147.             return false;
  148.         }
  149.         // 割引対象外だったらスルー
  150.         if (false === $entity->getProduct()->isPlgCcpEnabledDiscount()) {
  151.             return false;
  152.         }
  153.         if (!$this->security->getUser() instanceof Customer) {
  154.             return false;
  155.         }
  156.         // 特定会員登録されているか確認
  157.         if (!$this->security->getUser()->isPlgCcpCustomerClass()) {
  158.             return false;
  159.         }
  160.         return true;
  161.     }
  162.     protected function supportsNonmember($entity): bool
  163.     {
  164.         // 非会員なのでsupportsと逆
  165.         if ($this->security->getUser() instanceof Customer) {
  166.             return false;
  167.         }
  168.         // supports同様
  169.         if (!$entity instanceof ProductClass) {
  170.             return false;
  171.         }
  172.         // supports同様
  173.         // 割引対象外だったらスルー
  174.         if (false === $entity->getProduct()->isPlgCcpEnabledDiscount()) {
  175.             return false;
  176.         }
  177.         if (preg_match('/saketen_admin/'$_SERVER['REQUEST_URI'])) {
  178.             return false;
  179.         }
  180.         return true;
  181.     }
  182. }