app/Plugin/SalesReport42/Controller/SalesReportController.php line 49

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Plugin\SalesReport42\Controller;
  13. use Eccube\Controller\AbstractController;
  14. use Plugin\SalesReport42\Form\Type\SalesReportType;
  15. use Plugin\SalesReport42\Service\SalesReportService;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\StreamedResponse;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. /**
  20.  * Class SalesReportController.
  21.  */
  22. class SalesReportController extends AbstractController
  23. {
  24.     /** @var SalesReportService */
  25.     protected $salesReportService;
  26.     /**
  27.      * SalesReportController constructor.
  28.      *
  29.      * @param SalesReportService $salesReportService
  30.      */
  31.     public function __construct(SalesReportService $salesReportService)
  32.     {
  33.         $this->salesReportService $salesReportService;
  34.     }
  35.     /**
  36.      * 期間別集計.
  37.      *
  38.      * @param Request $request
  39.      * @Route("%eccube_admin_route%/plugin/sales_report/term", name="sales_report_admin_term")
  40.      *
  41.      * @return \Symfony\Component\HttpFoundation\Response
  42.      */
  43.     public function term(Request $request)
  44.     {
  45.         return $this->response($request'term');
  46.     }
  47.     /**
  48.      * 商品別集計.
  49.      *
  50.      * @param Request $request
  51.      * @Route("%eccube_admin_route%/plugin/sales_report/product", name="sales_report_admin_product")
  52.      *
  53.      * @return \Symfony\Component\HttpFoundation\Response
  54.      */
  55.     public function product(Request $request)
  56.     {
  57.         return $this->response($request'product');
  58.     }
  59.     /**
  60.      * 年代別集計.
  61.      *
  62.      * @param Request $request
  63.      * @Route("%eccube_admin_route%/plugin/sales_report/age", name="sales_report_admin_age")
  64.      *
  65.      * @return \Symfony\Component\HttpFoundation\Response
  66.      */
  67.     public function age(Request $request)
  68.     {
  69.         return $this->response($request'age');
  70.     }
  71.     /**
  72.      * 商品CSVの出力.
  73.      *
  74.      * @param Request $request
  75.      * @param string $type
  76.      * @Route("%eccube_admin_route%/plugin/sales_report/export/{type}", name="sales_report_admin_export", methods={"POST"})
  77.      *
  78.      * @return StreamedResponse
  79.      */
  80.     public function export(Request $request$type)
  81.     {
  82.         set_time_limit(0);
  83.         $response = new StreamedResponse();
  84.         $session $request->getSession();
  85.         if ($session->has('eccube.admin.sales_report.export')) {
  86.             $searchData $session->get('eccube.admin.sales_report.export');
  87.         } else {
  88.             $searchData = [];
  89.         }
  90.         $data = [
  91.             'graph' => null,
  92.             'raw' => null,
  93.         ];
  94.         // Query data from database
  95.         if ($searchData) {
  96.             if ($searchData['term_end']) {
  97.                 $searchData['term_end'] = $searchData['term_end']->modify('- 1 day');
  98.             }
  99.             $data $this->salesReportService
  100.                 ->setReportType($type)
  101.                 ->setTerm($searchData['term_type'], $searchData)
  102.                 ->getData();
  103.         }
  104.         $response->setCallback(function () use ($data$request$type) {
  105.             $exportSeparator $this->eccubeConfig['eccube_csv_export_separator'];
  106.             $exportEncoding $this->eccubeConfig['eccube_csv_export_encoding'];
  107.             // Export data by type
  108.             switch ($type) {
  109.                 case 'term':
  110.                     $this->salesReportService->exportTermCsv($data['raw'], $exportSeparator$exportEncoding);
  111.                     break;
  112.                 case 'product':
  113.                     $this->salesReportService->exportProductCsv($data['raw'], $exportSeparator$exportEncoding);
  114.                     break;
  115.                 case 'age':
  116.                     $this->salesReportService->exportAgeCsv($data['raw'], $exportSeparator$exportEncoding);
  117.                     break;
  118.                 default:
  119.                     $this->salesReportService->exportTermCsv($data['raw'], $exportSeparator$exportEncoding);
  120.             }
  121.         });
  122.         // Set filename by type
  123.         $now = new \DateTime();
  124.         switch ($type) {
  125.             case 'term':
  126.                 $filename 'salesreport_term_'.$now->format('YmdHis').'.csv';
  127.                 break;
  128.             case 'product':
  129.                 $filename 'salesreport_product_'.$now->format('YmdHis').'.csv';
  130.                 break;
  131.             case 'age':
  132.                 $filename 'salesreport_age_'.$now->format('YmdHis').'.csv';
  133.                 break;
  134.             default:
  135.                 $filename 'salesreport_term_'.$now->format('YmdHis').'.csv';
  136.         }
  137.         $response->headers->set('Content-Type''application/octet-stream;');
  138.         $response->headers->set('Content-Disposition''attachment; filename='.$filename);
  139.         log_info('売上集計CSV出力ファイル名', [$filename]);
  140.         return $response;
  141.     }
  142.     /**
  143.      * direct by report type(default term).
  144.      *
  145.      * @param Request $request
  146.      * @param null $reportType
  147.      *
  148.      * @return \Symfony\Component\HttpFoundation\Response
  149.      */
  150.     private function response(Request $request$reportType null)
  151.     {
  152.         $builder $this->formFactory
  153.             ->createBuilder(SalesReportType::class);
  154.         if (!is_null($reportType) && $reportType !== 'term') {
  155.             $builder->remove('unit');
  156.         }
  157.         /* @var $form \Symfony\Component\Form\Form */
  158.         $form $builder->getForm();
  159.         $form->handleRequest($request);
  160.         $data = [
  161.             'graph' => null,
  162.             'raw' => null,
  163.         ];
  164.         $options = [];
  165.         if (!is_null($reportType) && $form->isSubmitted() && $form->isValid()) {
  166.             $session $request->getSession();
  167.             $searchData $form->getData();
  168.             $searchData['term_type'] = $form->get('term_type')->getData();
  169.             $session->set('eccube.admin.sales_report.export'$searchData);
  170.             $termType $form->get('term_type')->getData();
  171.             $data $this->salesReportService
  172.                 ->setReportType($reportType)
  173.                 ->setTerm($termType$searchData)
  174.                 ->getData();
  175.             $options $this->getRenderOptions($reportType$searchData);
  176.         }
  177.         $template is_null($reportType) ? 'term' $reportType;
  178.         log_info('SalesReport Plugin : render ', ['template' => $template]);
  179.         return $this->render(
  180.             '@SalesReport42/admin/'.$template.'.twig',
  181.             [
  182.                 'form' => $form->createView(),
  183.                 'graphData' => json_encode($data['graph']),
  184.                 'rawData' => $data['raw'],
  185.                 'type' => $reportType,
  186.                 'options' => $options,
  187.             ]
  188.         );
  189.     }
  190.     /**
  191.      * get option params for render.
  192.      *
  193.      * @param $termType
  194.      * @param $searchData
  195.      *
  196.      * @return array options
  197.      */
  198.     private function getRenderOptions($termType$searchData)
  199.     {
  200.         $options = [];
  201.         switch ($termType) {
  202.             case 'term':
  203.                 // 期間の集計単位
  204.                 if (isset($searchData['unit'])) {
  205.                     $options['unit'] = $searchData['unit'];
  206.                 }
  207.                 break;
  208.             default:
  209.                 // no option
  210.                 break;
  211.         }
  212.         return $options;
  213.     }
  214. }