src/Entity/KitPromotionnel.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\KitPromotionnelRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\Validator\Constraints as Assert;
  8. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  9. /**
  10.  * @ORM\Entity(repositoryClass=KitPromotionnelRepository::class)
  11.  * @ORM\HasLifecycleCallbacks()
  12.  */
  13. class KitPromotionnel
  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.      * @Assert\NotBlank
  24.      */
  25.     private $nom;
  26.     /**
  27.      * @ORM\Column(type="text", nullable=true)
  28.      */
  29.     private $description;
  30.     /**
  31.      * @ORM\Column(type="float")
  32.      * @Assert\NotNull
  33.      * @Assert\GreaterThanOrEqual(value=0)
  34.      */
  35.     private $prixKit;
  36.     /**
  37.      * @ORM\Column(type="boolean", options={"default": true})
  38.      */
  39.     private $actif = true;
  40.     /**
  41.      * @ORM\Column(type="string", length=255, nullable=true)
  42.      */
  43.     private $image;
  44.     /**
  45.      * @ORM\Column(type="datetime", nullable=true)
  46.      */
  47.     private $dateDebut;
  48.     /**
  49.      * @ORM\Column(type="datetime", nullable=true)
  50.      */
  51.     private $dateFin;
  52.     /**
  53.      * @ORM\Column(type="datetime")
  54.      */
  55.     private $createdAt;
  56.     /**
  57.      * @ORM\Column(type="datetime")
  58.      */
  59.     private $updatedAt;
  60.     /**
  61.      * @ORM\OneToMany(targetEntity=KitPromotionnelItem::class, mappedBy="kitPromotionnel", cascade={"persist", "remove"}, orphanRemoval=true)
  62.      * @Assert\Valid
  63.      * @Assert\Count(min=1, minMessage="Ajoutez au moins un produit au kit promotionnel.")
  64.      */
  65.     private $items;
  66.     public function __construct()
  67.     {
  68.         $this->items = new ArrayCollection();
  69.     }
  70.     public function getId(): ?int
  71.     {
  72.         return $this->id;
  73.     }
  74.     public function getNom(): ?string
  75.     {
  76.         return $this->nom;
  77.     }
  78.     public function setNom(string $nom): self
  79.     {
  80.         $this->nom = $nom;
  81.         return $this;
  82.     }
  83.     public function getDescription(): ?string
  84.     {
  85.         return $this->description;
  86.     }
  87.     public function setDescription(?string $description): self
  88.     {
  89.         $this->description = $description;
  90.         return $this;
  91.     }
  92.     public function getPrixKit(): ?float
  93.     {
  94.         return $this->prixKit;
  95.     }
  96.     public function setPrixKit(float $prixKit): self
  97.     {
  98.         $this->prixKit = $prixKit;
  99.         return $this;
  100.     }
  101.     public function isActif(): ?bool
  102.     {
  103.         return $this->actif;
  104.     }
  105.     public function setActif(bool $actif): self
  106.     {
  107.         $this->actif = $actif;
  108.         return $this;
  109.     }
  110.     public function getImage(): ?string
  111.     {
  112.         return $this->image;
  113.     }
  114.     public function setImage(?string $image): self
  115.     {
  116.         $this->image = $image;
  117.         return $this;
  118.     }
  119.     public function getDateDebut(): ?\DateTimeInterface
  120.     {
  121.         return $this->dateDebut;
  122.     }
  123.     public function setDateDebut(?\DateTimeInterface $dateDebut): self
  124.     {
  125.         $this->dateDebut = $dateDebut;
  126.         return $this;
  127.     }
  128.     public function getDateFin(): ?\DateTimeInterface
  129.     {
  130.         return $this->dateFin;
  131.     }
  132.     public function setDateFin(?\DateTimeInterface $dateFin): self
  133.     {
  134.         $this->dateFin = $dateFin;
  135.         return $this;
  136.     }
  137.     public function getCreatedAt(): ?\DateTimeInterface
  138.     {
  139.         return $this->createdAt;
  140.     }
  141.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  142.     {
  143.         $this->createdAt = $createdAt;
  144.         return $this;
  145.     }
  146.     public function getUpdatedAt(): ?\DateTimeInterface
  147.     {
  148.         return $this->updatedAt;
  149.     }
  150.     public function setUpdatedAt(\DateTimeInterface $updatedAt): self
  151.     {
  152.         $this->updatedAt = $updatedAt;
  153.         return $this;
  154.     }
  155.     /**
  156.      * @return Collection<int, KitPromotionnelItem>
  157.      */
  158.     public function getItems(): Collection
  159.     {
  160.         return $this->items;
  161.     }
  162.     public function addItem(KitPromotionnelItem $item): self
  163.     {
  164.         if (!$this->items->contains($item)) {
  165.             $this->items[] = $item;
  166.             $item->setKitPromotionnel($this);
  167.         }
  168.         return $this;
  169.     }
  170.     public function removeItem(KitPromotionnelItem $item): self
  171.     {
  172.         if ($this->items->removeElement($item)) {
  173.             if ($item->getKitPromotionnel() === $this) {
  174.                 $item->setKitPromotionnel(null);
  175.             }
  176.         }
  177.         return $this;
  178.     }
  179.     public function isDisponible(?\DateTimeInterface $referenceDate = null): bool
  180.     {
  181.         if (!$this->actif) {
  182.             return false;
  183.         }
  184.         if (null === $referenceDate) {
  185.             $referenceDate = new \DateTimeImmutable();
  186.         }
  187.         $dateDebut = $this->getDateDebutReference();
  188.         $dateFin = $this->getDateFinReference();
  189.         if (null !== $dateDebut && $referenceDate < $dateDebut) {
  190.             return false;
  191.         }
  192.         if (null !== $dateFin && $referenceDate > $dateFin) {
  193.             return false;
  194.         }
  195.         return !$this->items->isEmpty();
  196.     }
  197.     public function getNombreArticles(): int
  198.     {
  199.         return $this->items->count();
  200.     }
  201.     private function getDateDebutReference(): ?\DateTimeInterface
  202.     {
  203.         if (null === $this->dateDebut) {
  204.             return null;
  205.         }
  206.         return clone $this->dateDebut;
  207.     }
  208.     private function getDateFinReference(): ?\DateTimeInterface
  209.     {
  210.         if (null === $this->dateFin) {
  211.             return null;
  212.         }
  213.         $dateFin = clone $this->dateFin;
  214.         if ('00:00:00' === $dateFin->format('H:i:s')) {
  215.             $dateFin->setTime(23, 59, 59);
  216.         }
  217.         return $dateFin;
  218.     }
  219.     /**
  220.      * @ORM\PrePersist
  221.      */
  222.     public function onPrePersist(): void
  223.     {
  224.         $now = new \DateTimeImmutable();
  225.         $this->createdAt = $now;
  226.         $this->updatedAt = $now;
  227.     }
  228.     /**
  229.      * @ORM\PreUpdate
  230.      */
  231.     public function onPreUpdate(): void
  232.     {
  233.         $this->updatedAt = new \DateTimeImmutable();
  234.     }
  235.     /**
  236.      * @Assert\Callback
  237.      */
  238.     public function validate(ExecutionContextInterface $context, $payload): void
  239.     {
  240.         if (null !== $this->dateDebut && null !== $this->dateFin && $this->dateDebut > $this->dateFin) {
  241.             $context->buildViolation('La date de fin doit etre posterieure a la date de debut.')
  242.                 ->atPath('dateFin')
  243.                 ->addViolation();
  244.         }
  245.     }
  246. }