<?php
namespace DcSiteBundle\Services;
use CoreBundle\Entity\Dealer;
use CoreBundle\Entity\Vehicles\Vehicle;
use CoreBundle\Factory\Vehicle as VehicleFactory;
use CoreBundle\Model\DealerModel;
use CoreBundle\Model\Vehicles\Repository as VehicleRepository;
use CoreBundle\Services\MediaExtensionVidi;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\RouterInterface;
class CatalogService
{
private EntityManagerInterface $em;
private MediaExtensionVidi $mediaExtension;
private RouterInterface $router;
private RequestStack $requestStack;
private VehicleRepository $vehicleRepository;
private VehicleFactory $vehicleFactory;
public function __construct(EntityManagerInterface $em, MediaExtensionVidi $mediaExtension, RequestStack $requestStack,
RouterInterface $router, VehicleRepository $vehicleRepository, VehicleFactory $vehicleFactory)
{
$this->em = $em;
$this->mediaExtension = $mediaExtension;
$this->requestStack = $requestStack;
$this->router = $router;
$this->vehicleRepository = $vehicleRepository;
$this->vehicleFactory = $vehicleFactory;
}
public function getCategoryVehicles(Dealer $dealer): array
{
$request = $this->requestStack->getCurrentRequest();
$locale = $request->getLocale();
$categoryVehicle = $this->em->getRepository(\CoreBundle\Entity\Vehicles\Category::class)->getCategoryVehicle($dealer);
return array_map(fn($row) => [
'id' => $row->getId(),
'title' => $row->getTitle($locale),
'position' => $row->getPosition(),
'url' => $row->getUrl(),
], $categoryVehicle);
}
public function getCatalogVehicleByCategory($dealer, $categoryUrl = null, $vehicleTypes = [])
{
$vehicles = $this->em->getRepository(\CoreBundle\Entity\Vehicles\Vehicle::class)->getVehicleByUrlCategory($dealer, $categoryUrl, $vehicleTypes);
$vehicles = array_map(function($row) {
$row = $this->vehicleFactory->createByEntity($row);
$locale = $this->requestStack->getCurrentRequest()->getLocale();
return [
'id' => $row->getVehicleId(),
'title' => $row->getModelName(),
'customTitle' => $row->getCustomName($locale),
'isNew' => $row->isNew(),
'isPreOrder' => $row->isPreOrder(),
'onTestDrive' => $row->getTestDrive(),
'position' => $row->getPosition(),
'url' => $row->getUrl(),
'hasHybrid' => $row->hasHybrid(),
'price' => round($row->price()),
'hasActionPrice' => $row->hasActionPrice(),
'image' => $row->getPreviewPath('small'),
'image_webp' => $row->getPreviewPathWebp('reference'),
'modelId' => $row->getModel()->getId(),
'preorderPrice' => $row->getPreorderPrice(),
];
}, $vehicles);
if (!empty($vehicles)) {
usort($vehicles, fn($a, $b) => $a['position'] <=> $b['position']);
}
return $vehicles;
}
public function getCatalogVehicleGroupByCategory($dealer, $categoryUrl = null, $vehicleTypes = [])
{
$request = $this->requestStack->getCurrentRequest();
$locale = $request->getLocale();
$vehicles = $this->em->getRepository(\CoreBundle\Entity\Vehicles\Vehicle::class)->getVehicleByUrlCategory($dealer, $categoryUrl, $vehicleTypes);
/** @var Vehicle $vehicle */
foreach ($vehicles as $vehicle) {
$group = $vehicle->getGroup();
if (!$group) {
continue;
}
$vehicleEm = $this->vehicleFactory->createByEntity($vehicle);
if (!isset($data[$group->getId()])) {
$data[$group->getId()] = [
'id' => $group->getId(),
'title' => $group->getTitle($locale),
'slogan' => $vehicleEm->getSlogan($locale),
'image' => $this->mediaExtension->getPath($group->getPreview(), 'small'),
'image_webp' => $this->mediaExtension->pathWebp($group->getPreview(), 'reference'),
'position' => $group->getPosition(),
'price' => $vehicleEm->price(),
'hasActionPrice' => $vehicleEm->hasActionPrice(),
'models' => []
];
}
$data[$group->getId()]['models'][] = [
'title' => $vehicleEm->getModelName(),
'link' => $this->router->generate('lexus_card_car', ['url' => $vehicleEm->getUrl()]),
'price' => $vehicleEm->price(),
];
$data[$group->getId()]['price'] = $vehicleEm->price();
foreach ($data[$group->getId()]['models'] as $model) {
if ($model['price'] != 0 && $model['price'] < $data[$group->getId()]['price']) {
$data[$group->getId()]['price'] = $model['price'];
}
}
}
usort($data, fn($a, $b) => ($a['position'] == $b['position']) ? 0 : (($a['position'] < $b['position']) ? -1 : 1));
return $data;
}
public function getCatalog(Dealer $dealer)
{
$request = $this->requestStack->getCurrentRequest();
$locale = $request->getLocale();
$category = [];
$dealerModels = $this->vehicleRepository->getNewByDealer($dealer);
foreach ($dealerModels as $model) {
$vehicleModel = $this->vehicleFactory->createByEntity($model);
if (!$vehicleModel) {
continue;
}
$categoryId = $model->getCategory()? $model->getCategory()->getId():null;
if (!isset($category[$categoryId]) && $model->getCategory()) {
$category[$categoryId] = [
'id' => $model->getCategory()->getId(),
'title' => $vehicleModel->getCategory()->getTitle($locale),
'position' => $vehicleModel->getCategory()->getPosition(),
'url' => $vehicleModel->getCategory()->getUrl(),
'model' => []
];
}
$category[$categoryId]['model'][] = $vehicleModel;
}
usort($category, fn($a, $b) => $a['position'] <=> $b['position']);
return $category;
}
public function getCatalogCategory($catalog, $category = null)
{
if(!is_null($category)){
foreach ($catalog as $item){
if($item['url'] != $category){
continue;
}
return $item;
}
}
return $category;
}
public function getVehicleByCategory($catalog, $category = null, $locale = null)
{
$models = [];
foreach ($catalog as $item) {
if ($category && $category != $item['url']) {
continue;
}
foreach ($item['model'] as $modelItem) {
$models[] = [
'id' => $modelItem->getVehicleId(),
'title' => $modelItem->getModelName(),
'customTitle' => $modelItem->getCustomName($locale),
'isNew' => $modelItem->isNew(),
'isPreOrder' => $modelItem->isPreOrder(),
'onTestDrive' => $modelItem->getTestDrive(),
'position' => $modelItem->getPosition(),
'url' => $modelItem->getUrl(),
'hasHybrid' => $modelItem->hasHybrid(),
'price' => round($modelItem->price()),
'image' => $modelItem->getPreviewPath('small'),
'image_webp' => $modelItem->getPreviewPathWebp('reference'),
'modelId' => $modelItem->getModel()->getId(),
'preorderPrice' => $modelItem->getPreorderPrice(),
];
}
}
if (!empty($models)) {
usort($models, fn($a, $b) => $a['position'] <=> $b['position']);
}
return $models;
}
public function getAjaxResult($vehicles, $locale, $routeSettigs, ?Dealer $dealer = null): array
{
$vehicleAjaxResult = [];
$i = 0;
foreach ($vehicles as $item) {
/** @var \CoreBundle\Model\Vehicles\Vehicle $vehicle */
$vehicle = $item['vehicle'];
$vehicleAjaxResult[$i]['vehicle']['fullName'] = $vehicle->getFullName();
$vehicleAjaxResult[$i]['vehicle']['vehicleId'] = $vehicle->getVehicleId();
$vehicleAjaxResult[$i]['vehicle']['vehicleItemId'] = $vehicle->getVehicleItemId();
$vehicleAjaxResult[$i]['vehicle']['equipmentTitle'] = $vehicle->getEquipmentTitle();
$vehicleAjaxResult[$i]['vehicle']['enginePower'] = $vehicle->getEnginePower($locale);
$vehicleAjaxResult[$i]['vehicle']['year'] = $vehicle->getYear();
$vehicleAjaxResult[$i]['vehicle']['transmissionTypeName'] = $vehicle->getTransmissionTypeName($locale);
$vehicleAjaxResult[$i]['vehicle']['driveUnitTypeName'] = $vehicle->getDriveUnitTypeName($locale);
$vehicleAjaxResult[$i]['vehicle']['bodyTypeName'] = $vehicle->getBodyTypeName($locale);
$vehicleAjaxResult[$i]['vehicle']['bodyType'] = $vehicle->getBodyType()->getId();
$vehicleAjaxResult[$i]['vehicle']['turbine'] = $vehicle->getTurbine($locale);
$vehicleAjaxResult[$i]['vehicle']['control'] = $vehicle->getControl($locale);
$vehicleAjaxResult[$i]['vehicle']['powerReserve'] = $vehicle->getPowerReserve($locale);
$vehicleAjaxResult[$i]['vehicle']['wheelDiameter'] = $vehicle->getWheelDiameter($locale);
$vehicleAjaxResult[$i]['vehicle']['maximumFluidity'] = $vehicle->getMaximumFluidity($locale);
$vehicleAjaxResult[$i]['vehicle']['numberOfCylinders'] = $vehicle->getNumberOfCylinders($locale);
$vehicleAjaxResult[$i]['vehicle']['equippedMass'] = $vehicle->getEquippedMass($locale);
$vehicleAjaxResult[$i]['vehicle']['fuelVolume'] = $vehicle->getFuelVolume($locale);
$vehicleAjaxResult[$i]['vehicle']['bodyLength'] = $vehicle->getBodyLength($locale);
if($vehicle->getEngineVolume($locale) > 100) {
$enVol = number_format((int)$vehicle->getEngineVolume($locale) /1000, 1, '.', '');
} else {
$enVol = $vehicle->getEngineVolume($locale);
}
$vehicleAjaxResult[$i]['vehicle']['engineVolume'] = $enVol;
$vehicleAjaxResult[$i]['vehicle']['fuelTypeName'] = $vehicle->getFuelTypeName($locale);
$vehicleAjaxResult[$i]['vehicle']['mileage'] = number_format(round($vehicle->getMileage()), 0, '.', ' ');
$vehicleAjaxResult[$i]['vehicle']['isReserved'] = $vehicle->getVehicleItems()->first()->getIsReserved();
$vehicleAjaxResult[$i]['vehicle']['deposit'] = $vehicle->getVehicleItems()->first()->getDeposit();
$vehicleAjaxResult[$i]['creditPayment'] = number_format(round($item['creditPayment'] ?? null), 0, '.', ' ');
$vehicleAjaxResult[$i]['vehicleColors'] = $item['vehicleColors'] ?? null;
$vehicleAjaxResult[$i]['vehiclePicture'] = $item['vehiclePicture'] ?? null;
$vehicleAjaxResult[$i]['vehiclePictureSecond'] = $item['vehiclePictureSecond'] ?? null;
$vehicleAjaxResult[$i]['vehicle']['price'] = number_format(round($vehicle->price()), 0, '.', ' ');
$vehicleAjaxResult[$i]['vehicle']['fullPrice'] = number_format(floor($vehicle->fullPrice()), 0, '.', ' ');
$vehicleAjaxResult[$i]['vehicle']['isUsed'] = $vehicle->isUsed();
$vehicleAjaxResult[$i]['vehicle']['isSold'] = ($this->em->getRepository(\CoreBundle\Entity\Vehicles\VehicleItem::class)->find($vehicle->getVehicleItemId()))->getSold();
$vehicleAjaxResult[$i]['hasNds'] = $item['hasNds'] ?? null;
$vehicleAjaxResult[$i]['vehicle']['testDrive'] = $vehicle->getTestDrive();
$vehicleAjaxResult[$i]['vehicle']['testDriveHref'] = $this->router->generate('portal_service_vehicle_testdrive_form',['url' => $vehicle->getUrl()]);
$vehicleAjaxResult[$i]['isSelect'] = $item['isSelect'] ?? null;
$vehicleAjaxResult[$i]['vehicle']['dealerName'] = $vehicle->getDealer()->getName();
$vehicleAjaxResult[$i]['vehicle']['dealerAddress'] = $vehicle->getDealer()->getAddressByLocale($locale);
$vehicleAjaxResult[$i]['vehicle']['featuredId'] = $item['featuredId'] ?? null;
$vehicleAjaxResult[$i]['vehicle']['comparedId'] = $item['comparedId'] ?? null;
$vehicleAjaxResult[$i]['vehicle']['featuredData'] = [
'vehicleItemId' => $vehicle->getVehicleItemId(),
'locale' => $locale,
'link' => $this->router->generate('portal_new_car', ['dealer' => $vehicle->getDealer()->getUrl(), 'url' => $vehicle->getUrl(), 'variation' => $vehicle->getVehicleItemId()]),
];
if ($vehicle->isUsed()) {
$vehicleAjaxResult[$i]['vehicle']['options'] = $vehicle->getOptions($locale);
}
if ($vehicle->getDealer()->getBrand()) {
$vehicleAjaxResult[$i]['vehicle']['brand']['dealerCount'] = count($vehicle->getDealer()->getBrand()->getDealer());
} else {
$vehicleAjaxResult[$i]['vehicle']['brand']['dealerCount'] = 0;
}
$excludeDealerIds = [27, 35, 31, 34, 28, 4, 38, 42, 33, 3, 29, 37];
$vehicleAjaxResult[$i]['vehicle']['href'] = '';
if (!empty($routeSettigs) && ($routeSettigs['new_car'] || $routeSettigs['used_car'])) {
if ($vehicle->isUsed() && $routeSettigs['used_car']) {
if ($dealer && DealerModel::SECOND_HAND_DEALER == $dealer->getId()) {
$vehicleAjaxResult[$i]['vehicle']['href'] = $this->router->generate($routeSettigs['used_car'], [
'state' =>'used',
'type' => 'car',
'brand' => $vehicle->getBrand()->getUrl(),
'model' => $vehicle->getModel()->getUrl(),
'id' => $vehicle->getVehicleId(),
]);
} elseif ($vehicle->getDealer()->getId() == DealerModel::LAND_ROVER_DEALER || ($dealer && $dealer->getId() == DealerModel::LAND_ROVER_DEALER)) {
$vehicleAjaxResult[$i]['vehicle']['href'] = $this->router->generate($routeSettigs['used_car'],
['id' => $vehicle->getVehicleId()]);
} elseif (DealerModel::SECOND_HAND_DEALER == $dealer->getId()) {
$vehicleAjaxResult[$i]['vehicle']['href'] = $this->router->generate($routeSettigs['used_car'], [
'state' =>'used',
'type' => 'car',
'brand' => $vehicle->getBrand()->getUrl(),
'model' => $vehicle->getModel()->getUrl(),
'id' => $vehicle->getVehicleId(),
]);
}
else {
$vehicleAjaxResult[$i]['vehicle']['href'] = $this->router->generate($routeSettigs['used_car'], ['url' => $vehicle->getUrl()]);
}
}
if (!$vehicle->isUsed() && $routeSettigs['new_car']) {
if ($vehicle->getDealer()->getId() && in_array($vehicle->getDealer()->getId(), $excludeDealerIds)) {
$vehicleAjaxResult[$i]['vehicle']['href'] = $this->router->generate($routeSettigs['new_car'],
['car' => $vehicle->getUrl()]);
} elseif (in_array($vehicle->getDealer()->getId(), DealerModel::NISSAN_DEALERS)) {
$vehicleAjaxResult[$i]['vehicle']['href'] = $this->router->generate($routeSettigs['new_car'],
['car' => $vehicle->getUrl(), 'category' => $vehicle->getCategory()->getUrl()]);
}
else {
$vehicleAjaxResult[$i]['vehicle']['href'] = $this->router->generate($routeSettigs['new_car'],
['url' => $vehicle->getUrl()]);
}
}
}
if (empty($routeSettigs)){
if ($vehicle->isUsed()) {
$vehicleAjaxResult[$i]['vehicle']['href'] = $this->router->generate('portal_used_car',['url' => $vehicle->getUrl()]);
} else {
$vehicleAjaxResult[$i]['vehicle']['href'] = $this->router->generate('portal_new_car',
['dealer' => $vehicle->getDealer()->getUrl(), 'url' => $vehicle->getUrl(), 'variation' => $vehicle->getVehicleItemId()]);
}
}
$vehicleAjaxResult[$i]['vehicle']['optionsByEquipment'] = $vehicle->getOptionsByEquipment($vehicle->getEquipment(), $locale, true);
$vehicleAjaxResult[$i]['vehicle']['optionsByEquipment']['count'] = count($vehicle->getOptionsByEquipment($vehicle->getEquipment(), $locale, true));
$colors = [];
$j = 0;
foreach ($vehicle->getColors() as $color) {
$colors[$j]['image'] = $this->mediaExtension->getPath($color->getImage(), 'reference');
$colors[$j]['id'] = $color->getId();
++$j;
}
$vehicleAjaxResult[$i]['vehicle']['colors'] = $colors;
++$i;
}
return $vehicleAjaxResult;
}
}