src/Entity/VPN/Service/Service.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Entity\VPN\Service;
  3. use App\DependencyInjection\JDF;
  4. use App\Entity\BaseEntity;
  5. use App\Entity\Country;
  6. use App\Entity\Telegram\AgentPublicBot\Bot;
  7. use App\Entity\Telegram\AgentPublicBot\BotUser;
  8. use App\Entity\Transaction;
  9. use App\Entity\Generic\User;
  10. use App\Repository\VPN\Service\ServiceRepository;
  11. use DateTimeImmutable;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. use Doctrine\Common\Collections\Collection;
  14. use Doctrine\DBAL\Types\Types;
  15. use Doctrine\ORM\Mapping as ORM;
  16. use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
  17. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  18. #[ORM\Entity(repositoryClassServiceRepository::class)]
  19. class Service extends BaseEntity
  20. {
  21.     #[ORM\Id]
  22.     #[ORM\Column(type'guid'uniquetrue)]
  23.     #[ORM\GeneratedValue(strategy'CUSTOM')]
  24.     #[ORM\CustomIdGenerator(class: UuidGenerator::class)]
  25.     private ?string $id null;
  26.     #[ORM\Column(typeTypes::BIGINT)]
  27.     private ?string $clientId null;
  28.     #[ORM\ManyToOne(inversedBy'services')]
  29.     #[ORM\JoinColumn(nullablefalse)]
  30.     private ?User $owner null;
  31.     #[ORM\Column(length255)]
  32.     private ?string $username null;
  33.     #[ORM\Column(length255)]
  34.     private ?string $password null;
  35.     #[ORM\ManyToOne(inversedBy'services')]
  36.     #[ORM\JoinColumn(nullablefalse)]
  37.     private ?Plan $plan null;
  38.     #[ORM\Column(typeTypes::BIGINT)]
  39.     private ?string $total null;
  40.     #[ORM\Column(typeTypes::BIGINT)]
  41.     private ?string $used null;
  42.     #[ORM\Column(nullabletrue)]
  43.     private ?\DateTimeImmutable $expireAt null;
  44.     #[ORM\Column]
  45.     private ?bool $isEnabled null;
  46.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  47.     private ?string $note null;
  48.     #[ORM\Column(typeTypes::BIGINT)]
  49.     private ?string $totalPrice null;
  50.     #[ORM\Column(length255nullabletrue)]
  51.     private ?string $payMethod null;
  52.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  53.     private ?string $rollbackNote null;
  54.     #[ORM\Column(typeTypes::BIGINT)]
  55.     private ?string $ocServUsage null;
  56.     #[ORM\Column(typeTypes::BIGINT)]
  57.     private ?string $v2rayUsage null;
  58.     #[ORM\Column(typeTypes::BIGINT)]
  59.     private ?string $openVpnUsage null;
  60.     #[ORM\Column(length255nullabletrue)]
  61.     private ?string $serverNumber null;
  62.     #[ORM\Column(length255nullabletrue)]
  63.     private ?string $country null;
  64.     #[ORM\ManyToOne(inversedBy'services')]
  65.     private ?BotUser $botUser null;
  66.     #[ORM\ManyToOne(inversedBy'services')]
  67.     private ?Bot $creatorBot null;
  68.     #[ORM\Column]
  69.     private ?bool $sizeNotif null;
  70.     #[ORM\Column]
  71.     private ?bool $dayNotif null;
  72.     #[ORM\Column]
  73.     private ?bool $doneNotif null;
  74.     #[ORM\OneToMany(mappedBy'service'targetEntityServiceUsage::class)]
  75.     private Collection $serviceUsages;
  76.     public function __construct()
  77.     {
  78.         parent::__construct();
  79.         $this->setUsed(0);
  80.         $this->setIsEnabled(true);
  81.         $this->setOcServUsage(0);
  82.         $this->setOpenVpnUsage(0);
  83.         $this->setV2rayUsage(0);
  84.         $this->setServerNumber(random_int(50,150));
  85.         $this->setDoneNotif(0);
  86.         $this->setSizeNotif(0);
  87.         $this->setDayNotif(0);
  88.         $this->serviceUsages = new ArrayCollection();
  89.     }
  90.     public function getId(): ?string
  91.     {
  92.         return $this->id;
  93.     }
  94.     public function getClientId(): ?string
  95.     {
  96.         return $this->clientId;
  97.     }
  98.     public function setClientId(string $clientId): static
  99.     {
  100.         $this->clientId $clientId;
  101.         return $this;
  102.     }
  103.     public function getOwner(): ?User
  104.     {
  105.         return $this->owner;
  106.     }
  107.     public function setOwner(?User $owner): static
  108.     {
  109.         $this->owner $owner;
  110.         return $this;
  111.     }
  112.     public function getUsername(): ?string
  113.     {
  114.         return $this->username;
  115.     }
  116.     public function setUsername(string $username): static
  117.     {
  118.         $this->username $username;
  119.         return $this;
  120.     }
  121.     public function getPassword(): ?string
  122.     {
  123.         return $this->password;
  124.     }
  125.     public function setPassword(string $password): static
  126.     {
  127.         $this->password $password;
  128.         return $this;
  129.     }
  130.     public function getPlan(): ?Plan
  131.     {
  132.         return $this->plan;
  133.     }
  134.     public function setPlan(?Plan $plan): static
  135.     {
  136.         $this->plan $plan;
  137.         return $this;
  138.     }
  139.     public function getTotal(): ?string
  140.     {
  141.         return $this->total;
  142.     }
  143.     public function setTotal(string $total): static
  144.     {
  145.         $this->total $total;
  146.         return $this;
  147.     }
  148.     public function getUsed(): ?string
  149.     {
  150.         return $this->used;
  151.     }
  152.     public function setUsed(string $used): static
  153.     {
  154.         $this->used $used;
  155.         return $this;
  156.     }
  157.     public function getExpireAt(): ?\DateTimeImmutable
  158.     {
  159.         return $this->expireAt;
  160.     }
  161.     public function setExpireAt(?\DateTimeImmutable $expireAt): static
  162.     {
  163.         $this->expireAt $expireAt;
  164.         return $this;
  165.     }
  166.     public function isEnabled(): ?bool
  167.     {
  168.         return $this->isEnabled;
  169.     }
  170.     public function setIsEnabled(bool $isEnabled): static
  171.     {
  172.         $this->isEnabled $isEnabled;
  173.         return $this;
  174.     }
  175.     public function getNote(): ?string
  176.     {
  177.         return $this->note;
  178.     }
  179.     public function setNote(?string $note): static
  180.     {
  181.         $this->note $note;
  182.         return $this;
  183.     }
  184.     public function getTotalPrice(): ?string
  185.     {
  186.         return $this->totalPrice;
  187.     }
  188.     public function setTotalPrice(string $totalPrice): static
  189.     {
  190.         $this->totalPrice $totalPrice;
  191.         return $this;
  192.     }
  193.     public function getPayMethod(): ?string
  194.     {
  195.         return $this->payMethod;
  196.     }
  197.     public function setPayMethod(?string $payMethod): static
  198.     {
  199.         $this->payMethod $payMethod;
  200.         return $this;
  201.     }
  202.     public function v1ListService()
  203.     {
  204.         $now = new \DateTime();
  205.         if ($this->getExpireAt()){
  206.             $expireTime $this->getExpireAt();
  207.         }else{
  208.            $expireTime $now->modify('+1Year');
  209.         }
  210.         $interval $expireTime->diff($now);
  211.         if ($interval->days && $interval->24) {
  212.             $isLow true;
  213.         } else {
  214.             $isLow false;
  215.         }
  216.         if (!$this->isActive()){
  217.             $css 'text-theme-6';
  218.             $statusText 'غیر فعال - مرجوع شده';
  219.         }elseif ($this->isEnabled()){
  220.             $css =  'text-theme-9';
  221.             $statusText 'فعال';
  222.         }else{
  223.             $css 'text-theme-6';
  224.             $statusText 'غیر فعال';
  225.         }
  226.         $planTitle '';
  227.         $planTitle $planTitle $this->getPlan()->getDays() . ' روز ' '- ';
  228.         $planTitle $planTitle 'نامحدود کاربر';
  229.         $fullText '<a href="#" class="font-medium whitespace-nowrap  ' $css '">';
  230.         $fullText $fullText $planTitle .$statusText.'</a>';
  231.         $fullText $fullText '
  232.             <div class="text-gray-600 text-xs whitespace-nowrap mt-0.5" >
  233.             مالک : 
  234.                                         '.
  235.             $this->getOwner().'
  236.                                         <br>';
  237.         $fullText $fullText "
  238.                 از
  239.                 {$this->pDateFilter($this->getCreatedAt())}
  240.                 تا
  241.                 {$this->pDateFilter($this->getExpireAt())}
  242.                 ".'</div >';
  243.         $fullText $fullText "
  244.                 مصرف
  245.                 {$this->formatSizeUnits$this->getUsed())}
  246.                 از
  247.                 {$this->formatSizeUnits$this->getTotal())}
  248.                 ";
  249.         $fullText $fullText.    '</div >';
  250.         $fullText $fullText.'<br>'.'<a class="btn mt-1 btn-sm btn-primary" href="/v2/no-pass/'$this->getId() . '">' 'دریافت کانفیگ' '</a>';
  251.         return $fullText;
  252.     }
  253.     public function v2CommentService()
  254.     {
  255.         $fullText '<div class=" text-xs whitespace-nowrap mt-0.5">' $this->getMobile() . '</div>';
  256.         return $fullText;
  257.     }
  258.     public function v2InfoService()
  259.     {
  260.         $fullText '<div class=" text-xs whitespace-nowrap mt-0.5">
  261.                 نام : 
  262.                   ' $this->getUsername() . '
  263.             <br>
  264.             شناسه سرویس:
  265.             ' $this->getClientId() . '
  266.             <br>
  267.                                         کلمه عبور : 
  268.                                         ' .
  269.             $this->getPassword() . ' <br> 
  270.                                        متن آماده ارسال مشتری :‌ '  .
  271.             '
  272.                                         </div>';
  273. //        foreach ($this->getOcServServerServiceConnections() as $ocServServerServiceConnection) {
  274. //            if ($ocServServerServiceConnection->getServer() && $ocServServerServiceConnection->getServer()){
  275. //                $fullText = $fullText.'<br>'.'سرور :‌'.$ocServServerServiceConnection->getServer()->getTunnelIpOrDomain().'<br>';
  276. //            }
  277. //        }
  278.         $fullText $fullText '<button onclick="copy(';
  279.         $fullText $fullText "'";
  280.         $fullText $fullText $this->generateGuideText();
  281.         $fullText $fullText "'";
  282.         $fullText $fullText ')" class="btn mt-1 btn-sm btn-primary">کپی متن توضیحات</button>';
  283.         return $fullText;
  284.     }
  285.     public function pDateFilter($date)
  286.     {
  287.         if (!$date) return null;
  288.         date_default_timezone_set('Asia/Tehran');
  289.         $jdate = new JDF();
  290.         $gDate $date->format('Y-m-d');
  291. //        return $gDate;
  292.         $gDate explode('-'$gDate);
  293.         return $jdate->gregorianToJalali($gDate[0], $gDate[1], $gDate[2], '/');
  294. //        return $this->tr_num2($this->tr_num2($jdate->toJalali2($gDate[0], $gDate[1], $gDate[2]), 'fa'), 'fa');
  295.     }
  296.     public function getDaysLeft():int
  297.     {
  298.         $date1 = new DateTimeImmutable();
  299.         $date2 $this->getExpireAt();
  300.         if ($date2 $date1){
  301.             $diff $date2->diff($date1)->format("%a");
  302.             return intval($diff);
  303.         }else{
  304.             return  0;
  305.         }
  306.     }
  307.     public function getTrafficLeft():string
  308.     {
  309.         if ($this->getUsed() < $this->getTotal()){
  310.             $left $this->getTotal() - $this->getUsed();
  311.             return $this->readableSize($left);
  312.         }else{
  313.             return '0MB' ;
  314.         }
  315.     }
  316.     public function readableSize($bytes): string
  317.     {
  318.         if ($bytes >= 1073741824) {
  319.             $bytes number_format($bytes 10737418242) . ' GB';
  320.         } elseif ($bytes >= 1048576) {
  321.             $bytes number_format($bytes 10485762) . ' MB';
  322.         } elseif ($bytes >= 1024) {
  323.             $bytes number_format($bytes 10242) . ' KB';
  324.         } elseif ($bytes 1) {
  325.             $bytes $bytes ' bytes';
  326.         } elseif ($bytes == 1) {
  327.             $bytes $bytes ' byte';
  328.         } else {
  329.             $bytes '0 bytes';
  330.         }
  331.         return $bytes;
  332.     }
  333.     public function formatSizeUnits($bytes): string
  334.     {
  335.         if ($bytes >= 1073741824) {
  336.             $bytes number_format($bytes 10737418242) . ' GB';
  337.         } elseif ($bytes >= 1048576) {
  338.             $bytes number_format($bytes 10485762) . ' MB';
  339.         } elseif ($bytes >= 1024) {
  340.             $bytes number_format($bytes 10242) . ' KB';
  341.         } elseif ($bytes 1) {
  342.             $bytes $bytes ' bytes';
  343.         } elseif ($bytes == 1) {
  344.             $bytes $bytes ' byte';
  345.         } else {
  346.             $bytes '0 bytes';
  347.         }
  348.         return $bytes;
  349.     }
  350.     function convertToBytes(string $from): ?int
  351.     {
  352.         $units = ['B''KB''MB''GB''TB''PB'];
  353.         $number substr($from0, -2);
  354.         $suffix strtoupper(substr($from, -2));
  355.         if (is_numeric(substr($suffix01))) {
  356.             return preg_replace('/[^\d]/'''$from);
  357.         }
  358.         $exponent array_flip($units)[$suffix] ?? null;
  359.         if ($exponent === null) {
  360.             return null;
  361.         }
  362.         return $number * (1024 ** $exponent);
  363.     }
  364.     public function generateGuideText()
  365.     {
  366.         $text 'ضمن تشکر از خرید شما دوست عزیز💙'.
  367.             '\n'.
  368.             'برای دریافت آمورش - اپلیکیشن و کانفیگ خود میتوانید با ورود به لینک زیر و وارد کردن شناسه سرویس و کلمه عبور نسبت به دریافت آن اقدام فرمایید...'.
  369.             '\n'.
  370.             '\n'.
  371.             'شناسه سرویس  : '.$this->getClientId().
  372.             '\n'.
  373.             'کلمه عبور : '.$this->getPassword().
  374.             '\n'.
  375.             'حجم خریداری شده : '.$this->formatSizeUnits$this->getPlan()->getSize()).
  376.             '\n'.
  377.             'اتصال دو کاربر همزمان'.
  378.             '\n';
  379.         ;
  380.         return $text;
  381.     }
  382.     public function getRollbackNote(): ?string
  383.     {
  384.         return $this->rollbackNote;
  385.     }
  386.     public function setRollbackNote(?string $rollbackNote): static
  387.     {
  388.         $this->rollbackNote $rollbackNote;
  389.         return $this;
  390.     }
  391.     public function getOcServUsage(): ?string
  392.     {
  393.         return $this->ocServUsage;
  394.     }
  395.     public function setOcServUsage(string $ocServUsage): static
  396.     {
  397.         $this->ocServUsage $ocServUsage;
  398.         return $this;
  399.     }
  400.     public function getV2rayUsage(): ?string
  401.     {
  402.         return $this->v2rayUsage;
  403.     }
  404.     public function setV2rayUsage(string $v2rayUsage): static
  405.     {
  406.         $this->v2rayUsage $v2rayUsage;
  407.         return $this;
  408.     }
  409.     public function getOpenVpnUsage(): ?string
  410.     {
  411.         return $this->openVpnUsage;
  412.     }
  413.     public function setOpenVpnUsage(string $openVpnUsage): static
  414.     {
  415.         $this->openVpnUsage $openVpnUsage;
  416.         return $this;
  417.     }
  418.     public function getServerNumber(): ?string
  419.     {
  420.         return $this->serverNumber;
  421.     }
  422.     public function setServerNumber(?string $serverNumber): static
  423.     {
  424.         $this->serverNumber $serverNumber;
  425.         return $this;
  426.     }
  427.     public function getCountry(): ?string
  428.     {
  429.         return $this->country;
  430.     }
  431.     public function setCountry(?string $country): static
  432.     {
  433.         $this->country $country;
  434.         return $this;
  435.     }
  436.     public function getBotUser(): ?BotUser
  437.     {
  438.         return $this->botUser;
  439.     }
  440.     public function setBotUser(?BotUser $botUser): static
  441.     {
  442.         $this->botUser $botUser;
  443.         return $this;
  444.     }
  445.     public function getCreatorBot(): ?Bot
  446.     {
  447.         return $this->creatorBot;
  448.     }
  449.     public function setCreatorBot(?Bot $creatorBot): static
  450.     {
  451.         $this->creatorBot $creatorBot;
  452.         return $this;
  453.     }
  454.     public function isSizeNotif(): ?bool
  455.     {
  456.         return $this->sizeNotif;
  457.     }
  458.     public function setSizeNotif(bool $sizeNotif): static
  459.     {
  460.         $this->sizeNotif $sizeNotif;
  461.         return $this;
  462.     }
  463.     public function isDayNotif(): ?bool
  464.     {
  465.         return $this->dayNotif;
  466.     }
  467.     public function setDayNotif(bool $dayNotif): static
  468.     {
  469.         $this->dayNotif $dayNotif;
  470.         return $this;
  471.     }
  472.     public function isDoneNotif(): ?bool
  473.     {
  474.         return $this->doneNotif;
  475.     }
  476.     public function setDoneNotif(bool $doneNotif): static
  477.     {
  478.         $this->doneNotif $doneNotif;
  479.         return $this;
  480.     }
  481.     /**
  482.      * @return Collection<int, ServiceUsage>
  483.      */
  484.     public function getServiceUsages(): Collection
  485.     {
  486.         return $this->serviceUsages;
  487.     }
  488.     public function addServiceUsage(ServiceUsage $serviceUsage): static
  489.     {
  490.         if (!$this->serviceUsages->contains($serviceUsage)) {
  491.             $this->serviceUsages->add($serviceUsage);
  492.             $serviceUsage->setService($this);
  493.         }
  494.         return $this;
  495.     }
  496.     public function removeServiceUsage(ServiceUsage $serviceUsage): static
  497.     {
  498.         if ($this->serviceUsages->removeElement($serviceUsage)) {
  499.             // set the owning side to null (unless already changed)
  500.             if ($serviceUsage->getService() === $this) {
  501.                 $serviceUsage->setService(null);
  502.             }
  503.         }
  504.         return $this;
  505.     }
  506. }