HostingService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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\Cloud;
  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\Repositories\Cloud\ProductConfigurationRepository;
  24. use ModulesGarden\ProxmoxAddon\App\Services\Utility;
  25. use ModulesGarden\ProxmoxAddon\App\Enum\Cloud\ConfigurableOption;
  26. /**
  27. * Trait HostingService
  28. * @package ModulesGarden\ProxmoxAddon\App\Services\Cloud
  29. * @method ProductConfigurationRepository configuration()
  30. */
  31. trait HostingService
  32. {
  33. /**
  34. * @return mixed
  35. */
  36. public function getHostingId()
  37. {
  38. if (!$this->hostingId)
  39. {
  40. $this->hostingId = $this->getWhmcsParamByKey("serviceid");
  41. }
  42. return $this->hostingId;
  43. }
  44. /**
  45. * @param mixed $hostingId
  46. */
  47. public function setHostingId($hostingId)
  48. {
  49. $this->hostingId = $hostingId;
  50. return $this;
  51. }
  52. /**
  53. * @return Hosting
  54. */
  55. public function hosting()
  56. {
  57. if ($this->hosting instanceof Hosting)
  58. {
  59. return $this->hosting;
  60. }
  61. return $this->hosting = Hosting::where("id", $this->getHostingId())->firstOrFail();
  62. }
  63. public function isActive()
  64. {
  65. return Hosting::ofId($this->getHostingId())->active()->count() == 1;
  66. }
  67. public function isSupportedModule()
  68. {
  69. return Hosting::ofServerType($this->getHostingId(), "ProxmoxCloudVps")->count() == 1;
  70. }
  71. private function getCustomFieldId($fieldName)
  72. {
  73. return CustomField::select("id")
  74. ->where("type", "product")
  75. ->where("relid", $this->hosting()->packageid)
  76. ->where("fieldname", "like", $fieldName . "%")
  77. ->value("id");
  78. }
  79. public function customFieldUpdate($name, $value = '')
  80. {
  81. $fieldId = $this->getCustomFieldId($name);
  82. //Update
  83. if (CustomFieldValue::where('fieldid', $fieldId)->where("relid", $this->getHostingId())->count())
  84. {
  85. return CustomFieldValue::where('fieldid', $fieldId)->where("relid", $this->getHostingId())->update(['value' => $value]);
  86. }
  87. //Create
  88. $customFiledValue = new CustomFieldValue();
  89. $customFiledValue->fill([
  90. 'fieldid' => $fieldId,
  91. 'relid' => $this->getHostingId(),
  92. 'value' => $value
  93. ]);
  94. return $customFiledValue->save();
  95. }
  96. public function saveUsageLimit()
  97. {
  98. //bandwidth
  99. if ($this->getWhmcsConfigOption(ConfigurableOption::BANDWIDTH))
  100. {
  101. $bandwidthMb = $this->getWhmcsConfigOption(ConfigurableOption::BANDWIDTH);
  102. Utility::unitFormat($bandwidthMb, "gb", 'mb');
  103. }
  104. else
  105. {
  106. if ($this->getWhmcsConfigOption('Bandwidth Limit'))
  107. {
  108. $bandwidthMb = $this->getWhmcsConfigOption('Bandwidth Limit');
  109. Utility::unitFormat($bandwidthMb, "gb", 'mb');
  110. }
  111. else
  112. {
  113. if ($this->configuration()->getBandwidth())
  114. {
  115. $bandwidthMb = $this->configuration()->getBandwidth();
  116. Utility::unitFormat($bandwidthMb, "gb", 'mb');
  117. }
  118. }
  119. }
  120. //disk
  121. $diskMb = 0;
  122. if ($this->getWhmcsConfigOption(ConfigurableOption::STORAGE))
  123. {
  124. $diskMb = $this->getWhmcsConfigOption(ConfigurableOption::STORAGE);
  125. Utility::unitFormat($diskMb, $this->configuration()->getStorageUnit(), 'mb');
  126. }
  127. else
  128. {
  129. if ($this->configuration()->getStorageSize())
  130. {
  131. $diskMb = $this->configuration()->getStorageSize();
  132. Utility::unitFormat($diskMb, "gb", 'mb');
  133. }
  134. }
  135. $this->hosting()->update(['disklimit' => $diskMb, "bwlimit" => $bandwidthMb, "lastupdate" => date("Y-m-d H:i:s")]);
  136. }
  137. public function isBandwidthOverageUsage()
  138. {
  139. }
  140. }