<?phpnamespace App\Entity;use App\Repository\ActivityRepository;use Doctrine\Common\Collections\Collection;use Doctrine\Common\Collections\ArrayCollection;use Doctrine\ORM\Mapping as ORM;use Symfony\Component\Validator\Constraints as Assert;#[ORM\Entity(repositoryClass: ActivityRepository::class)]class Activity extends Calendar{ #[ORM\Column(nullable: true)] #[Assert\PositiveOrZero] protected ?int $stock = null; #[ORM\Column] protected ?int $price = null; #[ORM\Column(nullable: true)] protected ?int $modifiedPrice = null; /** * @ORM\OneToMany(mappedBy="activity", targetEntity="App\Entity\PurchaseItem", cascade={"remove"}) */ private ?Collection $purchaseItems = null; public function __construct() { $this->purchaseItems = new ArrayCollection(); } #[ORM\ManyToOne(inversedBy: 'activities')] #[ORM\JoinColumn(nullable: false)] protected ?Category $category = null; #[ORM\ManyToMany(targetEntity: User::class)] private Collection $users; #[ORM\ManyToMany(targetEntity: Tide::class, inversedBy: 'calendars')] protected Collection $tides; public function __toString() { return $this->title; } public function incrementStock(int $quantity): void { $this->stock += $quantity; } public function decrementStock(int $quantity): void { if ($this->stock >= $quantity) { $this->stock -= $quantity; } else { throw new \InvalidArgumentException('Not enough stock to decrement'); } } public function getStock(): ?int { return $this->stock; } public function setStock(int $stock): static { $this->stock = $stock; return $this; } public function getCategory(): ?Category { return $this->category; } public function setCategory(?Category $category): self { $this->category = $category; return $this; } protected function getDefaultPrice(): ?int { // Récupérer le prix par défaut de la catégorie associée à l'activité $category = $this->getCategory(); if ($category) { $categorySetting = $category->getCategorySetting(); if ($categorySetting) { return $categorySetting->getPrice(); } } return null; } public function getPrice(): ?int { if ($this->modifiedPrice !== null) { return $this->modifiedPrice; } else { return $this->getDefaultPrice(); } } public function getModifiedPrice(): ?int { return $this->modifiedPrice; } public function setModifiedPrice(?int $modifiedPrice): self { $this->modifiedPrice = $modifiedPrice; // Si modifiedPrice est défini, on l'utilise à la place de price $this->price = $modifiedPrice !== null ? $modifiedPrice : $this->price; return $this; } /** * @return Collection<int, PurchaseItem>|null */ public function getPurchaseItems(): ?Collection { return $this->purchaseItems; } public function addPurchaseItem(PurchaseItem $purchaseItem): self { if (!$this->purchaseItems->contains($purchaseItem)) { $this->purchaseItems->add($purchaseItem); $purchaseItem->setActivitie($this); } return $this; } public function removePurchaseItem(PurchaseItem $purchaseItem): self { if ($this->purchaseItems->removeElement($purchaseItem)) { // set the owning side to null (unless already changed) if ($purchaseItem->getActivity() === $this) { $purchaseItem->setActivity(null); } } return $this; } /** * @return Collection<User> */ public function getUsers(): Collection { return $this->users; } public function addUser(User $user): self { if (!$this->users->contains($user)) { $this->users[] = $user; } return $this; } public function removeUser(User $user): self { $this->users->removeElement($user); return $this; } /** * @return Collection<int, Tide> */ public function getTides(): Collection { return $this->tides; } public function addTide(Tide $tide): static { if (!$this->tides->contains($tide)) { $this->tides->add($tide); } return $this; } public function removeTide(Tide $tide): static { $this->tides->removeElement($tide); return $this; } public function getStart(): ?\DateTimeInterface { return $this->start; } public function setStart(\DateTimeInterface $start): self { $this->start = $start; return $this; } }