src/Entity/Activity.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Traits\TimeStampableTrait;
  4. use App\Repository\ActivityRepository;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\DBAL\Types\Types;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Symfony\Component\Serializer\Annotation\Groups;
  10. use Symfony\Component\Validator\Constraints as Assert;
  11. use App\Validator as CustomAssert;
  12. #[ORM\Entity(repositoryClass: ActivityRepository::class)]
  13. #[CustomAssert\StartDateActivity]
  14. #[CustomAssert\DateRange]
  15. class Activity
  16. {
  17.     use TimeStampableTrait;
  18.     #[ORM\Id]
  19.     #[ORM\GeneratedValue]
  20.     #[ORM\Column]
  21.     #[Groups(['objectif:read', 'activity:read', 'activiteBailleurs', 'entity:write'])]
  22.     private ?int $id = null;
  23.     #[ORM\Column(length: 255)]
  24.     #[Groups(['objectif:read', 'activity:read', 'activiteBailleurs', 'entity:write'])]
  25.     private ?string $code = null;
  26.     #[ORM\ManyToOne(inversedBy: 'activities')]
  27.     #[Groups(['entity:write'])]
  28.     private ?Objectif $objectif = null;
  29.     #[ORM\ManyToOne(inversedBy: 'activities')]
  30.     #[Groups(['objectif:read', 'activity:read', 'entity:write'])]
  31.     #[Assert\NotBlank(message: "Veuillez remplir ce champ")]
  32.     private ?Unite $unite = null;
  33.     #[ORM\Column(type: Types::TEXT, nullable: true)]
  34.     #[Groups(['objectif:read', 'activity:read', 'activiteBailleurs', 'entity:write'])]
  35.     #[Assert\NotBlank(message: "Veuillez remplir ce champ")]
  36.     private ?string $description = null;
  37.     // #[ORM\ManyToOne(inversedBy: 'activities')]
  38.     // #[Groups(['objectif:read', 'activity:read', 'entity:write'])]
  39.     // #[Assert\NotBlank(message: "Veuillez remplir ce champ")]
  40.     // private ?Priority $priority = null;
  41.     #[Assert\NotBlank(message: "Veuillez remplir ce champ")]
  42.     #[ORM\Column(length: 255)]
  43.     #[Groups(['objectif:read', 'activity:read', 'entity:write'])]
  44.     private ?int $status = null;
  45.     #[ORM\Column(type: Types::DATE_MUTABLE, nullable: true)]
  46.     #[Groups(['objectif:read', 'activity:read', 'entity:write'])]
  47.     #[Assert\NotBlank(message: "Veuillez remplir ce champ")]
  48.     private ?\DateTimeInterface $StartDateEstimate = null;
  49.     #[ORM\Column(type: Types::DATE_MUTABLE, nullable: true)]
  50.     #[Groups(['objectif:read', 'activity:read', 'entity:write'])]
  51.     #[Assert\NotBlank(message: "Veuillez remplir ce champ")]
  52.     private ?\DateTimeInterface $endDateEstimate = null;
  53.     #[ORM\Column(type: Types::DATE_MUTABLE, nullable: true)]
  54.     #[Groups(['objectif:read', 'activity:read', 'entity:write'])]
  55.     #[Assert\NotBlank(message: "Veuillez remplir ce champ")]
  56.     private ?\DateTimeInterface $startDate = null;
  57.     #[ORM\Column(type: Types::DATE_MUTABLE, nullable: true)]
  58.     #[Groups(['objectif:read', 'activity:read', 'entity:write',])]
  59.     #[Assert\NotBlank(message: "Veuillez remplir ce champ")]
  60.     private ?\DateTimeInterface $endDate = null;
  61.     #[ORM\Column(nullable: true)]
  62.     #[Groups(['objectif:read', 'activity:read', 'entity:write'])]
  63.     #[Assert\NotBlank(message: "Veuillez remplir ce champ")]
  64.     private ?int $quantity = null;
  65.     #[ORM\OneToMany(mappedBy: 'activity', targetEntity: ActiviteBailleur::class, cascade: ['remove'])]
  66.     #[Groups(['objectif:read', 'activity:read'])]
  67.     private Collection $activiteBailleurs;
  68.     #[ORM\OneToMany(mappedBy: 'activity', targetEntity: RateOfProgress::class,  cascade: ['persist', 'remove'])]
  69.     #[Groups(['objectif:read', 'activity:read', 'activity:read'])]
  70.     private Collection $rateOfProgress;
  71.     #[ORM\Column]
  72.     private ?bool $isValided = null;
  73.     #[ORM\OneToMany(mappedBy: 'activity', targetEntity: Comment::class,  cascade: ['persist', 'remove'])]
  74.     private Collection $comments;
  75.     #[ORM\ManyToOne(inversedBy: 'activities')]
  76.     private ?User $createdBy = null;
  77.     #[ORM\Column]
  78.     #[Groups(['objectif:read', 'activity:read', 'entity:write'])]
  79.     private ?int $reference = null;
  80.     public function __construct()
  81.     {
  82.         $this->createdAt = new \DateTimeImmutable();
  83.         $this->status = 1;
  84.         $this->quantity = 0;
  85.         $this->activiteBailleurs = new ArrayCollection();
  86.         $this->rateOfProgress = new ArrayCollection();
  87.         $this->isValided = false;
  88.         $this->comments = new ArrayCollection();
  89.         $this->reference = 0;
  90.     }
  91.     public function getId(): ?int
  92.     {
  93.         return $this->id;
  94.     }
  95.     public function getCode(): ?string
  96.     {
  97.         return $this->code;
  98.     }
  99.     public function setCode(?string $code): static
  100.     {
  101.         $this->code = $code;
  102.         return $this;
  103.     }
  104.     public function getObjectif(): ?Objectif
  105.     {
  106.         return $this->objectif;
  107.     }
  108.     public function setObjectif(?Objectif $objectif): static
  109.     {
  110.         $this->objectif = $objectif;
  111.         return $this;
  112.     }
  113.     public function getUnite(): ?Unite
  114.     {
  115.         return $this->unite;
  116.     }
  117.     public function setUnite(?Unite $unite): static
  118.     {
  119.         $this->unite = $unite;
  120.         return $this;
  121.     }
  122.     public function getDescription(): ?string
  123.     {
  124.         return $this->description;
  125.     }
  126.     public function setDescription(?string $description): static
  127.     {
  128.         $this->description = $description;
  129.         return $this;
  130.     }
  131.     // public function getPriority(): ?Priority
  132.     // {
  133.     //     return $this->priority;
  134.     // }
  135.     // public function setPriority(?Priority $priority): static
  136.     // {
  137.     //     $this->priority = $priority;
  138.     //     return $this;
  139.     // }
  140.     public function getStatus(): ?int
  141.     {
  142.         return $this->status;
  143.     }
  144.     public function setStatus(?int $status): static
  145.     {
  146.         $this->status = $status;
  147.         return $this;
  148.     }
  149.     public function getStartDateEstimate(): ?\DateTimeInterface
  150.     {
  151.         return $this->StartDateEstimate;
  152.     }
  153.     public function setStartDateEstimate(?\DateTimeInterface $StartDateEstimate): static
  154.     {
  155.         $this->StartDateEstimate = $StartDateEstimate;
  156.         return $this;
  157.     }
  158.     public function getEndDateEstimate(): ?\DateTimeInterface
  159.     {
  160.         return $this->endDateEstimate;
  161.     }
  162.     public function setEndDateEstimate(?\DateTimeInterface $endDateEstimate): static
  163.     {
  164.         $this->endDateEstimate = $endDateEstimate;
  165.         return $this;
  166.     }
  167.     public function getStartDate(): ?\DateTimeInterface
  168.     {
  169.         return $this->startDate;
  170.     }
  171.     public function setStartDate(?\DateTimeInterface $startDate): static
  172.     {
  173.         $this->startDate = $startDate;
  174.         return $this;
  175.     }
  176.     public function getEndDate(): ?\DateTimeInterface
  177.     {
  178.         return $this->endDate;
  179.     }
  180.     public function setEndDate(?\DateTimeInterface $endDate): static
  181.     {
  182.         $this->endDate = $endDate;
  183.         return $this;
  184.     }
  185.     public function status(): string
  186.     {
  187.         if ($this->status ==  1) {
  188.             return 'En cours';
  189.         } elseif ($this->status == 2) {
  190.             return 'Terminé';
  191.         } elseif ($this->status == 3) {
  192.             return 'Terminer';
  193.         } else {
  194.             return  '';
  195.         }
  196.     }
  197.     public function getQuantity(): ?int
  198.     {
  199.         return $this->quantity;
  200.     }
  201.     public function setQuantity(?int $quantity): static
  202.     {
  203.         $this->quantity = $quantity;
  204.         return $this;
  205.     }
  206.     /**
  207.      * @return Collection<int, ActiviteBailleur>
  208.      */
  209.     public function getActiviteBailleurs(): Collection
  210.     {
  211.         return $this->activiteBailleurs;
  212.     }
  213.     public function addActiviteBailleur(ActiviteBailleur $activiteBailleur): static
  214.     {
  215.         if (!$this->activiteBailleurs->contains($activiteBailleur)) {
  216.             $this->activiteBailleurs->add($activiteBailleur);
  217.             $activiteBailleur->setActivity($this);
  218.         }
  219.         return $this;
  220.     }
  221.     public function removeActiviteBailleur(ActiviteBailleur $activiteBailleur): static
  222.     {
  223.         if ($this->activiteBailleurs->removeElement($activiteBailleur)) {
  224.             // set the owning side to null (unless already changed)
  225.             if ($activiteBailleur->getActivity() === $this) {
  226.                 $activiteBailleur->setActivity(null);
  227.             }
  228.         }
  229.         return $this;
  230.     }
  231.     /**
  232.      * @return Collection<int, RateOfProgress>
  233.      */
  234.     public function getRateOfProgress(): Collection
  235.     {
  236.         return $this->rateOfProgress;
  237.     }
  238.     public function addRateOfProgress(RateOfProgress $rateOfProgress): static
  239.     {
  240.         if (!$this->rateOfProgress->contains($rateOfProgress)) {
  241.             $this->rateOfProgress->add($rateOfProgress);
  242.             $rateOfProgress->setActivity($this);
  243.         }
  244.         return $this;
  245.     }
  246.     public function removeRateOfProgress(RateOfProgress $rateOfProgress): static
  247.     {
  248.         if ($this->rateOfProgress->removeElement($rateOfProgress)) {
  249.             // set the owning side to null (unless already changed)
  250.             if ($rateOfProgress->getActivity() === $this) {
  251.                 $rateOfProgress->setActivity(null);
  252.             }
  253.         }
  254.         return $this;
  255.     }
  256.     // public function getUsersAssign(): array
  257.     // {
  258.     //     $userAssing = [];
  259.     //     foreach ($this->taskAssigns as $assig) {
  260.     //         $userAssing[] = $assig->getUser();
  261.     //     }
  262.     //     return $userAssing;
  263.     // }
  264.     // public function getUserNotAssign(array $users): array
  265.     // {
  266.     //     $userAssign = $this->getUsersAssign();
  267.     //     $userNotAssing = array_diff($users, $userAssign);
  268.     //     return $userNotAssing;
  269.     // }
  270.     public function isIsValided(): ?bool
  271.     {
  272.         return $this->isValided;
  273.     }
  274.     public function setIsValided(bool $isValided): static
  275.     {
  276.         $this->isValided = $isValided;
  277.         return $this;
  278.     }
  279.     #[Groups(['activity:read', 'objectif:read'])]
  280.     public function getTauxAvancement(): int
  281.     {
  282.         $taux = [];
  283.         $countAvaluation = count($this->getRateOfProgress());
  284.         foreach ($this->rateOfProgress as $rate) {
  285.             if ($rate->isIsValided()) {
  286.                 $taux[] = 100;
  287.             } else {
  288.                 $taux[] = 0;
  289.             }
  290.         }
  291.         $sumTaux = array_sum($taux) > 0 ? array_sum($taux) / $countAvaluation : 0;
  292.         return $sumTaux;
  293.     }
  294.     /**
  295.      * @return Collection<int, Comment>
  296.      */
  297.     public function getComments(): Collection
  298.     {
  299.         return $this->comments;
  300.     }
  301.     public function addComment(Comment $comment): static
  302.     {
  303.         if (!$this->comments->contains($comment)) {
  304.             $this->comments->add($comment);
  305.             $comment->setActivity($this);
  306.         }
  307.         return $this;
  308.     }
  309.     public function removeComment(Comment $comment): static
  310.     {
  311.         if ($this->comments->removeElement($comment)) {
  312.             // set the owning side to null (unless already changed)
  313.             if ($comment->getActivity() === $this) {
  314.                 $comment->setActivity(null);
  315.             }
  316.         }
  317.         return $this;
  318.     }
  319.     #[Groups(['activity:read', 'objectif:read'])]
  320.     public function getLastTaux()
  321.     {
  322.       
  323.         $lastValidedRate = $this->getLastRateValided();
  324.         return $this->getLastRateValided() ? $this->getLastRateValided()->getValeur() : 0;
  325.     }
  326.     public function getLastRateValided()
  327.     {
  328.         $validedRates = array_filter($this->rateOfProgress->toArray(), function ($rateOf) {
  329.             return $rateOf->isIsValided();
  330.         });
  331.         return end($validedRates) ?: null;
  332.     }
  333.     public function pao(){
  334.         return $this->objectif->getPao();
  335.     }
  336.     public function getCreatedBy(): ?User
  337.     {
  338.         return $this->createdBy;
  339.     }
  340.     public function setCreatedBy(?User $createdBy): static
  341.     {
  342.         $this->createdBy = $createdBy;
  343.         return $this;
  344.     }
  345.     public function getReference(): ?int
  346.     {
  347.         return $this->reference;
  348.     }
  349.     public function setReference(int $reference): static
  350.     {
  351.         $this->reference = $reference;
  352.         return $this;
  353.     }
  354.     public function getCommentaireRateOf(RateOfProgress $rateOfProgress) {
  355.         return $rateOfProgress?->getComment();
  356.     }
  357. }