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 36 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); | ||||
} | } | ||||
} | } | ||||
public function chargeEntitlements($apply = true) | |||||
{ | |||||
$charges = 0; | |||||
foreach ($this->entitlements()->get()->fresh() as $entitlement) { | |||||
// This entitlement has been created less than or equal to 14 days ago (this is at | |||||
// maximum the fourteenth 24-hour period). | |||||
if ($entitlement->created_at > Carbon::now()->subDays(14)) { | |||||
continue; | |||||
} | |||||
// This entitlement was created, or billed last, less than a month ago. | |||||
if ($entitlement->updated_at > Carbon::now()->subMonths(1)) { | |||||
continue; | |||||
} | |||||
// created more than a month ago -- was it billed? | |||||
if ($entitlement->updated_at <= Carbon::now()->subMonths(1)) { | |||||
$diff = $entitlement->updated_at->diffInMonths(Carbon::now()); | |||||
$charges += $entitlement->cost * $diff; | |||||
// if we're in dry-run, you know... | |||||
if (!$apply) { | |||||
continue; | |||||
} | |||||
$entitlement->updated_at = $entitlement->updated_at->copy()->addMonths($diff); | |||||
$entitlement->save(); | |||||
$this->debit($entitlement->cost * $diff); | |||||
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… | |||||
} | |||||
} | |||||
return $charges; | |||||
} | |||||
/** | |||||
* Calculate the expected charges to this wallet. | |||||
* | |||||
* @return int | |||||
*/ | |||||
public function expectedCharges() | |||||
{ | |||||
return $this->chargeEntitlements(false); | |||||
} | |||||
/** | /** | ||||
* 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 73 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?