<?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\Repository\OrderRepository;
use Eccube\Repository\ProductRepository;
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\CartController;
class CartControllerCustomizer extends CartController
{
/**
* @var OrderRepository
*/
protected $orderRepository;
/**
* @var ProductRepository
*/
protected $productRepository;
/**
* CartController constructor.
*
* @param ProductClassRepository $productClassRepository
* @param CartService $cartService
* @param PurchaseFlow $cartPurchaseFlow
* @param BaseInfoRepository $baseInfoRepository
* @param OrderRepository $orderRepository
*/
public function __construct(
ProductClassRepository $productClassRepository,
CartService $cartService,
PurchaseFlow $cartPurchaseFlow,
BaseInfoRepository $baseInfoRepository,
OrderRepository $orderRepository,
ProductRepository $productRepository
) {
parent::__construct(
$productClassRepository,
$cartService,
$cartPurchaseFlow,
$baseInfoRepository,
);
$this->orderRepository = $orderRepository;
$this->productRepository = $productRepository;
}
/**
* カート画面.
*
* @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;
// ▼追加処理ここから
$arrReserved = [];
$arrValied = [];
$arrOrder = $this->orderRepository->findAll();
// ▲追加処理ここまで
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();
// ▼追加処理ここから
$is_valied = true;
$currentperiod = '';
foreach ($Cart->getItems() as $item) {
$arrRental = [];
$period = $item->getPeriod();
if ($item->getProductClass()->getClassCategory1()) {
if (!$period) {
$is_valied = false;
}
else {
$item->updateReallyPeriod();
if (!$currentperiod) {
$currentperiod = $period;
}
if ($period != $currentperiod) {
$is_valied = false;
}
// レンタル期間が重複している注文がないかチェック
$product_id = $item->getProductClass()->getProduct()->getId();
foreach ($arrOrder as $order) {
if ($order->getOrderStatus() != '購入処理中' && $order->getOrderStatus() != '注文取消し') {
foreach ($order->getItems() as $item) {
$class = $item->getProductClass();
if ($class) {
if ($class->getProduct()->getId() == $product_id) {
$val = $item->getPeriod();
if ($val) {
$vals = explode('|', $val);
$arrRental[] = $vals[0];
}
}
}
}
}
}
// 商品情報から、貸出不可期間を取得
$invalidity_period = $this->productRepository->find($product_id)->getInvalidityPeriod();
if ($invalidity_period) {
$arrRental[] = $invalidity_period;
}
}
}
$arrReserved[] = implode(';', $arrRental);
}
$arrValied[] = $is_valied;
// ▲追加処理ここまで
}
// カートが分割された時のセッション情報を削除
$request->getSession()->remove(OrderHelper::SESSION_CART_DIVIDE_FLAG);
return [
'totalPrice' => $totalPrice,
'totalQuantity' => $totalQuantity,
// 空のカートを削除し取得し直す
'Carts' => $this->cartService->getCarts(true),
'least' => $least,
'quantity' => $quantity,
'is_delivery_free' => $isDeliveryFree,
// ▼ここから追加
'reserved' => implode('|', $arrReserved),
'valied' => implode('|', $arrValied),
'rental_setting' => $_SERVER['RENTAL_SETTING'],
];
}
/**
* カート明細の加算/減算/削除を行う. →こちらをベースにレンタル期間を変更する
*
* - 加算
* - 明細の個数を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':
$this->cartService->removeProduct($ProductClass);
break;
}
// カートを取得して明細の正規化を実行
$Carts = $this->cartService->getCarts();
$this->execPurchaseFlow($Carts);
log_info('カート演算処理終了', ['operation' => $operation, 'product_class_id' => $productClassId]);
return $this->redirectToRoute('cart');
}
/**
* カート明細のレンタル期間を変更する
*
* @Route(
* path="/cart/{cartItemId}/{period}",
* name="cart_item_update_period",
* methods={"PUT"},
* requirements={
* "cartItemId": "\d+"
* }
* )
*/
public function updateCartItemPeriod($cartItemId, $period)
{
log_info('カート明細期間操作開始', ['period' => $period, 'cart_item_id' => $cartItemId]);
$this->isTokenValid();
$period = str_replace(',', '~', $period);
$period = str_replace('-', '/', $period);
$this->cartService->changeCartItemPeriod($cartItemId, $period);
// カートを取得して明細の正規化を実行
$Carts = $this->cartService->getCarts();
$this->execPurchaseFlow($Carts);
log_info('カート明細期間処理終了', ['period' => $period, 'cart_item_id' => $cartItemId]);
return $this->redirectToRoute('cart');
}
}