src/CoreBundle/Repository/BrandRepository.php line 25

Open in your IDE?
  1. <?php
  2. /**
  3.  * @license proprietary
  4.  * This code is part of vidi portal application
  5.  */
  6. namespace CoreBundle\Repository;
  7. use DateTime;
  8. use CoreBundle\Entity\Brand;
  9. use CoreBundle\Entity\Dealer;
  10. use Doctrine\ORM\EntityRepository;
  11. use Doctrine\ORM\NonUniqueResultException;
  12. use Doctrine\ORM\NoResultException;
  13. use Doctrine\ORM\Query\Expr\Join;
  14. class BrandRepository extends EntityRepository
  15. {
  16.     /**
  17.      * @param bool $withoutImage
  18.      * @param null $state
  19.      * @return array
  20.      */
  21.     public function getAvailBrands($withoutImage false$state null$vehicleType null$hasNDS null)
  22.     {
  23.         $query $this->createQueryBuilder('b')
  24.             ->select('b, count(v.id) as cnt, MIN(case when d.position > 0 then d.position else 99 end) as position')
  25.             ->innerJoin('b.models''m')
  26.             ->innerJoin('m.vehicles''v'Join::WITH'v.state = 1')
  27.             ->innerJoin('v.dealer''d')
  28.             ->innerJoin('v.preview''p')
  29.             ->innerJoin('v.vehicle_items''vi'Join::WITH'vi.state = 1')
  30.             ->orderBy('position''ASC')
  31.             ->groupBy('b.id');
  32.         if ($state !== null) {
  33.             $query->andWhere('v.is_used = :state')
  34.                 ->setParameter('state'$state);
  35.         }
  36.         if ($vehicleType && !is_array($vehicleType)) {
  37.             $query->andWhere('v.vehicle_type = :type')
  38.                 ->setParameter('type'$vehicleType);
  39.         }
  40.         if ($vehicleType && is_array($vehicleType)) {
  41.             $query->andWhere('v.vehicle_type in (:type)')
  42.                 ->setParameter('type'$vehicleType);
  43.         }
  44.         if (!$withoutImage) {
  45.             $query->innerJoin('b.image''i')
  46.                 ->addSelect('i.id as image');
  47.             $query->innerJoin('b.logo''logo')
  48.                 ->addSelect('logo');
  49.         } else {
  50.             $query->leftJoin('b.image''i')
  51.                 ->addSelect('i.id as image');
  52.             $query->leftJoin('b.logo''logo')
  53.                 ->addSelect('logo');
  54.         }
  55.         if ($hasNDS) {
  56.             $query->andWhere('vi.has_nds = 1');
  57.         }
  58.         $query->orderBy('b.name');
  59.         return $query->getQuery()->getResult();
  60.     }
  61.     public function getBrandList($brandId$brandName)
  62.     {
  63.         $query $this->createQueryBuilder('b')
  64.             ->select('b.uid_1c as brandID, b.name as brandName')
  65.             ->andWhere('b.uid_1c is not null');
  66.         if ($brandId) {
  67.             $query->andWhere('b.uid_1c = :brand')
  68.                 ->setParameter('brand'$brandId);
  69.         }
  70.         if ($brandName) {
  71.             $query->andWhere('b.name LIKE :name')
  72.                 ->setParameter('name''%'.$brandName.'%');
  73.         }
  74.         return $query->getQuery()->getArrayResult();
  75.     }
  76.     /**
  77.      * @param string $url
  78.      * @return bool
  79.      * @throws NoResultException
  80.      * @throws NonUniqueResultException
  81.      */
  82.     public function checkByUrl($url)
  83.     {
  84.         $brandCounts $this->createQueryBuilder('b')
  85.             ->select('count(b.id)')
  86.             ->where('b.url = :url')
  87.             ->setParameter('url'$url)
  88.             ->getQuery()
  89.             ->getSingleScalarResult();
  90.         return $brandCounts 0;
  91.     }
  92.     public function getVehicleBrands($vehicleType null$isUsed false)
  93.     {
  94.         $brands $this->createQueryBuilder('b')
  95.             ->innerJoin('b.models''m')
  96.             ->innerJoin('m.vehicles''v'Join::WITH'v.state = true and v.is_used = :isUsed')
  97.             ->setParameter('isUsed'$isUsed);
  98.         if($vehicleType){
  99.             $brands->andWhere('v.vehicle_type = :vehicleType')
  100.             ->setParameter('vehicleType'$vehicleType);
  101.         }
  102.         $brands->orderBy('b.name''ASC')
  103.             ->groupBy('b.id');
  104.         return $brands->getQuery()->getResult();
  105.     }
  106.     /**
  107.      * @param $vehicleType
  108.      * @return Brand[]
  109.      */
  110.     public function getUsedVehicleBrands($vehicleType)
  111.     {
  112.         return $this->createQueryBuilder('b')
  113.             ->innerJoin('b.models''m')
  114.             ->innerJoin('m.vehicles''vehicle'Join::WITH'vehicle.state = 1 and vehicle.is_used = 1')
  115.             ->orderBy('b.name''ASC')
  116.             ->groupBy('b.id')
  117.             ->getQuery()
  118.             ->getResult();
  119.     }
  120.     public function getByName($name)
  121.     {
  122.         return $this->createQueryBuilder('b')
  123.             ->where('LOWER(b.name) = :name')
  124.             ->setParameter('name'strtolower($name))
  125.             ->setMaxResults(1)
  126.             ->getQuery()
  127.             ->getOneOrNullResult();
  128.     }
  129.     public function getCreditAvailBrand()
  130.     {
  131.         $query $this->createQueryBuilder('b')
  132.             ->select('b')
  133.             ->innerJoin('b.logo''l')
  134.             ->innerJoin('b.dealer''d')
  135.             ->innerJoin('b.models''m')
  136.             ->innerJoin('m.vehicles''vh'Join::WITH'vh.state = 1')
  137.             ->innerJoin('vh.variations''v'Join::WITH'v.state = 1');
  138.     }
  139.     /**
  140.      * @param bool $withoutUsedCar
  141.      * @param bool $with_credit_only
  142.      * @return array
  143.      */
  144.     public function getAvailBrandsInstance($withoutUsedCar false$with_credit_only falsebool $filterPalmira false)
  145.     {
  146.         $query $this->createQueryBuilder('b')
  147.             ->select('b');
  148.         if ($filterPalmira) {
  149.             $query->addSelect('d')
  150.                 ->where('d.id != :toyotaPalmiraId')
  151.                 ->setParameter('toyotaPalmiraId'7);
  152.         }
  153.         $query->innerJoin('b.logo''l')
  154.             ->innerJoin('b.dealer''d')
  155.             ->innerJoin('b.models''m')
  156.             ->innerJoin('m.vehicles''v'Join::WITH'v.state = 1')
  157.             ->innerJoin('v.vehicle_items''vi'Join::WITH'vi.state = 1');
  158.         if ($with_credit_only) {
  159.             $query->andWhere('v.credit_available = 1');
  160.         }
  161.         $query
  162.             ->orderBy('d.position''ASC')
  163.             ->groupBy('b.id''d.id');
  164.         if ($withoutUsedCar !== null) {
  165.             $query->andWhere('v.is_used = 0');
  166.         }
  167.         return $query->getQuery()->getResult();
  168.     }
  169.     /**
  170.      * @param bool $withoutUsedCar
  171.      * @param bool $withCreditOnly
  172.      * @return array
  173.      */
  174.     public function getCarAvailBrandsInstance($isUsed false$withCreditOnly falseDealer $dealer null$isAutomarket false): array
  175.     {
  176.         $query $this->createQueryBuilder('b')
  177.                 ->select('b''MIN(
  178.                    CASE
  179.                     WHEN (
  180.                         CASE
  181.                             WHEN ((v.is_used = 0 AND d.id != 6) OR (v.is_used = 1 AND d.id = 33))
  182.                             THEN (
  183.                                 CASE
  184.                                     WHEN vi.alt_price > 0 THEN vi.alt_price
  185.                                     WHEN vi.alt_usd_price > 0 THEN vi.alt_usd_price * d.rate
  186.                                     WHEN vi.alt_rate > 0 THEN vi.alt_rate * vi.price
  187.                                     ELSE vi.price * d.rate
  188.                                 END
  189.                             )
  190.                             ELSE (
  191.                                 CASE
  192.                                     WHEN vi.alt_price > 0 THEN vi.alt_price
  193.                                     WHEN vi.alt_usd_price > 0 THEN vi.alt_usd_price * d.rate
  194.                                     ELSE vi.price
  195.                                 END
  196.                             )
  197.                         END
  198.                     ) = 0
  199.                     THEN 9999999999
  200.                     ELSE (
  201.                         CASE
  202.                             WHEN ((v.is_used = 0 AND d.id != 6) OR (v.is_used = 1 AND d.id = 33))
  203.                             THEN (
  204.                                 CASE
  205.                                     WHEN vi.alt_price > 0 THEN vi.alt_price
  206.                                     WHEN vi.alt_usd_price > 0 THEN vi.alt_usd_price * d.rate
  207.                                     WHEN vi.alt_rate > 0 THEN vi.alt_rate * vi.price
  208.                                     ELSE vi.price * d.rate
  209.                                 END
  210.                             )
  211.                             ELSE (
  212.                                 CASE
  213.                                     WHEN vi.alt_price > 0 THEN vi.alt_price
  214.                                     WHEN vi.alt_usd_price > 0 THEN vi.alt_usd_price * d.rate
  215.                                     ELSE vi.price
  216.                                 END
  217.                             )
  218.                         END
  219.                     )
  220.                 END
  221.             ) as min_price')
  222.             ->innerJoin('b.models''m')
  223.             ->innerJoin('m.vehicles''v'Join::WITH'v.state = 1')
  224.             ->innerJoin('v.vehicle_items''vi'Join::WITH'vi.state = 1 AND v.vehicle_type = 1')
  225.             ->innerJoin('vi.vehicle''ve')
  226.             ->innerJoin('ve.dealer''d')
  227.             ->andWhere('v.is_used = :isUsed')
  228.             ->setParameter('isUsed'$isUsed);
  229.         if ($withCreditOnly) {
  230.             $query->andWhere('v.credit_available = 1');
  231.         }
  232.         if ($isUsed) {
  233.             $query->andWhere('vi.is_reserved = 0');
  234.             $query->andWhere('vi.deposit = 0');
  235.             $query->andWhere('vi.sold = 0');
  236.         }
  237.         if ($dealer && $isUsed) {
  238.             $query->andWhere('v.dealer = :dealer')->setParameter('dealer'$dealer);
  239.         }
  240.         if ($isAutomarket) {
  241.            $query ->andWhere('vi.state = 1')
  242.                 ->andWhere('ve.state = 1')
  243.                 ->andWhere('ve.vehicle_type = :type')
  244.                 ->andWhere('ve.is_not_filled = 0')
  245.                 ->andWhere('ve.is_delete != 1')
  246.                 ->andWhere('ve.is_used = :isUsed')
  247.                 ->andWhere('vi.date_of_sale > :mounthAgo OR vi.date_of_sale IS NULL')
  248.                 ->setParameter('type'1)
  249.                 ->setParameter('mounthAgo', (new DateTime())->modify('-30 days')->format('Y-m-d H:i:s'));
  250.         }
  251.         $query->groupBy('b.id');
  252.         $query->orderBy('b.name');
  253.         return $query->getQuery()->getResult();
  254.     }
  255.     /**
  256.      * @param false $withoutUsedCar
  257.      * @param false $with_credit_only
  258.      * @return array|int|string
  259.      */
  260.     public function getAvailBrandsInstanceName($withoutUsedCar false$with_credit_only false): array
  261.     {
  262.         $query $this->createQueryBuilder('b')
  263.             ->select('b.name')
  264.             ->innerJoin('b.logo''l')
  265.             ->innerJoin('b.dealer''d')
  266.             ->innerJoin('b.models''m')
  267.             ->innerJoin('m.vehicles''v'Join::WITH'v.state = 1')
  268.             ->innerJoin('v.vehicle_items''vi'Join::WITH'vi.state = 1');
  269.         if ($with_credit_only) {
  270.             $query->andWhere('v.credit_available = 1');
  271.         }
  272.         $query
  273.             ->orderBy('d.position''ASC')
  274.             ->groupBy('b.id''d.id');
  275.         if ($withoutUsedCar !== null) {
  276.             $query->andWhere('v.is_used = 0');
  277.         }
  278.         return array_column($query->getQuery()->getResult(), 'name');
  279.     }
  280.     public function getByDealer(Dealer $dealer)
  281.     {
  282.         return $this->createQueryBuilder('b')
  283.             ->select('b.id,b.name')
  284.             ->innerJoin('b.models''m')
  285.             ->innerJoin('m.vehicles''vh'Join::WITH'vh.state = 1 and vh.dealer = :dealer')
  286.             ->innerJoin('vh.preview''p')
  287.             ->setParameter('dealer'$dealer)
  288.             ->groupBy('b.id')
  289.             ->getQuery()->getResult();
  290.     }
  291.     public function getBrandByUrl($url){
  292.         return $this->createQueryBuilder('b')
  293.             ->andWhere('b.url = :url')->setParameter('url'$url)
  294.             ->andWhere('b.url is not null')
  295.             ->getQuery()->getOneOrNullResult();
  296.     }
  297.     public function getBrandsByUrl($url){
  298.         return $this->createQueryBuilder('b')
  299.             ->andWhere('b.url IN (:url)')->setParameter('url'$url)
  300.             ->andWhere('b.url is not null')
  301.             ->getQuery()->getResult();
  302.     }
  303.     public function getBrandsHasRegulation(){
  304.         return $this->createQueryBuilder('b')
  305.             ->innerJoin('b.models''m')
  306.             ->innerJoin('m.service_variations','sv')
  307.             ->innerJoin('sv.service_works''sw')
  308.             ->where('sw.is_regulations = :isRegulations')->setParameter('isRegulations'true)
  309.             ->getQuery()->getResult();
  310.     }
  311. }