<?php
namespace App\Entity;
use App\Traits\Creatable;
use App\Traits\Deleteable;
use App\Traits\Updateable;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* @ORM\Table(name="`creation`")
* @ORM\Entity(repositoryClass="App\Repository\CreationRepository")
* @Gedmo\SoftDeleteable(fieldName="deletedAt")
*/
class Creation
{
use Creatable, Updateable, Deleteable;
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private ?int $id = null;
/**
* @var ?string
*
* @ORM\Column(type="string", nullable=true)
*/
private ?string $name = null;
/**
* @var ?array
*
* @ORM\Column(type="json", nullable=true)
*/
private ?array $content = null;
/**
* @var boolean
*
* @ORM\Column(type="boolean")
*/
private bool $archived;
/**
* @var ?integer
*
* @ORM\Column(name="quantity", type="integer", nullable=true)
*/
private ?int $quantity = null;
/**
* @var ?integer
*
* @ORM\Column(name="width", type="integer", nullable=true)
*/
private ?int $width = null;
/**
* @var ?integer
*
* @ORM\Column(name="height", type="integer", nullable=true)
*/
private ?int $height = null;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Institution", inversedBy="creations", cascade={"persist"})
*/
private ?Institution $institution = null;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Product")
*/
private ?Product $product = null;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\ProductFeatureValue")
*/
private Collection $productFeatureValues;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\AttributValue")
*/
private Collection $attributValues;
/**
* @ORM\OneToMany(targetEntity="App\Entity\CreationComment", mappedBy="creation", cascade={"persist", "remove"})
*/
private Collection $creationComments;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Document", cascade={"persist", "remove"})
* @ORM\JoinTable(name="`creation_related_document`")
*/
private Collection $relatedDocuments;
/**
* @ORM\OneToMany(targetEntity="App\Entity\CartElement", mappedBy="creation", cascade={"persist", "remove"})
*/
private Collection $cartElements;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Order", mappedBy="creation")
*/
private Collection $orders;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\CreationCategory", inversedBy="creations")
* @ORM\JoinColumn(name="creation_category_id", referencedColumnName="id")
*/
private ?CreationCategory $creationCategory = null;
/**
* @ORM\OneToMany(targetEntity="App\Entity\CreationHistory", mappedBy="creation", cascade={"persist", "remove"})
* @ORM\OrderBy({"createdAt" = "DESC"})
*/
private Collection $creationHistories;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Model")
*/
private ?Model $model = null;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Matrix")
*/
private ?Matrix $matrix = null;
public function __construct()
{
$this->archived = false;
$this->creationComments = new ArrayCollection();
$this->relatedDocuments = new ArrayCollection();
$this->productFeatureValues = new ArrayCollection();
$this->attributValues = new ArrayCollection();
$this->cartElements = new ArrayCollection();
$this->orders = new ArrayCollection();
$this->creationHistories = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): self
{
$this->name = $name;
return $this;
}
public function getContent(): ?array
{
return $this->content;
}
public function setContent(?array $content): self
{
$this->content = $content;
return $this;
}
public function getArchived(): ?bool
{
return $this->archived;
}
public function setArchived(bool $archived): self
{
$this->archived = $archived;
return $this;
}
public function getQuantity(): ?int
{
return $this->quantity;
}
public function setQuantity(?int $quantity): self
{
$this->quantity = $quantity;
return $this;
}
public function getWidth(): ?int
{
return $this->width;
}
public function setWidth(?int $width): self
{
$this->width = $width;
return $this;
}
public function getHeight(): ?int
{
return $this->height;
}
public function setHeight(?int $height): self
{
$this->height = $height;
return $this;
}
public function getInstitution(): ?Institution
{
return $this->institution;
}
public function setInstitution(?Institution $institution): self
{
$this->institution = $institution;
return $this;
}
public function getProduct(): ?Product
{
return $this->product;
}
public function setProduct(?Product $product): self
{
$this->product = $product;
return $this;
}
/**
* @return Collection|ProductFeatureValue[]
*/
public function getProductFeatureValues(): Collection|array
{
return $this->productFeatureValues;
}
public function addProductFeatureValue(ProductFeatureValue $productFeatureValue): self
{
if (!$this->productFeatureValues->contains($productFeatureValue)) {
$this->productFeatureValues[] = $productFeatureValue;
}
return $this;
}
public function removeProductFeatureValue(ProductFeatureValue $productFeatureValue): self
{
if ($this->productFeatureValues->contains($productFeatureValue)) {
$this->productFeatureValues->removeElement($productFeatureValue);
}
return $this;
}
/**
* @return Collection<int, AttributValue>
*/
public function getAttributValues(): Collection
{
return $this->attributValues;
}
public function addAttributValue(AttributValue $attributValue): self
{
if (!$this->attributValues->contains($attributValue)) {
$this->attributValues[] = $attributValue;
}
return $this;
}
public function removeAttributValue(AttributValue $attributValue): self
{
$this->attributValues->removeElement($attributValue);
return $this;
}
/**
* @return Collection|CreationComment[]
*/
public function getCreationComments(): array|Collection
{
return $this->creationComments;
}
public function addCreationComment(CreationComment $creationComment): self
{
if (!$this->creationComments->contains($creationComment)) {
$this->creationComments[] = $creationComment;
$creationComment->setCreation($this);
}
return $this;
}
public function removeCreationComment(CreationComment $creationComment): self
{
if ($this->creationComments->contains($creationComment)) {
$this->creationComments->removeElement($creationComment);
// set the owning side to null (unless already changed)
if ($creationComment->getCreation() === $this) {
$creationComment->setCreation(null);
}
}
return $this;
}
/**
* @return Collection|Document[]
*/
public function getRelatedDocuments(): Collection|array
{
return $this->relatedDocuments;
}
public function addRelatedDocument(Document $relatedDocument): self
{
if (!$this->relatedDocuments->contains($relatedDocument)) {
$this->relatedDocuments[] = $relatedDocument;
}
return $this;
}
public function removeRelatedDocument(Document $relatedDocument): self
{
if ($this->relatedDocuments->contains($relatedDocument)) {
$this->relatedDocuments->removeElement($relatedDocument);
}
return $this;
}
/**
* @return Collection|CartElement[]
*/
public function getCartElements(): Collection|array
{
return $this->cartElements;
}
public function addCartElement(CartElement $cartElement): self
{
if (!$this->cartElements->contains($cartElement)) {
$this->cartElements[] = $cartElement;
$cartElement->setCreation($this);
}
return $this;
}
public function removeCartElement(CartElement $cartElement): self
{
if ($this->cartElements->contains($cartElement)) {
$this->cartElements->removeElement($cartElement);
// set the owning side to null (unless already changed)
if ($cartElement->getCreation() === $this) {
$cartElement->setCreation(null);
}
}
return $this;
}
/**
* @return Collection|Order[]
*/
public function getOrders(): Collection|array
{
return $this->orders;
}
public function addOrder(Order $order): self
{
if (!$this->orders->contains($order)) {
$this->orders[] = $order;
$order->setCreation($this);
}
return $this;
}
public function removeOrder(Order $order): self
{
if ($this->orders->contains($order)) {
$this->orders->removeElement($order);
// set the owning side to null (unless already changed)
if ($order->getCreation() === $this) {
$order->setCreation(null);
}
}
return $this;
}
public function getCreationCategory(): ?CreationCategory
{
return $this->creationCategory;
}
public function setCreationCategory(?CreationCategory $creationCategory): self
{
$this->creationCategory = $creationCategory;
return $this;
}
/**
* @return Collection<int, CreationHistory>
*/
public function getCreationHistories(): Collection
{
return $this->creationHistories;
}
public function addCreationHistory(CreationHistory $creationHistory): self
{
if (!$this->creationHistories->contains($creationHistory)) {
$this->creationHistories[] = $creationHistory;
$creationHistory->setCreation($this);
}
return $this;
}
public function removeCreationHistory(CreationHistory $creationHistory): self
{
if ($this->creationHistories->removeElement($creationHistory)) {
// set the owning side to null (unless already changed)
if ($creationHistory->getCreation() === $this) {
$creationHistory->setCreation(null);
}
}
return $this;
}
public function getModel(): ?Model
{
return $this->model;
}
public function setModel(?Model $model): self
{
$this->model = $model;
return $this;
}
public function getMatrix(): ?Matrix
{
return $this->matrix;
}
public function setMatrix(?Matrix $matrix): self
{
$this->matrix = $matrix;
return $this;
}
/**
* @return ArrayCollection
*/
public function getProductFeaturesValuesIndexed()
{
$productFeatureValues = new ArrayCollection();
foreach ($this->getProduct()->getProductFeatures() as $productFeature) {
foreach ($this->getProductFeatureValues() as $productFeatureValue) {
if (!is_null($productFeatureValue->getProductFeature()->getFeature()->getConstName())) {
if ($productFeatureValue->getProductFeature()->getFeature()->getConstName() === $productFeature->getFeature()->getConstName()) {
$productFeatureValues->add($productFeatureValue);
}
} elseif (!is_null($productFeatureValue->getProductFeature()->getFeature()->getPrintComConstName())) {
if ($productFeatureValue->getProductFeature()->getFeature()->getPrintComConstName() === $productFeature->getFeature()->getPrintComConstName()) {
$productFeatureValues->add($productFeatureValue);
}
}
}
}
return $productFeatureValues;
}
/**
* @return ProductFeatureValue|mixed|null
*/
public function getProductFeatureValueFormat()
{
$productFeatureValueFormat = null;
foreach ($this->getProductFeatureValues() as $productFeatureValue) {
if ($productFeatureValue->getProductFeature()->getFeature()->getConstName() === Feature::FORMAT or $productFeatureValue->getProductFeature()->getFeature()->getPrintComConstName() === Feature::SIZE) {
$productFeatureValueFormat = $productFeatureValue;
break;
}
}
return $productFeatureValueFormat;
}
/**
* @return bool
*/
public function hasFormatVariable()
{
$hasFormatVariable = false;
/** @var ProductFeatureValue $productFeatureValue */
foreach ($this->getProductFeatureValues() as $productFeatureValue) {
if (($productFeatureValue->getProductFeature()->getFeature()->getConstName() == Feature::FORMAT or $productFeatureValue->getProductFeature()->getFeature()->getPrintComConstName() == Feature::SIZE) and $productFeatureValue->getConstName() == ProductFeatureValue::VARIABLE) {
$hasFormatVariable = true;
break;
}
}
return $hasFormatVariable;
}
/**
* @return DateTime
*/
public function getExpirationDate()
{
$expirationDate = clone $this->getCreatedAt();
$expirationDate->modify('+ 7 DAYS');
return $expirationDate;
}
/**
* @return bool
*/
public function isDeletable(): bool
{
$isDeletable = true;
if ($this->getOrders()->count() > 0) {
$isDeletable = false;
}
return $isDeletable;
}
/**
* @return bool
*/
public function isUpdatable(): bool
{
$isUpdatable = true;
if (!$this->getProduct()->getBuilderAccess()) {
$isUpdatable = false;
}
if ($this->getOrders()->count() > 0) {
/** @var Order $order */
foreach ($this->getOrders() as $order) {
if (!$order->isMakingDone()) {
$isUpdatable = false;
}
}
}
return $isUpdatable;
}
/**
* @return CreationHistory|false|mixed|null
*/
public function getLastCreationHistory()
{
$creationHistory = null;
if ($this->getCreationHistories()->count() > 0) {
$creationHistory = $this->getCreationHistories()->first();
}
return $creationHistory;
}
}