Changeset View
Standalone View
src/app/Wallet.php
Show First 20 Lines • Show All 66 Lines • ▼ Show 20 Lines | public function chargeEntitlements($apply = true) | ||||
continue; | continue; | ||||
} | } | ||||
// This entitlement was created, or billed last, less than a month ago. | // This entitlement was created, or billed last, less than a month ago. | ||||
if ($entitlement->updated_at > Carbon::now()->subMonthsWithoutOverflow(1)) { | if ($entitlement->updated_at > Carbon::now()->subMonthsWithoutOverflow(1)) { | ||||
continue; | continue; | ||||
} | } | ||||
// created more than a month ago -- was it billed? | // created more than a month ago -- was it billed? | ||||
machniak: Put it as a first check, it's the less expensive one. | |||||
Done Inline ActionsSure. vanmeeuwen: Sure. | |||||
if ($entitlement->updated_at <= Carbon::now()->subMonthsWithoutOverflow(1)) { | if ($entitlement->updated_at <= Carbon::now()->subMonthsWithoutOverflow(1)) { | ||||
$diff = $entitlement->updated_at->diffInMonths(Carbon::now()); | $diff = $entitlement->updated_at->diffInMonths(Carbon::now()); | ||||
$charges += $entitlement->cost * $diff; | $charges += $entitlement->cost * $diff; | ||||
// if we're in dry-run, you know... | // if we're in dry-run, you know... | ||||
if (!$apply) { | if (!$apply) { | ||||
continue; | continue; | ||||
} | } | ||||
$entitlement->updated_at = $entitlement->updated_at->copy()->addMonthsWithoutOverflow($diff); | $entitlement->updated_at = $entitlement->updated_at->copy()->addMonthsWithoutOverflow($diff); | ||||
$entitlement->save(); | $entitlement->save(); | ||||
\App\Transaction::create( | |||||
[ | |||||
'wallet_id' => $this->id, | |||||
'amount' => $entitlement->cost * $diff, | |||||
'description' => sprintf( | |||||
'%s was charged for %s', | |||||
$entitlement->sku->title, | |||||
$entitlement->entitleable->toString() | |||||
) | |||||
] | |||||
); | |||||
$this->debit($entitlement->cost * $diff); | $this->debit($entitlement->cost * $diff); | ||||
} | } | ||||
} | } | ||||
return $charges; | return $charges; | ||||
} | } | ||||
/** | /** | ||||
Show All 33 Lines | public function credit(int $amount): Wallet | ||||
$this->save(); | $this->save(); | ||||
return $this; | return $this; | ||||
} | } | ||||
/** | /** | ||||
* Deduct an amount of pecunia from this wallet's balance. | * Deduct an amount of pecunia from this wallet's balance. | ||||
* | * | ||||
Not Done Inline ActionsUse a constant here. machniak: Use a constant here. | |||||
* @param int $amount The amount of pecunia to deduct (in cents). | * @param int $amount The amount of pecunia to deduct (in cents). | ||||
* | * | ||||
* @return Wallet Self | * @return Wallet Self | ||||
*/ | */ | ||||
public function debit(int $amount): Wallet | public function debit(int $amount): Wallet | ||||
Done Inline Actions$eTIDs do not have to be a reference, does it? machniak: $eTIDs do not have to be a reference, does it? | |||||
Done Inline ActionsNo, but why copy if we don't have to? My thinking is that it is potentially a very large list. vanmeeuwen: No, but why copy if we don't have to?
My thinking is that it is potentially a very large list. | |||||
Not Done Inline ActionsPHP is smart. It will not create a copy if you do not modify the local variable. It's called "copy on write". machniak: PHP is smart. It will not create a copy if you do not modify the local variable. It's called… | |||||
{ | { | ||||
$this->balance -= $amount; | $this->balance -= $amount; | ||||
$this->save(); | $this->save(); | ||||
return $this; | return $this; | ||||
Done Inline ActionsWhen displaying transaction log it would be nice to display wallet balance after the operation. My bank for example does this. So, I think it would make sense to store that information with the on-wallet transaction entries (credit/debit/award/penalty). machniak: When displaying transaction log it would be nice to display wallet balance after the operation. | |||||
Done Inline ActionsIf we wanted to in the future, we can derive either the column value or the displayed data by going backwards from "current balance", and I would like to not have to duplicate data that can potentially mismatch (because of rounding or whatever). vanmeeuwen: If we wanted to in the future, we can derive either the column value or the displayed data by… | |||||
Done Inline ActionsYou could, but it would be more complicated/less efficient if you want to archive older entries. machniak: You could, but it would be more complicated/less efficient if you want to archive older entries. | |||||
} | } | ||||
/** | /** | ||||
* Controllers of this wallet. | * Controllers of this wallet. | ||||
* | * | ||||
Done Inline ActionsA contant here. And Wallet could have it's own createTransaction() method too. machniak: A contant here. And Wallet could have it's own createTransaction() method too. | |||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany | ||||
*/ | */ | ||||
public function controllers() | public function controllers() | ||||
{ | { | ||||
return $this->belongsToMany( | return $this->belongsToMany( | ||||
Done Inline ActionsThis probably would be better to do with a single sql query. machniak: This probably would be better to do with a single sql query. | |||||
Done Inline ActionsIs there an $ets->update('transaction_id', $transaction->id); that you know of? I'll investigate, but I would not at this moment consider it a blocker -- and I don't know if $ets->update() would skip observers in a way that is similar to an Something::where('blah', $blah)->delete();. vanmeeuwen: Is there an `$ets->update('transaction_id', $transaction->id);` that you know of?
I'll… | |||||
Done Inline ActionsYes, update() will skip observers, and for this case we don't have observers, so it's all right. You can't call it on $ets here as it's an array, not query builder, so you'd have to build a query using:
machniak: Yes, update() will skip observers, and for this case we don't have observers, so it's all right. | |||||
'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 | ||||
); | ); | ||||
} | } | ||||
/** | /** | ||||
Show All 12 Lines | class Wallet extends Model | ||||
* @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'); | ||||
} | } | ||||
/** | /** | ||||
* Payments on this wallet. | * Transactions on this wallet. | ||||
* | * | ||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany | * @return \Illuminate\Database\Eloquent\Relations\HasMany | ||||
*/ | */ | ||||
public function payments() | public function payments() | ||||
{ | { | ||||
return $this->hasMany('App\Payment'); | return $this->hasMany('App\Transaction'); | ||||
} | } | ||||
} | } |
Put it as a first check, it's the less expensive one.