app/Customize/Controller/CartController.php line 132

Open in your IDE?
  1. <?php
  2. namespace Customize\Controller;
  3. use Eccube\Controller\AbstractController;
  4. use Eccube\Entity\BaseInfo;
  5. use Eccube\Entity\ProductClass;
  6. use Eccube\Event\EccubeEvents;
  7. use Eccube\Event\EventArgs;
  8. use Eccube\Repository\BaseInfoRepository;
  9. // use Eccube\Repository\Master\PrefRepository;
  10. use Eccube\Repository\ProductClassRepository;
  11. use Eccube\Service\CartService;
  12. use Eccube\Service\OrderHelper;
  13. use Eccube\Service\PurchaseFlow\PurchaseContext;
  14. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  15. use Eccube\Service\PurchaseFlow\PurchaseFlowResult;
  16. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use Customize\Service\PurchaseFlow\Processor\DeliveryFeePreprocessor;
  20. class CartController extends AbstractController
  21. {
  22.     /**
  23.      * @var ProductClassRepository
  24.      */
  25.     protected $productClassRepository;
  26.     /**
  27.      * @var CartService
  28.      */
  29.     protected $cartService;
  30.     /**
  31.      * @var PurchaseFlow
  32.      */
  33.     protected $purchaseFlow;
  34.     /**
  35.      * @var BaseInfo
  36.      */
  37.     protected $baseInfo;
  38.     // /**
  39.     //  * @var PrefRepository
  40.     //  */
  41.     // protected $prefRepository;
  42.     /**
  43.      * CartController constructor.
  44.      *
  45.      * @param ProductClassRepository $productClassRepository
  46.      * @param CartService $cartService
  47.      * @param PurchaseFlow $cartPurchaseFlow
  48.      * @param BaseInfoRepository $baseInfoRepository
  49.     //  * @param PrefRepository $prefRepository
  50.      */
  51.     public function __construct(
  52.         ProductClassRepository $productClassRepository,
  53.         CartService $cartService,
  54.         PurchaseFlow $cartPurchaseFlow,
  55.         BaseInfoRepository $baseInfoRepository
  56.         // PrefRepository $prefRepository
  57.     ) {
  58.         $this->productClassRepository $productClassRepository;
  59.         $this->cartService $cartService;
  60.         $this->purchaseFlow $cartPurchaseFlow;
  61.         $this->baseInfo $baseInfoRepository->get();
  62.         // $this->prefRepository = $prefRepository;
  63.     }
  64.     /**
  65.      * カート画面.
  66.      *
  67.      * @Route("/cart", name="cart", methods={"GET"})
  68.      * @Template("Cart/index.twig")
  69.      */
  70.     public function index(Request $request)
  71.     {
  72.         // カートを取得して明細の正規化を実行
  73.         $Carts $this->cartService->getCarts();
  74.         $this->execPurchaseFlow($Carts);
  75.         // TODO itemHolderから取得できるように
  76.         $least = [];
  77.         $quantity = [];
  78.         $isDeliveryFree = [];
  79.         $totalPrice 0;
  80.         $totalQuantity 0;
  81.         $estimateRequiredFlg false;
  82.         foreach ($Carts as $Cart) {
  83.             $quantity[$Cart->getCartKey()] = 0;
  84.             $isDeliveryFree[$Cart->getCartKey()] = false;
  85.             if ($this->baseInfo->getDeliveryFreeQuantity()) {
  86.                 if ($this->baseInfo->getDeliveryFreeQuantity() > $Cart->getQuantity()) {
  87.                     $quantity[$Cart->getCartKey()] = $this->baseInfo->getDeliveryFreeQuantity() - $Cart->getQuantity();
  88.                 } else {
  89.                     $isDeliveryFree[$Cart->getCartKey()] = true;
  90.                 }
  91.             }
  92.             if ($this->baseInfo->getDeliveryFreeAmount()) {
  93.                 if (!$isDeliveryFree[$Cart->getCartKey()] && $this->baseInfo->getDeliveryFreeAmount() <= $Cart->getTotalPrice()) {
  94.                     $isDeliveryFree[$Cart->getCartKey()] = true;
  95.                 } else {
  96.                     $least[$Cart->getCartKey()] = $this->baseInfo->getDeliveryFreeAmount() - $Cart->getTotalPrice();
  97.                 }
  98.             }
  99.             $totalPrice += $Cart->getTotalPrice();
  100.             $totalQuantity += $Cart->getQuantity();
  101.             foreach ($Cart->getCartItems() as $CartItem) {
  102.                 if (
  103.                     $CartItem->getProductClass()->getMadeOrderFlg() ||
  104.                     $CartItem->getProductClass()->getEstimateFlg()
  105.                 ) {
  106.                     $estimateRequiredFlg true;
  107.                 }
  108.                 // $saleTypeId = $CartItem->getProductClass()->getSaleType()->getId();
  109.                 // if ($saleTypeId == DeliveryFeePreprocessor::PREF_HOKKAIDO) {
  110.                 //     $estimateRequiredFlg = true;
  111.                 //     continue;
  112.                 // }
  113.             }
  114.         }
  115.         // カートが分割された時のセッション情報を削除
  116.         $request->getSession()->remove(OrderHelper::SESSION_CART_DIVIDE_FLAG);
  117.         return [
  118.             'totalPrice' => $totalPrice,
  119.             'totalQuantity' => $totalQuantity,
  120.             // 空のカートを削除し取得し直す
  121.             'Carts' => $this->cartService->getCarts(true),
  122.             'least' => $least,
  123.             'quantity' => $quantity,
  124.             'is_delivery_free' => $isDeliveryFree,
  125.             // 'Prefectures' => $this->prefRepository->findAll(),
  126.             'estimateRequiredFlg' => $estimateRequiredFlg
  127.         ];
  128.     }
  129.     /**
  130.      * @param $Carts
  131.      *
  132.      * @return \Symfony\Component\HttpFoundation\RedirectResponse|null
  133.      */
  134.     protected function execPurchaseFlow($Carts)
  135.     {
  136.         /** @var PurchaseFlowResult[] $flowResults */
  137.         $flowResults array_map(function ($Cart) {
  138.             $purchaseContext = new PurchaseContext($Cart$this->getUser());
  139.             return $this->purchaseFlow->validate($Cart$purchaseContext);
  140.         }, $Carts);
  141.         // 復旧不可のエラーが発生した場合はカートをクリアして再描画
  142.         $hasError false;
  143.         foreach ($flowResults as $result) {
  144.             if ($result->hasError()) {
  145.                 $hasError true;
  146.                 foreach ($result->getErrors() as $error) {
  147.                     $this->addRequestError($error->getMessage());
  148.                 }
  149.             }
  150.         }
  151.         if ($hasError) {
  152.             $this->cartService->clear();
  153.             return $this->redirectToRoute('cart');
  154.         }
  155.         $this->cartService->save();
  156.         foreach ($flowResults as $index => $result) {
  157.             foreach ($result->getWarning() as $warning) {
  158.                 if ($Carts[$index]->getItems()->count() > 0) {
  159.                     $cart_key $Carts[$index]->getCartKey();
  160.                     $this->addRequestError($warning->getMessage(), "front.cart.${cart_key}");
  161.                 } else {
  162.                     // キーが存在しない場合はグローバルにエラーを表示する
  163.                     $this->addRequestError($warning->getMessage());
  164.                 }
  165.             }
  166.         }
  167.         return null;
  168.     }
  169.     /**
  170.      * カート明細の加算/減算/削除を行う.
  171.      *
  172.      * - 加算
  173.      *      - 明細の個数を1増やす
  174.      * - 減算
  175.      *      - 明細の個数を1減らす
  176.      *      - 個数が0になる場合は、明細を削除する
  177.      * - 削除
  178.      *      - 明細を削除する
  179.      *
  180.      * @Route(
  181.      *     path="/cart/{operation}/{productClassId}/{shoppingFlag}",
  182.      *     name="cart_handle_item",
  183.      *     methods={"PUT"},
  184.      *     requirements={
  185.      *          "operation": "up|down|remove",
  186.      *          "productClassId": "\d+"
  187.      *     }
  188.      * )
  189.      */
  190.     public function handleCartItem($operation$productClassId$shoppingFlag=false)
  191.     {
  192.         log_info('カート明細操作開始', ['operation' => $operation'product_class_id' => $productClassId]);
  193.         $this->isTokenValid();
  194.         /** @var ProductClass $ProductClass */
  195.         $ProductClass $this->productClassRepository->find($productClassId);
  196.         if (is_null($ProductClass)) {
  197.             log_info('商品が存在しないため、カート画面へredirect', ['operation' => $operation'product_class_id' => $productClassId]);
  198.             return $this->redirectToRoute('cart');
  199.         }
  200.         // 明細の増減・削除
  201.         switch ($operation) {
  202.             case 'up':
  203.                 $this->cartService->addProduct($ProductClass1);
  204.                 break;
  205.             case 'down':
  206.                 $this->cartService->addProduct($ProductClass, -1);
  207.                 break;
  208.             case 'remove':
  209.                 $this->cartService->removeProduct($ProductClass);
  210.                 break;
  211.         }
  212.         // カートを取得して明細の正規化を実行
  213.         $Carts $this->cartService->getCarts();
  214.         $this->execPurchaseFlow($Carts);
  215.         log_info('カート演算処理終了', ['operation' => $operation'product_class_id' => $productClassId]);
  216.         // 購入手続きでも処理を併用するため
  217.         if ($shoppingFlag) {
  218.             return $this->redirectToRoute('shopping');
  219.         } else {
  220.             return $this->redirectToRoute('cart');
  221.         }
  222.     }
  223.     /**
  224.      * カートをロック状態に設定し、購入確認画面へ遷移する.
  225.      *
  226.      * @Route("/cart/buystep/{cart_key}", name="cart_buystep", requirements={"cart_key" = "[a-zA-Z0-9]+[_][\x20-\x7E]+"}, methods={"GET"})
  227.      */
  228.     public function buystep(Request $request$cart_key)
  229.     {
  230.         $Carts $this->cartService->getCart();
  231.         if (!is_object($Carts)) {
  232.             return $this->redirectToRoute('cart');
  233.         }
  234.         // FRONT_CART_BUYSTEP_INITIALIZE
  235.         $event = new EventArgs(
  236.             [],
  237.             $request
  238.         );
  239.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_CART_BUYSTEP_INITIALIZE);
  240.         $this->cartService->setPrimary($cart_key);
  241.         $this->cartService->save();
  242.         // FRONT_CART_BUYSTEP_COMPLETE
  243.         $event = new EventArgs(
  244.             [],
  245.             $request
  246.         );
  247.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_CART_BUYSTEP_COMPLETE);
  248.         if ($event->hasResponse()) {
  249.             return $event->getResponse();
  250.         }
  251.         return $this->redirectToRoute('shopping');
  252.     }
  253. }