src/Controller/VehicleStatisticController.php line 121
<?phpnamespace App\Controller;use App\Entity\VehicleStatistic;use App\Repository\DealerRepository;use App\Repository\VehicleRepository;use App\Repository\VehicleStatisticRepository;use App\Repository\VehicleFormRepository;use App\Service\VehicleStatisticService;use DateTime;use Knp\Component\Pager\PaginatorInterface;use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;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;#[Route('/vehicle-statistics')]class VehicleStatisticController extends AbstractController{#[Security('is_granted("ROLE_ADMIN")')]#[Route('/', name: 'app_vehicle_statistics_index', methods: ['GET'])]public function index(Request $request,DealerRepository $dealerRepository,VehicleStatisticRepository $vehicleStatisticRepository,VehicleFormRepository $vehicleFormRepository,VehicleRepository $vehicleRepository): Response {$dealerId = $request->query->get('dealer');$monthFrom = $request->query->get('month_from');$monthTo = $request->query->get('month_to');$dealers = $dealerRepository->findBy(['visible' => true], ['name' => 'ASC']);$selectedDealer = null;$statistics = [];$totalStats = [];$vehicleInquiries = [];$dealerVehicles = [];$allDealersStats = [];$yearFrom = null;$monthFromInt = null;if ($monthFrom) {[$yearFrom, $monthFromInt] = explode('-', $monthFrom);$yearFrom = (int) $yearFrom;$monthFromInt = (int) $monthFromInt;}$yearTo = null;$monthToInt = null;if ($monthTo) {[$yearTo, $monthToInt] = explode('-', $monthTo);$yearTo = (int) $yearTo;$monthToInt = (int) $monthToInt;}if ($dealerId) {$selectedDealer = $dealerRepository->find($dealerId);if ($selectedDealer) {$statistics = $vehicleStatisticRepository->getStatisticsByDealer($selectedDealer,$yearFrom,$monthFromInt,$yearTo,$monthToInt);$totalStats = $vehicleStatisticRepository->getTotalStatisticsByDealer($selectedDealer,$yearFrom,$monthFromInt,$yearTo,$monthToInt);$dateFromObj = $monthFrom ? new DateTime($monthFrom . '-01 00:00:00') : null;$dateToObj = $monthTo ? new DateTime($monthTo . '-' . date('t', strtotime($monthTo . '-01')) . ' 23:59:59') : null;$vehicleInquiries = $vehicleFormRepository->getInquiriesByDealer($selectedDealer,$dateFromObj,$dateToObj);$dealerVehicles = $vehicleRepository->findVisibleInPeriod($selectedDealer,$dateFromObj,$dateToObj);}} else {$allDealersStats = $vehicleStatisticRepository->getAllDealersStatistics($yearFrom,$monthFromInt,$yearTo,$monthToInt);}return $this->render('vehicle_statistics/index.html.twig', ['dealers' => $dealers,'selectedDealer' => $selectedDealer,'statistics' => $statistics,'totalStats' => $totalStats,'vehicleInquiries' => $vehicleInquiries,'dealerVehicles' => $dealerVehicles,'allDealersStats' => $allDealersStats,'filters' => ['dealer' => $dealerId,'month_from' => $monthFrom,'month_to' => $monthTo,]]);}#[Route('/track', name: 'app_vehicle_statistics_track', methods: ['POST'])]public function track(Request $request,VehicleRepository $vehicleRepository,VehicleStatisticService $vehicleStatisticService): JsonResponse {$vehicleId = $request->request->get('vehicle_id');$action = $request->request->get('action');if (!$vehicleId || !$action) {return new JsonResponse(['success' => false, 'error' => 'Missing parameters'], 400);}$vehicle = $vehicleRepository->find($vehicleId);if (!$vehicle) {return new JsonResponse(['success' => false, 'error' => 'Vehicle not found'], 404);}$allowedActions = ['view', 'call_dealer', 'phone_click', 'message_dealer'];if (!in_array($action, $allowedActions)) {return new JsonResponse(['success' => false, 'error' => 'Invalid action'], 400);}$tracked = $vehicleStatisticService->trackAction($vehicle, $action);return new JsonResponse(['success' => true,'tracked' => $tracked,'message' => $tracked ? 'Action tracked' : 'Action already tracked today']);}}