<?php
namespace App\Entity;
use App\Entity\Traits\TimeStampableTrait;
use App\Repository\ActivityRepository;
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;
use Symfony\Component\Validator\Constraints as Assert;
use App\Validator as CustomAssert;
#[ORM\Entity(repositoryClass: ActivityRepository::class)]
#[CustomAssert\StartDateActivity]
#[CustomAssert\DateRange]
class Activity
{
use TimeStampableTrait;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[Groups(['objectif:read', 'activity:read', 'activiteBailleurs', 'entity:write'])]
private ?int $id = null;
#[ORM\Column(length: 255)]
#[Groups(['objectif:read', 'activity:read', 'activiteBailleurs', 'entity:write'])]
private ?string $code = null;
#[ORM\ManyToOne(inversedBy: 'activities')]
#[Groups(['entity:write'])]
private ?Objectif $objectif = null;
#[ORM\ManyToOne(inversedBy: 'activities')]
#[Groups(['objectif:read', 'activity:read', 'entity:write'])]
#[Assert\NotBlank(message: "Veuillez remplir ce champ")]
private ?Unite $unite = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
#[Groups(['objectif:read', 'activity:read', 'activiteBailleurs', 'entity:write'])]
#[Assert\NotBlank(message: "Veuillez remplir ce champ")]
private ?string $description = null;
// #[ORM\ManyToOne(inversedBy: 'activities')]
// #[Groups(['objectif:read', 'activity:read', 'entity:write'])]
// #[Assert\NotBlank(message: "Veuillez remplir ce champ")]
// private ?Priority $priority = null;
#[Assert\NotBlank(message: "Veuillez remplir ce champ")]
#[ORM\Column(length: 255)]
#[Groups(['objectif:read', 'activity:read', 'entity:write'])]
private ?int $status = null;
#[ORM\Column(type: Types::DATE_MUTABLE, nullable: true)]
#[Groups(['objectif:read', 'activity:read', 'entity:write'])]
#[Assert\NotBlank(message: "Veuillez remplir ce champ")]
private ?\DateTimeInterface $StartDateEstimate = null;
#[ORM\Column(type: Types::DATE_MUTABLE, nullable: true)]
#[Groups(['objectif:read', 'activity:read', 'entity:write'])]
#[Assert\NotBlank(message: "Veuillez remplir ce champ")]
private ?\DateTimeInterface $endDateEstimate = null;
#[ORM\Column(type: Types::DATE_MUTABLE, nullable: true)]
#[Groups(['objectif:read', 'activity:read', 'entity:write'])]
#[Assert\NotBlank(message: "Veuillez remplir ce champ")]
private ?\DateTimeInterface $startDate = null;
#[ORM\Column(type: Types::DATE_MUTABLE, nullable: true)]
#[Groups(['objectif:read', 'activity:read', 'entity:write',])]
#[Assert\NotBlank(message: "Veuillez remplir ce champ")]
private ?\DateTimeInterface $endDate = null;
#[ORM\Column(nullable: true)]
#[Groups(['objectif:read', 'activity:read', 'entity:write'])]
#[Assert\NotBlank(message: "Veuillez remplir ce champ")]
private ?int $quantity = null;
#[ORM\OneToMany(mappedBy: 'activity', targetEntity: ActiviteBailleur::class, cascade: ['remove'])]
#[Groups(['objectif:read', 'activity:read'])]
private Collection $activiteBailleurs;
#[ORM\OneToMany(mappedBy: 'activity', targetEntity: RateOfProgress::class, cascade: ['persist', 'remove'])]
#[Groups(['objectif:read', 'activity:read', 'activity:read'])]
private Collection $rateOfProgress;
#[ORM\Column]
private ?bool $isValided = null;
#[ORM\OneToMany(mappedBy: 'activity', targetEntity: Comment::class, cascade: ['persist', 'remove'])]
private Collection $comments;
#[ORM\ManyToOne(inversedBy: 'activities')]
private ?User $createdBy = null;
#[ORM\Column]
#[Groups(['objectif:read', 'activity:read', 'entity:write'])]
private ?int $reference = null;
public function __construct()
{
$this->createdAt = new \DateTimeImmutable();
$this->status = 1;
$this->quantity = 0;
$this->activiteBailleurs = new ArrayCollection();
$this->rateOfProgress = new ArrayCollection();
$this->isValided = false;
$this->comments = new ArrayCollection();
$this->reference = 0;
}
public function getId(): ?int
{
return $this->id;
}
public function getCode(): ?string
{
return $this->code;
}
public function setCode(?string $code): static
{
$this->code = $code;
return $this;
}
public function getObjectif(): ?Objectif
{
return $this->objectif;
}
public function setObjectif(?Objectif $objectif): static
{
$this->objectif = $objectif;
return $this;
}
public function getUnite(): ?Unite
{
return $this->unite;
}
public function setUnite(?Unite $unite): static
{
$this->unite = $unite;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): static
{
$this->description = $description;
return $this;
}
// public function getPriority(): ?Priority
// {
// return $this->priority;
// }
// public function setPriority(?Priority $priority): static
// {
// $this->priority = $priority;
// return $this;
// }
public function getStatus(): ?int
{
return $this->status;
}
public function setStatus(?int $status): static
{
$this->status = $status;
return $this;
}
public function getStartDateEstimate(): ?\DateTimeInterface
{
return $this->StartDateEstimate;
}
public function setStartDateEstimate(?\DateTimeInterface $StartDateEstimate): static
{
$this->StartDateEstimate = $StartDateEstimate;
return $this;
}
public function getEndDateEstimate(): ?\DateTimeInterface
{
return $this->endDateEstimate;
}
public function setEndDateEstimate(?\DateTimeInterface $endDateEstimate): static
{
$this->endDateEstimate = $endDateEstimate;
return $this;
}
public function getStartDate(): ?\DateTimeInterface
{
return $this->startDate;
}
public function setStartDate(?\DateTimeInterface $startDate): static
{
$this->startDate = $startDate;
return $this;
}
public function getEndDate(): ?\DateTimeInterface
{
return $this->endDate;
}
public function setEndDate(?\DateTimeInterface $endDate): static
{
$this->endDate = $endDate;
return $this;
}
public function status(): string
{
if ($this->status == 1) {
return 'En cours';
} elseif ($this->status == 2) {
return 'Terminé';
} elseif ($this->status == 3) {
return 'Terminer';
} else {
return '';
}
}
public function getQuantity(): ?int
{
return $this->quantity;
}
public function setQuantity(?int $quantity): static
{
$this->quantity = $quantity;
return $this;
}
/**
* @return Collection<int, ActiviteBailleur>
*/
public function getActiviteBailleurs(): Collection
{
return $this->activiteBailleurs;
}
public function addActiviteBailleur(ActiviteBailleur $activiteBailleur): static
{
if (!$this->activiteBailleurs->contains($activiteBailleur)) {
$this->activiteBailleurs->add($activiteBailleur);
$activiteBailleur->setActivity($this);
}
return $this;
}
public function removeActiviteBailleur(ActiviteBailleur $activiteBailleur): static
{
if ($this->activiteBailleurs->removeElement($activiteBailleur)) {
// set the owning side to null (unless already changed)
if ($activiteBailleur->getActivity() === $this) {
$activiteBailleur->setActivity(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->setActivity($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->getActivity() === $this) {
$rateOfProgress->setActivity(null);
}
}
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;
// }
public function isIsValided(): ?bool
{
return $this->isValided;
}
public function setIsValided(bool $isValided): static
{
$this->isValided = $isValided;
return $this;
}
#[Groups(['activity:read', 'objectif: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;
}
/**
* @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->setActivity($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->getActivity() === $this) {
$comment->setActivity(null);
}
}
return $this;
}
#[Groups(['activity:read', 'objectif:read'])]
public function getLastTaux()
{
$lastValidedRate = $this->getLastRateValided();
return $this->getLastRateValided() ? $this->getLastRateValided()->getValeur() : 0;
}
public function getLastRateValided()
{
$validedRates = array_filter($this->rateOfProgress->toArray(), function ($rateOf) {
return $rateOf->isIsValided();
});
return end($validedRates) ?: null;
}
public function pao(){
return $this->objectif->getPao();
}
public function getCreatedBy(): ?User
{
return $this->createdBy;
}
public function setCreatedBy(?User $createdBy): static
{
$this->createdBy = $createdBy;
return $this;
}
public function getReference(): ?int
{
return $this->reference;
}
public function setReference(int $reference): static
{
$this->reference = $reference;
return $this;
}
public function getCommentaireRateOf(RateOfProgress $rateOfProgress) {
return $rateOfProgress?->getComment();
}
}