app/Plugin/CustomerRank42/Event/AdminProductEvent.php line 71

Open in your IDE?
  1. <?php
  2. /*
  3. * Plugin Name : CustomerRank
  4. *
  5. * Copyright (C) BraTech Co., Ltd. All Rights Reserved.
  6. * http://www.bratech.co.jp/
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Plugin\CustomerRank42\Event;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Eccube\Event\EccubeEvents;
  14. use Eccube\Event\EventArgs;
  15. use Eccube\Event\TemplateEvent;
  16. use Eccube\Repository\ProductClassRepository;
  17. use Plugin\CustomerRank42\Entity\CustomerPrice;
  18. use Plugin\CustomerRank42\Repository\CustomerPriceRateRepository;
  19. use Plugin\CustomerRank42\Repository\CustomerPriceRepository;
  20. use Plugin\CustomerRank42\Repository\CustomerRankRepository;
  21. use Plugin\CustomerRank42\Service\MemberPriceService;
  22. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  23. class AdminProductEvent implements EventSubscriberInterface
  24. {
  25.     private $entityManager;
  26.     private $productClassRepository;
  27.     private $customerRankRepository;
  28.     private $customerPriceRepository;
  29.     private $customerPriceRateRepository;
  30.     /**
  31.      * CustomerRankController constructor.
  32.      * @param CustomerRankRepository $customerRankRepository
  33.      */
  34.     public function __construct(
  35.             EntityManagerInterface $entityManager,
  36.             ProductClassRepository $productClassRepository,
  37.             CustomerRankRepository $customerRankRepository,
  38.             CustomerPriceRepository $customerPriceRepository,
  39.             CustomerPriceRateRepository $customerPriceRateRepository
  40.             )
  41.     {
  42.         $this->entityManager $entityManager;
  43.         $this->productClassRepository $productClassRepository;
  44.         $this->customerRankRepository $customerRankRepository;
  45.         $this->customerPriceRepository $customerPriceRepository;
  46.         $this->customerPriceRateRepository $customerPriceRateRepository;
  47.     }
  48.     /**
  49.      * @return array
  50.      */
  51.     public static function getSubscribedEvents()
  52.     {
  53.         return [
  54.             '@admin/Product/product.twig' => 'onTemplateAdminProductEdit',
  55.             EccubeEvents::ADMIN_PRODUCT_EDIT_COMPLETE => 'hookAdminProductEditComplete',
  56.             EccubeEvents::ADMIN_PRODUCT_COPY_COMPLETE => 'hookAdminProductCopyComplete',
  57.             EccubeEvents::ADMIN_PRODUCT_CSV_EXPORT => 'hookAdminProductCsvExport',
  58.             '@admin/Product/product_class.twig' => 'onTemplateAdminProductClassEdit',
  59.         ];
  60.     }
  61.     public function onTemplateAdminProductEdit(TemplateEvent $event)
  62.     {
  63.         $parameters $event->getParameters();
  64.         $CustomerRanks $this->customerRankRepository->getList();
  65.         $parameters['CustomerRanks'] = $CustomerRanks;
  66.         // 会員価格自動計算JS用(利率マスター・地金判定のsale_type_id)
  67.         // 参照: docs/会員価格自動反映_V4仕様書.md
  68.         $parameters['PriceRateHash'] = $this->customerPriceRateRepository->getRateHash();
  69.         $parameters['SaleTypeJigane'] = MemberPriceService::SALE_TYPE_JIGANE;
  70.         $parameters['RegularRankId'] = MemberPriceService::REGULAR_RANK_ID;
  71.         $event->setParameters($parameters);
  72.         $twig '@CustomerRank42/admin/Product/customer_price.twig';
  73.         $event->addSnippet($twig);
  74.         $js '@CustomerRank42/admin/Product/customer_price.js';
  75.         $event->addAsset($js);
  76.     }
  77.     public function hookAdminProductEditComplete(EventArgs $event)
  78.     {
  79.         $Product $event->getArgument('Product');
  80.         $form $event->getArgument('form');
  81.         $has_class $Product->hasProductClass();
  82.         if(!$has_class){
  83.             $CustomerRanks $this->customerRankRepository->getList();
  84.             $ProductClass $form['class']->getData();
  85.             foreach($CustomerRanks as $CustomerRank){
  86.                 if($form['class']->has('customer_price_'$CustomerRank->getId())){
  87.                     $CustomerPrice $this->customerPriceRepository->findOneBy(['ProductClass' => $ProductClass'CustomerRank' => $CustomerRank]);
  88.                     if(!$CustomerPrice){
  89.                         $CustomerPrice =  new CustomerPrice();
  90.                         $CustomerPrice->setProductClass($ProductClass);
  91.                         $CustomerPrice->setCustomerRank($CustomerRank);
  92.                     }
  93.                     $CustomerPrice->setPrice($form['class']->get('customer_price_'$CustomerRank->getId())->getData());
  94.                     $this->entityManager->persist($CustomerPrice);
  95.                 }
  96.             }
  97.             $this->entityManager->flush();
  98.         }
  99.     }
  100.     public function hookAdminProductCopyComplete(EventArgs $event)
  101.     {
  102.         $Product $event->getArgument('Product');
  103.         $CopyProduct $event->getArgument('CopyProduct');
  104.         $orgProductClasses $Product->getProductClasses();
  105.         $CustomerRanks $this->customerRankRepository->getList();
  106.         foreach($CustomerRanks as $CustomerRank){
  107.             foreach ($orgProductClasses as $ProductClass) {
  108.                 $CopyProductClass $this->productClassRepository->findOneBy(['Product'=> $CopyProduct'ClassCategory1' => $ProductClass->getClassCategory1(), 'ClassCategory2' => $ProductClass->getClassCategory2()]);
  109.                 $orgCustomerPrice $this->customerPriceRepository->findOneBy(['ProductClass' => $ProductClass'CustomerRank' => $CustomerRank]);
  110.                 if($CopyProductClass){
  111.                     $CustomerPrice = new CustomerPrice();
  112.                     $CustomerPrice->setProductClass($CopyProductClass);
  113.                     $CustomerPrice->setCustomerRank($CustomerRank);
  114.                     if($orgCustomerPrice){
  115.                         $CustomerPrice->setPrice($orgCustomerPrice->getPrice());
  116.                     }
  117.                     $this->entityManager->persist($CustomerPrice);
  118.                 }
  119.             }
  120.         }
  121.         $this->entityManager->flush();
  122.     }
  123.     public function hookAdminProductCsvExport(EventArgs $event)
  124.     {
  125.         $ExportCsvRow $event->getArgument('ExportCsvRow');
  126.         if ($ExportCsvRow->isDataNull()) {
  127.             $csvService $event->getArgument('csvService');
  128.             $ProductClass $event->getArgument('ProductClass');
  129.             $Csv $event->getArgument('Csv');
  130.             $csvEntityName str_replace('\\\\''\\'$Csv->getEntityName());
  131.             if($csvEntityName == 'Plugin\CustomerRank42\Entity\CustomerPrice'){
  132.                 $customer_rank_id ltrim($Csv->getFieldName(), 'customerrank_price_');
  133.                 if(is_numeric($customer_rank_id)){
  134.                     $CustomerRank $this->customerRankRepository->find($customer_rank_id);
  135.                     if(!is_null($CustomerRank)){
  136.                         $CustomerPrice $this->customerPriceRepository->findOneBy(['ProductClass' => $ProductClass'CustomerRank' => $CustomerRank]);
  137.                         if(!is_null($CustomerPrice)){
  138.                             $ExportCsvRow->setData($CustomerPrice->getPrice());
  139.                         }
  140.                     }
  141.                 }
  142.             }
  143.         }
  144.     }
  145.     public function onTemplateAdminProductClassEdit(TemplateEvent $event)
  146.     {
  147.         $parameters $event->getParameters();
  148.         $CustomerRanks $this->customerRankRepository->getList();
  149.         $parameters['CustomerRanks'] = $CustomerRanks;
  150.         $event->setParameters($parameters);
  151.         $twig '@CustomerRank42/admin/Product/customer_price_class.twig';
  152.         $event->addSnippet($twig);
  153.         $js '@CustomerRank42/admin/Product/customer_price_class.js';
  154.         $event->addAsset($js);
  155.     }
  156. }