<?php
namespace App\Entity;
use App\Repository\ProjectRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: ProjectRepository::class)]
class Project
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $slug = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $url = null;
#[ORM\OneToMany(mappedBy: 'project', targetEntity: ClientOrder::class, orphanRemoval: true)]
private Collection $clientOrders;
#[ORM\OneToMany(mappedBy: 'project', targetEntity: NielsenOrder::class, orphanRemoval: true)]
private Collection $nielsenOrders;
#[ORM\Column(length: 255)]
private ?string $name = null;
#[ORM\OneToOne(inversedBy: 'project', cascade: ['persist', 'remove'])]
private ?Magasin $magasin = null;
#[ORM\OneToOne(mappedBy: 'project', cascade: ['persist', 'remove'])]
private ?ProjectSettings $projectSettings = null;
#[ORM\Column(length: 255)]
private ?string $token = null;
public function __construct()
{
$this->clientOrders = new ArrayCollection();
$this->nielsenOrders = new ArrayCollection();
$this->token = bin2hex(random_bytes(16));
}
public function getChiffreAffaire(): float
{
return $this->getClientOrders()
->reduce(fn(int $carry, ClientOrder $clientOrder) => $carry + $clientOrder->getPrice(), 0);
}
public function getId(): ?int
{
return $this->id;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(?string $slug): static
{
$this->slug = $slug;
return $this;
}
public function getUrl(): ?string
{
return $this->url;
}
public function setUrl(?string $url): static
{
$this->url = $url;
return $this;
}
/**
* @return Collection<int, ClientOrder>
*/
public function getClientOrders(): Collection
{
return $this->clientOrders->filter(function(ClientOrder $clientOrder) {
return $clientOrder->isIsArchived() === false;
});
}
/**
* @return Collection<int, ClientOrder>
*/
public function getAllClientOrders(): Collection
{
return $this->clientOrders;
}
public function addClientOrder(ClientOrder $clientOrder): static
{
if (!$this->clientOrders->contains($clientOrder)) {
$this->clientOrders->add($clientOrder);
$clientOrder->setProject($this);
}
return $this;
}
public function removeClientOrder(ClientOrder $clientOrder): static
{
if ($this->clientOrders->removeElement($clientOrder)) {
// set the owning side to null (unless already changed)
if ($clientOrder->getProject() === $this) {
$clientOrder->setProject(null);
}
}
return $this;
}
/**
* @return Collection<int, NielsenOrder>
*/
public function getNielsenOrders(): Collection
{
return $this->nielsenOrders;
}
public function addNielsenOrder(NielsenOrder $nielsenOrder): static
{
if (!$this->nielsenOrders->contains($nielsenOrder)) {
$this->nielsenOrders->add($nielsenOrder);
$nielsenOrder->setProject($this);
}
return $this;
}
public function removeNielsenOrder(NielsenOrder $nielsenOrder): static
{
if ($this->nielsenOrders->removeElement($nielsenOrder)) {
// set the owning side to null (unless already changed)
if ($nielsenOrder->getProject() === $this) {
$nielsenOrder->setProject(null);
}
}
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): static
{
$this->name = $name;
return $this;
}
public function getMagasin(): ?Magasin
{
return $this->magasin;
}
public function setMagasin(?Magasin $magasin): static
{
$this->magasin = $magasin;
return $this;
}
public function getProjectSettings(): ?ProjectSettings
{
return $this->projectSettings;
}
public function setProjectSettings(ProjectSettings $projectSettings): static
{
// set the owning side of the relation if necessary
if ($projectSettings->getProject() !== $this) {
$projectSettings->setProject($this);
}
$this->projectSettings = $projectSettings;
return $this;
}
public function getToken(): ?string
{
return $this->token;
}
public function setToken(string $token): static
{
$this->token = $token;
return $this;
}
}