<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Customize\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Eccube\Entity\BaseInfo;
use Symfony\Component\Routing\Annotation\Route;
use Eccube\Controller\AbstractController;
use Eccube\Entity\Product;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Eccube\Repository\ProductClassRepository;
use Eccube\Entity\ProductClass;
use Eccube\Entity\ProductStock;
use Eccube\Entity\Master\ProductStatus;
use Eccube\Form\Type\Front\CustomerLoginType;
use Eccube\Repository\Master\ProductStatusRepository;
use Eccube\Repository\Master\SaleTypeRepository;
use Eccube\Repository\ProductRepository;
use Eccube\Repository\BaseInfoRepository;
use Eccube\Service\PurchaseFlow\PurchaseContext;
use Eccube\Service\PurchaseFlow\PurchaseFlow;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Customize\Service\OrderHelper;
use Customize\Service\CartService;
use Customize\Util\ApiUtil;
class TopController extends AbstractController
{
/**
* @var ProductStatusRepository
*/
protected $productStatusRepository;
/**
* @var ProductClassRepository
*/
protected $productClassRepository;
/**
* @var ProductRepository
*/
protected $productRepository;
/**
* @var DeliveryTimeRepository
*/
protected $saleTypeRepository;
/**
* @var CartService
*/
protected $cartService;
/**
* @var BaseInfo
*/
protected $BaseInfo;
/**
* @var OrderHelper
*/
protected $orderHelper;
/**
* ProductController constructor.
*
* @param ProductRepository $productRepository
* @param ProductStatusRepository $productStatusRepository
* @param ProductClassRepository $productClassRepository
* @param DeliveryTimeRepository $saleTypeRepository
* @param BaseInfoRepository $baseInfoRepository
* @param CartService $cartService
* @param PurchaseFlow $cartPurchaseFlow
* @param OrderHelper $orderHelper
*/
public function __construct(
PurchaseFlow $cartPurchaseFlow,
ProductRepository $productRepository,
ProductClassRepository $productClassRepository,
ProductStatusRepository $productStatusRepository,
BaseInfoRepository $baseInfoRepository,
SaleTypeRepository $saleTypeRepository,
CartService $cartService,
OrderHelper $orderHelper
) {
$this->purchaseFlow = $cartPurchaseFlow;
$this->productClassRepository = $productClassRepository;
$this->productRepository = $productRepository;
$this->productStatusRepository = $productStatusRepository;
$this->saleTypeRepository = $saleTypeRepository;
$this->BaseInfo = $baseInfoRepository->get();
$this->cartService = $cartService;
$this->orderHelper = $orderHelper;
}
/**
* @Route("/", name="homepage", methods={"GET", "POST"})
* @Template("index.twig")
*/
public function index(Request $request, AuthenticationUtils $authenticationUtils)
{
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$this->cartService->clear();
} else {
if ($this->isGranted('IS_AUTHENTICATED_FULLY')) {
$Cart = $this->cartService->getCart();
if ($Cart && $this->orderHelper->verifyCart($Cart)) {
return $this->redirectToRoute('shopping');
} else {
return $this->redirectToRoute('homepage');
}
}
}
/* @var $form \Symfony\Component\Form\FormInterface */
$builder = $this->formFactory->createNamedBuilder('', CustomerLoginType::class);
if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
$Customer = $this->getUser();
if ($Customer) {
$builder->get('login_email')->setData($Customer->getEmail());
}
}
$event = new EventArgs(
[
'builder' => $builder,
],
$request
);
$this->eventDispatcher->dispatch(EccubeEvents::FRONT_SHOPPING_LOGIN_INITIALIZE, $event);
$form = $builder->getForm();
// カートチェック.
$Cart = $this->cartService->getCart();
return [
'error' => $authenticationUtils->getLastAuthenticationError(),
'form' => $form->createView(),
'Cart' => $Cart,
];
}
/**
* @Route("/", name="user_data", methods={"GET", "POST"})
* @Template("index.twig")
*/
public function userData()
{
$Cart = $this->cartService->getCart();
return [
'Cart' => $Cart,
];
}
/**
* Move rank with ajax.
*
* @param Request $request
*
* @throws \Exception
*
* @return JsonResponse
*
* @Route("/get_cart", name="get_cart")
*/
public function get_cart()
{
$Cart = $this->cartService->getCart();
$res = [];
if (null !== $Cart) {
foreach($Cart->getCartItems() as $CartItem) {
$ProductClass = $CartItem->getProductClass();
$Product = $ProductClass->getProduct();
if ($Product->getId() > 8) {
$item = array(
'id' => $ProductClass->getId(),
'name' => $Product->getName(),
'rental' => round($ProductClass->getPrice01(), 0),
'total' => round($ProductClass->getPrice02(), 0)
);
array_push($res, $item);
}
}
}
return new JsonResponse($res);
}
/**
* Move rank with ajax.
*
* @param Request $request
*
* @throws \Exception
*
* @return Response
*
* @Route("/add_product", name="add_product")
*/
public function add_product(Request $request)
{
if ($request->isXmlHttpRequest() && $request->getMethod() == 'POST') {
$product_id = $request->get('product_id');
$index = $request->get('index');
$from = $request->get('from');
$from_date = date('y/m/d', $from / 1000);
$to = $request->get('to');
$to_date = date('y/m/d', $to / 1000);
$country_name_jp = $request->get('country');
$country_code = $request->get('country_code');
$des_price = $request->get('price');
$days = round(($to - $from) / (1000 * 60 * 60 * 24));
if (!empty($product_id) && !is_null($product_id)) {
//update Product
$Product = $this->productRepository->findWithSortedClassCategories($product_id);
$Product->setName($country_name_jp.'_'.$index.'_'.$from_date.'_'.$to_date.'_'.$country_code);
$Product->setRentalStart(new \DateTime('20'.$from_date));
$Product->setRentalEnd(new \DateTime('20'.$to_date));
$this->entityManager->persist($Product);
$this->entityManager->flush();
$ProductClass = null;
$ProductStock = null;
if (!$Product) {
throw new NotFoundHttpException();
}
// 規格無しの商品の場合は、デフォルト規格を表示用に取得する
$has_class = $Product->hasProductClass();
if (!$has_class) {
$ProductClasses = $Product->getProductClasses();
foreach ($ProductClasses as $pc) {
if (!is_null($pc->getClassCategory1())) {
continue;
}
if ($pc->isVisible()) {
$ProductClass = $pc;
break;
}
}
if ($this->BaseInfo->isOptionProductTaxRule() && $ProductClass->getTaxRule()) {
$ProductClass->setTaxRate($ProductClass->getTaxRule()->getTaxRate());
}
$ProductStock = $ProductClass->getProductStock();
}
$ProductClass
->setPrice01($des_price)
->setPrice02($des_price)
->setProduct($Product);
$this->entityManager->persist($ProductClass);
$this->entityManager->flush();
$ProductStock->setProductClass($ProductClass);
$this->entityManager->persist($ProductStock);
$this->entityManager->flush();
} else {
//register Product
$Product = new Product();
$Product->setStatus($this->productStatusRepository->find(ProductStatus::DISPLAY_SHOW));
$Product->setName($country_name_jp.'_'.$index.'_'.$from_date.'_'.$to_date.'_'.$country_code);
$Product->setRentalStart(new \DateTime('20'.$from_date));
$Product->setRentalEnd(new \DateTime('20'.$to_date));
$this->entityManager->persist($Product);
$this->entityManager->flush();
//register ProductStock
$ProductStock = new ProductStock();
// // 在庫無制限時はnullを設定
$ProductStock->setStock(null);
$this->entityManager->persist($ProductStock);
$this->entityManager->flush();
//register ProductClass
$ProductClass = new ProductClass();
// $ProductStatus = $this->productStatusRepository->find(ProductStatus::DISPLAY_HIDE);
$SaleType = $this->saleTypeRepository->findOneBy([], ['sort_no' => 'ASC']);
$ProductClass
->setVisible(true)
->setStockUnlimited(true)
->setSaleType($SaleType)
->setPrice01($des_price)
->setPrice02($des_price)
->setProduct($Product);
$ProductClass->setProductStock($ProductStock);
$this->entityManager->persist($ProductClass);
$this->entityManager->flush();
$ProductStock->setProductClass($ProductClass);
$this->entityManager->persist($ProductStock);
$this->entityManager->flush();
// log_info('商品登録完了', [$id]);
//add Product into Cart
if (!$ProductClass instanceof ProductClass) {
$ProductClassId = $ProductClass;
$ProductClass = $this->entityManager
->getRepository(ProductClass::class)
->find($ProductClassId);
if (is_null($ProductClass)) {
return new Response('fail');
}
}
$quantity = 1;
// カートへ追加
$this->cartService->addProduct($ProductClass, $quantity);
// レンタル料
$RetalProduct = $this->productRepository->findWithSortedClassCategories(env('RENTAL_PRODUCT_ID'));
$RetalProductClass = $RetalProduct->getProductClasses()[0];
$this->cartService->addProduct($RetalProductClass, $days);
}
// 明細の正規化
$Carts = $this->cartService->getCarts();
foreach ($Carts as $Cart) {
$result = $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart, $this->getUser()));
// 復旧不可のエラーが発生した場合は追加した明細を削除.
if ($result->hasError()) {
$this->cartService->removeProduct($ProductClassId);
foreach ($result->getErrors() as $error) {
$errorMessages[] = $error->getMessage();
}
}
foreach ($result->getWarning() as $warning) {
$errorMessages[] = $warning->getMessage();
}
}
$this->cartService->save();
return new Response($ProductClass->getId());
}
return new Response('fail');
}
/**
* Move rank with ajax.
*
* @param Request $request
*
* @throws \Exception
*
* @return JsonResponse
*
* @Route("/add_products", name="add_products")
*/
public function add_products(Request $request)
{
$res = 'fail';
if ($request->isXmlHttpRequest() && $request->getMethod() == 'POST') {
$cart_data = $request->get('cart_data');
$lpCartId = $request->get('lp_cart_id');
// Check cart reference from LP
log_info('[ADD PRODUCTS START] Check cart reference from LP.');
if (!empty($lpCartId)) {
// remote cart data in eccube
$this->cartService->clear();
}
log_info('[ADD PRODUCTS END] Check cart reference from LP.');
// Add product
if (!empty($cart_data)) {
log_info('[ADD PRODUCTS] Add main product.');
$json = json_decode($cart_data);
foreach ($json as $item) {
$traffic_limit = '1g';
$traffic_unlimit = '5g';
$traffic = $item->picked_plan === $traffic_unlimit ? strtoupper($traffic_unlimit) : strtoupper($traffic_limit);
$rentalFromDate = new \DateTime($item->from_date);
$rentalToDate = new \DateTime($item->to_date);
$rentalDays = $item->date_count;
$communicationFee = $item->country_price * $rentalDays;
$rentalFee = env('RENTAL_FEE_PER_DAY', 220) * $rentalDays;
$productName = $item->country_name.'_'.substr($item->from_date, 2).'_'.substr($item->to_date, 2).'_'.$item->country_code.'_'.$traffic;
if (!empty($item->productclass_id)) {
//update Product
$ProductClass = $this->productClassRepository->find($item->productclass_id);
$ProductStock = $ProductClass->getProductStock();
$Product = $ProductClass->getProduct();
$Product->setName($productName);
$Product->setRentalStart($rentalFromDate);
$Product->setRentalEnd($rentalToDate);
$this->entityManager->persist($Product);
$ProductClass
->setPrice01($communicationFee)
->setPrice02($communicationFee + $rentalFee)
->setProduct($Product);
$this->entityManager->persist($ProductClass);
$ProductStock->setProductClass($ProductClass);
$this->entityManager->persist($ProductStock);
$this->cartService->removeProduct($ProductClass);
$Carts = $this->cartService->getCarts();
foreach ($Carts as $Cart) {
$result = $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart, $this->getUser()));
}
$this->cartService->save();
$this->entityManager->flush();
} else {
//register Product
$Product = new Product();
$Product->setStatus($this->productStatusRepository->find(ProductStatus::DISPLAY_SHOW));
$Product->setName($productName);
$Product->setRentalStart($rentalFromDate);
$Product->setRentalEnd($rentalToDate);
// 商品オプションと区別するためのフラグを設定
$Product->setBasic(true);
$this->entityManager->persist($Product);
$this->entityManager->flush();
//register ProductStock
$ProductStock = new ProductStock();
// // 在庫無制限時はnullを設定
$ProductStock->setStock(null);
$this->entityManager->persist($ProductStock);
$this->entityManager->flush();
//register ProductClass
$ProductClass = new ProductClass();
// $ProductStatus = $this->productStatusRepository->find(ProductStatus::DISPLAY_HIDE);
$SaleType = $this->saleTypeRepository->findOneBy([], ['sort_no' => 'ASC']);
$ProductClass
->setVisible(true)
->setStockUnlimited(true)
->setSaleType($SaleType)
->setPrice01($communicationFee)
->setPrice02($communicationFee + $rentalFee)
->setProduct($Product);
$ProductClass->setProductStock($ProductStock);
$this->entityManager->persist($ProductClass);
$this->entityManager->flush();
$ProductStock->setProductClass($ProductClass);
$this->entityManager->persist($ProductStock);
$this->entityManager->flush();
// log_info('商品登録完了', [$id]);
//add Product into Cart
if (!$ProductClass instanceof ProductClass) {
$ProductClassId = $ProductClass;
$ProductClass = $this->entityManager
->getRepository(ProductClass::class)
->find($ProductClassId);
if (is_null($ProductClass)) {
return new JsonResponse($res);
}
}
}
$quantity = 1;
// カートへ追加
$this->cartService->addProduct($ProductClass, 1);
$Carts = $this->cartService->getCarts();
foreach ($Carts as $Cart) {
$result = $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart, $this->getUser()));
}
$this->cartService->save();
}
log_info('[ADD PRODUCTS END] Add main product.');
}
// Add option product
log_info('[ADD PRODUCTS START] Add option product.');
$security_option = $request->get('security_option');
$option_product_1 = $request->get('option_product_1');
$option_product_2 = $request->get('option_product_2');
$pre_order_id = $this->cartService->getPreOrderId();
$SecurityProductId = 3;
switch ($security_option) {
case '1':
$SecurityProductId = 3;
break;
case '2':
$SecurityProductId = 4;
break;
case '3':
$SecurityProductId = 5;
break;
default:
break;
}
$SecurityProduct = $this->productRepository->findWithSortedClassCategories($SecurityProductId);
$SecurityProductClass = null;
// 規格無しの商品の場合は、デフォルト規格を表示用に取得する
$has_class = $SecurityProduct->hasProductClass();
if (!$has_class) {
$SecurityProductClasses = $SecurityProduct->getProductClasses();
foreach ($SecurityProductClasses as $pc) {
if (!is_null($pc->getClassCategory1())) {
continue;
}
if ($pc->isVisible()) {
$SecurityProductClass = $pc;
break;
}
}
if ($this->BaseInfo->isOptionProductTaxRule() && $SecurityProductClass->getTaxRule()) {
$SecurityProductClass->setTaxRate($SecurityProductClass->getTaxRule()->getTaxRate());
}
}
$Cart = $this->cartService->getCart();
if ($pre_order_id != null) {
$Cart->setPreOrderId($pre_order_id);
}
$total_days = 0;
$dates = [];
foreach ($Cart->getCartItems() as $CartItem) {
$ProductClass = $CartItem->getProductClass();
$Product = $ProductClass->getProduct();
$str_name = $Product->getName();
if (str_contains($str_name, '_')) {
$contents = explode("_", $str_name);
$from = $contents[1];
$to = $contents[2];
array_push($dates, new \DateTime('20'.$from), new \DateTime('20'.$to));
} else {
$this->cartService->removeProduct($ProductClass);
}
}
if(sizeof($dates)>0) {
$max_date = max($dates);
$min_date = min($dates);
$diff = $max_date->diff($min_date);
$total_days = $diff->days + 1;
}
$quantity = strval($total_days);
// レンタル料
$RetalProduct = $this->productRepository->findWithSortedClassCategories(env('RENTAL_PRODUCT_ID'));
$RetalProductClass = $RetalProduct->getProductClasses()[0];
$this->cartService->addProduct($RetalProductClass, $quantity);
$Carts = $this->cartService->getCarts();
foreach ($Carts as $Cart) {
$result = $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart, $this->getUser()));
}
$this->cartService->save();
if (!$SecurityProductClass instanceof ProductClass) {
$SecurityProductClassId = $SecurityProductClass;
$SecurityProductClass = $this->entityManager
->getRepository(ProductClass::class)
->find($SecurityProductClassId);
if (is_null($SecurityProductClass)) {
return new Response('fail');
}
}
if ($pre_order_id != null) {
$this->cartService->setPreOrderId($pre_order_id);
}
// カートへ追加
$this->cartService->addProduct($SecurityProductClass, $quantity);
// デポジット
$DepositProductId = 8;
$DepositProduct = $this->productRepository->findWithSortedClassCategories($DepositProductId);
$DepositProductClass = null;
// 規格無しの商品の場合は、デフォルト規格を表示用に取得する
$has_class = $DepositProduct->hasProductClass();
if (!$has_class) {
$DepositProductClass = $DepositProduct->getProductClasses();
foreach ($DepositProductClass as $pc) {
if (!is_null($pc->getClassCategory1())) {
continue;
}
if ($pc->isVisible()) {
$DepositProductClass = $pc;
break;
}
}
}
if (!$DepositProductClass instanceof ProductClass) {
$DepositProductClassId = $DepositProductClass;
$DepositProductClass = $this->entityManager
->getRepository(ProductClass::class)
->find($DepositProductClassId);
if (is_null($DepositProductClass)) {
return new Response('fail');
}
}
// カートへ追加
$this->cartService->addProduct($DepositProductClass, 1);
// 明細の正規化
$Carts = $this->cartService->getCarts();
foreach ($Carts as $Cart) {
$result = $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart, $this->getUser()));
// 復旧不可のエラーが発生した場合は追加した明細を削除.
if ($result->hasError()) {
$this->cartService->removeProduct($ProductClassId);
foreach ($result->getErrors() as $error) {
$errorMessages[] = $error->getMessage();
}
}
foreach ($result->getWarning() as $warning) {
$errorMessages[] = $warning->getMessage();
}
}
$this->cartService->save();
if ($option_product_1 == '1') {
$OptionProduct1Id = 6;
$OptionProduct1 = $this->productRepository->findWithSortedClassCategories($OptionProduct1Id);
$OptionProduct1Class = null;
// 規格無しの商品の場合は、デフォルト規格を表示用に取得する
$has_class = $SecurityProduct->hasProductClass();
if (!$has_class) {
$OptionProduct1Classes = $OptionProduct1->getProductClasses();
foreach ($OptionProduct1Classes as $pc) {
if (!is_null($pc->getClassCategory1())) {
continue;
}
if ($pc->isVisible()) {
$OptionProduct1Class = $pc;
break;
}
}
if ($this->BaseInfo->isOptionProductTaxRule() && $OptionProduct1Class->getTaxRule()) {
$OptionProduct1Class->setTaxRate($OptionProduct1Class->getTaxRule()->getTaxRate());
}
}
if (!$OptionProduct1Class instanceof ProductClass) {
$OptionProduct1ClassId = $OptionProduct1Class;
$OptionProduct1Class = $this->entityManager
->getRepository(ProductClass::class)
->find($OptionProduct1ClassId);
if (is_null($OptionProduct1Class)) {
return new Response('fail');
}
}
// カートへ追加
$this->cartService->addProduct($OptionProduct1Class, $quantity);
// 明細の正規化
$Carts = $this->cartService->getCarts();
foreach ($Carts as $Cart) {
$result = $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart, $this->getUser()));
// 復旧不可のエラーが発生した場合は追加した明細を削除.
if ($result->hasError()) {
$this->cartService->removeProduct($ProductClassId);
foreach ($result->getErrors() as $error) {
$errorMessages[] = $error->getMessage();
}
}
foreach ($result->getWarning() as $warning) {
$errorMessages[] = $warning->getMessage();
}
}
if ($pre_order_id != null) {
$this->cartService->setPreOrderId($pre_order_id);
}
$this->cartService->save();
}
if ($option_product_2 == '1') {
$OptionProduct2Id = 7;
$OptionProduct2 = $this->productRepository->findWithSortedClassCategories($OptionProduct2Id);
$OptionProduct2Class = null;
// 規格無しの商品の場合は、デフォルト規格を表示用に取得する
$has_class = $SecurityProduct->hasProductClass();
if (!$has_class) {
$OptionProduct2Classes = $OptionProduct2->getProductClasses();
foreach ($OptionProduct2Classes as $pc) {
if (!is_null($pc->getClassCategory1())) {
continue;
}
if ($pc->isVisible()) {
$OptionProduct2Class = $pc;
break;
}
}
if ($this->BaseInfo->isOptionProductTaxRule() && $OptionProduct2Class->getTaxRule()) {
$OptionProduct2Class->setTaxRate($OptionProduct2Class->getTaxRule()->getTaxRate());
}
}
if (!$OptionProduct2Class instanceof ProductClass) {
$OptionProduct2ClassId = $OptionProduct2Class;
$OptionProduct2Class = $this->entityManager
->getRepository(ProductClass::class)
->find($OptionProduct2ClassId);
if (is_null($OptionProduct2Class)) {
return new Response('fail');
}
}
// カートへ追加
$this->cartService->addProduct($OptionProduct2Class, '1');
// 明細の正規化
$Carts = $this->cartService->getCarts();
foreach ($Carts as $Cart) {
$result = $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart, $this->getUser()));
// 復旧不可のエラーが発生した場合は追加した明細を削除.
if ($result->hasError()) {
$this->cartService->removeProduct($ProductClassId);
foreach ($result->getErrors() as $error) {
$errorMessages[] = $error->getMessage();
}
}
foreach ($result->getWarning() as $warning) {
$errorMessages[] = $warning->getMessage();
}
}
if ($pre_order_id != null) {
$this->cartService->setPreOrderId($pre_order_id);
}
$this->cartService->save();
log_info('[ADD PRODUCTS END] Add option product.');
}
$res = 'success';
return new JsonResponse($res);
}
return new JsonResponse($res);
}
/**
* Move rank with ajax.
*
* @param Request $request
*
* @throws \Exception
*
* @return Response
*
* @Route("/delete_cartitem", name="delete_cartitem")
*/
public function delete_cartitem(Request $request)
{
if ($request->isXmlHttpRequest() && $request->getMethod() == 'POST') {
// $cartitem_id = $request->get('cartitem_id');
// $product_id = $request->get('product_id');
$productclass_id = $request->get('productclass_id');
if (!empty($productclass_id) && !is_null($productclass_id)) {
/** @var ProductClass $ProductClass */
$ProductClass = $this->productClassRepository->find($productclass_id);
$this->cartService->removeProduct($ProductClass);
// カートを取得して明細の正規化を実行
$Carts = $this->cartService->getCarts();
$this->execPurchaseFlow($Carts);
log_info('カート演算処理終了', ['operation' => 'remove', 'product_class_id' => $productclass_id]);
return new Response("success");
} else {
return new Response('fail');
}
}
return new Response('fail');
}
/**
* Move rank with ajax.
*
* @param Request $request
*
* @throws \Exception
*
* @return Response
*
* @Route("/delete_all_cartitem", name="delete_all_cartitem")
*/
public function delete_all_cartitem(Request $request)
{
if ($request->isXmlHttpRequest() && $request->getMethod() == 'GET') {
$this->cartService->clear();
return new Response("success");
}
return new Response('fail');
}
/**
* @param $Carts
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse|null
*/
protected function execPurchaseFlow($Carts)
{
/** @var PurchaseFlowResult[] $flowResults */
$flowResults = array_map(function ($Cart) {
$purchaseContext = new PurchaseContext($Cart, $this->getUser());
return $this->purchaseFlow->validate($Cart, $purchaseContext);
}, $Carts);
// 復旧不可のエラーが発生した場合はカートをクリアして再描画
$hasError = false;
foreach ($flowResults as $result) {
if ($result->hasError()) {
$hasError = true;
foreach ($result->getErrors() as $error) {
$this->addRequestError($error->getMessage());
}
}
}
if ($hasError) {
$this->cartService->clear();
return $this->redirectToRoute('cart');
}
$this->cartService->save();
foreach ($flowResults as $index => $result) {
foreach ($result->getWarning() as $warning) {
if ($Carts[$index]->getItems()->count() > 0) {
$cart_key = $Carts[$index]->getCartKey();
$this->addRequestError($warning->getMessage(), "front.cart.${cart_key}");
} else {
// キーが存在しない場合はグローバルにエラーを表示する
$this->addRequestError($warning->getMessage());
}
}
}
return null;
}
/**
* Move rank with ajax.
*
* @param Request $request
*
* @throws \Exception
*
* @return Response
*
* @Route("/add_option_product", name="add_option_product")
*/
public function add_option_product(Request $request)
{
$pre_order_id = $this->cartService->getPreOrderId();
if ($request->isXmlHttpRequest() && $request->getMethod() == 'POST') {
$security_option = $request->get('security_option');
$option_product_1 = $request->get('option_product_1');
$option_product_2 = $request->get('option_product_2');
$pre_order_id = $request->get('pre_order_id');
$SecurityProductId = 3;
switch ($security_option) {
case '1':
$SecurityProductId = 3;
break;
case '2':
$SecurityProductId = 4;
break;
case '3':
$SecurityProductId = 5;
break;
default:
break;
}
$SecurityProduct = $this->productRepository->findWithSortedClassCategories($SecurityProductId);
$SecurityProductClass = null;
// 規格無しの商品の場合は、デフォルト規格を表示用に取得する
$has_class = $SecurityProduct->hasProductClass();
if (!$has_class) {
$SecurityProductClasses = $SecurityProduct->getProductClasses();
foreach ($SecurityProductClasses as $pc) {
if (!is_null($pc->getClassCategory1())) {
continue;
}
if ($pc->isVisible()) {
$SecurityProductClass = $pc;
break;
}
}
if ($this->BaseInfo->isOptionProductTaxRule() && $SecurityProductClass->getTaxRule()) {
$SecurityProductClass->setTaxRate($SecurityProductClass->getTaxRule()->getTaxRate());
}
}
$Cart = $this->cartService->getCart();
if ($pre_order_id != null) {
$Cart->setPreOrderId($pre_order_id);
}
$total_days = 0;
$dates = [];
foreach ($Cart->getCartItems() as $CartItem) {
$ProductClass = $CartItem->getProductClass();
$Product = $ProductClass->getProduct();
$str_name = $Product->getName();
if (str_contains($str_name, '_')) {
$contents = explode("_", $str_name);
$from = $contents[1];
$to = $contents[2];
array_push($dates, new \DateTime('20'.$from), new \DateTime('20'.$to));
// $days = floor((strtotime('20'.$to) - strtotime('20'.$from)) / (60 * 60 * 24)) + 1;
} else {
$this->cartService->removeProduct($ProductClass);
}
}
// $this->cartService->save();
if(sizeof($dates)>0) {
$max_date = max($dates);
$min_date = min($dates);
$diff = $max_date->diff($min_date);
$total_days = $diff->days + 1;
}
$quantity = strval($total_days);
// レンタル料
$RetalProduct = $this->productRepository->findWithSortedClassCategories(env('RENTAL_PRODUCT_ID'));
$RetalProductClass = $RetalProduct->getProductClasses()[0];
$this->cartService->addProduct($RetalProductClass, $quantity);
$Carts = $this->cartService->getCarts();
foreach ($Carts as $Cart) {
$result = $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart, $this->getUser()));
}
$this->cartService->save();
if (!$SecurityProductClass instanceof ProductClass) {
$SecurityProductClassId = $SecurityProductClass;
$SecurityProductClass = $this->entityManager
->getRepository(ProductClass::class)
->find($SecurityProductClassId);
if (is_null($SecurityProductClass)) {
return new Response('fail');
}
}
if ($pre_order_id != null) {
$this->cartService->setPreOrderId($pre_order_id);
}
// カートへ追加
$this->cartService->addProduct($SecurityProductClass, $quantity);
// デポジット
$DepositProductId = 8;
$DepositProduct = $this->productRepository->findWithSortedClassCategories($DepositProductId);
$DepositProductClass = null;
// 規格無しの商品の場合は、デフォルト規格を表示用に取得する
$has_class = $DepositProduct->hasProductClass();
if (!$has_class) {
$DepositProductClass = $DepositProduct->getProductClasses();
foreach ($DepositProductClass as $pc) {
if (!is_null($pc->getClassCategory1())) {
continue;
}
if ($pc->isVisible()) {
$DepositProductClass = $pc;
break;
}
}
}
if (!$DepositProductClass instanceof ProductClass) {
$DepositProductClassId = $DepositProductClass;
$DepositProductClass = $this->entityManager
->getRepository(ProductClass::class)
->find($DepositProductClassId);
if (is_null($DepositProductClass)) {
return new Response('fail');
}
}
// カートへ追加
$this->cartService->addProduct($DepositProductClass, 1);
// 明細の正規化
$Carts = $this->cartService->getCarts();
foreach ($Carts as $Cart) {
$result = $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart, $this->getUser()));
// 復旧不可のエラーが発生した場合は追加した明細を削除.
if ($result->hasError()) {
$this->cartService->removeProduct($ProductClassId);
foreach ($result->getErrors() as $error) {
$errorMessages[] = $error->getMessage();
}
}
foreach ($result->getWarning() as $warning) {
$errorMessages[] = $warning->getMessage();
}
}
$this->cartService->save();
if ($option_product_1 == '1') {
$OptionProduct1Id = 6;
$OptionProduct1 = $this->productRepository->findWithSortedClassCategories($OptionProduct1Id);
$OptionProduct1Class = null;
// 規格無しの商品の場合は、デフォルト規格を表示用に取得する
$has_class = $SecurityProduct->hasProductClass();
if (!$has_class) {
$OptionProduct1Classes = $OptionProduct1->getProductClasses();
foreach ($OptionProduct1Classes as $pc) {
if (!is_null($pc->getClassCategory1())) {
continue;
}
if ($pc->isVisible()) {
$OptionProduct1Class = $pc;
break;
}
}
if ($this->BaseInfo->isOptionProductTaxRule() && $OptionProduct1Class->getTaxRule()) {
$OptionProduct1Class->setTaxRate($OptionProduct1Class->getTaxRule()->getTaxRate());
}
}
if (!$OptionProduct1Class instanceof ProductClass) {
$OptionProduct1ClassId = $OptionProduct1Class;
$OptionProduct1Class = $this->entityManager
->getRepository(ProductClass::class)
->find($OptionProduct1ClassId);
if (is_null($OptionProduct1Class)) {
return new Response('fail');
}
}
// カートへ追加
$this->cartService->addProduct($OptionProduct1Class, $quantity);
// 明細の正規化
$Carts = $this->cartService->getCarts();
foreach ($Carts as $Cart) {
$result = $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart, $this->getUser()));
// 復旧不可のエラーが発生した場合は追加した明細を削除.
if ($result->hasError()) {
$this->cartService->removeProduct($ProductClassId);
foreach ($result->getErrors() as $error) {
$errorMessages[] = $error->getMessage();
}
}
foreach ($result->getWarning() as $warning) {
$errorMessages[] = $warning->getMessage();
}
}
if ($pre_order_id != null) {
$this->cartService->setPreOrderId($pre_order_id);
}
$this->cartService->save();
}
if ($option_product_2 == '1') {
$OptionProduct2Id = 7;
$OptionProduct2 = $this->productRepository->findWithSortedClassCategories($OptionProduct2Id);
$OptionProduct2Class = null;
// 規格無しの商品の場合は、デフォルト規格を表示用に取得する
$has_class = $SecurityProduct->hasProductClass();
if (!$has_class) {
$OptionProduct2Classes = $OptionProduct2->getProductClasses();
foreach ($OptionProduct2Classes as $pc) {
if (!is_null($pc->getClassCategory1())) {
continue;
}
if ($pc->isVisible()) {
$OptionProduct2Class = $pc;
break;
}
}
if ($this->BaseInfo->isOptionProductTaxRule() && $OptionProduct2Class->getTaxRule()) {
$OptionProduct2Class->setTaxRate($OptionProduct2Class->getTaxRule()->getTaxRate());
}
}
if (!$OptionProduct2Class instanceof ProductClass) {
$OptionProduct2ClassId = $OptionProduct2Class;
$OptionProduct2Class = $this->entityManager
->getRepository(ProductClass::class)
->find($OptionProduct2ClassId);
if (is_null($OptionProduct2Class)) {
return new Response('fail');
}
}
// カートへ追加
$this->cartService->addProduct($OptionProduct2Class, '1');
// 明細の正規化
$Carts = $this->cartService->getCarts();
foreach ($Carts as $Cart) {
$result = $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart, $this->getUser()));
// 復旧不可のエラーが発生した場合は追加した明細を削除.
if ($result->hasError()) {
$this->cartService->removeProduct($ProductClassId);
foreach ($result->getErrors() as $error) {
$errorMessages[] = $error->getMessage();
}
}
foreach ($result->getWarning() as $warning) {
$errorMessages[] = $warning->getMessage();
}
}
if ($pre_order_id != null) {
$this->cartService->setPreOrderId($pre_order_id);
}
$this->cartService->save();
}
return new Response('success');
}
return new Response('fail');
}
/**
* [countries description]
* @Route("/countries", name="get_countries")
* @return [type] [description]
*/
public function countries(Request $request)
{
if (!$request->isXmlHttpRequest()) {
return $this->json(['result' => false ]);
}
$countries = ApiUtil::getCountries();
return $this->json(['result' => $countries ]);
}
}