src/Entity/Promotion/PromotionCode.php line 44

  1. <?php
  2. /*
  3.  * This file is part of the Pellipop project.
  4.  *
  5.  * (c) Mobizel
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace App\Entity\Promotion;
  12. use App\Entity\Customer\Customer;
  13. use App\Entity\IdentifiableTrait;
  14. use App\Entity\Payment\Payment;
  15. use App\Entity\Product\CreditPack;
  16. use App\Entity\Promotion\UniquePromotionCode\UniquePromotion;
  17. use App\PromotionCodeTypes;
  18. use Doctrine\Common\Collections\ArrayCollection;
  19. use Doctrine\Common\Collections\Collection;
  20. use Doctrine\ORM\Mapping as ORM;
  21. use Sylius\Component\Resource\Annotation\SyliusCrudRoutes;
  22. use Sylius\Component\Resource\Model\ResourceInterface;
  23. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  24. use Symfony\Component\Validator\Constraints as Assert;
  25. #[ORM\Entity]
  26. #[UniqueEntity('code')]
  27. #[ORM\Table(name'app_promotion_code')]
  28. #[SyliusCrudRoutes(alias'app.promotion_code'path'/admin/promotion-codes'section'backend'templates'backend/crud'grid'app_backend_promotion_code'except: ['show'], vars: [
  29.     'all' => [
  30.         'subheader' => 'app.ui.manage_your_promotion_codes',
  31.         'templates' => [
  32.             'form' => 'backend/promotion_code/_form.html.twig',
  33.         ],
  34.     ],
  35.     'index' => [
  36.         'icon' => 'percent',
  37.     ],
  38. ])]
  39. class PromotionCode implements ResourceInterface
  40. {
  41.     use IdentifiableTrait;
  42.     #[ORM\OneToMany(mappedBy'promotionCode'targetEntityPayment::class)]
  43.     private Collection $payments;
  44.     #[ORM\ManyToOne(targetEntityUniquePromotion::class, inversedBy'promotionCodes'cascade: ['persist'])]
  45.     #[ORM\JoinColumn(nullabletrue)]
  46.     private ?UniquePromotion $uniquePromotion;
  47.     #[ORM\Column(type'string')]
  48.     #[Assert\NotBlankAssert\Length(min3max150)]
  49.     private ?string $name null;
  50.     #[ORM\Column(type'string')]
  51.     #[Assert\NotBlankAssert\Length(min3max50), Assert\Regex('/^[\w-]*$/')]
  52.     private ?string $code null;
  53.     #[Assert\NotBlank]
  54.     #[ORM\Column(type'datetime')]
  55.     private ?\DateTimeInterface $startAt null;
  56.     #[Assert\NotBlank]
  57.     #[ORM\Column(type'datetime')]
  58.     private ?\DateTimeInterface $endAt null;
  59.     #[ORM\Column(type'integer')]
  60.     private int $used 0;
  61.     #[ORM\Column(type'datetime'nullabletrue)]
  62.     private ?\DateTimeInterface $usedAt null;
  63.     #[ORM\Column(type'string'nullabletrue)]
  64.     private ?string $userEmail null;
  65.     #[ORM\Column(type'datetime')]
  66.     private \DateTimeInterface $createdAt;
  67.     #[ORM\Column(type'json')]
  68.     private array $rules;
  69.     #[ORM\ManyToMany(targetEntityCustomer::class)]
  70.     private Collection $customers;
  71.     private array $creditPacks = [];
  72.     public function __construct()
  73.     {
  74.         $this->payments = new ArrayCollection();
  75.         $this->rules = [];
  76.         $this->createdAt = new \DateTimeImmutable();
  77.         $this->customers = new ArrayCollection();
  78.     }
  79.     public function getName(): ?string
  80.     {
  81.         return $this->name;
  82.     }
  83.     public function setName(string $name): self
  84.     {
  85.         $this->name $name;
  86.         return $this;
  87.     }
  88.     public function getCode(): ?string
  89.     {
  90.         return $this->code;
  91.     }
  92.     public function setCode(string $code): self
  93.     {
  94.         $this->code $code;
  95.         return $this;
  96.     }
  97.     public function getStartAt(): ?\DateTimeInterface
  98.     {
  99.         return $this->startAt;
  100.     }
  101.     public function setStartAt(?\DateTimeInterface $startAt): self
  102.     {
  103.         $this->startAt $startAt;
  104.         return $this;
  105.     }
  106.     public function getEndAt(): ?\DateTimeInterface
  107.     {
  108.         return $this->endAt;
  109.     }
  110.     public function setEndAt(?\DateTimeInterface $endAt): self
  111.     {
  112.         $this->endAt $endAt;
  113.         return $this;
  114.     }
  115.     public function getUsed(): int
  116.     {
  117.         return $this->used;
  118.     }
  119.     public function setUsed(int $used): self
  120.     {
  121.         $this->used $used;
  122.         return $this;
  123.     }
  124.     public function getCreatedAt(): \DateTimeInterface
  125.     {
  126.         return $this->createdAt;
  127.     }
  128.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  129.     {
  130.         $this->createdAt $createdAt;
  131.         return $this;
  132.     }
  133.     public function getRules()
  134.     {
  135.         return $this->rules;
  136.     }
  137.     public function setRules(array $rules): self
  138.     {
  139.         $this->rules $rules;
  140.         return $this;
  141.     }
  142.     public function hasRule(string $rule): bool
  143.     {
  144.         return array_key_exists($rule$this->rules);
  145.     }
  146.     public function getRuleType(): ?string
  147.     {
  148.         return (!empty($this->rules)) ? strval(array_key_first($this->rules)) : null;
  149.     }
  150.     public function getCreditsOffered(): int
  151.     {
  152.         if (true === $this->hasRule(PromotionCodeTypes::TYPE_CREDITS_OFFERED)) {
  153.             return $this->rules[PromotionCodeTypes::TYPE_CREDITS_OFFERED];
  154.         }
  155.         return 0;
  156.     }
  157.     public function getCreditPacks(): array
  158.     {
  159.         return $this->creditPacks;
  160.     }
  161.     public function addCreditPack(CreditPack $creditPack): self
  162.     {
  163.         if (!in_array($creditPack$this->creditPackstrue)) {
  164.             $this->creditPacks [] = $creditPack;
  165.         }
  166.         return $this;
  167.     }
  168.     /**
  169.      * @return Collection<int, Customer>
  170.      */
  171.     public function getCustomers(): Collection
  172.     {
  173.         return $this->customers;
  174.     }
  175.     public function addCustomer(Customer $customer): self
  176.     {
  177.         if (!$this->customers->contains($customer)) {
  178.             $this->customers->add($customer);
  179.         }
  180.         return $this;
  181.     }
  182.     public function removeCustomer(Customer $customer): self
  183.     {
  184.         $this->customers->removeElement($customer);
  185.         return $this;
  186.     }
  187.     public function getUniquePromotion(): ?UniquePromotion
  188.     {
  189.         return $this->uniquePromotion;
  190.     }
  191.     public function setUniquePromotion(?UniquePromotion $uniquePromotion): void
  192.     {
  193.         $this->uniquePromotion $uniquePromotion;
  194.     }
  195.     public function getUsedAt(): ?\DateTimeInterface
  196.     {
  197.         return $this->usedAt;
  198.     }
  199.     public function setUsedAt(?\DateTimeInterface $usedAt): void
  200.     {
  201.         $this->usedAt $usedAt;
  202.     }
  203.     public function getUserEmail(): ?string
  204.     {
  205.         return $this->userEmail;
  206.     }
  207.     public function setUserEmail(?string $userEmail): void
  208.     {
  209.         $this->userEmail $userEmail;
  210.     }
  211. }