src/Entity/Objective.php line 13
<?php
namespace App\Entity;
use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use App\Repository\ObjectiveRepository;
use DateTime;
#[ORM\Entity(repositoryClass: ObjectiveRepository::class)]
class Objective
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 1000)]
private ?string $title = null;
#[ORM\Column]
private ?\DateTimeImmutable $createdAt = null;
#[ORM\Column]
private ?\DateTimeImmutable $targetAt = null;
#[ORM\Column(nullable: true)]
private ?\DateTimeImmutable $doneAt = null;
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: true)]
private ?User $author = null;
#[ORM\ManyToOne(inversedBy: 'actions')]
#[ORM\JoinColumn(nullable: false)]
private ?Company $company = null;
#[ORM\Column(length: 500, nullable: true)]
private ?string $resource = null;
#[ORM\Column(length: 500, nullable: true)]
private ?string $kpi = null;
#[ORM\OneToMany(mappedBy: 'objective', targetEntity: Action::class, orphanRemoval: true, fetch: 'EAGER')]
private Collection $actions;
#[ORM\Column(nullable: true)]
private ?bool $paused = null;
#[ORM\Column(nullable: true)]
private ?\DateTimeImmutable $startAt = null;
#[ORM\ManyToOne(inversedBy: 'objectives')]
private ?User $stakeholder = null;
#[ORM\Column(length: 100, nullable: true)]
private ?string $stakeholderText = null;
#[ORM\Column(length: 100, nullable: true)]
private ?string $type = null;
#[ORM\Column(length: 100, nullable: true)]
private ?string $priority = null;
public function __construct()
{
$this->createdAt = new DateTimeImmutable();
$this->actions = new ArrayCollection();
}
public function getStatus()
{
$now = new DateTimeImmutable();
if ($this->paused) {
$value = ["status" => "paused", "date" => $this->targetAt->format('d/m/Y'), "dateNotFormatted" => $this->targetAt, "text" => "En pause, la date cible est le ", "textShort" => "En pause"];
} elseif ($this->startAt > $now) {
$value = ["status" => "notstarted", "date" => $this->targetAt->format('d/m/Y'), "dateNotFormatted" => $this->targetAt, "text" => "Débute le ", "textShort" => "Débute le "];
} elseif ($this->targetAt < $now && !$this->doneAt) {
$value = ["status" => "late", "date" => $this->targetAt->format('d/m/Y'), "dateNotFormatted" => $this->targetAt, "text" => "En retard, la date cible était le ", "textShort" => "En retard depuis le"];
} elseif ($this->doneAt) {
$value = ["status" => "terminated", "date" => $this->doneAt->format('d/m/Y'), "dateNotFormatted" => $this->doneAt, "text" => "Terminé le ", "textShort" => "Terminé le "];
} elseif ($this->targetAt > $now) {
$value = ["status" => "ongoing", "date" => $this->targetAt->format('d/m/Y'), "dateNotFormatted" => $this->targetAt, "text" => "En cours, la date cible est le ", "textShort" => "En cours pour le"];
}
return $value;
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getTargetAt(): ?\DateTimeImmutable
{
return $this->targetAt;
}
public function setTargetAt(\DateTimeImmutable $targetAt): self
{
$this->targetAt = $targetAt;
return $this;
}
public function getDoneAt(): ?\DateTimeImmutable
{
return $this->doneAt;
}
public function setDoneAt(?\DateTimeImmutable $doneAt): self
{
$this->doneAt = $doneAt;
return $this;
}
public function getAuthor(): ?User
{
return $this->author;
}
public function setAuthor(?User $author): self
{
$this->author = $author;
return $this;
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): self
{
$this->company = $company;
return $this;
}
public function getResource(): ?string
{
return $this->resource;
}
public function setResource(?string $resource): self
{
$this->resource = $resource;
return $this;
}
public function getKpi(): ?string
{
return $this->kpi;
}
public function setKpi(?string $kpi): self
{
$this->kpi = $kpi;
return $this;
}
/**
* @return Collection<int, Action>
*/
public function getActions(): Collection
{
return $this->actions;
}
public function addAction(Action $actionDetail): self
{
if (!$this->actions->contains($actionDetail)) {
$this->actions->add($actionDetail);
$actionDetail->setObjective($this);
}
return $this;
}
public function removeAction(Action $actionDetail): self
{
if ($this->actions->removeElement($actionDetail)) {
// set the owning side to null (unless already changed)
if ($actionDetail->getObjective() === $this) {
$actionDetail->setObjective(null);
}
}
return $this;
}
public function getDetailsCompleted()
{
if (count($this->actions) == 0) {
$value = ['percentage' => 0, "percentageColor" => "paused", "validated" => 0, "total" => 0];
} else {
$validated = 0;
$notValidated = 0;
foreach ($this->actions as $key => $detail) {
if ($detail->isStatus()) {
$validated++;
} else {
$notValidated++;
}
}
$percentage = round($validated / count($this->actions) * 100);
// dd($percentage);
if ($percentage == 0) {
$percentageColor = "paused";
} elseif ($percentage < 33) {
$percentageColor = "late";
} elseif ($percentage < 100) {
$percentageColor = "ongoing";
} else {
$percentageColor = "terminated";
}
$value = ['percentage' => $percentage, "percentageColor" => $percentageColor, "validated" => $validated, "total" => count($this->actions)];
}
return $value;
}
public function isPaused(): ?bool
{
return $this->paused;
}
public function setPaused(?bool $paused): self
{
$this->paused = $paused;
return $this;
}
public function getStartAt(): ?\DateTimeImmutable
{
return $this->startAt;
}
public function setStartAt(?\DateTimeImmutable $startAt): self
{
$this->startAt = $startAt;
return $this;
}
public function getStakeholderLabel()
{
if ($this->stakeholder && $this->stakeholderText) {
return $this->stakeholder->getName() . ", " . $this->stakeholderText;
} else if ($this->stakeholder) {
return $this->stakeholder->getName();
} else if ($this->stakeholderText) {
return $this->stakeholderText;
} else {
return "-";
}
}
public function getStakeholder(): ?User
{
return $this->stakeholder;
}
public function setStakeholder(?User $stakeholder): self
{
$this->stakeholder = $stakeholder;
return $this;
}
public function getStakeholderText(): ?string
{
return $this->stakeholderText;
}
public function setStakeholderText(?string $stakeholderText): self
{
$this->stakeholderText = $stakeholderText;
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(string $type): static
{
$this->type = $type;
return $this;
}
public function getTypeLabel()
{
if ($this->type == "label_nr") {
return "Label NR";
} elseif ($this->type == "bilan_carbone") {
return "Bilan Carbone";
} else if ($this->type == "audit_nr") {
return "Audit NR";
} else {
return "Autre";
}
}
public function getPriority(): ?string
{
return $this->priority;
}
public function setPriority(?string $priority): static
{
$this->priority = $priority;
return $this;
}
}