Changeset View
Changeset View
Standalone View
Standalone View
src/app/Wallet.php
Show First 20 Lines • Show All 62 Lines • ▼ Show 20 Lines | public function chargeEntitlements($apply = true) | ||||
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()->subMonths(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? | ||||
if ($entitlement->updated_at <= Carbon::now()->subMonths(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()->addMonths($diff); | $entitlement->updated_at = $entitlement->updated_at->copy()->addMonthsWithoutOverflow($diff); | ||||
$entitlement->save(); | $entitlement->save(); | ||||
$this->debit($entitlement->cost * $diff); | $this->debit($entitlement->cost * $diff); | ||||
} | } | ||||
} | } | ||||
return $charges; | return $charges; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 102 Lines • Show Last 20 Lines |