Changeset View
Changeset View
Standalone View
Standalone View
src/app/Wallet.php
<?php | <?php | ||||
namespace App; | namespace App; | ||||
use App\User; | use App\User; | ||||
use Carbon\Carbon; | |||||
use Iatstuti\Database\Support\NullableFields; | use Iatstuti\Database\Support\NullableFields; | ||||
use Illuminate\Database\Eloquent\Model; | use Illuminate\Database\Eloquent\Model; | ||||
/** | /** | ||||
* The eloquent definition of a wallet -- a container with a chunk of change. | * The eloquent definition of a wallet -- a container with a chunk of change. | ||||
* | * | ||||
* A wallet is owned by an {@link \App\User}. | * A wallet is owned by an {@link \App\User}. | ||||
* | * | ||||
Show All 37 Lines | class Wallet extends Model | ||||
public function addController(User $user) | public function addController(User $user) | ||||
{ | { | ||||
if (!$this->controllers->contains($user)) { | if (!$this->controllers->contains($user)) { | ||||
$this->controllers()->save($user); | $this->controllers()->save($user); | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Calculate the expected charges to this wallet. | |||||
* | |||||
* @return int | |||||
*/ | |||||
public function expectedCharges() | |||||
{ | |||||
$charges = 0; | |||||
foreach ($this->entitlements()->get()->fresh() as $entitlement) { | |||||
$entry = \App\Changelog::withTrashed() | |||||
->where('entitlement_id', $entitlement->id)->latest()->first(); | |||||
// created less than or equal to 14 days ago (this is the fourteenth 24-hour period) | |||||
if ($entry->updated_at >= Carbon::now()->subDays(14)) { | |||||
continue; | |||||
} | |||||
// created less than or equal to a month ago, can't have been billed | |||||
if ($entry->updated_at >= Carbon::now()->subMonths(1)) { | |||||
$charges += $entitlement->cost; | |||||
} | |||||
// created more than a month ago -- was it billed? | |||||
if ($entry->updated_at < Carbon::now()->subMonths(1)) { | |||||
$diff = $entry->updated_at->diffInMonths(Carbon::now()); | |||||
$charges += $entitlement->cost * $diff; | |||||
} | |||||
if ($entry->deleted_at) { | |||||
// TODO | |||||
machniak: I guess we should use db transactions here. Also, I'm thinking that we may anyway need a… | |||||
Done Inline ActionsThis would be in some sort of transactions table, for sure -- I just don't want a future use of cashier (stripe/braintree) to have us need to redo whatever that transactions table is. vanmeeuwen: This would be in some sort of transactions table, for sure -- I just don't want a future use of… | |||||
\Log::debug("should consider deleted_at"); | |||||
} | |||||
} | |||||
return $charges; | |||||
} | |||||
/** | |||||
* Remove a controller from this wallet. | * Remove a controller from this wallet. | ||||
* | * | ||||
* @param \App\User $user The user to remove as a controller from this wallet. | * @param \App\User $user The user to remove as a controller from this wallet. | ||||
* | * | ||||
* @return void | * @return void | ||||
*/ | */ | ||||
public function removeController(User $user) | public function removeController(User $user) | ||||
{ | { | ||||
Show All 22 Lines | class Wallet extends Model | ||||
* Deduct an amount of pecunia from this wallet's balance. | * Deduct an amount of pecunia from this wallet's balance. | ||||
* | * | ||||
* @param float $amount The amount of pecunia to deduct. | * @param float $amount The amount of pecunia to deduct. | ||||
* | * | ||||
* @return Wallet | * @return Wallet | ||||
*/ | */ | ||||
public function debit(float $amount) | public function debit(float $amount) | ||||
{ | { | ||||
\Log::debug("debitting wallet {$this->id} with {$amount}"); | |||||
$this->balance -= $amount; | $this->balance -= $amount; | ||||
$this->save(); | $this->save(); | ||||
return $this; | return $this; | ||||
} | } | ||||
/** | /** | ||||
Show All 34 Lines |
I guess we should use db transactions here. Also, I'm thinking that we may anyway need a changelog table to have easy access to what/when was charged?