<?php
/**
* @license proprietary
* This code is part of vidi portal application
*/
namespace CoreBundle\Model\Vehicles;
use CoreBundle\Entity\Rate;
use CoreBundle\Entity\Vehicles\Equipment;
use CoreBundle\Entity\Vehicles\Variation;
use CoreBundle\Entity\Vehicles\VariationCharacteristic;
use CoreBundle\Entity\Vehicles\VehicleItem;
class UsedVehicle extends AbstractVehicle
{
private static $VehicleItems = [];
/**
* @param $locale
* @return array
*/
public function getOptionsArray($locale)
{
$equipment = $this->getVehicleEntity()->getEquipments()->first();
$options = $equipment->getOptions();
$result = [];
/** @var \CoreBundle\Entity\Vehicles\EquipmentOptions $option */
foreach ($options as $option) {
$oneOption = $option->getOption();
$oType = $option->getOption()->getOptionType();
if (!isset($result[$oType])) {
$result[$oType] = [
'id' => $oType,
'title' => EquipmentOptions::getTypeName($oType, $locale),
'options' => []
];
}
if (!isset($result[$oType]['options'][$oneOption->getId()])) {
$opTitle = ($locale == 'ru') ? $oneOption->getTitleRu() : $oneOption->getTitleUa();
$opValue = ($locale == 'ru') ? $option->getValueRu() : $option->getValueUa();
$result[$oType]['options'][$oneOption->getId()] = [
'id' => $oneOption->getId(),
'title' => $opTitle,
'value' => $opValue
];
}
$opValue = ($locale == 'ru') ? $option->getValueRu() : $option->getValueUa();
$result[$oType]['options'][$oneOption->getId()]['values'][] = [
'price' => $option->getPrice(),
'value' => $opValue,
];
}
return $result;
}
public function getOptions($locale)
{
$options = [];
if ($this->getVehicleEntity()->getAutoRiaOptions()) {
$autoriaOptions = $this->getVehicleEntity()->getAutoRiaOptions()->toArray();
foreach ($autoriaOptions as $option){
$options[$option->getAutoriaId()] = $option->getTitle($locale);
}
}
if (!$options) {
$options = json_decode($this->getVehicleEntity()->getRiaOptions(),true);
}
return $options;
}
/**
* @return float
*/
public function getVehicleItemPrice()
{
return $this->getVehicleItem()->getPrice();
}
/**
* @return bool|VehicleItem
*/
public function getVehicleItem()
{
$Vehicle = $this->getVehicleEntity();
if (!isset(self::$VehicleItems[$Vehicle->getId()])) {
$VehicleItem = $Vehicle->getVehicleItems()->first();
if (!$VehicleItem) {
return false;
}
self::$VehicleItems[$Vehicle->getId()] = $VehicleItem;
}
return self::$VehicleItems[$Vehicle->getId()];
}
public function getRecommend()
{
return $this->getVehicleEntity()->getRecommend();
}
public function getSold()
{
return $this->getVehicleItem()->getSold();
}
public function getHasNds()
{
return $this->getVehicleItem()->getHasNds();
}
public function getDeposit()
{
return $this->getVehicleItem()->getDeposit();
}
public function hasDeposit()
{
return $this->getVehicleItem()->getDeposit();
}
public function getYear()
{
return $this->getVehicleItem()->getYear();
}
public function price()
{
$VehicleItem = $this->getVehicleItem();
if (!$VehicleItem) {
return 0;
}
if($VehicleItem->getSold() && $VehicleItem->getPrice() < 20000) {
return $VehicleItem->getPrice() * $VehicleItem->getVehicle()->getDealer()->getRate();
}
if ($VehicleItem->getAltPrice()) {
return $VehicleItem->getAltPrice();
}
//TODO костыль для JLR
if (in_array($VehicleItem->getVehicle()->getDealer()->getId(), [32, 33])) {
return $VehicleItem->calcPrice();
$rate = $this->em->getRepository(Rate::class)->getBiggestRate()->getRate();
return ceil($VehicleItem->getPrice() * $rate);
}
return $VehicleItem->getPrice();
}
/**
* @return Variation
*/
public function getVariation()
{
return $this->getVehicleItem()->getVariation();
}
/**
* @return Equipment
*/
public function getEquipment()
{
return $this->getVehicleItem()->getEquipment();
}
public function fullPrice()
{
$VehicleItem = $this->getVehicleItem();
//TODO костыль для JLR
if (in_array($VehicleItem->getVehicle()->getDealer()->getId(), [32, 33])) {
return ceil($VehicleItem->getPrice() * $VehicleItem->getVehicle()->getDealer()->getRate());
}
return $VehicleItem->getPrice();
}
public function hasActionPrice()
{
return $this->price() < $this->fullPrice();
}
public function getActionPercent()
{
if (!$this->hasActionPrice()) {
return 0;
}
return ceil(100 - ($this->price() * 100 / $this->fullPrice()));
}
public function getSingleOwner()
{
$VehicleItem = $this->getVehicleItem();
return $VehicleItem ? $VehicleItem->getSingleOwner() : false;
}
public function getNoAccident()
{
$VehicleItem = $this->getVehicleItem();
return $VehicleItem ? $VehicleItem->getNoAccident() : false;
}
public function getBuyInUkraine()
{
$VehicleItem = $this->getVehicleItem();
return $VehicleItem ? $VehicleItem->getBuyInUkraine() : false;
}
public function getServiceBook()
{
$VehicleItem = $this->getVehicleItem();
return $VehicleItem ? $VehicleItem->getServiceBook() : false;
}
public function hasReserved()
{
return $this->getVehicleItem()->getIsReserved();
}
public function creditMinAmount()
{
$VehicleItem = $this->getVehicleItem();
return $VehicleItem ? $VehicleItem->getCreditMinAmount() : false;
}
public function creditPercent()
{
$VehicleItem = $this->getVehicleItem();
return $VehicleItem ? $VehicleItem->getCreditPercent() : false;
}
public function getProgram()
{
$VehicleItem = $this->getVehicleItem();
return $VehicleItem ? $VehicleItem->getProgramm() : false;
}
public function characteristicByGroup($locale)
{
$variation = $this->getVehicleItem()->getVariation();
$characteristic = $variation->getCharacteristics();
$result = [];
/** @var VariationCharacteristic $vCharacteristic */
foreach ($characteristic as $vCharacteristic) {
if (!$vCharacteristic->getValue($locale)) {
continue;
}
$char = $vCharacteristic->getCharacteristic();
$group = $char->getGroup();
//TODO move to config + rewrite
$groupId = $group ? $group->getId() : 'other';
$groupTitle = $group ? $group->getTitle($locale) : 'Другие характеристики';
if (!isset($result[$groupId])) {
$result[$groupId] = [
'id' => $groupId,
'title' => $groupTitle,
'characteristics' => [],
];
}
$result[$groupId]['characteristics'][] = [
'title' => $char->getTitle($locale),
'value' => $vCharacteristic->getValue($locale),
];
}
return $result;
}
}