From a26f6dd137298e7ed9dbf19624e06ee140c9b961 Mon Sep 17 00:00:00 2001 From: IceToast Date: Thu, 16 Jun 2022 16:15:57 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20Added=20last=5Fbilled=20to?= =?UTF-8?q?=20server=20model=20&=20always=20charge=20first=20&=20fixed=20C?= =?UTF-8?q?harge=20Server=20command?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/ChargeServers.php | 24 +++++++++++++++-------- app/Http/Controllers/ServerController.php | 12 ++++++------ app/Models/Server.php | 1 + 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/app/Console/Commands/ChargeServers.php b/app/Console/Commands/ChargeServers.php index 678ab4b7..877cb8a5 100644 --- a/app/Console/Commands/ChargeServers.php +++ b/app/Console/Commands/ChargeServers.php @@ -4,7 +4,9 @@ namespace App\Console\Commands; use App\Models\Server; use App\Notifications\ServersSuspendedNotification; +use Carbon\Carbon; use Illuminate\Console\Command; +use Illuminate\Support\Facades\DB; class ChargeServers extends Command { @@ -45,7 +47,7 @@ class ChargeServers extends Command */ public function handle() { - Server::whereNull('suspended')->with('users', 'products')->chunk(10, function ($servers) { + Server::whereNull('suspended')->with('user', 'product')->chunk(10, function ($servers) { /** @var Server $server */ foreach ($servers as $server) { /** @var Product $product */ @@ -55,23 +57,29 @@ class ChargeServers extends Command $billing_period = $product->billing_period; + // check if server is due to be charged by comparing its last_billed date with the current date and the billing period $newBillingDate = null; switch($billing_period) { case 'monthly': - $newBillingDate = $server->last_billed->addMonth(); + $newBillingDate = Carbon::parse($server->last_billed)->addMonth(); break; case 'weekly': - $newBillingDate = $server->last_billed->addYear(); + $newBillingDate = Carbon::parse($server->last_billed)->addYear(); break; case 'daily': - $newBillingDate = $server->last_billed->addDay(); + $newBillingDate = Carbon::parse($server->last_billed)->addDay(); break; + case 'hourly': + $newBillingDate = Carbon::parse($server->last_billed)->addHour(); default: - $newBillingDate = $server->last_billed->addHour(); + $newBillingDate = Carbon::parse($server->last_billed)->addHour(); break; }; - if (!($newBillingDate <= now())) return; + + if (!($newBillingDate->isPast())) { + continue; + } // check if user has enough credits to charge the server if ($user->credits < $product->price) { @@ -94,8 +102,8 @@ class ChargeServers extends Command $this->line("{$user->name} Current credits: {$user->credits} Credits to be removed: {$product->price}"); $user->decrement('credits', $product->price); - // update server last_billed date - $server->last_billed = $newBillingDate; + // update server last_billed date in db + DB::table('servers')->where('id', $server->id)->update(['last_billed' => $newBillingDate]); } return $this->notifyUsers(); diff --git a/app/Http/Controllers/ServerController.php b/app/Http/Controllers/ServerController.php index c1c0a33a..45d72569 100644 --- a/app/Http/Controllers/ServerController.php +++ b/app/Http/Controllers/ServerController.php @@ -11,6 +11,7 @@ use App\Models\Product; use App\Models\Server; use App\Models\Settings; use App\Notifications\ServerCreationError; +use Carbon\Carbon; use Exception; use Illuminate\Database\Eloquent\Builder; use Illuminate\Http\Client\Response; @@ -151,6 +152,7 @@ class ServerController extends Controller $server = $request->user()->servers()->create([ 'name' => $request->input('name'), 'product_id' => $request->input('product'), + 'last_billed' => Carbon::now()->toDateTimeString(), ]); //get free allocation ID @@ -165,14 +167,12 @@ class ServerController extends Controller //update server with pterodactyl_id $server->update([ 'pterodactyl_id' => $serverAttributes['id'], - 'identifier' => $serverAttributes['identifier'] + 'identifier' => $serverAttributes['identifier'], + ]); - if (config('SETTINGS::SYSTEM:SERVER_CREATE_CHARGE_FIRST_HOUR', 'true') == 'true') { - if ($request->user()->credits >= $server->product->getHourlyPrice()) { - $request->user()->decrement('credits', $server->product->getHourlyPrice()); - } - } + // Charge first billing cycle + $request->user()->decrement('credits', $server->product->price); return redirect()->route('servers.index')->with('success', __('Server created')); } diff --git a/app/Models/Server.php b/app/Models/Server.php index 34c992ba..82770a30 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -47,6 +47,7 @@ class Server extends Model "identifier", "product_id", "pterodactyl_id", + "last_billed" ]; /**