<?php
namespace App\Controller;
use DateTime;
use App\Entity\BillingCenter;
use App\Form\FromToType;
use App\Repository\CommercialRepository;
use App\Repository\PrescriberRepository;
use App\Service\FormService;
use App\Service\InvoiceService;
use App\Service\StatisticsService;
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
{
/**
* @param InvoiceService $invoiceService
* @param PrescriberRepository $prescriberRepository
* @param CommercialRepository $commercialRepository
* @param StatisticsService $statisticsService
* @param FormService $formService
*/
public function __construct(
private readonly InvoiceService $invoiceService,
private readonly PrescriberRepository $prescriberRepository,
private readonly CommercialRepository $commercialRepository,
private readonly StatisticsService $statisticsService,
private readonly FormService $formService,
)
{
}
#[Route('/', name: 'app_home')]
public function index(Request $request): Response
{
if ($this->isGranted('ROLE_PRESCRIBER')) {
$prescriberId = $this->getUser()->getId();
return $this->redirectToRoute('app_prescriber_show', ['id' => $prescriberId]);
}
if ($this->isGranted('ROLE_COMMERCIAL')) {
$commercialId = $this->getUser()->getId();
return $this->redirectToRoute('app_commercial_show', ['id' => $commercialId]);
}
if ($this->isGranted('ROLE_EMPLOYEE')) {
return $this->redirectToRoute('app_home_employee');
} else {
return $this->redirectToRoute('app_login');
}
}
#[Route('/home', name: 'app_home_employee')]
public function homeEmployee(Request $request): Response
{
[$form, $startDate, $endDate, $billingCenter] = $this->formService->getFormDates($request);
$totalTurnover = $this->invoiceService->getTotalTurnoverBetweenTwoDates($startDate, $endDate, null, null, $billingCenter);
$totalProductsSells = $this->invoiceService->getTotalProductQuantity($startDate, $endDate, null, null, $billingCenter);
$totalOrdersQuantity = $this->invoiceService->getTotalOrders($startDate, $endDate, null, null, $billingCenter);
$turnoverByMonthChart = $this->statisticsService->getTurnoverByMonthChart($startDate, $endDate, null, null, $billingCenter);
$turnoverByDayChart = $this->statisticsService->getTurnoverByDay($startDate, $endDate, null, null, $billingCenter);
$topCommercial = $this->commercialRepository->getTopCommercial($startDate, $endDate, $billingCenter);
// On limite le tableau à 5 éléments
$top5Commercial = array_slice($topCommercial, 0, 5);
$topPrescriber = $this->prescriberRepository->getTopPrescriber($startDate, $endDate, null, $billingCenter);
// On limite le tableau à 5 éléments
$top5Prescriber = array_slice($topPrescriber, 0, 5);
$totalNewsPrescribers = count($this->prescriberRepository->getNewPrescribersFromDate($startDate, $endDate));
$totalNewsProspects = count($this->prescriberRepository->getNewPrescribersFromDate($startDate, $endDate, null, true));
return $this->render('home/index.html.twig', [
'form' => $form->createView(),
'totalProductsSells' => $totalProductsSells,
'totalOrdersQuantity' => $totalOrdersQuantity,
'topPrescriber' => $topPrescriber,
'top5Prescriber' => $top5Prescriber,
'topCommercial' => $topCommercial,
'top5Commercial' => $top5Commercial,
'turnoverByMonthChart' => $turnoverByMonthChart,
'totalNewsPrescribers' => $totalNewsPrescribers,
'turnoverByDayChart' => $turnoverByDayChart,
'totalTurnover' => $totalTurnover,
'totalNewsProspects' => $totalNewsProspects,
]);
}
}