<?php
namespace App\Entity;
use App\Repository\TaskRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
#[ORM\Entity(repositoryClass: TaskRepository::class)]
class Task
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
#[Groups(['LogService'])]
private $id;
#[ORM\Column(type: 'date')]
#[Groups(['LogService'])]
private $week;
#[ORM\ManyToOne(targetEntity: Project::class, inversedBy: 'tasks')]
#[ORM\JoinColumn(nullable: false)]
private $project;
#[ORM\ManyToOne(targetEntity: TaskType::class, inversedBy: 'tasks')]
#[ORM\JoinColumn(nullable: false)]
private $taskType;
#[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'tasks')]
#[ORM\JoinColumn(nullable: false)]
private $user;
#[ORM\OneToMany(targetEntity: TimeSpent::class, mappedBy: 'task', orphanRemoval: true, fetch: 'EXTRA_LAZY')]
#[ORM\OrderBy(['date' => 'ASC'])]
private $timeSpents;
#[ORM\Column(type: 'datetime', nullable: true)]
#[Groups(['LogService'])]
private $createdAt;
#[ORM\Column(type: 'datetime', nullable: true)]
#[Groups(['LogService'])]
private $updatedAt;
public function __construct()
{
$this->timeSpents = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getWeek(): ?\DateTimeInterface
{
return $this->week;
}
public function setWeek(\DateTimeInterface $week): self
{
$this->week = $week;
return $this;
}
public function getProject(): ?Project
{
return $this->project;
}
public function setProject(?Project $project): self
{
$this->project = $project;
return $this;
}
public function getTaskType(): ?TaskType
{
return $this->taskType;
}
public function setTaskType(?TaskType $taskType): self
{
$this->taskType = $taskType;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
/**
* @return Collection<int, TimeSpent>
*/
public function getTimeSpents(): Collection
{
return $this->timeSpents;
}
public function addTimeSpent(TimeSpent $timeSpent): self
{
if (!$this->timeSpents->contains($timeSpent)) {
$this->timeSpents[] = $timeSpent;
$timeSpent->setTask($this);
}
return $this;
}
public function removeTimeSpent(TimeSpent $timeSpent): self
{
if ($this->timeSpents->removeElement($timeSpent)) {
// set the owning side to null (unless already changed)
if ($timeSpent->getTask() === $this) {
$timeSpent->setTask(null);
}
}
return $this;
}
public function getTimeSpentHours($date)
{
foreach($this->timeSpents as $timeSpent){
if($timeSpent->getDate()->format('Y-m-d') == $date) return $timeSpent->getHours();
}
return 0;
}
public function getWeeklyTotalHours(){
$total = 0;
foreach($this->timeSpents as $timeSpent){
$total += $timeSpent->getHours();
}
return $total;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(?\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
}