<?php
namespace App\Entity;
use App\Entity\Enum\InvoiceStateEnum;
use DateTime;
use DateTimeInterface;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* @ORM\Entity(repositoryClass="App\Repository\InvoiceRepository")
* @Vich\Uploadable
*/
class Invoice
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="datetime")
* @Gedmo\Timestampable(on="create")
*/
private $createdAt;
/**
* @ORM\Column(type="datetime", nullable=true)
* @Gedmo\Timestampable(on="update")
*/
private $updatedAt;
/**
* @ORM\Column(type="float")
*/
private $amount;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $referenceZervant;
/**
* @ORM\Column(type="string", length=25)
*/
private $state = InvoiceStateEnum::STATE_PENDING;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $pdfPath;
/**
* @Vich\UploadableField(mapping="invoice_pdf", fileNameProperty="pdfPath")
* @var File
*/
private $pdfFile;
/**
* @ORM\OneToOne(targetEntity="App\Entity\Order", mappedBy="invoice")
*/
private $order;
/**
* @ORM\OneToOne(targetEntity=DeviceSale::class, mappedBy="invoice", cascade={"persist", "remove"})
*/
private $deviceSale;
public function getLabel(): string
{
return 'Facture #'.$this->id;
}
public function getId(): ?int
{
return $this->id;
}
public function getCreatedAt(): ?DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getAmount(): ?float
{
return $this->amount;
}
public function getAmountHT(): ?float
{
return round($this->amount/1.2,2);
}
public function setAmount(float $amount): self
{
$this->amount = $amount;
return $this;
}
public function getReferenceZervant(): ?string
{
return $this->referenceZervant;
}
public function setReferenceZervant(?string $referenceZervant): self
{
$this->referenceZervant = $referenceZervant;
return $this;
}
public function getState(): ?string
{
return $this->state;
}
public function isPending(){
return $this->state == InvoiceStateEnum::STATE_PENDING;
}
public function isValidated(){
return $this->state == InvoiceStateEnum::STATE_VALIDATED;
}
public function canBeDownloaded(){
return $this->isValidated() && !is_null($this->getPdfPath());
}
public function setState(string $state): self
{
$this->state = $state;
return $this;
}
/**
* @return Order
*/
public function getOrder()
{
return $this->order;
}
/**
* @param mixed $order
*/
public function setOrder(Order $order): void
{
$this->order = $order;
$this->order->setInvoice($this);
}
/**
* @return mixed
*/
public function getPdfPath()
{
return $this->pdfPath;
}
/**
* @param mixed $pdfPath
*/
public function setPdfPath($pdfPath): void
{
$this->pdfPath = $pdfPath;
}
/**
* @return File
*/
public function getPdfFile(): ?File
{
return $this->pdfFile;
}
/**
* @param File $pdfFile
*/
public function setPdfFile(File $pdfFile): void
{
$this->pdfFile = $pdfFile;
//update timestamp
if ($pdfFile) {
$this->updatedAt = new DateTime('now');
}
}
/**
* @return mixed
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* @param mixed $updatedAt
*/
public function setUpdatedAt($updatedAt): void
{
$this->updatedAt = $updatedAt;
}
public function getDeviceSale(): ?DeviceSale
{
return $this->deviceSale;
}
public function setDeviceSale(?DeviceSale $deviceSale): self
{
$this->deviceSale = $deviceSale;
// set (or unset) the owning side of the relation if necessary
$newInvoice = null === $deviceSale ? null : $this;
if ($deviceSale->getInvoice() !== $newInvoice) {
$deviceSale->setInvoice($newInvoice);
}
return $this;
}
/**
* Return the associated freelancer, either linked by the order or by the sale
* @return Freelancer|null
*/
public function getFreelancer(): ?Freelancer
{
if($this->order){
return $this->getOrder()->getFreelancer();
}elseif($this->deviceSale){
return $this->getDeviceSale()->getProspectOnStore()->getStore()->getAdministrator();
}
return null;
}
}