src/CoreBundle/Model/OnlineEstimateModel.php line 469

Open in your IDE?
  1. <?php
  2. namespace CoreBundle\Model;
  3. use DateTime;
  4. use CoreBundle\Entity\User;
  5. use CoreBundle\Entity\Dealer;
  6. use CoreBundle\Entity\VehicleEstimate;
  7. use CoreBundle\Factory\Vehicle;
  8. use CoreBundle\Model\Api\AutoRia\AutoRia;
  9. use CoreBundle\Model\Vehicles\Repository;
  10. use DcSiteBundle\Entity\Markdown;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Doctrine\ORM\OptimisticLockException;
  13. class OnlineEstimateModel
  14. {
  15.     /**
  16.      * @var Rate
  17.      */
  18.     private $rateModel;
  19.     /**
  20.      * @var EntityManagerInterface
  21.      */
  22.     private $em;
  23.     /**
  24.      * @var AutoRia
  25.      */
  26.     private $AutoRia;
  27.     /**
  28.      * OnlineEstimateModel constructor.
  29.      * @param Rate $rate
  30.      * @param EntityManagerInterface $em
  31.      * @param Vehicle $VehicleFactory
  32.      * @param Repository $repository
  33.      */
  34.     public function __construct(Rate $rateEntityManagerInterface $emAutoRia $AutoRia)
  35.     {
  36.         $this->rateModel $rate;
  37.         $this->em $em;
  38.         $this->AutoRia $AutoRia;
  39.     }
  40.     private function isSelect($params$dealer null$isCRM false) {
  41.         $old = (new DateTime())->format('Y')-$params['year'];
  42.         $mileage explode('-'$params['mileage']);
  43.         if (count($mileage) == 2) {
  44.             $mileageTo $mileage[1];
  45.         } else {
  46.             $mileage $params['mileage'] < 1000 $params['mileage'] : ceil($params['mileage']/1000);
  47.             $mileageTo $mileage + ($mileage*0.60);
  48.         }
  49.         $dealerInProgram = !$dealer || in_array($dealer->getId(), [8,27,35,34,3,29,28,32,33,37,38,15,30,9,31,39]);
  50.         $isOurBrand in_array($params['brand']['id'], [55,47,58,75,24,15,76,31,37,28,128,85,56,5456,49]);
  51.         $inOld $old <=5;
  52.         $inMileage $mileageTo <= 100;
  53.         return $dealerInProgram && $isOurBrand && $inOld && $inMileage;
  54.     }
  55.     public function getPrices($estimate$params)
  56.     {
  57.         $itemIds = [];
  58.         foreach ($estimate->result->search_result_common->data as $row) {
  59.             if($row->type == 'UsedAuto') {
  60.                 $itemIds[] = $row->id;
  61.             }
  62.         }
  63.         $fullItems = [];
  64.         $fuelId $params['fuel'];
  65.         foreach ($itemIds as $id) {
  66.             $data $this->AutoRia->getFullInfo($id);
  67.             if(!$data) {
  68.                 continue;
  69.             }
  70.             if(in_array($fuelId,[1,2,3,4]) && !in_array($data->autoData->fuelId,[1,2,3,4])) {
  71.                 continue;
  72.             }
  73.             $item['price'] = $data->UAH;
  74.             $item['year'] = $data->autoData->year;
  75.             $item['raceInt'] = $data->autoData->raceInt;
  76.             $item['fuelId'] = $data->autoData->fuelId;
  77.             $item['gearBoxId'] = $data->autoData->gearBoxId ?? null;
  78.             $fullItems[$id] = $item;
  79.         }
  80.         $yCof = [
  81.             '+' => [
  82.                 2022 => 0.85,
  83.                 2021 => 0.9,
  84.                 2020 => 0.93,
  85.                 2019 => 0.94,
  86.                 2018 => 0.94,
  87.                 2017 => 0.95,
  88.                 2016 => 0.95,
  89.                 2015 => 0.96,
  90.                 2014 => 0.96,
  91.                 2013 => 0.97,
  92.                 2012 => 0.97,
  93.                 2011 => 0.98,
  94.                 2010 => 0.98,
  95.                 2009 => 0.99,
  96.                 2008 => 0.99,
  97.             ],
  98.             '-' => [
  99.                 2021 => 1.15,
  100.                 2020 => 1.1,
  101.                 2019 => 1.07,
  102.                 2018 => 1.06,
  103.                 2017 => 1.06,
  104.                 2016 => 1.05,
  105.                 2015 => 1.05,
  106.                 2014 => 1.04,
  107.                 2013 => 1.04,
  108.                 2012 => 1.03,
  109.                 2011 => 1.03,
  110.                 2010 => 1.02,
  111.                 2009 => 1.02,
  112.                 2008 => 1.01,
  113.                 2007 => 1.01,
  114.             ]
  115.         ];
  116.         foreach($fullItems as $id => $item) {
  117.             $Cyear $params['year'];
  118.             $mYear $params['year']-1;
  119.             $pYear $params['year']+1;
  120.             $uCoh false;
  121.             if($item['year'] == $Cyear) {
  122.                 $uCoh 1;
  123.             }
  124.             if($item['year'] == $mYear) {
  125.                 if(isset($yCof['-'][$mYear])) {
  126.                     $uCoh $yCof['-'][$mYear];
  127.                 }
  128.             }
  129.             if($item['year'] == $pYear) {
  130.                 if(isset($yCof['+'][$pYear])) {
  131.                     $uCoh $yCof['+'][$pYear];
  132.                 }
  133.             }
  134.             if(!$uCoh) {
  135.                 unset($fullItems[$id]);
  136.                 continue;
  137.             }
  138.             $fullItems[$id]['newPrice'] = $fullItems[$id]['price']*$uCoh;
  139.             if($fuelId == && in_array($item['fuelId'],[1,3,4])) {
  140.                 $fullItems[$id]['newPrice'] *= 1.07;
  141.             }
  142.             if($item['fuelId'] == && in_array($fuelId,[1,3,4])) {
  143.                 $fullItems[$id]['newPrice'] /= 1.07;
  144.             }
  145.         }
  146.         $raceC = [
  147.             '+' => [
  148.                 '0-50' => false,
  149.                 '51-100' => 1.1,
  150.                 '101-150' => 1.05,
  151.                 '151-200' => 1.05,
  152.                 '201-500' => 1.1,
  153.             ],
  154.             '-' => [
  155.                 '0-50' => 0.9,
  156.                 '51-100' => 0.95,
  157.                 '101-150' => 0.95,
  158.                 '151-200' => 0.95,
  159.             ],
  160.         ];
  161.         $mileage explode('-'$params['mileage']);
  162.         $mileageFrom $mileage[0];
  163.         $mileageTo $mileage[1];
  164.         $pKeys array_keys($raceC['+']);
  165.         $mKeys array_keys($raceC['-']);
  166.         $currentPIndex array_search($params['mileage'],$pKeys);
  167.         $currentMIndex array_search($params['mileage'],$mKeys);
  168.         $pCofIndex false;
  169.         $mCofIndex false;
  170.         if($currentPIndex !== false && $currentPIndex && isset($pKeys[$currentPIndex-1])) {
  171.             $pCofIndex $pKeys[$currentPIndex-1];
  172.         }
  173.         if($currentMIndex !== false && isset($mKeys[$currentMIndex+1])) {
  174.             $mCofIndex $mKeys[$currentMIndex+1];
  175.         }
  176.         foreach($fullItems as $id => $item) {
  177.             $race $item['raceInt'];
  178.             $rCof false;
  179.             if($race >= $mileageFrom && $race <= $mileageTo) {
  180.                 $rCof 1;
  181.             }
  182.             if($pCofIndex) {
  183.                 $arr explode('-',$pCofIndex);
  184.                 if($race >= $arr[0] && $race <= $arr[1]) {
  185.                     $rCof $raceC['+'][$pKeys[$currentPIndex]];
  186.                 }
  187.             }
  188.             if($mCofIndex) {
  189.                 $arr explode('-',$mCofIndex);
  190.                 if($race >= $arr[0] && $race <= $arr[1]) {
  191.                     $rCof $raceC['-'][$mKeys[$currentMIndex]];
  192.                 }
  193.             }
  194.             if(!$rCof) {
  195.                 unset($fullItems[$id]);
  196.                 continue;
  197.             }
  198.             $fullItems[$id]['newPrice'] *= $rCof;
  199.         }
  200.         return $fullItems;
  201.     }
  202.     public function updateByNew($params,$result)
  203.     {
  204.         $ageCar = (date('Y') - $params['year']) * 12;
  205.         $ageCar $ageCar == $ageCar;
  206.         if ($ageCar <= 60) {
  207.             $apiParams = [
  208.                 'markaId' => [$params['brand']['id']],
  209.                 'modelId' => [$params['model']['id']]
  210.             ];
  211.             if (isset($params['fuel']) && $params['fuel']) {
  212.                 $fuelId $this->AutoRia->getFuelApi($params['fuel']);
  213.                 $apiParams array_merge($apiParams, ['fuelId' => $fuelId]);
  214.             }
  215.             if (isset($params['transmission']) && $params['transmission']) {
  216.                 $gearId $this->AutoRia->getGearApi($params['transmission']);
  217.                 $apiParams array_merge($apiParams, ['gearId' => $gearId]);
  218.             }
  219.             $cars $this->AutoRia->getCars($apiParams);
  220.             /** @var Markdown $markdown */
  221.             $markdown $this->em->getRepository(Markdown::class)->findOneBy(['ria_model_id' => $params['model']['id']]);
  222.             if(!$markdown) {
  223.                 $markdown $this->em->getRepository(Markdown::class)->findOneBy(['ria_brand_id' => $params['brand']['id']]);
  224.             }
  225.             if ($cars && count($cars->autos) > && $markdown) {
  226.                 $avgPrice 0;
  227.                 foreach ($cars->autos as $item) {
  228.                     $avgPrice += $item->price;
  229.                 }
  230.                 $avgPrice /= count($cars->autos);
  231.                 //$avgPrice = array_sum(array_column($cars->autos, 'price')) / count($cars->autos);
  232.                 $getRetail 'getRetail' $ageCar;
  233.                 $markdownValue $markdown->$getRetail();
  234.                 $markdownPrice round($avgPrice - ($avgPrice $markdownValue 100));
  235.                 if ($result['isSelect']) {
  236.                     $newPrices $this->calcPricesUAH(0$markdownPrice);
  237.                 } else {
  238.                     $newPrices $this->calcPricesUAH($markdownPrice0);
  239.                 }
  240.                 $result['success'] = true;
  241.                 if($result['price']) {
  242.                     $result['price'] = ($markdownPrice $result['price']) /2;
  243.                     $result['priceTI'] = ($newPrices['priceTradeIn'] + $result['priceTI']) /2;
  244.                     $result['priceBuy'] = ($newPrices['priceBuy'] + $result['priceBuy']) /2;
  245.                     $result['priceSelect'] = ($newPrices['priceSelect'] + $result['priceSelect']) /2;
  246.                     $result['priceSelectBuy'] = ($newPrices['priceSelectBuy'] + $result['priceSelectBuy']) /2;
  247.                 } else {
  248.                     $result['price'] = $markdownPrice;
  249.                     $result['priceTI'] = $newPrices['priceTradeIn'];
  250.                     $result['priceBuy'] = $newPrices['priceBuy'];
  251.                     $result['priceSelect'] = $newPrices['priceSelect'];
  252.                     $result['priceSelectBuy'] = $newPrices['priceSelectBuy'];
  253.                 }
  254.             }
  255.         }
  256.         return $result;
  257.     }
  258.     public function processEstimateResultNew($result$params$dealer null$isCRM false)
  259.     {
  260.         $rate = (float) $this->rateModel->getRate();
  261.         $fullItems $this->getPrices($result$params);
  262.         $pricesArr = [];
  263.         foreach ($fullItems as $row) {
  264.             $pricesArr[] = ceil($row['newPrice']);
  265.         }
  266.         $price 0;
  267.         $priceSelect 0;
  268.         if (count($pricesArr) > || ($dealer && $dealer->getId() == 16) ) {
  269.             $priceSelect $this->averagePriceItems(40$pricesArrtrue);
  270.             $price $this->averagePriceItems(10$pricesArr);
  271.         }
  272.         $prices $this->calcPricesNew($price$priceSelect$this->isSelect($params,$dealer,$isCRM));
  273.         $vehicle = ($params['brand']['title'] ?? '').' '.($params['model']['title'] ?? '');
  274.         $year $params['year'];
  275.         $hash md5(time().$price.'salt-retds4346FE#R42');
  276.         $data = [
  277.             'isSelect' => $this->isSelect($params,$dealer),
  278.             'vehicle' => $vehicle,
  279.             'hash' => $hash,
  280.             'fullItems' => $fullItems,
  281.             'countItems' => is_array($fullItems) ? count($fullItems) : 0,
  282.             'year' => $year,
  283.             'odometer' => $params['mileage'],
  284.             'success' => $price 0,
  285.             'price' => $price,
  286.             'rate' => $rate,
  287.             'priceSelectBase' => $priceSelect,
  288.             'priceTI' => $prices['priceTradeIn'],
  289.             'priceBuy' => $prices['priceBuy'],
  290.             'priceSelect' => $prices['priceSelect'],
  291.             'priceSelectBuy' => $prices['priceSelectBuy'],
  292.         ];
  293.         return $this->updateByNew($params,$data);
  294.     }
  295.     /**
  296.      * @param $result
  297.      * @param $params
  298.      * @param null $dealer
  299.      * @return array
  300.      * @throws OptimisticLockException
  301.      */
  302.     public function processEstimateResult($result$params$dealer null)
  303.     {
  304.         $rate = (float) $this->rateModel->getRate();
  305.         $price 0;
  306.         $priceSelect 0;
  307.         if (count($result->prices) > || ($dealer && $dealer->getId() == 16) ) {
  308.             $priceSelect $this->averagePriceItems(50$result->pricestrue);
  309.             $price $this->averagePriceItems(30$result->pricesfalse);
  310.         }
  311.         $odd null;
  312.         $prices $this->calcPrices($price$priceSelect$odd$this->isSelect($params,$dealer));
  313.         $vehicle $params['brand']['title'].' '.$params['model']['title'];
  314.         $year $params['year'];
  315.         $hash md5(time().$price.'salt-retds4346FE#R42');
  316.         $data = [
  317.             'isSelect' => $this->isSelect($params,$dealer),
  318.             'vehicle' => $vehicle,
  319.             'hash' => $hash,
  320.             'year' => $year,
  321.             'odometer' => $params['mileage'],
  322.             'success' => $price 0,
  323.             'rate' => $rate,
  324.             'price' => $price,
  325.             'priceSelectBase' => $priceSelect,
  326.             'priceTI' => $prices['priceTradeIn'],
  327.             'priceBuy' => $prices['priceBuy'],
  328.             'priceSelect' => $prices['priceSelect'],
  329.             'priceSelectBuy' => $prices['priceSelectBuy'],
  330.         ];
  331.         return $data;
  332.     }
  333.     public function saveEstimateResult($params$dataDealer $dealer nullUser $User null$userCar null$fromCrm false) {
  334.         $estimate = new VehicleEstimate();
  335.         $estimateResultData = [
  336.             'request' => $params,
  337.             'result' => $data,
  338.         ];
  339.         $estimate->setDateCreate(new DateTime());
  340.         $estimate->setDealer($dealer ?? null);
  341.         $estimate->setIsFinish(0);
  342.         $estimate->setIsSelect($data['isSelect']);
  343.         $estimate->setPhone($params['phone'] ?? null);
  344.         $estimate->setEmail($params['email'] ?? 'CRM');
  345.         $estimate->setUtm($params['utm']);
  346.         $estimate->setHref($params['href']);
  347.         $estimate->setReferrer($params['referrer']);
  348.         $estimate->setGclId($params['gcl_id']);
  349.         $estimate->setName($params['name'] ?? null);
  350.         $estimate->setUser($User ?? null);
  351.         $estimate->setUserCar($userCar);
  352.         $estimate->setCrmState(0);
  353.         $estimate->setHash($data['hash']);
  354.         $estimate->setIsSend(0);
  355.         $estimate->setCrmState($fromCrm 0);
  356.         $estimate->setData(json_encode($estimateResultData));
  357.         $this->em->persist($estimate);
  358.         $this->em->flush();
  359.         return $estimate->getId();
  360.     }
  361.     /**
  362.      * @param VehicleEstimate $estimate
  363.      * @return array
  364.      * @throws OptimisticLockException
  365.      */
  366.     public function processEstimateEntity(VehicleEstimate $estimate)
  367.     {
  368.         $estimateData json_decode($estimate->getData());
  369.         return (array) $estimateData->result;
  370.     }
  371.     /**
  372.      * @param $price
  373.      * @param $priceSelect
  374.      * @param $odd
  375.      * @return array
  376.      * @throws OptimisticLockException
  377.      */
  378.     public function calcPricesNew($price$priceSelect$isSelect)
  379.     {
  380.         $odd 10;
  381.         $priceBuyInUSD round($price * ((100 $odd) / 100));
  382.         $priceBuySelectInUSD round($priceSelect * ((100 $odd) / 100));
  383.         $priceTradeInUSD round($priceBuyInUSD 1.02);
  384.         $priceSelectUSD round($priceBuySelectInUSD);
  385.         $priceBuy = (float) round($priceBuyInUSD);
  386.         $priceTradeIn = (float) round($priceTradeInUSD);
  387.         $priceSelect = (float) round($priceSelectUSD);
  388.         if($isSelect) {
  389.             $priceBuy round($priceSelect / (0.04));
  390.         }
  391.         $prices = [
  392.             'priceBuyInUSD' => $priceBuyInUSD,
  393.             'priceBuy' => $priceBuy,
  394.             'priceTradeIn' => $priceTradeIn,
  395.             'priceTradeInUSD' => $priceTradeInUSD,
  396.             'priceSelectUSD' => $priceSelectUSD,
  397.             'priceSelect' => $priceSelect,
  398.             'priceSelectBuy' => $priceSelect*0.98,
  399.             'priceUSD' => $price,
  400.         ];
  401.         return $prices;
  402.     }
  403.     /**
  404.      * @param $price
  405.      * @param $priceSelect
  406.      * @param $odd
  407.      * @return array
  408.      * @throws OptimisticLockException
  409.      */
  410.     public function calcPrices($price$priceSelect$odd null$isSelect)
  411.     {
  412.         if (is_null($odd)) {
  413.             $odd 10;
  414.         }
  415.         $rate $this->rateModel->getRate();
  416.         $priceBuyInUSD round($price * ((100 $odd) / 100));
  417.         $priceBuySelectInUSD round($priceSelect * ((100 $odd) / 100));
  418.         $priceTradeInUSD round($priceBuyInUSD 1.02);
  419.         $priceSelectUSD round($priceBuySelectInUSD);
  420.         $priceBuy = (float) round($priceBuyInUSD $rate);
  421.         $priceTradeIn = (float) round($priceTradeInUSD $rate);
  422.         $priceSelect = (float) round($priceSelectUSD $rate);
  423.         if($isSelect) {
  424.             $priceBuy round($priceSelect / (0.04));
  425.         }
  426.         $prices = [
  427.             'priceBuyInUSD' => $priceBuyInUSD,
  428.             'rate' => $rate,
  429.             'priceBuy' => $priceBuy,
  430.             'priceTradeIn' => $priceTradeIn,
  431.             'priceTradeInUSD' => $priceTradeInUSD,
  432.             'priceSelectUSD' => $priceSelectUSD,
  433.             'priceSelect' => $priceSelect,
  434.             'priceSelectBuy' => $priceSelect*0.98,
  435.             'priceUSD' => $price,
  436.         ];
  437.         return $prices;
  438.     }
  439.     public function calcPricesUAH($price$priceSelect$odd null)
  440.     {
  441.         if (is_null($odd)) {
  442.             $odd 10;
  443.         }
  444.         $priceBuy round($price * ((100 $odd) / 100));
  445.         $priceBuySelect round($priceSelect * ((100 $odd) / 100));
  446.         $priceBuyTradeIn round(($priceSelect $priceBuySelect $priceBuy) * 1.02);
  447.         if($priceSelect) {
  448.             $priceBuy $priceBuySelect 0.98;
  449.         }
  450.         return [
  451.             'priceBuy' => $priceBuy,
  452.             'priceTradeIn' => $priceBuyTradeIn,
  453.             'priceSelect' => $priceBuySelect,
  454.             'priceSelectBuy' => $priceBuySelect*0.98,
  455.         ];
  456.     }
  457.     private function averagePriceItems($percent$priceItems $isReverse false)
  458.     {
  459.         if(count($priceItems) > 3) {
  460.             $isReverse rsort($priceItems) : sort($priceItems);
  461.             $offset floor(count($priceItems) * 10 100);
  462.             $offset $offset $offset;
  463.             $limit floor(count($priceItems) * $percent 100);
  464.             $limit $limit $limit;
  465.             $length count($priceItems) - $offset $limit;
  466.             $priceItems array_slice($priceItems$offset$length);
  467.             return round(array_sum($priceItems) / count($priceItems));
  468.         }
  469.         else
  470.         {
  471.             return null;
  472.         }
  473.     }
  474. }