<?php
namespace App\Entity;
use App\Repository\PackRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=PackRepository::class)
*/
class Pack
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $reference;
/**
*
* @ORM\Column(type="text", nullable=true)
*/
private $description;
/**
*
* @ORM\Column(type="float", nullable=true)
*/
private $price_ttc;
/**
* @ORM\Column(type="float", nullable=true)
*/
private $final_price_ttc = 0.0;
/**
*
* @ORM\ManyToMany(targetEntity=File::class, cascade={"persist"})
*/
private $pictures;
/**
* @ORM\ManyToMany(targetEntity=ProduitDeclinationValue::class, inversedBy="packs")
*/
private $declinations;
/**
* @ORM\OneToMany(targetEntity=DocumentProduit::class, mappedBy="pack")
*/
private $documentPacks;
/**
* @ORM\OneToMany(targetEntity=Comment::class, mappedBy="pack")
*/
private $comments;
/**
* @ORM\OneToMany(targetEntity=Activity::class, mappedBy="pack")
*/
private $activities;
/**
* @ORM\OneToMany(targetEntity=PackItem::class, mappedBy="pack", cascade={"persist", "remove"}, orphanRemoval=true)
* @ORM\OrderBy({"position" = "ASC", "id" = "ASC"})
*/
private $items;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $isArchived;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $createdAt;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $information;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $metaDescription;
/**
* @ORM\Column(type="boolean", options={"default": true})
*/
private $showInWebSite = true;
/**
* @ORM\ManyToOne(targetEntity=Produit::class)
*/
private $produit1;
/**
* @ORM\ManyToOne(targetEntity=Produit::class)
*/
private $produit2;
/**
* @ORM\Column(type="float", nullable=true)
*/
private $remise;
public function __construct()
{
$this->declinations = new ArrayCollection();
$this->pictures = new ArrayCollection();
$this->documentPacks = new ArrayCollection();
$this->items = new ArrayCollection();
$this->comments = new ArrayCollection();
$this->activities = new ArrayCollection();
$this->createdAt = new \DateTime();
}
public function getId(): ?int
{
return $this->id;
}
public function addDeclination(ProduitDeclinationValue $declination): self
{
if (!$this->declinations->contains($declination)) {
$this->declinations[] = $declination;
}
return $this;
}
public function removeDeclination(ProduitDeclinationValue $declination): self
{
$this->declinations->removeElement($declination);
return $this;
}
public function getDeclinations(): Collection
{
return $this->declinations;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getReference(): ?string
{
return $this->reference;
}
public function setReference(?string $reference): self
{
$this->reference = $reference;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getPriceTtc(): ?float
{
return $this->price_ttc;
}
public function setPriceTtc(?float $price_ttc): self
{
$this->price_ttc = $price_ttc;
$this->recomputeFinalPrice();
return $this;
}
public function getFinalPriceTtc(): ?float
{
return $this->final_price_ttc;
}
public function setFinalPriceTtc(?float $final_price_ttc): self
{
$this->final_price_ttc = $final_price_ttc;
return $this;
}
/**
* @return Collection|File[]
*/
public function getPictures(): Collection
{
return $this->pictures;
}
public function addPicture(File $picture): self
{
if (!$this->pictures->contains($picture)) {
$this->pictures[] = $picture;
}
return $this;
}
public function removePicture(File $picture): self
{
$this->pictures->removeElement($picture);
return $this;
}
public function getPicture(): Collection
{
return $this->getPictures();
}
public function getIsArchived(): ?bool
{
return $this->isArchived;
}
public function setIsArchived(?bool $isArchived): self
{
$this->isArchived = $isArchived;
return $this;
}
public function getProduit1(): ?Produit
{
return $this->produit1;
}
public function setProduit1(?Produit $produit1): self
{
$this->produit1 = $produit1;
return $this;
}
public function getProduit2(): ?Produit
{
return $this->produit2;
}
public function setProduit2(?Produit $produit2): self
{
$this->produit2 = $produit2;
return $this;
}
public function getRemise(): ?float
{
return $this->remise;
}
public function setRemise(?float $remise): self
{
$this->remise = $remise;
$this->recomputeFinalPrice();
return $this;
}
public function getPrixApresRemise(): ?float
{
if (!$this->produit1 || !$this->produit2 || $this->remise === null) {
return null;
}
$prixOriginal = (float) $this->produit1->getPriceTtc() + (float) $this->produit2->getPriceTtc();
return $prixOriginal * (1 - $this->remise / 100);
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(?\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getInformation(): ?string
{
return $this->information;
}
public function setInformation(?string $information): self
{
$this->information = $information;
return $this;
}
public function getMetaDescription(): ?string
{
return $this->metaDescription;
}
public function setMetaDescription(?string $metaDescription): self
{
$this->metaDescription = $metaDescription;
return $this;
}
public function getShowInWebsite(): ?bool
{
return $this->showInWebSite;
}
public function setShowInWebSite(?bool $showInWebSite): self
{
$this->showInWebSite = (bool) $showInWebSite;
return $this;
}
/**
* @return Collection|PackItem[]
*/
public function getItems(): Collection
{
return $this->items;
}
public function addItem(PackItem $item): self
{
if (!$this->items->contains($item)) {
$this->items[] = $item;
$item->setPack($this);
$this->recomputePriceFromItems();
}
return $this;
}
public function removeItem(PackItem $item): self
{
if ($this->items->removeElement($item)) {
if ($item->getPack() === $this) {
$item->setPack(null);
}
$this->recomputePriceFromItems();
}
return $this;
}
public function recomputePriceFromItems(): self
{
$sum = 0.0;
foreach ($this->items as $item) {
$produit = $item->getProduit();
if ($produit) {
$sum += (float) ($produit->getPriceTtc() ?? 0);
}
}
$this->price_ttc = round($sum, 3);
$this->recomputeFinalPrice();
return $this;
}
private function recomputeFinalPrice(): void
{
$base = (float) ($this->price_ttc ?? 0);
$discount = (float) ($this->remise ?? 0);
$this->final_price_ttc = round(max(0, $base - ($base * $discount / 100)), 3);
}
public function __toString(): string
{
return (string) ($this->name ?? ('Pack #' . $this->id));
}
/**
* @return Collection|DocumentProduit[]
*/
public function getDocumentPacks(): Collection
{
return $this->documentPacks;
}
/**
* @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->setPack($this);
}
return $this;
}
public function removeComment(Comment $comment): self
{
if ($this->comments->removeElement($comment)) {
if ($comment->getPack() === $this) {
$comment->setPack(null);
}
}
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->setPack($this);
}
return $this;
}
public function removeActivity(Activity $activity): self
{
if ($this->activities->removeElement($activity)) {
if ($activity->getPack() === $this) {
$activity->setPack(null);
}
}
return $this;
}
}