app/Customize/Controller/MypageControllerExtends.php line 444

Open in your IDE?
  1. <?php
  2. namespace Customize\Controller;
  3. use Eccube\Controller\Mypage\MypageController;
  4. use Plugin\ECCUBE4LineLoginIntegration42\Repository\LineLoginIntegrationRepository;
  5. use Plugin\Coupon42\Repository\CouponRepository;
  6. use Customize\Repository\CouponRepository as CustomizeCouponRepository;
  7. use Customize\Repository\CouponOrderRepository as CustomizeCouponOrderRepository;
  8. use Plugin\Coupon42\Service\CouponService;
  9. use Customize\Service\CouponService as CustomizeCouponService;
  10. use Eccube\Entity\BaseInfo;
  11. use Eccube\Entity\Customer;
  12. use Eccube\Entity\Order;
  13. use Eccube\Entity\Product;
  14. use Eccube\Event\EccubeEvents;
  15. use Eccube\Event\EventArgs;
  16. use Eccube\Exception\CartException;
  17. use Eccube\Form\Type\Front\CustomerLoginType;
  18. use Eccube\Repository\BaseInfoRepository;
  19. use Eccube\Repository\CustomerFavoriteProductRepository;
  20. use Eccube\Repository\OrderRepository;
  21. use Customize\Repository\ProductRepository;
  22. use Eccube\Service\CartService;
  23. use Eccube\Service\PurchaseFlow\PurchaseContext;
  24. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  25. use Knp\Component\Pager\PaginatorInterface;
  26. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  27. use Symfony\Component\HttpFoundation\Request;
  28. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  29. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  30. use Symfony\Component\Routing\Annotation\Route;
  31. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  32. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  33. class MypageControllerExtends extends MypageController
  34. {
  35.     /**
  36.      * @var ProductRepository
  37.      */
  38.     protected $productRepository;
  39.     /**
  40.      * @var CustomerFavoriteProductRepository
  41.      */
  42.     protected $customerFavoriteProductRepository;
  43.     /**
  44.      * @var BaseInfo
  45.      */
  46.     protected $BaseInfo;
  47.     /**
  48.      * @var CartService
  49.      */
  50.     protected $cartService;
  51.     /**
  52.      * @var OrderRepository
  53.      */
  54.     protected $orderRepository;
  55.     /**
  56.      * @var PurchaseFlow
  57.      */
  58.     protected $purchaseFlow;
  59.     /**
  60.      * @var CouponService
  61.      */
  62.     private $couponService;
  63.     /**
  64.      * @var CustomizeCouponService
  65.      */
  66.     private $customizeCouponService;
  67.     /**
  68.      * @var CouponRepository
  69.      */
  70.     private $couponRepository;
  71.     /**
  72.      * @var CustomizeCouponRepository
  73.      */
  74.     private $customizeCouponRepository;
  75.     
  76.     /**
  77.      * @var CustomizeCouponOrderRepository
  78.      */ 
  79.     private $customizeCouponOrderRepository;
  80.     /**
  81.      * @var LineIntegrationRepository
  82.      */
  83.     private $lineIntegrationRepository;
  84.     /**
  85.      * MypageController constructor.
  86.      *
  87.      * @param OrderRepository $orderRepository
  88.      * @param ProductRepository $productRepository
  89.      * @param CustomerFavoriteProductRepository $customerFavoriteProductRepository
  90.      * @param CartService $cartService
  91.      * @param BaseInfoRepository $baseInfoRepository
  92.      * @param PurchaseFlow $purchaseFlow
  93.      */
  94.     public function __construct(
  95.         OrderRepository $orderRepository,
  96.         ProductRepository $productRepository,
  97.         CustomerFavoriteProductRepository $customerFavoriteProductRepository,
  98.         CartService $cartService,
  99.         BaseInfoRepository $baseInfoRepository,
  100.         PurchaseFlow $purchaseFlow,
  101.         CouponService $couponService,
  102.         CustomizeCouponService $customizeCouponService,
  103.         CouponRepository $couponRepository,
  104.         CustomizeCouponOrderRepository $customizeCouponOrderRepository,
  105.         CustomizeCouponRepository $customizeCouponRepository,
  106.         LineLoginIntegrationRepository $lineIntegrationRepository,
  107.     ) {
  108.         $this->orderRepository $orderRepository;
  109.         $this->productRepository $productRepository;
  110.         $this->customerFavoriteProductRepository $customerFavoriteProductRepository;
  111.         $this->BaseInfo $baseInfoRepository->get();
  112.         $this->cartService $cartService;
  113.         $this->purchaseFlow $purchaseFlow;
  114.         $this->couponService $couponService;
  115.         $this->customizeCouponService $customizeCouponService;
  116.         $this->couponRepository $couponRepository;
  117.         $this->customizeCouponOrderRepository $customizeCouponOrderRepository;
  118.         $this->customizeCouponRepository $customizeCouponRepository;
  119.         $this->lineIntegrationRepository $lineIntegrationRepository;
  120.     }
  121.     /**
  122.      * ログイン画面.
  123.      *
  124.      * @Route("/mypage/login", name="mypage_login", methods={"GET", "POST"})
  125.      * @Template("Mypage/login.twig")
  126.      */
  127.     public function login(Request $requestAuthenticationUtils $utils)
  128.     {
  129.         // LINEから来たかどうかのパラメータを見てLINEボタンの表示可否を制御
  130.         $fromLine $request->query->get('fromline');
  131.         if($fromLine != null && $fromLine == 1){
  132.             $fromLine true;
  133.             $this->session->set('fromLine'$fromLine);
  134.         } else {
  135.             $fromLine false;
  136.         }
  137.         // ログイン済みの場合はマイページに。遷移元がLINEの場合のみ会員情報更新画面へ遷移。
  138.         if ($this->isGranted('IS_AUTHENTICATED_FULLY')) {
  139.             log_info('認証済のためログイン処理をスキップ');
  140.             if($fromLine){
  141.                 return $this->redirectToRoute('mypage_change');
  142.             } else {
  143.                 return $this->redirectToRoute('mypage');
  144.             }
  145.         }
  146.         /* @var $form \Symfony\Component\Form\FormInterface */
  147.         $builder $this->formFactory
  148.             ->createNamedBuilder(''CustomerLoginType::class);
  149.         $builder->get('login_memory')->setData((bool) $request->getSession()->get('_security.login_memory'));
  150.         if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  151.             $Customer $this->getUser();
  152.             if ($Customer instanceof Customer) {
  153.                 $builder->get('login_email')
  154.                     ->setData($Customer->getEmail());
  155.             }
  156.         }
  157.         $event = new EventArgs(
  158.             [
  159.                 'builder' => $builder,
  160.             ],
  161.             $request
  162.         );
  163.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_LOGIN_INITIALIZE);
  164.         $form $builder->getForm();
  165.         $sendto $request->query->get('sendto');
  166.         $param  $request->query->get('param');
  167.         if ($sendto !== null) {
  168.             $this->session->set('sendto'$sendto);
  169.         }
  170.         if ($param !== null) {
  171.             $this->session->set('param'$param);
  172.         }
  173.         if ($sendto === 'detail' && $param) {
  174.             $this->setLoginTargetPath($this->generateUrl('product_detail', ['id' => $param], UrlGeneratorInterface::ABSOLUTE_URL));
  175.         } elseif ($sendto === 'list' && $param) {
  176.             $this->setLoginTargetPath($this->generateUrl('product_list', ['name' => $param], UrlGeneratorInterface::ABSOLUTE_URL));
  177.         } elseif ($sendto === 'homepage') {
  178.             $this->setLoginTargetPath($this->generateUrl('homepage', [], UrlGeneratorInterface::ABSOLUTE_URL));
  179.         }
  180.         // ログイン済みの場合でLINEから来た際はマイページに遷移させる
  181.         // $fromLine = $this->session->get('fromLine');
  182.         $user $this->getUser();
  183.         if($this->isGranted('IS_AUTHENTICATED_FULLY') && $fromLine){
  184.             return $this->redirectToRoute('mypage_change');
  185.         }
  186.         return [
  187.             'error' => $utils->getLastAuthenticationError(),
  188.             'form' => $form->createView(),
  189.             'fromLine' => $fromLine,
  190.         ];
  191.     }
  192.     /**
  193.      * マイページ.
  194.      *
  195.      * @Route("/mypage/", name="mypage", methods={"GET"})
  196.      * @Template("Mypage/index.twig")
  197.      */
  198.     public function index(Request $requestPaginatorInterface $paginator)
  199.     {
  200.         $Customer $this->getUser();
  201.         // 購入処理中/決済処理中ステータスの受注を非表示にする.
  202.         $this->entityManager
  203.             ->getFilters()
  204.             ->enable('incomplete_order_status_hidden');
  205.         // paginator
  206.         $qb $this->orderRepository->getQueryBuilderByCustomer($Customer);
  207.         $event = new EventArgs(
  208.             [
  209.                 'qb' => $qb,
  210.                 'Customer' => $Customer,
  211.             ],
  212.             $request
  213.         );
  214.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_INDEX_SEARCH);
  215.         $pagination $paginator->paginate(
  216.             $qb,
  217.             $request->get('pageno'1),
  218.             $this->eccubeConfig['eccube_search_pmax']
  219.         );
  220.         // 会員登録済みでLINE連携済み、かつクーポンコード未活用であればフラグ連携する
  221.         $isShowCouponCode false;
  222.         $customer_id $Customer->getId();
  223.         // LINE連携レコードを取得
  224.         $lineIntegration $this->lineIntegrationRepository->
  225.             findOneBy(['customer_id' => $customer_id]);
  226.         // クーポンコードは固定でベタ打ち
  227.         $formCouponCd 'linelogin3000';
  228.         
  229.         // クーポンを未使用かどうかを取得する
  230.         $Coupon $this->couponRepository->findActiveCoupon($formCouponCd);
  231.         $couponUsedOrNot $this->couponService->checkCouponUsedOrNot($formCouponCd$Customer);
  232.         // Line連携しており、クーポン未使用の場合はフラグを返す
  233.         if ($lineIntegration && !$couponUsedOrNot) {
  234.             $isShowCouponCode true;
  235.         }
  236.         return [
  237.             'pagination' => $pagination,
  238.             'isShowCouponCode' => $isShowCouponCode,
  239.         ];
  240.     }
  241.     /**
  242.      * 購入履歴詳細を表示する.
  243.      *
  244.      * @Route("/mypage/history/{order_no}", name="mypage_history", methods={"GET"})
  245.      * @Template("Mypage/history.twig")
  246.      */
  247.     public function history(Request $request$order_no)
  248.     {
  249.         $this->entityManager->getFilters()
  250.             ->enable('incomplete_order_status_hidden');
  251.         $Order $this->orderRepository->findOneBy(
  252.             [
  253.                 'order_no' => $order_no,
  254.                 'Customer' => $this->getUser(),
  255.             ]
  256.         );
  257.         $event = new EventArgs(
  258.             [
  259.                 'Order' => $Order,
  260.             ],
  261.             $request
  262.         );
  263.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_HISTORY_INITIALIZE);
  264.         /** @var Order $Order */
  265.         $Order $event->getArgument('Order');
  266.         if (!$Order) {
  267.             throw new NotFoundHttpException();
  268.         }
  269.         $stockOrder true;
  270.         foreach ($Order->getOrderItems() as $orderItem) {
  271.             if ($orderItem->isProduct() && $orderItem->getQuantity() < 0) {
  272.                 $stockOrder false;
  273.                 break;
  274.             }
  275.         }
  276.         return [
  277.             'Order' => $Order,
  278.             'stockOrder' => $stockOrder,
  279.         ];
  280.     }
  281.     /**
  282.      * 再購入を行う.
  283.      *
  284.      * @Route("/mypage/order/{order_no}", name="mypage_order", methods={"PUT"})
  285.      */
  286.     public function order(Request $request$order_no)
  287.     {
  288.         $this->isTokenValid();
  289.         log_info('再注文開始', [$order_no]);
  290.         $Customer $this->getUser();
  291.         /* @var $Order \Eccube\Entity\Order */
  292.         $Order $this->orderRepository->findOneBy(
  293.             [
  294.                 'order_no' => $order_no,
  295.                 'Customer' => $Customer,
  296.             ]
  297.         );
  298.         $event = new EventArgs(
  299.             [
  300.                 'Order' => $Order,
  301.                 'Customer' => $Customer,
  302.             ],
  303.             $request
  304.         );
  305.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_INITIALIZE);
  306.         if (!$Order) {
  307.             log_info('対象の注文が見つかりません', [$order_no]);
  308.             throw new NotFoundHttpException();
  309.         }
  310.         // エラーメッセージの配列
  311.         $errorMessages = [];
  312.         foreach ($Order->getOrderItems() as $OrderItem) {
  313.             try {
  314.                 if ($OrderItem->getProduct() && $OrderItem->getProductClass()) {
  315.                     $this->cartService->addProduct($OrderItem->getProductClass(), $OrderItem->getQuantity());
  316.                     // 明細の正規化
  317.                     $Carts $this->cartService->getCarts();
  318.                     foreach ($Carts as $Cart) {
  319.                         $result $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart$this->getUser()));
  320.                         // 復旧不可のエラーが発生した場合は追加した明細を削除.
  321.                         if ($result->hasError()) {
  322.                             $this->cartService->removeProduct($OrderItem->getProductClass());
  323.                             foreach ($result->getErrors() as $error) {
  324.                                 $errorMessages[] = $error->getMessage();
  325.                             }
  326.                         }
  327.                         foreach ($result->getWarning() as $warning) {
  328.                             $errorMessages[] = $warning->getMessage();
  329.                         }
  330.                     }
  331.                     $this->cartService->save();
  332.                 }
  333.             } catch (CartException $e) {
  334.                 log_info($e->getMessage(), [$order_no]);
  335.                 $this->addRequestError($e->getMessage());
  336.             }
  337.         }
  338.         foreach ($errorMessages as $errorMessage) {
  339.             $this->addRequestError($errorMessage);
  340.         }
  341.         $event = new EventArgs(
  342.             [
  343.                 'Order' => $Order,
  344.                 'Customer' => $Customer,
  345.             ],
  346.             $request
  347.         );
  348.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_COMPLETE);
  349.         if ($event->getResponse() !== null) {
  350.             return $event->getResponse();
  351.         }
  352.         log_info('再注文完了', [$order_no]);
  353.         return $this->redirect($this->generateUrl('cart'));
  354.     }
  355.     /**
  356.      * 注文済みのレシピ帖を表示する.
  357.      *
  358.      * @Route("/mypage/recipe", name="mypage_recipe", methods={"GET"})
  359.      * @Template("Mypage/recipe.twig")
  360.      */
  361.     public function recipe(Request $requestPaginatorInterface $paginator)
  362.     {
  363.         $Customer $this->getUser();
  364.         // 購入処理中/決済処理中ステータスの受注を非表示にする.
  365.         $this->entityManager
  366.             ->getFilters()
  367.             ->enable('incomplete_order_status_hidden');
  368.         // paginator
  369.         $qb $this->productRepository->findPurchasedRecipeProductsByCustomer($Customer);
  370.         $pagination $paginator->paginate(
  371.             $qb,
  372.             $request->get('pageno'1),
  373.             $this->eccubeConfig['eccube_search_pmax']
  374.         );
  375.         
  376.         return [
  377.             'pagination' => $pagination,
  378.         ];
  379.     }
  380.     /**
  381.      * お気に入り商品を表示する.
  382.      *
  383.      * @Route("/mypage/favorite", name="mypage_favorite", methods={"GET"})
  384.      * @Template("Mypage/favorite.twig")
  385.      */
  386.     public function favorite(Request $requestPaginatorInterface $paginator)
  387.     {
  388.         if (!$this->BaseInfo->isOptionFavoriteProduct()) {
  389.             throw new NotFoundHttpException();
  390.         }
  391.         $Customer $this->getUser();
  392.         // paginator
  393.         $qb $this->customerFavoriteProductRepository->getQueryBuilderByCustomer($Customer);
  394.         $event = new EventArgs(
  395.             [
  396.                 'qb' => $qb,
  397.                 'Customer' => $Customer,
  398.             ],
  399.             $request
  400.         );
  401.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_FAVORITE_SEARCH);
  402.         $pagination $paginator->paginate(
  403.             $qb,
  404.             $request->get('pageno'1),
  405.             $this->eccubeConfig['eccube_search_pmax'],
  406.             ['wrap-queries' => true]
  407.         );
  408.         
  409.         return [
  410.             'pagination' => $pagination,
  411.         ];
  412.     }
  413.     /**
  414.      * お気に入り商品を削除する.
  415.      *
  416.      * @Route("/mypage/favorite/{id}/delete", name="mypage_favorite_delete", methods={"DELETE"}, requirements={"id" = "\d+"})
  417.      */
  418.     public function delete(Request $requestProduct $Product)
  419.     {
  420.         $this->isTokenValid();
  421.         $Customer $this->getUser();
  422.         log_info('お気に入り商品削除開始', [$Customer->getId(), $Product->getId()]);
  423.         $CustomerFavoriteProduct $this->customerFavoriteProductRepository->findOneBy(['Customer' => $Customer'Product' => $Product]);
  424.         if ($CustomerFavoriteProduct) {
  425.             $this->customerFavoriteProductRepository->delete($CustomerFavoriteProduct);
  426.         } else {
  427.             throw new BadRequestHttpException();
  428.         }
  429.         $event = new EventArgs(
  430.             [
  431.                 'Customer' => $Customer,
  432.                 'CustomerFavoriteProduct' => $CustomerFavoriteProduct,
  433.             ], $request
  434.         );
  435.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_DELETE_COMPLETE);
  436.         log_info('お気に入り商品削除完了', [$Customer->getId(), $CustomerFavoriteProduct->getId()]);
  437.         return $this->redirect($this->generateUrl('mypage_favorite'));
  438.     }
  439.     /**
  440.      * 持っているクーポン一覧を取得する.
  441.      *
  442.      * @Route("/mypage/coupon", name="mypage_coupon", methods={"GET"})
  443.      * @Template("Mypage/coupon.twig")
  444.      */
  445.     public function coupon(Request $requestPaginatorInterface $paginator)
  446.     {
  447.         $Customer $this->getUser();
  448.         $activeCoupons $this->customizeCouponService->getAvailableCoupons($Customer);
  449.         $usedCoupons $this->customizeCouponOrderRepository->getUsedCoupon($Customer);
  450.         
  451.         return [
  452.             'activeCoupons' => $activeCoupons,
  453.             'usedCoupons' => $usedCoupons,
  454.         ];
  455.     }
  456. }