src/Controller/VehicleStatisticController.php line 121

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\VehicleStatistic;
  4. use App\Repository\DealerRepository;
  5. use App\Repository\VehicleRepository;
  6. use App\Repository\VehicleStatisticRepository;
  7. use App\Repository\VehicleFormRepository;
  8. use App\Service\VehicleStatisticService;
  9. use DateTime;
  10. use Knp\Component\Pager\PaginatorInterface;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. #[Route('/vehicle-statistics')]
  18. class VehicleStatisticController extends AbstractController
  19. {
  20.     #[Security('is_granted("ROLE_ADMIN")')]
  21.     #[Route('/'name'app_vehicle_statistics_index'methods: ['GET'])]
  22.     public function index(
  23.         Request $request
  24.         DealerRepository $dealerRepository,
  25.         VehicleStatisticRepository $vehicleStatisticRepository,
  26.         VehicleFormRepository $vehicleFormRepository,
  27.         VehicleRepository $vehicleRepository
  28.     ): Response {
  29.         $dealerId $request->query->get('dealer');
  30.         $monthFrom $request->query->get('month_from');
  31.         $monthTo $request->query->get('month_to');
  32.         
  33.         $dealers $dealerRepository->findBy(['visible' => true], ['name' => 'ASC']);
  34.         $selectedDealer null;
  35.         $statistics = [];
  36.         $totalStats = [];
  37.         $vehicleInquiries = [];
  38.         $dealerVehicles = [];
  39.         $allDealersStats = [];
  40.         $yearFrom null;
  41.         $monthFromInt null;
  42.         if ($monthFrom) {
  43.             [$yearFrom$monthFromInt] = explode('-'$monthFrom);
  44.             $yearFrom = (int) $yearFrom;
  45.             $monthFromInt = (int) $monthFromInt;
  46.         }
  47.         
  48.         $yearTo null;
  49.         $monthToInt null;
  50.         if ($monthTo) {
  51.             [$yearTo$monthToInt] = explode('-'$monthTo);
  52.             $yearTo = (int) $yearTo;
  53.             $monthToInt = (int) $monthToInt;
  54.         }
  55.         
  56.         if ($dealerId) {
  57.             $selectedDealer $dealerRepository->find($dealerId);
  58.             
  59.             if ($selectedDealer) {
  60.                 $statistics $vehicleStatisticRepository->getStatisticsByDealer(
  61.                     $selectedDealer
  62.                     $yearFrom
  63.                     $monthFromInt
  64.                     $yearTo
  65.                     $monthToInt
  66.                 );
  67.                 
  68.                 $totalStats $vehicleStatisticRepository->getTotalStatisticsByDealer(
  69.                     $selectedDealer
  70.                     $yearFrom
  71.                     $monthFromInt
  72.                     $yearTo
  73.                     $monthToInt
  74.                 );
  75.                 $dateFromObj $monthFrom ? new DateTime($monthFrom '-01 00:00:00') : null;
  76.                 $dateToObj $monthTo ? new DateTime($monthTo '-' date('t'strtotime($monthTo '-01')) . ' 23:59:59') : null;
  77.                 
  78.                 $vehicleInquiries $vehicleFormRepository->getInquiriesByDealer(
  79.                     $selectedDealer
  80.                     $dateFromObj
  81.                     $dateToObj
  82.                 );
  83.                 $dealerVehicles $vehicleRepository->findVisibleInPeriod(
  84.                     $selectedDealer,
  85.                     $dateFromObj,
  86.                     $dateToObj
  87.                 );
  88.             }
  89.         } else {
  90.             $allDealersStats $vehicleStatisticRepository->getAllDealersStatistics(
  91.                 $yearFrom
  92.                 $monthFromInt
  93.                 $yearTo
  94.                 $monthToInt
  95.             );
  96.         }
  97.         
  98.         return $this->render('vehicle_statistics/index.html.twig', [
  99.             'dealers' => $dealers,
  100.             'selectedDealer' => $selectedDealer,
  101.             'statistics' => $statistics,
  102.             'totalStats' => $totalStats,
  103.             'vehicleInquiries' => $vehicleInquiries,
  104.             'dealerVehicles' => $dealerVehicles,
  105.             'allDealersStats' => $allDealersStats,
  106.             'filters' => [
  107.                 'dealer' => $dealerId,
  108.                 'month_from' => $monthFrom,
  109.                 'month_to' => $monthTo,
  110.             ]
  111.         ]);
  112.     }
  113.     #[Route('/track'name'app_vehicle_statistics_track'methods: ['POST'])]
  114.     public function track(
  115.         Request $request
  116.         VehicleRepository $vehicleRepository,
  117.         VehicleStatisticService $vehicleStatisticService
  118.     ): JsonResponse {
  119.         $vehicleId $request->request->get('vehicle_id');
  120.         $action $request->request->get('action');
  121.         
  122.         if (!$vehicleId || !$action) {
  123.             return new JsonResponse(['success' => false'error' => 'Missing parameters'], 400);
  124.         }
  125.         
  126.         $vehicle $vehicleRepository->find($vehicleId);
  127.         if (!$vehicle) {
  128.             return new JsonResponse(['success' => false'error' => 'Vehicle not found'], 404);
  129.         }
  130.         $allowedActions = ['view''call_dealer''phone_click''message_dealer'];
  131.         if (!in_array($action$allowedActions)) {
  132.             return new JsonResponse(['success' => false'error' => 'Invalid action'], 400);
  133.         }
  134.         
  135.         $tracked $vehicleStatisticService->trackAction($vehicle$action);
  136.         
  137.         return new JsonResponse([
  138.             'success' => true
  139.             'tracked' => $tracked,
  140.             'message' => $tracked 'Action tracked' 'Action already tracked today'
  141.         ]);
  142.     }
  143. }