src/Entity/Operation/MultiOffer.php line 115

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