app/Plugin/SalesRankingB4/Controller/SalesRankingController.php line 31

Open in your IDE?
  1. <?php
  2. namespace Plugin\SalesRankingB4\Controller;
  3. use Eccube\Controller\AbstractController;
  4. use Eccube\Repository\OrderItemRepository;
  5. use Eccube\Repository\ProductRepository;
  6. use Eccube\Entity\Master\ProductStatus;
  7. use Eccube\Entity\Master\OrderStatus;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Plugin\SalesRankingB4\Repository\SalesRankingConfigRepository;
  11. /**
  12.  * Class SalesRankingController.
  13.  */
  14. class SalesRankingController extends AbstractController
  15. {
  16.     /**
  17.      * @Route("/block/sales_ranking_block", name="block_sales_ranking_block")
  18.      * @Template("@SalesRankingB4/Block/sales_ranking_block.twig")
  19.      *
  20.      * @param SalesRankingConfigRepository $configRepository
  21.      *
  22.      * @return array
  23.      */
  24.     public function index(SalesRankingConfigRepository $configRepository)
  25.     {
  26.         $OrderItem $this->container->get(OrderItemRepository::class);
  27.         $Product $this->container->get(ProductRepository::class);
  28.         $excludes = [OrderStatus::CANCELOrderStatus::PENDINGOrderStatus::PROCESSINGOrderStatus::RETURNED];
  29.         $Config $configRepository->get();
  30.         $Date = new \DateTime();
  31.         $end $Date->format("Y-m-d 23:59:59");
  32.         $term "- " $Config->getTerm() . " day";
  33.         $start $Date->modify($term)->format("Y-m-d 00:00:00");
  34.         $query $OrderItem->createQueryBuilder('oi')
  35.             ->select('SUM(oi.quantity) as total,p.id')
  36.             ->innerJoin('Eccube\Entity\Product''p''WITH''p.id = oi.Product')
  37.             ->innerJoin('Eccube\Entity\Order''o''WITH''oi.Order = o')
  38.             ->where('p.Status = :Disp')
  39.             ->andWhere('o.OrderStatus NOT IN (:excludes)')
  40.             ->andWhere('o.create_date >= :start')
  41.             ->andWhere('o.create_date <= :end')
  42.             ->orderBy('total''DESC')
  43.             ->setParameter('Disp'ProductStatus::DISPLAY_SHOW)
  44.             ->setParameter(':excludes'$excludes)
  45.             ->setParameter(':start'$start)
  46.             ->setParameter(':end'$end)
  47.             ->groupBy('p.id')
  48.             ->setMaxResults($Config->getDisplayNum())
  49.             ->getQuery();
  50.         $ranking_products= array();
  51.         $result $query->getResult();
  52.         foreach ($result as $item) {
  53.             $ranking_products[] = $Product->find($item['id']);
  54.         }
  55.         return [
  56.             'ranking_products' => $ranking_products,
  57.             'display_name_flg' => $Config->getDisplayName(),
  58.             'display_code_flg' => $Config->getDisplayCode(),
  59.             'display_price_flg' => $Config->getDisplayPrice(),
  60.             'display_description_list_flg' => $Config->getDisplayDescriptionList(),
  61.         ];
  62.     }
  63. }