app/Plugin/PiaHistory/EventSubscriber/ProductClassEditEventSubscriber.php line 102

Open in your IDE?
  1. <?php
  2. namespace Plugin\PiaHistory\EventSubscriber;
  3. use Eccube\Entity\Product;
  4. use Eccube\Repository\ProductRepository;
  5. use Eccube\Repository\MemberRepository;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Plugin\PiaHistory\Entity\PiaHistory;
  8. use Plugin\PiaHistory\Entity\PiaHisProductClass;
  9. use Plugin\PiaHistory\Entity\PiaHisProductStock;
  10. use Plugin\PiaHistory\Entity\PiaHisRankPrice;
  11. use Plugin\PiaHistory\Repository\PiaHistoryRepository;
  12. use Plugin\PiaHistory\Repository\PiaHisProductClassRepository;
  13. use Plugin\PiaHistory\Repository\PiaHisProductStockRepository;
  14. use Plugin\PiaHistory\Repository\PiaHisRankPriceRepository;
  15. use Eccube\Event\EccubeEvents;
  16. use Eccube\Event\EventArgs;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  19. use Symfony\Component\HttpKernel\KernelEvents;
  20. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  21. class ProductClassEditEventSubscriber implements EventSubscriberInterface
  22. {
  23.     /**
  24.      * @var PiaHistoryRepository
  25.      */
  26.     private $piaHistoryRepository;
  27.     /**
  28.      * @var PiaHisProductClassRepository
  29.      */
  30.     private $piaHisProductClassRepository;
  31.     /**
  32.      * @var PiaHisProductStockRepository
  33.      */
  34.     private $piaHisProductStockRepository;
  35.     /**
  36.      * @var PiaHisRankPriceRepository
  37.      */
  38.     private $piaHisRankPriceRepository;
  39.     /**
  40.      * @var ProductRepository
  41.      */
  42.     private $productRepository;
  43.     /**
  44.      * @var MemberRepository
  45.      */
  46.     private $memberRepository;
  47.     /**
  48.      * @var TokenStorageInterface
  49.      */
  50.     private $tokenStorage;
  51.     /**
  52.      * @var EntityManagerInterface
  53.      */
  54.     private $entityManager;
  55.     /**
  56.      * ProductClassEditEventSubscriber constructor.
  57.      */
  58.     public function __construct(
  59.         PiaHistoryRepository $piaHistoryRepository,
  60.         PiaHisProductClassRepository $piaHisProductClassRepository,
  61.         PiaHisProductStockRepository $piaHisProductStockRepository,
  62.         PiaHisRankPriceRepository $piaHisRankPriceRepository,
  63.         ProductRepository $productRepository,
  64.         MemberRepository $memberRepository,
  65.         TokenStorageInterface $tokenStorage,
  66.         EntityManagerInterface $entityManager
  67.     ) {
  68.         $this->piaHistoryRepository $piaHistoryRepository;
  69.         $this->piaHisProductClassRepository $piaHisProductClassRepository;
  70.         $this->piaHisProductStockRepository $piaHisProductStockRepository;
  71.         $this->piaHisRankPriceRepository $piaHisRankPriceRepository;
  72.         $this->productRepository $productRepository;
  73.         $this->memberRepository $memberRepository;
  74.         $this->tokenStorage $tokenStorage;
  75.         $this->entityManager $entityManager;
  76.     }
  77.     /**
  78.      * @return array
  79.      */
  80.     public static function getSubscribedEvents()
  81.     {
  82.         return [
  83.             KernelEvents::RESPONSE => ['onKernelResponse'0],
  84.         ];
  85.     }
  86.     /**
  87.      * @param ResponseEvent $event
  88.      */
  89.     public function onKernelResponse(ResponseEvent $event)
  90.     {
  91.         $request $event->getRequest();
  92.         $response $event->getResponse();
  93.         // 商品規格編集画面のPOSTリクエストのみ処理
  94.         if ($request->isMethod('POST') && 
  95.             $request->attributes->get('_route') === 'admin_product_product_class' &&
  96.             $response->getStatusCode() === 302) { // リダイレクトレスポンス
  97.             $productId $request->get('id');
  98.             if ($productId) {
  99.                 $this->saveProductClassHistory($productId);
  100.             }
  101.         }
  102.     }
  103.     /**
  104.      * 商品クラス履歴を保存
  105.      *
  106.      * @param int $productId
  107.      */
  108.     private function saveProductClassHistory($productId)
  109.     {
  110.         try {
  111.             // EntityManagerの状態をチェック
  112.             if (!$this->entityManager->isOpen()) {
  113.                 error_log('PiaHistory: EntityManager is closed, skipping product class history save');
  114.                 return;
  115.             }
  116.             $product $this->productRepository->find($productId);
  117.             if (!$product) {
  118.                 return;
  119.             }
  120.             // トランザクション開始
  121.             $this->entityManager->beginTransaction();
  122.             // 管理者情報を取得
  123.             $adminId 1;
  124.             $adminName '管理者';
  125.             if ($this->tokenStorage->getToken() && $this->tokenStorage->getToken()->getUser()) {
  126.                 $admin $this->tokenStorage->getToken()->getUser();
  127.                 $adminId $admin->getId();
  128.                 $adminName $admin->getUsername();
  129.                 
  130.                 // 管理者の詳細情報を取得
  131.                 $member $this->memberRepository->find($adminId);
  132.                 if ($member) {
  133.                     $adminName $member->getName();
  134.                 }
  135.             }
  136.             // 履歴メイン情報を作成
  137.             $history = new PiaHistory();
  138.             $history->setProductId($productId);
  139.             $history->setUpdateId($adminId);
  140.             $history->setUpdateName($adminName);
  141.             $history->setUpdateNum(1); // 更新回数(仮)
  142.             $history->setUpdateNote('商品規格情報を更新しました');
  143.             $history->setCreateDate(new \DateTime());
  144.             $history->setUpdateDate(new \DateTime());
  145.             $history->setDelFlg(0);
  146.             $this->piaHistoryRepository->save($history);
  147.             $historyId $history->getId();
  148.             // 商品規格履歴を保存
  149.             $this->saveProductClassBasicHistory($historyId$product);
  150.             // 商品在庫履歴を保存
  151.             $this->saveProductStockHistory($historyId$product);
  152.             // 商品ランク価格履歴を保存
  153.             $this->saveRankPriceHistory($historyId$product);
  154.             
  155.             // 最後にまとめてflush
  156.             $this->piaHistoryRepository->flush();
  157.             
  158.             // トランザクションコミット
  159.             $this->entityManager->commit();
  160.         } catch (\Exception $e) {
  161.             // トランザクションロールバック
  162.             if ($this->entityManager->getConnection()->isTransactionActive()) {
  163.                 $this->entityManager->rollback();
  164.             }
  165.             
  166.             // エラーログを出力
  167.             error_log('PiaHistory: 商品クラス履歴保存エラー - ' $e->getMessage());
  168.         }
  169.     }
  170.     /**
  171.      * 商品規格基本履歴を保存
  172.      *
  173.      * @param int $historyId
  174.      * @param Product $product
  175.      */
  176.     private function saveProductClassBasicHistory($historyIdProduct $product)
  177.     {
  178.         foreach ($product->getProductClasses() as $productClass) {
  179.             $classHistory = new PiaHisProductClass();
  180.             $classHistory->setHisId($historyId);
  181.             $classHistory->setProductId($product->getId());
  182.             $classHistory->setProductTypeId(null); // EC-CUBE4では商品規格タイプが存在しない
  183.             $classHistory->setClassCategoryId1($productClass->getClassCategory1() ? $productClass->getClassCategory1()->getId() : null);
  184.             $classHistory->setClassCategoryId2($productClass->getClassCategory2() ? $productClass->getClassCategory2()->getId() : null);
  185.             $classHistory->setDeliveryDateId($productClass->getDeliveryDuration() ? $productClass->getDeliveryDuration()->getId() : null);
  186.             $classHistory->setProductCode($productClass->getCode());
  187.             $classHistory->setStock($productClass->getStock());
  188.             $classHistory->setStockUnlimited($productClass->isStockUnlimited());
  189.             $classHistory->setSaleLimit($productClass->getSaleLimit());
  190.             $classHistory->setPrice01($productClass->getPrice01());
  191.             $classHistory->setPrice02($productClass->getPrice02());
  192.             $classHistory->setDeliveryFee($productClass->getDeliveryFee());
  193.             $this->entityManager->persist($classHistory);
  194.         }
  195.     }
  196.     /**
  197.      * 商品在庫履歴を保存
  198.      *
  199.      * @param int $historyId
  200.      * @param Product $product
  201.      */
  202.     private function saveProductStockHistory($historyIdProduct $product)
  203.     {
  204.         foreach ($product->getProductClasses() as $productClass) {
  205.             $stockHistory = new PiaHisProductStock();
  206.             $stockHistory->setHisId($historyId);
  207.             $stockHistory->setProductClassId($productClass->getId());
  208.             $stockHistory->setStock($productClass->getStock());
  209.             $this->entityManager->persist($stockHistory);
  210.         }
  211.     }
  212.     /**
  213.      * 商品ランク価格履歴を保存
  214.      *
  215.      * @param int $historyId
  216.      * @param Product $product
  217.      */
  218.     private function saveRankPriceHistory($historyIdProduct $product)
  219.     {
  220.         // 商品ランク価格の履歴保存処理
  221.         // 他のプラグインとの連携が必要な場合はここで実装
  222.         foreach ($product->getProductClasses() as $productClass) {
  223.             // ランク価格の取得と保存処理
  224.             // 例: CustomerRankプラグインとの連携
  225.         }
  226.     }
  227. }