# Changeset View

Changeset View

# Standalone View

Standalone View

# src/app/Observers/EntitlementObserver.php

Show First 20 Lines • Show All 117 Lines • ▼ Show 20 Line(s) | |||||

// Determine if we're still within the free first month | // Determine if we're still within the free first month | ||||

$freeMonthEnds = $owner->created_at->copy()->addMonthsWithoutOverflow(1); | $freeMonthEnds = $owner->created_at->copy()->addMonthsWithoutOverflow(1); | ||||

if ($freeMonthEnds >= Carbon::now()) { | if ($freeMonthEnds >= Carbon::now()) { | ||||

return; | return; | ||||

} | } | ||||

$cost = 0; | |||||

$now = Carbon::now(); | $now = Carbon::now(); | ||||

// 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($now); | $diffInMonths = $entitlement->updated_at->diffInMonths($now); | ||||

$cost += (int) ($entitlement->cost * $discount * $diffInMonths); | $cost = (int) ($entitlement->cost * $discount * $diffInMonths); | ||||

$fee = (int) ($entitlement->fee * $diffInMonths); | |||||

// this moves the hypothetical updated at forward to however many months past the original | // this moves the hypothetical updated at forward to however many months past the original | ||||

$updatedAt = $entitlement->updated_at->copy()->addMonthsWithoutOverflow($diffInMonths); | $updatedAt = $entitlement->updated_at->copy()->addMonthsWithoutOverflow($diffInMonths); | ||||

// now we have the diff in days since the last "billed" period end. | // now we have the diff in days since the last "billed" period end. | ||||

// This may be an entitlement paid up until February 28th, 2020, with today being March | // This may be an entitlement paid up until February 28th, 2020, with today being March | ||||

// 12th 2020. Calculating the costs for the entitlement is based on the daily price | // 12th 2020. Calculating the costs for the entitlement is based on the daily price | ||||

// the price per day is based on the number of days in the last month | // the price per day is based on the number of days in the last month | ||||

// or the current month if the period does not overlap with the previous month | // or the current month if the period does not overlap with the previous month | ||||

// FIXME: This really should be simplified to $daysInMonth=30 | // FIXME: This really should be simplified to $daysInMonth=30 | ||||

$diffInDays = $updatedAt->diffInDays($now); | $diffInDays = $updatedAt->diffInDays($now); | ||||

if ($now->day >= $diffInDays) { | if ($now->day >= $diffInDays) { | ||||

$daysInMonth = $now->daysInMonth; | $daysInMonth = $now->daysInMonth; | ||||

} else { | } else { | ||||

$daysInMonth = \App\Utils::daysInLastMonth(); | $daysInMonth = \App\Utils::daysInLastMonth(); | ||||

} | } | ||||

$pricePerDay = $entitlement->cost / $daysInMonth; | $pricePerDay = $entitlement->cost / $daysInMonth; | ||||

$feePerDay = $entitlement->fee / $daysInMonth; | |||||

$cost += (int) (round($pricePerDay * $discount * $diffInDays, 0)); | $cost += (int) (round($pricePerDay * $discount * $diffInDays, 0)); | ||||

$fee += (int) (round($feePerDay * $diffInDays, 0)); | |||||

$profit = $cost - $fee; | |||||

if ($profit != 0 && $owner->tenant && ($wallet = $owner->tenant->wallet())) { | |||||

$desc = "Charged user {$owner->email}"; | |||||

$method = $profit > 0 ? 'credit' : 'debit'; | |||||

$wallet->{$method}(abs($profit), $desc); | |||||

} | |||||

if ($cost == 0) { | if ($cost == 0) { | ||||

return; | return; | ||||

} | } | ||||

$entitlement->wallet->debit($cost); | $entitlement->wallet->debit($cost); | ||||

} | } | ||||

} | } |