<?php
namespace App\Entity;
use App\Entity\User;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* Purchase instruction for physical sales.
*
* @author Paweł Jędrzejewski <pjedrzejewski@diweb.pl>
*
* @ORM\Entity(repositoryClass="App\Repository\PurchaseInstructionRepository")
*
* @ORM\Table(
* name="purchase_instruction",
* indexes={
*
* @ORM\Index(columns={"status"}),
* @ORM\Index(columns={"created_at"}),
* @ORM\Index(columns={"updated_at"}),
* @ORM\Index(columns={"amount"})
* }
* )
*/
class PurchaseInstruction
{
public const CANCELED = 'Annulé';
public const ACCEPTED = 'Accepté';
public const REFUSED = 'Refusé';
public const WON = 'Gagné';
public const LOST = 'Perdu';
public const ORIGIN_B2C = 'B2C';
public const ORIGIN_B2B = 'B2B';
/**
* @ORM\Id
*
* @ORM\Column(type="bigint")
*
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* Related vehicle.
*
* @ORM\ManyToOne(targetEntity="App\Entity\Vehicle", inversedBy="purchaseInstructions")
*
* @ORM\JoinColumn(name="vehicle_id", referencedColumnName="id")
*/
protected $vehicle;
/**
* Related sale.
*
* @ORM\ManyToOne(targetEntity="App\Entity\Sale")
*
* @ORM\JoinColumn(name="sale_id", referencedColumnName="id")
*/
protected $sale;
/**
* User.
*
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="purchaseInstructions")
*
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
/**
* Amount that user is willing to pay on physical sale for the related vehicle.
*
* @ORM\Column(type="decimal", precision=10, scale=2, nullable=true)
*/
protected $amount;
/**
* Amount excluding tax.
*
* @ORM\Column(type="decimal", precision=10, scale=4, nullable=true)
*/
protected $amountHT;
/**
* True if user is able to pay VAT tax, false otherwise.
*
* @ORM\Column(type="boolean", name="pays_vat")
*/
protected $paysVat;
/**
* @Gedmo\Timestampable(on="create")
*
* @ORM\Column(type="datetime", name="created_at")
*/
protected $createdAt;
/**
* @Gedmo\Timestampable(on="update")
*
* @ORM\Column(type="datetime", name="updated_at")
*/
protected $updatedAt;
/**
* @ORM\Column(type="string")
*/
protected $status;
/**
* @ORM\Column(type="string")
*/
protected $origin;
/**
* Constructor.
*/
public function __construct(Vehicle $vehicle, ?User $user = null)
{
$this->vehicle = $vehicle;
$this->sale = $vehicle->getEvent()->getSale();
$this->user = $user;
$this->amount = $vehicle->getStartingPrice();
$this->paysVat = false;
$this->status = self::REFUSED;
$this->origin = self::ORIGIN_B2C;
}
/**
* Get id.
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set id.
*
* @param int $id
*/
public function setId($id): void
{
$this->id = $id;
}
/**
* Return the vehicle.
*
* @return Vehicle
*/
public function getVehicle()
{
return $this->vehicle;
}
/**
* Set the vehicle.
*/
public function setVehicle(Vehicle $vehicle): void
{
$this->vehicle = $vehicle;
}
/**
* Return the sale.
*
* @return Sale
*/
public function getSale()
{
return $this->sale;
}
/**
* Set the sale.
*/
public function setSale(Sale $sale): void
{
$this->sale = $sale;
}
/**
* Get user.
*
* @return User
*/
public function getUser()
{
return $this->user;
}
/**
* Set user.
*/
public function setUser(?User $user = null): void
{
$this->user = $user;
}
/**
* Get purchase instruction amount.
*
* @return int
*/
public function getAmount()
{
return intval($this->amount);
}
/**
* Set purchase instruction amount.
*
* @param int $amount
*/
public function setAmount($amount): void
{
$this->amount = $amount;
}
/**
* Get purchase instruction amount Excluding Tax.
*
* @return float
*/
public function getAmountHT()
{
return (float) $this->amountHT;
}
/**
* Set purchase instruction amount Excluding Tax.
*
* @param float $amountHT
*/
public function setAmountHT($amountHT): void
{
$this->amountHT = $amountHT;
}
/**
* Check whether the user declares VAT tax payment.
*
* @return bool
*/
public function getPaysVat()
{
return $this->paysVat;
}
/**
* Set whether the user declares VAT tax payment.
*
* @param bool $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;
}
/**
* Return time of update.
*
* @return DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* Set time of update.
*/
public function setUpdatedAt(\DateTime $updatedAt): void
{
$this->updatedAt = $updatedAt;
}
/**
* Return status.
*/
public function getStatus()
{
return $this->status;
}
/**
* Set status.
*/
public function setStatus($status): void
{
$this->status = $status;
}
/**
* Return origin.
*/
public function getOrigin()
{
return $this->origin;
}
/**
* Set origin.
*/
public function setOrigin($origin): void
{
$this->origin = $origin;
}
public function getIsOutOfDate()
{
$startdate = \DateTime::createFromFormat('d/m/Y H:i:s', sprintf('%s 00:00:00', $this->vehicle->getSale()->getStartDate()->format('d/m/Y')));
return new \DateTime() >= $startdate;
}
}