diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index d6b2fd18..80c1ff5e 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -76,7 +76,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 52a37c28..2c23e9c9 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -37,7 +37,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 5f8af024..e4e3fc66 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -233,17 +233,19 @@ class User extends Authenticatable implements MustVerifyEmail * @return string */ public function creditUsage() - { + { $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(); }