<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\SaleEventRepository")
*
* @ORM\Table(name="sale_event")
*/
class SaleEvent
{
public const PERIOD_AM = 'am';
public const PERIOD_PM = 'pm';
public const PERIOD_UNDEFINED = 'nc';
/**
* @ORM\Id
*
* @ORM\GeneratedValue(strategy="AUTO")
*
* @ORM\Column(type="bigint")
*
* @Groups({"read_sale_event"})
*/
protected $id;
/**
* @ORM\Column(type="string")
*
* @Assert\NotBlank(groups={"api_rms_saleevent"})
*
* @Groups({"read_sale_event"})
*/
protected $startTime;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\SaleEventTitle", cascade={"persist"})
*
* @ORM\JoinColumn(name="sale_event_title_id", referencedColumnName="id", nullable=true)
*
* @Groups({"read_sale_event"})
*/
private $title;
/**
* @ORM\Column(type="string")
*
* @Assert\NotBlank(groups={"api_rms_saleevent"})
*
* @Groups({"read_sale_event"})
*/
private $period;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Sale", inversedBy="events", fetch="EAGER", cascade={"persist"})
*
* @ORM\JoinColumn(nullable=false)
*
* @Groups({"read_sale_event"})
*/
private $sale;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Vehicle", mappedBy="event", cascade={"persist"})
*/
private $vehicles;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $lotsImport;
public function __construct()
{
$this->vehicles = new ArrayCollection();
$this->lotsImport = 0;
}
public function getId()
{
return $this->id;
}
public function getStartTime()
{
return $this->startTime;
}
public function setStartTime($startTime): void
{
$this->startTime = $startTime;
}
public function getTitle()
{
return $this->title;
}
public function setTitle($title): void
{
$this->title = $title;
}
public function getPeriod()
{
return $this->period;
}
public function setPeriod($period): void
{
$this->period = $period;
}
public function setSale(Sale $sale): void
{
$this->sale = $sale;
}
public function getSale()
{
return $this->sale;
}
public function addVehicle(Vehicle $vehicle)
{
$this->vehicles[] = $vehicle;
$vehicle->setEvent($this);
return $this;
}
public function removeVehicle(Vehicle $vehicle): void
{
$this->vehicles->removeElement($vehicle);
}
public function getVehicles()
{
return $this->vehicles;
}
public function isLotsImport()
{
return (bool) $this->lotsImport;
}
public function setLotsImport($lotsImport): void
{
$this->lotsImport = $lotsImport;
}
public function getActiveVehicles()
{
$criteria = Criteria::create();
$criteria->where(
Criteria::expr()->neq('salingState', Vehicle::SALING_STATE_EXCLUS)
)
->andWhere(
Criteria::expr()->neq('salingState', Vehicle::SALING_STATE_SUPPRIME)
)
->andWhere(
Criteria::expr()->neq('salingState', Vehicle::SALING_STATE_NO_STARTING_PRICE)
);
return $this->getVehicles()->matching($criteria);
}
public function getLabel()
{
return $this->getTitle()->getName();
}
public function getStartDateTime()
{
return new \DateTime($this->sale->getStartDate()->format('Y-m-d').' '.$this->startTime);
}
public function isEmpty()
{
if (0 === count($this->getVehicles())) {
return true;
}
$criteria = Criteria::create();
$criteria
->where(
Criteria::expr()->eq('salingState', Vehicle::SALING_STATE_SUPPRIME)
)
;
return count($this->getVehicles()) === count($this->getVehicles()->matching($criteria));
}
}