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

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