<?php
namespace App\Entity;
use App\Repository\PromotionRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JsonSerializable;
use ApiPlatform\Core\Annotation\ApiResource;
use phpDocumentor\Reflection\Types\Boolean;
/**
* @ORM\Entity(repositoryClass=PromotionRepository::class)
*/
class Promotion implements JsonSerializable
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $description;
/**
* @ORM\Column(type="datetime")
*/
private $startAt;
/**
* @ORM\Column(type="datetime")
*/
private $endAt;
/**
* @ORM\Column(type="datetime")
*/
private $createdAt;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $discountType;
/**
* @ORM\Column(type="float", nullable=true)
*/
private $discountValue;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $code;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $totalQuantity;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $quntityUser;
/**
* @ORM\OneToMany(targetEntity=Produit::class, mappedBy="promotion")
*/
private $produits;
/**
* @ORM\OneToMany(targetEntity=User::class, mappedBy="promotion")
*/
private $clients;
public function __construct()
{
$this->produits = new ArrayCollection();
$this->clients = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getStartAt(): ?\DateTimeInterface
{
return $this->startAt;
}
public function setStartAt(\DateTimeInterface $startAt): self
{
$this->startAt = $startAt;
return $this;
}
public function getEndAt(): ?\DateTimeInterface
{
return $this->endAt;
}
public function setEndAt(\DateTimeInterface $endAt): self
{
$this->endAt = $endAt;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getDiscountType(): ?string
{
return $this->discountType;
}
public function setDiscountType(?string $discountType): self
{
$this->discountType = $discountType;
return $this;
}
public function getDiscountValue(): ?float
{
return $this->discountValue;
}
public function setDiscountValue(?float $discountValue): self
{
$this->discountValue = $discountValue;
return $this;
}
public function getCode(): ?string
{
return $this->code;
}
public function setCode(?string $code): self
{
$this->code = $code;
return $this;
}
public function getTotalQuantity(): ?int
{
return $this->totalQuantity;
}
public function setTotalQuantity(?int $totalQuantity): self
{
$this->totalQuantity = $totalQuantity;
return $this;
}
public function getQuntityUser(): ?int
{
return $this->quntityUser;
}
public function setQuntityUser(?int $quntityUser): self
{
$this->quntityUser = $quntityUser;
return $this;
}
/**
* @return Collection|Produit[]
*/
public function getProduits(): Collection
{
return $this->produits;
}
public function addProduit(Produit $produit): self
{
if (!$this->produits->contains($produit)) {
$this->produits[] = $produit;
$produit->setPromotion($this);
}
return $this;
}
public function removeProduit(Produit $produit): self
{
if ($this->produits->removeElement($produit)) {
// set the owning side to null (unless already changed)
if ($produit->getPromotion() === $this) {
$produit->setPromotion(null);
}
}
return $this;
}
/**
* @return Collection|User[]
*/
public function getClients(): Collection
{
return $this->clients;
}
public function addClient(User $client): self
{
if (!$this->clients->contains($client)) {
$this->clients[] = $client;
$client->setPromotion($this);
}
return $this;
}
public function removeClient(User $client): self
{
if ($this->clients->removeElement($client)) {
// set the owning side to null (unless already changed)
if ($client->getPromotion() === $this) {
$client->setPromotion(null);
}
}
return $this;
}
// Fonction pour déterminer la validité de promo en temps
public function isValid(): ?bool
{
$dateJour = new \DateTime('now');
if ($dateJour >= $this->startAt && $dateJour <= $this->endAt) {
return true;
}
return false;
}
public function jsonSerialize()
{
return array(
'id' => $this->getId(),
'start' => $this->getStartAt(),
'end' => $this->getEndAt(),
'discountType' => $this->getDiscountType(),
'discountValue' => $this->getDiscountValue(),
'code' => $this->getCode(),
'isValid' => $this->isValid()
);
}
}