<?php
namespace App\Entity;
use App\Repository\KitPromotionnelRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
/**
* @ORM\Entity(repositoryClass=KitPromotionnelRepository::class)
* @ORM\HasLifecycleCallbacks()
*/
class KitPromotionnel
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank
*/
private $nom;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $description;
/**
* @ORM\Column(type="float")
* @Assert\NotNull
* @Assert\GreaterThanOrEqual(value=0)
*/
private $prixKit;
/**
* @ORM\Column(type="boolean", options={"default": true})
*/
private $actif = true;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $image;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $dateDebut;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $dateFin;
/**
* @ORM\Column(type="datetime")
*/
private $createdAt;
/**
* @ORM\Column(type="datetime")
*/
private $updatedAt;
/**
* @ORM\OneToMany(targetEntity=KitPromotionnelItem::class, mappedBy="kitPromotionnel", cascade={"persist", "remove"}, orphanRemoval=true)
* @Assert\Valid
* @Assert\Count(min=1, minMessage="Ajoutez au moins un produit au kit promotionnel.")
*/
private $items;
public function __construct()
{
$this->items = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getNom(): ?string
{
return $this->nom;
}
public function setNom(string $nom): self
{
$this->nom = $nom;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getPrixKit(): ?float
{
return $this->prixKit;
}
public function setPrixKit(float $prixKit): self
{
$this->prixKit = $prixKit;
return $this;
}
public function isActif(): ?bool
{
return $this->actif;
}
public function setActif(bool $actif): self
{
$this->actif = $actif;
return $this;
}
public function getImage(): ?string
{
return $this->image;
}
public function setImage(?string $image): self
{
$this->image = $image;
return $this;
}
public function getDateDebut(): ?\DateTimeInterface
{
return $this->dateDebut;
}
public function setDateDebut(?\DateTimeInterface $dateDebut): self
{
$this->dateDebut = $dateDebut;
return $this;
}
public function getDateFin(): ?\DateTimeInterface
{
return $this->dateFin;
}
public function setDateFin(?\DateTimeInterface $dateFin): self
{
$this->dateFin = $dateFin;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* @return Collection<int, KitPromotionnelItem>
*/
public function getItems(): Collection
{
return $this->items;
}
public function addItem(KitPromotionnelItem $item): self
{
if (!$this->items->contains($item)) {
$this->items[] = $item;
$item->setKitPromotionnel($this);
}
return $this;
}
public function removeItem(KitPromotionnelItem $item): self
{
if ($this->items->removeElement($item)) {
if ($item->getKitPromotionnel() === $this) {
$item->setKitPromotionnel(null);
}
}
return $this;
}
public function isDisponible(?\DateTimeInterface $referenceDate = null): bool
{
if (!$this->actif) {
return false;
}
if (null === $referenceDate) {
$referenceDate = new \DateTimeImmutable();
}
$dateDebut = $this->getDateDebutReference();
$dateFin = $this->getDateFinReference();
if (null !== $dateDebut && $referenceDate < $dateDebut) {
return false;
}
if (null !== $dateFin && $referenceDate > $dateFin) {
return false;
}
return !$this->items->isEmpty();
}
public function getNombreArticles(): int
{
return $this->items->count();
}
private function getDateDebutReference(): ?\DateTimeInterface
{
if (null === $this->dateDebut) {
return null;
}
return clone $this->dateDebut;
}
private function getDateFinReference(): ?\DateTimeInterface
{
if (null === $this->dateFin) {
return null;
}
$dateFin = clone $this->dateFin;
if ('00:00:00' === $dateFin->format('H:i:s')) {
$dateFin->setTime(23, 59, 59);
}
return $dateFin;
}
/**
* @ORM\PrePersist
*/
public function onPrePersist(): void
{
$now = new \DateTimeImmutable();
$this->createdAt = $now;
$this->updatedAt = $now;
}
/**
* @ORM\PreUpdate
*/
public function onPreUpdate(): void
{
$this->updatedAt = new \DateTimeImmutable();
}
/**
* @Assert\Callback
*/
public function validate(ExecutionContextInterface $context, $payload): void
{
if (null !== $this->dateDebut && null !== $this->dateFin && $this->dateDebut > $this->dateFin) {
$context->buildViolation('La date de fin doit etre posterieure a la date de debut.')
->atPath('dateFin')
->addViolation();
}
}
}