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; | |||||
$changelogs = \App\Changelog::where('wallet_id', $this->id)->latest()->get(); | |||||
foreach ($changelogs as $entry) { | |||||
// only if irrefundable | |||||
switch ($entry->action) { | |||||
case 'creating': | |||||
if ($entry->created_at < Carbon::now()->subDays(14)) { | |||||
if ($entry->created_at >= Carbon::now()->subMonths(1)) { | |||||
$charges += $entry->cost; | |||||
} else { | |||||
$diff = $entry->created_at->diffInMonths(Carbon::now()); | |||||
$charges += $diff * $entry->cost; | |||||
} | |||||
} | |||||
break; | |||||
case 'charging': | |||||
$charges -= $entry->cost; | |||||
break; | |||||
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… | |||||
default: | |||||
break; | |||||
} | |||||
} | |||||
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 20 Lines • Show All 72 Lines • Show Last 20 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?