/** * Get savings accounts. * * @param Carbon $start * @param Carbon $end * * @return Collection */ public function getSavingsAccounts(Carbon $start, Carbon $end) : Collection { $accounts = $this->user->accounts()->accountTypeIn(['Default account', 'Asset account'])->orderBy('accounts.name', 'ASC')->leftJoin('account_meta', 'account_meta.account_id', '=', 'accounts.id')->where('account_meta.name', 'accountRole')->where('accounts.active', 1)->where('account_meta.data', '"savingAsset"')->get(['accounts.*']); $accounts->each(function (Account $account) use($start, $end) { $account->startBalance = Steam::balance($account, $start); $account->endBalance = Steam::balance($account, $end); // diff (negative when lost, positive when gained) $diff = bcsub($account->endBalance, $account->startBalance); if ($diff < 0 && $account->startBalance > 0) { // percentage lost compared to start. $pct = $diff * -1 / $account->startBalance * 100; $pct = $pct > 100 ? 100 : $pct; $account->difference = $diff; $account->percentage = round($pct); return; } if ($diff >= 0 && $account->startBalance > 0) { $pct = $diff / $account->startBalance * 100; $pct = $pct > 100 ? 100 : $pct; $account->difference = $diff; $account->percentage = round($pct); return; } $account->difference = $diff; $account->percentage = 100; }); return $accounts; }
/** * @param array $types * * @return Collection */ public function getActiveAccountsByType(array $types) : Collection { /** @var Collection $result */ $query = $this->user->accounts()->with(['accountmeta' => function (HasMany $query) { $query->where('name', 'accountRole'); }]); if (count($types) > 0) { $query->accountTypeIn($types); } $query->where('active', 1); $result = $query->get(['accounts.*']); $result = $result->sortBy(function (Account $account) { return strtolower($account->name); }); return $result; }
/** * @param User $user * @param $name * * @return Account|null */ public static function findAccount(User $user, $name) { /** @var Account $account */ foreach ($user->accounts()->get() as $account) { if ($account->name == $name) { Log::debug('Trying to find "' . $name . '" in "' . $account->name . '", and found it!'); return $account; } Log::debug('Trying to find "' . $name . '" in "' . $account->name . '".'); } return null; }