Code cleanup in AccountForm.

This commit is contained in:
James Cole
2019-08-10 17:11:57 +02:00
parent 3daddd690f
commit c0033ae56b
4 changed files with 126 additions and 170 deletions

View File

@@ -55,31 +55,25 @@ class Steam
$cache->addProperty('balance');
$cache->addProperty($date);
if ($cache->has()) {
//return $cache->get(); // @codeCoverageIgnore
return $cache->get(); // @codeCoverageIgnore
}
//
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
$currencyId = (int)$repository->getMetaValue($account, 'currency_id');
$currency = $repository->getAccountCurrency($account) ?? app('amount')->getDefaultCurrencyByUser($account->user);
// use system default currency:
if (0 === $currencyId) {
$currency = app('amount')->getDefaultCurrencyByUser($account->user);
$currencyId = $currency->id;
}
// first part: get all balances in own currency:
$nativeBalance = (string)$account->transactions()
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->where('transaction_journals.date', '<=', $date->format('Y-m-d 23:59:59'))
->where('transactions.transaction_currency_id', $currencyId)
->where('transactions.transaction_currency_id', $currency->id)
->sum('transactions.amount');
// get all balances in foreign currency:
$foreignBalance = (string)$account->transactions()
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->where('transaction_journals.date', '<=', $date->format('Y-m-d'))
->where('transactions.foreign_currency_id', $currencyId)
->where('transactions.transaction_currency_id', '!=', $currencyId)
->where('transactions.foreign_currency_id', $currency->id)
->where('transactions.transaction_currency_id', '!=', $currency->id)
->sum('transactions.foreign_amount');
$balance = bcadd($nativeBalance, $foreignBalance);