<?php
namespace CoreBundle\Model\Service;
use CoreBundle\Entity\Model;
use CoreBundle\Services\MediaExtensionVidi;
use DcSiteBundle\Entity\ServiceWorkGroup;
use DcSiteBundle\Entity\WorkGroupRelation;
use Doctrine\ORM\EntityManagerInterface;
class ServiceWork
{
private $em;
const MAIN_SERVICE_ID = 7447;
/**
* CallbackRequestLog constructor.
* @param EntityManagerInterface $entityManager
*/
public function __construct(EntityManagerInterface $entityManager, MediaExtensionVidi $mediaExtension)
{
$this->em = $entityManager;
$this->mediaExtension = $mediaExtension;
}
public function getModelsByGroupAndBrand($workGroup, $brand)
{
$result = $this->em->getRepository(WorkGroupRelation::class)->getWorksByGroup($workGroup);
$workIds = [];
foreach ($result as $el) {
$workIds[] = $el->getWork()->getId();
}
$models = $this->em->getRepository(Model::class)->getModelByWorkGroup($workIds, $brand);
return $models;
}
public function getTopGroup($workGroup, $work)
{
if ($work->getUrl() == $workGroup){
return true;
} elseif ($work->getParent()){
return $this->getTopGroup($workGroup, $work->getParent());
} else {
return false;
}
}
public function getWorksByModelOrBrand($workGroup, $subGroup = '', $model = '', $brand)
{
$serviceWorks = $this->em->getRepository(ServiceWork::class)->getWorksByModelAndBrand($model, $brand);
$otherWorkGroups = [];
$selectGroup = [];
$prices = [];
if ($serviceWorks){
$subGroups = $this->em->getRepository(ServiceWorkGroup::class)->getServiceGroupByParent($workGroup);
foreach ($subGroups as $group){
foreach ($serviceWorks as $work){
if ($this->getTopGroup($group->getUrl(), $work['work']->getGroup())){
$prices[$group->GetId()][] = $work['min_price'];
}
}
$otherWorkGroups[] = [
'group' => $group,
'min_price' => isset($prices[$group->GetId()]) ? min($prices[$group->GetId()]) : 0,
];
}
if ($subGroup){
$group = $this->em->getRepository(ServiceWorkGroup::class)->findOneBy(['url' => $subGroup]);
foreach ($serviceWorks as $work){
if ($this->getTopGroup($group->getUrl(), $work['work']->getGroup())){
$prices[$group->GetId()][] = $work['min_price'];
}
}
$selectGroup[] = [
'group' => $group,
'min_price' => isset($prices[$group->GetId()]) ? min($prices[$group->GetId()]) : 0,
];
}
}
$arRes = [
'otherGroups' => $otherWorkGroups,
'mainGroup' => $selectGroup
];
return $arRes;
}
public function getGroupBrands($workGroup)
{
$result = $this->em->getRepository(WorkGroupRelation::class)->getWorksByGroup($workGroup);
$brands = [];
foreach ($result as $el){
$brand = $el->getWork()->getDealer()->getBrand();
$brands[$brand->getId()] = [
'id' => $brand->getId(),
'name' => $brand->getName(),
'url' => $brand->getUrl(),
'img' => $this->mediaExtension->getPath($brand->getLogo(), 'reference'),
];
}
return $brands;
}
public function getMainService()
{
return $this->em->getRepository(ServiceWorkGroup::class)->find(self::MAIN_SERVICE_ID);
}
public function getSubGroupsByBrand($workGroup, $brand)
{
$subGroups = $this->em->getRepository(ServiceWorkGroup::class)->getServiceGroupByParent($workGroup);
$subGroupsIds = [];
foreach ($subGroups as $group){
$subGroupsIds[] = $group->getId();
}
$filtereSubGroups = [];
if ($subGroupsIds){
$result = $this->em->getRepository(WorkGroupRelation::class)->getGroupByBrand($subGroupsIds, $brand);
foreach ($result as $el){
$filtereSubGroups[$el->getGroup()->getId()] = $el->getGroup();
}
}
return $filtereSubGroups;
}
}