src/Entity/Pack.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\PackRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. /**
  8.  * @ORM\Entity(repositoryClass=PackRepository::class)
  9.  */
  10. class Pack
  11. {
  12.     /**
  13.      * @ORM\Id()
  14.      * @ORM\GeneratedValue()
  15.      * @ORM\Column(type="integer")
  16.      */
  17.     private $id;
  18.     /**
  19.      * @ORM\Column(type="string", length=255)
  20.      */
  21.     private $name;
  22.     /**
  23.      * @ORM\Column(type="string", length=255, nullable=true)
  24.      */
  25.     private $reference;
  26.     /**
  27.      *
  28.      * @ORM\Column(type="text", nullable=true)
  29.      */
  30.     private $description;
  31.     /**
  32.      *
  33.      * @ORM\Column(type="float", nullable=true)
  34.      */
  35.     private $price_ttc;
  36.     /**
  37.      * @ORM\Column(type="float", nullable=true)
  38.      */
  39.     private $final_price_ttc 0.0;
  40.     /**
  41.      *
  42.      * @ORM\ManyToMany(targetEntity=File::class, cascade={"persist"})
  43.      */
  44.     private $pictures;
  45.     /**
  46.      * @ORM\ManyToMany(targetEntity=ProduitDeclinationValue::class, inversedBy="packs")
  47.      */
  48.     private $declinations;
  49.     /**
  50.      * @ORM\OneToMany(targetEntity=DocumentProduit::class, mappedBy="pack")
  51.      */
  52.     private $documentPacks;
  53.     /**
  54.      * @ORM\OneToMany(targetEntity=Comment::class, mappedBy="pack")
  55.      */
  56.     private $comments;
  57.     /**
  58.      * @ORM\OneToMany(targetEntity=Activity::class, mappedBy="pack")
  59.      */
  60.     private $activities;
  61.     /**
  62.      * @ORM\OneToMany(targetEntity=PackItem::class, mappedBy="pack", cascade={"persist", "remove"}, orphanRemoval=true)
  63.      * @ORM\OrderBy({"position" = "ASC", "id" = "ASC"})
  64.      */
  65.     private $items;
  66.     /**
  67.      * @ORM\Column(type="boolean", nullable=true)
  68.      */
  69.     private $isArchived;
  70.     /**
  71.      * @ORM\Column(type="datetime", nullable=true)
  72.      */
  73.     private $createdAt;
  74.     /**
  75.      * @ORM\Column(type="text", nullable=true)
  76.      */
  77.     private $information;
  78.     /**
  79.      * @ORM\Column(type="string", length=255, nullable=true)
  80.      */
  81.     private $metaDescription;
  82.     /**
  83.      * @ORM\Column(type="boolean", options={"default": true})
  84.      */
  85.     private $showInWebSite true;
  86.     /**
  87.      * @ORM\ManyToOne(targetEntity=Produit::class)
  88.      */
  89.     private $produit1;
  90.     /**
  91.      * @ORM\ManyToOne(targetEntity=Produit::class)
  92.      */
  93.     private $produit2;
  94.     /**
  95.      * @ORM\Column(type="float", nullable=true)
  96.      */
  97.     private $remise;
  98.     public function __construct()
  99.     {
  100.         $this->declinations = new ArrayCollection();
  101.         $this->pictures = new ArrayCollection();
  102.         $this->documentPacks = new ArrayCollection();
  103.         $this->items = new ArrayCollection();
  104.         $this->comments = new ArrayCollection();
  105.         $this->activities = new ArrayCollection();
  106.         $this->createdAt = new \DateTime();
  107.     }
  108.     
  109.     public function getId(): ?int
  110.     {
  111.         return $this->id;
  112.     }
  113.     
  114.     public function addDeclination(ProduitDeclinationValue $declination): self
  115.     {
  116.         if (!$this->declinations->contains($declination)) {
  117.             $this->declinations[] = $declination;
  118.         }
  119.         return $this;
  120.     }
  121.     public function removeDeclination(ProduitDeclinationValue $declination): self
  122.     {
  123.         $this->declinations->removeElement($declination);
  124.         return $this;
  125.     }
  126.     public function getDeclinations(): Collection
  127.     {
  128.         return $this->declinations;
  129.     }
  130.     public function getName(): ?string
  131.     {
  132.         return $this->name;
  133.     }
  134.     public function setName(string $name): self
  135.     {
  136.         $this->name $name;
  137.         return $this;
  138.     }
  139.     public function getReference(): ?string
  140.     {
  141.         return $this->reference;
  142.     }
  143.     public function setReference(?string $reference): self
  144.     {
  145.         $this->reference $reference;
  146.         return $this;
  147.     }
  148.     public function getDescription(): ?string
  149.     {
  150.         return $this->description;
  151.     }
  152.     public function setDescription(?string $description): self
  153.     {
  154.         $this->description $description;
  155.         return $this;
  156.     }
  157.     public function getPriceTtc(): ?float
  158.     {
  159.         return $this->price_ttc;
  160.     }
  161.     public function setPriceTtc(?float $price_ttc): self
  162.     {
  163.         $this->price_ttc $price_ttc;
  164.         $this->recomputeFinalPrice();
  165.         return $this;
  166.     }
  167.     public function getFinalPriceTtc(): ?float
  168.     {
  169.         return $this->final_price_ttc;
  170.     }
  171.     public function setFinalPriceTtc(?float $final_price_ttc): self
  172.     {
  173.         $this->final_price_ttc $final_price_ttc;
  174.         return $this;
  175.     }
  176.     /**
  177.      * @return Collection|File[]
  178.      */
  179.     public function getPictures(): Collection
  180.     {
  181.         return $this->pictures;
  182.     }
  183.     public function addPicture(File $picture): self
  184.     {
  185.         if (!$this->pictures->contains($picture)) {
  186.             $this->pictures[] = $picture;
  187.         }
  188.         return $this;
  189.     }
  190.     public function removePicture(File $picture): self
  191.     {
  192.         $this->pictures->removeElement($picture);
  193.         return $this;
  194.     }
  195.     public function getPicture(): Collection
  196.     {
  197.         return $this->getPictures();
  198.     }
  199.     public function getIsArchived(): ?bool
  200.     {
  201.         return $this->isArchived;
  202.     }
  203.     public function setIsArchived(?bool $isArchived): self
  204.     {
  205.         $this->isArchived $isArchived;
  206.         return $this;
  207.     }
  208.     public function getProduit1(): ?Produit
  209.     {
  210.         return $this->produit1;
  211.     }
  212.     public function setProduit1(?Produit $produit1): self
  213.     {
  214.         $this->produit1 $produit1;
  215.         return $this;
  216.     }
  217.     public function getProduit2(): ?Produit
  218.     {
  219.         return $this->produit2;
  220.     }
  221.     public function setProduit2(?Produit $produit2): self
  222.     {
  223.         $this->produit2 $produit2;
  224.         return $this;
  225.     }
  226.     public function getRemise(): ?float
  227.     {
  228.         return $this->remise;
  229.     }
  230.     public function setRemise(?float $remise): self
  231.     {
  232.         $this->remise $remise;
  233.         $this->recomputeFinalPrice();
  234.         return $this;
  235.     }
  236.     public function getPrixApresRemise(): ?float
  237.     {
  238.         if (!$this->produit1 || !$this->produit2 || $this->remise === null) {
  239.             return null;
  240.         }
  241.         $prixOriginal = (float) $this->produit1->getPriceTtc() + (float) $this->produit2->getPriceTtc();
  242.         return $prixOriginal * ($this->remise 100);
  243.     }
  244.     public function getCreatedAt(): ?\DateTimeInterface
  245.     {
  246.         return $this->createdAt;
  247.     }
  248.     public function setCreatedAt(?\DateTimeInterface $createdAt): self
  249.     {
  250.         $this->createdAt $createdAt;
  251.         return $this;
  252.     }
  253.     public function getInformation(): ?string
  254.     {
  255.         return $this->information;
  256.     }
  257.     public function setInformation(?string $information): self
  258.     {
  259.         $this->information $information;
  260.         return $this;
  261.     }
  262.     public function getMetaDescription(): ?string
  263.     {
  264.         return $this->metaDescription;
  265.     }
  266.     public function setMetaDescription(?string $metaDescription): self
  267.     {
  268.         $this->metaDescription $metaDescription;
  269.         return $this;
  270.     }
  271.     public function getShowInWebsite(): ?bool
  272.     {
  273.         return $this->showInWebSite;
  274.     }
  275.     public function setShowInWebSite(?bool $showInWebSite): self
  276.     {
  277.         $this->showInWebSite = (bool) $showInWebSite;
  278.         return $this;
  279.     }
  280.     /**
  281.      * @return Collection|PackItem[]
  282.      */
  283.     public function getItems(): Collection
  284.     {
  285.         return $this->items;
  286.     }
  287.     public function addItem(PackItem $item): self
  288.     {
  289.         if (!$this->items->contains($item)) {
  290.             $this->items[] = $item;
  291.             $item->setPack($this);
  292.             $this->recomputePriceFromItems();
  293.         }
  294.         return $this;
  295.     }
  296.     public function removeItem(PackItem $item): self
  297.     {
  298.         if ($this->items->removeElement($item)) {
  299.             if ($item->getPack() === $this) {
  300.                 $item->setPack(null);
  301.             }
  302.             $this->recomputePriceFromItems();
  303.         }
  304.         return $this;
  305.     }
  306.     public function recomputePriceFromItems(): self
  307.     {
  308.         $sum 0.0;
  309.         foreach ($this->items as $item) {
  310.             $produit $item->getProduit();
  311.             if ($produit) {
  312.                 $sum += (float) ($produit->getPriceTtc() ?? 0);
  313.             }
  314.         }
  315.         $this->price_ttc round($sum3);
  316.         $this->recomputeFinalPrice();
  317.         return $this;
  318.     }
  319.     private function recomputeFinalPrice(): void
  320.     {
  321.         $base = (float) ($this->price_ttc ?? 0);
  322.         $discount = (float) ($this->remise ?? 0);
  323.         $this->final_price_ttc round(max(0$base - ($base $discount 100)), 3);
  324.     }
  325.     public function __toString(): string
  326.     {
  327.         return (string) ($this->name ?? ('Pack #' $this->id));
  328.     }
  329.     /**
  330.      * @return Collection|DocumentProduit[]
  331.      */
  332.     public function getDocumentPacks(): Collection
  333.     {
  334.         return $this->documentPacks;
  335.     }
  336.     /**
  337.      * @return Collection|Comment[]
  338.      */
  339.     public function getComments(): Collection
  340.     {
  341.         return $this->comments;
  342.     }
  343.     public function addComment(Comment $comment): self
  344.     {
  345.         if (!$this->comments->contains($comment)) {
  346.             $this->comments[] = $comment;
  347.             $comment->setPack($this);
  348.         }
  349.         return $this;
  350.     }
  351.     public function removeComment(Comment $comment): self
  352.     {
  353.         if ($this->comments->removeElement($comment)) {
  354.             if ($comment->getPack() === $this) {
  355.                 $comment->setPack(null);
  356.             }
  357.         }
  358.         return $this;
  359.     }
  360.     /**
  361.      * @return Collection|Activity[]
  362.      */
  363.     public function getActivities(): Collection
  364.     {
  365.         return $this->activities;
  366.     }
  367.     public function addActivity(Activity $activity): self
  368.     {
  369.         if (!$this->activities->contains($activity)) {
  370.             $this->activities[] = $activity;
  371.             $activity->setPack($this);
  372.         }
  373.         return $this;
  374.     }
  375.     public function removeActivity(Activity $activity): self
  376.     {
  377.         if ($this->activities->removeElement($activity)) {
  378.             if ($activity->getPack() === $this) {
  379.                 $activity->setPack(null);
  380.             }
  381.         }
  382.         return $this;
  383.     }
  384. }