<?php
namespace App\Controller;
use App\Entity\Magasins;
use App\Entity\Sales;
use App\Entity\Clients;
use App\Repository\MagasinsRepository;
use App\Repository\SalesRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class HomeController extends AbstractController
{
/**
* @Route("/", name="home")
*/
public function index(Request $request, SalesRepository $salesRepository, EntityManagerInterface $entityManager): Response
{
if (!$this->getUser()) {
return $this->redirectToRoute('app_login');
}
$searchDate = $request->query->get('month')??false;
// Logique pour vérifier plusieurs rôles
if (!$this->isGranted('ROLE_MANAGER') && !$this->isGranted('ROLE_SUPERADMIN')) {
return $this->redirectToRoute('access_denied');
}
if ($this->isGranted('ROLE_MANAGER')) {
// Récupérer le magasin associé à l'utilisateur
$user = $this->getUser();
$em = $this->getDoctrine()->getManager();
$ListSales = $this->getSalesByMonth($em, $user, $searchDate, 10);
$sales = $this->getSalesByMonth($em, $user, $searchDate);
$salesLastMonth = $this->getSalesByLastMonth($em, $user, $searchDate);
if (count($salesLastMonth) > 0) {
$percentageChange = ((count($sales) - count($salesLastMonth)) / count($salesLastMonth)) * 100;
} else {
$percentageChange = count($sales) > 0 ? 100 : 0; // Si 0 ventes le mois dernier
}
// Récupérer les clients associé au magasin
$clients = $this->getClientsByMonth($em, $user, $searchDate);
$clientsLastMonth = $this->getClientsByLastMonth($em, $user, $searchDate);
if (count($clientsLastMonth) > 0) {
$percentageClientChange = ((count($clients) - count($clientsLastMonth)) / count($clientsLastMonth)) * 100;
} else {
$percentageClientChange = count($clients) > 0 ? 100 : 0; // Si 0 ventes le mois dernier
}
// Taux de conversion
if (count($sales) > 0) {
$conversionRate = (count($clients) / count($sales)) * 100;
} else {
$conversionRate = 0; // Si 0 ventes
}
// Taux de converstion last month
if (count($salesLastMonth) > 0) {
$conversionRateLastMonth = (count($clientsLastMonth) / count($salesLastMonth)) * 100;
} else {
$conversionRateLastMonth = 0; // Si 0 ventes le mois dernier
}
// Calcul du Ticket Moyen
$ticketMoyen = $this->getTicketsMoyenByMonth($em, $user, $searchDate);
$ticketMoyenLastMonth = $this->getTicketsMoyenByLastMonth($em, $user, $searchDate);
// Calcul du Ticket Moyen last month en pourcentage
if ($ticketMoyenLastMonth > 0) {
$percentageTicketMoyenChange = (($ticketMoyen - $ticketMoyenLastMonth) / $ticketMoyenLastMonth) * 100;
} else {
$percentageTicketMoyenChange = $ticketMoyen > 0 ? 100 : 0; // Si 0 ventes le mois dernier
}
/**/
if ($searchDate) {
$start = new \DateTime("$searchDate-01");
$start->setTime(0, 0, 0);
$lastDay = new \DateTime("$searchDate-01");
$end = $lastDay->modify('last day of this month');
$end->setTime(23, 59, 59);
} else {
$start = new \DateTime('first day of this month');
$start->setTime(0, 0, 0); // Début du mois à 00:00:00
$end = new \DateTime('last day of this month');
$end->setTime(23, 59, 59); // Fin du mois à 23:59:59
}
$sql = "
SELECT DATE(s.date_add) AS day, COUNT(s.id) AS order_count
FROM sales s
WHERE s.magasin_id = :magasin AND s.date_add BETWEEN :start AND :end
GROUP BY day
ORDER BY day ASC
";
$conn = $entityManager->getConnection();
$result = $conn->executeQuery($sql, [
'magasin' => $user->getMagasin()->getId(),
'start' => $start->format('Y-m-d H:i:s'),
'end' => $end->format('Y-m-d H:i:s')
]);
// Récupérer les résultats avec fetchAllAssociative() après executeQuery
$nbrOrders = $result->fetchAllAssociative();
}
return $this->render('dashboard/index.html.twig', [
'controller_name' => 'HomeController',
'date_current' => date('Y-m'),
'sales' => [
'total' => $sales??[],
'list' => $ListSales??[],
'lastMonth' => $salesLastMonth??[],
'percentage' => $percentageChange
],
'clients' => [
'list' => $clients??[],
'lastMonth' => $clientsLastMonth??[],
'percentage' => $percentageClientChange
],
'nbrOrders' => $nbrOrders,
'ticketMoyen' => $ticketMoyen??0,
'percentageTicketMoyenChange' => $percentageTicketMoyenChange??0,
'conversionRate' => $conversionRate??0,
'conversionRateLastMonth' => $conversionRateLastMonth??0,
//'margeBrute' => $margeBrute
]);
}
public function getSalesByMonth(EntityManagerInterface $em, $user, $searchDate, $limit = false){
if ($searchDate) {
$start = new \DateTime("$searchDate-01");
$start->setTime(0, 0, 0);
$lastDay = new \DateTime("$searchDate-01");
$end = $lastDay->modify('last day of this month');
$end->setTime(23, 59, 59);
} else {
$start = new \DateTime('first day of this month');
$start->setTime(0, 0, 0); // Début du mois à 00:00:00
$end = new \DateTime('last day of this month');
$end->setTime(23, 59, 59); // Fin du mois à 23:59:59
}
$qb = $em->getRepository(Sales::class)->createQueryBuilder('s');
$qb->where('s.magasin = :magasin')
->andWhere('s.date_add BETWEEN :start AND :end')
->orderBy('s.date_add', 'DESC')
->setParameter('magasin', $user->getMagasin())
->setParameter('start', $start->format('Y-m-d H:i:s'))
->setParameter('end', $end->format('Y-m-d H:i:s'));
if ($limit) {
$qb->setMaxResults($limit);
}
return $qb->getQuery()->getResult();
}
public function getSalesByLastMonth(EntityManagerInterface $em, $user, $searchDate){
if ($searchDate) {
$firstDayLastMonth = new \DateTime("$searchDate-01");
$start = $firstDayLastMonth->modify('-1 month'); // Reculer d'un mois
$start->setTime(0, 0, 0); // Début du mois à 00:00:00
$lastDayLastMonth = clone $firstDayLastMonth;
$end = $lastDayLastMonth->modify('last day of this month');
$end->setTime(23, 59, 59); // Fin du mois à 23:59:59
} else {
$start = new \DateTime('first day of last month');
$start->setTime(0, 0, 0); // Début du mois à 00:00:00
$end = new \DateTime('last day of last month');
$end->setTime(23, 59, 59); // Fin du mois à 23:59:59
}
$qb = $em->getRepository(Sales::class)->createQueryBuilder('s');
$qb->where('s.magasin = :magasin')
->andWhere('s.date_add BETWEEN :start AND :end')
->setParameter('magasin', $user->getMagasin())
->setParameter('start', $start->format('Y-m-d H:i:s'))
->setParameter('end', $end->format('Y-m-d H:i:s'));
return $qb->getQuery()->getResult();
}
public function getClientsByMonth(EntityManagerInterface $em, $user, $searchDate){
if ($searchDate) {
$start = new \DateTime("$searchDate-01");
$start->setTime(0, 0, 0);
$lastDay = new \DateTime("$searchDate-01");
$end = $lastDay->modify('last day of this month');
$end->setTime(23, 59, 59);
} else {
$start = new \DateTime('first day of this month');
$start->setTime(0, 0, 0); // Début du mois à 00:00:00
$end = new \DateTime('last day of this month');
$end->setTime(23, 59, 59); // Fin du mois à 23:59:59
}
$qb = $em->getRepository(Clients::class)->createQueryBuilder('s');
$qb->where('s.magasin = :magasin')
->andWhere('s.date_add BETWEEN :start AND :end')
->setParameter('magasin', $user->getMagasin())
->setParameter('start', $start->format('Y-m-d H:i:s'))
->setParameter('end', $end->format('Y-m-d H:i:s'));
return $qb->getQuery()->getResult();
}
public function getClientsByLastMonth(EntityManagerInterface $em, $user, $searchDate){
if ($searchDate) {
$firstDayLastMonth = new \DateTime("$searchDate-01");
$start = $firstDayLastMonth->modify('-1 month'); // Reculer d'un mois
$start->setTime(0, 0, 0); // Début du mois à 00:00:00
$lastDayLastMonth = clone $firstDayLastMonth;
$end = $lastDayLastMonth->modify('last day of this month');
$end->setTime(23, 59, 59); // Fin du mois à 23:59:59
} else {
$start = new \DateTime('first day of last month');
$start->setTime(0, 0, 0); // Début du mois à 00:00:00
$end = new \DateTime('last day of last month');
$end->setTime(23, 59, 59); // Fin du mois à 23:59:59
}
$qb = $em->getRepository(Clients::class)->createQueryBuilder('s');
$qb->where('s.magasin = :magasin')
->andWhere('s.date_add BETWEEN :start AND :end')
->setParameter('magasin', $user->getMagasin())
->setParameter('start', $start->format('Y-m-d H:i:s'))
->setParameter('end', $end->format('Y-m-d H:i:s'));
return $qb->getQuery()->getResult();
}
public function getTicketsMoyenByMonth(EntityManagerInterface $em, $user, $searchDate){
if ($searchDate) {
$start = new \DateTime("$searchDate-01");
$start->setTime(0, 0, 0);
$lastDay = new \DateTime("$searchDate-01");
$end = $lastDay->modify('last day of this month');
$end->setTime(23, 59, 59);
} else {
$start = new \DateTime('first day of this month');
$start->setTime(0, 0, 0); // Début du mois à 00:00:00
$end = new \DateTime('last day of this month');
$end->setTime(23, 59, 59); // Fin du mois à 23:59:59
}
$qb = $em->getRepository(Sales::class)->createQueryBuilder('s');
$qb->leftJoin('s.sale_details', 'sd') // Assurez-vous que la relation est bien définie dans l'entité Sales
->select('SUM(sd.price) / COUNT(DISTINCT s.id) as ticket_moyen')
->where('s.magasin = :magasin')
->andWhere('s.date_add BETWEEN :start AND :end')
->setParameter('magasin', $user->getMagasin())
->setParameter('start', $start->format('Y-m-d H:i:s'))
->setParameter('end', $end->format('Y-m-d H:i:s'));
return $qb->getQuery()->getSingleScalarResult();
}
public function getTicketsMoyenByLastMonth(EntityManagerInterface $em, $user, $searchDate){
if ($searchDate) {
$firstDayLastMonth = new \DateTime("$searchDate-01");
$start = $firstDayLastMonth->modify('-1 month'); // Reculer d'un mois
$start->setTime(0, 0, 0); // Début du mois à 00:00:00
$lastDayLastMonth = clone $firstDayLastMonth;
$end = $lastDayLastMonth->modify('last day of this month');
$end->setTime(23, 59, 59); // Fin du mois à 23:59:59
} else {
$start = new \DateTime('first day of last month');
$start->setTime(0, 0, 0); // Début du mois à 00:00:00
$end = new \DateTime('last day of last month');
$end->setTime(23, 59, 59); // Fin du mois à 23:59:59
}
$qb = $em->getRepository(Sales::class)->createQueryBuilder('s');
$qb->leftJoin('s.sale_details', 'sd') // Assurez-vous que la relation est bien définie dans l'entité Sales
->select('SUM(sd.price) / COUNT(DISTINCT s.id) as ticket_moyen')
->where('s.magasin = :magasin')
->andWhere('s.date_add BETWEEN :start AND :end')
->setParameter('magasin', $user->getMagasin())
->setParameter('start', $start->format('Y-m-d H:i:s'))
->setParameter('end', $end->format('Y-m-d H:i:s'));
return $qb->getQuery()->getSingleScalarResult();
}
}