src/Entity/Operation/Coupon.php line 81

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Operation;
  3. use App\Entity\PeriodicityOperationMultiOffer;
  4. use ApiPlatform\Core\Annotation\ApiFilter;
  5. use ApiPlatform\Core\Annotation\ApiResource;
  6. use App\Controller\Api\Operation\Coupon\GetAllPublicCoupons;
  7. use App\Controller\Api\Operation\Coupon\GetCouponsByUserDepartment;
  8. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter;
  9. use App\Entity\Details\Operation\DetailsCoupon;
  10. use App\Repository\Operation\CouponRepository;
  11. use Doctrine\Common\Annotations\Annotation\IgnoreAnnotation;
  12. use Doctrine\ORM\Mapping as ORM;
  13. use Exception;
  14. use Symfony\Component\Serializer\Annotation\Groups;
  15. use Symfony\Component\Serializer\Annotation\SerializedName;
  16. use Symfony\Component\Validator\Constraints as Assert;
  17. use App\Controller\Api\Operation\Coupon\GetOnePublicCoupon;
  18. /**
  19.  * @ORM\Entity(repositoryClass=CouponRepository::class)
  20.  * @ApiResource(
  21.  *     attributes={
  22.  *          "force_eager"=false,
  23.  *          "normalization_context"={
  24.  *              "groups"={
  25.  *                  "OperationCoupon:output",
  26.  *                  "OperationCoupon:io",
  27.  *                  "get_all_public_coupons",
  28.  *                  "get_coupons_by_user_department",
  29.  *              },
  30.  *              "enable_max_depth"=true
  31.  *          },
  32.  *          "denormalization_context"={
  33.  *              "groups"={
  34.  *                  "OperationCoupon:input",
  35.  *                  "OperationCoupon:io"
  36.  *              },
  37.  *              "enable_max_depth"=true
  38.  *          }
  39.  *      },
  40.  *     collectionOperations={
  41.  *          "post"={
  42.  *              "method"="POST",
  43.  *              "access_control"="is_granted('ROLE_ADMIN_SOGEC')",
  44.  *          },
  45.  *          "public-get"={
  46.  *              "method"="GET",
  47.  *              "path"="/public/coupons",
  48.  *              "access_control"="is_granted('ROLE_PUBLIC')",
  49.  *              "validation_groups"={"get_all_public_coupons"},
  50.  *              "controller"=GetAllPublicCoupons::class,
  51.  *              "defaults"={"_api_receive"=false},
  52.  *          },
  53.  *          "get-by-user-department"={
  54.  *              "method"="GET",
  55.  *              "path"="/coupons",
  56.  *              "access_control"="is_granted('ROLE_USER')",
  57.  *              "validation_groups"={"get_coupons_by_user_department"},
  58.  *              "controller"=GetCouponsByUserDepartment::class,
  59.  *              "defaults"={"_api_receive"=false},
  60.  *          }
  61.  *     },
  62.  *     itemOperations={
  63.  *          "put"={
  64.  *              "method"="PUT",
  65.  *              "access_control"="is_granted('ROLE_ADMIN_SOGEC')",
  66.  *          },
  67.  *          "get"={
  68.  *              "method"="GET",
  69.  *          },
  70.  *          "public-get"={
  71.  *              "method"="GET",
  72.  *              "path"="/public/coupon/{id}",
  73.  *              "controller"=GetOnePublicCoupon::class,
  74.  *          },
  75.  *      }
  76.  * )
  77.  */
  78. class Coupon extends Operation
  79. {
  80.     /**
  81.      * @ORM\Column(type="integer", options={"default" : 0})
  82.      * @Groups({
  83.      *     "OperationCoupon:io",
  84.      *     "get_all_public_coupons",
  85.      *     "get_coupons_by_user_department",
  86.      * })
  87.      */
  88.     private int $stock 0;
  89.     /**
  90.      * @ORM\Column(type="integer", options={"default" : 0})
  91.      * @SerializedName("current_stock")
  92.      * @Groups({
  93.      *      "OperationCoupon:io",
  94.      *      "get_all_public_coupons",
  95.      *      "get_coupons_by_user_department",
  96.      * })
  97.      */
  98.     private int $currentStock 0;
  99.     /**
  100.      * @SerializedName("max_participation_per_month")
  101.      * @ORM\Column(type="integer")
  102.      * @Groups({
  103.      *     "OperationCoupon:io",
  104.      * })
  105.      */
  106.     private ?int $maxParticipationPerMonth 0;
  107.     /**
  108.      * @ORM\Column(type="datetime", options={"default": "CURRENT_TIMESTAMP"})
  109.      * @Assert\GreaterThan(propertyPath="billyStartDate")
  110.      * @Assert\NotBlank()
  111.      * @Groups({
  112.      *     "OperationCoupon:io",
  113.      * })
  114.      * @ApiFilter(DateFilter::class, strategy=DateFilter::EXCLUDE_NULL)
  115.      */
  116.     private $startDate;
  117.     /**
  118.      * @ORM\Column(type="datetime", options={"default": "CURRENT_TIMESTAMP"})
  119.      * @Assert\LessThanOrEqual(propertyPath="billyEndDate")
  120.      * @Assert\NotBlank()
  121.      * @Groups({
  122.      *     "OperationCoupon:io",
  123.      * })
  124.      * @ApiFilter(DateFilter::class, strategy=DateFilter::EXCLUDE_NULL)
  125.      */
  126.     private $endDate;
  127.     /**
  128.      * @ORM\Column(type="datetime", options={"default": "CURRENT_TIMESTAMP"}, nullable=true)
  129.      * @Assert\NotBlank()
  130.      * @Groups({
  131.      *     "OperationCoupon:io"
  132.      * })
  133.      * @ApiFilter(DateFilter::class, strategy=DateFilter::EXCLUDE_NULL)
  134.      */
  135.     private ?\DateTimeInterface $billyStartDate;
  136.     /**
  137.      * @ORM\Column(type="datetime", options={"default": "CURRENT_TIMESTAMP"})
  138.      * @Assert\NotBlank()
  139.      * @Groups({
  140.      *     "OperationCoupon:io"
  141.      * })
  142.      * @ApiFilter(DateFilter::class, strategy=DateFilter::EXCLUDE_NULL)
  143.      */
  144.     private ?\DateTimeInterface $billyEndDate;
  145.     /**
  146.      * @ORM\Column(type="boolean", options={"default": 0})
  147.      */
  148.     private bool $online false;
  149.     /**
  150.      * @ORM\OneToOne(targetEntity=DetailsCoupon::class, cascade={"persist", "remove"})
  151.      * @SerializedName("details_coupon")
  152.      * @Groups({
  153.      *     "OperationCoupon:io",
  154.      *     "get_all_public_coupons",
  155.      *     "get_coupons_by_user_department",
  156.      * })
  157.      */
  158.     private ?DetailsCoupon $details;
  159.     /**
  160.      * @ORM\Column(type="integer", options={"default": 1})
  161.      */
  162.     private int $maxPartByUser 1;
  163.     /**
  164.      * @Groups({
  165.      *     "OperationCoupon:o",
  166.      *     "get_all_public_coupons",
  167.      *     "get_coupons_by_user_department",
  168.      * })
  169.      */
  170.     private bool $printed false;
  171.     /**
  172.      * @ORM\Column(type="string", length=255)
  173.      */
  174.     private $periodicity;
  175.     public function getDetails(): ?DetailsCoupon
  176.     {
  177.         return $this->details;
  178.     }
  179.     public function setDetails(?DetailsCoupon $details): self
  180.     {
  181.         $this->details $details;
  182.         $details->setCoupon($this);
  183.         return $this;
  184.     }
  185.     /**
  186.      * @return bool
  187.      */
  188.     public function isPrinted(): bool
  189.     {
  190.         return $this->printed;
  191.     }
  192.     /**
  193.      * @param bool $printed
  194.      * @return Coupon
  195.      */
  196.     public function setPrinted(bool $printed): Coupon
  197.     {
  198.         $this->printed $printed;
  199.         return $this;
  200.     }
  201.     public function getStock(): ?int
  202.     {
  203.         return $this->stock;
  204.     }
  205.     public function hasStock(): bool
  206.     {
  207.         return (bool)$this->stock;
  208.     }
  209.     public function setStock(int $stock): self
  210.     {
  211.         $this->stock $stock;
  212.         return $this;
  213.     }
  214.     public function getCurrentStock(): int
  215.     {
  216.         return $this->currentStock;
  217.     }
  218.     public function setCurrentStock(int $currentStock): self
  219.     {
  220.         $this->currentStock $currentStock;
  221.         return $this;
  222.     }
  223.     public function getStartDate(): ?\DateTimeInterface
  224.     {
  225.         return $this->startDate;
  226.     }
  227.     public function setStartDate(\DateTimeInterface $startDate): self
  228.     {
  229.         $this->startDate $startDate;
  230.         return $this;
  231.     }
  232.     public function getEndDate(): ?\DateTimeInterface
  233.     {
  234.         return $this->endDate;
  235.     }
  236.     public function setEndDate(\DateTimeInterface $endDate): self
  237.     {
  238.         $this->endDate $endDate;
  239.         return $this;
  240.     }
  241.     public function isOnline(): bool
  242.     {
  243.         return (bool)$this->online;
  244.     }
  245.     public function getOnline(): bool
  246.     {
  247.         return (bool)$this->online;
  248.     }
  249.     public function setOnline(bool $online): self
  250.     {
  251.         $this->online $online;
  252.         return $this;
  253.     }
  254.     public function getMaxPartByUser(): ?int
  255.     {
  256.         return $this->maxPartByUser;
  257.     }
  258.     public function setMaxPartByUser(int $maxPartByUser): self
  259.     {
  260.         $this->maxPartByUser $maxPartByUser;
  261.         return $this;
  262.     }
  263.     public function decreaseStock()
  264.     {
  265.         $this->currentStock--;
  266.     }
  267.     public function getBillyStartDate(): ?\DateTimeInterface
  268.     {
  269.         return $this->billyStartDate;
  270.     }
  271.     public function setBillyStartDate(?\DateTimeInterface $billyStartDate): self
  272.     {
  273.         $this->billyStartDate $billyStartDate;
  274.         return $this;
  275.     }
  276.     public function getBillyEndDate(): ?\DateTimeInterface
  277.     {
  278.         return $this->billyEndDate;
  279.     }
  280.     public function setBillyEndDate(?\DateTimeInterface $billyEndDate): self
  281.     {
  282.         $this->billyEndDate $billyEndDate;
  283.         return $this;
  284.     }
  285.     public function getPeriodicity(): ?string
  286.     {
  287.         return $this->periodicity;
  288.     }
  289.     /**
  290.      * @throws Exception
  291.      */
  292.     public function setPeriodicity(?string $periodicity): self
  293.     {
  294.         if ($periodicity) {
  295.             if (PeriodicityOperationMultiOffer::BILLY_PERIODICITY[$periodicity] ?? 0) {
  296.                 $this->periodicity $periodicity;
  297.             } else {
  298.                 throw new Exception(sprintf("Periodicity not found : %s"$periodicity));
  299.             }
  300.         } else {
  301.             $this->periodicity PeriodicityOperationMultiOffer::BILLY_PERIODICITY_GLOBAL;
  302.         }
  303.         return $this;
  304.     }
  305.     public function getMaxParticipationPerMonth(): ?int
  306.     {
  307.         return $this->maxParticipationPerMonth;
  308.     }
  309.     public function setMaxParticipationPerMonth(?int $maxParticipationPerMonth): self
  310.     {
  311.         $this->maxParticipationPerMonth PeriodicityOperationMultiOffer::getOperationMaxParticipationPerMonth($maxParticipationPerMonth$this->periodicity,
  312.             $this->stock$this->billyStartDate$this->billyEndDate);
  313.         return $this;
  314.     }
  315. }