src/Controller/HomeController.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Magasins;
  4. use App\Entity\Sales;
  5. use App\Entity\Clients;
  6. use App\Repository\MagasinsRepository;
  7. use App\Repository\SalesRepository;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. class HomeController extends AbstractController
  14. {
  15.     /**
  16.      * @Route("/", name="home")
  17.      */
  18.     public function index(Request $requestSalesRepository $salesRepositoryEntityManagerInterface $entityManager): Response
  19.     {
  20.         if (!$this->getUser()) {
  21.             return $this->redirectToRoute('app_login');
  22.         }
  23.         $searchDate $request->query->get('month')??false;
  24.         // Logique pour vérifier plusieurs rôles
  25.         if (!$this->isGranted('ROLE_MANAGER') && !$this->isGranted('ROLE_SUPERADMIN')) {
  26.             return $this->redirectToRoute('access_denied');
  27.         }
  28.         if ($this->isGranted('ROLE_MANAGER')) {
  29.             // Récupérer le magasin associé à l'utilisateur
  30.             $user $this->getUser();
  31.             $em $this->getDoctrine()->getManager();
  32.             $ListSales $this->getSalesByMonth($em$user$searchDate10);
  33.             $sales $this->getSalesByMonth($em$user$searchDate);
  34.             $salesLastMonth $this->getSalesByLastMonth($em$user$searchDate);
  35.             if (count($salesLastMonth) > 0) {
  36.                 $percentageChange = ((count($sales) - count($salesLastMonth)) / count($salesLastMonth)) * 100;
  37.             } else {
  38.                 $percentageChange count($sales) > 100 0// Si 0 ventes le mois dernier
  39.             }
  40.             
  41.             // Récupérer les clients associé au magasin
  42.             $clients $this->getClientsByMonth($em$user$searchDate);
  43.             $clientsLastMonth $this->getClientsByLastMonth($em$user$searchDate);
  44.             if (count($clientsLastMonth) > 0) {
  45.                 $percentageClientChange = ((count($clients) - count($clientsLastMonth)) / count($clientsLastMonth)) * 100;
  46.             } else {
  47.                 $percentageClientChange count($clients) > 100 0// Si 0 ventes le mois dernier
  48.             }
  49.             // Taux de conversion
  50.             if (count($sales) > 0) {
  51.                 $conversionRate = (count($clients) / count($sales)) * 100;
  52.             } else {
  53.                 $conversionRate 0// Si 0 ventes
  54.             }
  55.             // Taux de converstion last month
  56.             if (count($salesLastMonth) > 0) {
  57.                 $conversionRateLastMonth = (count($clientsLastMonth) / count($salesLastMonth)) * 100;
  58.             } else {
  59.                 $conversionRateLastMonth 0// Si 0 ventes le mois dernier
  60.             }
  61.             // Calcul du Ticket Moyen
  62.             $ticketMoyen $this->getTicketsMoyenByMonth($em$user$searchDate);
  63.             $ticketMoyenLastMonth $this->getTicketsMoyenByLastMonth($em$user$searchDate);
  64.             // Calcul du Ticket Moyen last month en pourcentage
  65.             if ($ticketMoyenLastMonth 0) {
  66.                 $percentageTicketMoyenChange = (($ticketMoyen $ticketMoyenLastMonth) / $ticketMoyenLastMonth) * 100;
  67.             } else {
  68.                 $percentageTicketMoyenChange $ticketMoyen 100 0// Si 0 ventes le mois dernier
  69.             }
  70.             /**/
  71.             if ($searchDate) {
  72.                 $start = new \DateTime("$searchDate-01");
  73.                 $start->setTime(000);
  74.     
  75.                 $lastDay = new \DateTime("$searchDate-01");
  76.                 $end $lastDay->modify('last day of this month');
  77.                 $end->setTime(235959);
  78.             } else {
  79.                 $start = new \DateTime('first day of this month');
  80.                 $start->setTime(000);  // Début du mois à 00:00:00
  81.     
  82.                 $end = new \DateTime('last day of this month');
  83.                 $end->setTime(235959);  // Fin du mois à 23:59:59
  84.             }
  85.             $sql "
  86.                 SELECT DATE(s.date_add) AS day, COUNT(s.id) AS order_count
  87.                 FROM sales s
  88.                 WHERE s.magasin_id = :magasin AND s.date_add BETWEEN :start AND :end
  89.                 GROUP BY day
  90.                 ORDER BY day ASC
  91.             ";
  92.             $conn $entityManager->getConnection();
  93.             $result $conn->executeQuery($sql, [
  94.                 'magasin' => $user->getMagasin()->getId(),
  95.                 'start' => $start->format('Y-m-d H:i:s'),
  96.                 'end' => $end->format('Y-m-d H:i:s')
  97.             ]);
  98.             // Récupérer les résultats avec fetchAllAssociative() après executeQuery
  99.             $nbrOrders $result->fetchAllAssociative();
  100.         }
  101.         return $this->render('dashboard/index.html.twig', [
  102.             'controller_name' => 'HomeController',
  103.             'date_current' => date('Y-m'),
  104.             'sales' => [
  105.                 'total' => $sales??[],
  106.                 'list' => $ListSales??[],
  107.                 'lastMonth' => $salesLastMonth??[],
  108.                 'percentage' => $percentageChange
  109.             ],
  110.             'clients' => [
  111.                 'list' => $clients??[],
  112.                 'lastMonth' => $clientsLastMonth??[],
  113.                 'percentage' => $percentageClientChange
  114.             ],
  115.             'nbrOrders' => $nbrOrders,
  116.             'ticketMoyen' => $ticketMoyen??0,
  117.             'percentageTicketMoyenChange' => $percentageTicketMoyenChange??0,
  118.             'conversionRate' => $conversionRate??0,
  119.             'conversionRateLastMonth' => $conversionRateLastMonth??0,
  120.             //'margeBrute' => $margeBrute
  121.         ]);
  122.     }
  123.     
  124.     public function getSalesByMonth(EntityManagerInterface $em$user$searchDate$limit false){
  125.         
  126.         if ($searchDate) {
  127.             $start = new \DateTime("$searchDate-01");
  128.             $start->setTime(000);
  129.             $lastDay = new \DateTime("$searchDate-01");
  130.             $end $lastDay->modify('last day of this month');
  131.             $end->setTime(235959);
  132.         } else {
  133.             $start = new \DateTime('first day of this month');
  134.             $start->setTime(000);  // Début du mois à 00:00:00
  135.             $end = new \DateTime('last day of this month');
  136.             $end->setTime(235959);  // Fin du mois à 23:59:59
  137.         }
  138.         $qb $em->getRepository(Sales::class)->createQueryBuilder('s');
  139.         $qb->where('s.magasin = :magasin')
  140.             ->andWhere('s.date_add BETWEEN :start AND :end')
  141.             ->orderBy('s.date_add''DESC')
  142.             ->setParameter('magasin'$user->getMagasin())
  143.             ->setParameter('start'$start->format('Y-m-d H:i:s'))
  144.             ->setParameter('end'$end->format('Y-m-d H:i:s'));
  145.         if ($limit) {
  146.             $qb->setMaxResults($limit);
  147.         }
  148.         return $qb->getQuery()->getResult();
  149.     }
  150.     public function getSalesByLastMonth(EntityManagerInterface $em$user$searchDate){
  151.         
  152.         if ($searchDate) {
  153.             $firstDayLastMonth = new \DateTime("$searchDate-01"); 
  154.             $start $firstDayLastMonth->modify('-1 month'); // Reculer d'un mois
  155.             $start->setTime(000);  // Début du mois à 00:00:00
  156.             $lastDayLastMonth = clone $firstDayLastMonth;
  157.             $end $lastDayLastMonth->modify('last day of this month');
  158.             $end->setTime(235959);  // Fin du mois à 23:59:59
  159.         } else {    
  160.             $start = new \DateTime('first day of last month');
  161.             $start->setTime(000);  // Début du mois à 00:00:00
  162.             $end = new \DateTime('last day of last month');
  163.             $end->setTime(235959);  // Fin du mois à 23:59:59
  164.         }
  165.         $qb $em->getRepository(Sales::class)->createQueryBuilder('s');
  166.         $qb->where('s.magasin = :magasin')
  167.             ->andWhere('s.date_add BETWEEN :start AND :end')
  168.             ->setParameter('magasin'$user->getMagasin())
  169.             ->setParameter('start'$start->format('Y-m-d H:i:s'))
  170.             ->setParameter('end'$end->format('Y-m-d H:i:s'));
  171.         return $qb->getQuery()->getResult();
  172.     }
  173.     public function getClientsByMonth(EntityManagerInterface $em$user$searchDate){
  174.         
  175.         if ($searchDate) {
  176.             $start = new \DateTime("$searchDate-01");
  177.             $start->setTime(000);
  178.             $lastDay = new \DateTime("$searchDate-01");
  179.             $end $lastDay->modify('last day of this month');
  180.             $end->setTime(235959);
  181.         } else {
  182.             $start = new \DateTime('first day of this month');
  183.             $start->setTime(000);  // Début du mois à 00:00:00
  184.             $end = new \DateTime('last day of this month');
  185.             $end->setTime(235959);  // Fin du mois à 23:59:59
  186.         }
  187.         $qb $em->getRepository(Clients::class)->createQueryBuilder('s');
  188.         $qb->where('s.magasin = :magasin')
  189.             ->andWhere('s.date_add BETWEEN :start AND :end')
  190.             ->setParameter('magasin'$user->getMagasin())
  191.             ->setParameter('start'$start->format('Y-m-d H:i:s'))
  192.             ->setParameter('end'$end->format('Y-m-d H:i:s'));
  193.         return $qb->getQuery()->getResult();
  194.     }
  195.     public function getClientsByLastMonth(EntityManagerInterface $em$user$searchDate){
  196.         
  197.         if ($searchDate) {
  198.             $firstDayLastMonth = new \DateTime("$searchDate-01"); 
  199.             $start $firstDayLastMonth->modify('-1 month'); // Reculer d'un mois
  200.             $start->setTime(000);  // Début du mois à 00:00:00
  201.             $lastDayLastMonth = clone $firstDayLastMonth;
  202.             $end $lastDayLastMonth->modify('last day of this month');
  203.             $end->setTime(235959);  // Fin du mois à 23:59:59
  204.         } else { 
  205.             $start = new \DateTime('first day of last month');
  206.             $start->setTime(000);  // Début du mois à 00:00:00
  207.             $end = new \DateTime('last day of last month');
  208.             $end->setTime(235959);  // Fin du mois à 23:59:59
  209.         }
  210.         $qb $em->getRepository(Clients::class)->createQueryBuilder('s');
  211.         $qb->where('s.magasin = :magasin')
  212.             ->andWhere('s.date_add BETWEEN :start AND :end')
  213.             ->setParameter('magasin'$user->getMagasin())
  214.             ->setParameter('start'$start->format('Y-m-d H:i:s'))
  215.             ->setParameter('end'$end->format('Y-m-d H:i:s'));
  216.         return $qb->getQuery()->getResult();
  217.     }
  218.     public function getTicketsMoyenByMonth(EntityManagerInterface $em$user$searchDate){
  219.         
  220.         if ($searchDate) {
  221.             $start = new \DateTime("$searchDate-01");
  222.             $start->setTime(000);
  223.             $lastDay = new \DateTime("$searchDate-01");
  224.             $end $lastDay->modify('last day of this month');
  225.             $end->setTime(235959);
  226.         } else {
  227.             $start = new \DateTime('first day of this month');
  228.             $start->setTime(000);  // Début du mois à 00:00:00
  229.             $end = new \DateTime('last day of this month');
  230.             $end->setTime(235959);  // Fin du mois à 23:59:59
  231.         }
  232.         $qb $em->getRepository(Sales::class)->createQueryBuilder('s');
  233.         $qb->leftJoin('s.sale_details''sd'// Assurez-vous que la relation est bien définie dans l'entité Sales
  234.         ->select('SUM(sd.price) / COUNT(DISTINCT s.id) as ticket_moyen')
  235.         ->where('s.magasin = :magasin')
  236.         ->andWhere('s.date_add BETWEEN :start AND :end')
  237.         ->setParameter('magasin'$user->getMagasin())
  238.         ->setParameter('start'$start->format('Y-m-d H:i:s'))
  239.         ->setParameter('end'$end->format('Y-m-d H:i:s'));
  240.         return $qb->getQuery()->getSingleScalarResult();
  241.     }
  242.     public function getTicketsMoyenByLastMonth(EntityManagerInterface $em$user$searchDate){
  243.         
  244.         if ($searchDate) {
  245.             $firstDayLastMonth = new \DateTime("$searchDate-01"); 
  246.             $start $firstDayLastMonth->modify('-1 month'); // Reculer d'un mois
  247.             $start->setTime(000);  // Début du mois à 00:00:00
  248.             $lastDayLastMonth = clone $firstDayLastMonth;
  249.             $end $lastDayLastMonth->modify('last day of this month');
  250.             $end->setTime(235959);  // Fin du mois à 23:59:59
  251.         } else {
  252.             $start = new \DateTime('first day of last month');
  253.             $start->setTime(000);  // Début du mois à 00:00:00
  254.             $end = new \DateTime('last day of last month');        
  255.             $end->setTime(235959);  // Fin du mois à 23:59:59
  256.         }
  257.         $qb $em->getRepository(Sales::class)->createQueryBuilder('s');
  258.         $qb->leftJoin('s.sale_details''sd'// Assurez-vous que la relation est bien définie dans l'entité Sales
  259.         ->select('SUM(sd.price) / COUNT(DISTINCT s.id) as ticket_moyen')
  260.         ->where('s.magasin = :magasin')
  261.         ->andWhere('s.date_add BETWEEN :start AND :end')
  262.         ->setParameter('magasin'$user->getMagasin())
  263.         ->setParameter('start'$start->format('Y-m-d H:i:s'))
  264.         ->setParameter('end'$end->format('Y-m-d H:i:s'));
  265.         return $qb->getQuery()->getSingleScalarResult();
  266.     }
  267. }