Skip to content
52 changes: 52 additions & 0 deletions app/Enums/BillingPeriod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Enums;

enum BillingPeriod: int
{
case HOURLY = 1;
case DAILY = 2;
case WEEKLY = 3;
case MONTHLY = 4;
case QUARTERLY = 5;
case HALF_ANNUALLY = 6;
case ANNUALLY = 7;

public function label(): string
{
return match($this) {
self::HOURLY => __('Hourly'),
self::DAILY => __('Daily'),
self::WEEKLY => __('Weekly'),
self::MONTHLY => __('Monthly'),
self::QUARTERLY => __('Quarterly'),
self::HALF_ANNUALLY => __('Half Annually'),
self::ANNUALLY => __('Annually'),
};
}

public function description(): string
{
return match($this) {
self::HOURLY => __('Charge the server every hour.'),
self::DAILY => __('Charge the server every day.'),
self::WEEKLY => __('Charge the server every week.'),
self::MONTHLY => __('Charge the server every month.'),
self::QUARTERLY => __('Charge the server every quarter.'),
self::HALF_ANNUALLY => __('Charge the server every half year.'),
self::ANNUALLY => __('Charge the server every year.'),
};
}

public static function options(): array
{
return collect(self::cases())->mapWithKeys(function ($period) {
return [$period->value => $period->label()];
})->toArray();
}

public static function fromValue(int $value): ?self
{
return self::tryFrom($value);
}
}
8 changes: 6 additions & 2 deletions app/Http/Controllers/Admin/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers\Admin;

use App\Enums\BillingPeriod;
use App\Enums\BillingPriority;
use App\Helpers\CurrencyHelper;
use App\Http\Controllers\Controller;
Expand Down Expand Up @@ -95,7 +96,7 @@ public function store(Request $request)
'eggs.*' => 'required|exists:eggs,id',
'disabled' => 'nullable',
'oom_killer' => 'nullable',
'billing_period' => 'required|in:hourly,daily,weekly,monthly,quarterly,half-annually,annually',
'default_billing_period' => ['required', new Enum(BillingPeriod::class)],
'default_billing_priority' => ['required', new Enum(BillingPriority::class)]
]);

Expand Down Expand Up @@ -173,7 +174,7 @@ public function update(Request $request, Product $product): RedirectResponse
'eggs.*' => 'required|exists:eggs,id',
'disabled' => 'nullable',
'oom_killer' => 'nullable',
'billing_period' => 'required|in:hourly,daily,weekly,monthly,quarterly,half-annually,annually',
'default_billing_period' => ['required', new Enum(BillingPeriod::class)],
'default_billing_priority' => ['required', new Enum(BillingPriority::class)]
]);

Expand Down Expand Up @@ -257,6 +258,9 @@ public function dataTable()
->addColumn('eggs', function (Product $product) {
return $product->eggs()->count();
})
->addColumn('default_billing_period', function (Product $product) {
return $product->default_billing_period->label();
})
->editColumn('disabled', function (Product $product) {
$checked = $product->disabled == false ? 'checked' : '';

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Admin/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ public function notify(Request $request)
try {
$user->notify(new DynamicNotification($data['via'], $database, $mail));
$successCount++;
} catch (\Throwable $e)
} catch (\Throwable $e) {
Log::error('Mass notification error for user ' . $user->id . ': ' . $e->getMessage());
}
}
Expand Down
9 changes: 5 additions & 4 deletions app/Http/Controllers/ServerController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
app/Http/Controllers/ServerController.php
<?php

namespace App\Http\Controllers;
Expand All @@ -17,6 +16,7 @@
use App\Settings\ServerSettings;
use App\Settings\PterodactylSettings;
use App\Classes\PterodactylClient;
use App\Enums\BillingPeriod;
use App\Enums\BillingPriority;
use App\Settings\GeneralSettings;
use Exception;
Expand All @@ -25,6 +25,7 @@
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Request as FacadesRequest;
use Illuminate\Support\Facades\Validator;
Expand Down Expand Up @@ -129,6 +130,7 @@ public function store(Request $request): RedirectResponse
'product' => 'required|exists:products,id',
'egg_variables' => 'nullable|string',
'billing_priority' => ['nullable', new Enum(BillingPriority::class)],
'billing_period' => ['nullable', new Enum(BillingPeriod::class)],
]);

$server = $this->createServer($request);
Expand Down Expand Up @@ -275,6 +277,7 @@ private function createServer(Request $request): ?Server
'product_id' => $product->id,
'last_billed' => Carbon::now(),
'billing_priority' => $request->input('billing_priority', $product->default_billing_priority),
'billing_period' => $request->input('billing_period', $product->default_billing_period),
]);
Comment thread
Ferks-FK marked this conversation as resolved.

$allocationId = $this->pterodactyl->getFreeAllocationId($node);
Expand Down Expand Up @@ -309,8 +312,6 @@ private function createServer(Request $request): ?Server

private function handlePostCreation(User $user, Server $server): void
{
logger('Product Price: ' . $server->product->price);

$user->decrement('credits', $server->product->price);

try {
Expand Down Expand Up @@ -388,7 +389,7 @@ public function cancel(Server $server): RedirectResponse
}
}

