Changeset View
Changeset View
Standalone View
Standalone View
src/app/Wallet.php
Show First 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | class Wallet extends Model | ||||
{ | { | ||||
if (!$this->controllers->contains($user)) { | if (!$this->controllers->contains($user)) { | ||||
$this->controllers()->save($user); | $this->controllers()->save($user); | ||||
} | } | ||||
} | } | ||||
public function chargeEntitlements($apply = true) | public function chargeEntitlements($apply = true) | ||||
{ | { | ||||
// This wallet has been created less than a month ago, this is the trial period | |||||
if ($this->owner->created_at >= Carbon::now()->subMonthsWithoutOverflow(1)) { | |||||
machniak: Maybe it should be $this->owner->created_at? In case this is a second wallet created later. | |||||
// Move all the current entitlement's updated_at timestamps forward to one month after | |||||
// this wallet was created. | |||||
$freeMonthEnds = $this->owner->created_at->copy()->addMonthsWithoutOverflow(1); | |||||
foreach ($this->entitlements()->get()->fresh() as $entitlement) { | |||||
if ($entitlement->updated_at < $freeMonthEnds) { | |||||
$entitlement->updated_at = $freeMonthEnds; | |||||
$entitlement->save(); | |||||
} | |||||
} | |||||
return 0; | |||||
} | |||||
$charges = 0; | $charges = 0; | ||||
$discount = $this->getDiscountRate(); | $discount = $this->getDiscountRate(); | ||||
DB::beginTransaction(); | DB::beginTransaction(); | ||||
// used to parent individual entitlement billings to the wallet debit. | // used to parent individual entitlement billings to the wallet debit. | ||||
$entitlementTransactions = []; | $entitlementTransactions = []; | ||||
foreach ($this->entitlements()->get()->fresh() as $entitlement) { | foreach ($this->entitlements()->get()->fresh() as $entitlement) { | ||||
// This entitlement has been created less than or equal to 14 days ago (this is at | // This entitlement has been created less than or equal to 14 days ago (this is at | ||||
// maximum the fourteenth 24-hour period). | // maximum the fourteenth 24-hour period). | ||||
if ($entitlement->created_at > Carbon::now()->subDays(14)) { | if ($entitlement->created_at > Carbon::now()->subDays(14)) { | ||||
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? | // updated last more than a month ago -- was it billed? | ||||
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()); | ||||
$cost = (int) ($entitlement->cost * $discount * $diff); | $cost = (int) ($entitlement->cost * $discount * $diff); | ||||
$charges += $cost; | $charges += $cost; | ||||
// 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(); | ||||
if ($cost == 0) { | if ($cost == 0) { | ||||
continue; | continue; | ||||
} | } | ||||
$entitlementTransactions[] = $entitlement->createTransaction( | $entitlementTransactions[] = $entitlement->createTransaction( | ||||
\App\Transaction::ENTITLEMENT_BILLED, | \App\Transaction::ENTITLEMENT_BILLED, | ||||
▲ Show 20 Lines • Show All 278 Lines • Show Last 20 Lines |
Maybe it should be $this->owner->created_at? In case this is a second wallet created later.