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; | ||||
} | } | ||||
/** | /** | ||||
* 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::DECIMAL); | |||||
return $nf->formatCurrency($amount, $this->currency); | |||||
} | |||||
return sprintf('%.2f %s', $amount, $this->currency); | |||||
} | |||||
/** | |||||
* 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() | ||||
{ | { | ||||
return $this->belongsToMany( | return $this->belongsToMany( | ||||
'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 | ||||
); | ); | ||||
} | } | ||||
/** | /** | ||||
* 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 | |||||
* | * | ||||
* @return Wallet Self | * @return Wallet Self | ||||
*/ | */ | ||||
public function credit(int $amount): Wallet | public function credit(int $amount, string $description = ''): Wallet | ||||
{ | { | ||||
$this->balance += $amount; | $this->balance += $amount; | ||||
$this->save(); | $this->save(); | ||||
\App\Transaction::create( | \App\Transaction::create( | ||||
[ | [ | ||||
'user_email' => \App\Utils::userEmailOrNull(), | 'user_email' => \App\Utils::userEmailOrNull(), | ||||
'object_id' => $this->id, | 'object_id' => $this->id, | ||||
'object_type' => \App\Wallet::class, | 'object_type' => \App\Wallet::class, | ||||
'type' => \App\Transaction::WALLET_CREDIT, | 'type' => \App\Transaction::WALLET_CREDIT, | ||||
'amount' => $amount | 'amount' => $amount, | ||||
'description' => $description | |||||
] | ] | ||||
); | ); | ||||
return $this; | return $this; | ||||
} | } | ||||
/** | /** | ||||
* Deduct an amount of pecunia from this wallet's balance. | * Deduct an amount of pecunia from this wallet's balance. | ||||
▲ Show 20 Lines • Show All 142 Lines • Show Last 20 Lines |