Changeset View
Changeset View
Standalone View
Standalone View
src/app/Observers/EntitlementObserver.php
Show First 20 Lines • Show All 91 Lines • ▼ Show 20 Lines | public function deleted(Entitlement $entitlement) | ||||
$entitlement->createTransaction(\App\Transaction::ENTITLEMENT_DELETED); | $entitlement->createTransaction(\App\Transaction::ENTITLEMENT_DELETED); | ||||
} | } | ||||
public function deleting(Entitlement $entitlement) | public function deleting(Entitlement $entitlement) | ||||
{ | { | ||||
// Start calculating the costs for the consumption of this entitlement if the | // Start calculating the costs for the consumption of this entitlement if the | ||||
// existing consumption spans >= 14 days. | // existing consumption spans >= 14 days. | ||||
// anything's free for 14 days | // | ||||
// Effect is that anything's free for the first 14 days | |||||
if ($entitlement->created_at >= Carbon::now()->subDays(14)) { | if ($entitlement->created_at >= Carbon::now()->subDays(14)) { | ||||
return; | return; | ||||
} | } | ||||
$owner = $entitlement->wallet->owner; | |||||
// Determine if we're still within the free first month | |||||
$freeMonthEnds = $owner->created_at->copy()->addMonthsWithoutOverflow(1); | |||||
if ($freeMonthEnds >= Carbon::now()) { | |||||
return; | |||||
} | |||||
$cost = 0; | $cost = 0; | ||||
// get the discount rate applied to the wallet. | // get the discount rate applied to the wallet. | ||||
$discount = $entitlement->wallet->getDiscountRate(); | $discount = $entitlement->wallet->getDiscountRate(); | ||||
// just in case this had not been billed yet, ever | // just in case this had not been billed yet, ever | ||||
$diffInMonths = $entitlement->updated_at->diffInMonths(Carbon::now()); | $diffInMonths = $entitlement->updated_at->diffInMonths(Carbon::now()); | ||||
$cost += (int) ($entitlement->cost * $discount * $diffInMonths); | $cost += (int) ($entitlement->cost * $discount * $diffInMonths); | ||||
Show All 34 Lines |