Product.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace App\Models;
  3. use Hidehalo\Nanoid\Client;
  4. use Illuminate\Database\Eloquent\Factories\HasFactory;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  7. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  8. use Spatie\Activitylog\LogOptions;
  9. use Spatie\Activitylog\Traits\LogsActivity;
  10. use App\Models\Pterodactyl\Egg;
  11. use App\Models\Pterodactyl\Node;
  12. class Product extends Model
  13. {
  14. use HasFactory;
  15. use LogsActivity;
  16. public function getActivitylogOptions(): LogOptions
  17. {
  18. return LogOptions::defaults()
  19. -> logOnlyDirty()
  20. -> logOnly(['*'])
  21. -> dontSubmitEmptyLogs();
  22. }
  23. public $incrementing = false;
  24. protected $guarded = ['id'];
  25. public static function boot()
  26. {
  27. parent::boot();
  28. static::creating(function (Product $product) {
  29. $client = new Client();
  30. $product->{$product->getKeyName()} = $client->generateId($size = 21);
  31. });
  32. static::deleting(function (Product $product) {
  33. $product->nodes()->detach();
  34. $product->eggs()->detach();
  35. });
  36. }
  37. public function getHourlyPrice()
  38. {
  39. // calculate the hourly price with the billing period
  40. switch($this->billing_period) {
  41. case 'daily':
  42. return $this->price / 24;
  43. case 'weekly':
  44. return $this->price / 24 / 7;
  45. case 'monthly':
  46. return $this->price / 24 / 30;
  47. case 'quarterly':
  48. return $this->price / 24 / 30 / 3;
  49. case 'half-annually':
  50. return $this->price / 24 / 30 / 6;
  51. case 'annually':
  52. return $this->price / 24 / 365;
  53. default:
  54. return $this->price;
  55. }
  56. }
  57. public function getMonthlyPrice()
  58. {
  59. // calculate the hourly price with the billing period
  60. switch($this->billing_period) {
  61. case 'hourly':
  62. return $this->price * 24 * 30;
  63. case 'daily':
  64. return $this->price * 30;
  65. case 'weekly':
  66. return $this->price * 4;
  67. case 'monthly':
  68. return $this->price;
  69. case 'quarterly':
  70. return $this->price / 3;
  71. case 'half-annually':
  72. return $this->price / 6;
  73. case 'annually':
  74. return $this->price / 12;
  75. default:
  76. return $this->price;
  77. }
  78. }
  79. public function getWeeklyPrice()
  80. {
  81. return $this->price / 4;
  82. }
  83. /**
  84. * @return BelongsTo
  85. */
  86. public function servers()
  87. {
  88. return $this->belongsTo(Server::class, 'id', 'product_id');
  89. }
  90. /**
  91. * @return BelongsToMany
  92. */
  93. public function eggs()
  94. {
  95. return $this->belongsToMany(Egg::class);
  96. }
  97. /**
  98. * @return BelongsToMany
  99. */
  100. public function nodes()
  101. {
  102. return $this->belongsToMany(Node::class);
  103. }
  104. }