diff --git a/app/Http/Controllers/Admin/OverViewController.php b/app/Http/Controllers/Admin/OverViewController.php index 58af87b7..8d6e3dfa 100644 --- a/app/Http/Controllers/Admin/OverViewController.php +++ b/app/Http/Controllers/Admin/OverViewController.php @@ -13,6 +13,8 @@ use App\Models\User; use Illuminate\Support\Facades\Cache; use App\Classes\Pterodactyl; use App\Models\Product; +use App\Models\Ticket; +use Carbon\Carbon; class OverViewController extends Controller { @@ -20,61 +22,126 @@ class OverViewController extends Controller public function index() { - $counters = Cache::remember('counters', self::TTL, function () { - $output = collect(); - $output->put('users', User::query()->count()); - $output->put('credits', number_format(User::query()->where("role","!=","admin")->sum('credits'), 2, '.', '')); - $output->put('payments', Payment::query()->count()); - $output->put('eggs', Egg::query()->count()); - $output->put('nests', Nest::query()->count()); - $output->put('locations', Location::query()->count()); + //Get counters + $counters = collect(); + //Set basic variables in the collection + $counters->put('users', User::query()->count()); + $counters->put('credits', number_format(User::query()->where("role","!=","admin")->sum('credits'), 2, '.', '')); + $counters->put('payments', Payment::query()->count()); + $counters->put('eggs', Egg::query()->count()); + $counters->put('nests', Nest::query()->count()); + $counters->put('locations', Location::query()->count()); - $output->put('servers', collect()); - $output['servers']->active = 0; - $output['servers']->total = 0; - $output->put('earnings', collect()); - $output['earnings']->active = 0; - $output['earnings']->total = 0; - $output->put('totalUsagePercent', 0); - - return $output; - }); + //Prepare for counting + $counters->put('servers', collect()); + $counters['servers']->active = 0; + $counters['servers']->total = 0; + $counters->put('earnings', collect()); + $counters['earnings']->active = 0; + $counters['earnings']->total = 0; + $counters->put('totalUsagePercent', 0); + + //Prepare subCollection 'payments' + $counters->put('payments', collect()); + //Get and save payments from last 2 months for later filtering and looping + $payments = Payment::query()->where('created_at', '>=', Carbon::today()->startOfMonth()->subMonth())->where('status', 'paid')->get(); + //Prepare collections and set a few variables + $counters['payments']->put('thisMonth', collect()); + $counters['payments']->put('lastMonth', collect()); + $counters['payments']['thisMonth']->timeStart = Carbon::today()->startOfMonth()->toDateString(); + $counters['payments']['thisMonth']->timeEnd = Carbon::today()->toDateString(); + $counters['payments']['lastMonth']->timeStart = Carbon::today()->startOfMonth()->subMonth()->toDateString(); + $counters['payments']['lastMonth']->timeEnd = Carbon::today()->endOfMonth()->subMonth()->toDateString(); + + //Fill out variables for each currency separately + foreach($payments->where('created_at', '>=', Carbon::today()->startOfMonth()) as $payment){ + $paymentCurrency = $payment->currency_code; + if(!isset($counters['payments']['thisMonth'][$paymentCurrency])){ + $counters['payments']['thisMonth']->put($paymentCurrency, collect()); + $counters['payments']['thisMonth'][$paymentCurrency]->total = 0; + $counters['payments']['thisMonth'][$paymentCurrency]->count = 0; + } + $counters['payments']['thisMonth'][$paymentCurrency]->total += $payment->total_price; + $counters['payments']['thisMonth'][$paymentCurrency]->count ++; + } + foreach($payments->where('created_at', '<', Carbon::today()->startOfMonth()) as $payment){ + $paymentCurrency = $payment->currency_code; + if(!isset($counters['payments']['lastMonth'][$paymentCurrency])){ + $counters['payments']['lastMonth']->put($paymentCurrency, collect()); + $counters['payments']['lastMonth'][$paymentCurrency]->total = 0; + $counters['payments']['lastMonth'][$paymentCurrency]->count = 0; + } + $counters['payments']['lastMonth'][$paymentCurrency]->total += $payment->total_price; + $counters['payments']['lastMonth'][$paymentCurrency]->count ++; + } + $counters['payments']->total = Payment::query()->count(); $lastEgg = Egg::query()->latest('updated_at')->first(); $syncLastUpdate = $lastEgg ? $lastEgg->updated_at->isoFormat('LLL') : __('unknown'); - $nodes = Cache::remember('nodes', self::TTL, function() use($counters){ - $output = collect(); - foreach($nodes = Node::query()->get() as $node){ //gets all node information and prepares the structure - $nodeId = $node['id']; - $output->put($nodeId, collect()); - $output[$nodeId]->name = $node['name']; - $node = Pterodactyl::getNode($nodeId); - $output[$nodeId]->usagePercent = round(max($node['allocated_resources']['memory']/($node['memory']*($node['memory_overallocate']+100)/100), $node['allocated_resources']['disk']/($node['disk']*($node['disk_overallocate']+100)/100))*100, 2); - $counters['totalUsagePercent'] += $output[$nodeId]->usagePercent; - $output[$nodeId]->totalServers = 0; - $output[$nodeId]->activeServers = 0; - $output[$nodeId]->totalEarnings = 0; - $output[$nodeId]->activeEarnings = 0; + + //Get node information + $nodes = collect(); + foreach($DBnodes = Node::query()->get() as $DBnode){ //gets all node information and prepares the structure + $nodeId = $DBnode['id']; + $nodes->put($nodeId, collect()); + $nodes[$nodeId]->name = $DBnode['name']; + $pteroNode = Pterodactyl::getNode($nodeId); + $nodes[$nodeId]->usagePercent = round(max($pteroNode['allocated_resources']['memory']/($pteroNode['memory']*($pteroNode['memory_overallocate']+100)/100), $pteroNode['allocated_resources']['disk']/($pteroNode['disk']*($pteroNode['disk_overallocate']+100)/100))*100, 2); + $counters['totalUsagePercent'] += $nodes[$nodeId]->usagePercent; + + $nodes[$nodeId]->totalServers = 0; + $nodes[$nodeId]->activeServers = 0; + $nodes[$nodeId]->totalEarnings = 0; + $nodes[$nodeId]->activeEarnings = 0; + } + $counters['totalUsagePercent'] = ($DBnodes->count())?round($counters['totalUsagePercent']/$DBnodes->count(), 2):0; + + foreach(Pterodactyl::getServers() as $server){ //gets all servers from Pterodactyl and calculates total of credit usage for each node separately + total + $nodeId = $server['attributes']['node']; + + if($CPServer = Server::query()->where('pterodactyl_id', $server['attributes']['id'])->first()){ + $prize = Product::query()->where('id', $CPServer->product_id)->first()->price; + if (!$CPServer->suspended){ + $counters['earnings']->active += $prize; + $counters['servers']->active ++; + $nodes[$nodeId]->activeEarnings += $prize; + $nodes[$nodeId]->activeServers ++; + } + $counters['earnings']->total += $prize; + $counters['servers']->total ++; + $nodes[$nodeId]->totalEarnings += $prize; + $nodes[$nodeId]->totalServers ++; } - $counters['totalUsagePercent'] = ($nodes->count())?round($counters['totalUsagePercent']/$nodes->count(), 2):0; + } - foreach(Pterodactyl::getServers() as $server){ //gets all servers from Pterodactyl and calculates total of credit usage for each node separately + total - $nodeId = $server['attributes']['node']; - - if($CPServer = Server::query()->where('pterodactyl_id', $server['attributes']['id'])->first()){ - $prize = Product::query()->where('id', $CPServer->product_id)->first()->price; - if (!$CPServer->suspended){ - $counters['earnings']->active += $prize; - $counters['servers']->active ++; - $output[$nodeId]->activeEarnings += $prize; - $output[$nodeId]->activeServers ++; - } - $counters['earnings']->total += $prize; - $counters['servers']->total ++; - $output[$nodeId]->totalEarnings += $prize; - $output[$nodeId]->totalServers ++; + + + //Get latest tickets + $tickets = Cache::remember('tickets', self::TTL, function(){ + $output = collect(); + foreach(Ticket::query()->latest()->take(3)->get() as $ticket){ + $output->put($ticket->ticket_id, collect()); + $output[$ticket->ticket_id]->title = $ticket->title; + $user = User::query()->where('id', $ticket->user_id)->first(); + $output[$ticket->ticket_id]->user_id = $user->id; + $output[$ticket->ticket_id]->user = $user->name; + $output[$ticket->ticket_id]->status = $ticket->status; + $output[$ticket->ticket_id]->last_updated = $ticket->updated_at->diffForHumans(); + switch ($ticket->status) { + case 'Open': + $output[$ticket->ticket_id]->statusBadgeColor = 'badge-success'; + break; + case 'Closed': + $output[$ticket->ticket_id]->statusBadgeColor = 'badge-danger'; + break; + case 'Answered': + $output[$ticket->ticket_id]->statusBadgeColor = 'badge-info'; + break; + default: + $output[$ticket->ticket_id]->statusBadgeColor = 'badge-warning'; + break; } } return $output; @@ -84,7 +151,8 @@ class OverViewController extends Controller 'counters' => $counters, 'nodes' => $nodes, 'syncLastUpdate' => $syncLastUpdate, - 'perPageLimit' => ($counters['servers']->total != Server::query()->count())?true:false + 'perPageLimit' => ($counters['servers']->total != Server::query()->count())?true:false, + 'tickets' => $tickets ]); } diff --git a/app/Http/Controllers/ProductController.php b/app/Http/Controllers/ProductController.php index a5e7bcbd..61e480f0 100644 --- a/app/Http/Controllers/ProductController.php +++ b/app/Http/Controllers/ProductController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Classes\Pterodactyl; use App\Models\Egg; use App\Models\Location; use App\Models\Node; @@ -55,6 +56,10 @@ class ProductController extends Controller public function getLocationsBasedOnEgg(Request $request, Egg $egg) { $nodes = $this->getNodesBasedOnEgg($request, $egg); + foreach($nodes as $key => $node){ + $pteroNode = Pterodactyl::getNode($node->id); + if($pteroNode['allocated_resources']['memory']>=($pteroNode['memory']*($pteroNode['memory_overallocate']+100)/100)||$pteroNode['allocated_resources']['disk']>=($pteroNode['disk']*($pteroNode['disk_overallocate']+100)/100)) $nodes->forget($key); + } $locations = collect(); //locations @@ -87,7 +92,7 @@ class ProductController extends Controller { if (is_null($egg->id) || is_null($node->id)) return response()->json('node and egg id is required', '400'); - return Product::query() + $products = Product::query() ->where('disabled', '=', false) ->whereHas('nodes', function (Builder $builder) use ($node) { $builder->where('id', '=', $node->id); @@ -96,5 +101,12 @@ class ProductController extends Controller $builder->where('id', '=', $egg->id); }) ->get(); + + $pteroNode = Pterodactyl::getNode($node->id); + foreach($products as $key => $product){ + if($product->memory>($pteroNode['memory']*($pteroNode['memory_overallocate']+100)/100)-$pteroNode['allocated_resources']['memory']||$product->disk>($pteroNode['disk']*($pteroNode['disk_overallocate']+100)/100)-$pteroNode['allocated_resources']['disk']) $product->doesNotFit = true; + } + + return $products; } } diff --git a/app/Http/Controllers/ServerController.php b/app/Http/Controllers/ServerController.php index 23aa53f8..5ee16dd5 100644 --- a/app/Http/Controllers/ServerController.php +++ b/app/Http/Controllers/ServerController.php @@ -241,6 +241,9 @@ class ServerController extends Controller $serverRelationships = $serverAttributes['relationships']; $serverLocationAttributes = $serverRelationships['location']['attributes']; + //Get current product + $currentProduct = Product::where('id', $server->product_id)->first(); + //Set server infos $server->location = $serverLocationAttributes['long'] ? $serverLocationAttributes['long'] : @@ -249,11 +252,19 @@ class ServerController extends Controller $server->node = $serverRelationships['node']['attributes']['name']; $server->name = $serverAttributes['name']; $server->egg = $serverRelationships['egg']['attributes']['name']; - $products = Product::orderBy("created_at")->get(); + + $pteroNode = Pterodactyl::getNode($serverRelationships['node']['attributes']['id']); + + $products = Product::orderBy("created_at") + ->whereHas('nodes', function (Builder $builder) use ($serverRelationships) { //Only show products for that node + $builder->where('id', '=', $serverRelationships['node']['attributes']['id']); + }) + ->get(); // Set the each product eggs array to just contain the eggs name foreach ($products as $product) { $product->eggs = $product->eggs->pluck('name')->toArray(); + if($product->memory-$currentProduct->memory>($pteroNode['memory']*($pteroNode['memory_overallocate']+100)/100)-$pteroNode['allocated_resources']['memory']||$product->disk-$currentProduct->disk>($pteroNode['disk']*($pteroNode['disk_overallocate']+100)/100)-$pteroNode['allocated_resources']['disk']) $product->doesNotFit = true; } return view('servers.settings')->with([ diff --git a/resources/views/admin/overview/index.blade.php b/resources/views/admin/overview/index.blade.php index b02d431e..957c72e1 100644 --- a/resources/views/admin/overview/index.blade.php +++ b/resources/views/admin/overview/index.blade.php @@ -90,7 +90,7 @@
{{__('Payments')}} - {{$counters['payments']}} + {{$counters['payments']->total}}
@@ -142,6 +142,42 @@ {{__('Last updated :date', ['date' => $syncLastUpdate])}} +
+
+
+
+ {{__('Latest tickets')}} +
+
+
+
+ @if(!$tickets->count()){{__('There are no tickets')}}. + @else + + + + + + + + + + + + @foreach($tickets as $ticket_id => $ticket) + + + + + + + @endforeach + + +
{{__('Title')}}{{__('User')}}{{__('Status')}}{{__('Last updated')}}
#{{$ticket_id}} - {{$ticket->title}}{{$ticket->user}}{{$ticket->status}}{{$ticket->last_updated}}
+ @endif +
+
@@ -163,7 +199,7 @@
- {{__('Individual nodes')}} + {{__('Individual nodes')}}
@@ -210,6 +246,71 @@
+
+
+
+
+ {{__('Latest payments')}} +
+
+
+
+
+
+ {{__('Last month')}}: + + + + + + + + + + + + @foreach($counters['payments']['lastMonth'] as $currency => $income) + + + + + + @endforeach + +
{{__('Currency')}}{{__('Number of payments')}}{{__('Total income')}}
{{$currency}}{{$income->count}}{{$income->total}}
+
+
+ {{__('This month')}}: + + + + + + + + + + + + @foreach($counters['payments']['thisMonth'] as $currency => $income) + + + + + + @endforeach + +
{{__('Currency')}}{{__('Number of payments')}}{{__('Total income')}}
{{$currency}}{{$income->count}}{{$income->total}}
+
+
+
+ +
+
diff --git a/resources/views/moderator/ticket/show.blade.php b/resources/views/moderator/ticket/show.blade.php index 22bd2ea1..f6a309f2 100644 --- a/resources/views/moderator/ticket/show.blade.php +++ b/resources/views/moderator/ticket/show.blade.php @@ -51,11 +51,13 @@ @endif

Created on: {{ $ticket->created_at->diffForHumans() }}

+ @if($ticket->status!='Closed')
{{csrf_field()}} {{method_field("POST") }}
+ @endif diff --git a/resources/views/servers/create.blade.php b/resources/views/servers/create.blade.php index 5454a6b6..4ac74f79 100644 --- a/resources/views/servers/create.blade.php +++ b/resources/views/servers/create.blade.php @@ -218,10 +218,10 @@ diff --git a/resources/views/servers/settings.blade.php b/resources/views/servers/settings.blade.php index e320533c..1a1b269b 100644 --- a/resources/views/servers/settings.blade.php +++ b/resources/views/servers/settings.blade.php @@ -255,13 +255,13 @@ @foreach($products as $product) @if(in_array($server->egg, $product->eggs) && $product->id != $server->product->id && $product->disabled == false) - + @endif @endforeach
{{__("Once the Upgrade button is pressed, we will automatically deduct the amount for the first hour according to the new product from your credits")}}.
-
{{_("Server will be automatically restarted once upgraded")}} +
{{__("Server will be automatically restarted once upgraded")}}