feat: ✨ Added last_billed to server model & always charge first & fixed Charge Server command
This commit is contained in:
parent
5b738be6e1
commit
da0dd37559
|
@ -4,7 +4,9 @@ namespace App\Console\Commands;
|
||||||
|
|
||||||
use App\Models\Server;
|
use App\Models\Server;
|
||||||
use App\Notifications\ServersSuspendedNotification;
|
use App\Notifications\ServersSuspendedNotification;
|
||||||
|
use Carbon\Carbon;
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
class ChargeServers extends Command
|
class ChargeServers extends Command
|
||||||
{
|
{
|
||||||
|
@ -45,7 +47,7 @@ class ChargeServers extends Command
|
||||||
*/
|
*/
|
||||||
public function handle()
|
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 */
|
/** @var Server $server */
|
||||||
foreach ($servers as $server) {
|
foreach ($servers as $server) {
|
||||||
/** @var Product $product */
|
/** @var Product $product */
|
||||||
|
@ -55,23 +57,29 @@ class ChargeServers extends Command
|
||||||
|
|
||||||
$billing_period = $product->billing_period;
|
$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
|
// check if server is due to be charged by comparing its last_billed date with the current date and the billing period
|
||||||
$newBillingDate = null;
|
$newBillingDate = null;
|
||||||
switch($billing_period) {
|
switch($billing_period) {
|
||||||
case 'monthly':
|
case 'monthly':
|
||||||
$newBillingDate = $server->last_billed->addMonth();
|
$newBillingDate = Carbon::parse($server->last_billed)->addMonth();
|
||||||
break;
|
break;
|
||||||
case 'weekly':
|
case 'weekly':
|
||||||
$newBillingDate = $server->last_billed->addYear();
|
$newBillingDate = Carbon::parse($server->last_billed)->addYear();
|
||||||
break;
|
break;
|
||||||
case 'daily':
|
case 'daily':
|
||||||
$newBillingDate = $server->last_billed->addDay();
|
$newBillingDate = Carbon::parse($server->last_billed)->addDay();
|
||||||
break;
|
break;
|
||||||
|
case 'hourly':
|
||||||
|
$newBillingDate = Carbon::parse($server->last_billed)->addHour();
|
||||||
default:
|
default:
|
||||||
$newBillingDate = $server->last_billed->addHour();
|
$newBillingDate = Carbon::parse($server->last_billed)->addHour();
|
||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
if (!($newBillingDate <= now())) return;
|
|
||||||
|
if (!($newBillingDate->isPast())) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// check if user has enough credits to charge the server
|
// check if user has enough credits to charge the server
|
||||||
if ($user->credits < $product->price) {
|
if ($user->credits < $product->price) {
|
||||||
|
@ -94,8 +102,8 @@ class ChargeServers extends Command
|
||||||
$this->line("<fg=blue>{$user->name}</> Current credits: <fg=green>{$user->credits}</> Credits to be removed: <fg=red>{$product->price}</>");
|
$this->line("<fg=blue>{$user->name}</> Current credits: <fg=green>{$user->credits}</> Credits to be removed: <fg=red>{$product->price}</>");
|
||||||
$user->decrement('credits', $product->price);
|
$user->decrement('credits', $product->price);
|
||||||
|
|
||||||
// update server last_billed date
|
// update server last_billed date in db
|
||||||
$server->last_billed = $newBillingDate;
|
DB::table('servers')->where('id', $server->id)->update(['last_billed' => $newBillingDate]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->notifyUsers();
|
return $this->notifyUsers();
|
||||||
|
|
|
@ -10,6 +10,7 @@ use App\Models\Node;
|
||||||
use App\Models\Product;
|
use App\Models\Product;
|
||||||
use App\Models\Server;
|
use App\Models\Server;
|
||||||
use App\Notifications\ServerCreationError;
|
use App\Notifications\ServerCreationError;
|
||||||
|
use Carbon\Carbon;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Illuminate\Database\Eloquent\Builder;
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
use Illuminate\Http\Client\Response;
|
use Illuminate\Http\Client\Response;
|
||||||
|
@ -180,6 +181,7 @@ class ServerController extends Controller
|
||||||
$server = $request->user()->servers()->create([
|
$server = $request->user()->servers()->create([
|
||||||
'name' => $request->input('name'),
|
'name' => $request->input('name'),
|
||||||
'product_id' => $request->input('product'),
|
'product_id' => $request->input('product'),
|
||||||
|
'last_billed' => Carbon::now()->toDateTimeString(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
//get free allocation ID
|
//get free allocation ID
|
||||||
|
@ -201,11 +203,8 @@ class ServerController extends Controller
|
||||||
'identifier' => $serverAttributes['identifier'],
|
'identifier' => $serverAttributes['identifier'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (config('SETTINGS::SYSTEM:SERVER_CREATE_CHARGE_FIRST_HOUR', 'true') == 'true') {
|
// Charge first billing cycle
|
||||||
if ($request->user()->credits >= $server->product->getHourlyPrice()) {
|
$request->user()->decrement('credits', $server->product->price);
|
||||||
$request->user()->decrement('credits', $server->product->getHourlyPrice());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return redirect()->route('servers.index')->with('success', __('Server created'));
|
return redirect()->route('servers.index')->with('success', __('Server created'));
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,12 +47,13 @@ class Server extends Model
|
||||||
* @var string[]
|
* @var string[]
|
||||||
*/
|
*/
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'name',
|
"name",
|
||||||
'description',
|
"description",
|
||||||
'suspended',
|
"suspended",
|
||||||
'identifier',
|
"identifier",
|
||||||
'product_id',
|
"product_id",
|
||||||
'pterodactyl_id',
|
"pterodactyl_id",
|
||||||
|
"last_billed"
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue