<?php
namespace App\Entity\OnlineShop;
use App\Entity\AbstractBase;
use App\Entity\MiniAbstractBase;
use App\Entity\Traits\HasDocumentTrait;
use App\Entity\Traits\NameTrait;
use App\Entity\User;
use App\Enum\DocumentTypeEnum;
use App\Repository\OnlineShop\SupplierDocumentRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Table(name="vulco_supplier_document")
*
* @ORM\Entity(repositoryClass=SupplierDocumentRepository::class)
*
* @ORM\HasLifecycleCallbacks()
*
* @Gedmo\SoftDeleteable(fieldName="removedAt", timeAware=false)
*
* @Vich\Uploadable
*/
class SupplierDocument extends AbstractBase
{
use HasDocumentTrait;
use NameTrait;
/**
* @ORM\Column(type="string", length=255)
*/
private string $name;
/**
* @Vich\UploadableField(mapping="supplier_document", fileNameProperty="documentName")
*/
private ?File $document = null;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $documentName = null;
/**
* @ORM\Column(type="integer")
*/
private int $documentType = DocumentTypeEnum::PROMOTION;
/**
* @ORM\Column(type="integer", nullable=false, options={"default": 0})
*/
private int $numDownloads = 0;
/**
* @ORM\OneToMany(targetEntity="App\Entity\OnlineShop\SupplierDocumentDownload", mappedBy="document", cascade={"persist", "remove"}, orphanRemoval=true)
* @ORM\OrderBy({"createdAt": "DESC"})
*/
private ?Collection $downloads;
/**
* @ORM\Column(type="date", nullable=true)
*/
private ?\DateTimeInterface $startDate = null;
/**
* @ORM\Column(type="date", nullable=true)
*/
private ?\DateTimeInterface $endDate = null;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\OnlineShop\SupplierCategory")
*
* @ORM\JoinColumn(name="supplier_category_id", referencedColumnName="id")
*/
private ?SupplierCategory $category = null;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\OnlineShop\Supplier", inversedBy="documents")
*
* @ORM\JoinColumn(name="supplier_id", referencedColumnName="id", onDelete="CASCADE")
*/
private Supplier $supplier;
public function __construct()
{
$this->downloads = new ArrayCollection();
}
public function getDocumentType(): int
{
return $this->documentType;
}
public function getDocumentTypeName(): string
{
return array_key_exists($this->documentType, DocumentTypeEnum::getTranslations()) ? DocumentTypeEnum::getTranslations()[$this->documentType] : MiniAbstractBase::DEFAULT_EMPTY_STRING;
}
public function setDocumentType(int $documentType): self
{
$this->documentType = $documentType;
return $this;
}
public function getNumDownloads(): int
{
return $this->numDownloads;
}
public function setNumDownloads(int $numDownloads): self
{
$this->numDownloads = $numDownloads;
return $this;
}
public function getDownloads(): ?Collection
{
return $this->downloads;
}
public function setDownloads(?Collection $downloads): self
{
$this->downloads = $downloads;
return $this;
}
public function existsDownload(User $user): bool
{
$result = false;
$download = new SupplierDocumentDownload();
$download->setDocument($this);
$download->setUser($user);
$predicate = function ($key, SupplierDocumentDownload $element) use ($download) {
return ($element->getDocument()->getId() === $download->getDocument()->getId()) &&
($element->getUser()->getId() === $download->getUser()->getId());
};
if ($this->downloads->exists($predicate)) {
$result = true;
}
return $result;
}
public function addDownload(SupplierDocumentDownload $download): self
{
$predicate = function ($key, SupplierDocumentDownload $element) use ($download) {
return ($element->getDocument()->getId() === $download->getDocument()->getId()) &&
($element->getUser()->getId() === $download->getUser()->getId());
};
if (!$this->downloads->exists($predicate)) {
$this->downloads->add($download);
$this->setNumDownloads($this->getNumDownloads() + 1);
}
return $this;
}
public function removeDownload(SupplierDocumentDownload $download): self
{
if ($this->downloads->contains($download)) {
$this->downloads->removeElement($download);
$this->setNumDownloads($this->getNumDownloads() - 1);
}
return $this;
}
public function countDownloads(): int
{
if (null !== $this->getDownloads()) {
return count($this->getDownloads());
}
return 0;
}
public function getStartDate(): ?\DateTimeInterface
{
return $this->startDate;
}
public function getStartDateAsString(): string
{
return $this->getDateAsString($this->getStartDate());
}
public function setStartDate(?\DateTimeInterface $startDate): self
{
$this->startDate = $startDate;
return $this;
}
public function getEndDate(): ?\DateTimeInterface
{
return $this->endDate;
}
public function getEndDateAsString(): string
{
return $this->getDateAsString($this->getEndDate());
}
public function setEndDate(?\DateTimeInterface $endDate): self
{
$this->endDate = $endDate;
return $this;
}
public function getCategory(): ?SupplierCategory
{
return $this->category;
}
public function setCategory(SupplierCategory $category): self
{
$this->category = $category;
return $this;
}
public function getSupplier(): Supplier
{
return $this->supplier;
}
public function setSupplier(Supplier $supplier): self
{
$this->supplier = $supplier;
return $this;
}
public function isEnabled(): bool
{
$now = new \DateTime();
$now->setTime(0, 0);
$startDate = $this->getStartDate();
$startDate?->setTime(0, 0);
$endDate = $this->getEndDate();
$endDate?->setTime(0, 0);
if ($startDate && $endDate) {
return $startDate <= $now && $now <= $endDate;
}
if ($startDate && !$endDate) {
return $startDate <= $now;
}
if (!$startDate && $endDate) {
return $now <= $endDate;
}
return true;
}
public function __toString(): string
{
return $this->id ? $this->getName() : MiniAbstractBase::DEFAULT_EMPTY_STRING;
}
}