app/Customize/Controller/MyPageController.php line 102

Open in your IDE?
  1. <?php
  2. namespace Customize\Controller;
  3. use Carbon\Carbon;
  4. use Customize\Service\EcconnectfmService;
  5. use Customize\Repository\OrderRepository;
  6. use Customize\Repository\OrderItemRepository;
  7. use Eccube\Entity\Customer;
  8. use Eccube\Event\EccubeEvents;
  9. use Eccube\Event\EventArgs;
  10. use Eccube\Form\Type\AddCartType;
  11. use Eccube\Controller\AbstractController;
  12. use Eccube\Repository\ProductClassRepository;
  13. use Eccube\Repository\ProductRepository;
  14. use Eccube\Service\CartService;
  15. use Eccube\Service\PurchaseFlow\PurchaseContext;
  16. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  17. use Knp\Component\Pager\PaginatorInterface;
  18. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  22. use Symfony\Component\Routing\Annotation\Route;
  23. class MyPageController extends AbstractController
  24. {
  25.     /**
  26.      * @var PurchaseFlow
  27.      */
  28.     protected $purchaseFlow;
  29.     /**
  30.      * @var OrderRepository
  31.      */
  32.     protected $orderRepository;
  33.     /**
  34.      * @var OrderItemRepository
  35.      */
  36.     protected $orderItemRepository;
  37.     /**
  38.      * @var ProductRepository
  39.      */
  40.     protected $productRepository;
  41.     /**
  42.      * @var ProductClassRepository
  43.      */
  44.     protected $productClassRepository;
  45.     /**
  46.      * @var CartService
  47.      */
  48.     protected $cartService;
  49.     /**
  50.      * @var EcconnectfmService
  51.      */
  52.     private EcconnectfmService $ecconnectfmService;
  53.     /**
  54.      * MypageController constructor.
  55.      */
  56.     public function __construct(
  57.         PurchaseFlow $cartPurchaseFlow,
  58.         OrderRepository $orderRepository,
  59.         OrderItemRepository $orderItemRepository,
  60.         ProductRepository $productRepository,
  61.         ProductClassRepository $productClassRepository,
  62.         CartService $cartService,
  63.         EcconnectfmService $ecconnectfmService
  64.     ) {
  65.         $this->purchaseFlow $cartPurchaseFlow;
  66.         $this->orderRepository $orderRepository;
  67.         $this->orderItemRepository $orderItemRepository;
  68.         $this->productRepository $productRepository;
  69.         $this->productClassRepository $productClassRepository;
  70.         $this->cartService $cartService;
  71.         $this->ecconnectfmService $ecconnectfmService;
  72.     }
  73.     /**
  74.      * @Route("/mypage/detail", name="mypage_detail", methods={"GET"})
  75.      * @Template("Mypage/detail.twig")
  76.      */
  77.     public function detail()
  78.     {
  79.         /** @var Customer $Customer */
  80.         $Customer $this->getUser();
  81.         $Customer->setPlainPassword($this->eccubeConfig['eccube_default_password']);
  82.         return [
  83.             'Customer' => $Customer,
  84.         ];
  85.     }
  86.     /**
  87.      * @Route("/mypage/history", name="mypage_history", methods={"GET"})
  88.      * @Template("Mypage/history_index.twig")
  89.      */
  90.     public function history(Request $requestPaginatorInterface $paginator)
  91.     {
  92.         $Customer $this->getUser();
  93.         // 購入処理中/決済処理中ステータスの受注を非表示にする.
  94.         $this->entityManager
  95.             ->getFilters()
  96.             ->enable('incomplete_order_status_hidden');
  97.         // paginator
  98.         $qb $this->orderRepository->getQueryBuilderByCustomer($Customer);
  99.         $pagination $paginator->paginate(
  100.             $qb,
  101.             $request->get('pageno'1),
  102.             $this->eccubeConfig['eccube_search_pmax']
  103.         );
  104.         return [
  105.             'pagination' => $pagination,
  106.         ];
  107.     }
  108.     /**
  109.      * @Route("/mypage/history/{id}", name="mypage_history_detail", methods={"GET"}, requirements={"id" = "\d+"})
  110.      * @Template("Mypage/history_detail.twig")
  111.      */
  112.     public function historyDetail(Request $request$id)
  113.     {
  114.         if (empty($id)) {
  115.             return $this->redirectToRoute('mypage_history');
  116.         }
  117.         $Customer $this->getUser();
  118.         $Order $this->orderRepository->getOrderByCustomer($Customer$id);
  119.         return [
  120.             'Customer' => $Customer,
  121.             'Order' => $Order,
  122.         ];
  123.     }
  124.     /**
  125.      * @Route("/mypage/reserve", name="mypage_reserve", methods={"GET"})
  126.      * @Template("Mypage/reserve.twig")
  127.      */
  128.     public function reserve(Request $requestPaginatorInterface $paginator)
  129.     {
  130.         /** @var Customer $Customer */
  131.         $Customer $this->getUser();
  132.         $Customer->setPlainPassword($this->eccubeConfig['eccube_default_password']);
  133.         $qb $this->orderRepository->getReserveQueryBuilderByCustomer($Customer);
  134.         $pagination $paginator->paginate(
  135.             $qb,
  136.             $request->get('pageno'1),
  137.             $this->eccubeConfig['eccube_search_pmax'],
  138.             ['wrap-queries' => true]
  139.         );
  140.         $Reserves $qb->getQuery()->getResult();
  141.         return [
  142.             'pagination' => $pagination,
  143.             'totalQuantity' => empty($Reserves) ? count($Reserves[0]->getOrderItems())
  144.         ];
  145.     }
  146.     /**
  147.      * 予約明細の加算/減算/削除を行う.
  148.      *
  149.      * - 加算
  150.      *      - 明細の個数を1増やす
  151.      * - 減算
  152.      *      - 明細の個数を1減らす
  153.      *      - 個数が0になる場合は、明細を削除する
  154.      * - 削除
  155.      *      - 明細を削除する
  156.      *
  157.      * @Route(
  158.      *     path="/reserve/{operation}/{productId}/{productClassId}",
  159.      *     name="reserve_handle_item",
  160.      *     methods={"PUT"},
  161.      *     requirements={
  162.      *          "operation": "extend|remove",
  163.      *          "productId": "\d+",
  164.      *          "productClassId": "\d+"
  165.      *     }
  166.      * )
  167.      */
  168.     public function handleReserveItem($operation$productId$productClassId)
  169.     {
  170.         log_info('予約明細操作開始', ['operation' => $operation'product_class_id' => $productClassId]);
  171.         $this->isTokenValid();
  172.         $Product $this->productRepository->find($productId);
  173.         $ProductClass $this->productClassRepository->find($productClassId);
  174.         if (is_null($Product) || is_null($ProductClass)) {
  175.             log_info('予約商品が存在しないため、予約画面へredirect', ['operation' => $operation'product_id' => $productId'product_class_id' => $productClassId]);
  176.             return $this->redirectToRoute('mypage_reserve');
  177.         }
  178.         $ReserveItems $this->orderItemRepository->getReserveItems($this->getUser(), $Product$ProductClass);
  179.         if (is_null($ReserveItems)) {
  180.             log_info('予約商品が存在しないため、予約画面へredirect', ['operation' => $operation'product_id' => $productId'product_class_id' => $productClassId]);
  181.             return $this->redirectToRoute('mypage_reserve');
  182.         }
  183.         // 明細の増減・削除
  184.         switch ($operation) {
  185.             case 'extend':
  186.                 foreach ($ReserveItems as $ReserveItem) {
  187.                     $ReserveItem->setExpirationContactFlg(false);
  188.                     $ReserveItem->setExpirationDate(Carbon::now()->addDays(90));
  189.                     $this->entityManager->persist($ReserveItem);
  190.                     $this->entityManager->flush();
  191.                 }
  192.                 break;
  193.             case 'remove':
  194.                 $updateItems = [];
  195.                 foreach ($ReserveItems as $ReserveItem) {
  196.                     $updateItem['serial_no'] = $ReserveItem->getProductClass()->getSerialNo();
  197.                     $updateItem['quantity'] = $ReserveItem->getQuantity();
  198.                     $updateItem['note'] = '予約注文キャンセルによる引き落とし';
  199.                     $updateItems[] = $updateItem;
  200.                     $this->entityManager->remove($ReserveItem);
  201.                     $this->entityManager->flush();
  202.                 }
  203.                 $this->ecconnectfmService->updateFmProductStock($updateItems);
  204.                 break;
  205.         }
  206.         log_info('予約演算処理終了', ['operation' => $operation'product_id' => $productId'product_class_id' => $productClassId]);
  207.         return $this->redirectToRoute('mypage_reserve');
  208.     }
  209.     /**
  210.      * カートに追加.
  211.      *
  212.      * @Route("/reserve/add_cart", name="reserve_add_cart", methods={"POST"})
  213.      */
  214.     public function addCart(Request $request)
  215.     {
  216.         $requestData $request->request->all();
  217.         if (
  218.             empty($requestData['product_id']) ||
  219.             empty($requestData['product_class_id']) ||
  220.             empty($requestData['quantity'])
  221.         ) {
  222.             throw new NotFoundHttpException();
  223.         }
  224.         $Product $this->productRepository->findWithSortedClassCategories($requestData['product_id']);
  225.         $targetProductClass null;
  226.         foreach ($Product->getProductClasses() as $ProductClass) {
  227.             if ($ProductClass->getId() == $requestData['product_class_id']) {
  228.                 $targetProductClass $ProductClass;
  229.             }
  230.         }
  231.         if (empty($targetProductClass)) {
  232.             throw new NotFoundHttpException();
  233.         }
  234.         $builder $this->formFactory->createNamedBuilder(
  235.             '',
  236.             AddCartType::class,
  237.             null,
  238.             [
  239.                 'product' => $Product,
  240.                 'id_add_product_id' => false,
  241.             ],
  242.         );
  243.         $event = new EventArgs(
  244.             [
  245.                 'builder' => $builder,
  246.                 'Product' => $Product,
  247.             ],
  248.             $request
  249.         );
  250.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_PRODUCT_CART_ADD_INITIALIZE);
  251.         /* @var $form \Symfony\Component\Form\FormInterface */
  252.         $form $builder->getForm();
  253.         log_info(
  254.             'カート追加処理開始',
  255.             [
  256.                 'product_id' => $Product->getId(),
  257.                 'product_class_id' => $requestData['product_class_id'],
  258.                 'quantity' => $requestData['quantity'],
  259.             ]
  260.         );
  261.         // カートへ追加
  262.         $this->cartService->addProduct($requestData['product_class_id'], $requestData['quantity']);
  263.         // 明細の正規化
  264.         $Carts $this->cartService->getCarts();
  265.         foreach ($Carts as $Cart) {
  266.             $result $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart$this->getUser()));
  267.             // 復旧不可のエラーが発生した場合は追加した明細を削除.
  268.             if ($result->hasError()) {
  269.                 $this->cartService->removeProduct($requestData['product_class_id']);
  270.                 foreach ($result->getErrors() as $error) {
  271.                     $errorMessages[] = $error->getMessage();
  272.                 }
  273.             }
  274.             foreach ($result->getWarning() as $warning) {
  275.                 $errorMessages[] = $warning->getMessage();
  276.             }
  277.         }
  278.         $this->cartService->save();
  279.         log_info(
  280.             'カート追加処理完了',
  281.             [
  282.                 'product_id' => $Product->getId(),
  283.                 'product_class_id' => $requestData['product_class_id'],
  284.                 'quantity' => $requestData['quantity'],
  285.             ]
  286.         );
  287.         $event = new EventArgs(
  288.             [
  289.                 'form' => $form,
  290.                 'Product' => $Product,
  291.             ],
  292.             $request
  293.         );
  294.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_PRODUCT_CART_ADD_COMPLETE);
  295.         if ($event->getResponse() !== null) {
  296.             return $event->getResponse();
  297.         }
  298.         // 不要になった予約商品を削除
  299.         $ReserveItems $this->orderItemRepository->getReserveItems($this->getUser(), $Product$targetProductClass);
  300.         foreach ($ReserveItems as $ReserveItem) {
  301.             $this->entityManager->remove($ReserveItem);
  302.             $this->entityManager->flush();
  303.         }
  304.         return $this->json(['done' => true]);
  305.     }
  306. }