<?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 Eccube\Entity\BaseInfo;
use Eccube\Entity\ProductClass;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Eccube\Repository\BaseInfoRepository;
use Eccube\Repository\ProductClassRepository;
use Eccube\Service\CartService;
use Eccube\Service\OrderHelper;
use Eccube\Service\PurchaseFlow\PurchaseContext;
use Eccube\Service\PurchaseFlow\PurchaseFlow;
use Eccube\Service\PurchaseFlow\PurchaseFlowResult;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Eccube\Controller\AbstractController;
use Customize\Form\Type\CartOptionType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Eccube\Repository\CartItemRepository;
class CustomCartController extends AbstractController
{
/**
* @var ProductClassRepository
*/
protected $productClassRepository;
/**
* @var CartItemRepository
*/
protected $cartItemRepository;
/**
* @var CartService
*/
protected $cartService;
/**
* @var PurchaseFlow
*/
protected $purchaseFlow;
/**
* @var BaseInfo
*/
protected $baseInfo;
/**
* CartController constructor.
*
* @param ProductClassRepository $productClassRepository
* @param CartItemRepository $cartItemRepository
* @param CartService $cartService
* @param PurchaseFlow $cartPurchaseFlow
* @param BaseInfoRepository $baseInfoRepository
*/
public function __construct(
ProductClassRepository $productClassRepository,
CartItemRepository $cartItemRepository,
CartService $cartService,
PurchaseFlow $cartPurchaseFlow,
BaseInfoRepository $baseInfoRepository
) {
$this->productClassRepository = $productClassRepository;
$this->cartItemRepository = $cartItemRepository;
$this->cartService = $cartService;
$this->purchaseFlow = $cartPurchaseFlow;
$this->baseInfo = $baseInfoRepository->get();
}
/**
* カート画面.
*
* @Route("/cart", name="cart", methods={"GET"})
* @Template("Cart/index.twig")
*/
public function index(Request $request)
{
// カートを取得して明細の正規化を実行
$Carts = $this->cartService->getCarts();
$this->execPurchaseFlow($Carts);
// TODO itemHolderから取得できるように
$least = [];
$quantity = [];
$isDeliveryFree = [];
$totalPrice = 0;
$totalQuantity = 0;
foreach ($Carts as $Cart) {
$quantity[$Cart->getCartKey()] = 0;
$isDeliveryFree[$Cart->getCartKey()] = false;
if ($this->baseInfo->getDeliveryFreeQuantity()) {
if ($this->baseInfo->getDeliveryFreeQuantity() > $Cart->getQuantity()) {
$quantity[$Cart->getCartKey()] = $this->baseInfo->getDeliveryFreeQuantity() - $Cart->getQuantity();
} else {
$isDeliveryFree[$Cart->getCartKey()] = true;
}
}
if ($this->baseInfo->getDeliveryFreeAmount()) {
if (!$isDeliveryFree[$Cart->getCartKey()] && $this->baseInfo->getDeliveryFreeAmount() <= $Cart->getTotalPrice()) {
$isDeliveryFree[$Cart->getCartKey()] = true;
} else {
$least[$Cart->getCartKey()] = $this->baseInfo->getDeliveryFreeAmount() - $Cart->getTotalPrice();
}
}
$totalPrice += $Cart->getTotalPrice();
$totalQuantity += $Cart->getQuantity();
}
// カートが分割された時のセッション情報を削除
$request->getSession()->remove(OrderHelper::SESSION_CART_DIVIDE_FLAG);
// オプション項目入力フォームを追加
$builder = $this->formFactory->createBuilder( );
$builder
->add(
'data',
CollectionType::class,
[
'entry_type' => CartOptionType::class,
'entry_options' => [
//'product' => $Product,
//'id_add_product_id' => false,
//'attr' => ['class' => 'oneclass'],
],
'allow_add' => true,
'allow_delete' => true,
'label' => '',
]
);
//$form = $this->createForm(CartOptionType::class);
//$form->handleRequest($request);
return [
'totalPrice' => $totalPrice,
'totalQuantity' => $totalQuantity,
// 空のカートを削除し取得し直す
'Carts' => $this->cartService->getCarts(true),
'least' => $least,
'quantity' => $quantity,
'is_delivery_free' => $isDeliveryFree,
//'form' => $form->createView(), //cartoption
'form' => $builder->getForm()->createView(),
];
}
/**
* @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;
}
/**
* カート明細の加算/減算/削除を行う.
*
* - 加算
* - 明細の個数を1増やす
* - 減算
* - 明細の個数を1減らす
* - 個数が0になる場合は、明細を削除する
* - 削除
* - 明細を削除する
*
* @Route(
* path="/cart/{operation}/{productClassId}",
* name="cart_handle_item",
* methods={"PUT"},
* requirements={
* "operation": "up|down|remove",
* "productClassId": "\d+"
* }
* )
*/
public function handleCartItem($operation, $productClassId)
{
log_info('カート明細操作開始', ['operation' => $operation, 'product_class_id' => $productClassId]);
$this->isTokenValid();
/** @var ProductClass $ProductClass */
$ProductClass = $this->productClassRepository->find($productClassId);
// if (is_null($ProductClass)) {
// log_info('商品が存在しないため、カート画面へredirect', ['operation' => $operation, 'product_class_id' => $productClassId]);
// return $this->redirectToRoute('cart');
// }
// 明細の増減・削除
switch ($operation) {
case 'up':
$this->cartService->addProduct($ProductClass, 1);
break;
case 'down':
$this->cartService->addProduct($ProductClass, -1);
break;
case 'remove':
// 削除の場合カート画面からはCartItem.idが渡されている。そのままremoveProductへ渡す。
$this->cartService->removeCartItem($productClassId);
break;
}
// カートを取得して明細の正規化を実行
$Carts = $this->cartService->getCarts();
$this->execPurchaseFlow($Carts);
log_info('カート演算処理終了', ['operation' => $operation, 'product_class_id' => $productClassId]);
return $this->redirectToRoute('cart');
}
/**
* カートをロック状態に設定し、購入確認画面へ遷移する.
*
* @Route("/cart/buystep/{cart_key}", name="cart_buystep", requirements={"cart_key" = "[a-zA-Z0-9]+[_][\x20-\x7E]+"}, methods={"GET"})
*/
public function buystep(Request $request, $cart_key)
{
$Carts = $this->cartService->getCart();
if (!is_object($Carts)) {
return $this->redirectToRoute('cart');
}
// FRONT_CART_BUYSTEP_INITIALIZE
$event = new EventArgs(
[],
$request
);
$this->eventDispatcher->dispatch($event, EccubeEvents::FRONT_CART_BUYSTEP_INITIALIZE);
$this->cartService->setPrimary($cart_key);
$this->cartService->save();
// FRONT_CART_BUYSTEP_COMPLETE
$event = new EventArgs(
[],
$request
);
$this->eventDispatcher->dispatch($event, EccubeEvents::FRONT_CART_BUYSTEP_COMPLETE);
if ($event->hasResponse()) {
return $event->getResponse();
}
return $this->redirectToRoute('shopping');
}
/**
* カートの商品にオプション情報追加.
*
* @Route("/cart/add_option", name="cart_add_option", methods={"POST"})
*/
public function add_option(Request $request)
{
$form = $request->get('form');
foreach ($form['data'] as $Item) {
$cartitem = $this->cartItemRepository->find($Item['cart_item_id']);
$cartitem->setEngraving($Item['engraving']);
if (isset($Item['engraving_font'])) {
$cartitem->setEngravingFont($Item['engraving_font']);
}
$cartitem->setEngravingText($Item['engraving_text']);
$cartitem->setStone($Item['stone']);
$cartitem->setOptionTotalPrice($Item['option_total_price']);
// DB書き込み
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($cartitem);
$entityManager->flush();
}
// すでにOrderItemが作成されている場合は作り直す
$Cart = $this->entityManager->getRepository(\Eccube\Entity\Cart::class)->findOneBy(array('cart_key' => $form['data'][0]['cart_key']));
if($Cart) {
$Order = $this->entityManager->getRepository(\Eccube\Entity\Order::class)->findOneBy(array('pre_order_id' => $Cart->getPreOrderId()));
if($Order) {
$OrderItems = $this->entityManager->getRepository(\Eccube\Entity\OrderItem::class)->findBy(array('Order' => $Order));
if($OrderItems) {
$cnt = 0;
foreach ($OrderItems as $OrderItem) {
if ($OrderItem->isProduct()) {
// 保存
$newOrderItem = clone $OrderItem;
// DBから削除
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($OrderItem);
$entityManager->flush();
// オプション情報更新
if (array_key_exists('engraving', $form['data'][$cnt])) {
$newOrderItem->setEngraving($form['data'][$cnt]['engraving']);
}
if (array_key_exists('engraving_font', $form['data'][$cnt])) {
$newOrderItem->setEngravingFont($form['data'][$cnt]['engraving_font']);
}
if (array_key_exists('engraving_text', $form['data'][$cnt])) {
$newOrderItem->setEngravingText($form['data'][$cnt]['engraving_text']);
}
if (array_key_exists('stone', $form['data'][$cnt])) {
$newOrderItem->setStone($form['data'][$cnt]['stone']);
}
if (array_key_exists('option_total_price', $form['data'][$cnt])) {
$newOrderItem->setOptionTotalPrice($form['data'][$cnt]['option_total_price']);
}
$Order->addOrderItem($newOrderItem);
$this->entityManager->persist($newOrderItem);
$entityManager->flush();
$cnt++;
}
}
}
}
}
$cart_key = $form['data'][0]['cart_key'];
return $this->redirectToRoute('cart_buystep',['cart_key' => $cart_key]);
}
}