Changeset View
Changeset View
Standalone View
Standalone View
src/app/Wallet.php
Show First 20 Lines • Show All 113 Lines • ▼ Show 20 Lines | public function chargeEntitlements($apply = true) | ||||
DB::commit(); | DB::commit(); | ||||
return $charges; | return $charges; | ||||
} | } | ||||
/** | /** | ||||
* Calculate for how long the current balance will last. | * Calculate for how long the current balance will last. | ||||
* | * | ||||
* @return \Carbon\Carbon Date | * Returns NULL for balance < 0 or discount = 100% or on a fresh account | ||||
* | |||||
* @return \Carbon\Carbon|null Date | |||||
*/ | */ | ||||
public function balanceLastsUntil() | public function balanceLastsUntil() | ||||
{ | { | ||||
$balance = $this->balance; | if ($this->balance < 0 || $this->getDiscount() == 100) { | ||||
return null; | |||||
} | |||||
// retrieve any expected charges | // retrieve any expected charges | ||||
$expectedCharge = $this->expectedCharges(); | $expectedCharge = $this->expectedCharges(); | ||||
// get the costs per day for all entitlements billed against this wallet | // get the costs per day for all entitlements billed against this wallet | ||||
$costsPerDay = $this->costsPerDay(); | $costsPerDay = $this->costsPerDay(); | ||||
if (!$costsPerDay) { | |||||
return null; | |||||
} | |||||
// the number of days this balance, minus the expected charges, would last | // the number of days this balance, minus the expected charges, would last | ||||
$daysDelta = ($balance - $expectedCharge) / $costsPerDay; | $daysDelta = ($this->balance - $expectedCharge) / $costsPerDay; | ||||
// calculate from the last entitlement billed | // calculate from the last entitlement billed | ||||
$entitlement = $this->entitlements()->orderBy('updated_at', 'desc')->first(); | $entitlement = $this->entitlements()->orderBy('updated_at', 'desc')->first(); | ||||
return $entitlement->updated_at->copy()->addDays($daysDelta); | $until = $entitlement->updated_at->copy()->addDays($daysDelta); | ||||
} | |||||
/** | // Don't return dates from the past | ||||
* A helper to display human-readable amount of money using | if ($until < Carbon::now() && !$until->isToday()) { | ||||
* the wallet currency and specified locale. | return null; | ||||
* | |||||
* @param int $amount A amount of money (in cents) | |||||
* @param string $locale A locale for the output | |||||
* | |||||
* @return string String representation, e.g. "9.99 CHF" | |||||
*/ | |||||
public function money(int $amount, $locale = 'de_DE') | |||||
{ | |||||
$amount = round($amount / 100, 2); | |||||
// Prefer intl extension's number formatter | |||||
if (class_exists('NumberFormatter')) { | |||||
$nf = new \NumberFormatter($locale, \NumberFormatter::CURRENCY); | |||||
$result = $nf->formatCurrency($amount, $this->currency); | |||||
// Replace non-breaking space | |||||
return str_replace("\xC2\xA0", " ", $result); | |||||
} | } | ||||
return sprintf('%.2f %s', $amount, $this->currency); | return $until; | ||||
} | } | ||||
/** | /** | ||||
* Controllers of this wallet. | * Controllers of this wallet. | ||||
* | * | ||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany | ||||
*/ | */ | ||||
public function controllers() | public function controllers() | ||||
▲ Show 20 Lines • Show All 131 Lines • ▼ Show 20 Lines | class Wallet extends Model | ||||
* Ranges from 0.00 to 1.00. | * Ranges from 0.00 to 1.00. | ||||
*/ | */ | ||||
public function getDiscountRate() | public function getDiscountRate() | ||||
{ | { | ||||
return (100 - $this->getDiscount()) / 100; | return (100 - $this->getDiscount()) / 100; | ||||
} | } | ||||
/** | /** | ||||
* A helper to display human-readable amount of money using | |||||
* the wallet currency and specified locale. | |||||
* | |||||
* @param int $amount A amount of money (in cents) | |||||
* @param string $locale A locale for the output | |||||
* | |||||
* @return string String representation, e.g. "9.99 CHF" | |||||
*/ | |||||
public function money(int $amount, $locale = 'de_DE') | |||||
{ | |||||
$amount = round($amount / 100, 2); | |||||
// Prefer intl extension's number formatter | |||||
if (class_exists('NumberFormatter')) { | |||||
$nf = new \NumberFormatter($locale, \NumberFormatter::CURRENCY); | |||||
$result = $nf->formatCurrency($amount, $this->currency); | |||||
// Replace non-breaking space | |||||
return str_replace("\xC2\xA0", " ", $result); | |||||
} | |||||
return sprintf('%.2f %s', $amount, $this->currency); | |||||
} | |||||
/** | |||||
* The owner of the wallet -- the wallet is in his/her back pocket. | * The owner of the wallet -- the wallet is in his/her back pocket. | ||||
* | * | ||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo | ||||
*/ | */ | ||||
public function owner() | public function owner() | ||||
{ | { | ||||
return $this->belongsTo('App\User', 'user_id', 'id'); | return $this->belongsTo('App\User', 'user_id', 'id'); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 50 Lines • Show Last 20 Lines |