src/Entity/Subscription.php line 47
<?php
namespace App\Entity;
use DateTime;
use DateTimeInterface;
use Symfony\Component\Uid\Ulid;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\enums\SubscriptionStatus;
use App\Entity\enums\SubscriptionPaymentStatus;
use App\Repository\SubscriptionRepository;
use Doctrine\Common\Collections\Collection;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Bridge\Doctrine\IdGenerator\UlidGenerator;
use Gedmo\Mapping\Annotation as Gedmo;
#[ORM\Entity(repositoryClass: SubscriptionRepository::class)]
#[Gedmo\SoftDeleteable(fieldName: 'deletedAt', timeAware: false, hardDelete: true)]
#[ORM\HasLifecycleCallbacks()]
#[ApiResource(
attributes: ["security" => "is_granted('IS_AUTHENTICATED_FULLY')"],
itemOperations: [
'get' => [
'normalization_context' => [
'groups' => [
'read:subscription:basic',
'read:service:basic',
'read:child:basic'
]
]
],
],
collectionOperations: [
'get' => [
'normalization_context' => [
'groups' => [
'read:subscription:basic',
'read:service:basic',
'read:child:basic'
]
]
],
],
)]
class Subscription
{
use Timestamps;
#[ORM\Id]
#[ORM\Column(type: 'ulid', unique: true)]
#[ORM\GeneratedValue(strategy: "CUSTOM")]
#[ORM\CustomIdGenerator(class: UlidGenerator::class)]
#[Groups(['read:subscription:basic'])]
private ?Ulid $id = null;
#[ORM\Column(type: 'date')]
#[Groups(['read:subscription:basic'])]
private $start_date;
#[ORM\Column(type: 'date')]
#[Groups(['read:subscription:basic'])]
private $expire_date;
#[ORM\Column(type: 'float')]
private $price = 0;
#[ORM\Column(type: 'integer')]
#[Groups(['read:subscription:basic'])]
private $quantity = 1;
#[ORM\Column(type: 'float')]
#[Groups(['read:subscription:basic'])]
private $total = 0;
#[ORM\Column(type: 'string', length: 255, enumType: SubscriptionStatus::class)]
#[Groups(['read:subscription:basic'])]
private $status;
#[ORM\Column(type: 'string', length: 255, enumType: SubscriptionPaymentStatus::class)]
#[Groups(['read:subscription:basic'])]
private $status_payment;
#[ORM\Column(type: 'float')]
#[Groups(['read:subscription:basic'])]
private $discount = 0;
#[ORM\Column(type: 'text', nullable: true)]
#[Groups(['read:subscription:basic'])]
private $description;
#[ORM\ManyToOne(targetEntity: Service::class, inversedBy: 'subscriptions')]
#[ORM\JoinColumn(nullable: false)]
#[Groups(['read:subscription:basic'])]
private $service;
#[ORM\ManyToOne(targetEntity: Child::class, inversedBy: 'subscriptions')]
#[ORM\JoinColumn(nullable: false)]
#[Groups(['read:subscription:basic'])]
private $child;
#[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'subscriptions')]
#[ORM\JoinColumn(nullable: false)]
private $created_by;
#[ORM\ManyToOne(targetEntity: Company::class, inversedBy: 'subscriptions')]
#[ORM\JoinColumn(nullable: false)]
private $owner;
#[ORM\OneToMany(mappedBy: 'subscription', targetEntity: Payment::class, orphanRemoval: true)]
#[Groups(['read:subscription:basic'])]
private $payments;
#[ORM\ManyToOne(targetEntity: Season::class, inversedBy: 'subscriptions')]
#[ORM\JoinColumn(nullable: false)]
private $season;
public function __construct()
{
$this->start_date = new DateTime();
$this->expire_date = new DateTime();
$this->status = SubscriptionStatus::DRAFT;
$this->status_payment = SubscriptionPaymentStatus::NOT_PAYED;
$this->payments = new ArrayCollection();
}
public function __toString()
{
return $this->child . " (" . $this->service->getName() . " " . $this->getRestToPay() . " DZD )";
}
public function getId(): ?Ulid
{
return $this->id;
}
#[Groups(['read:subscription:basic'])]
public function getTotalToPay(): ?float
{
return $this->total - $this->discount;
}
#[Groups(['read:subscription:basic'])]
public function getRestToPay(): ?float
{
$rest = $this->getTotalToPay();
foreach ($this->payments as $payment) {
$rest -= $payment->getAmount();
}
return $rest;
}
public function isPaied(): ?float
{
$rest = $this->getRestToPay();
if ($rest > 0)
return false;
return true;
}
#[Groups(['read:subscription:basic'])]
public function isExpired(): ?int
{
$now = new DateTime();
$diff = $now->diff($this->expire_date);
return $diff->invert;
}
public function getStartDate(): ?DateTimeInterface
{
return $this->start_date;
}
public function setStartDate(\DateTimeInterface $start_date): self
{
$this->start_date = $start_date;
return $this;
}
public function getExpireDate(): ?\DateTimeInterface
{
return $this->expire_date;
}
public function setExpireDate(\DateTimeInterface $expire_date): self
{
$this->expire_date = $expire_date;
return $this;
}
public function getPrice(): ?float
{
return $this->price;
}
public function setPrice(float $price): self
{
$this->price = $price;
return $this;
}
public function getQuantity(): ?int
{
return $this->quantity;
}
public function setQuantity(int $quantity): self
{
$this->quantity = $quantity;
return $this;
}
public function getTotal(): ?float
{
return $this->total;
}
public function setTotal(float $total): self
{
$this->total = $total;
return $this;
}
public function getStatus(): ?SubscriptionStatus
{
return $this->status;
}
public function setStatus(SubscriptionStatus $status): self
{
$this->status = $status;
return $this;
}
public function getStatusPayment(): ?SubscriptionPaymentStatus
{
return $this->status_payment;
}
public function setStatusPayment(SubscriptionPaymentStatus $status_payment): self
{
$this->status_payment = $status_payment;
return $this;
}
public function getDiscount(): ?float
{
return $this->discount;
}
public function setDiscount(float $discount): self
{
$this->discount = $discount;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(string $description): self
{
$this->description = $description;
return $this;
}
public function getService(): ?Service
{
return $this->service;
}
public function setService(?Service $service): self
{
$this->service = $service;
return $this;
}
public function getChild(): ?Child
{
return $this->child;
}
public function setChild(?Child $child): self
{
$this->child = $child;
return $this;
}
public function getCreatedBy(): ?User
{
return $this->created_by;
}
public function setCreatedBy(?User $created_by): self
{
$this->created_by = $created_by;
return $this;
}
public function getOwner(): ?Company
{
return $this->owner;
}
public function setOwner(?Company $owner): self
{
$this->owner = $owner;
return $this;
}
/**
* @return Collection<int, Payment>
*/
public function getPayments(): Collection
{
return $this->payments;
}
public function addPayment(Payment $payment): self
{
if (!$this->payments->contains($payment)) {
$this->payments[] = $payment;
$payment->setSubscription($this);
}
return $this;
}
public function removePayment(Payment $payment): self
{
if ($this->payments->removeElement($payment)) {
// set the owning side to null (unless already changed)
if ($payment->getSubscription() === $this) {
$payment->setSubscription(null);
}
}
return $this;
}
public function getSeason(): ?Season
{
return $this->season;
}
public function setSeason(?Season $season): self
{
$this->season = $season;
return $this;
}
}