<?php
namespace App\Entity\VPN\Service;
use App\DependencyInjection\JDF;
use App\Entity\BaseEntity;
use App\Entity\Country;
use App\Entity\Telegram\AgentPublicBot\Bot;
use App\Entity\Telegram\AgentPublicBot\BotUser;
use App\Entity\Transaction;
use App\Entity\Generic\User;
use App\Repository\VPN\Service\ServiceRepository;
use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
#[ORM\Entity(repositoryClass: ServiceRepository::class)]
class Service extends BaseEntity
{
#[ORM\Id]
#[ORM\Column(type: 'guid', unique: true)]
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
#[ORM\CustomIdGenerator(class: UuidGenerator::class)]
private ?string $id = null;
#[ORM\Column(type: Types::BIGINT)]
private ?string $clientId = null;
#[ORM\ManyToOne(inversedBy: 'services')]
#[ORM\JoinColumn(nullable: false)]
private ?User $owner = null;
#[ORM\Column(length: 255)]
private ?string $username = null;
#[ORM\Column(length: 255)]
private ?string $password = null;
#[ORM\ManyToOne(inversedBy: 'services')]
#[ORM\JoinColumn(nullable: false)]
private ?Plan $plan = null;
#[ORM\Column(type: Types::BIGINT)]
private ?string $total = null;
#[ORM\Column(type: Types::BIGINT)]
private ?string $used = null;
#[ORM\Column(nullable: true)]
private ?\DateTimeImmutable $expireAt = null;
#[ORM\Column]
private ?bool $isEnabled = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $note = null;
#[ORM\Column(type: Types::BIGINT)]
private ?string $totalPrice = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $payMethod = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $rollbackNote = null;
#[ORM\Column(type: Types::BIGINT)]
private ?string $ocServUsage = null;
#[ORM\Column(type: Types::BIGINT)]
private ?string $v2rayUsage = null;
#[ORM\Column(type: Types::BIGINT)]
private ?string $openVpnUsage = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $serverNumber = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $country = null;
#[ORM\ManyToOne(inversedBy: 'services')]
private ?BotUser $botUser = null;
#[ORM\ManyToOne(inversedBy: 'services')]
private ?Bot $creatorBot = null;
#[ORM\Column]
private ?bool $sizeNotif = null;
#[ORM\Column]
private ?bool $dayNotif = null;
#[ORM\Column]
private ?bool $doneNotif = null;
#[ORM\OneToMany(mappedBy: 'service', targetEntity: ServiceUsage::class)]
private Collection $serviceUsages;
public function __construct()
{
parent::__construct();
$this->setUsed(0);
$this->setIsEnabled(true);
$this->setOcServUsage(0);
$this->setOpenVpnUsage(0);
$this->setV2rayUsage(0);
$this->setServerNumber(random_int(50,150));
$this->setDoneNotif(0);
$this->setSizeNotif(0);
$this->setDayNotif(0);
$this->serviceUsages = new ArrayCollection();
}
public function getId(): ?string
{
return $this->id;
}
public function getClientId(): ?string
{
return $this->clientId;
}
public function setClientId(string $clientId): static
{
$this->clientId = $clientId;
return $this;
}
public function getOwner(): ?User
{
return $this->owner;
}
public function setOwner(?User $owner): static
{
$this->owner = $owner;
return $this;
}
public function getUsername(): ?string
{
return $this->username;
}
public function setUsername(string $username): static
{
$this->username = $username;
return $this;
}
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(string $password): static
{
$this->password = $password;
return $this;
}
public function getPlan(): ?Plan
{
return $this->plan;
}
public function setPlan(?Plan $plan): static
{
$this->plan = $plan;
return $this;
}
public function getTotal(): ?string
{
return $this->total;
}
public function setTotal(string $total): static
{
$this->total = $total;
return $this;
}
public function getUsed(): ?string
{
return $this->used;
}
public function setUsed(string $used): static
{
$this->used = $used;
return $this;
}
public function getExpireAt(): ?\DateTimeImmutable
{
return $this->expireAt;
}
public function setExpireAt(?\DateTimeImmutable $expireAt): static
{
$this->expireAt = $expireAt;
return $this;
}
public function isEnabled(): ?bool
{
return $this->isEnabled;
}
public function setIsEnabled(bool $isEnabled): static
{
$this->isEnabled = $isEnabled;
return $this;
}
public function getNote(): ?string
{
return $this->note;
}
public function setNote(?string $note): static
{
$this->note = $note;
return $this;
}
public function getTotalPrice(): ?string
{
return $this->totalPrice;
}
public function setTotalPrice(string $totalPrice): static
{
$this->totalPrice = $totalPrice;
return $this;
}
public function getPayMethod(): ?string
{
return $this->payMethod;
}
public function setPayMethod(?string $payMethod): static
{
$this->payMethod = $payMethod;
return $this;
}
public function v1ListService()
{
$now = new \DateTime();
if ($this->getExpireAt()){
$expireTime = $this->getExpireAt();
}else{
$expireTime = $now->modify('+1Year');
}
$interval = $expireTime->diff($now);
if ($interval->days < 1 && $interval->h < 24) {
$isLow = true;
} else {
$isLow = false;
}
if (!$this->isActive()){
$css = 'text-theme-6';
$statusText = 'غیر فعال - مرجوع شده';
}elseif ($this->isEnabled()){
$css = 'text-theme-9';
$statusText = 'فعال';
}else{
$css = 'text-theme-6';
$statusText = 'غیر فعال';
}
$planTitle = '';
$planTitle = $planTitle . $this->getPlan()->getDays() . ' روز ' . '- ';
$planTitle = $planTitle . 'نامحدود کاربر';
$fullText = '<a href="#" class="font-medium whitespace-nowrap ' . $css . '">';
$fullText = $fullText . $planTitle .$statusText.'</a>';
$fullText = $fullText . '
<div class="text-gray-600 text-xs whitespace-nowrap mt-0.5" >
مالک :
'.
$this->getOwner().'
<br>';
$fullText = $fullText . "
از
{$this->pDateFilter($this->getCreatedAt())}
تا
{$this->pDateFilter($this->getExpireAt())}
".'</div >';
$fullText = $fullText . "
مصرف
{$this->formatSizeUnits( $this->getUsed())}
از
{$this->formatSizeUnits( $this->getTotal())}
";
$fullText = $fullText. '</div >';
$fullText = $fullText.'<br>'.'<a class="btn mt-1 btn-sm btn-primary" href="/v2/no-pass/'. $this->getId() . '">' . 'دریافت کانفیگ' . '</a>';
return $fullText;
}
public function v2CommentService()
{
$fullText = '<div class=" text-xs whitespace-nowrap mt-0.5">' . $this->getMobile() . '</div>';
return $fullText;
}
public function v2InfoService()
{
$fullText = '<div class=" text-xs whitespace-nowrap mt-0.5">
نام :
' . $this->getUsername() . '
<br>
شناسه سرویس:
' . $this->getClientId() . '
<br>
کلمه عبور :
' .
$this->getPassword() . ' <br>
متن آماده ارسال مشتری : ' .
'
</div>';
// foreach ($this->getOcServServerServiceConnections() as $ocServServerServiceConnection) {
// if ($ocServServerServiceConnection->getServer() && $ocServServerServiceConnection->getServer()){
// $fullText = $fullText.'<br>'.'سرور :'.$ocServServerServiceConnection->getServer()->getTunnelIpOrDomain().'<br>';
// }
// }
$fullText = $fullText . '<button onclick="copy(';
$fullText = $fullText . "'";
$fullText = $fullText . $this->generateGuideText();
$fullText = $fullText . "'";
$fullText = $fullText . ')" class="btn mt-1 btn-sm btn-primary">کپی متن توضیحات</button>';
return $fullText;
}
public function pDateFilter($date)
{
if (!$date) return null;
date_default_timezone_set('Asia/Tehran');
$jdate = new JDF();
$gDate = $date->format('Y-m-d');
// return $gDate;
$gDate = explode('-', $gDate);
return $jdate->gregorianToJalali($gDate[0], $gDate[1], $gDate[2], '/');
// return $this->tr_num2($this->tr_num2($jdate->toJalali2($gDate[0], $gDate[1], $gDate[2]), 'fa'), 'fa');
}
public function getDaysLeft():int
{
$date1 = new DateTimeImmutable();
$date2 = $this->getExpireAt();
if ($date2 > $date1){
$diff = $date2->diff($date1)->format("%a");
return intval($diff);
}else{
return 0;
}
}
public function getTrafficLeft():string
{
if ($this->getUsed() < $this->getTotal()){
$left = $this->getTotal() - $this->getUsed();
return $this->readableSize($left);
}else{
return '0MB' ;
}
}
public function readableSize($bytes): string
{
if ($bytes >= 1073741824) {
$bytes = number_format($bytes / 1073741824, 2) . ' GB';
} elseif ($bytes >= 1048576) {
$bytes = number_format($bytes / 1048576, 2) . ' MB';
} elseif ($bytes >= 1024) {
$bytes = number_format($bytes / 1024, 2) . ' KB';
} elseif ($bytes > 1) {
$bytes = $bytes . ' bytes';
} elseif ($bytes == 1) {
$bytes = $bytes . ' byte';
} else {
$bytes = '0 bytes';
}
return $bytes;
}
public function formatSizeUnits($bytes): string
{
if ($bytes >= 1073741824) {
$bytes = number_format($bytes / 1073741824, 2) . ' GB';
} elseif ($bytes >= 1048576) {
$bytes = number_format($bytes / 1048576, 2) . ' MB';
} elseif ($bytes >= 1024) {
$bytes = number_format($bytes / 1024, 2) . ' KB';
} elseif ($bytes > 1) {
$bytes = $bytes . ' bytes';
} elseif ($bytes == 1) {
$bytes = $bytes . ' byte';
} else {
$bytes = '0 bytes';
}
return $bytes;
}
function convertToBytes(string $from): ?int
{
$units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
$number = substr($from, 0, -2);
$suffix = strtoupper(substr($from, -2));
if (is_numeric(substr($suffix, 0, 1))) {
return preg_replace('/[^\d]/', '', $from);
}
$exponent = array_flip($units)[$suffix] ?? null;
if ($exponent === null) {
return null;
}
return $number * (1024 ** $exponent);
}
public function generateGuideText()
{
$text = 'ضمن تشکر از خرید شما دوست عزیز💙'.
'\n'.
'برای دریافت آمورش - اپلیکیشن و کانفیگ خود میتوانید با ورود به لینک زیر و وارد کردن شناسه سرویس و کلمه عبور نسبت به دریافت آن اقدام فرمایید...'.
'\n'.
'\n'.
'شناسه سرویس : '.$this->getClientId().
'\n'.
'کلمه عبور : '.$this->getPassword().
'\n'.
'حجم خریداری شده : '.$this->formatSizeUnits( $this->getPlan()->getSize()).
'\n'.
'اتصال دو کاربر همزمان'.
'\n';
;
return $text;
}
public function getRollbackNote(): ?string
{
return $this->rollbackNote;
}
public function setRollbackNote(?string $rollbackNote): static
{
$this->rollbackNote = $rollbackNote;
return $this;
}
public function getOcServUsage(): ?string
{
return $this->ocServUsage;
}
public function setOcServUsage(string $ocServUsage): static
{
$this->ocServUsage = $ocServUsage;
return $this;
}
public function getV2rayUsage(): ?string
{
return $this->v2rayUsage;
}
public function setV2rayUsage(string $v2rayUsage): static
{
$this->v2rayUsage = $v2rayUsage;
return $this;
}
public function getOpenVpnUsage(): ?string
{
return $this->openVpnUsage;
}
public function setOpenVpnUsage(string $openVpnUsage): static
{
$this->openVpnUsage = $openVpnUsage;
return $this;
}
public function getServerNumber(): ?string
{
return $this->serverNumber;
}
public function setServerNumber(?string $serverNumber): static
{
$this->serverNumber = $serverNumber;
return $this;
}
public function getCountry(): ?string
{
return $this->country;
}
public function setCountry(?string $country): static
{
$this->country = $country;
return $this;
}
public function getBotUser(): ?BotUser
{
return $this->botUser;
}
public function setBotUser(?BotUser $botUser): static
{
$this->botUser = $botUser;
return $this;
}
public function getCreatorBot(): ?Bot
{
return $this->creatorBot;
}
public function setCreatorBot(?Bot $creatorBot): static
{
$this->creatorBot = $creatorBot;
return $this;
}
public function isSizeNotif(): ?bool
{
return $this->sizeNotif;
}
public function setSizeNotif(bool $sizeNotif): static
{
$this->sizeNotif = $sizeNotif;
return $this;
}
public function isDayNotif(): ?bool
{
return $this->dayNotif;
}
public function setDayNotif(bool $dayNotif): static
{
$this->dayNotif = $dayNotif;
return $this;
}
public function isDoneNotif(): ?bool
{
return $this->doneNotif;
}
public function setDoneNotif(bool $doneNotif): static
{
$this->doneNotif = $doneNotif;
return $this;
}
/**
* @return Collection<int, ServiceUsage>
*/
public function getServiceUsages(): Collection
{
return $this->serviceUsages;
}
public function addServiceUsage(ServiceUsage $serviceUsage): static
{
if (!$this->serviceUsages->contains($serviceUsage)) {
$this->serviceUsages->add($serviceUsage);
$serviceUsage->setService($this);
}
return $this;
}
public function removeServiceUsage(ServiceUsage $serviceUsage): static
{
if ($this->serviceUsages->removeElement($serviceUsage)) {
// set the owning side to null (unless already changed)
if ($serviceUsage->getService() === $this) {
$serviceUsage->setService(null);
}
}
return $this;
}
}