<?php
namespace App\Entity;
use App\Constants\ActiveConstants;
use App\Constants\OrderStatusConstants;
use App\Constants\PageTypeConstant;
use App\Repository\PhoneNumberRepository;
use App\Traits\ActivityTrait;
use App\Traits\TimeTrackTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: PhoneNumberRepository::class)]
#[ORM\HasLifecycleCallbacks]
#[ORM\UniqueConstraint(columns: ['number'])]
class PhoneNumber
{
use TimeTrackTrait;
use ActivityTrait;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[Assert\NotNull]
#[Assert\NotBlank]
#[ORM\Column(length: 191)]
private ?string $number = null;
#[Assert\NotNull]
#[Assert\NotBlank]
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $iccsd = null;
#[Assert\NotNull]
#[Assert\NotBlank]
#[ORM\Column(length: 191, nullable: true)]
private ?string $pin = null;
private bool $lockedByUser = false;
#[Assert\NotNull]
#[Assert\NotBlank]
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $qrUrl = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?\DateTimeInterface $unfreezeTime = null;
#[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'lockedPhones')]
#[ORM\JoinColumn(name: 'id_locked_user', nullable: true, onDelete: 'SET NULL')]
private ?User $lockedUser = null;
#[Assert\NotNull]
#[Assert\NotBlank]
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?\DateTimeInterface $expireDate = null;
#[Assert\NotNull]
#[ORM\ManyToOne(inversedBy: 'phoneNumbers')]
#[ORM\JoinColumn(nullable: false)]
private ?Operator $operator = null;
#[ORM\ManyToOne(inversedBy: 'phoneNumbers')]
private ?User $user = null;
#[ORM\OneToMany(mappedBy: 'phoneNumber', targetEntity: Order::class)]
private $orders;
#[Assert\NotNull(message: 'Тариф не знайдено')]
#[ORM\ManyToOne(targetEntity: Tariff::class, inversedBy: 'phoneNumbers')]
#[ORM\JoinColumn(nullable: true)]
private ?Tariff $tariff = null;
public function __construct()
{
$this->orders = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getNumber(): ?string
{
return $this->number;
}
public function setNumber(string $number): static
{
$this->number = $number;
return $this;
}
public function getBalance(): ?float
{
return $this->balance;
}
public function setBalance(float $balance): static
{
$this->balance = $balance;
return $this;
}
public function getUnfreezeTime(): ?\DateTimeInterface
{
return $this->unfreezeTime;
}
public function setUnfreezeTime(?\DateTimeInterface $unfreezeTime): static
{
$this->unfreezeTime = $unfreezeTime;
return $this;
}
public function lockPhoneNumber(User $user, $period = PageTypeConstant::LOCK_PHONE_NUMBER_PERIOD): static
{
$now = new \DateTime();
$this->setLockedUser($user);
$now->modify("+{$period} minutes");
$this->setUnfreezeTime($now);
return $this;
}
public function getOperator(): ?Operator
{
return $this->operator;
}
public function setOperator(?Operator $operator): static
{
$this->operator = $operator;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): static
{
$this->user = $user;
return $this;
}
/**
* Set createdAt.
*
* @param \DateTime|null $createdAt
*
* @return PhoneNumber
*/
public function setCreatedAt($createdAt = null)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt.
*
* @return \DateTime|null
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set updatedAt.
*
* @param \DateTime|null $updatedAt
*
* @return PhoneNumber
*/
public function setUpdatedAt($updatedAt = null)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt.
*
* @return \DateTime|null
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
public function getTariff(): ?Tariff
{
return $this->tariff;
}
public function setTariff(?Tariff $tariff): static
{
$this->tariff = $tariff;
return $this;
}
public function getPin(): ?string
{
return $this->pin;
}
public function setPin(string $pin): static
{
$this->pin = $pin;
return $this;
}
public function getExpireDate(): ?\DateTimeInterface
{
return $this->expireDate;
}
public function setExpireDate(?\DateTimeInterface $expireDate): static
{
$this->expireDate = $expireDate;
return $this;
}
public function getQrUrl(): ?string
{
return $this->qrUrl;
}
public function setQrUrl(string $qr_url): static
{
$this->qrUrl = $qr_url;
return $this;
}
/**
* Set active.
*
* @param int $active
*
* @return PhoneNumber
*/
public function setActive($active)
{
$this->active = $active;
return $this;
}
/**
* Get active.
*
* @return int
*/
public function getActive()
{
return $this->active;
}
public function getActiveLabel(): string
{
return $this->active ? ActiveConstants::LABEL_YES : ActiveConstants::LABEL_NO;
}
public function getLockedByUserLabel(): string
{
return $this->getLockedByUser() ? ActiveConstants::LABEL_YES : ActiveConstants::LABEL_NO;
}
public function __toString()
{
return $this->getNumber();
}
public function getLockedUser(): ?User
{
return $this->lockedUser;
}
public function setLockedUser(?User $lockedUser): static
{
$this->lockedUser = $lockedUser;
return $this;
}
/**
* @return Collection<int, Order>
*/
public function getOrders(): Collection
{
return $this->orders;
}
public function addOrder(Order $order): static
{
if (!$this->orders->contains($order)) {
$this->orders->add($order);
$order->setPhoneNumber($this);
}
return $this;
}
public function removeOrder(Order $order): static
{
if ($this->orders->removeElement($order)) {
// set the owning side to null (unless already changed)
if ($order->getPhoneNumber() === $this) {
$order->setPhoneNumber(null);
}
}
return $this;
}
public function getIccsd(): ?string
{
return $this->iccsd;
}
public function getLockedByUser(): bool
{
$now = new \DateTime();
if ($now < $this->getUnfreezeTime()) {
return true;
}
return false;
}
public function setIccsd(string $iccsd): static
{
$this->iccsd = $iccsd;
return $this;
}
public function getOrderPayed()
{
foreach ($this->getOrders() as $order) {
if ($order->getStatus() == OrderStatusConstants::PAYED) {
return $order;
}
}
return null;
}
public function getUserPayed()
{
foreach ($this->getOrders() as $order) {
if ($order->getStatus() == OrderStatusConstants::PAYED) {
return $order->getUser();
}
}
return null;
}
public function getPrice(): ?float
{
return $this->price;
}
public function setPrice(?float $price): static
{
$this->price = $price;
return $this;
}
}