public function show(Server $server): \Illuminate\View\View
public function show(Server $server): \Illuminate\View\View|RedirectResponse
{
if ($server->user_id !== Auth::id()) {
return back()->with('error', __('This is not your Server!'));
Expand Down
10 changes: 9 additions & 1 deletion app/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Enums\BillingPeriod;
use App\Enums\BillingPriority;
use App\Facades\Currency;
use App\Models\Pterodactyl\Egg;
Expand Down Expand Up @@ -38,10 +39,12 @@ public function getActivitylogOptions(): LogOptions
'display_price',
'display_minimum_credits',
'default_billing_priority_label',
'default_billing_period_label',
];

protected $casts = [
'default_billing_priority' => BillingPriority::class
'default_billing_priority' => BillingPriority::class,
'default_billing_period' => BillingPeriod::class,
];
Comment thread
Ferks-FK marked this conversation as resolved.

public static function boot()
Expand Down Expand Up @@ -153,6 +156,11 @@ public function getDefaultBillingPriorityLabelAttribute()
return $this->default_billing_priority->label();
}

public function getDefaultBillingPeriodLabelAttribute()
{
return $this->default_billing_period->label();
}

public function getWeeklyPrice()
{
return $this->price / 4;
Expand Down
1 change: 1 addition & 0 deletions app/Models/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function getActivitylogOptions(): LogOptions
"suspended",
"identifier",
"billing_priority",
"billing_period",
"product_id",
Comment thread
Ferks-FK marked this conversation as resolved.
"pterodactyl_id",
"user_id",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

use App\Enums\BillingPeriod;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('products', function (Blueprint $table) {
$table->tinyInteger('default_billing_period')->after('default_billing_priority');
});
Comment thread
Ferks-FK marked this conversation as resolved.
Outdated

$periodMapping = [
'hourly' => BillingPeriod::HOURLY->value,
'daily' => BillingPeriod::DAILY->value,
'weekly' => BillingPeriod::WEEKLY->value,
'monthly' => BillingPeriod::MONTHLY->value,
'quarterly' => BillingPeriod::QUARTERLY->value,
'half-annually' => BillingPeriod::HALF_ANNUALLY->value,
'annually' => BillingPeriod::ANNUALLY->value,
];

DB::table('products')->select('id', 'billing_period')->get()->each(function ($product) use($periodMapping) {
$period = $periodMapping[$product->billing_period] ?? null;

if ($period) {
DB::table('products')
->where('id', $product->id)
->update(['default_billing_period' => $period]);
}
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('products', function (Blueprint $table) {
$table->dropColumn('default_billing_period');
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

use App\Enums\BillingPeriod;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('products', function (Blueprint $table) {
$table->dropColumn('billing_period');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('products', function (Blueprint $table) {
$table->string('billing_period')->after('minimum_credits');
});

DB::table('products')->select('id', 'default_billing_period')->get()->each(function ($product) {
$period = BillingPeriod::fromValue($product->default_billing_period);

if ($period) {
DB::table('products')
->where('id', $product->id)
->update(['billing_period' => str_replace(' ', '-', strtolower($period->label()))]);
Comment thread
Ferks-FK marked this conversation as resolved.
Outdated
}
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('servers', function (Blueprint $table) {
$table->tinyInteger('billing_period')->after('billing_priority')->nullable();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('servers', function (Blueprint $table) {
$table->dropColumn('billing_period');
});
}
};
32 changes: 6 additions & 26 deletions themes/default/views/admin/products/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,36 +81,16 @@ class="form-control @error('description') is-invalid @enderror" required="requir
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label for="billing_period">{{ __('Billing Period') }} <i
<label for="default_billing_period">{{ __('Billing Period') }} <i
data-toggle="popover" data-trigger="hover"
data-content="{{ __('Period when the user will be charged for the given price') }}"
class="fas fa-info-circle"></i></label>
<select id="billing_period" style="width:100%" class="custom-select"
name="billing_period" required autocomplete="off"
@error('billing_period') is-invalid @enderror>
<option value="hourly" @selected(old('billing_period', $product->billing_period ?? '') == 'hourly')>
{{ __('Hourly') }}
</option>
<option value="daily" @selected(old('billing_period', $product->billing_period ?? '') == 'daily')>
{{ __('Daily') }}
</option>
<option value="weekly" @selected(old('billing_period', $product->billing_period ?? '') == 'weekly')>
{{ __('Weekly') }}
</option>
<option value="monthly" @selected(old('billing_period', $product->billing_period ?? '') == 'monthly')>
{{ __('Monthly') }}
</option>
<option value="quarterly" @selected(old('billing_period', $product->billing_period ?? '') == 'quarterly')>
{{ __('Quarterly') }}
</option>
<option value="half-annually" @selected(old('billing_period', $product->billing_period ?? '') == 'half-annually')>
{{ __('Half Annually') }}
</option>
<option value="annually" @selected(old('billing_period', $product->billing_period ?? '') == 'annually')>
{{ __('Annually') }}
</option>
<select id="default_billing_period" style="width:100%" class="custom-select" name="default_billing_period" required autocomplete="off" @error('default_billing_period') is-invalid @enderror>
Comment thread
Ferks-FK marked this conversation as resolved.
Outdated
@foreach (App\Enums\BillingPeriod::options() as $value => $label)
<option value="{{ $value }}" @selected(old('default_billing_period', $product->default_billing_period->value ?? '') == $value || $value == App\Enums\BillingPeriod::HOURLY->value)>{{ $label }}</option>
Comment thread
Ferks-FK marked this conversation as resolved.
Outdated
@endforeach
</select>
@error('billing_period')
@error('default_billing_period')
<div class="invalid-feedback">
{{ $message }}
</div>
Expand Down
Loading