Hosting.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxAddon product developed. (Jan 17, 2019)
  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\Models\Whmcs;
  20. /**
  21. * Description of Hosting
  22. *
  23. * @author Pawel Kopec <pawelk@modulesgardne.com>
  24. * @property int $id
  25. * @property int $userid
  26. * @property int $orderid
  27. * @property int $packageid
  28. * @property int $server
  29. * @property string $regdate
  30. * @property string $domain
  31. * @property string $paymentmethod
  32. * @property float $firstpaymentamount
  33. * @property float $amount
  34. * @property string $billingcycle
  35. * @property string $nextduedate
  36. * @property string $nextinvoicedate
  37. * @property string $termination_date
  38. * @property string $completed_date
  39. * @property string $domainstatus
  40. * @property string $username
  41. * @property string $password
  42. * @property string $notes
  43. * @property string $subscriptionid
  44. * @property int $promoid
  45. * @property string $suspendreason
  46. * @property int $overideautosuspend
  47. * @property string $overidesuspenduntil
  48. * @property string $dedicatedip
  49. * @property string $assignedips
  50. * @property string $ns1
  51. * @property string $ns2
  52. * @property int $diskusage
  53. * @property int $disklimit
  54. * @property int $bwusage
  55. * @property int $bwlimit
  56. * @property string $lastupdate
  57. * @property string $created_at
  58. * @property string $updated_at
  59. * @method Hosting active()
  60. * @method Hosting activeAndSuspended
  61. * @method Hosting ofUserId($userId)
  62. * @method Hosting ofServerId($serverId)
  63. * @method Hosting ofCustomFieldNode($node)
  64. * @method Hosting ofvServerNode($node)
  65. * @method Hosting ofCustomFielVmid($vmid)
  66. * @method Hosting ofvServerVmid($vmid)
  67. * @method Hosting ofStatus($status)
  68. * @method static Hosting ofServerType($hostingId, $moduleName)
  69. * @method static Hosting ofId($id)
  70. * @method Hosting[] get()
  71. * @property CustomFieldValue[] $customFieldValues
  72. * @method static Hosting ofProxmoxVps()
  73. * @method static Hosting ofProxmoxCloud()
  74. * @method static Hosting ofProxmoxCloudAndStatusActiveAndSuspended
  75. * @method static Hosting ofProxmoxVpsAndProxmoxCloud()
  76. * @method static Hosting ofProductId($id)
  77. */
  78. class Hosting extends \ModulesGarden\ProxmoxAddon\Core\Models\Whmcs\Hosting
  79. {
  80. public function scopeActive($query)
  81. {
  82. return $query->where('domainstatus', 'Active');
  83. }
  84. public function scopeActiveAndSuspended($query)
  85. {
  86. return $query->whereIn('domainstatus', ['Active', 'Suspended']);
  87. }
  88. public function scopeOfId($query, $id)
  89. {
  90. return $query->where('id', $id);
  91. }
  92. public function scopeOfServerId($query, $serverId)
  93. {
  94. return $query->where('server', $serverId);
  95. }
  96. public function scopeOfUserId($query, $userId)
  97. {
  98. return $query->where('userid', $userId);
  99. }
  100. public function scopeOfCustomFieldNode($query, $node)
  101. {
  102. $h = 'tblhosting';
  103. $cf = 'tblcustomfields';
  104. $cfv = 'tblcustomfieldsvalues';
  105. $query->select("{$h}.*")
  106. ->rightJoin($cfv, "{$cfv}.relid", '=', "{$h}.id")
  107. ->rightJoin($cf, "{$cfv}.fieldid", '=', "{$cf}.id")
  108. ->where("{$cf}.type", "product")
  109. ->where("{$cf}.fieldname", "LIKE", "node%")
  110. ->where("{$cfv}.value", $node);
  111. return $query;
  112. }
  113. public function scopeOfCustomFielVmid($query, $vmid)
  114. {
  115. $h = 'tblhosting';
  116. $cf = 'tblcustomfields';
  117. $cfv = 'tblcustomfieldsvalues';
  118. $query->select("{$h}.*")
  119. ->rightJoin($cfv, "{$cfv}.relid", '=', "{$h}.id")
  120. ->rightJoin($cf, "{$cfv}.fieldid", '=', "{$cf}.id")
  121. ->where("{$cf}.type", "product")
  122. ->where("{$cf}.fieldname", "LIKE", "vmid%")
  123. ->where("{$cfv}.value", $vmid);
  124. return $query;
  125. }
  126. public function scopeOfvServerNode($query, $node)
  127. {
  128. $h = 'tblhosting';
  129. $vs = 'ProxmoxAddon_Vm';
  130. $query->select("{$h}.*")
  131. ->rightJoin($vs, "{$vs}.hosting_id", '=', "{$h}.id")
  132. ->where("{$vs}.node", $node);
  133. return $query;
  134. }
  135. public function scopeOfvServerVmid($query, $vmid)
  136. {
  137. $h = 'tblhosting';
  138. $vs = 'ProxmoxAddon_Vm';
  139. $query->select("{$h}.*")
  140. ->rightJoin($vs, "{$vs}.hosting_id", '=', "{$h}.id")
  141. ->where("{$vs}.vmid", $vmid);
  142. return $query;
  143. }
  144. public function scopeOfStatus($query, $status)
  145. {
  146. return $query->whereIn('domainstatus', $status);
  147. }
  148. public function scopeOfServerType($query, $hostingId, $moduleName)
  149. {
  150. $h = "tblhosting";
  151. $p = "tblproducts";
  152. $query->select("{$h}.*");
  153. return $query->rightJoin($p, function ($join) use ($p)
  154. {
  155. $join->on('packageid', '=', "{$p}.id");
  156. })->where("{$h}.id", $hostingId)
  157. ->where("{$p}.servertype", $moduleName);
  158. }
  159. public function ipAdd($ip)
  160. {
  161. if (!$this->dedicatedip)
  162. {
  163. $this->dedicatedip = $ip;
  164. return $this;
  165. }
  166. $ips = explode("\n", $this->assignedips);
  167. $ips[] = $ip;
  168. $ips = array_unique($ips);
  169. $this->assignedips = implode("\n", $ips);
  170. return $this;
  171. }
  172. public function ipDelete($ip)
  173. {
  174. if ($this->dedicatedip == $ip)
  175. {
  176. $this->dedicatedip = null;
  177. return $this;
  178. }
  179. $ips = explode("\n", $this->assignedips);
  180. foreach ($ips as $k => $v)
  181. {
  182. if (trim($v) == trim($ip))
  183. {
  184. unset($ips[$k]);
  185. }
  186. }
  187. $this->assignedips = implode("\n", $ips);
  188. return $this;
  189. }
  190. /**
  191. * @return \DateTime
  192. * @throws \Exception
  193. */
  194. public function getLastUpdate()
  195. {
  196. return new \DateTime($this->lastupdate);
  197. }
  198. public function customFieldValues()
  199. {
  200. return $this->hasMany("ModulesGarden\ProxmoxAddon\App\Models\Whmcs\CustomFieldValue", "relid");
  201. }
  202. public function scopeOfProxmoxVps($query)
  203. {
  204. $h = 'tblhosting';
  205. $s = 'tblservers';
  206. $query->select("{$h}.id", "{$h}.packageid", "{$h}.lastupdate", "{$h}.bwusage", "{$h}.bwlimit", "{$h}.regdate")
  207. ->rightJoin($s, "{$h}.server", '=', "{$s}.id")
  208. ->where("{$s}.type", "proxmoxVPS");
  209. return $query;
  210. }
  211. public function scopeOfProxmoxVpsAndProxmoxCloud($query)
  212. {
  213. $h = 'tblhosting';
  214. $s = 'tblservers';
  215. $query->select("{$h}.id", "{$h}.packageid", "{$h}.lastupdate", "{$h}.bwusage", "{$h}.bwlimit", "{$h}.regdate")
  216. ->rightJoin($s, "{$h}.server", '=', "{$s}.id")
  217. ->whereIn("{$s}.type", ["proxmoxVPS", "ProxmoxCloudVps" ]);
  218. return $query;
  219. }
  220. public function scopeOfProxmoxCloud($query)
  221. {
  222. $h = 'tblhosting';
  223. $s = 'tblservers';
  224. $query->select("{$h}.id", "{$h}.packageid", "{$h}.lastupdate", "{$h}.bwusage", "{$h}.bwlimit", "{$h}.regdate")
  225. ->rightJoin($s, "{$h}.server", '=', "{$s}.id")
  226. ->where("{$s}.type", "ProxmoxCloudVps");
  227. return $query;
  228. }
  229. public function scopeOfProxmoxCloudAndStatusActiveAndSuspended($query)
  230. {
  231. $h = 'tblhosting';
  232. $s = 'tblservers';
  233. $query->select("{$h}.*")
  234. ->rightJoin($s, "{$h}.server", '=', "{$s}.id")
  235. ->where("{$s}.type", "ProxmoxCloudVps")
  236. ->whereIn("{$h}.domainstatus", ['Active', 'Suspended']);
  237. return $query;
  238. }
  239. public function isBandwidthOverageUsage()
  240. {
  241. return $this->bwlimit && $this->bwlimit < $this->bwusage;
  242. }
  243. public function scopeOfProductId($query, $id){
  244. return $query->where('packageid', $id);
  245. }
  246. }