<?php
namespace App\Entity;
use App\Repository\ProduitRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JsonSerializable;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ORM\Entity(repositoryClass=ProduitRepository::class)
*/
class Produit implements JsonSerializable
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
private $isNew = false;
private $stock = 0;
/**
* @ORM\Column(type="string", length=255)
*/
private $reference;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
*
* @ORM\Column(type="text", nullable=true)
*/
private $description;
/**
* @ORM\Column(type="integer")
*/
private $usual_quantity;
/**
* @ORM\Column(type="float")
*/
private $buying_price;
/**
*
* @ORM\Column(type="float")
*/
private $price_ht;
/**
*
* @ORM\Column(type="float")
*/
private $price_ttc;
/**
* @ORM\ManyToOne(targetEntity=Category::class, inversedBy="produits")
*/
private $categories;
/**
* @ORM\ManyToOne(targetEntity=Tva::class, inversedBy="produits")
*/
private $tva;
/**
* @ORM\ManyToMany(targetEntity=Tag::class, inversedBy="produits")
*/
private $tags;
/**
* @ORM\Column(type="string", length=255)
*/
private $unit;
/**
* @ORM\Column(type="boolean")
*/
private $isStock;
/**
* @ORM\Column(type="boolean")
*/
private $isDeclination;
/**
* @ORM\Column(type="datetime")
*/
private $createdAt;
/**
* @ORM\ManyToMany(targetEntity=Declination::class, mappedBy="Produits")
*/
private $declinations;
/**
* @ORM\OneToMany(targetEntity=ProduitDeclinationValue::class, mappedBy="produit", orphanRemoval=true)
*/
private $produitDeclinationValues;
/**
*
* @ORM\ManyToMany(targetEntity=File::class, cascade={"persist"})
*/
private $picture;
public $image;
/**
* @ORM\OneToMany(targetEntity=DocumentProduit::class, mappedBy="produit")
*/
private $documentProduits;
/**
* @ORM\OneToMany(targetEntity=Stock::class, mappedBy="produit")
*/
private $stocks;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $uuid;
/**
* @ORM\ManyToOne(targetEntity=Promotion::class, inversedBy="produits")
*/
private $promotion;
/**
* @ORM\OneToMany(targetEntity=Activity::class, mappedBy="produit")
*/
private $activities;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $information;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $isArchived;
/**
* @ORM\OneToMany(targetEntity=Comment::class, mappedBy="produit")
*/
private $comments;
// /**
// * @ORM\ManyToOne(targetEntity=Category::class, inversedBy="produitsCategory")
// */
// private $principalCategory;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $deletedAt;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $reasonOfDelete;
public function __construct()
{
$this->tags = new ArrayCollection();
$this->declinations = new ArrayCollection();
$this->produitDeclinationValues = new ArrayCollection();
$this->picture = new ArrayCollection();
$this->documentProduits = new ArrayCollection();
$this->stocks = new ArrayCollection();
$this->activities = new ArrayCollection();
$this->comments = new ArrayCollection();
}
public function getImage(): ?string
{
return $this->image;
}
public function getId(): ?int
{
return $this->id;
}
public function getReference(): ?string
{
return $this->reference;
}
public function setReference(string $reference): self
{
$this->reference = $reference;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getUsualQuantity(): ?int
{
return $this->usual_quantity;
}
public function setUsualQuantity(int $usual_quantity): self
{
$this->usual_quantity = $usual_quantity;
return $this;
}
public function getBuyingPrice(): ?float
{
return $this->buying_price;
}
public function setBuyingPrice(float $buying_price): self
{
$this->buying_price = $buying_price;
return $this;
}
public function getPriceHt(): ?float
{
return (float) $this->price_ht;
}
public function setPriceHt(float $price_ht): self
{
$this->price_ht = $price_ht;
return $this;
}
public function getPriceTtc(): ?float
{
return $this->price_ttc;
}
public function setPriceTtc(float $price_ttc): self
{
$this->price_ttc = $price_ttc;
return $this;
}
public function getCategories(): ?Category
{
return $this->categories;
}
public function setCategories(?Category $categories): self
{
$this->categories = $categories;
return $this;
}
public function getTva(): ?Tva
{
return $this->tva;
}
public function setTva(?Tva $tva): self
{
$this->tva = $tva;
return $this;
}
/**
* @return Collection|Tag[]
*/
public function getTags(): Collection
{
return $this->tags;
}
public function addTag(Tag $tag): self
{
if (!$this->tags->contains($tag)) {
$this->tags[] = $tag;
}
return $this;
}
public function removeTag(Tag $tag): self
{
$this->tags->removeElement($tag);
return $this;
}
public function getUnit(): ?string
{
return $this->unit;
}
public function setUnit(string $unit): self
{
$this->unit = $unit;
return $this;
}
public function getIsStock(): ?bool
{
return $this->isStock;
}
public function setIsStock(bool $isStock): self
{
$this->isStock = $isStock;
return $this;
}
public function getIsDeclination(): ?bool
{
return $this->isDeclination;
}
public function setIsDeclination(bool $isDeclination): self
{
$this->isDeclination = $isDeclination;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
/**
* @return Collection|Declination[]
*/
public function getDeclinations(): Collection
{
return $this->declinations;
}
public function addDeclination(Declination $declination): self
{
if (!$this->declinations->contains($declination)) {
$this->declinations[] = $declination;
$declination->addProduit($this);
}
return $this;
}
public function removeDeclination(Declination $declination): self
{
if ($this->declinations->removeElement($declination)) {
$declination->removeProduit($this);
}
return $this;
}
/**
* @return Collection|ProduitDeclinationValue[]
*/
public function getProduitDeclinationValues(): Collection
{
return $this->produitDeclinationValues;
}
public function addProduitDeclinationValue(ProduitDeclinationValue $produitDeclinationValue): self
{
if (!$this->produitDeclinationValues->contains($produitDeclinationValue)) {
$this->produitDeclinationValues[] = $produitDeclinationValue;
$produitDeclinationValue->setProduit($this);
}
return $this;
}
public function removeProduitDeclinationValue(ProduitDeclinationValue $produitDeclinationValue): self
{
if ($this->produitDeclinationValues->removeElement($produitDeclinationValue)) {
// set the owning side to null (unless already changed)
if ($produitDeclinationValue->getProduit() === $this) {
$produitDeclinationValue->setProduit(null);
}
}
return $this;
}
/**
* @return Collection|File[]
*/
public function getPicture(): Collection
{
return $this->picture;
}
public function addPicture(File $picture): self
{
if (!$this->picture->contains($picture))
$this->picture[] = $picture;
return $this;
}
public function removePicture(File $picture): self
{
$this->picture->removeElement($picture);
return $this;
}
/**
* @return Collection|DocumentProduit[]
*/
public function getDocumentProduits(): Collection
{
return $this->documentProduits;
}
public function addDocumentProduit(DocumentProduit $documentProduit): self
{
if (!$this->documentProduits->contains($documentProduit)) {
$this->documentProduits[] = $documentProduit;
$documentProduit->setProduit($this);
}
return $this;
}
public function removeDocumentProduit(DocumentProduit $documentProduit): self
{
if ($this->documentProduits->removeElement($documentProduit))
// set the owning side to null (unless already changed)
if ($documentProduit->getProduit() === $this)
$documentProduit->setProduit(null);
return $this;
}
/**
* @return Collection|Stock[]
*/
public function getStocks(): Collection
{
return $this->stocks;
}
public function addStock(Stock $stock): self
{
if (!$this->stocks->contains($stock)) {
$this->stocks[] = $stock;
$stock->setProduit($this);
}
return $this;
}
public function removeStock(Stock $stock): self
{
if ($this->stocks->removeElement($stock))
// set the owning side to null (unless already changed)
if ($stock->getProduit() === $this)
$stock->setProduit(null);
return $this;
}
public function getUuid(): ?int
{
return $this->uuid;
}
public function setUuid(?int $uuid): self
{
$this->uuid = $uuid;
return $this;
}
public function getPromotion(): ?Promotion
{
return $this->promotion;
}
public function setPromotion(?Promotion $promotion): self
{
$this->promotion = $promotion;
return $this;
}
/**
* @return Collection|Activity[]
*/
public function getActivities(): Collection
{
return $this->activities;
}
public function addActivity(Activity $activity): self
{
if (!$this->activities->contains($activity)) {
$this->activities[] = $activity;
$activity->setProduit($this);
}
return $this;
}
public function removeActivity(Activity $activity): self
{
if ($this->activities->removeElement($activity))
// set the owning side to null (unless already changed)
if ($activity->getProduit() === $this)
$activity->setProduit(null);
return $this;
}
public function getInformation(): ?string
{
return $this->information;
}
public function setInformation(?string $information): self
{
$this->information = $information;
return $this;
}
public function getIsArchived(): ?bool
{
return $this->isArchived;
}
public function setIsArchived(?bool $isArchived): self
{
$this->isArchived = $isArchived;
return $this;
}
/**
* @return Collection|Comment[]
*/
public function getComments(): Collection
{
return $this->comments;
}
public function addComment(Comment $comment): self
{
if (!$this->comments->contains($comment)) {
$this->comments[] = $comment;
$comment->setProduit($this);
}
return $this;
}
public function removeComment(Comment $comment): self
{
if ($this->comments->removeElement($comment))
// set the owning side to null (unless already changed)
if ($comment->getProduit() === $this)
$comment->setProduit(null);
return $this;
}
public function getIsNew(): ?bool
{
return $this->isNew;
}
public function setIsNew(bool $isNew): self
{
$this->isNew = $isNew;
return $this;
}
public function getStock(): ?int
{
foreach ($this->getStocks() as $stock) {
$this->stock += $stock->getQtAvailable() - $stock->getQtReserved();
}
return $this->stock;
}
public function setStock($stock): self
{
$this->stock = $stock;
return $this;
}
public function isNew()
{
// On va chercher les produit nouveau dont la date de création ne dépasse pas 90 jours
$date = new \DateTime('now');
$date->modify('-90 day');
if ($this->getCreatedAt() >= $date) {
$this->setIsNew(true);
}
return $this->isNew;
}
public function jsonSerialize()
{
$tabPictures = [];
foreach ($this->getPicture() as $picture) {
array_push($tabPictures, $picture->getImageName());
}
return array(
'id' => $this->getId(),
'name' => $this->getName(),
'description' => $this->getDescription(),
'picture' => $tabPictures,
'image' => $this->getImage(),
'priceHT' => $this->getPriceHt(),
'priceTTC' => $this->getPriceTtc(),
'category' => $this->getCategories() ? $this->getCategories()->getId() : 0,
//'produitDeclinationValues' => $this->getProduitDeclinationValues()->toArray(),
'promo' => $this->getPromotion(),
'isNew' => $this->isNew(),
'stock' => $this->getStock(),
//'tags' => $this->getTags()->toArray(),
);
}
public function getPrincipalCategory(): ?Category
{
return $this->principalCategory;
}
public function setPrincipalCategory(?Category $principalCategory): self
{
$this->principalCategory = $principalCategory;
return $this;
}
public function getDeletedAt(): ?\DateTimeInterface
{
return $this->deletedAt;
}
public function setDeletedAt(?\DateTimeInterface $deletedAt): self
{
$this->deletedAt = $deletedAt;
return $this;
}
public function getReasonOfDelete(): ?string
{
return $this->reasonOfDelete;
}
public function setReasonOfDelete(?string $reasonOfDelete): self
{
$this->reasonOfDelete = $reasonOfDelete;
return $this;
}
}