src/Entity/Privatisation.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\CustomExtra;
  4. use App\Entity\History\Privatisation\PrivatisationHistory;
  5. use App\Entity\Payment\AbstractPayment;
  6. use App\Entity\Payment\CarteCadeauPayment;
  7. use App\Entity\Payment\EmptyPayment;
  8. use App\Repository\PrivatisationRepository;
  9. use Doctrine\Common\Collections\ArrayCollection;
  10. use Doctrine\Common\Collections\Collection;
  11. use Doctrine\ORM\Mapping as ORM;
  12. use App\Enum\PrivatisationStatus;
  13. /**
  14.  * @ORM\Entity(repositoryClass=PrivatisationRepository::class)
  15.  */
  16. class Privatisation implements InterfaceReservation
  17. {
  18.     /**
  19.      * @ORM\Id
  20.      * @ORM\GeneratedValue
  21.      * @ORM\Column(type="integer")
  22.      */
  23.     private $id;
  24.     /**
  25.      * @ORM\Column(type="integer")
  26.      */
  27.     private $nbParticipants;
  28.     /**
  29.      * @ORM\Column(type="datetime_immutable")
  30.      */
  31.     private $date;
  32.     /**
  33.      * @ORM\ManyToOne(targetEntity=Contact::class, inversedBy="privatisations")
  34.      * @ORM\JoinColumn(nullable=false)
  35.      */
  36.     private $contact;
  37.     /**
  38.      * @ORM\Column(type="text", nullable=true)
  39.      */
  40.     private $client_commentary;
  41.     /**
  42.      * @ORM\Column(type="text", nullable=true)
  43.      */
  44.     private $comment;
  45.     /**
  46.      * @ORM\OneToMany(targetEntity=ExtraReservation::class, mappedBy="privatisation", orphanRemoval=true, cascade={"persist"})
  47.      */
  48.     private $extraReservations;
  49.     /**
  50.      * @ORM\OneToMany(targetEntity=CustomExtra::class, mappedBy="privatisation", orphanRemoval=true, cascade={"persist", "remove"})
  51.      */
  52.     private $customExtras;
  53.     /**
  54.      * @ORM\Column(type="string", length=255)
  55.      */
  56.     private $offer;
  57.     /**
  58.      * @ORM\Column(type="boolean")
  59.      */
  60.     private $checkedCGV;
  61.     /**
  62.      * @ORM\OneToMany(targetEntity="App\Entity\History\Privatisation\PrivatisationHistory", mappedBy="privatisation", cascade={"remove"})
  63.      */
  64.     private $privatisationHistories;
  65.     /**
  66.      * @ORM\Column(type="float", nullable=true)
  67.      */
  68.     private $resteAPayer;
  69.     /**
  70.      * @ORM\OneToMany(targetEntity=AbstractPayment::class, mappedBy="privatisation")
  71.      */
  72.     private $abstractPayments;
  73.     /**
  74.      * @ORM\Column(type="string", length=50)
  75.      */
  76.     private string $status;
  77.     public function __construct()
  78.     {
  79.         $this->extraReservations = new ArrayCollection();
  80.         $this->customExtras = new ArrayCollection();
  81.         $this->privatisationHistories = new ArrayCollection();
  82.         $this->abstractPayments = new ArrayCollection();
  83.         // Set default status
  84.         $this->status PrivatisationStatus::PendingValidation->value;
  85.     }
  86.     public function getId(): ?int
  87.     {
  88.         return $this->id;
  89.     }
  90.     public function getNbParticipants(): ?int
  91.     {
  92.         return $this->nbParticipants;
  93.     }
  94.     public function setNbParticipants(int $nbParticipants): self
  95.     {
  96.         $this->nbParticipants $nbParticipants;
  97.         return $this;
  98.     }
  99.     public function getDate(): ?\DateTimeImmutable
  100.     {
  101.         return $this->date;
  102.     }
  103.     public function setDate(\DateTimeImmutable $date): self
  104.     {
  105.         $this->date $date;
  106.         return $this;
  107.     }
  108.     public function getContact(): ?Contact
  109.     {
  110.         return $this->contact;
  111.     }
  112.     public function setContact(?Contact $contact): self
  113.     {
  114.         $this->contact $contact;
  115.         return $this;
  116.     }
  117.     public function getClientCommentary(): ?string
  118.     {
  119.         return $this->client_commentary;
  120.     }
  121.     public function setClientCommentary(?string $client_commentary): self
  122.     {
  123.         $this->client_commentary $client_commentary;
  124.         return $this;
  125.     }
  126.     public function getComment(): ?string
  127.     {
  128.         return $this->comment;
  129.     }
  130.     public function setComment(?string $comment): self
  131.     {
  132.         $this->comment $comment;
  133.         return $this;
  134.     }
  135.     /**
  136.      * @return Collection<int, ExtraReservation>
  137.      */
  138.     public function getExtraReservations(): Collection
  139.     {
  140.         return $this->extraReservations;
  141.     }
  142.     /**
  143.      * @return Collection<int, ExtraReservation>
  144.      */
  145.     public function getActiveExtraReservations(): Collection
  146.     {
  147.         return $this->extraReservations->filter(function(ExtraReservation $extraReservation) {
  148.             return !$extraReservation->isEmpty();
  149.         });
  150.     }
  151.     public function addExtraReservation(ExtraReservation $extraReservation): self
  152.     {
  153.         if (!$this->extraReservations->contains($extraReservation)) {
  154.             $this->extraReservations[] = $extraReservation;
  155.             $extraReservation->setPrivatisation($this);
  156.         }
  157.         return $this;
  158.     }
  159.     public function removeExtraReservation(ExtraReservation $extraReservation): self
  160.     {
  161.         if ($this->extraReservations->removeElement($extraReservation)) {
  162.             // set the owning side to null (unless already changed)
  163.             if ($extraReservation->getPrivatisation() === $this) {
  164.                 $extraReservation->setPrivatisation(null);
  165.             }
  166.         }
  167.         return $this;
  168.     }
  169.     /**
  170.      * @return Collection<int, CustomExtra>
  171.      */
  172.     public function getCustomExtras(): Collection
  173.     {
  174.         return $this->customExtras;
  175.     }
  176.     public function addCustomExtra(CustomExtra $customExtra): self
  177.     {
  178.         if (!$this->customExtras->contains($customExtra)) {
  179.             $this->customExtras[] = $customExtra;
  180.             $customExtra->setPrivatisation($this);
  181.         }
  182.         return $this;
  183.     }
  184.     public function removeCustomExtra(CustomExtra $customExtra): self
  185.     {
  186.         if ($this->customExtras->removeElement($customExtra)) {
  187.             if ($customExtra->getPrivatisation() === $this) {
  188.                 $customExtra->setPrivatisation(null);
  189.             }
  190.         }
  191.         return $this;
  192.     }
  193.     public function getOffer(): ?string
  194.     {
  195.         return $this->offer;
  196.     }
  197.     public function setOffer(string $offer): self
  198.     {
  199.         $this->offer $offer;
  200.         return $this;
  201.     }
  202.     public function isCheckedCGV(): ?bool
  203.     {
  204.         return $this->checkedCGV;
  205.     }
  206.     public function setCheckedCGV(bool $checkedCGV): self
  207.     {
  208.         $this->checkedCGV $checkedCGV;
  209.         return $this;
  210.     }
  211.     /**
  212.      * @return Collection<int, \App\Entity\History\Privatisation\PrivatisationHistory>
  213.      */
  214.     public function getPrivatisationHistories(): Collection
  215.     {
  216.         return $this->privatisationHistories;
  217.     }
  218.     public function addPrivatisationHistory(\App\Entity\History\Privatisation\PrivatisationHistory $privatisationHistory): self
  219.     {
  220.         if (!$this->privatisationHistories->contains($privatisationHistory)) {
  221.             $this->privatisationHistories[] = $privatisationHistory;
  222.             $privatisationHistory->setPrivatisation($this);
  223.         }
  224.         return $this;
  225.     }
  226.     public function removePrivatisationHistory(\App\Entity\History\Privatisation\PrivatisationHistory $privatisationHistory): self
  227.     {
  228.         $this->privatisationHistories->removeElement($privatisationHistory);
  229.         return $this;
  230.     }
  231.     public function getResteAPayer(): ?float
  232.     {
  233.         return $this->resteAPayer ?? 0;
  234.     }
  235.     public function setResteAPayer(?float $resteAPayer): self
  236.     {
  237.         $this->resteAPayer $resteAPayer;
  238.         return $this;
  239.     }
  240.     /**
  241.      * @return Collection<int, AbstractPayment>
  242.      */
  243.     public function getAbstractPayments(): Collection
  244.     {
  245.         return $this->abstractPayments;
  246.     }
  247.     public function isPaidWithCarteCadeau(): bool
  248.     {
  249.         foreach ($this->abstractPayments as $payment) {
  250.             if ($payment instanceof CarteCadeauPayment) {
  251.                 return true;
  252.             }
  253.         }
  254.         return false;
  255.     }
  256.     public function getUsedCarteCadeaux(): Collection
  257.     {
  258.         $carteCadeaux = new ArrayCollection();
  259.         foreach ($this->abstractPayments as $payment) {
  260.             if ($payment instanceof CarteCadeauPayment) {
  261.                 $carteCadeaux->add($payment->getCartecadeau());
  262.             }
  263.         }
  264.         return $carteCadeaux;
  265.     }
  266.     public function addAbstractPayment(AbstractPayment $abstractPayment): self
  267.     {
  268.         if (!$this->abstractPayments->contains($abstractPayment)) {
  269.             $this->abstractPayments[] = $abstractPayment;
  270.             $abstractPayment->setPrivatisation($this);
  271.         }
  272.         return $this;
  273.     }
  274.     public function removeAbstractPayment(AbstractPayment $abstractPayment): self
  275.     {
  276.         if ($this->abstractPayments->removeElement($abstractPayment)) {
  277.             // set the owning side to null (unless already changed)
  278.             if ($abstractPayment->getPrivatisation() === $this) {
  279.                 $abstractPayment->setPrivatisation(null);
  280.             }
  281.         }
  282.         return $this;
  283.     }
  284.     public function getAllHistories(): array
  285.     {
  286.         $histories = new ArrayCollection();
  287.         foreach($this->getPrivatisationHistories() as $history) {
  288.             $histories->add($history);
  289.         }
  290.         foreach($this->getAbstractPayments() as $payment) {
  291.             if (!$payment instanceof EmptyPayment) {
  292.                 $histories->add($payment);
  293.             }
  294.         }
  295.         // sort by createdAt
  296.         $histories $histories->toArray();
  297.         usort($histories, function($a$b) {
  298.             return $b->getCreatedAt() <=> $a->getCreatedAt();
  299.         });
  300.         return $histories;
  301.     }
  302.     /**
  303.      * Get only payment histories from all histories
  304.      * @return array Payment histories sorted by creation date (newest first)
  305.      */
  306.     public function getPaymentHistories(): array
  307.     {
  308.         $paymentHistories = new ArrayCollection();
  309.         
  310.         foreach($this->getAbstractPayments() as $payment) {
  311.             if (!$payment instanceof EmptyPayment) {
  312.                 $paymentHistories->add($payment);
  313.             }
  314.         }
  315.         // sort by createdAt
  316.         $histories $paymentHistories->toArray();
  317.         usort($histories, function($a$b) {
  318.             return $b->getCreatedAt() <=> $a->getCreatedAt();
  319.         });
  320.         return $histories;
  321.     }
  322.     public function getStatus(): PrivatisationStatus
  323.     {
  324.         return PrivatisationStatus::from($this->status);
  325.     }
  326.     public function setStatus(PrivatisationStatus $status): self
  327.     {
  328.         $this->status $status->value;
  329.         return $this;
  330.     }
  331.     /**
  332.      * Shortcut helpers
  333.      */
  334.     public function isConfirmedStatus(): bool { return $this->status === PrivatisationStatus::Confirmed->value; }
  335.     public function isCanceledStatus(): bool  { return $this->status === PrivatisationStatus::Canceled->value; }
  336.     public function isDeletedStatus(): bool   { return $this->status === PrivatisationStatus::Deleted->value; }
  337.     /**
  338.      * Get the creation date from the CreatePrivatisationHistory
  339.      * 
  340.      * @return \DateTime|null The creation date or null if not found
  341.      */
  342.     public function getCreatedAt(): ?\DateTime
  343.     {
  344.         foreach ($this->privatisationHistories as $history) {
  345.             if ($history instanceof \App\Entity\History\Privatisation\CreatePrivatisationHistory) {
  346.                 return $history->getCreatedAt();
  347.             }
  348.         }
  349.         
  350.         return null;
  351.     }
  352.     // Legacy getters/setters for backward compatibility during migration
  353.     public function getCommentary(): ?string
  354.     {
  355.         return $this->client_commentary;
  356.     }
  357.     public function setCommentary(?string $commentary): self
  358.     {
  359.         $this->client_commentary $commentary;
  360.         return $this;
  361.     }
  362.     public function getDuration(): ?string
  363.     {
  364.         return $this->offer;
  365.     }
  366.     public function setDuration(string $duration): self
  367.     {
  368.         $this->offer $duration;
  369.         return $this;
  370.     }
  371.     public function getArrivalDate(): ?\DateTimeInterface
  372.     {
  373.         $date = clone $this->getDate();
  374.         return $date->modify('-15 minutes');
  375.     }
  376.     public function getDepartureDate(): ?\DateTimeInterface
  377.     {
  378.         $date = clone $this->getDate();
  379.         return $this->getOffer() == "120" ?
  380.             $date->modify('+135 minutes') :  // 2h privatisation + 15 min buffer
  381.             $date->modify('+195 minutes');    // 3h privatisation + 15 min buffer
  382.     }
  383.     public function getDateFormated(): string
  384.     {
  385.         // French day and month names
  386.         $days = [
  387.             'Sunday' => 'Dimanche',
  388.             'Monday' => 'Lundi',
  389.             'Tuesday' => 'Mardi',
  390.             'Wednesday' => 'Mercredi',
  391.             'Thursday' => 'Jeudi',
  392.             'Friday' => 'Vendredi',
  393.             'Saturday' => 'Samedi'
  394.         ];
  395.         $months = [
  396.             'January' => 'janvier',
  397.             'February' => 'février',
  398.             'March' => 'mars',
  399.             'April' => 'avril',
  400.             'May' => 'mai',
  401.             'June' => 'juin',
  402.             'July' => 'juillet',
  403.             'August' => 'août',
  404.             'September' => 'septembre',
  405.             'October' => 'octobre',
  406.             'November' => 'novembre',
  407.             'December' => 'décembre'
  408.         ];
  409.         $date $this->getDate();
  410.         // Format components
  411.         $dayName $days[$date->format('l')]; // Full day name
  412.         $day $date->format('d'); // Day of the month
  413.         $monthName $months[$date->format('F')]; // Full month name
  414.         $year $date->format('Y'); // Year
  415.         $time $date->format('H\h') . str_pad($date->format('i'), 2'0'STR_PAD_LEFT);
  416.         // Combine the formatted string
  417.         $formattedDate "$dayName $day $monthName $year à $time";
  418.         return $formattedDate;
  419.     }
  420. }