fix: 🐛 Credt usage at dashboard

This commit is contained in:
IceToast 2022-07-22 04:54:33 +02:00 committed by IceToast
parent 54e14d5f2b
commit e310dba243
No known key found for this signature in database
GPG key ID: 1464353E063A5B97
3 changed files with 25 additions and 3 deletions

View file

@ -97,7 +97,7 @@ class HomeController extends Controller
/** Build our Time-Left-Box */ /** Build our Time-Left-Box */
if ($credits > 0.01 and $usage > 0) { 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, '.', ''); $hoursLeft = number_format($credits / ($usage / 30 / 24), 2, '.', '');
$bg = $this->getTimeLeftBoxBackground($daysLeft); $bg = $this->getTimeLeftBoxBackground($daysLeft);

View file

@ -43,7 +43,21 @@ class Product extends Model
public function getHourlyPrice() 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() public function getDailyPrice()

View file

@ -251,12 +251,20 @@ class User extends Authenticatable implements MustVerifyEmail
{ {
$usage = 0; $usage = 0;
foreach ($this->getServersWithProduct() as $server) { foreach ($this->getServersWithProduct() as $server) {
$usage += $server->product->price; $usage += $server->product->getHourlyPrice() * 24 * 30;
} }
return number_format($usage, 2, '.', ''); return number_format($usage, 2, '.', '');
} }
private function getServersWithProduct() {
return $this->servers()
->whereNull('suspended')
->whereNull('cancelled')
->with('product')
->get();
}
/** /**
* @return array|string|string[] * @return array|string|string[]
*/ */