<?php
/*
* This file is part of CustomerClassPrice
*
* Copyright(c) Akira Kurozumi <info@a-zumi.net>
*
* https://a-zumi.net
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\CustomerClassPrice42\Doctrine\EventListener;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use Doctrine\Persistence\Event\LifecycleEventArgs;
use Eccube\Entity\Customer;
use Eccube\Entity\ProductClass;
use Eccube\Service\TaxRuleService;
use Plugin\CustomerClassPrice42\Entity\CustomerClassPrice;
use Plugin\CustomerClassPrice42\Service\DiscountHelper;
use Symfony\Component\Security\Core\Security;
class CustomerClassPriceEventListener
{
/**
* @var EntityManagerInterface
*/
private EntityManagerInterface $entityManager;
/**
* @var TaxRuleService
*/
private TaxRuleService $taxRuleService;
/**
* @var DiscountHelper
*/
private DiscountHelper $discountHelper;
/**
* @var Security
*/
private Security $security;
public function __construct(
EntityManagerInterface $entityManager,
TaxRuleService $taxRuleService,
DiscountHelper $discountHelper,
Security $security
) {
$this->entityManager = $entityManager;
$this->taxRuleService = $taxRuleService;
$this->discountHelper = $discountHelper;
$this->security = $security;
}
/**
* @param LifecycleEventArgs $args
*
* @return void
*/
public function postLoad(LifecycleEventArgs $args): void
{
$entity = $args->getObject();
/*
* 非会員設定部分
*/
if ($this->supportsNonmember($entity)) {
global $kernel;
$container = $kernel->getContainer();
$twig = $container->get('twig');
$globals = $twig->getGlobals();
$nonmemberFlag = $globals['nonmember_flag'];
$nonmemberRatio = intval($globals['nonmember_ratio']);
if ($nonmemberFlag) {
//error_log( print_r($nonmemberRatio, true) . "\n", 3, "/home/liquor-sato/temp/log.txt");
$entity->setPrice02IncTax(
$this->taxRuleService->getPriceIncTax(
$this->discountHelper->calculatePriceNonmember($entity->getPrice02(), $nonmemberRatio),
$entity->getProduct(),
$entity
)
);
// 商品一覧や商品詳細で税抜の販売価格を割引価格に設定
$entity->setPrice02($this->discountHelper->calculatePriceNonmember($entity->getPrice02(), $nonmemberRatio));
}
}
if (false === $this->supports($entity)) {
return;
}
/** @var Customer $customer */
$customer = $this->security->getUser();
$customerClass = $customer->getPlgCcpCustomerClass();
// 割引率が設定されている特定会員だったら割引価格セット
if ($customerClass->getDiscountRate()) {
$entity->setPrice02IncTax(
$this->taxRuleService->getPriceIncTax(
$this->discountHelper->calculatePrice($entity->getPrice02(), $customer),
$entity->getProduct(),
$entity
)
);
// 商品一覧や商品詳細で税抜の販売価格を割引価格に設定
$entity->setPrice02($this->discountHelper->calculatePrice($entity->getPrice02(), $customer));
}
$customerClassPrice = $this->entityManager->getRepository(CustomerClassPrice::class)->findOneBy([
'customerClass' => $customerClass,
'productClass' => $entity,
]);
// 特定会員価格が設定されていたら価格をセット
if ($customerClassPrice instanceof CustomerClassPrice) {
if ($customerClassPrice->getPrice()) {
$entity->setPrice02IncTax(
$this->taxRuleService->getPriceIncTax(
$customerClassPrice->getPrice(),
$entity->getProduct(),
$entity
)
);
// 商品一覧や商品詳細で税抜の販売価格を特定会員価格に設定
$entity->setPrice02($customerClassPrice->getPrice());
}
}
}
/**
* @param PreUpdateEventArgs $args
*
* @return void
*/
public function preUpdate(PreUpdateEventArgs $args): void
{
$entity = $args->getObject();
//if (false === $this->supports($entity)) {
// return;
//}
if (false === $this->supports($entity) && false === $this->supportsNonmember($entity)) {
return;
}
// 特定会員価格に設定された販売価格をもとに戻す
if ($args->hasChangedField('price02')) {
$price02 = $args->getOldValue('price02');
$entity->setPrice02($price02);
}
}
/**
* @param $entity
*
* @return bool
*/
protected function supports($entity): bool
{
if (!$entity instanceof ProductClass) {
return false;
}
// 割引対象外だったらスルー
if (false === $entity->getProduct()->isPlgCcpEnabledDiscount()) {
return false;
}
if (!$this->security->getUser() instanceof Customer) {
return false;
}
// 特定会員登録されているか確認
if (!$this->security->getUser()->isPlgCcpCustomerClass()) {
return false;
}
return true;
}
protected function supportsNonmember($entity): bool
{
// 非会員なのでsupportsと逆
if ($this->security->getUser() instanceof Customer) {
return false;
}
// supports同様
if (!$entity instanceof ProductClass) {
return false;
}
// supports同様
// 割引対象外だったらスルー
if (false === $entity->getProduct()->isPlgCcpEnabledDiscount()) {
return false;
}
if (preg_match('/saketen_admin/', $_SERVER['REQUEST_URI'])) {
return false;
}
return true;
}
}