<?php
namespace Customize\Controller\Block;
use Customize\Controller\ProductController;
use Customize\Repository\CustomProductRepository;
use Eccube\Controller\AbstractController;
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 mysql_xdevapi\Exception;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class CartController extends AbstractController
{
/**
* @var ProductClassRepository
*/
protected $productClassRepository;
/**
* @var CartService
*/
protected $cartService;
/**
* @var PurchaseFlow
*/
protected $purchaseFlow;
/**
* @var BaseInfo
*/
protected $baseInfo;
/**
* @var ProductController
*/
protected $productController;
/**
* @var CustomProductRepository
*/
protected $customProductRepository;
/**
* CartController constructor.
*
* @param ProductClassRepository $productClassRepository
* @param CartService $cartService
* @param PurchaseFlow $cartPurchaseFlow
* @param BaseInfoRepository $baseInfoRepository
*/
public function __construct(
ProductClassRepository $productClassRepository,
CartService $cartService,
PurchaseFlow $cartPurchaseFlow,
BaseInfoRepository $baseInfoRepository,
ProductController $productController,
CustomProductRepository $customProductRepository
) {
$this->productClassRepository = $productClassRepository;
$this->cartService = $cartService;
$this->purchaseFlow = $cartPurchaseFlow;
$this->baseInfo = $baseInfoRepository->get();
$this->productController = $productController;
$this->customProductRepository = $customProductRepository;
}
/**
* @Route("/block/header", name="block_header")
* @Template("Block/header.twig")
*/
public function index(Request $request)
{
// カートを取得して明細の正規化を実行
$Carts = $this->cartService->getCarts();
// 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);
$cartItems = $this->cartService->getCarts(true);
$priorityProducts = [];
$colorOrSize = [];
if (!empty($cartItems)) {
foreach ($cartItems as $cartItem) {
$cartItem = $cartItem->getItems();
foreach ($cartItem as $item) {
$product = $item->getProductClass()->getProduct();
$colorOrSize[$product->getId()] = $this->customProductRepository->getColorOrSize($product->getId());
if (!empty($colorOrSize[$product->getId()]))
{
$colorOrSize[$product->getId()] = $colorOrSize[$product->getId()][0];
}
$priorityProduct = $this->customProductRepository->getPriorityFlagProduct($item->getProductClass()->getProduct()->getId());
if (is_object($priorityProduct)) {
$priorityProducts[$product->getId()] = $priorityProduct;
}else{
$priorityProducts[$product->getId()] = $product;
}
}
}
}
return [
'totalPrice' => $totalPrice,
'totalQuantity' => $totalQuantity,
// 空のカートを削除し取得し直す
'Carts' => $this->cartService->getCarts(true),
'PriorityProducts' => $priorityProducts,
'ColorOrSize' => $colorOrSize,
'least' => $least,
'quantity' => $quantity,
'is_delivery_free' => $isDeliveryFree,
];
}
}