<?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 Customize\Event\EccubeEvents;
use Eccube\Entity\Customer;
use Customize\Entity\AuctionResult;
use Customize\Form\Type\SearchSalesType;
use Customize\Repository\AuctionResultRepository;
use Customize\Repository\BitHistoryRepository;
use Eccube\Repository\CustomerRepository;
use Customize\Repository\ProductRepository;
use Eccube\Controller\AbstractController;
use Eccube\Entity\Product;
use Eccube\Event\EventArgs;
use Eccube\Repository\CustomerFavoriteProductRepository;
use Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination;
use Knp\Component\Pager\PaginatorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class SalesController extends AbstractController
{
/**
* @var AuctionResultRepository
*/
protected $auctionResultRepository;
/**
* @var BitHistoryRepository
*/
protected $bitHistoryRepository;
/**
* @var CustomerFavoriteProductRepository
*/
protected $customerFavoriteProductRepository;
/**
* @var ProductRepository
*/
protected $productRepository;
/**
* @var CustomerRepository
*/
protected $customerRepository;
/**
* ListingController constructor.
*/
public function __construct(
ProductRepository $productRepository,
AuctionResultRepository $auctionResultRepository,
BitHistoryRepository $bitHistoryRepository,
CustomerRepository $customerRepository
) {
$this->productRepository = $productRepository;
$this->auctionResultRepository = $auctionResultRepository;
$this->bitHistoryRepository = $bitHistoryRepository;
$this->customerRepository = $customerRepository;
}
/**
* 売上一覧画面.
*
* @Route("/sales/list", name="sales_list", methods={"GET"})
*
* @Template("Product/sales.twig")
*/
public function index(Request $request, PaginatorInterface $paginator)
{
if ($this->isGranted('ROLE_USER')) {
// handleRequestは空のqueryの場合は無視するため
if ($request->getMethod() === 'GET') {
$request->query->set('pageno', $request->query->get('pageno', ''));
$request->query->set('orderby', $request->query->get('orderby', 3));
}
// searchForm
/* @var $builder \Symfony\Component\Form\FormBuilderInterface */
$builder = $this->formFactory->createNamedBuilder('', SearchSalesType::class);
$Customer = $this->getUser();
if ($request->getMethod() === 'GET') {
$builder->setMethod('GET');
}
$event = new EventArgs(
[
'builder' => $builder,
],
$request
);
$this->eventDispatcher->dispatch($event, EccubeEvents::FRONT_PRODUCT_INDEX_INITIALIZE);
/* @var $searchForm \Symfony\Component\Form\FormInterface */
$searchForm = $builder->getForm();
$searchForm->handleRequest($request);
// paginator
$searchData = $searchForm->getData();
$searchData['Customer'] = $Customer;
$qb = $this->productRepository->getQueryBuilderBySalesSearchData($searchData);
$event = new EventArgs(
[
'searchData' => $searchData,
'qb' => $qb,
],
$request
);
$this->eventDispatcher->dispatch($event, EccubeEvents::FRONT_PRODUCT_INDEX_SEARCH);
$searchData = $event->getArgument('searchData');
$query = $qb->getQuery()
->useResultCache(true, $this->eccubeConfig['eccube_result_cache_lifetime_short']);
/** @var SlidingPagination $pagination */
$pagination = $paginator->paginate(
$query,
! empty($searchData['pageno']) ? $searchData['pageno'] : 1,
! empty($searchData['disp_number']) ? $searchData['disp_number'] : 20
);
$Products = $qb->getQuery()->getResult();
$biddingCompaniesCounts = [];
$productIds = array_map(function ($Product) {
return $Product->getId();
}, $Products);
$biddingCompaniesResults = $this->bitHistoryRepository->countBiddingCompaniesForProducts($productIds);
foreach ($biddingCompaniesResults as $result) {
$biddingCompaniesCounts[$result['productId']] = $result['count'];
}
foreach ($pagination as $Product) {
// もし結果が存在しない場合は 0 をセット
$biddingCompaniesCounts[$Product->getId()] = $biddingCompaniesCounts[$Product->getId()] ?? 0;
}
$totalPrice = $this->productRepository->getTotalSalesPrice($Customer, $searchData["auction"]);
$totalCount = count($pagination);
return [
'subtitle' => '売上一覧',
'pagination' => $pagination,
'search_form' => $searchForm->createView(),
'Customer' => $Customer,
'biddingCompaniesCounts' => $biddingCompaniesCounts,
'totalPrice' => $totalPrice,
'totalCount' => $totalCount
];
} else {
// 非会員の場合、ログイン画面を表示
return $this->redirectToRoute('mypage_login');
}
}
protected function getPageTitle($searchData)
{
if (isset($searchData['name']) && ! empty($searchData['name'])) {
return trans('front.auction.search_result');
} elseif (isset($searchData['category_id']) && $searchData['category_id']) {
return $searchData['category_id']->getName();
} else {
return trans('front.auction.all_auctions');
}
}
}