app/Customize/Controller/TopController.php line 148

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 Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  14. use Eccube\Entity\BaseInfo;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Eccube\Controller\AbstractController;
  17. use Eccube\Entity\Product;
  18. use Eccube\Event\EccubeEvents;
  19. use Eccube\Event\EventArgs;
  20. use Eccube\Repository\ProductClassRepository;
  21. use Eccube\Entity\ProductClass;
  22. use Eccube\Entity\ProductStock;
  23. use Eccube\Entity\Master\ProductStatus;
  24. use Eccube\Form\Type\Front\CustomerLoginType;
  25. use Eccube\Repository\Master\ProductStatusRepository;
  26. use Eccube\Repository\Master\SaleTypeRepository;
  27. use Eccube\Repository\ProductRepository;
  28. use Eccube\Repository\BaseInfoRepository;
  29. use Eccube\Service\PurchaseFlow\PurchaseContext;
  30. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  31. use Symfony\Component\HttpFoundation\Request;
  32. use Symfony\Component\HttpFoundation\Response;
  33. use Symfony\Component\HttpFoundation\JsonResponse;
  34. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  35. use Customize\Service\OrderHelper;
  36. use Customize\Service\CartService;
  37. use Customize\Util\ApiUtil;
  38. class TopController extends AbstractController
  39. {
  40.     /**
  41.      * @var ProductStatusRepository
  42.      */
  43.     protected $productStatusRepository;
  44.     /**
  45.      * @var ProductClassRepository
  46.      */
  47.     protected $productClassRepository;
  48.     
  49.     /**
  50.      * @var ProductRepository
  51.      */
  52.     protected $productRepository;
  53.     /**
  54.      * @var DeliveryTimeRepository
  55.      */
  56.     protected $saleTypeRepository;
  57.     /**
  58.      * @var CartService
  59.      */
  60.     protected $cartService;
  61.     /**
  62.      * @var BaseInfo
  63.      */
  64.     protected $BaseInfo;
  65.     /**
  66.      * @var OrderHelper
  67.      */
  68.     protected $orderHelper;
  69.     /**
  70.      * ProductController constructor.
  71.      *
  72.      * @param ProductRepository $productRepository
  73.      * @param ProductStatusRepository $productStatusRepository
  74.      * @param ProductClassRepository $productClassRepository
  75.      * @param DeliveryTimeRepository $saleTypeRepository
  76.      * @param BaseInfoRepository $baseInfoRepository
  77.      * @param CartService $cartService
  78.      * @param PurchaseFlow $cartPurchaseFlow
  79.      * @param OrderHelper $orderHelper
  80.      */
  81.     public function __construct(
  82.         PurchaseFlow $cartPurchaseFlow,
  83.         ProductRepository $productRepository,
  84.         ProductClassRepository $productClassRepository,
  85.         ProductStatusRepository $productStatusRepository,
  86.         BaseInfoRepository $baseInfoRepository,
  87.         SaleTypeRepository $saleTypeRepository,
  88.         CartService $cartService,
  89.         OrderHelper $orderHelper
  90.     ) {
  91.         $this->purchaseFlow $cartPurchaseFlow;
  92.         $this->productClassRepository $productClassRepository;
  93.         $this->productRepository $productRepository;
  94.         $this->productStatusRepository $productStatusRepository;
  95.         $this->saleTypeRepository $saleTypeRepository;
  96.         $this->BaseInfo $baseInfoRepository->get();
  97.         $this->cartService $cartService;
  98.         $this->orderHelper $orderHelper;
  99.     }
  100.     /**
  101.      * @Route("/", name="homepage", methods={"GET", "POST"})
  102.      * @Template("index.twig")
  103.      */
  104.     public function index(Request $requestAuthenticationUtils $authenticationUtils)
  105.     {
  106.         if ($_SERVER['REQUEST_METHOD'] === 'GET') {
  107.             $this->cartService->clear();
  108.         } else {
  109.             if ($this->isGranted('IS_AUTHENTICATED_FULLY')) {
  110.                 $Cart $this->cartService->getCart();
  111.                 if ($Cart && $this->orderHelper->verifyCart($Cart)) {
  112.                     return $this->redirectToRoute('shopping');
  113.                 } else {
  114.                     return $this->redirectToRoute('homepage');
  115.                 }
  116.                 
  117.             }
  118.         }
  119.         
  120.         /* @var $form \Symfony\Component\Form\FormInterface */
  121.         $builder $this->formFactory->createNamedBuilder(''CustomerLoginType::class);
  122.         if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  123.             $Customer $this->getUser();
  124.             if ($Customer) {
  125.                 $builder->get('login_email')->setData($Customer->getEmail());
  126.             }
  127.         }
  128.         $event = new EventArgs(
  129.             [
  130.                 'builder' => $builder,
  131.             ],
  132.             $request
  133.         );
  134.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_SHOPPING_LOGIN_INITIALIZE$event);
  135.         $form $builder->getForm();
  136.         // カートチェック.
  137.         $Cart $this->cartService->getCart();
  138.         return [
  139.             'error' => $authenticationUtils->getLastAuthenticationError(),
  140.             'form' => $form->createView(),
  141.             'Cart' => $Cart,
  142.         ];
  143.     }
  144.     /**
  145.      * @Route("/", name="user_data", methods={"GET", "POST"})
  146.      * @Template("index.twig")
  147.      */
  148.     public function userData()
  149.     {
  150.         $Cart $this->cartService->getCart();
  151.         return [
  152.             'Cart' => $Cart,
  153.         ];
  154.     }
  155.     /**
  156.      * Move rank with ajax.
  157.      *
  158.      * @param Request     $request
  159.      *
  160.      * @throws \Exception
  161.      *
  162.      * @return JsonResponse
  163.      *
  164.      * @Route("/get_cart", name="get_cart")
  165.      */
  166.     public function get_cart()
  167.     {
  168.         $Cart $this->cartService->getCart();
  169.         $res = [];
  170.         if (null !== $Cart) {
  171.             foreach($Cart->getCartItems() as $CartItem) {
  172.                 $ProductClass $CartItem->getProductClass();
  173.                 $Product $ProductClass->getProduct();
  174.                 if ($Product->getId() > 8) {
  175.                     $item = array(
  176.                         'id' => $ProductClass->getId(),
  177.                         'name' => $Product->getName(),
  178.                         'rental' => round($ProductClass->getPrice01(), 0),
  179.                         'total' => round($ProductClass->getPrice02(), 0)
  180.                     );
  181.                     array_push($res$item);
  182.                 }
  183.             }
  184.         }
  185.         
  186.         return new JsonResponse($res);
  187.         
  188.     }
  189.     /**
  190.      * Move rank with ajax.
  191.      *
  192.      * @param Request     $request
  193.      *
  194.      * @throws \Exception
  195.      *
  196.      * @return Response
  197.      *
  198.      * @Route("/add_product", name="add_product")
  199.      */
  200.     public function add_product(Request $request)
  201.     {
  202.         if ($request->isXmlHttpRequest() && $request->getMethod() == 'POST') {
  203.             $product_id $request->get('product_id');
  204.             $index $request->get('index');
  205.             $from $request->get('from');
  206.             $from_date date('y/m/d'$from 1000);
  207.             $to $request->get('to');
  208.             $to_date date('y/m/d'$to 1000);
  209.             $country_name_jp $request->get('country');
  210.             $country_code $request->get('country_code');
  211.             $des_price $request->get('price');
  212.             $days round(($to $from) / (1000 60 60 24));
  213.             
  214.             if (!empty($product_id) && !is_null($product_id)) {
  215.                 //update Product
  216.                 $Product $this->productRepository->findWithSortedClassCategories($product_id);
  217.                 $Product->setName($country_name_jp.'_'.$index.'_'.$from_date.'_'.$to_date.'_'.$country_code);
  218.                 $Product->setRentalStart(new \DateTime('20'.$from_date));
  219.                 $Product->setRentalEnd(new \DateTime('20'.$to_date));
  220.                 $this->entityManager->persist($Product);
  221.                 $this->entityManager->flush();
  222.                 $ProductClass null;
  223.                 $ProductStock null;
  224.                 if (!$Product) {
  225.                     throw new NotFoundHttpException();
  226.                 }
  227.                 // 規格無しの商品の場合は、デフォルト規格を表示用に取得する
  228.                 $has_class $Product->hasProductClass();
  229.                 if (!$has_class) {
  230.                     $ProductClasses $Product->getProductClasses();
  231.                     foreach ($ProductClasses as $pc) {
  232.                         if (!is_null($pc->getClassCategory1())) {
  233.                             continue;
  234.                         }
  235.                         if ($pc->isVisible()) {
  236.                             $ProductClass $pc;
  237.                             break;
  238.                         }
  239.                     }
  240.                     if ($this->BaseInfo->isOptionProductTaxRule() && $ProductClass->getTaxRule()) {
  241.                         $ProductClass->setTaxRate($ProductClass->getTaxRule()->getTaxRate());
  242.                     }
  243.                     $ProductStock $ProductClass->getProductStock();
  244.                 }
  245.                 $ProductClass
  246.                     ->setPrice01($des_price)
  247.                     ->setPrice02($des_price)
  248.                     ->setProduct($Product);
  249.                 $this->entityManager->persist($ProductClass);
  250.                 $this->entityManager->flush();
  251.                 $ProductStock->setProductClass($ProductClass);
  252.                 $this->entityManager->persist($ProductStock);
  253.                 $this->entityManager->flush();
  254.             } else {
  255.                 //register Product
  256.                 $Product = new Product();
  257.                 $Product->setStatus($this->productStatusRepository->find(ProductStatus::DISPLAY_SHOW));
  258.                 $Product->setName($country_name_jp.'_'.$index.'_'.$from_date.'_'.$to_date.'_'.$country_code);
  259.                 $Product->setRentalStart(new \DateTime('20'.$from_date));
  260.                 $Product->setRentalEnd(new \DateTime('20'.$to_date));
  261.                 $this->entityManager->persist($Product);
  262.                 $this->entityManager->flush();
  263.                 //register ProductStock
  264.                 $ProductStock = new ProductStock();
  265.                 // // 在庫無制限時はnullを設定
  266.                 $ProductStock->setStock(null);
  267.                 $this->entityManager->persist($ProductStock);
  268.                 $this->entityManager->flush();
  269.                 //register ProductClass
  270.                 $ProductClass = new ProductClass();
  271.                 // $ProductStatus = $this->productStatusRepository->find(ProductStatus::DISPLAY_HIDE);
  272.                 $SaleType $this->saleTypeRepository->findOneBy([], ['sort_no' => 'ASC']);
  273.                 $ProductClass
  274.                     ->setVisible(true)
  275.                     ->setStockUnlimited(true)
  276.                     ->setSaleType($SaleType)
  277.                     ->setPrice01($des_price)
  278.                     ->setPrice02($des_price)
  279.                     ->setProduct($Product);
  280.                 $ProductClass->setProductStock($ProductStock);
  281.                 $this->entityManager->persist($ProductClass);
  282.                 $this->entityManager->flush();
  283.                 $ProductStock->setProductClass($ProductClass);
  284.                 $this->entityManager->persist($ProductStock);
  285.                 $this->entityManager->flush();
  286.                 // log_info('商品登録完了', [$id]);
  287.                 //add Product into Cart
  288.                 if (!$ProductClass instanceof ProductClass) {
  289.                     $ProductClassId $ProductClass;
  290.                     $ProductClass $this->entityManager
  291.                         ->getRepository(ProductClass::class)
  292.                         ->find($ProductClassId);
  293.                     if (is_null($ProductClass)) {
  294.                         return new Response('fail');
  295.                     }
  296.                 }
  297.                 $quantity 1;
  298.                 // カートへ追加
  299.                 $this->cartService->addProduct($ProductClass$quantity);
  300.                 // レンタル料
  301.                 $RetalProduct $this->productRepository->findWithSortedClassCategories(env('RENTAL_PRODUCT_ID'));
  302.                 $RetalProductClass $RetalProduct->getProductClasses()[0];
  303.                 $this->cartService->addProduct($RetalProductClass$days);
  304.             }
  305.             
  306.             // 明細の正規化
  307.             $Carts $this->cartService->getCarts();
  308.             foreach ($Carts as $Cart) {
  309.                 $result $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart$this->getUser()));
  310.                 // 復旧不可のエラーが発生した場合は追加した明細を削除.
  311.                 if ($result->hasError()) {
  312.                     $this->cartService->removeProduct($ProductClassId);
  313.                     foreach ($result->getErrors() as $error) {
  314.                         $errorMessages[] = $error->getMessage();
  315.                     }
  316.                 }
  317.                 foreach ($result->getWarning() as $warning) {
  318.                     $errorMessages[] = $warning->getMessage();
  319.                 }
  320.             }
  321.             $this->cartService->save();
  322.             return new Response($ProductClass->getId());
  323.         }
  324.         
  325.         return new Response('fail');
  326.     }
  327.     /**
  328.      * Move rank with ajax.
  329.      *
  330.      * @param Request     $request
  331.      *
  332.      * @throws \Exception
  333.      *
  334.      * @return JsonResponse
  335.      *
  336.      * @Route("/add_products", name="add_products")
  337.      */
  338.     public function add_products(Request $request)
  339.     {
  340.         $res 'fail';
  341.         if ($request->isXmlHttpRequest() && $request->getMethod() == 'POST') {
  342.             $cart_data $request->get('cart_data');
  343.             $lpCartId $request->get('lp_cart_id');
  344.             // Check cart reference from LP
  345.             log_info('[ADD PRODUCTS START] Check cart reference from LP.');
  346.             if (!empty($lpCartId)) {
  347.                 // remote cart data in eccube
  348.                 $this->cartService->clear();
  349.             }
  350.             log_info('[ADD PRODUCTS END] Check cart reference from LP.');
  351.             // Add product
  352.             if (!empty($cart_data)) {
  353.                 log_info('[ADD PRODUCTS] Add main product.');
  354.                 $json json_decode($cart_data);
  355.                 foreach ($json as $item) {
  356.                     $traffic_limit '1g';
  357.                     $traffic_unlimit '5g';
  358.                     $traffic $item->picked_plan === $traffic_unlimit strtoupper($traffic_unlimit) : strtoupper($traffic_limit);
  359.                     $rentalFromDate = new \DateTime($item->from_date);
  360.                     $rentalToDate = new \DateTime($item->to_date);
  361.                     $rentalDays $item->date_count;
  362.                     $communicationFee $item->country_price $rentalDays;
  363.                     $rentalFee env('RENTAL_FEE_PER_DAY'220) * $rentalDays;
  364.                     $productName $item->country_name.'_'.substr($item->from_date2).'_'.substr($item->to_date2).'_'.$item->country_code.'_'.$traffic;
  365.                     if (!empty($item->productclass_id)) {
  366.                         //update Product
  367.                         $ProductClass $this->productClassRepository->find($item->productclass_id);
  368.                         $ProductStock $ProductClass->getProductStock();
  369.                         $Product $ProductClass->getProduct();
  370.                         $Product->setName($productName);
  371.                         $Product->setRentalStart($rentalFromDate);
  372.                         $Product->setRentalEnd($rentalToDate);
  373.                         $this->entityManager->persist($Product);
  374.                         $ProductClass
  375.                             ->setPrice01($communicationFee)
  376.                             ->setPrice02($communicationFee $rentalFee)
  377.                             ->setProduct($Product);
  378.                         $this->entityManager->persist($ProductClass);
  379.                         $ProductStock->setProductClass($ProductClass);
  380.                         $this->entityManager->persist($ProductStock);
  381.                         $this->cartService->removeProduct($ProductClass);
  382.                         $Carts $this->cartService->getCarts();
  383.                         foreach ($Carts as $Cart) {
  384.                             $result $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart$this->getUser()));
  385.                         }
  386.                         $this->cartService->save();
  387.                         $this->entityManager->flush();
  388.                     } else {
  389.                         //register Product
  390.                         $Product = new Product();
  391.                         $Product->setStatus($this->productStatusRepository->find(ProductStatus::DISPLAY_SHOW));
  392.                         $Product->setName($productName);
  393.                         $Product->setRentalStart($rentalFromDate);
  394.                         $Product->setRentalEnd($rentalToDate);
  395.                         // 商品オプションと区別するためのフラグを設定
  396.                         $Product->setBasic(true);
  397.                         
  398.                         $this->entityManager->persist($Product);
  399.                         $this->entityManager->flush();
  400.                         //register ProductStock
  401.                         $ProductStock = new ProductStock();
  402.                         // // 在庫無制限時はnullを設定
  403.                         $ProductStock->setStock(null);
  404.                         $this->entityManager->persist($ProductStock);
  405.                         $this->entityManager->flush();
  406.                         //register ProductClass
  407.                         $ProductClass = new ProductClass();
  408.                         // $ProductStatus = $this->productStatusRepository->find(ProductStatus::DISPLAY_HIDE);
  409.                         $SaleType $this->saleTypeRepository->findOneBy([], ['sort_no' => 'ASC']);
  410.                         $ProductClass
  411.                             ->setVisible(true)
  412.                             ->setStockUnlimited(true)
  413.                             ->setSaleType($SaleType)
  414.                             ->setPrice01($communicationFee)
  415.                             ->setPrice02($communicationFee $rentalFee)
  416.                             ->setProduct($Product);
  417.                         $ProductClass->setProductStock($ProductStock);
  418.                         $this->entityManager->persist($ProductClass);
  419.                         $this->entityManager->flush();
  420.                         $ProductStock->setProductClass($ProductClass);
  421.                         $this->entityManager->persist($ProductStock);
  422.                         $this->entityManager->flush();
  423.                         // log_info('商品登録完了', [$id]);
  424.                         //add Product into Cart
  425.                         if (!$ProductClass instanceof ProductClass) {
  426.                             $ProductClassId $ProductClass;
  427.                             $ProductClass $this->entityManager
  428.                                 ->getRepository(ProductClass::class)
  429.                                 ->find($ProductClassId);
  430.                             if (is_null($ProductClass)) {
  431.                                 return new JsonResponse($res);
  432.                             }
  433.                         }
  434.                     }
  435.                     $quantity 1;
  436.                     // カートへ追加
  437.                     $this->cartService->addProduct($ProductClass1);
  438.                     $Carts $this->cartService->getCarts();
  439.                     foreach ($Carts as $Cart) {
  440.                         $result $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart$this->getUser()));
  441.                     }
  442.                     $this->cartService->save();
  443.                 }
  444.                 log_info('[ADD PRODUCTS END] Add main product.');
  445.             }
  446.         
  447.             // Add option product
  448.             log_info('[ADD PRODUCTS START] Add option product.');
  449.             $security_option $request->get('security_option');
  450.             $option_product_1 $request->get('option_product_1');
  451.             $option_product_2 $request->get('option_product_2');
  452.             $pre_order_id $this->cartService->getPreOrderId();
  453.             $SecurityProductId 3;
  454.             switch ($security_option) {
  455.                 case '1':
  456.                     $SecurityProductId 3;
  457.                     break;
  458.                 case '2':
  459.                     $SecurityProductId 4;
  460.                     break;
  461.                 case '3':
  462.                     $SecurityProductId 5;
  463.                     break;
  464.                 default:
  465.                     break;
  466.             }
  467.             $SecurityProduct $this->productRepository->findWithSortedClassCategories($SecurityProductId);
  468.             $SecurityProductClass null;
  469.             // 規格無しの商品の場合は、デフォルト規格を表示用に取得する
  470.             $has_class $SecurityProduct->hasProductClass();
  471.             if (!$has_class) {
  472.                 $SecurityProductClasses $SecurityProduct->getProductClasses();
  473.                 foreach ($SecurityProductClasses as $pc) {
  474.                     if (!is_null($pc->getClassCategory1())) {
  475.                         continue;
  476.                     }
  477.                     if ($pc->isVisible()) {
  478.                         $SecurityProductClass $pc;
  479.                         break;
  480.                     }
  481.                 }
  482.                 if ($this->BaseInfo->isOptionProductTaxRule() && $SecurityProductClass->getTaxRule()) {
  483.                     $SecurityProductClass->setTaxRate($SecurityProductClass->getTaxRule()->getTaxRate());
  484.                 }
  485.             }
  486.             $Cart $this->cartService->getCart();
  487.             if ($pre_order_id != null) {
  488.                 $Cart->setPreOrderId($pre_order_id);
  489.             }
  490.             
  491.             $total_days 0;
  492.             $dates = [];
  493.             foreach ($Cart->getCartItems() as $CartItem) {
  494.                 $ProductClass $CartItem->getProductClass();
  495.                 $Product $ProductClass->getProduct();
  496.                 $str_name $Product->getName();
  497.                 if (str_contains($str_name'_')) {
  498.                     $contents explode("_"$str_name);
  499.                     $from $contents[1];
  500.                     $to $contents[2];
  501.                     array_push($dates, new \DateTime('20'.$from), new \DateTime('20'.$to));
  502.                 } else {
  503.                     $this->cartService->removeProduct($ProductClass);
  504.                 }
  505.             }
  506.             if(sizeof($dates)>0) {
  507.                 $max_date max($dates);
  508.                 $min_date min($dates);
  509.                 $diff $max_date->diff($min_date);
  510.                 $total_days $diff->days 1;
  511.             }
  512.             $quantity strval($total_days);
  513.             // レンタル料
  514.             $RetalProduct $this->productRepository->findWithSortedClassCategories(env('RENTAL_PRODUCT_ID'));
  515.             $RetalProductClass $RetalProduct->getProductClasses()[0];
  516.             $this->cartService->addProduct($RetalProductClass$quantity);
  517.             $Carts $this->cartService->getCarts();
  518.             foreach ($Carts as $Cart) {
  519.                 $result $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart$this->getUser()));
  520.             }
  521.             $this->cartService->save();
  522.             if (!$SecurityProductClass instanceof ProductClass) {
  523.                 $SecurityProductClassId $SecurityProductClass;
  524.                 $SecurityProductClass $this->entityManager
  525.                     ->getRepository(ProductClass::class)
  526.                     ->find($SecurityProductClassId);
  527.                 if (is_null($SecurityProductClass)) {
  528.                     return new Response('fail');
  529.                 }
  530.             }
  531.             if ($pre_order_id != null) {
  532.                 $this->cartService->setPreOrderId($pre_order_id);
  533.             }
  534.             // カートへ追加
  535.             $this->cartService->addProduct($SecurityProductClass$quantity);
  536.             // デポジット
  537.             $DepositProductId 8;
  538.             $DepositProduct $this->productRepository->findWithSortedClassCategories($DepositProductId);
  539.             $DepositProductClass null;
  540.             // 規格無しの商品の場合は、デフォルト規格を表示用に取得する
  541.             $has_class $DepositProduct->hasProductClass();
  542.             if (!$has_class) {
  543.                 $DepositProductClass $DepositProduct->getProductClasses();
  544.                 foreach ($DepositProductClass as $pc) {
  545.                     if (!is_null($pc->getClassCategory1())) {
  546.                         continue;
  547.                     }
  548.                     if ($pc->isVisible()) {
  549.                         $DepositProductClass $pc;
  550.                         break;
  551.                     }
  552.                 }
  553.             }
  554.             if (!$DepositProductClass instanceof ProductClass) {
  555.                 $DepositProductClassId $DepositProductClass;
  556.                 $DepositProductClass $this->entityManager
  557.                     ->getRepository(ProductClass::class)
  558.                     ->find($DepositProductClassId);
  559.                 if (is_null($DepositProductClass)) {
  560.                     return new Response('fail');
  561.                 }
  562.             }
  563.             // カートへ追加
  564.             $this->cartService->addProduct($DepositProductClass1);
  565.             
  566.             // 明細の正規化
  567.             $Carts $this->cartService->getCarts();
  568.             foreach ($Carts as $Cart) {
  569.                 $result $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart$this->getUser()));
  570.                 // 復旧不可のエラーが発生した場合は追加した明細を削除.
  571.                 if ($result->hasError()) {
  572.                     $this->cartService->removeProduct($ProductClassId);
  573.                     foreach ($result->getErrors() as $error) {
  574.                         $errorMessages[] = $error->getMessage();
  575.                     }
  576.                 }
  577.                 foreach ($result->getWarning() as $warning) {
  578.                     $errorMessages[] = $warning->getMessage();
  579.                 }
  580.             }
  581.             
  582.             
  583.             $this->cartService->save();
  584.             if ($option_product_1 == '1') {
  585.                 $OptionProduct1Id 6;
  586.                 $OptionProduct1 $this->productRepository->findWithSortedClassCategories($OptionProduct1Id);
  587.                 $OptionProduct1Class null;
  588.                 // 規格無しの商品の場合は、デフォルト規格を表示用に取得する
  589.                 $has_class $SecurityProduct->hasProductClass();
  590.                 if (!$has_class) {
  591.                     $OptionProduct1Classes $OptionProduct1->getProductClasses();
  592.                     foreach ($OptionProduct1Classes as $pc) {
  593.                         if (!is_null($pc->getClassCategory1())) {
  594.                             continue;
  595.                         }
  596.                         if ($pc->isVisible()) {
  597.                             $OptionProduct1Class $pc;
  598.                             break;
  599.                         }
  600.                     }
  601.                     if ($this->BaseInfo->isOptionProductTaxRule() && $OptionProduct1Class->getTaxRule()) {
  602.                         $OptionProduct1Class->setTaxRate($OptionProduct1Class->getTaxRule()->getTaxRate());
  603.                     }
  604.                 }
  605.                 if (!$OptionProduct1Class instanceof ProductClass) {
  606.                     $OptionProduct1ClassId $OptionProduct1Class;
  607.                     $OptionProduct1Class $this->entityManager
  608.                         ->getRepository(ProductClass::class)
  609.                         ->find($OptionProduct1ClassId);
  610.                     if (is_null($OptionProduct1Class)) {
  611.                         return new Response('fail');
  612.                     }
  613.                 }
  614.                 // カートへ追加
  615.                 $this->cartService->addProduct($OptionProduct1Class$quantity);
  616.                 // 明細の正規化
  617.                 $Carts $this->cartService->getCarts();
  618.                 foreach ($Carts as $Cart) {
  619.                     $result $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart$this->getUser()));
  620.                     // 復旧不可のエラーが発生した場合は追加した明細を削除.
  621.                     if ($result->hasError()) {
  622.                         $this->cartService->removeProduct($ProductClassId);
  623.                         foreach ($result->getErrors() as $error) {
  624.                             $errorMessages[] = $error->getMessage();
  625.                         }
  626.                     }
  627.                     foreach ($result->getWarning() as $warning) {
  628.                         $errorMessages[] = $warning->getMessage();
  629.                     }
  630.                 }
  631.                 if ($pre_order_id != null) {
  632.                     $this->cartService->setPreOrderId($pre_order_id);
  633.                 }
  634.                 $this->cartService->save();
  635.             }            
  636.             if ($option_product_2 == '1') {
  637.                 $OptionProduct2Id 7;
  638.                 $OptionProduct2 $this->productRepository->findWithSortedClassCategories($OptionProduct2Id);
  639.                 $OptionProduct2Class null;
  640.                 // 規格無しの商品の場合は、デフォルト規格を表示用に取得する
  641.                 $has_class $SecurityProduct->hasProductClass();
  642.                 if (!$has_class) {
  643.                     $OptionProduct2Classes $OptionProduct2->getProductClasses();
  644.                     foreach ($OptionProduct2Classes as $pc) {
  645.                         if (!is_null($pc->getClassCategory1())) {
  646.                             continue;
  647.                         }
  648.                         if ($pc->isVisible()) {
  649.                             $OptionProduct2Class $pc;
  650.                             break;
  651.                         }
  652.                     }
  653.                     if ($this->BaseInfo->isOptionProductTaxRule() && $OptionProduct2Class->getTaxRule()) {
  654.                         $OptionProduct2Class->setTaxRate($OptionProduct2Class->getTaxRule()->getTaxRate());
  655.                     }
  656.                 }
  657.                 if (!$OptionProduct2Class instanceof ProductClass) {
  658.                     $OptionProduct2ClassId $OptionProduct2Class;
  659.                     $OptionProduct2Class $this->entityManager
  660.                         ->getRepository(ProductClass::class)
  661.                         ->find($OptionProduct2ClassId);
  662.                     if (is_null($OptionProduct2Class)) {
  663.                         return new Response('fail');
  664.                     }
  665.                 }
  666.                 // カートへ追加
  667.                 $this->cartService->addProduct($OptionProduct2Class'1');
  668.                 // 明細の正規化
  669.                 $Carts $this->cartService->getCarts();
  670.                 foreach ($Carts as $Cart) {
  671.                     $result $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart$this->getUser()));
  672.                     // 復旧不可のエラーが発生した場合は追加した明細を削除.
  673.                     if ($result->hasError()) {
  674.                         $this->cartService->removeProduct($ProductClassId);
  675.                         foreach ($result->getErrors() as $error) {
  676.                             $errorMessages[] = $error->getMessage();
  677.                         }
  678.                     }
  679.                     foreach ($result->getWarning() as $warning) {
  680.                         $errorMessages[] = $warning->getMessage();
  681.                     }
  682.                 }
  683.                 if ($pre_order_id != null) {
  684.                     $this->cartService->setPreOrderId($pre_order_id);
  685.                 }
  686.                 $this->cartService->save();
  687.                 log_info('[ADD PRODUCTS END] Add option product.');
  688.             }    
  689.             $res 'success';
  690.             return new JsonResponse($res);
  691.         }
  692.         
  693.         return new JsonResponse($res);
  694.     }
  695.     /**
  696.      * Move rank with ajax.
  697.      *
  698.      * @param Request     $request
  699.      *
  700.      * @throws \Exception
  701.      *
  702.      * @return Response
  703.      *
  704.      * @Route("/delete_cartitem", name="delete_cartitem")
  705.      */
  706.     public function delete_cartitem(Request $request)
  707.     {
  708.        if ($request->isXmlHttpRequest() && $request->getMethod() == 'POST') {
  709.             // $cartitem_id = $request->get('cartitem_id');
  710.             // $product_id = $request->get('product_id');
  711.             $productclass_id $request->get('productclass_id');
  712.             if (!empty($productclass_id) && !is_null($productclass_id)) {
  713.                 /** @var ProductClass $ProductClass */
  714.                 $ProductClass $this->productClassRepository->find($productclass_id);
  715.                 $this->cartService->removeProduct($ProductClass);
  716.                 // カートを取得して明細の正規化を実行
  717.                 $Carts $this->cartService->getCarts();
  718.                 $this->execPurchaseFlow($Carts);
  719.                 log_info('カート演算処理終了', ['operation' => 'remove''product_class_id' => $productclass_id]);
  720.                 return new Response("success");
  721.             } else {
  722.                 return new Response('fail');
  723.             }
  724.        }
  725.         
  726.         return new Response('fail');
  727.     }
  728.     /**
  729.      * Move rank with ajax.
  730.      *
  731.      * @param Request     $request
  732.      *
  733.      * @throws \Exception
  734.      *
  735.      * @return Response
  736.      *
  737.      * @Route("/delete_all_cartitem", name="delete_all_cartitem")
  738.      */
  739.     public function delete_all_cartitem(Request $request)
  740.     {
  741.         if ($request->isXmlHttpRequest() && $request->getMethod() == 'GET') {
  742.             $this->cartService->clear();
  743.             return new Response("success");
  744.         }
  745.         
  746.         return new Response('fail');
  747.     }
  748.     /**
  749.      * @param $Carts
  750.      *
  751.      * @return \Symfony\Component\HttpFoundation\RedirectResponse|null
  752.      */
  753.     protected function execPurchaseFlow($Carts)
  754.     {
  755.         /** @var PurchaseFlowResult[] $flowResults */
  756.         $flowResults array_map(function ($Cart) {
  757.             $purchaseContext = new PurchaseContext($Cart$this->getUser());
  758.             return $this->purchaseFlow->validate($Cart$purchaseContext);
  759.         }, $Carts);
  760.         // 復旧不可のエラーが発生した場合はカートをクリアして再描画
  761.         $hasError false;
  762.         foreach ($flowResults as $result) {
  763.             if ($result->hasError()) {
  764.                 $hasError true;
  765.                 foreach ($result->getErrors() as $error) {
  766.                     $this->addRequestError($error->getMessage());
  767.                 }
  768.             }
  769.         }
  770.         if ($hasError) {
  771.             $this->cartService->clear();
  772.             return $this->redirectToRoute('cart');
  773.         }
  774.         $this->cartService->save();
  775.         foreach ($flowResults as $index => $result) {
  776.             foreach ($result->getWarning() as $warning) {
  777.                 if ($Carts[$index]->getItems()->count() > 0) {
  778.                     $cart_key $Carts[$index]->getCartKey();
  779.                     $this->addRequestError($warning->getMessage(), "front.cart.${cart_key}");
  780.                 } else {
  781.                     // キーが存在しない場合はグローバルにエラーを表示する
  782.                     $this->addRequestError($warning->getMessage());
  783.                 }
  784.             }
  785.         }
  786.         return null;
  787.     }
  788.     /**
  789.      * Move rank with ajax.
  790.      *
  791.      * @param Request     $request
  792.      *
  793.      * @throws \Exception
  794.      *
  795.      * @return Response
  796.      *
  797.      * @Route("/add_option_product", name="add_option_product")
  798.      */
  799.     public function add_option_product(Request $request)
  800.     {
  801.         $pre_order_id $this->cartService->getPreOrderId();
  802.         
  803.         if ($request->isXmlHttpRequest() && $request->getMethod() == 'POST') {
  804.             $security_option $request->get('security_option');
  805.             $option_product_1 $request->get('option_product_1');
  806.             $option_product_2 $request->get('option_product_2');
  807.             $pre_order_id $request->get('pre_order_id');
  808.             $SecurityProductId 3;
  809.             switch ($security_option) {
  810.                 case '1':
  811.                     $SecurityProductId 3;
  812.                     break;
  813.                 case '2':
  814.                     $SecurityProductId 4;
  815.                     break;
  816.                 case '3':
  817.                     $SecurityProductId 5;
  818.                     break;
  819.                 default:
  820.                     break;
  821.             }
  822.             $SecurityProduct $this->productRepository->findWithSortedClassCategories($SecurityProductId);
  823.             $SecurityProductClass null;
  824.             // 規格無しの商品の場合は、デフォルト規格を表示用に取得する
  825.             $has_class $SecurityProduct->hasProductClass();
  826.             if (!$has_class) {
  827.                 $SecurityProductClasses $SecurityProduct->getProductClasses();
  828.                 foreach ($SecurityProductClasses as $pc) {
  829.                     if (!is_null($pc->getClassCategory1())) {
  830.                         continue;
  831.                     }
  832.                     if ($pc->isVisible()) {
  833.                         $SecurityProductClass $pc;
  834.                         break;
  835.                     }
  836.                 }
  837.                 if ($this->BaseInfo->isOptionProductTaxRule() && $SecurityProductClass->getTaxRule()) {
  838.                     $SecurityProductClass->setTaxRate($SecurityProductClass->getTaxRule()->getTaxRate());
  839.                 }
  840.             }
  841.             $Cart $this->cartService->getCart();
  842.             if ($pre_order_id != null) {
  843.                 $Cart->setPreOrderId($pre_order_id);
  844.             }
  845.             
  846.             $total_days 0;
  847.             $dates = [];
  848.             foreach ($Cart->getCartItems() as $CartItem) {
  849.                 $ProductClass $CartItem->getProductClass();
  850.                 $Product $ProductClass->getProduct();
  851.                 $str_name $Product->getName();
  852.                 if (str_contains($str_name'_')) {
  853.                     $contents explode("_"$str_name);
  854.                     $from $contents[1];
  855.                     $to $contents[2];
  856.                     array_push($dates, new \DateTime('20'.$from), new \DateTime('20'.$to));
  857.                     // $days = floor((strtotime('20'.$to) - strtotime('20'.$from)) / (60 * 60 * 24)) + 1;
  858.                 } else {
  859.                     $this->cartService->removeProduct($ProductClass);
  860.                 }
  861.             }
  862.             // $this->cartService->save();
  863.             if(sizeof($dates)>0) {
  864.                 $max_date max($dates);
  865.                 $min_date min($dates);
  866.                 $diff $max_date->diff($min_date);
  867.                 $total_days $diff->days 1;
  868.             }
  869.             $quantity strval($total_days);
  870.             // レンタル料
  871.             $RetalProduct $this->productRepository->findWithSortedClassCategories(env('RENTAL_PRODUCT_ID'));
  872.             $RetalProductClass $RetalProduct->getProductClasses()[0];
  873.             $this->cartService->addProduct($RetalProductClass$quantity);
  874.             $Carts $this->cartService->getCarts();
  875.             foreach ($Carts as $Cart) {
  876.                 $result $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart$this->getUser()));
  877.             }
  878.             $this->cartService->save();
  879.             if (!$SecurityProductClass instanceof ProductClass) {
  880.                 $SecurityProductClassId $SecurityProductClass;
  881.                 $SecurityProductClass $this->entityManager
  882.                     ->getRepository(ProductClass::class)
  883.                     ->find($SecurityProductClassId);
  884.                 if (is_null($SecurityProductClass)) {
  885.                     return new Response('fail');
  886.                 }
  887.             }
  888.             if ($pre_order_id != null) {
  889.                 $this->cartService->setPreOrderId($pre_order_id);
  890.             }
  891.             // カートへ追加
  892.             $this->cartService->addProduct($SecurityProductClass$quantity);
  893.             // デポジット
  894.             $DepositProductId 8;
  895.             $DepositProduct $this->productRepository->findWithSortedClassCategories($DepositProductId);
  896.             $DepositProductClass null;
  897.             // 規格無しの商品の場合は、デフォルト規格を表示用に取得する
  898.             $has_class $DepositProduct->hasProductClass();
  899.             if (!$has_class) {
  900.                 $DepositProductClass $DepositProduct->getProductClasses();
  901.                 foreach ($DepositProductClass as $pc) {
  902.                     if (!is_null($pc->getClassCategory1())) {
  903.                         continue;
  904.                     }
  905.                     if ($pc->isVisible()) {
  906.                         $DepositProductClass $pc;
  907.                         break;
  908.                     }
  909.                 }
  910.             }
  911.             if (!$DepositProductClass instanceof ProductClass) {
  912.                 $DepositProductClassId $DepositProductClass;
  913.                 $DepositProductClass $this->entityManager
  914.                     ->getRepository(ProductClass::class)
  915.                     ->find($DepositProductClassId);
  916.                 if (is_null($DepositProductClass)) {
  917.                     return new Response('fail');
  918.                 }
  919.             }
  920.             // カートへ追加
  921.             $this->cartService->addProduct($DepositProductClass1);
  922.             
  923.             // 明細の正規化
  924.             $Carts $this->cartService->getCarts();
  925.             foreach ($Carts as $Cart) {
  926.                 $result $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart$this->getUser()));
  927.                 // 復旧不可のエラーが発生した場合は追加した明細を削除.
  928.                 if ($result->hasError()) {
  929.                     $this->cartService->removeProduct($ProductClassId);
  930.                     foreach ($result->getErrors() as $error) {
  931.                         $errorMessages[] = $error->getMessage();
  932.                     }
  933.                 }
  934.                 foreach ($result->getWarning() as $warning) {
  935.                     $errorMessages[] = $warning->getMessage();
  936.                 }
  937.             }
  938.             
  939.             
  940.             $this->cartService->save();
  941.             if ($option_product_1 == '1') {
  942.                 $OptionProduct1Id 6;
  943.                 $OptionProduct1 $this->productRepository->findWithSortedClassCategories($OptionProduct1Id);
  944.                 $OptionProduct1Class null;
  945.                 // 規格無しの商品の場合は、デフォルト規格を表示用に取得する
  946.                 $has_class $SecurityProduct->hasProductClass();
  947.                 if (!$has_class) {
  948.                     $OptionProduct1Classes $OptionProduct1->getProductClasses();
  949.                     foreach ($OptionProduct1Classes as $pc) {
  950.                         if (!is_null($pc->getClassCategory1())) {
  951.                             continue;
  952.                         }
  953.                         if ($pc->isVisible()) {
  954.                             $OptionProduct1Class $pc;
  955.                             break;
  956.                         }
  957.                     }
  958.                     if ($this->BaseInfo->isOptionProductTaxRule() && $OptionProduct1Class->getTaxRule()) {
  959.                         $OptionProduct1Class->setTaxRate($OptionProduct1Class->getTaxRule()->getTaxRate());
  960.                     }
  961.                 }
  962.                 if (!$OptionProduct1Class instanceof ProductClass) {
  963.                     $OptionProduct1ClassId $OptionProduct1Class;
  964.                     $OptionProduct1Class $this->entityManager
  965.                         ->getRepository(ProductClass::class)
  966.                         ->find($OptionProduct1ClassId);
  967.                     if (is_null($OptionProduct1Class)) {
  968.                         return new Response('fail');
  969.                     }
  970.                 }
  971.                 // カートへ追加
  972.                 $this->cartService->addProduct($OptionProduct1Class$quantity);
  973.                 // 明細の正規化
  974.                 $Carts $this->cartService->getCarts();
  975.                 foreach ($Carts as $Cart) {
  976.                     $result $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart$this->getUser()));
  977.                     // 復旧不可のエラーが発生した場合は追加した明細を削除.
  978.                     if ($result->hasError()) {
  979.                         $this->cartService->removeProduct($ProductClassId);
  980.                         foreach ($result->getErrors() as $error) {
  981.                             $errorMessages[] = $error->getMessage();
  982.                         }
  983.                     }
  984.                     foreach ($result->getWarning() as $warning) {
  985.                         $errorMessages[] = $warning->getMessage();
  986.                     }
  987.                 }
  988.                 if ($pre_order_id != null) {
  989.                     $this->cartService->setPreOrderId($pre_order_id);
  990.                 }
  991.                 $this->cartService->save();
  992.             }            
  993.             if ($option_product_2 == '1') {
  994.                 $OptionProduct2Id 7;
  995.                 $OptionProduct2 $this->productRepository->findWithSortedClassCategories($OptionProduct2Id);
  996.                 $OptionProduct2Class null;
  997.                 // 規格無しの商品の場合は、デフォルト規格を表示用に取得する
  998.                 $has_class $SecurityProduct->hasProductClass();
  999.                 if (!$has_class) {
  1000.                     $OptionProduct2Classes $OptionProduct2->getProductClasses();
  1001.                     foreach ($OptionProduct2Classes as $pc) {
  1002.                         if (!is_null($pc->getClassCategory1())) {
  1003.                             continue;
  1004.                         }
  1005.                         if ($pc->isVisible()) {
  1006.                             $OptionProduct2Class $pc;
  1007.                             break;
  1008.                         }
  1009.                     }
  1010.                     if ($this->BaseInfo->isOptionProductTaxRule() && $OptionProduct2Class->getTaxRule()) {
  1011.                         $OptionProduct2Class->setTaxRate($OptionProduct2Class->getTaxRule()->getTaxRate());
  1012.                     }
  1013.                 }
  1014.                 if (!$OptionProduct2Class instanceof ProductClass) {
  1015.                     $OptionProduct2ClassId $OptionProduct2Class;
  1016.                     $OptionProduct2Class $this->entityManager
  1017.                         ->getRepository(ProductClass::class)
  1018.                         ->find($OptionProduct2ClassId);
  1019.                     if (is_null($OptionProduct2Class)) {
  1020.                         return new Response('fail');
  1021.                     }
  1022.                 }
  1023.                 // カートへ追加
  1024.                 $this->cartService->addProduct($OptionProduct2Class'1');
  1025.                 // 明細の正規化
  1026.                 $Carts $this->cartService->getCarts();
  1027.                 foreach ($Carts as $Cart) {
  1028.                     $result $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart$this->getUser()));
  1029.                     // 復旧不可のエラーが発生した場合は追加した明細を削除.
  1030.                     if ($result->hasError()) {
  1031.                         $this->cartService->removeProduct($ProductClassId);
  1032.                         foreach ($result->getErrors() as $error) {
  1033.                             $errorMessages[] = $error->getMessage();
  1034.                         }
  1035.                     }
  1036.                     foreach ($result->getWarning() as $warning) {
  1037.                         $errorMessages[] = $warning->getMessage();
  1038.                     }
  1039.                 }
  1040.                 if ($pre_order_id != null) {
  1041.                     $this->cartService->setPreOrderId($pre_order_id);
  1042.                 }
  1043.                 $this->cartService->save();
  1044.             }    
  1045.             return new Response('success');
  1046.         }
  1047.         return new Response('fail');
  1048.     }
  1049.     /**
  1050.      * [countries description]
  1051.      * @Route("/countries", name="get_countries")
  1052.      * @return [type] [description]
  1053.      */
  1054.     public function countries(Request $request)
  1055.     {
  1056.         if (!$request->isXmlHttpRequest()) {
  1057.             return $this->json(['result' => false ]);
  1058.         }
  1059.         $countries ApiUtil::getCountries();
  1060.         return $this->json(['result' => $countries ]);
  1061.     }
  1062. }