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 91 Lines • ▼ Show 20 Lines | public function credit(int $amount, string $description = ''): Wallet | ||||
); | ); | ||||
return $this; | return $this; | ||||
} | } | ||||
/** | /** | ||||
* Deduct an amount of pecunia from this wallet's balance. | * Deduct an amount of pecunia from this wallet's balance. | ||||
* | * | ||||
* @param int $amount The amount of pecunia to deduct (in cents). | * @param int $amount The amount of pecunia to deduct (in cents). | ||||
* @param array $eTIDs List of transaction IDs for the individual entitlements that make up | * @param string $description The transaction description | ||||
* this debit record, if any. | * @param array $eTIDs List of transaction IDs for the individual entitlements | ||||
* that make up this debit record, if any. | |||||
* @return Wallet Self | * @return Wallet Self | ||||
*/ | */ | ||||
public function debit(int $amount, array $eTIDs = []): Wallet | public function debit(int $amount, string $description = '', array $eTIDs = []): Wallet | ||||
{ | { | ||||
if ($amount == 0) { | if ($amount == 0) { | ||||
return $this; | return $this; | ||||
} | } | ||||
$this->balance -= $amount; | $this->balance -= $amount; | ||||
$this->save(); | $this->save(); | ||||
$transaction = \App\Transaction::create( | $transaction = \App\Transaction::create( | ||||
[ | [ | ||||
'object_id' => $this->id, | 'object_id' => $this->id, | ||||
'object_type' => \App\Wallet::class, | 'object_type' => \App\Wallet::class, | ||||
'type' => \App\Transaction::WALLET_DEBIT, | 'type' => \App\Transaction::WALLET_DEBIT, | ||||
'amount' => $amount * -1 | 'amount' => $amount * -1, | ||||
'description' => $description | |||||
] | ] | ||||
); | ); | ||||
if (!empty($eTIDs)) { | |||||
\App\Transaction::whereIn('id', $eTIDs)->update(['transaction_id' => $transaction->id]); | \App\Transaction::whereIn('id', $eTIDs)->update(['transaction_id' => $transaction->id]); | ||||
} | |||||
return $this; | return $this; | ||||
} | } | ||||
/** | /** | ||||
* The discount assigned to the wallet. | * The discount assigned to the wallet. | ||||
* | * | ||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo | ||||
▲ Show 20 Lines • Show All 131 Lines • Show Last 20 Lines |