<?php
namespace App\Entity;
use App\Entity\Traits\TimeStampable;
use App\Repository\TaskRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
#[ORM\Entity(repositoryClass: TaskRepository::class)]
#[ORM\HasLifecycleCallbacks]
class Task
{
use TimeStampable;
#[ORM\Id]
#[ORM\GeneratedValue]
#[Groups(['taskProjet:read', 'task:read'])]
#[ORM\Column]
private ?int $id = null;
#[ORM\ManyToOne(inversedBy: 'tasks')]
private ?Projet $projet = null;
#[ORM\Column(length: 255)]
#[Groups(['taskProjet:read', 'task:read'])]
private ?string $titre = null;
#[ORM\Column(length: 2000, nullable: true)]
#[Groups(['taskProjet:read', 'task:read'])]
private ?string $description = null;
#[ORM\Column(type: Types::DATE_MUTABLE)]
#[Groups(['task:read'])]
private ?\DateTimeInterface $dateStart = null;
#[ORM\Column(type: Types::DATE_MUTABLE)]
#[Groups(['task:read'])]
private ?\DateTimeInterface $dateEnd = null;
#[ORM\ManyToOne(inversedBy: 'tasks')]
#[Groups(['taskProjet:read', 'task:read'])]
private ?Priority $priority = null;
#[ORM\OneToMany(mappedBy: 'task', targetEntity: TaskAssign::class, cascade: ['remove'])]
private Collection $taskAssigns;
#[ORM\Column(length: 255, nullable: true)]
// #[Groups(['taskProjet:read'])]
private ?string $status = null;
#[ORM\OneToMany(mappedBy: 'tache', targetEntity: Document::class)]
private Collection $documents;
#[ORM\OneToMany(mappedBy: 'task', targetEntity: Comment::class, cascade: ['remove'])]
private Collection $comments;
#[ORM\OneToMany(mappedBy: 'tache', targetEntity: RateOfProgress::class, cascade: ['persist', 'remove'])]
#[Groups(['task:read'])]
private Collection $rateOfProgress;
#[ORM\Column]
private ?bool $isValided = null;
public function __construct()
{
$this->status = "nouvelle";
$this->taskAssigns = new ArrayCollection();
$this->createdAt = new \DateTimeImmutable();
$this->documents = new ArrayCollection();
$this->comments = new ArrayCollection();
$this->rateOfProgress = new ArrayCollection();
$this->isValided = false;
}
public function getId(): ?int
{
return $this->id;
}
public function getProjet(): ?Projet
{
return $this->projet;
}
public function setProjet(?Projet $projet): self
{
$this->projet = $projet;
return $this;
}
public function getTitre(): ?string
{
return $this->titre;
}
public function setTitre(string $titre): self
{
$this->titre = $titre;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(string $description): self
{
$this->description = $description;
return $this;
}
public function getDateStart(): ?\DateTimeInterface
{
return $this->dateStart;
}
public function setDateStart(\DateTimeInterface $dateStart): self
{
$this->dateStart = $dateStart;
return $this;
}
public function getDateEnd(): ?\DateTimeInterface
{
return $this->dateEnd;
}
public function setDateEnd(\DateTimeInterface $dateEnd): self
{
$this->dateEnd = $dateEnd;
return $this;
}
public function getPriority(): ?Priority
{
return $this->priority;
}
public function setPriority(?Priority $priority): self
{
$this->priority = $priority;
$this->priority->setIsDeleted(false);
return $this;
}
/**
* @return Collection<int, TaskAssign>
*/
public function getTaskAssigns(): Collection
{
return $this->taskAssigns;
}
public function addTaskAssign(TaskAssign $taskAssign): self
{
if (!$this->taskAssigns->contains($taskAssign)) {
$this->taskAssigns->add($taskAssign);
$taskAssign->setTask($this);
}
return $this;
}
public function removeTaskAssign(TaskAssign $taskAssign): self
{
if ($this->taskAssigns->removeElement($taskAssign)) {
// set the owning side to null (unless already changed)
if ($taskAssign->getTask() === $this) {
$taskAssign->setTask(null);
}
}
return $this;
}
public function getStatus(): ?string
{
return $this->status;
}
public function setStatus(string $status): self
{
$this->status = $status;
return $this;
}
public function getUsersAssign(): array
{
$userAssing = [];
foreach ($this->taskAssigns as $assig) {
$userAssing[] = $assig->getUser();
}
return $userAssing;
}
public function getUserNotAssign(array $users): array
{
$userAssign = $this->getUsersAssign();
$userNotAssing = array_diff($users, $userAssign);
return $userNotAssing;
}
/**
* @return Collection<int, Document>
*/
public function getDocuments(): Collection
{
return $this->documents;
}
public function addDocument(Document $document): static
{
if (!$this->documents->contains($document)) {
$this->documents->add($document);
$document->setTache($this);
}
return $this;
}
public function removeDocument(Document $document): static
{
if ($this->documents->removeElement($document)) {
// set the owning side to null (unless already changed)
if ($document->getTache() === $this) {
$document->setTache(null);
}
}
return $this;
}
/**
* @return Collection<int, Comment>
*/
public function getComments(): Collection
{
return $this->comments;
}
public function addComment(Comment $comment): static
{
if (!$this->comments->contains($comment)) {
$this->comments->add($comment);
$comment->setTask($this);
}
return $this;
}
public function removeComment(Comment $comment): static
{
if ($this->comments->removeElement($comment)) {
// set the owning side to null (unless already changed)
if ($comment->getTask() === $this) {
$comment->setTask(null);
}
}
return $this;
}
/**
* @return Collection<int, RateOfProgress>
*/
public function getRateOfProgress(): Collection
{
return $this->rateOfProgress;
}
public function addRateOfProgress(RateOfProgress $rateOfProgress): static
{
if (!$this->rateOfProgress->contains($rateOfProgress)) {
$this->rateOfProgress->add($rateOfProgress);
$rateOfProgress->setTache($this);
}
return $this;
}
public function removeRateOfProgress(RateOfProgress $rateOfProgress): static
{
if ($this->rateOfProgress->removeElement($rateOfProgress)) {
// set the owning side to null (unless already changed)
if ($rateOfProgress->getTache() === $this) {
$rateOfProgress->setTache(null);
}
}
return $this;
}
public function isIsValided(): ?bool
{
return $this->isValided;
}
public function setIsValided(bool $isValided): static
{
$this->isValided = $isValided;
return $this;
}
#[Groups(['taskProjet:read', 'task:read'])]
public function getTauxAvancement(): int
{
$taux = [];
$countAvaluation = count($this->getRateOfProgress());
foreach ($this->rateOfProgress as $rate) {
if ($rate->isIsValided()) {
$taux[] = 100;
} else {
$taux[] = 0;
}
}
$sumTaux = array_sum($taux) > 0 ? array_sum($taux)/$countAvaluation: 0;
return $sumTaux;
}
}