src/Entity/ItemVariation.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Enum\RegistrationStatus;
  4. use App\Entity\Traits\Identifiable;
  5. use App\Repository\ItemVariationRepository;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\ORM\Mapping as ORM;
  9. #[ORM\Entity(repositoryClassItemVariationRepository::class)]
  10. class ItemVariation
  11. {
  12.     use Identifiable;
  13.     #[ORM\Column(type'string'length100nullabletrue)]
  14.     private $name;
  15.     #[ORM\Column(type'smallint'nullabletrue)]
  16.     private $price;
  17.     #[ORM\Column(type'boolean')]
  18.     private $isDisplayed true;
  19.     #[ORM\Column(type'boolean')]
  20.     private $hasStock false;
  21.     #[ORM\ManyToOne(targetEntityItem::class, inversedBy'variations')]
  22.     #[ORM\JoinColumn(nullablefalse)]
  23.     private $item;
  24.     #[ORM\ManyToOne(targetEntityItemSize::class, inversedBy'itemVariations')]
  25.     private $size;
  26.     #[ORM\OneToOne(targetEntityFileItem::class, cascade: ['persist''remove'])]
  27.     private $file;
  28.     #[ORM\OneToMany(mappedBy'purchaseItem'targetEntityCartItemPurchase::class)]
  29.     private $purchases;
  30.     #[ORM\OneToMany(mappedBy'rentalItem'targetEntityCartItemRental::class)]
  31.     private $rentals;
  32.     #[ORM\OneToMany(mappedBy'variation'targetEntityItemVariationSection::class, orphanRemovaltruecascade: ['persist'])]
  33.     private $itemVariationSections;
  34.     public function __construct()
  35.     {
  36.         $this->purchases = new ArrayCollection();
  37.         $this->rentals = new ArrayCollection();
  38.         $this->itemVariationSections = new ArrayCollection();
  39.     }
  40.     public function getName(bool $size true): ?string
  41.     {
  42.         $name $this->name ?? $this->getItem()?->getName();
  43.         if ($size) {
  44.             if ($this->getSize()) {
  45.                 $name sprintf('%s - %s'$name$this->getSize()->getName());
  46.             } else {
  47.                 $name sprintf('%s - Taille unique'$name);
  48.             }
  49.         }
  50.         return $name;
  51.     }
  52.     public function setName(?string $name): self
  53.     {
  54.         $this->name $name;
  55.         return $this;
  56.     }
  57.     public function getPrice(): ?int
  58.     {
  59.         return $this->price ?? $this->getItem()?->getPrice();
  60.     }
  61.     public function setPrice(?int $price): self
  62.     {
  63.         $this->price $price;
  64.         return $this;
  65.     }
  66.     public function getIsDisplayed(): ?bool
  67.     {
  68.         return $this->getItem()?->getIsDisplayed() ? $this->isDisplayed false;
  69.     }
  70.     public function setIsDisplayed(bool $isDisplayed): self
  71.     {
  72.         $this->isDisplayed $isDisplayed;
  73.         return $this;
  74.     }
  75.     public function getHasStock(): ?bool
  76.     {
  77.         return $this->hasStock;
  78.     }
  79.     public function setHasStock(bool $hasStock): self
  80.     {
  81.         $this->hasStock $hasStock;
  82.         return $this;
  83.     }
  84.     public function getItem(): ?Item
  85.     {
  86.         return $this->item;
  87.     }
  88.     public function setItem(?Item $item): self
  89.     {
  90.         $this->item $item;
  91.         return $this;
  92.     }
  93.     public function getSize(): ?ItemSize
  94.     {
  95.         return $this->size;
  96.     }
  97.     public function setSize(?ItemSize $size): self
  98.     {
  99.         $this->size $size;
  100.         return $this;
  101.     }
  102.     public function getFile(): ?FileItem
  103.     {
  104.         return $this->file ?? $this->getItem()?->getFile();
  105.     }
  106.     public function setFile(?FileItem $file): self
  107.     {
  108.         $this->file $file;
  109.         return $this;
  110.     }
  111.     /**
  112.      * @return Collection<int, CartItemPurchase>
  113.      */
  114.     public function getPurchases(): Collection
  115.     {
  116.         return $this->purchases;
  117.     }
  118.     public function addPurchase(CartItemPurchase $purchase): self
  119.     {
  120.         if (!$this->purchases->contains($purchase)) {
  121.             $this->purchases[] = $purchase;
  122.             $purchase->setPurchaseItem($this);
  123.         }
  124.         return $this;
  125.     }
  126.     public function removePurchase(CartItemPurchase $purchase): self
  127.     {
  128.         if ($this->purchases->removeElement($purchase)) {
  129.             // set the owning side to null (unless already changed)
  130.             if ($purchase->getPurchaseItem() === $this) {
  131.                 $purchase->setPurchaseItem(null);
  132.             }
  133.         }
  134.         return $this;
  135.     }
  136.     /**
  137.      * @return Collection<int, CartItemRental>
  138.      */
  139.     public function getRentals(): Collection
  140.     {
  141.         return $this->rentals;
  142.     }
  143.     public function addRental(CartItemRental $rental): self
  144.     {
  145.         if (!$this->rentals->contains($rental)) {
  146.             $this->rentals[] = $rental;
  147.             $rental->setRentalItem($this);
  148.         }
  149.         return $this;
  150.     }
  151.     public function removeRental(CartItemRental $rental): self
  152.     {
  153.         if ($this->rentals->removeElement($rental)) {
  154.             // set the owning side to null (unless already changed)
  155.             if ($rental->getRentalItem() === $this) {
  156.                 $rental->setRentalItem(null);
  157.             }
  158.         }
  159.         return $this;
  160.     }
  161.     /**
  162.      * @return Collection<int, ItemVariationSection>
  163.      */
  164.     public function getItemVariationSections(): Collection
  165.     {
  166.         return $this->itemVariationSections;
  167.     }
  168.     public function addItemVariationSection(ItemVariationSection $itemVariationSection): self
  169.     {
  170.         if (!$this->itemVariationSections->contains($itemVariationSection)) {
  171.             $this->itemVariationSections[] = $itemVariationSection;
  172.             $itemVariationSection->setVariation($this);
  173.         }
  174.         return $this;
  175.     }
  176.     public function removeItemVariationSection(ItemVariationSection $itemVariationSection): self
  177.     {
  178.         if ($this->itemVariationSections->removeElement($itemVariationSection)) {
  179.             // set the owning side to null (unless already changed)
  180.             if ($itemVariationSection->getVariation() === $this) {
  181.                 $itemVariationSection->setVariation(null);
  182.             }
  183.         }
  184.         return $this;
  185.     }
  186.     public function getRemainingStock(?Section $section null): ?int
  187.     {
  188.         if (!$this->hasStock) {
  189.             return null;
  190.         }
  191.         $remaining 0;
  192.         foreach ($this->getItemVariationSections() as $itemVariationSection) {
  193.             if ($section && $section !== $itemVariationSection->getSection()) {
  194.                 continue;
  195.             }
  196.             foreach ($itemVariationSection->getStocks() as $stock) {
  197.                 $remaining += $stock->getStock();
  198.             }
  199.         }
  200.         $usages array_merge($this->getPurchases()->toArray(), $this->getRentals()->toArray());
  201.         foreach ($usages as $purchase) {
  202.             $registration $purchase->getCart()->getOrder()->getRegistration();
  203.             if ($section && $registration->getSeason()->getSection() !== $section) {
  204.                 continue;
  205.             }
  206.             if (in_array($registration->getStatus(), RegistrationStatus::VALID_VALUEStrue)) {
  207.                 $remaining--;
  208.             }
  209.         }
  210.         return $remaining;
  211.     }
  212.     /**
  213.      * @return array<int, Section>
  214.      */
  215.     public function getSections(): array
  216.     {
  217.         $sections = [];
  218.         foreach ($this->getItemVariationSections() as $itemVariationSection) {
  219.             $sections[] = $itemVariationSection->getSection();
  220.         }
  221.         return $sections;
  222.     }
  223. }