HostingService.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS Product developed. (27.03.19)
  4. * *
  5. *
  6. * CREATED BY MODULESGARDEN -> http://modulesgarden.com
  7. * CONTACT -> contact@modulesgarden.com
  8. *
  9. *
  10. * This software is furnished under a license and may be used and copied
  11. * only in accordance with the terms of such license and with the
  12. * inclusion of the above copyright notice. This software or any other
  13. * copies thereof may not be provided or otherwise made available to any
  14. * other person. No title to and ownership of the software is hereby
  15. * transferred.
  16. *
  17. *
  18. * ******************************************************************** */
  19. namespace ModulesGarden\ProxmoxAddon\App\Services\Vps;
  20. use ModulesGarden\ProxmoxAddon\App\Models\Whmcs\CustomField;
  21. use ModulesGarden\ProxmoxAddon\App\Models\Whmcs\CustomFieldValue;
  22. use ModulesGarden\ProxmoxAddon\App\Models\Whmcs\Hosting;
  23. use ModulesGarden\ProxmoxAddon\App\Services\Utility;
  24. use ModulesGarden\ProxmoxAddon\App\Enum\Vps\ConfigurableOption;
  25. trait HostingService
  26. {
  27. /**
  28. * @return mixed
  29. */
  30. public function getHostingId()
  31. {
  32. if (!$this->hostingId)
  33. {
  34. $this->hostingId = $this->getWhmcsParamByKey("serviceid");
  35. }
  36. return $this->hostingId;
  37. }
  38. /**
  39. * @param mixed $hostingId
  40. */
  41. public function setHostingId($hostingId)
  42. {
  43. $this->hostingId = $hostingId;
  44. return $this;
  45. }
  46. /**
  47. * @return Hosting
  48. */
  49. public function hosting()
  50. {
  51. if ($this->hosting instanceof Hosting)
  52. {
  53. return $this->hosting;
  54. }
  55. return $this->hosting = Hosting::where("id", $this->getHostingId())->firstOrFail();
  56. }
  57. public function isActive()
  58. {
  59. return Hosting::ofId($this->getHostingId())->active()->count() == 1;
  60. }
  61. public function isSupportedModule()
  62. {
  63. return Hosting::ofServerType($this->getHostingId(), "proxmoxVPS")->count() == 1;
  64. }
  65. private function getCustomFieldId($fieldName)
  66. {
  67. return CustomField::select("id")
  68. ->where("type", "product")
  69. ->where("relid", $this->hosting()->packageid)
  70. ->where("fieldname", "like", $fieldName . "%")
  71. ->value("id");
  72. }
  73. public function customFieldUpdate($name, $value = '')
  74. {
  75. $fieldId = $this->getCustomFieldId($name);
  76. //Update
  77. if (CustomFieldValue::where('fieldid', $fieldId)->where("relid", $this->getHostingId())->count())
  78. {
  79. return CustomFieldValue::where('fieldid', $fieldId)->where("relid", $this->getHostingId())->update(['value' => $value]);
  80. }
  81. //Create
  82. $customFiledValue = new CustomFieldValue();
  83. $customFiledValue->fill([
  84. 'fieldid' => $fieldId,
  85. 'relid' => $this->getHostingId(),
  86. 'value' => $value
  87. ]);
  88. return $customFiledValue->save();
  89. }
  90. public function saveUsageLimit()
  91. {
  92. //bandwidth
  93. if ($this->getWhmcsConfigOption(ConfigurableOption::BANDWIDTH))
  94. {
  95. $bandwidthMb = $this->getWhmcsConfigOption(ConfigurableOption::BANDWIDTH);
  96. Utility::unitFormat($bandwidthMb, "gb", 'mb');
  97. }
  98. else
  99. {
  100. if ($this->getWhmcsConfigOption('Bandwidth Limit'))
  101. {
  102. $bandwidthMb = $this->getWhmcsConfigOption('Bandwidth Limit');
  103. Utility::unitFormat($bandwidthMb, "gb", 'mb');
  104. }
  105. else
  106. {
  107. if ($this->configuration()->getBandwidth())
  108. {
  109. $bandwidthMb = $this->configuration()->getBandwidth();
  110. Utility::unitFormat($bandwidthMb, "gb", 'mb');
  111. }
  112. }
  113. }
  114. //disk
  115. $diskMb = 0;
  116. if ($this->getWhmcsConfigOption(ConfigurableOption::DISK_SIZE))
  117. {
  118. $diskMb = $this->getWhmcsConfigOption(ConfigurableOption::DISK_SIZE);
  119. Utility::unitFormat($diskMb, $this->configuration()->getDiskUnit(), 'mb');
  120. }
  121. else
  122. {
  123. if ($this->configuration()->getDiskSize())
  124. {
  125. $diskMb = $this->configuration()->getDiskSize();
  126. Utility::unitFormat($diskMb, "gb", 'mb');
  127. }
  128. }
  129. $this->hosting()->update(['disklimit' => $diskMb, "bwlimit" => $bandwidthMb, "lastupdate" => date("Y-m-d H:i:s")]);
  130. }
  131. public function isBandwidthOverageUsage()
  132. {
  133. }
  134. }