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

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\Event\EccubeEvents;
  19. use Eccube\Event\EventArgs;
  20. use Eccube\Exception\CartException;
  21. use Eccube\Form\Type\Front\CustomerLoginType;
  22. use Eccube\Repository\BaseInfoRepository;
  23. use Eccube\Repository\CustomerFavoriteProductRepository;
  24. use Eccube\Repository\OrderRepository;
  25. use Eccube\Repository\ProductRepository;
  26. use Eccube\Service\CartService;
  27. use Eccube\Service\PurchaseFlow\PurchaseContext;
  28. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  29. use Knp\Component\Pager\PaginatorInterface;
  30. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  31. use Symfony\Component\HttpFoundation\Request;
  32. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  33. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  34. use Symfony\Component\Routing\Annotation\Route;
  35. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  36. use Plugin\EccubePaymentLite42\Repository\RegularOrderRepository;
  37. use Doctrine\ORM\EntityManagerInterface;
  38. use Symfony\Component\HttpFoundation\Response;
  39. use Eccube\Repository\ShippingRepository;
  40. use Plugin\CouponPro42\Repository\CouponRepository;
  41. class MypageController extends AbstractController
  42. {
  43.     /**
  44.      * @var ProductRepository
  45.      */
  46.     protected $productRepository;
  47.     /**
  48.      * @var CustomerFavoriteProductRepository
  49.      */
  50.     protected $customerFavoriteProductRepository;
  51.     /**
  52.      * @var BaseInfo
  53.      */
  54.     protected $BaseInfo;
  55.     /**
  56.      * @var CartService
  57.      */
  58.     protected $cartService;
  59.     /**
  60.      * @var OrderRepository
  61.      */
  62.     protected $orderRepository;
  63.     /**
  64.      * @var PurchaseFlow
  65.      */
  66.     protected $purchaseFlow;
  67.     /**
  68.      * @var RegularOrderRepository
  69.      */
  70.     private $regularOrderRepository;
  71.     /**
  72.      * @var ShippingRepository
  73.      */
  74.     private $shippingRepository;
  75.     /**
  76.      * @var CouponRepository
  77.      */
  78.     protected $couponRepository;
  79.     /**
  80.      * MypageController constructor.
  81.      *
  82.      * @param OrderRepository $orderRepository
  83.      * @param CustomerFavoriteProductRepository $customerFavoriteProductRepository
  84.      * @param CartService $cartService
  85.      * @param BaseInfoRepository $baseInfoRepository
  86.      * @param PurchaseFlow $purchaseFlow
  87.      */
  88.     public function __construct(
  89.         OrderRepository $orderRepository,
  90.         CustomerFavoriteProductRepository $customerFavoriteProductRepository,
  91.         CartService $cartService,
  92.         BaseInfoRepository $baseInfoRepository,
  93.         PurchaseFlow $purchaseFlow,
  94.         RegularOrderRepository $regularOrderRepository,
  95.         ShippingRepository $shippingRepository,
  96.         CouponRepository $couponRepository
  97.     ) {
  98.         $this->orderRepository $orderRepository;
  99.         $this->customerFavoriteProductRepository $customerFavoriteProductRepository;
  100.         $this->BaseInfo $baseInfoRepository->get();
  101.         $this->cartService $cartService;
  102.         $this->purchaseFlow $purchaseFlow;
  103.         $this->regularOrderRepository $regularOrderRepository;
  104.         $this->shippingRepository $shippingRepository;
  105.         $this->couponRepository $couponRepository;
  106.     }
  107.     /**
  108.      * ログイン画面.
  109.      *
  110.      * @Route("/mypage/login", name="mypage_login", methods={"GET", "POST"})
  111.      * @Template("Mypage/login.twig")
  112.      */
  113.     public function login(Request $requestAuthenticationUtils $utils)
  114.     {
  115.         if ($this->isGranted('IS_AUTHENTICATED_FULLY')) {
  116.             log_info('認証済のためログイン処理をスキップ');
  117.             return $this->redirectToRoute('mypage');
  118.         }
  119.         /* @var $form \Symfony\Component\Form\FormInterface */
  120.         $builder $this->formFactory
  121.             ->createNamedBuilder(''CustomerLoginType::class);
  122.         $builder->get('login_memory')->setData((bool) $request->getSession()->get('_security.login_memory'));
  123.         if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  124.             $Customer $this->getUser();
  125.             if ($Customer instanceof Customer) {
  126.                 $builder->get('login_email')
  127.                     ->setData($Customer->getEmail());
  128.             }
  129.         }
  130.         $event = new EventArgs(
  131.             [
  132.                 'builder' => $builder,
  133.             ],
  134.             $request
  135.         );
  136.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_LOGIN_INITIALIZE);
  137.         $form $builder->getForm();
  138.         return [
  139.             'error' => $utils->getLastAuthenticationError(),
  140.             'form' => $form->createView(),
  141.         ];
  142.     }
  143.     /**
  144.      * マイページ.
  145.      *
  146.      * @Route("/mypage/", name="mypage", methods={"GET"})
  147.      * @Template("Mypage/index.twig")
  148.      */
  149.     public function index(Request $requestPaginatorInterface $paginator)
  150.     {
  151.         $Customer $this->getUser();
  152.         // 購入処理中/決済処理中ステータスの受注を非表示にする.
  153.         $this->entityManager
  154.             ->getFilters()
  155.             ->enable('incomplete_order_status_hidden');
  156.         // paginator
  157.         $qb $this->orderRepository->getQueryBuilderByCustomer($Customer);
  158.         $event = new EventArgs(
  159.             [
  160.                 'qb' => $qb,
  161.                 'Customer' => $Customer,
  162.             ],
  163.             $request
  164.         );
  165.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_INDEX_SEARCH);
  166.         $pagination $paginator->paginate(
  167.             $qb,
  168.             $request->get('pageno'1),
  169.             $this->eccubeConfig['eccube_search_pmax']
  170.         );
  171.         $regular_qb $this->regularOrderRepository->getQueryBuilderByCustomer($Customer);
  172.         // 全ての定期注文を取得
  173.         $regularOrders $regular_qb->getQuery()->getResult();
  174.         return [
  175.             'pagination' => $pagination,
  176.             'regularOrders' => $regularOrders,
  177.         ];
  178.     }
  179.     /**
  180.      * 購入履歴詳細を表示する.
  181.      *
  182.      * @Route("/mypage/history/{order_no}", name="mypage_history", methods={"GET"})
  183.      * @Template("Mypage/history.twig")
  184.      */
  185.     public function history(Request $request$order_no)
  186.     {
  187.         $this->entityManager->getFilters()
  188.             ->enable('incomplete_order_status_hidden');
  189.         $Order $this->orderRepository->findOneBy(
  190.             [
  191.                 'order_no' => $order_no,
  192.                 'Customer' => $this->getUser(),
  193.             ]
  194.         );
  195.         $event = new EventArgs(
  196.             [
  197.                 'Order' => $Order,
  198.             ],
  199.             $request
  200.         );
  201.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_HISTORY_INITIALIZE);
  202.         /** @var Order $Order */
  203.         $Order $event->getArgument('Order');
  204.         if (!$Order) {
  205.             throw new NotFoundHttpException();
  206.         }
  207.         $stockOrder true;
  208.         foreach ($Order->getOrderItems() as $orderItem) {
  209.             if ($orderItem->isProduct() && $orderItem->getQuantity() < 0) {
  210.                 $stockOrder false;
  211.                 break;
  212.             }
  213.         }
  214.         // クーポン情報の取得
  215.         $coupon null;
  216.         if ($Order->getOrderCoupon()) {
  217.             $coupon $Order->getOrderCoupon()->getCoupon();
  218.         }
  219.         return [
  220.             'Order' => $Order,
  221.             'stockOrder' => $stockOrder,
  222.             'coupon' => $coupon,
  223.         ];
  224.     }
  225.     /**
  226.      * 再購入を行う.
  227.      *
  228.      * @Route("/mypage/order/{order_no}", name="mypage_order", methods={"PUT"})
  229.      */
  230.     public function order(Request $request$order_no)
  231.     {
  232.         $this->isTokenValid();
  233.         log_info('再注文開始', [$order_no]);
  234.         $Customer $this->getUser();
  235.         /* @var $Order \Eccube\Entity\Order */
  236.         $Order $this->orderRepository->findOneBy(
  237.             [
  238.                 'order_no' => $order_no,
  239.                 'Customer' => $Customer,
  240.             ]
  241.         );
  242.         $event = new EventArgs(
  243.             [
  244.                 'Order' => $Order,
  245.                 'Customer' => $Customer,
  246.             ],
  247.             $request
  248.         );
  249.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_INITIALIZE);
  250.         if (!$Order) {
  251.             log_info('対象の注文が見つかりません', [$order_no]);
  252.             throw new NotFoundHttpException();
  253.         }
  254.         // エラーメッセージの配列
  255.         $errorMessages = [];
  256.         foreach ($Order->getOrderItems() as $OrderItem) {
  257.             try {
  258.                 if ($OrderItem->getProduct() && $OrderItem->getProductClass()) {
  259.                     $this->cartService->addProduct($OrderItem->getProductClass(), $OrderItem->getQuantity());
  260.                     // 明細の正規化
  261.                     $Carts $this->cartService->getCarts();
  262.                     foreach ($Carts as $Cart) {
  263.                         $result $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart$this->getUser()));
  264.                         // 復旧不可のエラーが発生した場合は追加した明細を削除.
  265.                         if ($result->hasError()) {
  266.                             $this->cartService->removeProduct($OrderItem->getProductClass());
  267.                             foreach ($result->getErrors() as $error) {
  268.                                 $errorMessages[] = $error->getMessage();
  269.                             }
  270.                         }
  271.                         foreach ($result->getWarning() as $warning) {
  272.                             $errorMessages[] = $warning->getMessage();
  273.                         }
  274.                     }
  275.                     $this->cartService->save();
  276.                 }
  277.             } catch (CartException $e) {
  278.                 log_info($e->getMessage(), [$order_no]);
  279.                 $this->addRequestError($e->getMessage());
  280.             }
  281.         }
  282.         foreach ($errorMessages as $errorMessage) {
  283.             $this->addRequestError($errorMessage);
  284.         }
  285.         $event = new EventArgs(
  286.             [
  287.                 'Order' => $Order,
  288.                 'Customer' => $Customer,
  289.             ],
  290.             $request
  291.         );
  292.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_COMPLETE);
  293.         if ($event->getResponse() !== null) {
  294.             return $event->getResponse();
  295.         }
  296.         log_info('再注文完了', [$order_no]);
  297.         return $this->redirect($this->generateUrl('cart'));
  298.     }
  299.     /**
  300.      * お気に入り商品を表示する.
  301.      *
  302.      * @Route("/mypage/favorite", name="mypage_favorite", methods={"GET"})
  303.      * @Template("Mypage/favorite.twig")
  304.      */
  305.     public function favorite(Request $requestPaginatorInterface $paginator)
  306.     {
  307.         if (!$this->BaseInfo->isOptionFavoriteProduct()) {
  308.             throw new NotFoundHttpException();
  309.         }
  310.         $Customer $this->getUser();
  311.         // paginator
  312.         $qb $this->customerFavoriteProductRepository->getQueryBuilderByCustomer($Customer);
  313.         $event = new EventArgs(
  314.             [
  315.                 'qb' => $qb,
  316.                 'Customer' => $Customer,
  317.             ],
  318.             $request
  319.         );
  320.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_FAVORITE_SEARCH);
  321.         $pagination $paginator->paginate(
  322.             $qb,
  323.             $request->get('pageno'1),
  324.             $this->eccubeConfig['eccube_search_pmax'],
  325.             ['wrap-queries' => true]
  326.         );
  327.         return [
  328.             'pagination' => $pagination,
  329.         ];
  330.     }
  331.     /**
  332.      * お気に入り商品を削除する.
  333.      *
  334.      * @Route("/mypage/favorite/{id}/delete", name="mypage_favorite_delete", methods={"DELETE"}, requirements={"id" = "\d+"})
  335.      */
  336.     public function delete(Request $requestProduct $Product)
  337.     {
  338.         $this->isTokenValid();
  339.         $Customer $this->getUser();
  340.         log_info('お気に入り商品削除開始', [$Customer->getId(), $Product->getId()]);
  341.         $CustomerFavoriteProduct $this->customerFavoriteProductRepository->findOneBy(['Customer' => $Customer'Product' => $Product]);
  342.         if ($CustomerFavoriteProduct) {
  343.             $this->customerFavoriteProductRepository->delete($CustomerFavoriteProduct);
  344.         } else {
  345.             throw new BadRequestHttpException();
  346.         }
  347.         $event = new EventArgs(
  348.             [
  349.                 'Customer' => $Customer,
  350.                 'CustomerFavoriteProduct' => $CustomerFavoriteProduct,
  351.             ],
  352.             $request
  353.         );
  354.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_DELETE_COMPLETE);
  355.         log_info('お気に入り商品削除完了', [$Customer->getId(), $CustomerFavoriteProduct->getId()]);
  356.         return $this->redirect($this->generateUrl('mypage_favorite'));
  357.     }
  358.     /**
  359.      * 領収書印刷画面を表示する.
  360.      *
  361.      * @Route("/mypage/{order_no}/print_receipt/print", name="mypage_receipt_print")
  362.      *
  363.      * @param Request $request
  364.      * @param $order_no
  365.      * @return \Symfony\Component\HttpFoundation\Response
  366.      */
  367.     public function exportReceiptPrint(Request $request$order_no,)
  368.     {
  369.         $Customer $this->getUser();
  370.         /* @var $Order \Eccube\Entity\Order */
  371.         $Order $this->orderRepository->findOneBy(
  372.             [
  373.                 'order_no' => $order_no,
  374.                 'Customer' => $Customer,
  375.             ]
  376.         );
  377.         if (!$Order) {
  378.             throw new NotFoundHttpException();
  379.         }
  380.         $locale $this->session->get('_locale''ja');
  381.         return $this->render('Mypage/print_receipt.twig', [
  382.             'Order' => $Order,
  383.             'locale' => $locale,  // ロケール情報を追加
  384.         ]);
  385.     }
  386.     /**
  387.      * 領収書印刷画面を表示する(管理画面).
  388.      *
  389.      * @Route("/admin/{order_no}/print_receipt_admin/print", name="admin_receipt_print")
  390.      *
  391.      * @param Request $request
  392.      * @param $order_no
  393.      * @return \Symfony\Component\HttpFoundation\Response
  394.      */
  395.     public function exportReceiptPrintAdmin(Request $request$order_no,)
  396.     {
  397.         // 受注番号から注文情報を取得
  398.         /* @var $Order \Eccube\Entity\Order */
  399.         $Order $this->orderRepository->findOneBy(
  400.             [
  401.                 'order_no' => $order_no,
  402.             ]
  403.         );
  404.         if (!$Order) {
  405.             throw new NotFoundHttpException();
  406.         }
  407.         return $this->render('@admin/Order/print_receipt_admin.twig', [
  408.             'Order' => $Order,
  409.         ]);
  410.     }
  411.     /**
  412.      * 請求書印刷画面を表示する.
  413.      *
  414.      * @Route("/mypage/{order_no}/print_invoice/print", name="mypage_invoice_print")
  415.      *
  416.      * @param Request $request
  417.      * @param $order_no
  418.      * @return \Symfony\Component\HttpFoundation\Response
  419.      */
  420.     public function exportInvoicePrint(Request $request$order_no,)
  421.     {
  422.         $Customer $this->getUser();
  423.         /* @var $Order \Eccube\Entity\Order */
  424.         $Order $this->orderRepository->findOneBy(
  425.             [
  426.                 'order_no' => $order_no,
  427.                 'Customer' => $Customer,
  428.             ]
  429.         );
  430.         $locale $this->session->get('_locale''ja');
  431.         if (!$Order) {
  432.             throw new NotFoundHttpException();
  433.         }
  434.         return $this->render('Mypage/print_invoice.twig', [
  435.             'Order' => $Order,
  436.             'locale' => $locale,  // ロケール情報を追加
  437.         ]);
  438.     }
  439.     /**
  440.      * 請求書印刷画面を表示する(管理画面).
  441.      *
  442.      * @Route("/admin/{order_no}/print_invoice_admin/print", name="admin_invoice_print")
  443.      *
  444.      * @param Request $request
  445.      * @param $order_no
  446.      * @return \Symfony\Component\HttpFoundation\Response
  447.      */
  448.     public function exportInvoicePrintAdmin(Request $request$order_no,)
  449.     {
  450.         // 受注番号から注文情報を取得
  451.         /* @var $Order \Eccube\Entity\Order */
  452.         $Order $this->orderRepository->findOneBy(
  453.             [
  454.                 'order_no' => $order_no,
  455.             ]
  456.         );
  457.         $locale $this->session->get('_locale''ja');
  458.         if (!$Order) {
  459.             throw new NotFoundHttpException();
  460.         }
  461.         // 受注情報から顧客情報(会員情報)を取得
  462.         // $Customer = $Order->getCustomer();
  463.         return $this->render('@admin/Order/print_invoice_admin.twig', [
  464.             'Order' => $Order,
  465.             'locale' => $locale,  // ロケール情報を追加
  466.         ]);
  467.     }
  468.     /**
  469.      * 納品書印刷画面を表示する.
  470.      *
  471.      * @Route("/mypage/{shipping_no}/delivery_note/print", name="mypage_delivery_note_print")
  472.      *
  473.      * @param Request $request
  474.      * @param $shipping_no
  475.      * @return \Symfony\Component\HttpFoundation\Response
  476.      */
  477.     public function exportPrint(Request $request$shipping_no)
  478.     {
  479.         $Customer $this->getUser();
  480.         /* @var $Shipping \Eccube\Entity\Shipping */
  481.         $Shipping $this->shippingRepository->find($shipping_no);
  482.         if (!$Shipping) {
  483.             throw new NotFoundHttpException();
  484.         }
  485.         /* @var $Order \Eccube\Entity\Order */
  486.         $Order $Shipping->getOrder();
  487.         // if (!$Order || $Order->getCustomer() !== $Customer) {
  488.         //     throw new NotFoundHttpException();
  489.         // }
  490.         return $this->render('Mypage/print_delivery_note.twig', [
  491.             'Shipping' => $Shipping,
  492.             'Order' => $Order,
  493.         ]);
  494.     }
  495.     /**
  496.      * 管理画面納品書印刷画面を表示する.
  497.      *
  498.      * @Route("/admin/{shipping_no}/delivery_note/print", name="admin_delivery_note_print")
  499.      *
  500.      * @param Request $request
  501.      * @param $shipping_no
  502.      * @return \Symfony\Component\HttpFoundation\Response
  503.      */
  504.     public function adminExportPrint(Request $request$shipping_no)
  505.     {
  506.         $Customer $this->getUser();
  507.         /* @var $Shipping \Eccube\Entity\Shipping */
  508.         $Shipping $this->shippingRepository->find($shipping_no);
  509.         if (!$Shipping) {
  510.             throw new NotFoundHttpException();
  511.         }
  512.         /* @var $Order \Eccube\Entity\Order */
  513.         $Order $Shipping->getOrder();
  514.         // if (!$Order || $Order->getCustomer() !== $Customer) {
  515.         //     throw new NotFoundHttpException();
  516.         // }
  517.         return $this->render('@admin/Order/print_delivery_note_admin.twig', [
  518.             'Shipping' => $Shipping,
  519.             'Order' => $Order,
  520.         ]);
  521.     }
  522.     /**
  523.      * 領収書発行フラグを保存
  524.      * @Route("/mypage/receipt/{order_no}/upd", name="mypage_receipt_upd", methods={"POST"})
  525.      */
  526.     public function updateReceiptStatus(Request $request$order_no,  EntityManagerInterface $entityManager)
  527.     {
  528.         // データベース更新ロジックを実装
  529.         $order $entityManager->getRepository(Order::class)->findOneBy(['order_no' => $order_no]);
  530.         if (!$order) {
  531.             return new Response('Order not found'Response::HTTP_NOT_FOUND);
  532.         }
  533.         // is_digital_receipt_issuedカラムの値を1に変更
  534.         $order->setIsDigitalReceiptIssued(1);
  535.         $entityManager->flush();
  536.         return $this->json([
  537.             'success' => true
  538.         ]);
  539.     }
  540.     /**
  541.      * 領収書の但し書き,宛名を保存
  542.      * @Route("/mypage/receipt/{order_no}/proviso", name="mypage_receipt_proviso", methods={"POST"})
  543.      */
  544.     public function SaveProvisoReceipt(Request $request$order_noEntityManagerInterface $entityManager$proviso_value null)
  545.     {
  546.         // データベース更新ロジックを実装
  547.         $order $entityManager->getRepository(Order::class)->findOneBy(['order_no' => $order_no]);
  548.         if (!$order) {
  549.             return new Response('Order not found'Response::HTTP_NOT_FOUND);
  550.         }
  551.         // リクエストから送信されたデータを取得
  552.         $proviso_value $request->request->get('proviso_value');
  553.         $proviso_name_value $request->request->get('proviso_name_value');
  554.         // 注文エンティティに但し書き,宛名の情報を設定し、保存
  555.         $order->setIsDigitalReceiptProviso($proviso_value);
  556.         $order->setReceiptName($proviso_name_value);
  557.         $entityManager->flush();
  558.         return $this->json([
  559.             'success' => true
  560.         ]);
  561.     }
  562.     /**
  563.      * 請求書の宛名を保存
  564.      * @Route("/mypage/receipt/{order_no}/invoice", name="mypage_invoice_name", methods={"POST"})
  565.      */
  566.     public function SaveInvoiceName(Request $request$order_noEntityManagerInterface $entityManager$proviso_value null)
  567.     {
  568.         // データベース更新ロジックを実装
  569.         $order $entityManager->getRepository(Order::class)->findOneBy(['order_no' => $order_no]);
  570.         if (!$order) {
  571.             return new Response('Order not found'Response::HTTP_NOT_FOUND);
  572.         }
  573.         // リクエストから送信されたデータを取得
  574.         $invoiceName $request->request->get('invoice_name');
  575.         // 注文エンティティに但し書き,宛名の情報を設定し、保存
  576.         $order->setInvoiceName($invoiceName);
  577.         $entityManager->flush();
  578.         return $this->json([
  579.             'success' => true
  580.         ]);
  581.     }
  582. }