src/Controller/StatsController.php line 57

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Shop;
  4. use App\Repository\ProductRepository;
  5. use App\Repository\SalesRepository;
  6. use Doctrine\DBAL\Exception;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. class StatsController extends AppController
  14. {
  15.     /**
  16.      * @Route("/stats", name="app_stats_dashboard")
  17.      */
  18.     public function tdb(): Response
  19.     {
  20.         return $this->renderWithParams('stats/index.html.twig', [
  21.             'controller_name' => 'StatsController',
  22.         ]);
  23.     }
  24.     /**
  25.      * @Route("/stats/sale/{year}/{day}/{shopId}", name="app_stats_sale", defaults={"shopId": null, "year": null, "day": null} )
  26.      */
  27.     public function saleStats($year$day$shopIdSalesRepository $salesRepositoryEntityManagerInterface $entityManager): Response
  28.     {
  29.         if($year == null){
  30.             $year date("Y");
  31.         }
  32.         if($day == null){
  33.             $day date("Y-m-d");
  34.         }
  35.         /** @var Shop[] $sites */
  36.         $sites $entityManager->getRepository(Shop::class)->findAll();
  37.         $selectedShopId null;
  38.         if($shopId != null){
  39.             /** @var Shop $selectedShop */
  40.             $selectedShop $entityManager->getRepository(Shop::class)->find($shopId);
  41.             $selectedShopId $selectedShop->getId();
  42.         }
  43.         $totalYear $salesRepository->getTotalYear($selectedShopId$year);
  44.         $totalWeek $salesRepository->getTotalWeek($day$selectedShopId);
  45.         $numersYear $salesRepository->getNumbersByMonth($selectedShopId$year);
  46.         $numersWeek $salesRepository->getNumbersByDay($day$selectedShopId);
  47.         $numersByCat $salesRepository->getNumbersByCat($selectedShopId$year);
  48.         $yearsFilter $this->getYearsToNow();
  49.         return $this->renderWithParams('stats/sales.html.twig', [
  50.             'totalYear' => $totalYear,
  51.             'totalWeek' => $totalWeek,
  52.             'numbersYear' => $numersYear,
  53.             'numbersWeek' => $numersWeek,
  54.             'numersByCat' => $numersByCat,
  55.             'sites' => $sites,
  56.             'selectedSite' => $selectedShopId,
  57.             'years' => array_reverse($yearsFilter),
  58.             'yearFilterValue' => $year,
  59.             'dayFilterValue' => $day
  60.         ]);
  61.     }
  62.     /**
  63.      * @Route("/stats/draw-daily-sales-chart/{dateFilter}/{shopFilter}", name="app_stats_draw_daily_sales_chart", defaults={"shopFilter":null})
  64.      */
  65.     public function drawDailySalesChart($dateFilter$shopFilterRequest $requestSalesRepository $salesRepository): Response
  66.     {
  67.         $totalWeek $salesRepository->getTotalWeek($dateFilter,$shopFilter);
  68.         $numersWeek $salesRepository->getNumbersByDay($dateFilter$shopFilter);
  69.         return new JsonResponse(
  70.             json_encode($numersWeek),
  71.             200
  72.         );
  73.     }
  74.     /**
  75.      * @Route("/stats/bestsellers/{month}/{shopId}", name="app_stats_bestsellers", defaults={"shopId": null,"month": null} )
  76.      * @throws Exception
  77.      */
  78.     public function bestsellers($month$shopIdProductRepository $productRepositoryEntityManagerInterface $entityManager): Response
  79.     {
  80.         $months $this->getMonthsFilter();
  81.         /** @var Shop[] $sites */
  82.         $sites $entityManager->getRepository(Shop::class)->findAll();
  83.         $selectedShopId null;
  84.         if($shopId != null){
  85.             /** @var Shop $selectedShop */
  86.             $selectedShop $entityManager->getRepository(Shop::class)->find($shopId);
  87.             $selectedShopId $selectedShop->getId();
  88.         }
  89.         $bestSellers $productRepository->getNumbersByMonth($month$selectedShopId);
  90.         return $this->renderWithParams('stats/best-sellers.html.twig', [
  91.             'numbersYear' => $bestSellers,
  92.             'sites' => $sites,
  93.             'selectedSite' => $selectedShopId,
  94.             'months' => $months,
  95.             'selectedMonth' => $month
  96.         ]);
  97.     }
  98.     /**
  99.      * @Route("/stats/trafic/{shopId}", name="app_stats_trafic", defaults={"shopId": null})
  100.      */
  101.     public function trafic($shopIdSalesRepository $salesRepositoryEntityManagerInterface $entityManager): Response
  102.     {
  103.         /** @var Shop[] $sites */
  104.         $sites $entityManager->getRepository(Shop::class)->findAll();
  105.         $selectedShopId null;
  106.         if($shopId != null){
  107.             /** @var Shop $selectedShop */
  108.             $selectedShop $entityManager->getRepository(Shop::class)->find($shopId);
  109.             $selectedShopId $selectedShop->getId();
  110.         }
  111.         $chartDays = [];
  112.         $chartCat1 = [];
  113.         $chartCat2 = [];
  114.         $chartCat3 = [];
  115.         $chartCat4 = [];
  116.         $days = [
  117.             '1' => 'Dimanche',
  118.             '2' => 'Lundi',
  119.             '3' => 'Mardi',
  120.             '4' => 'Mercredi',
  121.             '5' => 'Jeudi',
  122.             '6' => 'Vendredi',
  123.             '7' => 'Samedi'
  124.         ];
  125.         $traffic $salesRepository->getTrafficByDay($selectedShopId);
  126.         foreach ($days as $day => $label){
  127.             $found false;
  128.             foreach ($traffic as $stat)
  129.             {
  130.                 if ($stat["day"] == $day)
  131.                 {
  132.                     $found true;
  133.                     $chartDays[] = $label;
  134.                     $chartCat1[] = $stat['8_12'];
  135.                     $chartCat2[] = $stat['12_16'];
  136.                     $chartCat3[] = $stat['16_20'];
  137.                     $chartCat4[] = $stat['20_00'];
  138.                 }
  139.             }
  140.             if(!$found)
  141.             {
  142.                 $chartDays[] = $label;
  143.                 $chartCat1[] = 0;
  144.                 $chartCat2[] = 0;
  145.                 $chartCat3[] = 0;
  146.                 $chartCat4[] = 0;
  147.             }
  148.         }
  149.         return $this->renderWithParams('stats/trafic.html.twig', [
  150.             'days' => $chartDays,
  151.             'cat1' => $chartCat1,
  152.             'cat2' => $chartCat2,
  153.             'cat3' => $chartCat3,
  154.             'cat4' => $chartCat4,
  155.             'sites' => $sites,
  156.             'selectedSite' => $selectedShopId
  157.         ]);
  158.     }
  159.     /**
  160.      * @Route("/stats/product", name="app_stats_product")
  161.      */
  162.     public function product(): Response
  163.     {
  164.         return $this->renderWithParams('stats/product.html.twig', [
  165.             'controller_name' => 'StatsController',
  166.         ]);
  167.     }
  168.     /**
  169.      * @Route("/stats/benefices", name="app_stats_benefices")
  170.      */
  171.     public function benefices(): Response
  172.     {
  173.         return $this->renderWithParams('stats/benefices.html.twig', [
  174.             'controller_name' => 'StatsController',
  175.         ]);
  176.     }
  177.     /**
  178.      * @return array[]
  179.      */
  180.     private function getMonthsFilter()
  181.     {
  182.         setlocale(LC_TIME"fr_FR");
  183.         $today = new \DateTime();
  184.         $months = [
  185.             array(
  186.                 "month" => $today->format('m'),
  187.                 "year" => $today->format('Y'),
  188.                 "label" => $today->format('M - Y')
  189.             )
  190.         ];
  191.         for($i 12$i 0$i--)
  192.         {
  193.             $interval\DateInterval::createFromDateString('1 month');
  194.             $month $today->sub($interval);
  195.             $entry = [];
  196.             $entry["month"] = $month->format('m') ;
  197.             $entry["year"] = $month->format('Y');
  198.             $entry["label"] = $month->format('M - Y');
  199.             $months[] = $entry;
  200.         }
  201.         return $months;
  202.     }
  203.     private function getYearsToNow(){
  204.         $currentYear date("Y");
  205.         return range(2024$currentYear);
  206.     }
  207. }