app/Customize/Controller/CartController.php line 124

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 Customize\Controller;
  13. use Customize\Repository\Master\MtbPrintRepository;
  14. use Customize\Service\TaxRuleServiceCustom as TaxRuleService;
  15. use Eccube\Controller\AbstractController;
  16. use Eccube\Entity\BaseInfo;
  17. use Eccube\Entity\ProductClass;
  18. use Eccube\Event\EccubeEvents;
  19. use Eccube\Event\EventArgs;
  20. use Eccube\Repository\BaseInfoRepository;
  21. use Eccube\Repository\ClassCategoryRepository;
  22. use Eccube\Repository\ClassNameRepository;
  23. use Eccube\Repository\ProductClassRepository;
  24. use Customize\Service\CartService;
  25. use Eccube\Repository\TaxRuleRepository;
  26. use Eccube\Service\OrderHelper;
  27. use Eccube\Service\PurchaseFlow\PurchaseContext;
  28. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  29. use Eccube\Service\PurchaseFlow\PurchaseFlowResult;
  30. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  31. use Symfony\Component\HttpFoundation\Request;
  32. use Symfony\Component\Routing\Annotation\Route;
  33. use Eccube\Repository\ProductRepository;
  34. class CartController extends AbstractController
  35. {
  36.     /**
  37.      * @var ProductClassRepository
  38.      */
  39.     protected $productClassRepository;
  40.     /**
  41.      * @var CartService
  42.      */
  43.     protected $cartService;
  44.     /**
  45.      * @var PurchaseFlow
  46.      */
  47.     protected $purchaseFlow;
  48.     /**
  49.      * @var BaseInfo
  50.      */
  51.     protected $baseInfo;
  52.     protected $mtbPrintRepository;
  53.     protected $taxRuleService;
  54.     protected $taxRuleRepository;
  55.     protected $productRepository;
  56.     protected $classNameRepository;
  57.     protected $classCategoryRepository;
  58.     /**
  59.      * CartController constructor.
  60.      *
  61.      * @param ProductClassRepository $productClassRepository
  62.      * @param CartService $cartService
  63.      * @param PurchaseFlow $cartPurchaseFlow
  64.      * @param BaseInfoRepository $baseInfoRepository
  65.      */
  66.     public function __construct(
  67.         ProductClassRepository $productClassRepository,
  68.         CartService $cartService,
  69.         PurchaseFlow $cartPurchaseFlow,
  70.         BaseInfoRepository $baseInfoRepository,
  71.         MtbPrintRepository $mtbPrintRepository,
  72.         TaxRuleService $taxRuleService,
  73.         TaxRuleRepository $taxRuleRepository,
  74.         ProductRepository $productRepository,
  75.         ClassNameRepository $classNameRepository,
  76.         ClassCategoryRepository $classCategoryRepository
  77.     ) {
  78.         $this->productClassRepository $productClassRepository;
  79.         $this->cartService $cartService;
  80.         $this->purchaseFlow $cartPurchaseFlow;
  81.         $this->baseInfo $baseInfoRepository->get();
  82.         $this->mtbPrintRepository $mtbPrintRepository;
  83.         $this->taxRuleService $taxRuleService;
  84.         $this->taxRuleRepository $taxRuleRepository;
  85.         $this->productRepository $productRepository;
  86.         $this->classNameRepository $classNameRepository;
  87.         $this->classCategoryRepository $classCategoryRepository;
  88.     }
  89.     /**
  90.      * カート画面.
  91.      *
  92.      * @Route("/cart", name="cart", methods={"GET"})
  93.      * @Template("Cart/index.twig")
  94.      */
  95.     public function index(Request $request)
  96.     {
  97.         // カートを取得して明細の正規化を実行
  98.         $userRate 0;
  99.         if($this->getUser()) {
  100.             $userRate $this->baseInfo->getOptionUserRate();
  101.         }
  102.         $Carts $this->cartService->getCarts();
  103.         $this->execPurchaseFlow($Carts);
  104.         // TODO itemHolderから取得できるように
  105.         $least = [];
  106.         $quantity = [];
  107.         $isDeliveryFree = [];
  108.         $totalPrice 0;
  109.         $totalQuantity 0;
  110.         $data_fee_print_all_price $this->mtbPrintRepository->findOneBy(['product_print_fee_type' => 4'discriminator_type' => 'fee_print_all']);
  111.         $fee_print_all_price 0;
  112.         $platePrint = [];
  113.         $taxRate $this->taxRuleService->getTaxRate();
  114.         foreach ($Carts as $Cart) {
  115.             $totalProductPrice 0;
  116.             $quantity[$Cart->getCartKey()] = 0;
  117.             $isDeliveryFree[$Cart->getCartKey()] = false;
  118.             if ($this->baseInfo->getDeliveryFreeQuantity()) {
  119.                 if ($this->baseInfo->getDeliveryFreeQuantity() > $Cart->getQuantity()) {
  120.                     $quantity[$Cart->getCartKey()] = $this->baseInfo->getDeliveryFreeQuantity() - $Cart->getQuantity();
  121.                 } else {
  122.                     $isDeliveryFree[$Cart->getCartKey()] = true;
  123.                 }
  124.             }
  125.             $classNameSize $this->classNameRepository->findBy([
  126.                 "name" => "サイズ"
  127.             ]);
  128.             $arrIgnoreMerge = [];
  129.             foreach($classNameSize as $value) {
  130.                 $ClassCategories $this->classCategoryRepository->getList($value);
  131.                 foreach ($ClassCategories as $ClassCategory) {
  132.                     $arrIgnoreMerge[] = $ClassCategory->getId();
  133.                 }
  134.             }
  135.             $newArrId = [];
  136.             foreach($Cart->GetCartItems() as $cartItem) {
  137.                 if ($cartItem->getIsPrint() == 1) {
  138.                     $idProduct $cartItem->getProductClass()->getProduct()->getId();
  139.                     $product $this->productRepository->find($idProduct);
  140.                     if(!isset($newArrId[$idProduct.'_'.$cartItem->getIsRepurchase().'_'.$cartItem->getProductPrintColor()])) {
  141.                         $newArrId[$idProduct.'_'.$cartItem->getIsRepurchase().'_'.$cartItem->getProductPrintColor()] = [];
  142.                     }
  143.                     if(isset($newArrId[$idProduct.'_'.$cartItem->getIsRepurchase().'_'.$cartItem->getProductPrintColor()]['qty'])) {
  144.                         $newArrId[$idProduct '_' $cartItem->getIsRepurchase() . '_' $cartItem->getProductPrintColor()]['qty'] += $cartItem->getQuantity();
  145.                         $qty $newArrId[$idProduct '_' $cartItem->getIsRepurchase() . '_' $cartItem->getProductPrintColor()]['qty'];
  146.                     }else {
  147.                         $qty $cartItem->getQuantity();
  148.                     }
  149.                     $newArrId[$idProduct.'_'.$cartItem->getIsRepurchase().'_'.$cartItem->getProductPrintColor()]['qty'] = $qty;
  150.                     $newArrId[$idProduct.'_'.$cartItem->getIsRepurchase().'_'.$cartItem->getProductPrintColor()]['idProduct'] = $idProduct;
  151.                     $newArrId[$idProduct.'_'.$cartItem->getIsRepurchase().'_'.$cartItem->getProductPrintColor()]['isRepurchase'] = $cartItem->getIsRepurchase();
  152.                     $newArrId[$idProduct.'_'.$cartItem->getIsRepurchase().'_'.$cartItem->getProductPrintColor()]['color'] = $cartItem->getProductPrintColor();
  153.                     $newArrId[$idProduct.'_'.$cartItem->getIsRepurchase().'_'.$cartItem->getProductPrintColor()]['id'][] = $cartItem->getId();
  154.                 }
  155.             }
  156.             $newArrUpdate = [];
  157.             foreach($newArrId as $k => $v) {
  158.                 foreach ($v['id'] as $vv) {
  159.                     $newArrUpdate[$vv] = [
  160.                         'idProduct' => $v['idProduct'],
  161.                         'qty' => $v['qty']
  162.                     ];
  163.                 }
  164.             }
  165.             foreach($Cart->GetCartItems() as $cartItem) {
  166.                 if ($cartItem->getIsPrint() == 1) {
  167.                     $qty $newArrUpdate[$cartItem->getId()]['qty'];
  168.                     $idProduct $cartItem->getProductClass()->getProduct()->getId();
  169.                     $product_print_fee_price 0;
  170.                     if ($cartItem->getProductPrintFeeType() == 1) {
  171.                         $dataMtbPrint $this->mtbPrintRepository->findBy(
  172.                             [
  173.                                 'product_print_fee_type' => $cartItem->getProductPrintFeeType(),
  174.                                 'discriminator_type' => 'fee_print_name'
  175.                             ]
  176.                         );
  177.                         foreach ($dataMtbPrint as $value) {
  178.                             if ($value['product_print_fee_type_max'] > 0) {
  179.                                 if ($qty >= $value['product_print_fee_type_min'] && $qty <= $value['product_print_fee_type_max']) {
  180.                                     $product_print_fee_price $value['price'];
  181.                                     break;
  182.                                 }
  183.                             } else {
  184.                                 $product_print_fee_price $value['price'];
  185.                             }
  186.                         }
  187.                     } elseif ($cartItem->getProductPrintFeeType() == 3) {
  188.                         $dataMtbPrint $this->mtbPrintRepository->findBy(
  189.                             [
  190.                                 'product_print_fee_type' => $cartItem->getProductPrintFeeType(),
  191.                                 'discriminator_type' => 'fee_print_name'
  192.                             ]
  193.                         );
  194.                         foreach ($dataMtbPrint as $value) {
  195.                             if ($value['product_print_fee_type_max'] > 0) {
  196.                                 $product_print_fee_price 50;
  197.                             } else {
  198.                                 if ($qty >= $value['product_print_fee_type_min']) {
  199.                                     $product_print_fee_price $value['price'];
  200.                                     break;
  201.                                 }
  202.                             }
  203.                         }
  204.                     } else {
  205.                         if($cartItem->getProductClass()) {
  206.                             $idProduct $cartItem->getProductClass()->getProduct()->getId();
  207.                             $product $this->productRepository->find($idProduct);
  208.                             $product_print_fee_price $product->getProductPrintFeePrice();
  209.                         }
  210.                     }
  211.                     $cartItem->setProductPrintFeePrice($product_print_fee_price);
  212.                 }
  213.             }
  214.             foreach($Cart->GetCartItems() as $cartItem) {
  215.                 $pricePrint 0;
  216.                 if($cartItem->isProduct()) {
  217.                     $totalProductPrice += $cartItem->getPrice() * $cartItem->getQuantity();
  218.                 }
  219.                 if ($cartItem->getIsPrint() == 1) {
  220.                     if ($cartItem->getIsRepurchase() == 0) {
  221.                         $platePrint[] = 1;
  222.                     }
  223.                     $pricePrint $cartItem->getProductPrintFeePrice() * $cartItem->getQuantity();
  224.                 }
  225.                 $totalPrice $totalPrice + ($cartItem->getTotalPrice()+$pricePrint);
  226.             }
  227.             $priceUserRate $totalProductPrice * ($userRate/100);
  228.             if (count($platePrint) > 0) {
  229.                 $fee_print_all_price = (int)$data_fee_print_all_price->getPrice()*count($platePrint);
  230.                 if ($data_fee_print_all_price ) {
  231.                     $fee_print_all_price = (int)$data_fee_print_all_price->getPrice()*count($platePrint);
  232.                 }
  233.             }
  234.             $totalPriceProductFee = ($totalPrice $priceUserRate) + $fee_print_all_price;
  235. //            $totalPriceTaxRate = $totalPriceProductFee * ($taxRate/100);
  236.             $totalPrice $totalPriceProductFee;
  237.             if ($this->baseInfo->getDeliveryFreeAmount()) {
  238.                 if (!$isDeliveryFree[$Cart->getCartKey()] && $this->baseInfo->getDeliveryFreeAmount() <= $totalPrice) {
  239.                     $isDeliveryFree[$Cart->getCartKey()] = true;
  240.                 } else {
  241.                     $least[$Cart->getCartKey()] = $this->baseInfo->getDeliveryFreeAmount() - $totalPrice;
  242.                 }
  243.             }
  244. //            $totalPrice += $Cart->getTotalPrice();
  245.             $totalQuantity += $Cart->getQuantity();
  246.         }
  247.         // カートが分割された時のセッション情報を削除
  248.         $request->getSession()->remove(OrderHelper::SESSION_CART_DIVIDE_FLAG);
  249.         $TaxRule $this->taxRuleRepository->getByRule();
  250.         $roundType 'common';
  251.         if($TaxRule->getRoundingType()->getId() == \Eccube\Entity\Master\RoundingType::ROUND) {
  252.             $roundType 'common';
  253.         } else if($TaxRule->getRoundingType()->getId() == \Eccube\Entity\Master\RoundingType::FLOOR) {
  254.             $roundType 'floor';
  255.         } else if($TaxRule->getRoundingType()->getId() == \Eccube\Entity\Master\RoundingType::CEIL) {
  256.             $roundType 'ceil';
  257.         } else {
  258.             $roundType 'ceil';
  259.         }
  260.         return [
  261.             'totalPrice' => $totalPrice,
  262.             'fee_print_all_price' => $fee_print_all_price,
  263.             'totalPlatePrint' => count($platePrint),
  264.             'totalQuantity' => $totalQuantity,
  265.             // 空のカートを削除し取得し直す
  266.             'Carts' => $this->cartService->getCarts(true),
  267.             'least' => $least,
  268.             'quantity' => $quantity,
  269.             'is_delivery_free' => $isDeliveryFree,
  270.             'userRate' => $userRate,
  271.             'taxRate' => $taxRate,
  272.             'roundType' => $roundType,
  273.         ];
  274.     }
  275.     /**
  276.      * @param $Carts
  277.      *
  278.      * @return \Symfony\Component\HttpFoundation\RedirectResponse|null
  279.      */
  280.     protected function execPurchaseFlow($Carts)
  281.     {
  282.         /** @var PurchaseFlowResult[] $flowResults */
  283.         $flowResults array_map(function ($Cart) {
  284.             $purchaseContext = new PurchaseContext($Cart$this->getUser());
  285.             return $this->purchaseFlow->validate($Cart$purchaseContext);
  286.         }, $Carts);
  287.         // 復旧不可のエラーが発生した場合はカートをクリアして再描画
  288.         $hasError false;
  289.         foreach ($flowResults as $result) {
  290.             if ($result->hasError()) {
  291.                 $hasError true;
  292.                 foreach ($result->getErrors() as $error) {
  293.                     $this->addRequestError($error->getMessage());
  294.                 }
  295.             }
  296.         }
  297.         if ($hasError) {
  298.             $this->cartService->clear();
  299.             return $this->redirectToRoute('cart');
  300.         }
  301.         $this->cartService->save();
  302.         foreach ($flowResults as $index => $result) {
  303.             foreach ($result->getWarning() as $warning) {
  304.                 if ($Carts[$index]->getItems()->count() > 0) {
  305.                     $cart_key $Carts[$index]->getCartKey();
  306.                     $this->addRequestError($warning->getMessage(), "front.cart.${cart_key}");
  307.                 } else {
  308.                     // キーが存在しない場合はグローバルにエラーを表示する
  309.                     $this->addRequestError($warning->getMessage());
  310.                 }
  311.             }
  312.         }
  313.         return null;
  314.     }
  315.     /**
  316.      * カート明細の加算/減算/削除を行う.
  317.      *
  318.      * - 加算
  319.      *      - 明細の個数を1増やす
  320.      * - 減算
  321.      *      - 明細の個数を1減らす
  322.      *      - 個数が0になる場合は、明細を削除する
  323.      * - 削除
  324.      *      - 明細を削除する
  325.      *
  326.      * @Route(
  327.      *     path="/cart/{operation}/{productClassId}",
  328.      *     name="cart_handle_item",
  329.      *     methods={"PUT"},
  330.      *     requirements={
  331.      *          "operation": "up|down|remove",
  332.      *          "productClassId": "\d+"
  333.      *     }
  334.      * )
  335.      */
  336.     public function handleCartItem($operation$productClassId$cartItemId 0)
  337.     {
  338.         log_info('カート明細操作開始', ['operation' => $operation'product_class_id' => $productClassId]);
  339.         $this->isTokenValid();
  340.         /** @var ProductClass $ProductClass */
  341.         $ProductClass $this->productClassRepository->find($productClassId);
  342.         if (is_null($ProductClass)) {
  343.             log_info('商品が存在しないため、カート画面へredirect', ['operation' => $operation'product_class_id' => $productClassId]);
  344.             return $this->redirectToRoute('cart');
  345.         }
  346.         $cartItemId 0;
  347.         if(isset($_GET['cartItemId'])) {
  348.             $cartItemId = (int)$_GET['cartItemId'];
  349.         }
  350.         $isPrint false;
  351.         foreach($this->cartService->getCart()->getItems() as $value) {
  352.             if($value->getId() == $cartItemId) {
  353.                 if($value->getIsPrint() == 1) {
  354.                     $isPrint true;
  355.                 }
  356.                 break;
  357.             }
  358.         }
  359.         $qty 1;
  360.         if($isPrint == true) {
  361.             $printUnit = (int)$ProductClass->getProduct()->getPrinterUnit();
  362.             $orderUnit = (int)$ProductClass->getProduct()->getOrderUnit();
  363.             if($printUnit 0) {
  364.                 $qty $printUnit;
  365.             } else {
  366.                 if($orderUnit 0) {
  367.                     $qty $orderUnit;
  368.                 } else {
  369.                     $qty 1;
  370.                 }
  371.             }
  372.         } else {
  373.             $orderUnit = (int)$ProductClass->getProduct()->getOrderUnit();
  374.             $qty = ($orderUnit 0) ? $ProductClass->getProduct()->getOrderUnit() : 1;
  375.         }
  376.         // 明細の増減・削除
  377.         switch ($operation) {
  378.             case 'up':
  379.                 $this->cartService->addProduct($ProductClass$qty, ['update_quantity' => true'productClassId' => $productClassId'cartItemId' => $cartItemId]);
  380.                 break;
  381.             case 'down':
  382.                 $this->cartService->addProduct($ProductClass, -$qty, ['update_quantity' => true'cartItemId' => $cartItemId]);
  383.                 break;
  384.             case 'remove':
  385.                 $this->cartService->removeProduct($ProductClass$cartItemId);
  386.                 break;
  387.         }
  388.         // カートを取得して明細の正規化を実行
  389.         $Carts $this->cartService->getCarts();
  390.         $this->execPurchaseFlow($Carts);
  391.         log_info('カート演算処理終了', ['operation' => $operation'product_class_id' => $productClassId]);
  392.         return $this->redirectToRoute('cart');
  393.     }
  394.     /**
  395.      * カートをロック状態に設定し、購入確認画面へ遷移する.
  396.      *
  397.      * @Route("/cart/buystep/{cart_key}", name="cart_buystep", requirements={"cart_key" = "[a-zA-Z0-9]+[_][\x20-\x7E]+"}, methods={"GET"})
  398.      */
  399.     public function buystep(Request $request$cart_key)
  400.     {
  401.         $Carts $this->cartService->getCart();
  402.         if (!is_object($Carts)) {
  403.             return $this->redirectToRoute('cart');
  404.         }
  405.         // FRONT_CART_BUYSTEP_INITIALIZE
  406.         $event = new EventArgs(
  407.             [],
  408.             $request
  409.         );
  410.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_CART_BUYSTEP_INITIALIZE);
  411.         $this->cartService->setPrimary($cart_key);
  412.         $this->cartService->save();
  413.         // FRONT_CART_BUYSTEP_COMPLETE
  414.         $event = new EventArgs(
  415.             [],
  416.             $request
  417.         );
  418.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_CART_BUYSTEP_COMPLETE);
  419.         if ($event->hasResponse()) {
  420.             return $event->getResponse();
  421.         }
  422.         return $this->redirectToRoute('shopping');
  423.     }
  424. }