<?php
namespace App\Controller;
use App\Entity\Shop;
use App\Repository\ProductRepository;
use App\Repository\SalesRepository;
use Doctrine\DBAL\Exception;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class StatsController extends AppController
{
/**
* @Route("/stats", name="app_stats_dashboard")
*/
public function tdb(): Response
{
return $this->renderWithParams('stats/index.html.twig', [
'controller_name' => 'StatsController',
]);
}
/**
* @Route("/stats/sale/{year}/{day}/{shopId}", name="app_stats_sale", defaults={"shopId": null, "year": null, "day": null} )
*/
public function saleStats($year, $day, $shopId, SalesRepository $salesRepository, EntityManagerInterface $entityManager): Response
{
if($year == null){
$year = date("Y");
}
if($day == null){
$day = date("Y-m-d");
}
/** @var Shop[] $sites */
$sites = $entityManager->getRepository(Shop::class)->findAll();
$selectedShopId = null;
if($shopId != null){
/** @var Shop $selectedShop */
$selectedShop = $entityManager->getRepository(Shop::class)->find($shopId);
$selectedShopId = $selectedShop->getId();
}
$totalYear = $salesRepository->getTotalYear($selectedShopId, $year);
$totalWeek = $salesRepository->getTotalWeek($day, $selectedShopId);
$numersYear = $salesRepository->getNumbersByMonth($selectedShopId, $year);
$numersWeek = $salesRepository->getNumbersByDay($day, $selectedShopId);
$numersByCat = $salesRepository->getNumbersByCat($selectedShopId, $year);
$yearsFilter = $this->getYearsToNow();
return $this->renderWithParams('stats/sales.html.twig', [
'totalYear' => $totalYear,
'totalWeek' => $totalWeek,
'numbersYear' => $numersYear,
'numbersWeek' => $numersWeek,
'numersByCat' => $numersByCat,
'sites' => $sites,
'selectedSite' => $selectedShopId,
'years' => array_reverse($yearsFilter),
'yearFilterValue' => $year,
'dayFilterValue' => $day
]);
}
/**
* @Route("/stats/draw-daily-sales-chart/{dateFilter}/{shopFilter}", name="app_stats_draw_daily_sales_chart", defaults={"shopFilter":null})
*/
public function drawDailySalesChart($dateFilter, $shopFilter, Request $request, SalesRepository $salesRepository): Response
{
$totalWeek = $salesRepository->getTotalWeek($dateFilter,$shopFilter);
$numersWeek = $salesRepository->getNumbersByDay($dateFilter, $shopFilter);
return new JsonResponse(
json_encode($numersWeek),
200
);
}
/**
* @Route("/stats/bestsellers/{month}/{shopId}", name="app_stats_bestsellers", defaults={"shopId": null,"month": null} )
* @throws Exception
*/
public function bestsellers($month, $shopId, ProductRepository $productRepository, EntityManagerInterface $entityManager): Response
{
$months = $this->getMonthsFilter();
/** @var Shop[] $sites */
$sites = $entityManager->getRepository(Shop::class)->findAll();
$selectedShopId = null;
if($shopId != null){
/** @var Shop $selectedShop */
$selectedShop = $entityManager->getRepository(Shop::class)->find($shopId);
$selectedShopId = $selectedShop->getId();
}
$bestSellers = $productRepository->getNumbersByMonth($month, $selectedShopId);
return $this->renderWithParams('stats/best-sellers.html.twig', [
'numbersYear' => $bestSellers,
'sites' => $sites,
'selectedSite' => $selectedShopId,
'months' => $months,
'selectedMonth' => $month
]);
}
/**
* @Route("/stats/trafic/{shopId}", name="app_stats_trafic", defaults={"shopId": null})
*/
public function trafic($shopId, SalesRepository $salesRepository, EntityManagerInterface $entityManager): Response
{
/** @var Shop[] $sites */
$sites = $entityManager->getRepository(Shop::class)->findAll();
$selectedShopId = null;
if($shopId != null){
/** @var Shop $selectedShop */
$selectedShop = $entityManager->getRepository(Shop::class)->find($shopId);
$selectedShopId = $selectedShop->getId();
}
$chartDays = [];
$chartCat1 = [];
$chartCat2 = [];
$chartCat3 = [];
$chartCat4 = [];
$days = [
'1' => 'Dimanche',
'2' => 'Lundi',
'3' => 'Mardi',
'4' => 'Mercredi',
'5' => 'Jeudi',
'6' => 'Vendredi',
'7' => 'Samedi'
];
$traffic = $salesRepository->getTrafficByDay($selectedShopId);
foreach ($days as $day => $label){
$found = false;
foreach ($traffic as $stat)
{
if ($stat["day"] == $day)
{
$found = true;
$chartDays[] = $label;
$chartCat1[] = $stat['8_12'];
$chartCat2[] = $stat['12_16'];
$chartCat3[] = $stat['16_20'];
$chartCat4[] = $stat['20_00'];
}
}
if(!$found)
{
$chartDays[] = $label;
$chartCat1[] = 0;
$chartCat2[] = 0;
$chartCat3[] = 0;
$chartCat4[] = 0;
}
}
return $this->renderWithParams('stats/trafic.html.twig', [
'days' => $chartDays,
'cat1' => $chartCat1,
'cat2' => $chartCat2,
'cat3' => $chartCat3,
'cat4' => $chartCat4,
'sites' => $sites,
'selectedSite' => $selectedShopId
]);
}
/**
* @Route("/stats/product", name="app_stats_product")
*/
public function product(): Response
{
return $this->renderWithParams('stats/product.html.twig', [
'controller_name' => 'StatsController',
]);
}
/**
* @Route("/stats/benefices", name="app_stats_benefices")
*/
public function benefices(): Response
{
return $this->renderWithParams('stats/benefices.html.twig', [
'controller_name' => 'StatsController',
]);
}
/**
* @return array[]
*/
private function getMonthsFilter()
{
setlocale(LC_TIME, "fr_FR");
$today = new \DateTime();
$months = [
array(
"month" => $today->format('m'),
"year" => $today->format('Y'),
"label" => $today->format('M - Y')
)
];
for($i = 12; $i > 0; $i--)
{
$interval= \DateInterval::createFromDateString('1 month');
$month = $today->sub($interval);
$entry = [];
$entry["month"] = $month->format('m') ;
$entry["year"] = $month->format('Y');
$entry["label"] = $month->format('M - Y');
$months[] = $entry;
}
return $months;
}
private function getYearsToNow(){
$currentYear = date("Y");
return range(2024, $currentYear);
}
}