<?php
namespace App\Entity;
use App\Repository\CustomProductRepository;
use App\Traits\DateTrait;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
use JMS\Serializer\Annotation\Expose;
use JMS\Serializer\Annotation\Groups;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* Produit personnalisé
*
* @ORM\Entity(repositoryClass=CustomProductRepository::class)
*
* @Serializer\ExclusionPolicy("ALL")
* @Vich\Uploadable
*/
class CustomProduct
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*
* @Expose
* @Groups({"customProduct:list", "customProduct:item"})
*/
private ?int $id = null;
/**
* Label du produit
*
* @ORM\Column(type="string", length=255)
*
* @Assert\Length(
* min = 2,
* max = 255,
* )
*
* @Expose
* @Groups({"customProduct:list", "customProduct:item"})
*/
private ?string $label = null;
/**
* Description du produit
*
* @ORM\Column(type="text", nullable=true)
*
* @Expose
* @Groups({"customProduct:item"})
*/
private ?string $description = null;
/**
* Valeur (prix) du produit
*
* @ORM\Column(type="float", nullable=false)
*
* @Assert\PositiveOrZero
*
* @Expose
* @Groups({"customProduct:list", "customProduct:item"})
*/
private ?float $value = null;
/**
* Indique si le produit est actif
*
* @ORM\Column(type="boolean")
*
* @Expose
* @Groups({"customProduct:list", "customProduct:item"})
*/
private bool $enabled = true;
/**
* Image du produit
*
* @ORM\Column(type="string", length=255, nullable=true)
*
* @Expose
* @Groups({"customProduct:list", "customProduct:item"})
*/
private ?string $image = null;
/**
* Vich Uploader
*
* @Vich\UploadableField(mapping="custom_product_images", fileNameProperty="image")
* @var null|File
*/
private ?File $imageFile = null;
/**
* Liste des contacts (email) quand il y a une demande pour ce produit
*
* @ORM\Column(type="array", nullable=true)
*
* @Expose
* @Groups({"customProduct:item"})
*/
private array $contacts = [];
/**
* Liste des champs pour commander ce produit
*
* @ORM\OneToMany(
* targetEntity=CustomProductField::class,
* mappedBy="product",
* orphanRemoval=true,
* cascade={"persist","remove"}
* )
*/
private $fields;
/**
* @ORM\ManyToOne(targetEntity=CustomProductTemplate::class)
*/
private ?CustomProductTemplate $template = null;
/**
* Type de produit
*
* @ORM\ManyToOne(targetEntity=CustomProductType::class, inversedBy="templates")
* @ORM\JoinColumn(nullable=false, name="type", referencedColumnName="slug")
*
* @Expose
* @Groups({"customProduct:list", "customProduct:item"})
*/
private ?CustomProductType $type = null;
/**
* @ORM\Column(type="string", length=10, nullable=true)
*
* @Expose
* @Groups({"customProduct:list", "customProduct:item"})
*/
private $sku;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $stockAlert;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $reference;
/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="customProducts")
*/
private $createdBy;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $categoryValues;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $furtherInformation;
/**
* Stock du produit
*
* @ORM\Column(type="integer", nullable=true)
*
* @Assert\PositiveOrZero
*
* @Expose
* @Groups({"customProduct:list", "customProduct:item"})
*/
private ?int $stock = null;
/**
* Indique si on peut commander plusieurs fois le produit par commande
*
* @ORM\Column(type="boolean")
*
* @Expose
* @Groups({"customProduct:list", "customProduct:item"})
*/
private bool $allowedMultiple = true;
/**
* Indique si le prix du produit est fixe
*
* @ORM\Column(type="boolean")
*
* @Expose
* @Groups({"customProduct:list", "customProduct:item"})
*/
private bool $fixedValue = true;
/**
* Indique la catégorie de point par defaut
*
* @ORM\Column(type="string", nullable=true)
*
* @Expose
* @Groups({"customProduct:list", "customProduct:item"})
*/
private ?string $defaultPointCategory = null;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $productUserQuota;
/**
* @ORM\OneToMany(targetEntity=QuotaProductUser::class, mappedBy="customProduct", orphanRemoval=true)
*/
private $quotaProductUsers;
/**
* @ORM\Column(type="json", nullable=true)
*/
private $visibilityFromJobs = [];
/**
* @ORM\OneToMany(targetEntity=QuotaProductJob::class, mappedBy="customProduct", cascade={"persist", "remove"}, orphanRemoval=true)
*/
private $quotaProductJobs;
/**
* Indique si une transaction de débit est créé lors de la commande
*
* @ORM\Column(type="boolean")
*/
private $debitAtOrder = false;
use DateTrait;
public function __construct()
{
$this->fields = new ArrayCollection();
$this->quotaProductUsers = new ArrayCollection();
$this->quotaProductJobs = new ArrayCollection();
}
/**
* @Serializer\VirtualProperty()
* @Serializer\SerializedName("array_category_values")
*
* @Expose
* @Groups({"customProduct:list", "customProduct:item"})
*/
public function getArrayCategoryValues()
{
return json_decode($this->categoryValues, true) ?? [];
}
public function modifyValueToCategory(string $slug, int $value): CustomProduct
{
$newArray = $this->getArrayCategoryValues();
$newArray[$slug] = $value;
$this->categoryValues = json_encode($newArray);
return $this;
}
public function getId(): ?int
{
return $this->id;
}
public function setId(int $id): self
{
$this->id = $id;
return $this;
}
public function getTemplate(): ?CustomProductTemplate
{
return $this->template;
}
public function setTemplate(?CustomProductTemplate $template): self
{
$this->template = $template;
return $this;
}
public function getImageFile(): ?File
{
return $this->imageFile;
}
public function setImageFile(File $imageFile): CustomProduct
{
$this->imageFile = $imageFile;
$this->updatedAt = new DateTime();
return $this;
}
public function getLabel(): ?string
{
return $this->label;
}
public function setLabel(string $label): self
{
$this->label = $label;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getValue(): ?float
{
return $this->value;
}
public function setValue(?float $value): self
{
$this->value = $value;
return $this;
}
public function isEnabled(): ?bool
{
return $this->enabled;
}
public function setEnabled(bool $enabled): self
{
$this->enabled = $enabled;
return $this;
}
public function getImage(): ?string
{
return $this->image;
}
public function setImage(?string $image): self
{
$this->image = $image;
return $this;
}
public function getContacts(): ?array
{
return $this->contacts;
}
public function setContacts(array $contacts): self
{
$this->contacts = $contacts;
return $this;
}
/**
* @return Collection<int, CustomProductField>
*/
public function getFields(): Collection
{
return $this->fields;
}
public function addField(CustomProductField $field): self
{
if (!$this->fields->contains($field)) {
$this->fields[] = $field;
$field->setProduct($this);
}
return $this;
}
public function removeField(CustomProductField $field): self
{
if ($this->fields->removeElement($field)) {
// set the owning side to null (unless already changed)
if ($field->getProduct() === $this) {
$field->setProduct(null);
}
}
return $this;
}
public function getType(): ?CustomProductType
{
return $this->type;
}
public function setType(?CustomProductType $type): self
{
$this->type = $type;
return $this;
}
public function getSku(): ?string
{
return $this->sku;
}
public function setSku(?string $sku): self
{
$this->sku = $sku;
return $this;
}
public function getStockAlert(): ?int
{
return $this->stockAlert;
}
public function setStockAlert(?int $stockAlert): self
{
$this->stockAlert = $stockAlert;
return $this;
}
public function getStock(): ?int
{
return $this->stock;
}
public function setStock(?int $stock): self
{
$this->stock = $stock;
return $this;
}
public function getReference(): ?string
{
return $this->reference;
}
public function setReference(?string $reference): self
{
$this->reference = $reference;
return $this;
}
public function getCreatedBy(): ?User
{
return $this->createdBy;
}
public function setCreatedBy(?User $createdBy): self
{
$this->createdBy = $createdBy;
return $this;
}
public function getCategoryValues(): ?string
{
return $this->categoryValues;
}
public function setCategoryValues(?string $categoryValues): self
{
$this->categoryValues = $categoryValues;
return $this;
}
public function getFurtherInformation(): ?string
{
return $this->furtherInformation;
}
public function setFurtherInformation(?string $furtherInformation): self
{
$this->furtherInformation = $furtherInformation;
return $this;
}
public function isAllowedMultiple(): ?bool
{
return $this->allowedMultiple;
}
public function setAllowedMultiple(bool $allowedMultiple): self
{
$this->allowedMultiple = $allowedMultiple;
return $this;
}
public function isFixedValue(): ?bool
{
return $this->fixedValue;
}
public function setFixedValue(bool $fixedValue): self
{
$this->fixedValue = $fixedValue;
return $this;
}
public function getDefaultPointCategory(): ?string
{
return $this->defaultPointCategory;
}
public function setDefaultPointCategory(?string $defaultPointCategory): self
{
$this->defaultPointCategory = $defaultPointCategory;
return $this;
}
public function getProductUserQuota(): ?int
{
return $this->productUserQuota;
}
public function setProductUserQuota(?int $productUserQuota): self
{
$this->productUserQuota = $productUserQuota;
return $this;
}
/**
* @return Collection<int, QuotaProductUser>
*/
public function getQuotaProductUsers(): Collection
{
return $this->quotaProductUsers;
}
public function addQuotaProductUser(QuotaProductUser $quotaProductUser): self
{
if (!$this->quotaProductUsers->contains($quotaProductUser)) {
$this->quotaProductUsers[] = $quotaProductUser;
$quotaProductUser->setCustomProduct($this);
}
return $this;
}
public function removeQuotaProductUser(QuotaProductUser $quotaProductUser): self
{
if ($this->quotaProductUsers->removeElement($quotaProductUser)) {
// set the owning side to null (unless already changed)
if ($quotaProductUser->getCustomProduct() === $this) {
$quotaProductUser->setCustomProduct(null);
}
}
return $this;
}
public function getVisibilityFromJobs(): ?array
{
return $this->visibilityFromJobs;
}
public function setVisibilityFromJobs(?array $visibilityFromJobs): self
{
$this->visibilityFromJobs = $visibilityFromJobs;
return $this;
}
public function getQuotaProductJobs(): Collection
{
return $this->quotaProductJobs;
}
public function addQuotaProductJob(QuotaProductJob $quotaProductJob): self
{
if (!$this->quotaProductJobs->contains($quotaProductJob)) {
$this->quotaProductJobs->add($quotaProductJob);
$quotaProductJob->setCustomProduct($this);
}
return $this;
}
public function removeQuotaProductJob(QuotaProductJob $quotaProductJob): self
{
if ($this->quotaProductJobs->removeElement($quotaProductJob)) {
if ($quotaProductJob->getCustomProduct() === $this) {
$quotaProductJob->setCustomProduct(null);
}
}
return $this;
}
public function isDebitAtOrder(): ?bool
{
return $this->debitAtOrder;
}
public function setDebitAtOrder(bool $debitAtOrder): self
{
$this->debitAtOrder = $debitAtOrder;
return $this;
}
}