src/Entity/Trajet.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\TrajetRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. /**
  8.  * @ORM\Entity(repositoryClass=TrajetRepository::class)
  9.  * @ORM\HasLifecycleCallbacks()
  10.  */
  11. class Trajet
  12. {
  13.     /**
  14.      * @ORM\Id
  15.      * @ORM\GeneratedValue
  16.      * @ORM\Column(type="integer")
  17.      */
  18.     private $id;
  19.     /**
  20.      * @ORM\ManyToOne(targetEntity=User::class, inversedBy="trajets")
  21.      * @ORM\JoinColumn(nullable=false, onDelete="RESTRICT")
  22.      */
  23.     private $conducteur;
  24.     /**
  25.      * @ORM\Column(type="string", length=100)
  26.      */
  27.     private $depart;
  28.     /**
  29.      * @ORM\Column(type="string", length=100)
  30.      */
  31.     private $arrivee;
  32.     /**
  33.      * @ORM\Column(type="date")
  34.      */
  35.     private $dateTrajet;
  36.     /**
  37.      * @ORM\Column(type="time")
  38.      */
  39.     private $heureTrajet;
  40.     /**
  41.      * @ORM\Column(type="integer")
  42.      */
  43.     private $placesDisponibles;
  44.     /**
  45.      * @ORM\Column(type="decimal", precision=10, scale=2)
  46.      */
  47.     private $prix;
  48.     /**
  49.      * @ORM\OneToMany(targetEntity=Reservation::class, mappedBy="trajet")
  50.      */
  51.     private $reservations;
  52.     /**
  53.      * @ORM\OneToMany(targetEntity=Message::class, mappedBy="trajet")
  54.      */
  55.     private $messages;
  56.     /**
  57.      * @ORM\OneToMany(targetEntity=Notes::class, mappedBy="trajet")
  58.      */
  59.     private $notes;
  60.     /**
  61.      * @ORM\Column(type="text", nullable=true)
  62.      */
  63.     private $description;
  64.     /**
  65.      * @ORM\Column(type="boolean", nullable=true)
  66.      */
  67.     private $annule;
  68.     /**
  69.      * @ORM\Column(type="integer")
  70.      *
  71.      * Nombre total de places définies par le conducteur
  72.      */
  73.     private $places;
  74.     /**
  75.      * @ORM\Column(type="datetime")
  76.      */
  77.     private $createdAt;
  78.     /**
  79.      * @ORM\PrePersist
  80.      */
  81.     public function setCreatedAtValue(): void
  82.     {
  83.         $this->createdAt = new \DateTime();
  84.     }
  85.     public function __construct()
  86.     {
  87.         $this->reservations = new ArrayCollection();
  88.         $this->messages = new ArrayCollection();
  89.         $this->notes = new ArrayCollection();
  90.     }
  91.     public function getId(): ?int
  92.     {
  93.         return $this->id;
  94.     }
  95.     public function getConducteur(): ?User
  96.     {
  97.         return $this->conducteur;
  98.     }
  99.     public function setConducteur(?User $conducteur): self
  100.     {
  101.         $this->conducteur $conducteur;
  102.         return $this;
  103.     }
  104.     public function getDepart(): ?string
  105.     {
  106.         return $this->depart;
  107.     }
  108.     public function setDepart(string $depart): self
  109.     {
  110.         $this->depart $depart;
  111.         return $this;
  112.     }
  113.     public function getArrivee(): ?string
  114.     {
  115.         return $this->arrivee;
  116.     }
  117.     public function setArrivee(string $arrivee): self
  118.     {
  119.         $this->arrivee $arrivee;
  120.         return $this;
  121.     }
  122.     public function getDateTrajet(): ?\DateTimeInterface
  123.     {
  124.         return $this->dateTrajet;
  125.     }
  126.     public function setDateTrajet(\DateTimeInterface $dateTrajet): self
  127.     {
  128.         $this->dateTrajet $dateTrajet;
  129.         return $this;
  130.     }
  131.     public function getHeureTrajet(): ?\DateTimeInterface
  132.     {
  133.         return $this->heureTrajet;
  134.     }
  135.     public function setHeureTrajet(\DateTimeInterface $heureTrajet): self
  136.     {
  137.         $this->heureTrajet $heureTrajet;
  138.         return $this;
  139.     }
  140.     public function getPlacesDisponibles(): ?int
  141.     {
  142.         return $this->placesDisponibles;
  143.     }
  144.     public function setPlacesDisponibles(int $placesDisponibles): self
  145.     {
  146.         $this->placesDisponibles $placesDisponibles;
  147.         return $this;
  148.     }
  149.     public function getPrix(): ?string
  150.     {
  151.         return $this->prix;
  152.     }
  153.     public function setPrix(string $prix): self
  154.     {
  155.         $this->prix $prix;
  156.         return $this;
  157.     }
  158.     /**
  159.      * @return Collection<int, Reservation>
  160.      */
  161.     public function getReservations(): Collection
  162.     {
  163.         return $this->reservations;
  164.     }
  165.     public function addReservation(Reservation $reservation): self
  166.     {
  167.         if (!$this->reservations->contains($reservation)) {
  168.             $this->reservations[] = $reservation;
  169.             $reservation->setTrajet($this);
  170.         }
  171.         return $this;
  172.     }
  173.     public function removeReservation(Reservation $reservation): self
  174.     {
  175.         if ($this->reservations->removeElement($reservation)) {
  176.             // set the owning side to null (unless already changed)
  177.             if ($reservation->getTrajet() === $this) {
  178.                 $reservation->setTrajet(null);
  179.             }
  180.         }
  181.         return $this;
  182.     }
  183.     /**
  184.      * @return Collection<int, Message>
  185.      */
  186.     public function getMessages(): Collection
  187.     {
  188.         return $this->messages;
  189.     }
  190.     public function addMessage(Message $message): self
  191.     {
  192.         if (!$this->messages->contains($message)) {
  193.             $this->messages[] = $message;
  194.             $message->setTrajet($this);
  195.         }
  196.         return $this;
  197.     }
  198.     public function removeMessage(Message $message): self
  199.     {
  200.         if ($this->messages->removeElement($message)) {
  201.             // set the owning side to null (unless already changed)
  202.             if ($message->getTrajet() === $this) {
  203.                 $message->setTrajet(null);
  204.             }
  205.         }
  206.         return $this;
  207.     }
  208.     /**
  209.      * @return Collection<int, Notes>
  210.      */
  211.     public function getNotes(): Collection
  212.     {
  213.         return $this->notes;
  214.     }
  215.     public function addNote(Notes $note): self
  216.     {
  217.         if (!$this->notes->contains($note)) {
  218.             $this->notes[] = $note;
  219.             $note->setTrajet($this);
  220.         }
  221.         return $this;
  222.     }
  223.     public function removeNote(Notes $note): self
  224.     {
  225.         if ($this->notes->removeElement($note)) {
  226.             // set the owning side to null (unless already changed)
  227.             if ($note->getTrajet() === $this) {
  228.                 $note->setTrajet(null);
  229.             }
  230.         }
  231.         return $this;
  232.     }
  233.     public function getDescription(): ?string
  234.     {
  235.         return $this->description;
  236.     }
  237.     public function setDescription(?string $description): self
  238.     {
  239.         $this->description $description;
  240.         return $this;
  241.     }
  242.     public function isAnnule(): ?bool
  243.     {
  244.         return $this->annule;
  245.     }
  246.     public function setAnnule(?bool $annule): self
  247.     {
  248.         $this->annule $annule;
  249.         return $this;
  250.     }
  251.     public function getPlaces(): ?int
  252.     {
  253.         return $this->places;
  254.     }
  255.     public function setPlaces(int $places): self
  256.     {
  257.         $this->places $places;
  258.         return $this;
  259.     }
  260.     /**
  261.      * Met à jour dynamiquement le nombre de places disponibles
  262.      * en fonction des réservations actives (en_attente, acceptee, payee).
  263.      * Ce recalcul repart de zéro à chaque appel.
  264.      */
  265.     public function majPlacesDisponibles(): void
  266.     {
  267.         $placesPrises 0;
  268.         foreach ($this->getReservations() as $res) {
  269.             if (in_array($res->getStatut(), ['en_attente''acceptee''payee'])) {
  270.                 $placesPrises += $res->getPlaces();
  271.             }
  272.         }
  273.         $this->setPlacesDisponibles($this->getPlaces() - $placesPrises);
  274.     }
  275.     public function getCreatedAt(): ?\DateTimeInterface
  276.     {
  277.         return $this->createdAt;
  278.     }
  279.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  280.     {
  281.         $this->createdAt $createdAt;
  282.         return $this;
  283.     }
  284.     
  285. }