src/Entity/Order.php line 17
<?phpnamespace App\Entity;use ApiPlatform\Metadata\ApiResource;use App\Repository\OrderRepository;use App\Traits\Timestamps;use Doctrine\Common\Collections\ArrayCollection;use Doctrine\Common\Collections\Collection;use Doctrine\DBAL\Types\Types;use Doctrine\ORM\Mapping as ORM;#[ORM\Entity(repositoryClass: OrderRepository::class)]#[ORM\Table(name: '`order`')]#[ApiResource()]#[ORM\HasLifecycleCallbacks]class Order{use Timestamps;/*** Statuts possibles pour une commande*/public const STATUS_PENDING = 'pending';public const STATUS_CONFIRMED = 'confirmed';public const STATUS_CANCELLED = 'cancelled';public const STATUS_UNPAID = 'unpaid';/*** Liste des statuts disponibles*/public const STATUSES = ['En attente' => self::STATUS_PENDING,'Confirmé' => self::STATUS_CONFIRMED,'Annulé' => self::STATUS_CANCELLED,'Impayé' => self::STATUS_UNPAID];#[ORM\Id]#[ORM\GeneratedValue]#[ORM\Column]private ?int $id = null;#[ORM\ManyToOne(fetch: 'EAGER', inversedBy: 'orders')]#[ORM\JoinColumn(nullable: false)]private ?Client $client = null;#[ORM\ManyToOne(fetch: 'EAGER')]#[ORM\JoinColumn(nullable: false)]private ?Plan $plan = null;#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2)]private ?string $price = null;#[ORM\Column(type: Types::DATE_MUTABLE)]private ?\DateTimeInterface $startDate = null;#[ORM\Column(type: Types::DATE_MUTABLE)]private ?\DateTimeInterface $endDate = null;#[ORM\Column(type: Types::BOOLEAN, options: ['default' => false])]private bool $autoRenew = false;#[ORM\ManyToMany(targetEntity: Plan::class)]#[ORM\JoinTable(name: 'order_extra_services')]private Collection $extraServices;#[ORM\Column(length: 20)]private ?string $status = self::STATUS_PENDING;#[ORM\OneToMany(mappedBy: 'order', targetEntity: Payment::class, cascade: ['persist', 'remove'])]private Collection $payments;/*** Référence de la commande - par INKY*/#[ORM\Column(length: 255, nullable: true)]private ?string $orderReference = null;#[ORM\Column(type: Types::BOOLEAN, options: ['default' => false])]private bool $annualPayment = false;public function getId(): ?int{return $this->id;}public function getClient(): ?Client{return $this->client;}public function setClient(?Client $client): static{$this->client = $client;return $this;}public function getPlan(): ?Plan{return $this->plan;}public function setPlan(?Plan $plan): static{$this->plan = $plan;$this->setPrice($plan->getPrice());return $this;}public function getPrice(): ?string{return $this->price;}public function setPrice(string $price): static{$this->price = $price;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 isAutoRenew(): bool{return $this->autoRenew;}public function setAutoRenew(bool $autoRenew): static{$this->autoRenew = $autoRenew;return $this;}public function __construct(){$this->extraServices = new ArrayCollection();$this->payments = new ArrayCollection();}/*** @return Collection<int, Plan>*/public function getExtraServices(): Collection{return $this->extraServices;}public function addExtraService(Plan $extraService): static{if (!$this->extraServices->contains($extraService)) {$this->extraServices->add($extraService);}return $this;}public function removeExtraService(Plan $extraService): static{$this->extraServices->removeElement($extraService);return $this;}public function getStatus(): ?string{return $this->status;}public function setStatus(string $status): static{if (!in_array($status, [self::STATUS_PENDING, self::STATUS_CONFIRMED, self::STATUS_CANCELLED, self::STATUS_UNPAID])) {throw new \InvalidArgumentException(sprintf('Statut invalide "%s". Les statuts autorisés sont : %s',$status,implode(', ', [self::STATUS_PENDING, self::STATUS_CONFIRMED, self::STATUS_CANCELLED, self::STATUS_UNPAID])));}$this->status = $status;return $this;}/*** @return bool*/public function isPending(): bool{return $this->status === self::STATUS_PENDING;}/*** @return bool*/public function isConfirmed(): bool{return $this->status === self::STATUS_CONFIRMED;}/*** @return bool*/public function isCancelled(): bool{return $this->status === self::STATUS_CANCELLED;}/*** @return bool*/public function isUnpaid(): bool{return $this->status === self::STATUS_UNPAID;}/*** @return Collection<int, Payment>*/public function getPayments(): Collection{return $this->payments;}public function addPayment(Payment $payment): static{if (!$this->payments->contains($payment)) {$this->payments->add($payment);$payment->setOrder($this);}return $this;}public function removePayment(Payment $payment): static{if ($this->payments->removeElement($payment)) {// Set the owning side to null (unless already changed)if ($payment->getOrder() === $this) {$payment->setOrder(null);}}return $this;}/*** Récupère le dernier paiement (le plus récent)*/public function getLatestPayment(): ?Payment{if ($this->payments->isEmpty()) {return null;}$payments = $this->payments->toArray();usort($payments, function(Payment $a, Payment $b) {return $b->getCreatedAt() <=> $a->getCreatedAt();});return $payments[0];}/*** Récupère le premier paiement confirmé*/public function getConfirmedPayment(): ?Payment{foreach ($this->payments as $payment) {if ($payment->isConfirmed()) {return $payment;}}return null;}/*** Méthode de compatibilité pour récupérer l'ID de paiement Konnect du dernier paiement*/public function getPaymentId(): ?string{$latestPayment = $this->getLatestPayment();return $latestPayment?->getKonnectPaymentId();}/*** Méthode de compatibilité pour créer un nouveau paiement avec l'ID Konnect*/public function setPaymentId(?string $konnectPaymentId): static{if ($konnectPaymentId === null) {return $this;}$payment = new Payment();$payment->setKonnectPaymentId($konnectPaymentId);$payment->setAmount($this->getPrice());$payment->setStatus(Payment::STATUS_PENDING);$this->addPayment($payment);return $this;}/*** Référence de la commande - par INKY*/public function getOrderReference(): ?string{return $this->orderReference;}/*** Référence de la commande - par INKY*/public function setOrderReference(?string $orderReference): static{$this->orderReference = $orderReference;return $this;}public function isAnnualPayment(): bool{return $this->annualPayment;}public function setAnnualPayment(bool $annualPayment): static{$this->annualPayment = $annualPayment;return $this;}/*** Génère automatiquement une référence de commande avant la persistance*/#[ORM\PrePersist]public function generateOrderReferenceOnPrePersist(): void{if ($this->orderReference === null) {$date = new \DateTime();$timestamp = $date->format('YmdHis');$this->orderReference = "ORD-$timestamp";}}}