src/Entity/Activity.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\ActivityRepository;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\Validator\Constraints as Assert;
  8. #[ORM\Entity(repositoryClass: ActivityRepository::class)]
  9. class Activity extends Calendar
  10. {
  11.     #[ORM\Column(nullable: true)]
  12.     #[Assert\PositiveOrZero]
  13.     protected ?int $stock = null;
  14.     #[ORM\Column]
  15.     protected ?int $price = null;
  16.     #[ORM\Column(nullable: true)]
  17.     protected ?int $modifiedPrice = null;
  18.        /**
  19.     * @ORM\OneToMany(mappedBy="activity", targetEntity="App\Entity\PurchaseItem", cascade={"remove"})
  20.      */
  21.     private ?Collection $purchaseItems = null; 
  22.     public function __construct()
  23.     {
  24.         $this->purchaseItems = new ArrayCollection();
  25.     }
  26.    
  27.     #[ORM\ManyToOne(inversedBy: 'activities')]
  28.     #[ORM\JoinColumn(nullable: false)]
  29.     protected ?Category $category = null;
  30.     #[ORM\ManyToMany(targetEntity: User::class)]
  31.     private Collection $users;
  32.     #[ORM\ManyToMany(targetEntity: Tide::class, inversedBy: 'calendars')]
  33.     protected Collection $tides;
  34.     public function __toString()
  35.     {
  36.         return $this->title;
  37.     }
  38.  
  39.     public function incrementStock(int $quantity): void
  40.     {
  41.         $this->stock += $quantity;
  42.     }
  43.     public function decrementStock(int $quantity): void
  44.     {
  45.         if ($this->stock >= $quantity) {
  46.             $this->stock -= $quantity;
  47.         } else {
  48.             throw new \InvalidArgumentException('Not enough stock to decrement');
  49.         }
  50.     }
  51.     public function getStock(): ?int
  52.     {
  53.         return $this->stock;
  54.     }
  55.     public function setStock(int $stock): static
  56.     {
  57.         $this->stock = $stock;
  58.         return $this;
  59.     }
  60.     public function getCategory(): ?Category
  61.     {
  62.         return $this->category;
  63.     }
  64.     public function setCategory(?Category $category): self
  65.     {
  66.         $this->category = $category;
  67.         return $this;
  68.     }
  69.     
  70.     protected function getDefaultPrice(): ?int
  71.     {
  72.         // Récupérer le prix par défaut de la catégorie associée à l'activité
  73.         $category = $this->getCategory();
  74.         if ($category) {
  75.             $categorySetting = $category->getCategorySetting();
  76.             if ($categorySetting) {
  77.                 return $categorySetting->getPrice();
  78.             }
  79.         }
  80.         return null;
  81.     }
  82.  
  83.     public function getPrice(): ?int
  84.     {
  85.         if ($this->modifiedPrice !== null) {
  86.             return $this->modifiedPrice;
  87.         } else {
  88.             return $this->getDefaultPrice();
  89.         }
  90.     }
  91.     
  92.     public function getModifiedPrice(): ?int
  93.     {
  94.         return $this->modifiedPrice;
  95.     }
  96.     
  97.     public function setModifiedPrice(?int $modifiedPrice): self
  98.     {
  99.         $this->modifiedPrice = $modifiedPrice;
  100.         // Si modifiedPrice est défini, on l'utilise à la place de price
  101.         $this->price = $modifiedPrice !== null ? $modifiedPrice : $this->price;
  102.         return $this;
  103.     }
  104.     
  105.     
  106.  
  107.      /**
  108.      * @return Collection<int, PurchaseItem>|null
  109.      */
  110.     public function getPurchaseItems(): ?Collection
  111.     {
  112.         return $this->purchaseItems;
  113.     }
  114.     public function addPurchaseItem(PurchaseItem $purchaseItem): self
  115.     {
  116.         if (!$this->purchaseItems->contains($purchaseItem)) {
  117.             $this->purchaseItems->add($purchaseItem);
  118.             $purchaseItem->setActivitie($this);
  119.         }
  120.         return $this;
  121.     }
  122.     public function removePurchaseItem(PurchaseItem $purchaseItem): self
  123.     {
  124.         if ($this->purchaseItems->removeElement($purchaseItem)) {
  125.             // set the owning side to null (unless already changed)
  126.             if ($purchaseItem->getActivity() === $this) {
  127.                 $purchaseItem->setActivity(null);
  128.             }
  129.         }
  130.         return $this;
  131.     }
  132.    /**
  133.      * @return Collection<User>
  134.      */
  135.     public function getUsers(): Collection
  136.     {
  137.         return $this->users;
  138.     }
  139.     public function addUser(User $user): self
  140.     {
  141.         if (!$this->users->contains($user)) {
  142.             $this->users[] = $user;
  143.         }
  144.         return $this;
  145.     }
  146.     public function removeUser(User $user): self
  147.     {
  148.         $this->users->removeElement($user);
  149.         return $this;
  150.     }
  151.  /**
  152.       * @return Collection<int, Tide>
  153.       */
  154.      public function getTides(): Collection
  155.      {
  156.          return $this->tides;
  157.      }
  158.      public function addTide(Tide $tide): static
  159.      {
  160.          if (!$this->tides->contains($tide)) {
  161.              $this->tides->add($tide);
  162.          }
  163.          return $this;
  164.      }
  165.      public function removeTide(Tide $tide): static
  166.      {
  167.          $this->tides->removeElement($tide);
  168.          return $this;
  169.      }
  170.    
  171.      public function getStart(): ?\DateTimeInterface
  172.      {
  173.          return $this->start;
  174.      }
  175.      public function setStart(\DateTimeInterface $start): self
  176.      {
  177.          $this->start = $start;
  178.          return $this;
  179.      }
  180.     
  181. }