src/DcSiteBundle/Services/CatalogService.php line 220

Open in your IDE?
  1. <?php
  2. namespace DcSiteBundle\Services;
  3. use CoreBundle\Entity\Dealer;
  4. use CoreBundle\Entity\Vehicles\Vehicle;
  5. use CoreBundle\Factory\Vehicle as VehicleFactory;
  6. use CoreBundle\Model\DealerModel;
  7. use CoreBundle\Model\Vehicles\Repository as VehicleRepository;
  8. use CoreBundle\Services\MediaExtensionVidi;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Symfony\Component\Routing\RouterInterface;
  12. class CatalogService
  13. {
  14.     private EntityManagerInterface $em;
  15.     private MediaExtensionVidi $mediaExtension;
  16.     private RouterInterface $router;
  17.     private RequestStack $requestStack;
  18.     private VehicleRepository $vehicleRepository;
  19.     private VehicleFactory $vehicleFactory;
  20.     public function __construct(EntityManagerInterface $emMediaExtensionVidi $mediaExtensionRequestStack $requestStack,
  21.                                 RouterInterface $routerVehicleRepository $vehicleRepositoryVehicleFactory $vehicleFactory)
  22.     {
  23.         $this->em $em;
  24.         $this->mediaExtension $mediaExtension;
  25.         $this->requestStack $requestStack;
  26.         $this->router $router;
  27.         $this->vehicleRepository $vehicleRepository;
  28.         $this->vehicleFactory $vehicleFactory;
  29.     }
  30.     public function getCategoryVehicles(Dealer $dealer): array
  31.     {
  32.         $request $this->requestStack->getCurrentRequest();
  33.         $locale $request->getLocale();
  34.         $categoryVehicle $this->em->getRepository(\CoreBundle\Entity\Vehicles\Category::class)->getCategoryVehicle($dealer);
  35.         return array_map(fn($row) => [
  36.             'id' => $row->getId(),
  37.             'title' => $row->getTitle($locale),
  38.             'position' => $row->getPosition(),
  39.             'url' => $row->getUrl(),
  40.         ], $categoryVehicle);
  41.     }
  42.     public function getCatalogVehicleByCategory($dealer$categoryUrl null$vehicleTypes = [])
  43.     {
  44.         $vehicles $this->em->getRepository(\CoreBundle\Entity\Vehicles\Vehicle::class)->getVehicleByUrlCategory($dealer$categoryUrl$vehicleTypes);
  45.         $vehicles array_map(function($row) {
  46.             $row $this->vehicleFactory->createByEntity($row);
  47.             $locale $this->requestStack->getCurrentRequest()->getLocale();
  48.             return [
  49.                 'id' => $row->getVehicleId(),
  50.                 'title' => $row->getModelName(),
  51.                 'customTitle' => $row->getCustomName($locale),
  52.                 'isNew' => $row->isNew(),
  53.                 'isPreOrder' => $row->isPreOrder(),
  54.                 'onTestDrive' => $row->getTestDrive(),
  55.                 'position' => $row->getPosition(),
  56.                 'url' => $row->getUrl(),
  57.                 'hasHybrid' => $row->hasHybrid(),
  58.                 'price' => round($row->price()),
  59.                 'hasActionPrice' => $row->hasActionPrice(),
  60.                 'image' => $row->getPreviewPath('small'),
  61.                 'image_webp' => $row->getPreviewPathWebp('reference'),
  62.                 'modelId' => $row->getModel()->getId(),
  63.                 'preorderPrice' => $row->getPreorderPrice(),
  64.             ];
  65.         }, $vehicles);
  66.         if (!empty($vehicles)) {
  67.             usort($vehicles, fn($a$b) => $a['position'] <=> $b['position']);
  68.         }
  69.         return $vehicles;
  70.     }
  71.     public function getCatalogVehicleGroupByCategory($dealer$categoryUrl null$vehicleTypes = [])
  72.     {
  73.         $request $this->requestStack->getCurrentRequest();
  74.         $locale $request->getLocale();
  75.         $vehicles $this->em->getRepository(\CoreBundle\Entity\Vehicles\Vehicle::class)->getVehicleByUrlCategory($dealer$categoryUrl$vehicleTypes);
  76.         /** @var Vehicle $vehicle */
  77.         foreach ($vehicles as $vehicle) {
  78.             $group $vehicle->getGroup();
  79.             if (!$group) {
  80.                 continue;
  81.             }
  82.             $vehicleEm $this->vehicleFactory->createByEntity($vehicle);
  83.             if (!isset($data[$group->getId()])) {
  84.                 $data[$group->getId()] = [
  85.                     'id' => $group->getId(),
  86.                     'title' => $group->getTitle($locale),
  87.                     'slogan' => $vehicleEm->getSlogan($locale),
  88.                     'image' => $this->mediaExtension->getPath($group->getPreview(), 'small'),
  89.                     'image_webp' => $this->mediaExtension->pathWebp($group->getPreview(), 'reference'),
  90.                     'position' => $group->getPosition(),
  91.                     'price' =>  $vehicleEm->price(),
  92.                     'hasActionPrice' => $vehicleEm->hasActionPrice(),
  93.                     'models' => []
  94.                 ];
  95.             }
  96.             $data[$group->getId()]['models'][] = [
  97.                 'title' => $vehicleEm->getModelName(),
  98.                 'link' => $this->router->generate('lexus_card_car', ['url' => $vehicleEm->getUrl()]),
  99.                 'price' =>  $vehicleEm->price(),
  100.             ];
  101.             $data[$group->getId()]['price'] = $vehicleEm->price();
  102.             foreach ($data[$group->getId()]['models'] as $model) {
  103.                 if ($model['price'] != && $model['price'] < $data[$group->getId()]['price']) {
  104.                     $data[$group->getId()]['price'] = $model['price'];
  105.                 }
  106.             }
  107.         }
  108.         usort($data, fn($a$b) => ($a['position'] == $b['position']) ? : (($a['position'] < $b['position']) ? -1));
  109.         return $data;
  110.     }
  111.     public function getCatalog(Dealer $dealer)
  112.     {
  113.         $request $this->requestStack->getCurrentRequest();
  114.         $locale $request->getLocale();
  115.         $category = [];
  116.         $dealerModels $this->vehicleRepository->getNewByDealer($dealer);
  117.         foreach ($dealerModels as $model) {
  118.             $vehicleModel $this->vehicleFactory->createByEntity($model);
  119.             if (!$vehicleModel) {
  120.                 continue;
  121.             }
  122.             $categoryId $model->getCategory()? $model->getCategory()->getId():null;
  123.             if (!isset($category[$categoryId]) && $model->getCategory()) {
  124.                 $category[$categoryId] = [
  125.                     'id' => $model->getCategory()->getId(),
  126.                     'title' => $vehicleModel->getCategory()->getTitle($locale),
  127.                     'position' => $vehicleModel->getCategory()->getPosition(),
  128.                     'url' => $vehicleModel->getCategory()->getUrl(),
  129.                     'model' => []
  130.                 ];
  131.             }
  132.             $category[$categoryId]['model'][] = $vehicleModel;
  133.         }
  134.         usort($category, fn($a$b) => $a['position'] <=> $b['position']);
  135.         return $category;
  136.     }
  137.     public function getCatalogCategory($catalog$category null)
  138.     {
  139.         if(!is_null($category)){
  140.             foreach ($catalog as $item){
  141.                 if($item['url'] != $category){
  142.                     continue;
  143.                 }
  144.                 return $item;
  145.             }
  146.         }
  147.         return $category;
  148.     }
  149.     public function getVehicleByCategory($catalog$category null$locale null)
  150.     {
  151.         $models = [];
  152.         foreach ($catalog as $item) {
  153.             if ($category && $category != $item['url']) {
  154.                 continue;
  155.             }
  156.             foreach ($item['model'] as $modelItem) {
  157.                 $models[] = [
  158.                     'id' => $modelItem->getVehicleId(),
  159.                     'title' => $modelItem->getModelName(),
  160.                     'customTitle' => $modelItem->getCustomName($locale),
  161.                     'isNew' => $modelItem->isNew(),
  162.                     'isPreOrder' => $modelItem->isPreOrder(),
  163.                     'onTestDrive' => $modelItem->getTestDrive(),
  164.                     'position' => $modelItem->getPosition(),
  165.                     'url' => $modelItem->getUrl(),
  166.                     'hasHybrid' => $modelItem->hasHybrid(),
  167.                     'price' => round($modelItem->price()),
  168.                     'image' => $modelItem->getPreviewPath('small'),
  169.                     'image_webp' => $modelItem->getPreviewPathWebp('reference'),
  170.                     'modelId' => $modelItem->getModel()->getId(),
  171.                     'preorderPrice' => $modelItem->getPreorderPrice(),
  172.                 ];
  173.             }
  174.         }
  175.         if (!empty($models)) {
  176.             usort($models, fn($a$b) => $a['position'] <=> $b['position']);
  177.         }
  178.         return $models;
  179.     }
  180.     public function getAjaxResult($vehicles$locale$routeSettigs, ?Dealer $dealer null): array
  181.     {
  182.         $vehicleAjaxResult = [];
  183.         $i 0;
  184.         foreach ($vehicles as $item) {
  185.             /** @var \CoreBundle\Model\Vehicles\Vehicle $vehicle */
  186.             $vehicle $item['vehicle'];
  187.             $vehicleAjaxResult[$i]['vehicle']['fullName'] = $vehicle->getFullName();
  188.             $vehicleAjaxResult[$i]['vehicle']['vehicleId'] = $vehicle->getVehicleId();
  189.             $vehicleAjaxResult[$i]['vehicle']['vehicleItemId'] = $vehicle->getVehicleItemId();
  190.             $vehicleAjaxResult[$i]['vehicle']['equipmentTitle'] = $vehicle->getEquipmentTitle();
  191.             $vehicleAjaxResult[$i]['vehicle']['enginePower'] = $vehicle->getEnginePower($locale);
  192.             $vehicleAjaxResult[$i]['vehicle']['year'] = $vehicle->getYear();
  193.             $vehicleAjaxResult[$i]['vehicle']['transmissionTypeName'] = $vehicle->getTransmissionTypeName($locale);
  194.             $vehicleAjaxResult[$i]['vehicle']['driveUnitTypeName'] = $vehicle->getDriveUnitTypeName($locale);
  195.             $vehicleAjaxResult[$i]['vehicle']['bodyTypeName'] = $vehicle->getBodyTypeName($locale);
  196.             $vehicleAjaxResult[$i]['vehicle']['bodyType'] = $vehicle->getBodyType()->getId();
  197.             $vehicleAjaxResult[$i]['vehicle']['turbine'] = $vehicle->getTurbine($locale);
  198.             $vehicleAjaxResult[$i]['vehicle']['control'] = $vehicle->getControl($locale);
  199.             $vehicleAjaxResult[$i]['vehicle']['powerReserve'] = $vehicle->getPowerReserve($locale);
  200.             $vehicleAjaxResult[$i]['vehicle']['wheelDiameter'] = $vehicle->getWheelDiameter($locale);
  201.             $vehicleAjaxResult[$i]['vehicle']['maximumFluidity'] = $vehicle->getMaximumFluidity($locale);
  202.             $vehicleAjaxResult[$i]['vehicle']['numberOfCylinders'] = $vehicle->getNumberOfCylinders($locale);
  203.             $vehicleAjaxResult[$i]['vehicle']['equippedMass'] = $vehicle->getEquippedMass($locale);
  204.             $vehicleAjaxResult[$i]['vehicle']['fuelVolume'] = $vehicle->getFuelVolume($locale);
  205.             $vehicleAjaxResult[$i]['vehicle']['bodyLength'] = $vehicle->getBodyLength($locale);
  206.             if($vehicle->getEngineVolume($locale) > 100) {
  207.                 $enVol number_format((int)$vehicle->getEngineVolume($locale) /10001'.''');
  208.             } else {
  209.                 $enVol $vehicle->getEngineVolume($locale);
  210.             }
  211.             $vehicleAjaxResult[$i]['vehicle']['engineVolume'] = $enVol;
  212.             $vehicleAjaxResult[$i]['vehicle']['fuelTypeName'] = $vehicle->getFuelTypeName($locale);
  213.             $vehicleAjaxResult[$i]['vehicle']['mileage'] = number_format(round($vehicle->getMileage()), 0'.'' ');
  214.             $vehicleAjaxResult[$i]['vehicle']['isReserved'] = $vehicle->getVehicleItems()->first()->getIsReserved();
  215.             $vehicleAjaxResult[$i]['vehicle']['deposit'] = $vehicle->getVehicleItems()->first()->getDeposit();
  216.             $vehicleAjaxResult[$i]['creditPayment'] = number_format(round($item['creditPayment'] ?? null), 0'.'' ');
  217.             $vehicleAjaxResult[$i]['vehicleColors'] = $item['vehicleColors'] ?? null;
  218.             $vehicleAjaxResult[$i]['vehiclePicture'] = $item['vehiclePicture'] ?? null;
  219.             $vehicleAjaxResult[$i]['vehiclePictureSecond'] = $item['vehiclePictureSecond'] ?? null;
  220.             $vehicleAjaxResult[$i]['vehicle']['price'] = number_format(round($vehicle->price()), 0'.'' ');
  221.             $vehicleAjaxResult[$i]['vehicle']['fullPrice'] = number_format(floor($vehicle->fullPrice()), 0'.'' ');
  222.             $vehicleAjaxResult[$i]['vehicle']['isUsed'] = $vehicle->isUsed();
  223.             $vehicleAjaxResult[$i]['vehicle']['isSold'] = ($this->em->getRepository(\CoreBundle\Entity\Vehicles\VehicleItem::class)->find($vehicle->getVehicleItemId()))->getSold();
  224.             $vehicleAjaxResult[$i]['hasNds'] = $item['hasNds'] ?? null;
  225.             $vehicleAjaxResult[$i]['vehicle']['testDrive'] = $vehicle->getTestDrive();
  226.             $vehicleAjaxResult[$i]['vehicle']['testDriveHref'] = $this->router->generate('portal_service_vehicle_testdrive_form',['url' => $vehicle->getUrl()]);
  227.             $vehicleAjaxResult[$i]['isSelect'] = $item['isSelect'] ?? null;
  228.             $vehicleAjaxResult[$i]['vehicle']['dealerName'] = $vehicle->getDealer()->getName();
  229.             $vehicleAjaxResult[$i]['vehicle']['dealerAddress'] = $vehicle->getDealer()->getAddressByLocale($locale);
  230.             $vehicleAjaxResult[$i]['vehicle']['featuredId'] = $item['featuredId'] ?? null;
  231.             $vehicleAjaxResult[$i]['vehicle']['comparedId'] = $item['comparedId'] ?? null;
  232.             $vehicleAjaxResult[$i]['vehicle']['featuredData'] = [
  233.                 'vehicleItemId' => $vehicle->getVehicleItemId(),
  234.                 'locale' => $locale,
  235.                 'link' => $this->router->generate('portal_new_car', ['dealer' => $vehicle->getDealer()->getUrl(), 'url' => $vehicle->getUrl(), 'variation' => $vehicle->getVehicleItemId()]),
  236.             ];
  237.             if ($vehicle->isUsed()) {
  238.                 $vehicleAjaxResult[$i]['vehicle']['options'] = $vehicle->getOptions($locale);
  239.             }
  240.             if ($vehicle->getDealer()->getBrand()) {
  241.                 $vehicleAjaxResult[$i]['vehicle']['brand']['dealerCount'] = count($vehicle->getDealer()->getBrand()->getDealer());
  242.             } else {
  243.                 $vehicleAjaxResult[$i]['vehicle']['brand']['dealerCount'] = 0;
  244.             }
  245.             $excludeDealerIds = [2735313428438423332937];
  246.             $vehicleAjaxResult[$i]['vehicle']['href'] = '';
  247.             if (!empty($routeSettigs) && ($routeSettigs['new_car'] || $routeSettigs['used_car'])) {
  248.                 if ($vehicle->isUsed() && $routeSettigs['used_car']) {
  249.                     if ($dealer && DealerModel::SECOND_HAND_DEALER == $dealer->getId()) {
  250.                         $vehicleAjaxResult[$i]['vehicle']['href'] = $this->router->generate($routeSettigs['used_car'], [
  251.                             'state' =>'used',
  252.                             'type' => 'car',
  253.                             'brand' => $vehicle->getBrand()->getUrl(),
  254.                             'model' => $vehicle->getModel()->getUrl(),
  255.                             'id' => $vehicle->getVehicleId(),
  256.                         ]);
  257.                     } elseif ($vehicle->getDealer()->getId() == DealerModel::LAND_ROVER_DEALER || ($dealer && $dealer->getId() == DealerModel::LAND_ROVER_DEALER)) {
  258.                         $vehicleAjaxResult[$i]['vehicle']['href'] = $this->router->generate($routeSettigs['used_car'],
  259.                             ['id' => $vehicle->getVehicleId()]);
  260.                     } elseif (DealerModel::SECOND_HAND_DEALER == $dealer->getId()) {
  261.                         $vehicleAjaxResult[$i]['vehicle']['href'] = $this->router->generate($routeSettigs['used_car'], [
  262.                             'state' =>'used',
  263.                             'type' => 'car',
  264.                             'brand' => $vehicle->getBrand()->getUrl(),
  265.                             'model' => $vehicle->getModel()->getUrl(),
  266.                             'id' => $vehicle->getVehicleId(),
  267.                         ]);
  268.                     }
  269.                     else {
  270.                         $vehicleAjaxResult[$i]['vehicle']['href'] = $this->router->generate($routeSettigs['used_car'], ['url' => $vehicle->getUrl()]);
  271.                     }
  272.                 }
  273.                 if (!$vehicle->isUsed() && $routeSettigs['new_car']) {
  274.                     if ($vehicle->getDealer()->getId() && in_array($vehicle->getDealer()->getId(), $excludeDealerIds)) {
  275.                         $vehicleAjaxResult[$i]['vehicle']['href'] = $this->router->generate($routeSettigs['new_car'],
  276.                             ['car' => $vehicle->getUrl()]);
  277.                     } elseif (in_array($vehicle->getDealer()->getId(), DealerModel::NISSAN_DEALERS)) {
  278.                         $vehicleAjaxResult[$i]['vehicle']['href'] = $this->router->generate($routeSettigs['new_car'],
  279.                             ['car' => $vehicle->getUrl(), 'category' => $vehicle->getCategory()->getUrl()]);
  280.                     }
  281.                     else {
  282.                         $vehicleAjaxResult[$i]['vehicle']['href'] = $this->router->generate($routeSettigs['new_car'],
  283.                             ['url' => $vehicle->getUrl()]);
  284.                     }
  285.                 }
  286.             }
  287.             if (empty($routeSettigs)){
  288.                 if ($vehicle->isUsed()) {
  289.                     $vehicleAjaxResult[$i]['vehicle']['href'] = $this->router->generate('portal_used_car',['url' => $vehicle->getUrl()]);
  290.                 } else {
  291.                     $vehicleAjaxResult[$i]['vehicle']['href'] =  $this->router->generate('portal_new_car',
  292.                         ['dealer' => $vehicle->getDealer()->getUrl(), 'url' => $vehicle->getUrl(), 'variation' => $vehicle->getVehicleItemId()]);
  293.                 }
  294.             }
  295.             $vehicleAjaxResult[$i]['vehicle']['optionsByEquipment'] = $vehicle->getOptionsByEquipment($vehicle->getEquipment(), $localetrue);
  296.             $vehicleAjaxResult[$i]['vehicle']['optionsByEquipment']['count'] = count($vehicle->getOptionsByEquipment($vehicle->getEquipment(), $localetrue));
  297.             $colors = [];
  298.             $j 0;
  299.             foreach ($vehicle->getColors() as $color) {
  300.                 $colors[$j]['image'] = $this->mediaExtension->getPath($color->getImage(), 'reference');
  301.                 $colors[$j]['id'] = $color->getId();
  302.                 ++$j;
  303.             }
  304.             $vehicleAjaxResult[$i]['vehicle']['colors'] = $colors;
  305.             ++$i;
  306.         }
  307.         return $vehicleAjaxResult;
  308.     }
  309. }