src/Eccube/Controller/Mypage/MypageController.php line 176

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  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 Eccube\Controller\Mypage;
  13. use Eccube\Controller\AbstractController;
  14. use Eccube\Entity\BaseInfo;
  15. use Eccube\Entity\Customer;
  16. use Eccube\Entity\Order;
  17. use Eccube\Entity\Product;
  18. use Eccube\Entity\FavoriteKey;        //@##ADD 2021-11-21
  19. use Eccube\Entity\FavoriteProduct;    //@##ADD 2021-11-21
  20. use Eccube\Event\EccubeEvents;
  21. use Eccube\Event\EventArgs;
  22. use Eccube\Exception\CartException;
  23. use Eccube\Form\Type\Front\CustomerLoginType;
  24. use Eccube\Repository\BaseInfoRepository;
  25. use Eccube\Repository\CustomerFavoriteProductRepository;
  26. use Eccube\Repository\FavoriteKeyRepository;        //@##ADD 2021-11-21
  27. use Eccube\Repository\FavoriteProductRepository;    //@##ADD 2021-11-21
  28. use Eccube\Repository\OrderRepository;
  29. use Eccube\Repository\ProductRepository;
  30. use Eccube\Service\CartService;
  31. use Eccube\Service\PurchaseFlow\PurchaseContext;
  32. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  33. use Eccube\Service\CalculatePriceService;            //@##ADD 2022-02-24
  34. use Knp\Component\Pager\PaginatorInterface;
  35. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  36. use Symfony\Component\HttpFoundation\Request;
  37. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  38. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  39. use Symfony\Component\Routing\Annotation\Route;
  40. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  41. class MypageController extends AbstractController
  42. {
  43.     /**
  44.      * @var ProductRepository
  45.      */
  46.     protected $productRepository;
  47.     /**
  48.      * @var CustomerFavoriteProductRepository
  49.      */
  50.     protected $customerFavoriteProductRepository;
  51. //@##ADD START 2021-11-21
  52.     /**
  53.      * @var FavoriteKeyRepository
  54.      */
  55.     protected $favoriteKeyRepository;
  56.     /**
  57.      * @var FavoriteProductRepository
  58.      */
  59.     protected $favoriteProductRepository;
  60.     /**
  61.      * @var CalculatePriceService
  62.      */
  63.     protected $calculatePriceService;
  64. //@##ADD END
  65.     /**
  66.      * @var BaseInfo
  67.      */
  68.     protected $BaseInfo;
  69.     /**
  70.      * @var CartService
  71.      */
  72.     protected $cartService;
  73.     /**
  74.      * @var OrderRepository
  75.      */
  76.     protected $orderRepository;
  77.     /**
  78.      * @var PurchaseFlow
  79.      */
  80.     protected $purchaseFlow;
  81.     /**
  82.      * MypageController constructor.
  83.      *
  84.      * @param OrderRepository $orderRepository
  85.      * @param CustomerFavoriteProductRepository $customerFavoriteProductRepository
  86.      * @param FavoriteKeyRepository $favoriteKeyRepository
  87.      * @param FavoriteProductRepository $favoriteProductRepository
  88.      * @param CalculatePriceService $calculatePriceService
  89.      * @param CartService $cartService
  90.      * @param BaseInfoRepository $baseInfoRepository
  91.      * @param PurchaseFlow $purchaseFlow
  92.      */
  93.     public function __construct(
  94.         OrderRepository $orderRepository,
  95.         CustomerFavoriteProductRepository $customerFavoriteProductRepository,
  96.         FavoriteKeyRepository $favoriteKeyRepository,            //@##ADD 2021-11-21
  97.         FavoriteProductRepository $favoriteProductRepository,    //@##ADD 2021-11-21
  98.         CalculatePriceService $calculatePriceService,            //@##ADD 2022-02-24
  99.         CartService $cartService,
  100.         BaseInfoRepository $baseInfoRepository,
  101.         PurchaseFlow $purchaseFlow
  102.     ) {
  103.         $this->orderRepository $orderRepository;
  104.         $this->customerFavoriteProductRepository $customerFavoriteProductRepository;
  105.         $this->favoriteKeyRepository $favoriteKeyRepository;            //@##ADD 2021-11-21
  106.         $this->favoriteProductRepository $favoriteProductRepository;    //@##ADD 2021-11-21
  107.         $this->calculatePriceService $calculatePriceService;            //@##ADD 2022-02-24
  108.         $this->BaseInfo $baseInfoRepository->get();
  109.         $this->cartService $cartService;
  110.         $this->purchaseFlow $purchaseFlow;
  111.     }
  112.     /**
  113.      * ログイン画面.
  114.      *
  115.      * @Route("/mypage/login", name="mypage_login", methods={"GET", "POST"})
  116.      * @Template("Mypage/login.twig")
  117.      */
  118.     public function login(Request $requestAuthenticationUtils $utils)
  119.     {
  120.         if ($this->isGranted('IS_AUTHENTICATED_FULLY')) {
  121.             log_info('認証済のためログイン処理をスキップ');
  122.             return $this->redirectToRoute('mypage');
  123.         }
  124.         /* @var $form \Symfony\Component\Form\FormInterface */
  125.         $builder $this->formFactory
  126.             ->createNamedBuilder(''CustomerLoginType::class);
  127.         $builder->get('login_memory')->setData((bool) $request->getSession()->get('_security.login_memory'));
  128.         if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  129.             $Customer $this->getUser();
  130.             if ($Customer instanceof Customer) {
  131.                 $builder->get('login_email')
  132.                     ->setData($Customer->getEmail());
  133.             }
  134.         }
  135.         $event = new EventArgs(
  136.             [
  137.                 'builder' => $builder,
  138.             ],
  139.             $request
  140.         );
  141.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_LOGIN_INITIALIZE);
  142.         $form $builder->getForm();
  143.         return [
  144.             'error' => $utils->getLastAuthenticationError(),
  145.             'form' => $form->createView(),
  146.         ];
  147.     }
  148.     /**
  149.      * マイページ.
  150.      *
  151.      * @Route("/mypage/", name="mypage", methods={"GET"})
  152.      * @Template("Mypage/index.twig")
  153.      */
  154.     public function index(Request $requestPaginatorInterface $paginator)
  155.     {
  156.         $Customer $this->getUser();
  157.         // 購入処理中/決済処理中ステータスの受注を非表示にする.
  158.         $this->entityManager
  159.             ->getFilters()
  160.             ->enable('incomplete_order_status_hidden');
  161.         // paginator
  162.         $qb $this->orderRepository->getQueryBuilderByCustomer($Customer);
  163.         $event = new EventArgs(
  164.             [
  165.                 'qb' => $qb,
  166.                 'Customer' => $Customer,
  167.             ],
  168.             $request
  169.         );
  170.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_INDEX_SEARCH);
  171.         $pagination $paginator->paginate(
  172.             $qb,
  173.             $request->get('pageno'1),
  174.             $this->eccubeConfig['eccube_search_pmax']
  175.         );
  176.         return [
  177.             'pagination' => $pagination,
  178.         ];
  179.     }
  180.     /**
  181.      * 購入履歴詳細を表示する.
  182.      *
  183.      * @Route("/mypage/history/{order_no}", name="mypage_history", methods={"GET"})
  184.      * @Template("Mypage/history.twig")
  185.      */
  186.     public function history(Request $request$order_no)
  187.     {
  188.         $this->entityManager->getFilters()
  189.             ->enable('incomplete_order_status_hidden');
  190.         $Order $this->orderRepository->findOneBy(
  191.             [
  192.                 'order_no' => $order_no,
  193.                 'Customer' => $this->getUser(),
  194.             ]
  195.         );
  196.         $event = new EventArgs(
  197.             [
  198.                 'Order' => $Order,
  199.             ],
  200.             $request
  201.         );
  202.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_HISTORY_INITIALIZE);
  203.         /** @var Order $Order */
  204.         $Order $event->getArgument('Order');
  205.         if (!$Order) {
  206.             throw new NotFoundHttpException();
  207.         }
  208.         $stockOrder true;
  209.         foreach ($Order->getOrderItems() as $orderItem) {
  210.             if ($orderItem->isProduct() && $orderItem->getQuantity() < 0) {
  211.                 $stockOrder false;
  212.                 break;
  213.             }
  214.         }
  215.         return [
  216.             'Order' => $Order,
  217.             'stockOrder' => $stockOrder,
  218.         ];
  219.     }
  220.     /**
  221.      * 再購入を行う.
  222.      *
  223.      * @Route("/mypage/order/{order_no}", name="mypage_order", methods={"PUT"})
  224.      */
  225.     public function order(Request $request$order_no)
  226.     {
  227.         $this->isTokenValid();
  228.         log_info('再注文開始', [$order_no]);
  229.         $Customer $this->getUser();
  230.         /* @var $Order \Eccube\Entity\Order */
  231.         $Order $this->orderRepository->findOneBy(
  232.             [
  233.                 'order_no' => $order_no,
  234.                 'Customer' => $Customer,
  235.             ]
  236.         );
  237.         $event = new EventArgs(
  238.             [
  239.                 'Order' => $Order,
  240.                 'Customer' => $Customer,
  241.             ],
  242.             $request
  243.         );
  244.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_INITIALIZE);
  245.         if (!$Order) {
  246.             log_info('対象の注文が見つかりません', [$order_no]);
  247.             throw new NotFoundHttpException();
  248.         }
  249.         // エラーメッセージの配列
  250.         $errorMessages = [];
  251.         foreach ($Order->getOrderItems() as $OrderItem) {
  252.             try {
  253.                 if ($OrderItem->getProduct() && $OrderItem->getProductClass()) {
  254.                     $this->cartService->addProduct($OrderItem->getProductClass(), $OrderItem->getQuantity());
  255.                     // 明細の正規化
  256.                     $Carts $this->cartService->getCarts();
  257.                     foreach ($Carts as $Cart) {
  258.                         $result $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart$this->getUser()));
  259.                         // 復旧不可のエラーが発生した場合は追加した明細を削除.
  260.                         if ($result->hasError()) {
  261.                             $this->cartService->removeProduct($OrderItem->getProductClass());
  262.                             foreach ($result->getErrors() as $error) {
  263.                                 $errorMessages[] = $error->getMessage();
  264.                             }
  265.                         }
  266.                         foreach ($result->getWarning() as $warning) {
  267.                             $errorMessages[] = $warning->getMessage();
  268.                         }
  269.                     }
  270.                     $this->cartService->save();
  271.                 }
  272.             } catch (CartException $e) {
  273.                 log_info($e->getMessage(), [$order_no]);
  274.                 $this->addRequestError($e->getMessage());
  275.             }
  276.         }
  277.         foreach ($errorMessages as $errorMessage) {
  278.             $this->addRequestError($errorMessage);
  279.         }
  280.         $event = new EventArgs(
  281.             [
  282.                 'Order' => $Order,
  283.                 'Customer' => $Customer,
  284.             ],
  285.             $request
  286.         );
  287.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_COMPLETE);
  288.         if ($event->getResponse() !== null) {
  289.             return $event->getResponse();
  290.         }
  291.         log_info('再注文完了', [$order_no]);
  292.         return $this->redirect($this->generateUrl('cart'));
  293.     }
  294.     /**
  295.      * お気に入り商品を表示する.
  296.      *
  297.      * @Route("/mypage/favorite", name="mypage_favorite", methods={"GET"})
  298.      * @Template("Mypage/favorite.twig")
  299.      */
  300.     public function favorite(Request $requestPaginatorInterface $paginator)
  301.     {
  302.         if (!$this->BaseInfo->isOptionFavoriteProduct()) {
  303.             throw new NotFoundHttpException();
  304.         }
  305.         $Customer $this->getUser();
  306. //@##ADD START 2021-11-21 upseek
  307.         $cookieKey $request->cookies->get('eccube_favorite');
  308.         if($cookieKey) {
  309.             log_warning('add_favorite Customer=[' $Customer->getId() . '] has cookie=[' $cookieKey ']');
  310.             $FavoriteKey $this->favoriteKeyRepository->findWithCookieKey($cookieKey);
  311.             if($FavoriteKey) {
  312.                 $FavoriteProducts $this->favoriteProductRepository->findBy(['FavoriteKey' => $FavoriteKey]);
  313.                 foreach($FavoriteProducts as $FavoriteProduct) {
  314.                     $shelvingOption $FavoriteProduct->getShelvingOption();
  315.                     if($shelvingOption != null) {
  316.                         $shelvingOption $shelvingOption->convertForCustomerFavoriteProduct();
  317.                     }
  318.                     $this->customerFavoriteProductRepository->addFavorite($Customer$FavoriteProduct->getProduct(), $shelvingOption);
  319.                     $this->favoriteProductRepository->delete($FavoriteProduct);
  320.                     log_warning('add_favorite Customer=[' $Customer->getId() . '] Product=[' $FavoriteProduct->getProduct()->getId() . ']');
  321.                 }
  322.                 $this->favoriteKeyRepository->delete($FavoriteKey);
  323.             }
  324.             setcookie('eccube_favorite'$cookieKey, (time() - 60 60), $this->eccubeConfig['env(ECCUBE_COOKIE_PATH)']);
  325.             log_warning('add_favorite Customer=[' $Customer->getId() . '] done.');
  326.             return $this->redirectToRoute('mypage_favorite');
  327.         }
  328. //@##ADD END
  329.         // paginator
  330.         $qb $this->customerFavoriteProductRepository->getQueryBuilderByCustomer($Customer);
  331.         $event = new EventArgs(
  332.             [
  333.                 'qb' => $qb,
  334.                 'Customer' => $Customer,
  335.             ],
  336.             $request
  337.         );
  338.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_FAVORITE_SEARCH);
  339.         $pagination $paginator->paginate(
  340.             $qb,
  341.             $request->get('pageno'1),
  342.             $this->eccubeConfig['eccube_search_pmax'],
  343.             ['wrap-queries' => true]
  344.         );
  345. //@##ADD START 2022-02-24
  346.         //シミュレーション料金を計算
  347.         foreach($pagination as $CustomerFavoriteProduct) {
  348.             $ShelvingOption $CustomerFavoriteProduct->getShelvingOption();
  349.             if($ShelvingOption != null) {
  350.                 $shelvingPrice $this->calculatePriceService->calculate($ShelvingOptiontrue);
  351.                 $CustomerFavoriteProduct->setShelvingPrice($shelvingPrice);
  352.             }
  353.         }
  354. //@##ADD END
  355.         return [
  356.             'pagination' => $pagination,
  357.         ];
  358.     }
  359.     /**
  360.      * お気に入り商品を削除する.
  361.      *
  362.      * @Route("/mypage/favorite/{id}/delete", name="mypage_favorite_delete", methods={"DELETE"}, requirements={"id" = "\d+"})
  363.      */
  364.     public function delete(Request $requestProduct $Product)
  365.     {
  366.         $this->isTokenValid();
  367.         $Customer $this->getUser();
  368. //@##ADD START 2022-07-13
  369.         // CustomerFavoriteProduct ID
  370.         $cfp $request->query->get('cfp');
  371. //@##ADD END
  372.         log_info('お気に入り商品削除開始', [$Customer->getId(), $Product->getId(), $cfp]);
  373. //@##CHG START 2022-07-13
  374.         //$CustomerFavoriteProduct = $this->customerFavoriteProductRepository->findOneBy(['Customer' => $Customer, 'Product' => $Product]);
  375.         if($cfp) {
  376.             $CustomerFavoriteProduct $this->customerFavoriteProductRepository->findOneBy(['Customer' => $Customer'id' => $cfp]);
  377.         } else {
  378.             $CustomerFavoriteProduct $this->customerFavoriteProductRepository->findOneBy(['Customer' => $Customer'Product' => $Product]);
  379.         }
  380. //@##CHG END
  381.         if ($CustomerFavoriteProduct) {
  382.             $this->customerFavoriteProductRepository->delete($CustomerFavoriteProduct);
  383.         } else {
  384.             throw new BadRequestHttpException();
  385.         }
  386.         $event = new EventArgs(
  387.             [
  388.                 'Customer' => $Customer,
  389.                 'CustomerFavoriteProduct' => $CustomerFavoriteProduct,
  390.             ], $request
  391.         );
  392.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_DELETE_COMPLETE);
  393.         log_info('お気に入り商品削除完了', [$Customer->getId(), $CustomerFavoriteProduct->getId()]);
  394.         return $this->redirect($this->generateUrl('mypage_favorite'));
  395.     }
  396. }