src/Entity/Subscription.php line 47

  1. <?php
  2. namespace App\Entity;
  3. use DateTime;
  4. use DateTimeInterface;
  5. use Symfony\Component\Uid\Ulid;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use App\Entity\enums\SubscriptionStatus;
  8. use App\Entity\enums\SubscriptionPaymentStatus;
  9. use App\Repository\SubscriptionRepository;
  10. use Doctrine\Common\Collections\Collection;
  11. use ApiPlatform\Core\Annotation\ApiResource;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. use Symfony\Component\Serializer\Annotation\Groups;
  14. use Symfony\Bridge\Doctrine\IdGenerator\UlidGenerator;
  15. use Gedmo\Mapping\Annotation as Gedmo;
  16. #[ORM\Entity(repositoryClassSubscriptionRepository::class)]
  17. #[Gedmo\SoftDeleteable(fieldName'deletedAt'timeAwarefalsehardDeletetrue)]
  18. #[ORM\HasLifecycleCallbacks()]
  19. #[ApiResource(
  20.     attributes: ["security" => "is_granted('IS_AUTHENTICATED_FULLY')"],
  21.     itemOperations: [
  22.         'get' => [
  23.             'normalization_context' => [
  24.                 'groups' => [
  25.                     'read:subscription:basic',
  26.                     'read:service:basic',
  27.                     'read:child:basic'
  28.                 ]
  29.             ]
  30.         ],
  31.     ],
  32.     collectionOperations: [
  33.         'get' => [
  34.             'normalization_context' => [
  35.                 'groups' => [
  36.                     'read:subscription:basic',
  37.                     'read:service:basic',
  38.                     'read:child:basic'
  39.                 ]
  40.             ]
  41.         ],
  42.     ],
  43. )]
  44. class Subscription
  45. {
  46.     use Timestamps;
  47.     #[ORM\Id]
  48.     #[ORM\Column(type'ulid'uniquetrue)]
  49.     #[ORM\GeneratedValue(strategy"CUSTOM")]
  50.     #[ORM\CustomIdGenerator(class: UlidGenerator::class)]
  51.     #[Groups(['read:subscription:basic'])]
  52.     private ?Ulid $id null;
  53.     #[ORM\Column(type'date')]
  54.     #[Groups(['read:subscription:basic'])]
  55.     private $start_date;
  56.     #[ORM\Column(type'date')]
  57.     #[Groups(['read:subscription:basic'])]
  58.     private $expire_date;
  59.     #[ORM\Column(type'float')]
  60.     private $price 0;
  61.     #[ORM\Column(type'integer')]
  62.     #[Groups(['read:subscription:basic'])]
  63.     private $quantity 1;
  64.     #[ORM\Column(type'float')]
  65.     #[Groups(['read:subscription:basic'])]
  66.     private $total 0;
  67.     #[ORM\Column(type'string'length255enumTypeSubscriptionStatus::class)]
  68.     #[Groups(['read:subscription:basic'])]
  69.     private $status;
  70.     #[ORM\Column(type'string'length255enumTypeSubscriptionPaymentStatus::class)]
  71.     #[Groups(['read:subscription:basic'])]
  72.     private $status_payment;
  73.     #[ORM\Column(type'float')]
  74.     #[Groups(['read:subscription:basic'])]
  75.     private $discount 0;
  76.     #[ORM\Column(type'text'nullabletrue)]
  77.     #[Groups(['read:subscription:basic'])]
  78.     private $description;
  79.     #[ORM\ManyToOne(targetEntityService::class, inversedBy'subscriptions')]
  80.     #[ORM\JoinColumn(nullablefalse)]
  81.     #[Groups(['read:subscription:basic'])]
  82.     private $service;
  83.     #[ORM\ManyToOne(targetEntityChild::class, inversedBy'subscriptions')]
  84.     #[ORM\JoinColumn(nullablefalse)]
  85.     #[Groups(['read:subscription:basic'])]
  86.     private $child;
  87.     #[ORM\ManyToOne(targetEntityUser::class, inversedBy'subscriptions')]
  88.     #[ORM\JoinColumn(nullablefalse)]
  89.     private $created_by;
  90.     #[ORM\ManyToOne(targetEntityCompany::class, inversedBy'subscriptions')]
  91.     #[ORM\JoinColumn(nullablefalse)]
  92.     private $owner;
  93.     #[ORM\OneToMany(mappedBy'subscription'targetEntityPayment::class, orphanRemovaltrue)]
  94.     #[Groups(['read:subscription:basic'])]
  95.     private $payments;
  96.     #[ORM\ManyToOne(targetEntitySeason::class, inversedBy'subscriptions')]
  97.     #[ORM\JoinColumn(nullablefalse)]
  98.     private $season;
  99.     public function __construct()
  100.     {
  101.         $this->start_date = new DateTime();
  102.         $this->expire_date = new DateTime();
  103.         $this->status SubscriptionStatus::DRAFT;
  104.         $this->status_payment SubscriptionPaymentStatus::NOT_PAYED;
  105.         $this->payments = new ArrayCollection();
  106.     }
  107.     public function __toString()
  108.     {
  109.         return $this->child " (" $this->service->getName() . " " $this->getRestToPay() . " DZD )";
  110.     }
  111.     public function getId(): ?Ulid
  112.     {
  113.         return $this->id;
  114.     }
  115.     #[Groups(['read:subscription:basic'])]
  116.     public function getTotalToPay(): ?float
  117.     {
  118.         return $this->total $this->discount;
  119.     }
  120.     #[Groups(['read:subscription:basic'])]
  121.     public function getRestToPay(): ?float
  122.     {
  123.         $rest $this->getTotalToPay();
  124.         foreach ($this->payments as $payment) {
  125.             $rest -= $payment->getAmount();
  126.         }
  127.         return $rest;
  128.     }
  129.     public function isPaied(): ?float
  130.     {
  131.         $rest $this->getRestToPay();
  132.         if ($rest 0)
  133.             return false;
  134.         return true;
  135.     }
  136.     #[Groups(['read:subscription:basic'])]
  137.     public function isExpired(): ?int
  138.     {
  139.         $now = new DateTime();
  140.         $diff $now->diff($this->expire_date);
  141.         return $diff->invert;
  142.     }
  143.     public function getStartDate(): ?DateTimeInterface
  144.     {
  145.         return $this->start_date;
  146.     }
  147.     public function setStartDate(\DateTimeInterface $start_date): self
  148.     {
  149.         $this->start_date $start_date;
  150.         return $this;
  151.     }
  152.     public function getExpireDate(): ?\DateTimeInterface
  153.     {
  154.         return $this->expire_date;
  155.     }
  156.     public function setExpireDate(\DateTimeInterface $expire_date): self
  157.     {
  158.         $this->expire_date $expire_date;
  159.         return $this;
  160.     }
  161.     public function getPrice(): ?float
  162.     {
  163.         return $this->price;
  164.     }
  165.     public function setPrice(float $price): self
  166.     {
  167.         $this->price $price;
  168.         return $this;
  169.     }
  170.     public function getQuantity(): ?int
  171.     {
  172.         return $this->quantity;
  173.     }
  174.     public function setQuantity(int $quantity): self
  175.     {
  176.         $this->quantity $quantity;
  177.         return $this;
  178.     }
  179.     public function getTotal(): ?float
  180.     {
  181.         return $this->total;
  182.     }
  183.     public function setTotal(float $total): self
  184.     {
  185.         $this->total $total;
  186.         return $this;
  187.     }
  188.     public function getStatus(): ?SubscriptionStatus
  189.     {
  190.         return $this->status;
  191.     }
  192.     public function setStatus(SubscriptionStatus $status): self
  193.     {
  194.         $this->status $status;
  195.         return $this;
  196.     }
  197.     public function getStatusPayment(): ?SubscriptionPaymentStatus
  198.     {
  199.         return $this->status_payment;
  200.     }
  201.     public function setStatusPayment(SubscriptionPaymentStatus $status_payment): self
  202.     {
  203.         $this->status_payment $status_payment;
  204.         return $this;
  205.     }
  206.     public function getDiscount(): ?float
  207.     {
  208.         return $this->discount;
  209.     }
  210.     public function setDiscount(float $discount): self
  211.     {
  212.         $this->discount $discount;
  213.         return $this;
  214.     }
  215.     public function getDescription(): ?string
  216.     {
  217.         return $this->description;
  218.     }
  219.     public function setDescription(string $description): self
  220.     {
  221.         $this->description $description;
  222.         return $this;
  223.     }
  224.     public function getService(): ?Service
  225.     {
  226.         return $this->service;
  227.     }
  228.     public function setService(?Service $service): self
  229.     {
  230.         $this->service $service;
  231.         return $this;
  232.     }
  233.     public function getChild(): ?Child
  234.     {
  235.         return $this->child;
  236.     }
  237.     public function setChild(?Child $child): self
  238.     {
  239.         $this->child $child;
  240.         return $this;
  241.     }
  242.     public function getCreatedBy(): ?User
  243.     {
  244.         return $this->created_by;
  245.     }
  246.     public function setCreatedBy(?User $created_by): self
  247.     {
  248.         $this->created_by $created_by;
  249.         return $this;
  250.     }
  251.     public function getOwner(): ?Company
  252.     {
  253.         return $this->owner;
  254.     }
  255.     public function setOwner(?Company $owner): self
  256.     {
  257.         $this->owner $owner;
  258.         return $this;
  259.     }
  260.     /**
  261.      * @return Collection<int, Payment>
  262.      */
  263.     public function getPayments(): Collection
  264.     {
  265.         return $this->payments;
  266.     }
  267.     public function addPayment(Payment $payment): self
  268.     {
  269.         if (!$this->payments->contains($payment)) {
  270.             $this->payments[] = $payment;
  271.             $payment->setSubscription($this);
  272.         }
  273.         return $this;
  274.     }
  275.     public function removePayment(Payment $payment): self
  276.     {
  277.         if ($this->payments->removeElement($payment)) {
  278.             // set the owning side to null (unless already changed)
  279.             if ($payment->getSubscription() === $this) {
  280.                 $payment->setSubscription(null);
  281.             }
  282.         }
  283.         return $this;
  284.     }
  285.     public function getSeason(): ?Season
  286.     {
  287.         return $this->season;
  288.     }
  289.     public function setSeason(?Season $season): self
  290.     {
  291.         $this->season $season;
  292.         return $this;
  293.     }
  294. }