Changeset View
Changeset View
Standalone View
Standalone View
src/app/Wallet.php
Show First 20 Lines • Show All 111 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. | |||||
* | |||||
* @return \Carbon\Carbon Date | |||||
*/ | |||||
public function balanceLastsUntil() | |||||
{ | |||||
$balance = $this->balance; | |||||
// retrieve any expected charges | |||||
$expectedCharge = $this->expectedCharges(); | |||||
// get the costs per day for all entitlements billed against this wallet | |||||
$costsPerDay = $this->costsPerDay(); | |||||
// the number of days this balance, minus the expected charges, would last | |||||
$daysDelta = ($balance - $expectedCharge) / $costsPerDay; | |||||
// calculate from the last entitlement billed | |||||
$entitlement = $this->entitlements()->orderBy('updated_at', 'desc')->first(); | |||||
return $entitlement->updated_at->copy()->addDays($daysDelta); | |||||
} | |||||
/** | |||||
* A helper to display human-readable amount of money using | * A helper to display human-readable amount of money using | ||||
* the wallet currency and specified locale. | * the wallet currency and specified locale. | ||||
* | * | ||||
* @param int $amount A amount of money (in cents) | * @param int $amount A amount of money (in cents) | ||||
* @param string $locale A locale for the output | * @param string $locale A locale for the output | ||||
* | * | ||||
* @return string String representation, e.g. "9.99 CHF" | * @return string String representation, e.g. "9.99 CHF" | ||||
*/ | */ | ||||
Show All 23 Lines | public function controllers() | ||||
'App\User', // The foreign object definition | 'App\User', // The foreign object definition | ||||
'user_accounts', // The table name | 'user_accounts', // The table name | ||||
'wallet_id', // The local foreign key | 'wallet_id', // The local foreign key | ||||
'user_id' // The remote foreign key | 'user_id' // The remote foreign key | ||||
); | ); | ||||
} | } | ||||
/** | /** | ||||
* Retrieve the costs per day of everything charged to this wallet. | |||||
* | |||||
* @return float | |||||
*/ | |||||
public function costsPerDay() | |||||
{ | |||||
$costs = (float) 0; | |||||
foreach ($this->entitlements as $entitlement) { | |||||
$costs += $entitlement->costsPerDay(); | |||||
} | |||||
return $costs; | |||||
} | |||||
/** | |||||
* Add an amount of pecunia to this wallet's balance. | * Add an amount of pecunia to this wallet's balance. | ||||
* | * | ||||
* @param int $amount The amount of pecunia to add (in cents). | * @param int $amount The amount of pecunia to add (in cents). | ||||
* @param string $description The transaction description | * @param string $description The transaction description | ||||
* | * | ||||
* @return Wallet Self | * @return Wallet Self | ||||
*/ | */ | ||||
public function credit(int $amount, string $description = ''): Wallet | public function credit(int $amount, string $description = ''): Wallet | ||||
▲ Show 20 Lines • Show All 163 Lines • Show Last 20 Lines |