Changeset View
Changeset View
Standalone View
Standalone View
src/app/Wallet.php
Show First 20 Lines • Show All 70 Lines • ▼ Show 20 Lines | public function chargeEntitlements($apply = true) | ||||
$entitlement->updated_at = $freeMonthEnds; | $entitlement->updated_at = $freeMonthEnds; | ||||
$entitlement->save(); | $entitlement->save(); | ||||
} | } | ||||
} | } | ||||
return 0; | return 0; | ||||
} | } | ||||
$profit = 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 = []; | ||||
Show All 9 Lines | public function chargeEntitlements($apply = true) | ||||
continue; | continue; | ||||
} | } | ||||
// updated last 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); | ||||
$fee = (int) ($entitlement->fee * $diff); | |||||
$charges += $cost; | $charges += $cost; | ||||
$profit += $cost - $fee; | |||||
// 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() | $entitlement->updated_at = $entitlement->updated_at->copy() | ||||
->addMonthsWithoutOverflow($diff); | ->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, | ||||
$cost | $cost | ||||
); | ); | ||||
} | } | ||||
} | } | ||||
if ($apply) { | if ($apply) { | ||||
$this->debit($charges, $entitlementTransactions); | $this->debit($charges, $entitlementTransactions); | ||||
// Credit/debit the reseller | |||||
if ($profit != 0 && $this->owner->tenant) { | |||||
// FIXME: Should we have a simpler way to skip this for non-reseller tenant(s) | |||||
if ($wallet = $this->owner->tenant->wallet()) { | |||||
$desc = "Charged user {$this->owner->email}"; | |||||
$method = $profit > 0 ? 'credit' : 'debit'; | |||||
$wallet->{$method}(abs($profit), $desc); | |||||
} | |||||
} | |||||
} | } | ||||
DB::commit(); | DB::commit(); | ||||
return $charges; | return $charges; | ||||
} | } | ||||
/** | /** | ||||
▲ Show 20 Lines • Show All 263 Lines • Show Last 20 Lines |