<?php
/*
* Plugin Name : CustomerRank
*
* Copyright (C) BraTech Co., Ltd. All Rights Reserved.
* http://www.bratech.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\CustomerRank42\Event;
use Doctrine\ORM\EntityManagerInterface;
use Eccube\Event\EventArgs;
use Plugin\CustomerRank42\Entity\CustomerPrice;
use Plugin\CustomerRank42\Repository\CustomerPriceRepository;
use Plugin\CustomerRank42\Repository\CustomerRankRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CsvImportProductExtEvent implements EventSubscriberInterface
{
private $entityManager;
private $customerRankRepository;
private $customerPriceRepository;
/**
* CustomerRankController constructor.
* @param CustomerRankRepository $customerRankRepository
*/
public function __construct(
EntityManagerInterface $entityManager,
CustomerRankRepository $customerRankRepository,
CustomerPriceRepository $customerPriceRepository
)
{
$this->entityManager = $entityManager;
$this->customerRankRepository = $customerRankRepository;
$this->customerPriceRepository = $customerPriceRepository;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'csvimportproductext.admin.product.csv.import.product.descriptions' => 'hookAdminProductCsvImportProductDescriptions',
'csvimportproductext.admin.product.csv.import.product.check'=> 'hookAdminProductCsvImportProductCheck',
'csvimportproductext.admin.product.csv.import.product.process' => 'hookAdminProductCsvImportProductProcess',
];
}
public function hookAdminProductCsvImportProductDescriptions(EventArgs $event)
{
$header = $event->getArgument('header');
$key = $event->getArgument('key');
$CustomerRanks = $this->customerRankRepository->findAll();
foreach($CustomerRanks as $CustomerRank){
if($key == $CustomerRank->getName() . trans('customerrank.common.customer_price')){
$header['description'] = trans('customerrank.admin.product.product_csv.customer_price_description');
$header['required'] = false;
}
}
$event->setArgument('header',$header);
}
public function hookAdminProductCsvImportProductCheck(EventArgs $event)
{
$row = $event->getArgument('row');
$lineNo = $event->getArgument('lineNo');
$errors = $event->getArgument('errors');
$CustomerRanks = $this->customerRankRepository->findAll();
foreach($CustomerRanks as $CustomerRank){
if(isset($row[$CustomerRank->getName(). trans('customerrank.common.customer_price')])){
if($row[$CustomerRank->getName(). trans('customerrank.common.customer_price')] !== '' && !is_numeric($row[$CustomerRank->getName(). trans('customerrank.common.customer_price')])){
$message = trans('admin.common.csv_invalid_greater_than_zero', [
'%line%' => $lineNo,
'%name%' => $CustomerRank->getName(). trans('customerrank.common.customer_price'),
]);
$errors[] = $message;
}
}
}
$event->setArgument('errors',$errors);
}
public function hookAdminProductCsvImportProductProcess(EventArgs $event)
{
$row = $event->getArgument('row');
$ProductClass = $event->getArgument('ProductClass');
$CustomerRanks = $this->customerRankRepository->findAll();
foreach($CustomerRanks as $CustomerRank){
if(isset($row[$CustomerRank->getName() . trans('customerrank.common.customer_price')])){
$plgPrice = $this->customerPriceRepository->findOneBy(['ProductClass' => $ProductClass, 'CustomerRank' => $CustomerRank]);
if($row[$CustomerRank->getName() . trans('customerrank.common.customer_price')] != ''){
if(is_null($plgPrice)){
$plgPrice = new CustomerPrice();
$plgPrice->setProductClass($ProductClass);
$plgPrice->setCustomerRank($CustomerRank);
}
$plgPrice->setPrice($row[$CustomerRank->getName() . trans('customerrank.common.customer_price')]);
$this->entityManager->persist($plgPrice);
}else{
if(isset($plgPrice))$this->entityManager->remove($plgPrice);
}
}
if(isset($plgPrice))unset($plgPrice);
}
}
}