src/Entity/Order.php line 17

  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Metadata\ApiResource;
  4. use App\Repository\OrderRepository;
  5. use App\Traits\Timestamps;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\DBAL\Types\Types;
  9. use Doctrine\ORM\Mapping as ORM;
  10. #[ORM\Entity(repositoryClassOrderRepository::class)]
  11. #[ORM\Table(name'`order`')]
  12. #[ApiResource()]
  13. #[ORM\HasLifecycleCallbacks]
  14. class Order
  15. {
  16.     use Timestamps;
  17.     
  18.     /**
  19.      * Statuts possibles pour une commande
  20.      */
  21.     public const STATUS_PENDING 'pending';
  22.     public const STATUS_CONFIRMED 'confirmed';
  23.     public const STATUS_CANCELLED 'cancelled';
  24.     public const STATUS_UNPAID 'unpaid';
  25.     
  26.     /**
  27.      * Liste des statuts disponibles
  28.      */
  29.     public const STATUSES = [
  30.         'En attente' => self::STATUS_PENDING,
  31.         'Confirmé' => self::STATUS_CONFIRMED,
  32.         'Annulé' => self::STATUS_CANCELLED,
  33.         'Impayé' => self::STATUS_UNPAID
  34.     ];
  35.     #[ORM\Id]
  36.     #[ORM\GeneratedValue]
  37.     #[ORM\Column]
  38.     private ?int $id null;
  39.     #[ORM\ManyToOne(fetch'EAGER'inversedBy'orders')]
  40.     #[ORM\JoinColumn(nullablefalse)]
  41.     private ?Client $client null;
  42.     #[ORM\ManyToOne(fetch'EAGER')]
  43.     #[ORM\JoinColumn(nullablefalse)]
  44.     private ?Plan $plan null;
  45.     #[ORM\Column(typeTypes::DECIMALprecision10scale2)]
  46.     private ?string $price null;
  47.     #[ORM\Column(typeTypes::DATE_MUTABLE)]
  48.     private ?\DateTimeInterface $startDate null;
  49.     #[ORM\Column(typeTypes::DATE_MUTABLE)]
  50.     private ?\DateTimeInterface $endDate null;
  51.     #[ORM\Column(typeTypes::BOOLEANoptions: ['default' => false])]
  52.     private bool $autoRenew false;
  53.     #[ORM\ManyToMany(targetEntityPlan::class)]
  54.     #[ORM\JoinTable(name'order_extra_services')]
  55.     private Collection $extraServices;
  56.     
  57.     #[ORM\Column(length20)]
  58.     private ?string $status self::STATUS_PENDING;
  59.     #[ORM\OneToMany(mappedBy'order'targetEntityPayment::class, cascade: ['persist''remove'])]
  60.     private Collection $payments;
  61.     /**
  62.      * Référence de la commande - par INKY
  63.      */
  64.     #[ORM\Column(length255nullabletrue)]
  65.     private ?string $orderReference null;
  66.     #[ORM\Column(typeTypes::BOOLEANoptions: ['default' => false])]
  67.     private bool $annualPayment false;
  68.     public function getId(): ?int
  69.     {
  70.         return $this->id;
  71.     }
  72.     public function getClient(): ?Client
  73.     {
  74.         return $this->client;
  75.     }
  76.     public function setClient(?Client $client): static
  77.     {
  78.         $this->client $client;
  79.         return $this;
  80.     }
  81.     public function getPlan(): ?Plan
  82.     {
  83.         return $this->plan;
  84.     }
  85.     public function setPlan(?Plan $plan): static
  86.     {
  87.         $this->plan $plan;
  88.         $this->setPrice($plan->getPrice());
  89.         return $this;
  90.     }
  91.     public function getPrice(): ?string
  92.     {
  93.         return $this->price;
  94.     }
  95.     public function setPrice(string $price): static
  96.     {
  97.         $this->price $price;
  98.         return $this;
  99.     }
  100.     public function getStartDate(): ?\DateTimeInterface
  101.     {
  102.         return $this->startDate;
  103.     }
  104.     public function setStartDate(\DateTimeInterface $startDate): static
  105.     {
  106.         $this->startDate $startDate;
  107.         return $this;
  108.     }
  109.     public function getEndDate(): ?\DateTimeInterface
  110.     {
  111.         return $this->endDate;
  112.     }
  113.     public function setEndDate(\DateTimeInterface $endDate): static
  114.     {
  115.         $this->endDate $endDate;
  116.         return $this;
  117.     }
  118.     public function isAutoRenew(): bool
  119.     {
  120.         return $this->autoRenew;
  121.     }
  122.     public function setAutoRenew(bool $autoRenew): static
  123.     {
  124.         $this->autoRenew $autoRenew;
  125.         return $this;
  126.     }
  127.     public function __construct()
  128.     {
  129.         $this->extraServices = new ArrayCollection();
  130.         $this->payments = new ArrayCollection();
  131.     }
  132.     /**
  133.      * @return Collection<int, Plan>
  134.      */
  135.     public function getExtraServices(): Collection
  136.     {
  137.         return $this->extraServices;
  138.     }
  139.     public function addExtraService(Plan $extraService): static
  140.     {
  141.         if (!$this->extraServices->contains($extraService)) {
  142.             $this->extraServices->add($extraService);
  143.         }
  144.         return $this;
  145.     }
  146.     public function removeExtraService(Plan $extraService): static
  147.     {
  148.         $this->extraServices->removeElement($extraService);
  149.         return $this;
  150.     }
  151.     
  152.     public function getStatus(): ?string
  153.     {
  154.         return $this->status;
  155.     }
  156.     public function setStatus(string $status): static
  157.     {
  158.         if (!in_array($status, [self::STATUS_PENDINGself::STATUS_CONFIRMEDself::STATUS_CANCELLEDself::STATUS_UNPAID])) {
  159.             throw new \InvalidArgumentException(sprintf(
  160.                 'Statut invalide "%s". Les statuts autorisés sont : %s',
  161.                 $status,
  162.                 implode(', ', [self::STATUS_PENDINGself::STATUS_CONFIRMEDself::STATUS_CANCELLEDself::STATUS_UNPAID])
  163.             ));
  164.         }
  165.         
  166.         $this->status $status;
  167.         return $this;
  168.     }
  169.     
  170.     /**
  171.      * @return bool
  172.      */
  173.     public function isPending(): bool
  174.     {
  175.         return $this->status === self::STATUS_PENDING;
  176.     }
  177.     
  178.     /**
  179.      * @return bool
  180.      */
  181.     public function isConfirmed(): bool
  182.     {
  183.         return $this->status === self::STATUS_CONFIRMED;
  184.     }
  185.     
  186.     /**
  187.      * @return bool
  188.      */
  189.     public function isCancelled(): bool
  190.     {
  191.         return $this->status === self::STATUS_CANCELLED;
  192.     }
  193.     
  194.     /**
  195.      * @return bool
  196.      */
  197.     public function isUnpaid(): bool
  198.     {
  199.         return $this->status === self::STATUS_UNPAID;
  200.     }
  201.     /**
  202.      * @return Collection<int, Payment>
  203.      */
  204.     public function getPayments(): Collection
  205.     {
  206.         return $this->payments;
  207.     }
  208.     public function addPayment(Payment $payment): static
  209.     {
  210.         if (!$this->payments->contains($payment)) {
  211.             $this->payments->add($payment);
  212.             $payment->setOrder($this);
  213.         }
  214.         return $this;
  215.     }
  216.     public function removePayment(Payment $payment): static
  217.     {
  218.         if ($this->payments->removeElement($payment)) {
  219.             // Set the owning side to null (unless already changed)
  220.             if ($payment->getOrder() === $this) {
  221.                 $payment->setOrder(null);
  222.             }
  223.         }
  224.         return $this;
  225.     }
  226.     /**
  227.      * Récupère le dernier paiement (le plus récent)
  228.      */
  229.     public function getLatestPayment(): ?Payment
  230.     {
  231.         if ($this->payments->isEmpty()) {
  232.             return null;
  233.         }
  234.         $payments $this->payments->toArray();
  235.         usort($payments, function(Payment $aPayment $b) {
  236.             return $b->getCreatedAt() <=> $a->getCreatedAt();
  237.         });
  238.         return $payments[0];
  239.     }
  240.     /**
  241.      * Récupère le premier paiement confirmé
  242.      */
  243.     public function getConfirmedPayment(): ?Payment
  244.     {
  245.         foreach ($this->payments as $payment) {
  246.             if ($payment->isConfirmed()) {
  247.                 return $payment;
  248.             }
  249.         }
  250.         return null;
  251.     }
  252.     /**
  253.      * Méthode de compatibilité pour récupérer l'ID de paiement Konnect du dernier paiement
  254.      */
  255.     public function getPaymentId(): ?string
  256.     {
  257.         $latestPayment $this->getLatestPayment();
  258.         return $latestPayment?->getKonnectPaymentId();
  259.     }
  260.     /**
  261.      * Méthode de compatibilité pour créer un nouveau paiement avec l'ID Konnect
  262.      */
  263.     public function setPaymentId(?string $konnectPaymentId): static
  264.     {
  265.         if ($konnectPaymentId === null) {
  266.             return $this;
  267.         }
  268.         $payment = new Payment();
  269.         $payment->setKonnectPaymentId($konnectPaymentId);
  270.         $payment->setAmount($this->getPrice());
  271.         $payment->setStatus(Payment::STATUS_PENDING);
  272.         
  273.         $this->addPayment($payment);
  274.         return $this;
  275.     }
  276.     /**
  277.      * Référence de la commande - par INKY
  278.      */
  279.     public function getOrderReference(): ?string
  280.     {
  281.         return $this->orderReference;
  282.     }
  283.     /**
  284.      * Référence de la commande - par INKY
  285.      */
  286.     public function setOrderReference(?string $orderReference): static
  287.     {
  288.         $this->orderReference $orderReference;
  289.         return $this;
  290.     }
  291.     public function isAnnualPayment(): bool
  292.     {
  293.         return $this->annualPayment;
  294.     }
  295.     public function setAnnualPayment(bool $annualPayment): static
  296.     {
  297.         $this->annualPayment $annualPayment;
  298.         return $this;
  299.     }
  300.     /**
  301.      * Génère automatiquement une référence de commande avant la persistance
  302.      */
  303.     #[ORM\PrePersist]
  304.     public function generateOrderReferenceOnPrePersist(): void
  305.     {
  306.         if ($this->orderReference === null) {
  307.             $date = new \DateTime();
  308.             $timestamp $date->format('YmdHis');
  309.             $this->orderReference "ORD-$timestamp";
  310.         }
  311.     }
  312. }