src/Entity/Purchase.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use DateTime;
  4. use Doctrine\DBAL\Types\Types;
  5. use App\Repository\PurchaseRepository;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Symfony\Component\Validator\Constraints as Assert;
  10. #[ORM\Entity(repositoryClass: PurchaseRepository::class)]
  11. #[ORM\HasLifecycleCallbacks]
  12. class Purchase
  13. {
  14.     public const STATUS_PENDING = 'PENDING';
  15.     public const STATUS_PAID = 'PAID';
  16.     #[ORM\Id]
  17.     #[ORM\GeneratedValue]
  18.     #[ORM\Column]
  19.     private ?int $id = null;
  20.     public function __toString()
  21. {
  22.     return (string) $this->id; // or any other property you want to represent the entity with
  23. }
  24.     #[ORM\Column]
  25.     private ?int $total = null;
  26.     
  27.     //par défaut le statut est pending (en attente)
  28.     #[ORM\Column(length: 255)]
  29.     protected ?string $status = 'PENDING';
  30.     #[ORM\ManyToOne(inversedBy: 'purchases')]
  31.     protected ?User $user = null;
  32.     #[ORM\Column(type: Types::DATE_MUTABLE, nullable: true)]
  33.     private ?\DateTimeInterface $created_at = null;
  34.     #[ORM\OneToMany(mappedBy: 'purchase', targetEntity: PurchaseItem::class, orphanRemoval: true, cascade:["persist"])]
  35.     private Collection $purchaseItems;
  36.     
  37.     public function __construct()
  38.     {
  39.         $this->purchaseItems = new ArrayCollection();
  40.     }
  41.     #[ORM\PrePersist]
  42.     public function prePersist()
  43.     {
  44.         //avant de persister  si pas de date donnée
  45.         //je donne nvlle date
  46.         if (empty($this->created_at)) {
  47.             $this->created_at = new DateTime();
  48.         }
  49.     }
  50.     #[ORM\PreFlush]
  51.     public function preFlush()
  52.     {
  53.      //   dd($this->purchaseItems);
  54.       $total = 0;
  55.        foreach ($this->purchaseItems as $item) {
  56.           $total += $item->getTotal();
  57.        }
  58.        $this->total = $total;
  59.     }  
  60.     public function getId(): ?int
  61.     {
  62.         return $this->id;
  63.     }
  64.   
  65.     public function getTotal(): ?int
  66.     {
  67.         return $this->total;
  68.     }
  69.     public function setTotal(int $total): self
  70.     {
  71.         $this->total = $total;
  72.         return $this;
  73.     }
  74.     public function getStatus(): ?string
  75.     {
  76.         return $this->status;
  77.     }
  78.     public function setStatus(string $status): self
  79.     {
  80.         $this->status = $status;
  81.         return $this;
  82.     }
  83.     public function getUser(): ?User
  84.     {
  85.         return $this->user;
  86.     }
  87.     public function setUser(?User $user): self
  88. {
  89.     $this->user = $user;
  90.     if ($user !== null) {
  91.         $user->addPurchase($this);
  92.     }
  93.     return $this;
  94. }
  95.     /**
  96.      * @return Collection<int, PurchaseItem>
  97.      */
  98.     public function getPurchaseItems(): Collection
  99.     {
  100.         return $this->purchaseItems;
  101.     }
  102.     public function addPurchaseItem(PurchaseItem $purchaseItem): self
  103.     {
  104.         if (!$this->purchaseItems->contains($purchaseItem)) {
  105.             $this->purchaseItems->add($purchaseItem);
  106.             $purchaseItem->setPurchase($this);
  107.         }
  108.         return $this;
  109.     }
  110.     public function removePurchaseItem(PurchaseItem $purchaseItem): self
  111.     {
  112.         if ($this->purchaseItems->removeElement($purchaseItem)) {
  113.             // set the owning side to null (unless already changed)
  114.             if ($purchaseItem->getPurchase() === $this) {
  115.                 $purchaseItem->setPurchase(null);
  116.             }
  117.         }
  118.         return $this;
  119.     }
  120.   
  121.     public function getCreated_at(): ?\DateTimeInterface
  122.     {
  123.         return $this->created_at;
  124.     }
  125.     public function setCreatedAt(\DateTimeInterface $created_at): self
  126.     {
  127.         $this->created_at = $created_at;
  128.         return $this;
  129.     }
  130.   
  131. }