<?php
namespace App\Entity;
use App\Entity\Product\GuestProduct;
use DateTime;
use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM;
use Exception;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* Class Invoice
* @package App\Entity
*
* @ORM\Table(name="malys_invoice")
* @ORM\Entity(repositoryClass="App\Repository\InvoiceRepository")
* @ORM\HasLifecycleCallbacks()
* @Vich\Uploadable
*/
class Invoice
{
public const STATUS_PROCESSING = '0-processing';
public const STATUS_PENDING = '1-pending';
public const STATUS_PROCESSED = '2-processed';
public const STATUS_REJECTED = '3-rejected';
public const STATUSES = [
self::STATUS_PENDING,
self::STATUS_PROCESSING,
self::STATUS_PROCESSED,
self::STATUS_REJECTED
];
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(name="status", type="string", length=255, nullable=false, options={"default"=App\Entity\Invoice::STATUS_PENDING})
* @Assert\Choice(choices=Invoice::STATUSES, message="The status you chose is forbidden")
* @var string
*/
private $status;
/**
* @ORM\Column(name="adherentNumber", type="string", length=255, nullable=true)
* @var string
*/
private $adherentNumber;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\InvoiceBatch", inversedBy="invoices", cascade={"persist"})
* @ORM\JoinColumn(name="invoice_batch_id", referencedColumnName="id", onDelete="CASCADE", nullable=true)
*/
public $invoiceBatch;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Customer", inversedBy="invoices");
* @ORM\JoinColumn(name="customer_id", referencedColumnName="id", onDelete="CASCADE", nullable=true)
*/
private $customer;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Supplier", inversedBy="invoices");
* @ORM\JoinColumn(name="supplier_id", referencedColumnName="id", onDelete="CASCADE", nullable=true)
*/
private $supplier;
/**
* @var DateTime
*
* @ORM\Column(name="createdAt", type="datetime", nullable=false)
*/
private $createdAt;
/**
* @var DateTime
*
* @ORM\Column(name="updatedAt", type="datetime", nullable=false)
*/
private $updatedAt;
/**
* @ORM\Column(name="fileName", type="string", nullable=false)
*/
protected $fileName;
/**
* @ORM\Column(name="mime_type", type="string", nullable=false)
*/
protected $mimeType;
/**
* @Vich\UploadableField(mapping="invoice_file", fileNameProperty="fileName", mimeType="mimeType")
* @Assert\File(
* groups={"invoices", "Default"},
* maxSize="10M",
* maxSizeMessage="Fichier trop volumineux. Max : 10MB",
* mimeTypes={"image/jpeg", "image/png", "application/pdf"},
* mimeTypesMessage="Type de fichier interdit. Format autorisés : jpeg, jpg, png, pdf"
* )
*/
private $fileUpload;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Product\GuestProduct", inversedBy="invoices", cascade={"persist"})
* @ORM\JoinTable(name="malys_invoices_products",
* joinColumns={@ORM\JoinColumn(name="invoice_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="product_id", referencedColumnName="id")}
* )
*/
private $products;
public function __construct()
{
$this->setStatus(self::STATUS_PENDING);
}
/**
* @return int|null
*/
public function getId(): ?int
{
return $this->id;
}
/**
* @return string|null
*/
public function getStatus(): ?string
{
return $this->status;
}
/**
* @param $status
* @return $this
*/
public function setStatus($status): Invoice
{
$this->status = $status;
return $this;
}
/**
* @return null|string
*/
public function getAdherentNumber(): ?string
{
return $this->adherentNumber;
}
/**
* @param string|null $adherentNumber
*/
public function setAdherentNumber(?string $adherentNumber): void
{
$this->adherentNumber = $adherentNumber;
}
/**
* @return ?InvoiceBatch
*/
public function getInvoiceBatch(): ?InvoiceBatch
{
return $this->invoiceBatch;
}
/**
* @param InvoiceBatch $invoiceBatch
* @return Invoice
*/
public function setInvoiceBatch(InvoiceBatch $invoiceBatch): Invoice
{
$this->invoiceBatch = $invoiceBatch;
$invoiceBatch->addInvoice($this);
return $this;
}
/**
*
* @return Invoice
* @ORM\PrePersist
*/
public function prePersist(): Invoice
{
$this->createdAt = new DateTime();
$this->updatedAt = new DateTime();
return $this;
}
/**
* @return DateTime|null
*/
public function getCreatedAt(): ?DateTime
{
return $this->createdAt;
}
/**
* Set createdAt
*
* @param null $date
* @return Invoice
*/
public function setCreatedAt($date = null): Invoice
{
if (is_null($date)) {
$date = new DateTime();
}
$this->createdAt = $date;
return $this;
}
/**
* @return DateTime|null
*/
public function getUpdatedAt(): ?DateTime
{
return $this->updatedAt;
}
/**
* Set updatedAt
*
* @return Invoice
* @ORM\PreUpdate
* @throws Exception
*/
public function setUpdatedAt(): Invoice
{
$this->updatedAt = new DateTime();
return $this;
}
/**
* @return mixed
*/
public function getFileName()
{
return $this->fileName;
}
/**
* @param mixed $fileName
*/
public function setFileName($fileName): void
{
$this->fileName = $fileName;
}
/**
* @return mixed
*/
public function getFileUpload()
{
return $this->fileUpload;
}
/**
* @param mixed $fileUpload
*/
public function setFileUpload($fileUpload = null): void
{
$this->fileUpload = $fileUpload;
if (!is_null($fileUpload)) {
$this->updatedAt = new DateTimeImmutable();
}
}
/**
* @return mixed
*/
public function getProducts()
{
return $this->products;
}
/**
* @param mixed $products
*/
public function setProducts($products): void
{
$this->products = $products;
}
/**
* @return mixed
*/
public function getCustomer()
{
return $this->customer;
}
/**
* @param Customer $customer
* @return Invoice
*/
public function setCustomer(Customer $customer): Invoice
{
$this->customer = $customer;
return $this;
}
/**
* @return mixed
*/
public function getSupplier()
{
return $this->supplier;
}
/**
* @param mixed $supplier
* @return Invoice
*/
public function setSupplier($supplier): Invoice
{
$this->supplier = $supplier;
return $this;
}
/**
* @return mixed
*/
public function getMimeType(): ?string
{
return $this->mimeType;
}
/**
* @param mixed $mimeType
*/
public function setMimeType($mimeType): void
{
$this->mimeType = $mimeType;
}
/**
* @param GuestProduct $product
*/
public function addProduct(GuestProduct $product)
{
if (! $this->products->contains($product)) {
$this->products->add($product);
$product->addInvoice($this);
}
}
/**
* @param GuestProduct $product
*/
public function removeProduct(GuestProduct $product)
{
if ($this->products->contains($product)) {
$this->products->removeElement($product);
}
}
}