From e310dba243f10d0ebe704d66dd581a8aa7be227b Mon Sep 17 00:00:00 2001 From: IceToast Date: Fri, 22 Jul 2022 04:54:33 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Credt=20usage=20at=20dash?= =?UTF-8?q?board?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/HomeController.php | 2 +- app/Models/Product.php | 16 +++++++++++++++- app/Models/User.php | 10 +++++++++- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index 5a511758..db00d18d 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -97,7 +97,7 @@ class HomeController extends Controller /** Build our Time-Left-Box */ if ($credits > 0.01 and $usage > 0) { - $daysLeft = number_format(($credits * 30) / $usage, 2, '.', ''); + $daysLeft = number_format($credits / ($usage / 30), 2, '.', ''); $hoursLeft = number_format($credits / ($usage / 30 / 24), 2, '.', ''); $bg = $this->getTimeLeftBoxBackground($daysLeft); diff --git a/app/Models/Product.php b/app/Models/Product.php index a31ebf50..c55d3be8 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -43,7 +43,21 @@ class Product extends Model public function getHourlyPrice() { - return ($this->price / 30) / 24; + // calculate the hourly price with the billing period + switch($this->billing_period) { + case 'daily': + return $this->price / 24; + case 'weekly': + return $this->price / 24 / 7; + case 'monthly': + return $this->price / 24 / 30; + case 'half-annually': + return $this->price / 24 / 30 / 6; + case 'annually': + return $this->price / 24 / 365; + default: + return $this->price; + } } public function getDailyPrice() diff --git a/app/Models/User.php b/app/Models/User.php index 85e2b240..91e15cab 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -251,12 +251,20 @@ class User extends Authenticatable implements MustVerifyEmail { $usage = 0; foreach ($this->getServersWithProduct() as $server) { - $usage += $server->product->price; + $usage += $server->product->getHourlyPrice() * 24 * 30; } return number_format($usage, 2, '.', ''); } + private function getServersWithProduct() { + return $this->servers() + ->whereNull('suspended') + ->whereNull('cancelled') + ->with('product') + ->get(); + } + /** * @return array|string|string[] */