<?php
namespace App\Entity\Operation;
use App\Entity\PeriodicityOperationMultiOffer;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Controller\Api\Operation\Coupon\GetAllPublicCoupons;
use App\Controller\Api\Operation\Coupon\GetCouponsByUserDepartment;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter;
use App\Entity\Details\Operation\DetailsCoupon;
use App\Repository\Operation\CouponRepository;
use Doctrine\Common\Annotations\Annotation\IgnoreAnnotation;
use Doctrine\ORM\Mapping as ORM;
use Exception;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\SerializedName;
use Symfony\Component\Validator\Constraints as Assert;
use App\Controller\Api\Operation\Coupon\GetOnePublicCoupon;
/**
* @ORM\Entity(repositoryClass=CouponRepository::class)
* @ApiResource(
* attributes={
* "force_eager"=false,
* "normalization_context"={
* "groups"={
* "OperationCoupon:output",
* "OperationCoupon:io",
* "get_all_public_coupons",
* "get_coupons_by_user_department",
* },
* "enable_max_depth"=true
* },
* "denormalization_context"={
* "groups"={
* "OperationCoupon:input",
* "OperationCoupon:io"
* },
* "enable_max_depth"=true
* }
* },
* collectionOperations={
* "post"={
* "method"="POST",
* "access_control"="is_granted('ROLE_ADMIN_SOGEC')",
* },
* "public-get"={
* "method"="GET",
* "path"="/public/coupons",
* "access_control"="is_granted('ROLE_PUBLIC')",
* "validation_groups"={"get_all_public_coupons"},
* "controller"=GetAllPublicCoupons::class,
* "defaults"={"_api_receive"=false},
* },
* "get-by-user-department"={
* "method"="GET",
* "path"="/coupons",
* "access_control"="is_granted('ROLE_USER')",
* "validation_groups"={"get_coupons_by_user_department"},
* "controller"=GetCouponsByUserDepartment::class,
* "defaults"={"_api_receive"=false},
* }
* },
* itemOperations={
* "put"={
* "method"="PUT",
* "access_control"="is_granted('ROLE_ADMIN_SOGEC')",
* },
* "get"={
* "method"="GET",
* },
* "public-get"={
* "method"="GET",
* "path"="/public/coupon/{id}",
* "controller"=GetOnePublicCoupon::class,
* },
* }
* )
*/
class Coupon extends Operation
{
/**
* @ORM\Column(type="integer", options={"default" : 0})
* @Groups({
* "OperationCoupon:io",
* "get_all_public_coupons",
* "get_coupons_by_user_department",
* })
*/
private int $stock = 0;
/**
* @ORM\Column(type="integer", options={"default" : 0})
* @SerializedName("current_stock")
* @Groups({
* "OperationCoupon:io",
* "get_all_public_coupons",
* "get_coupons_by_user_department",
* })
*/
private int $currentStock = 0;
/**
* @SerializedName("max_participation_per_month")
* @ORM\Column(type="integer")
* @Groups({
* "OperationCoupon:io",
* })
*/
private ?int $maxParticipationPerMonth = 0;
/**
* @ORM\Column(type="datetime", options={"default": "CURRENT_TIMESTAMP"})
* @Assert\GreaterThan(propertyPath="billyStartDate")
* @Assert\NotBlank()
* @Groups({
* "OperationCoupon:io",
* })
* @ApiFilter(DateFilter::class, strategy=DateFilter::EXCLUDE_NULL)
*/
private $startDate;
/**
* @ORM\Column(type="datetime", options={"default": "CURRENT_TIMESTAMP"})
* @Assert\LessThanOrEqual(propertyPath="billyEndDate")
* @Assert\NotBlank()
* @Groups({
* "OperationCoupon:io",
* })
* @ApiFilter(DateFilter::class, strategy=DateFilter::EXCLUDE_NULL)
*/
private $endDate;
/**
* @ORM\Column(type="datetime", options={"default": "CURRENT_TIMESTAMP"}, nullable=true)
* @Assert\NotBlank()
* @Groups({
* "OperationCoupon:io"
* })
* @ApiFilter(DateFilter::class, strategy=DateFilter::EXCLUDE_NULL)
*/
private ?\DateTimeInterface $billyStartDate;
/**
* @ORM\Column(type="datetime", options={"default": "CURRENT_TIMESTAMP"})
* @Assert\NotBlank()
* @Groups({
* "OperationCoupon:io"
* })
* @ApiFilter(DateFilter::class, strategy=DateFilter::EXCLUDE_NULL)
*/
private ?\DateTimeInterface $billyEndDate;
/**
* @ORM\Column(type="boolean", options={"default": 0})
*/
private bool $online = false;
/**
* @ORM\OneToOne(targetEntity=DetailsCoupon::class, cascade={"persist", "remove"})
* @SerializedName("details_coupon")
* @Groups({
* "OperationCoupon:io",
* "get_all_public_coupons",
* "get_coupons_by_user_department",
* })
*/
private ?DetailsCoupon $details;
/**
* @ORM\Column(type="integer", options={"default": 1})
*/
private int $maxPartByUser = 1;
/**
* @Groups({
* "OperationCoupon:o",
* "get_all_public_coupons",
* "get_coupons_by_user_department",
* })
*/
private bool $printed = false;
/**
* @ORM\Column(type="string", length=255)
*/
private $periodicity;
public function getDetails(): ?DetailsCoupon
{
return $this->details;
}
public function setDetails(?DetailsCoupon $details): self
{
$this->details = $details;
$details->setCoupon($this);
return $this;
}
/**
* @return bool
*/
public function isPrinted(): bool
{
return $this->printed;
}
/**
* @param bool $printed
* @return Coupon
*/
public function setPrinted(bool $printed): Coupon
{
$this->printed = $printed;
return $this;
}
public function getStock(): ?int
{
return $this->stock;
}
public function hasStock(): bool
{
return (bool)$this->stock;
}
public function setStock(int $stock): self
{
$this->stock = $stock;
return $this;
}
public function getCurrentStock(): int
{
return $this->currentStock;
}
public function setCurrentStock(int $currentStock): self
{
$this->currentStock = $currentStock;
return $this;
}
public function getStartDate(): ?\DateTimeInterface
{
return $this->startDate;
}
public function setStartDate(\DateTimeInterface $startDate): self
{
$this->startDate = $startDate;
return $this;
}
public function getEndDate(): ?\DateTimeInterface
{
return $this->endDate;
}
public function setEndDate(\DateTimeInterface $endDate): self
{
$this->endDate = $endDate;
return $this;
}
public function isOnline(): bool
{
return (bool)$this->online;
}
public function getOnline(): bool
{
return (bool)$this->online;
}
public function setOnline(bool $online): self
{
$this->online = $online;
return $this;
}
public function getMaxPartByUser(): ?int
{
return $this->maxPartByUser;
}
public function setMaxPartByUser(int $maxPartByUser): self
{
$this->maxPartByUser = $maxPartByUser;
return $this;
}
public function decreaseStock()
{
$this->currentStock--;
}
public function getBillyStartDate(): ?\DateTimeInterface
{
return $this->billyStartDate;
}
public function setBillyStartDate(?\DateTimeInterface $billyStartDate): self
{
$this->billyStartDate = $billyStartDate;
return $this;
}
public function getBillyEndDate(): ?\DateTimeInterface
{
return $this->billyEndDate;
}
public function setBillyEndDate(?\DateTimeInterface $billyEndDate): self
{
$this->billyEndDate = $billyEndDate;
return $this;
}
public function getPeriodicity(): ?string
{
return $this->periodicity;
}
/**
* @throws Exception
*/
public function setPeriodicity(?string $periodicity): self
{
if ($periodicity) {
if (PeriodicityOperationMultiOffer::BILLY_PERIODICITY[$periodicity] ?? 0) {
$this->periodicity = $periodicity;
} else {
throw new Exception(sprintf("Periodicity not found : %s", $periodicity));
}
} else {
$this->periodicity = PeriodicityOperationMultiOffer::BILLY_PERIODICITY_GLOBAL;
}
return $this;
}
public function getMaxParticipationPerMonth(): ?int
{
return $this->maxParticipationPerMonth;
}
public function setMaxParticipationPerMonth(?int $maxParticipationPerMonth): self
{
$this->maxParticipationPerMonth = PeriodicityOperationMultiOffer::getOperationMaxParticipationPerMonth($maxParticipationPerMonth, $this->periodicity,
$this->stock, $this->billyStartDate, $this->billyEndDate);
return $this;
}
}