<?php
namespace App\Entity;
use App\Entity\User;
use App\Entity\Vehicle;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* Simple bid entity.
*
* @ORM\Entity(repositoryClass="App\Repository\BidRepository")
*
* @ORM\Table(
* name="auction_bid",
* indexes={
*
* @ORM\Index(columns={"amount"}),
* @ORM\Index(columns={"created_at"}),
* @ORM\Index(columns={"bidded_at"}),
* @ORM\Index(columns={"timestamp"})
* }
* )
*
* @ORM\HasLifecycleCallbacks
*/
class Bid
{
public const ORIGIN_PI = 'OA';
public const ORIGIN_LIVE = 'Live';
public const ORIGIN_ROOM = 'Salle';
public const ORIGIN_ONLINE = 'Online';
public const ORIGIN_ANIMATION = 'Animation';
public const ORIGIN_AFTERSALE = 'Après-vente';
public const ORIGIN_INTERENCHERES = 'Interenchères';
public const ORIGIN_BID_FOR_USER = 'Pour un utilisateur';
public const STATUS_EXCEEDED = 'Dépassé';
public const STATUS_RESERVE_NOT_MET = 'Réserve non atteinte';
public const STATUS_WINNING = 'Gagnant';
public const STATUS_AUCTION_NOT_STARTED = 'Avant enchère';
/**
* @ORM\Id
*
* @ORM\Column(type="bigint")
*
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\OneToOne(targetEntity="App\Entity\Bid")
*
* @ORM\JoinColumn(name="previousBid_id", referencedColumnName="id")
*/
protected $previousBid;
/**
* Bidding user.
*
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="bids", cascade={"persist"})
*
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Auction", inversedBy="bids", cascade={"persist"})
*
* @ORM\JoinColumn(name="auction_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
*/
protected $auction;
/**
* @ORM\Column(type="decimal", precision=10, scale=2)
*/
protected $amount;
/**
* @ORM\Column(type="boolean")
*/
protected $paysVat;
/**
* @Gedmo\Timestampable(on="create")
*
* @ORM\Column(type="datetime", name="created_at")
*/
protected $createdAt;
/**
* @Gedmo\Timestampable(on="update")
*
* @ORM\Column(type="datetime", name="bidded_at")
*/
protected $biddedAt;
/**
* @ORM\Column(type="float")
*/
protected $timestamp;
/**
* @ORM\Column(type="integer", name="paused_Ms")
*/
protected $pausedMs = 0;
/**
* @ORM\Column(type="string", nullable=true)
*/
protected $origin;
public function __construct($user, $amount)
{
if (null !== $user) {
$this->setUser($user);
}
$this->setAmount($amount);
$this->paysVat = false;
$this->origin = self::ORIGIN_ONLINE;
}
/**
* @return Bid
*/
public static function createPurchasedBid(User $user, Bid $next, Auction $auction, $pricePitch)
{
$surbid = new self($user, $next->getAmount() + $pricePitch);
$surbid->setPrevious($next);
$surbid->setAuction($auction);
$surbid->setBiddedAt($next->getBiddedAt());
$surbid->setPausedMs($next->getPausedMs());
$surbid->setOrigin(self::ORIGIN_PI);
return $surbid;
}
/**
* @return int
*/
public function getId()
{
return $this->id;
}
public function setPrevious(?Bid $previous = null): void
{
$this->previousBid = $previous;
}
/**
* Returns the bidder.
*
* @return User
*/
public function getUser()
{
return $this->user;
}
/**
* Sets the bidder.
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* Returns the auction.
*
* @return Auction
*/
public function getAuction()
{
return $this->auction;
}
/**
* Sets the auction.
*/
public function setAuction(?Auction $auction = null)
{
$this->auction = $auction;
return $this;
}
public function getAmount()
{
return $this->amount;
}
public function setAmount($amount): void
{
$this->amount = $amount;
}
public function getNextAmount()
{
return $this->amount + $this->auction->getVehicle()->getSale()->getBidIncrement();
}
public function getPaysVat()
{
return $this->paysVat;
}
public function setPaysVat($paysVat): void
{
$this->paysVat = $paysVat;
}
/**
* Return time of creation.
*
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set time of creation.
*/
public function setCreatedAt(\DateTime $createdAt): void
{
$this->createdAt = $createdAt;
}
/**
* Returns time of bidding.
*
* @return \DateTime
*/
public function getBiddedAt()
{
return $this->biddedAt;
}
/**
* Sets time of bidding.
*/
public function setBiddedAt(\DateTime $biddedAt): void
{
$this->biddedAt = $biddedAt;
}
public function getTimestamp()
{
return $this->timestamp;
}
/**
* @ORM\PrePersist
*
* @ORM\PreUpdate
*/
public function updateTimestamp(): void
{
[$msec, $sec] = explode(' ', microtime());
$this->timestamp = (float) $msec + (float) $sec;
}
public function getPausedMs()
{
return $this->pausedMs;
}
public function setpausedMs($pausedMs)
{
$this->pausedMs = $pausedMs;
return $this;
}
public function getOrigin()
{
return $this->origin;
}
public function setOrigin($origin): void
{
$this->origin = $origin;
}
public function getStatus()
{
if ($this->getAuction()->getOpenedAt() > new \DateTime()) {
return self::STATUS_AUCTION_NOT_STARTED;
}
if ($this->getUser() === $this->getAuction()->getVehicle()->getWinner()) {
if (
in_array($this->getVehicle()->getSale()->getSalingType(), [Sale::SALING_STATE_MIXED, Sale::SALING_STATE_BUYOUT])
&& $this->getVehicle()->getBuyoutPrice() > 0
&& $this->getAmount() >= $this->getVehicle()->getBuyoutPrice()
) {
return self::STATUS_WINNING;
}
if ($this->getAmount() >= $this->getAuction()->getVehicle()->getReservePrice()) {
return self::STATUS_WINNING;
}
return self::STATUS_RESERVE_NOT_MET;
}
return self::STATUS_EXCEEDED;
}
/**
* @return Vehicle
*/
public function getVehicle()
{
return $this->auction->getVehicle();
}
}