src/Entity/Promotion.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\PromotionRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use JsonSerializable;
  8. use ApiPlatform\Core\Annotation\ApiResource;
  9. use phpDocumentor\Reflection\Types\Boolean;
  10. /**
  11.  * @ORM\Entity(repositoryClass=PromotionRepository::class)
  12.  */
  13. class Promotion implements JsonSerializable
  14. {
  15.     /**
  16.      * @ORM\Id
  17.      * @ORM\GeneratedValue
  18.      * @ORM\Column(type="integer")
  19.      */
  20.     private $id;
  21.     /**
  22.      * @ORM\Column(type="string", length=255)
  23.      */
  24.     private $name;
  25.     /**
  26.      * @ORM\Column(type="text", nullable=true)
  27.      */
  28.     private $description;
  29.     /**
  30.      * @ORM\Column(type="datetime")
  31.      */
  32.     private $startAt;
  33.     /**
  34.      * @ORM\Column(type="datetime")
  35.      */
  36.     private $endAt;
  37.     /**
  38.      * @ORM\Column(type="datetime")
  39.      */
  40.     private $createdAt;
  41.     /**
  42.      * @ORM\Column(type="string", length=255, nullable=true)
  43.      */
  44.     private $discountType;
  45.     /**
  46.      * @ORM\Column(type="float", nullable=true)
  47.      */
  48.     private $discountValue;
  49.     /**
  50.      * @ORM\Column(type="string", length=255, nullable=true)
  51.      */
  52.     private $code;
  53.     /**
  54.      * @ORM\Column(type="integer", nullable=true)
  55.      */
  56.     private $totalQuantity;
  57.     /**
  58.      * @ORM\Column(type="integer", nullable=true)
  59.      */
  60.     private ?int $quantityUser null;
  61.     
  62.     /**
  63.     * @ORM\Column(type="boolean", options={"default":true}) 
  64.     */
  65.     private bool $isEnabled true
  66.     /**
  67.      * @ORM\OneToMany(targetEntity=Produit::class, mappedBy="promotion")
  68.      */
  69.     private $produits;
  70.     /**
  71.      * @ORM\OneToMany(targetEntity=User::class, mappedBy="promotion")
  72.      */
  73.     private $clients;
  74.     /**
  75.      * @ORM\Column(type="string", length=20)
  76.      */
  77.     private $type 'produit';  // "produit" ou "client"
  78.     public function getType(): ?string
  79.     {
  80.         return $this->type;
  81.     }
  82.     public function setType(string $type): self
  83.     {
  84.         $this->type $type;
  85.         return $this;
  86.     }
  87.     public function __construct()
  88.     {
  89.         $this->produits = new ArrayCollection();
  90.         $this->clients = new ArrayCollection();
  91.     }
  92.     public function getId(): ?int
  93.     {
  94.         return $this->id;
  95.     }
  96.     public function getName(): ?string
  97.     {
  98.         return $this->name;
  99.     }
  100.     public function setName(string $name): self
  101.     {
  102.         $this->name $name;
  103.         return $this;
  104.     }
  105.     public function getDescription(): ?string
  106.     {
  107.         return $this->description;
  108.     }
  109.     public function setDescription(?string $description): self
  110.     {
  111.         $this->description $description;
  112.         return $this;
  113.     }
  114.     public function getStartAt(): ?\DateTimeInterface
  115.     {
  116.         return $this->startAt;
  117.     }
  118.     public function setStartAt(\DateTimeInterface $startAt): self
  119.     {
  120.         $this->startAt $startAt;
  121.         return $this;
  122.     }
  123.     public function getEndAt(): ?\DateTimeInterface
  124.     {
  125.         return $this->endAt;
  126.     }
  127.     public function setEndAt(\DateTimeInterface $endAt): self
  128.     {
  129.         $this->endAt $endAt;
  130.         return $this;
  131.     }
  132.     public function getCreatedAt(): ?\DateTimeInterface
  133.     {
  134.         return $this->createdAt;
  135.     }
  136.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  137.     {
  138.         $this->createdAt $createdAt;
  139.         return $this;
  140.     }
  141.     public function getDiscountType(): ?string
  142.     {
  143.         return $this->discountType;
  144.     }
  145.     public function setDiscountType(?string $discountType): self
  146.     {
  147.         $this->discountType $discountType;
  148.         return $this;
  149.     }
  150.     public function getDiscountValue(): ?float
  151.     {
  152.         return $this->discountValue;
  153.     }
  154.     public function setDiscountValue(?float $discountValue): self
  155.     {
  156.         $this->discountValue $discountValue;
  157.         return $this;
  158.     }
  159.     public function getCode(): ?string
  160.     {
  161.         return $this->code;
  162.     }
  163.     public function setCode(?string $code): self
  164.     {
  165.         $this->code $code;
  166.         return $this;
  167.     }
  168.     public function getTotalQuantity(): ?int
  169.     {
  170.         return $this->totalQuantity;
  171.     }
  172.     public function setTotalQuantity(?int $totalQuantity): self
  173.     {
  174.         $this->totalQuantity $totalQuantity;
  175.         return $this;
  176.     }
  177.     public function getQuantityUser(): ?int
  178.     {
  179.         return $this->quantityUser;
  180.     }
  181.     public function setQuantityUser(?int $quantityUser): self
  182.     {
  183.         $this->quantityUser $quantityUser;
  184.         return $this;
  185.     }
  186.     /**
  187.      * @return Collection|Produit[]
  188.      */
  189.     public function getProduits(): Collection
  190.     {
  191.         return $this->produits;
  192.     }
  193.     public function addProduit(Produit $produit): self
  194.     {
  195.         if( !$this->produits->contains($produit)) {
  196.             $this->produits[] = $produit;
  197.             $produit->setPromotion($this);
  198.         }
  199.         return $this;
  200.     }
  201.     public function removeProduit(Produit $produit): self
  202.     {
  203.         if( $this->produits->removeElement($produit)) {
  204.             // set the owning side to null (unless already changed)
  205.             if( $produit->getPromotion() === $this) {
  206.                 $produit->setPromotion(null);
  207.             }
  208.         }
  209.         return $this;
  210.     }
  211.     /**
  212.      * @return Collection|User[]
  213.      */
  214.     public function getClients(): Collection
  215.     {
  216.         return $this->clients;
  217.     }
  218.     public function addClient(User $client): self
  219.     {
  220.         if( !$this->clients->contains($client)) {
  221.             $this->clients[] = $client;
  222.             $client->setPromotion($this);
  223.         }
  224.         return $this;
  225.     }
  226.     public function removeClient(User $client): self
  227.     {
  228.         if( $this->clients->removeElement($client)) {
  229.             // set the owning side to null (unless already changed)
  230.             if( $client->getPromotion() === $this) {
  231.                 $client->setPromotion(null);
  232.             }
  233.         }
  234.         return $this;
  235.     }
  236.     // Fonction pour déterminer la validité de promo en temps
  237.     public function isValid(): ?bool
  238.     {
  239.         // Si la promotion est désactivée, elle n’est jamais "valide"
  240.         //if ($this->isEnabled === false) {
  241.             //return false;
  242.         //}
  243.         
  244.         $dateJour = new \DateTime('now');
  245.         if( $dateJour >= $this->startAt && $dateJour <= $this->endAt) {
  246.             return true;
  247.         }
  248.         return false;
  249.     }
  250.     public function isEnabled(): bool { return $this->isEnabled; }
  251.     public function setIsEnabled(bool $v): self $this->isEnabled $v; return $this; }
  252.     /** Helper runtime (non mappé) */
  253.     public function isActiveNow(\DateTimeInterface $now null): bool {
  254.         $now ??= new \DateTimeImmutable();
  255.         return $this->isEnabled && $this->getStartAt() <= $now && $this->getEndAt() >= $now;
  256.     }
  257.     public function jsonSerialize()
  258.     {
  259.         return array(
  260.             'id' => $this->getId(),
  261.             'start' => $this->getStartAt(),
  262.             'end' => $this->getEndAt(),
  263.             'discountType' => $this->getDiscountType(),
  264.             'discountValue' => $this->getDiscountValue(),
  265.             'code' => $this->getCode(),
  266.             'isValid' => $this->isValid()
  267.         );
  268.     }
  269. }