Code cleanup in AccountForm.

This commit is contained in:
James Cole 2019-08-10 17:11:57 +02:00
parent 3daddd690f
commit c0033ae56b
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
4 changed files with 126 additions and 170 deletions

View File

@ -23,7 +23,6 @@ declare(strict_types=1);
namespace FireflyIII\Support; namespace FireflyIII\Support;
use Crypt; use Crypt;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionCurrency;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Contracts\Encryption\DecryptException; use Illuminate\Contracts\Encryption\DecryptException;
@ -269,8 +268,6 @@ class Amount
/** /**
* @return \FireflyIII\Models\TransactionCurrency * @return \FireflyIII\Models\TransactionCurrency
*
* @throws \FireflyIII\Exceptions\FireflyException
*/ */
public function getDefaultCurrency(): TransactionCurrency public function getDefaultCurrency(): TransactionCurrency
{ {
@ -287,8 +284,6 @@ class Amount
* @param User $user * @param User $user
* *
* @return \FireflyIII\Models\TransactionCurrency * @return \FireflyIII\Models\TransactionCurrency
*
* @throws \FireflyIII\Exceptions\FireflyException
*/ */
public function getDefaultCurrencyByUser(User $user): TransactionCurrency public function getDefaultCurrencyByUser(User $user): TransactionCurrency
{ {
@ -309,12 +304,13 @@ class Amount
// could still be json encoded: // could still be json encoded:
if (strlen($currencyCode) > 3) { if (strlen($currencyCode) > 3) {
$currencyCode = json_decode($currencyCode) ?? 'EUR'; $currencyCode = json_decode($currencyCode, true) ?? 'EUR';
} }
$currency = TransactionCurrency::where('code', $currencyCode)->first(); $currency = TransactionCurrency::where('code', $currencyCode)->first();
if (null === $currency) { if (null === $currency) {
throw new FireflyException(sprintf('No currency found with code "%s"', $currencyCode)); // get EUR
$currency = TransactionCurrency::where('code', 'EUR')->first();
} }
$cache->store($currency); $cache->store($currency);

View File

@ -22,11 +22,8 @@
namespace FireflyIII\Support\Form; namespace FireflyIII\Support\Form;
use Carbon\Carbon;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType; use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use Log; use Log;
use Throwable; use Throwable;
@ -43,7 +40,7 @@ class AccountForm
use FormSupport; use FormSupport;
/** /**
* TODO describe. * Shows a <select> with all active asset accounts.
* *
* @param string $name * @param string $name
* @param mixed $value * @param mixed $value
@ -53,41 +50,30 @@ class AccountForm
*/ */
public function activeAssetAccountList(string $name, $value = null, array $options = null): string public function activeAssetAccountList(string $name, $value = null, array $options = null): string
{ {
// make repositories $repository = $this->getAccountRepository();
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
/** @var CurrencyRepositoryInterface $currencyRepos */
$currencyRepos = app(CurrencyRepositoryInterface::class);
$accountList = $repository->getActiveAccountsByType([AccountType::ASSET, AccountType::DEFAULT]); $accountList = $repository->getActiveAccountsByType([AccountType::ASSET, AccountType::DEFAULT]);
$defaultCurrency = app('amount')->getDefaultCurrency(); $defaultCurrency = app('amount')->getDefaultCurrency();
$grouped = []; $grouped = [];
// group accounts: $date = $this->getDate();
/** @var Account $account */ /** @var Account $account */
foreach ($accountList as $account) { foreach ($accountList as $account) {
$balance = app('steam')->balance($account, new Carbon); $balance = app('steam')->balance($account, $date);
$currencyId = (int)$repository->getMetaValue($account, 'currency_id');
$currency = $currencyRepos->findNull($currencyId);
$role = $repository->getMetaValue($account, 'account_role'); $role = $repository->getMetaValue($account, 'account_role');
if ('' === $role) { $currency = $repository->getAccountCurrency($account) ?? $defaultCurrency;
$role = 'no_account_type'; // @codeCoverageIgnore $role = '' === $role ? 'no_account_type' : $role;
} $key = (string)trans(sprintf('firefly.opt_group_%s', $role));
$formatted = app('amount')->formatAnything($currency, $balance, false);
if (null === $currency) { $name = sprintf('%s (%s)', $account->name, $formatted);
$currency = $defaultCurrency; $grouped[$key][$account->id] = $name;
}
$key = (string)trans('firefly.opt_group_' . $role);
$grouped[$key][$account->id] = $account->name . ' (' . app('amount')->formatAnything($currency, $balance, false) . ')';
} }
return $this->select($name, $grouped, $value, $options); return $this->select($name, $grouped, $value, $options);
} }
/** /**
* TODO describe. * Return a list that includes liabilities.
* *
* @param string $name * @param string $name
* @param mixed $value * @param mixed $value
@ -97,34 +83,32 @@ class AccountForm
*/ */
public function activeLongAccountList(string $name, $value = null, array $options = null): string public function activeLongAccountList(string $name, $value = null, array $options = null): string
{ {
// make repositories $types = [AccountType::ASSET, AccountType::DEFAULT, AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN,];
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
$accountList = $repository->getActiveAccountsByType(
[AccountType::ASSET, AccountType::DEFAULT, AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN,]
);
$liabilityTypes = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN]; $liabilityTypes = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN];
$repository = $this->getAccountRepository();
$accountList = $repository->getActiveAccountsByType($types);
$defaultCurrency = app('amount')->getDefaultCurrency(); $defaultCurrency = app('amount')->getDefaultCurrency();
$grouped = []; $grouped = [];
// group accounts: $date = $this->getDate();
/** @var Account $account */ /** @var Account $account */
foreach ($accountList as $account) { foreach ($accountList as $account) {
$balance = app('steam')->balance($account, new Carbon); $balance = app('steam')->balance($account, $date);
$currency = $repository->getAccountCurrency($account) ?? $defaultCurrency; $currency = $repository->getAccountCurrency($account) ?? $defaultCurrency;
$role = $repository->getMetaValue($account, 'account_role'); $role = $repository->getMetaValue($account, 'account_role');
if ('' === $role && !in_array($account->accountType->type, $liabilityTypes, true)) { if ('' === $role && !in_array($account->accountType->type, $liabilityTypes, true)) {
$role = 'no_account_type'; // @codeCoverageIgnore $role = 'no_account_type';
} }
if (in_array($account->accountType->type, $liabilityTypes, true)) { if (in_array($account->accountType->type, $liabilityTypes, true)) {
$role = 'l_' . $account->accountType->type; // @codeCoverageIgnore $role = sprintf('l_%s', $account->accountType->type);
} }
$key = (string)trans('firefly.opt_group_' . $role);
$grouped[$key][$account->id] = $account->name . ' (' . app('amount')->formatAnything($currency, $balance, false) . ')'; $key = (string)trans(sprintf('firefly.opt_group_%s', $role));
$formatted = app('amount')->formatAnything($currency, $balance, false);
$name = sprintf('%s (%s)', $account->name, $formatted);
$grouped[$key][$account->id] = $name;
} }
@ -132,8 +116,6 @@ class AccountForm
} }
/** /**
* TODO clean up.
*
* Grouped dropdown list of all accounts that are valid as the destination of a withdrawal. * Grouped dropdown list of all accounts that are valid as the destination of a withdrawal.
* *
* @param string $name * @param string $name
@ -144,24 +126,14 @@ class AccountForm
*/ */
public function activeWithdrawalDestinations(string $name, $value = null, array $options = null): string public function activeWithdrawalDestinations(string $name, $value = null, array $options = null): string
{ {
// make repositories $types = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN, AccountType::EXPENSE,];
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
$accountList = $repository->getActiveAccountsByType(
[
AccountType::MORTGAGE,
AccountType::DEBT,
AccountType::CREDITCARD,
AccountType::LOAN,
AccountType::EXPENSE,
]
);
$liabilityTypes = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN]; $liabilityTypes = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN];
$repository = $this->getAccountRepository();
$accountList = $repository->getActiveAccountsByType($types);
$defaultCurrency = app('amount')->getDefaultCurrency(); $defaultCurrency = app('amount')->getDefaultCurrency();
$grouped = []; $grouped = [];
$date = $this->getDate();
// add cash account first:
$cash = $repository->getCashAccount(); $cash = $repository->getCashAccount();
$key = (string)trans('firefly.cash_account_type'); $key = (string)trans('firefly.cash_account_type');
$grouped[$key][$cash->id] = sprintf('(%s)', (string)trans('firefly.cash')); $grouped[$key][$cash->id] = sprintf('(%s)', (string)trans('firefly.cash'));
@ -169,34 +141,30 @@ class AccountForm
// group accounts: // group accounts:
/** @var Account $account */ /** @var Account $account */
foreach ($accountList as $account) { foreach ($accountList as $account) {
$balance = app('steam')->balance($account, new Carbon); $balance = app('steam')->balance($account, $date);
$currency = $repository->getAccountCurrency($account) ?? $defaultCurrency; $currency = $repository->getAccountCurrency($account) ?? $defaultCurrency;
$role = (string)$repository->getMetaValue($account, 'account_role'); $role = (string)$repository->getMetaValue($account, 'account_role');
if ('' === $role && !in_array($account->accountType->type, $liabilityTypes, true)) { if ('' === $role && !in_array($account->accountType->type, $liabilityTypes, true)) {
$role = 'no_account_type'; // @codeCoverageIgnore $role = 'no_account_type';
} }
if ('no_account_type' === $role && AccountType::EXPENSE === $account->accountType->type) {
$role = 'expense_account'; // @codeCoverageIgnore
if ('no_account_type' === $role && AccountType::EXPENSE === $account->accountType->type) {
$role = 'expense_account';
} }
if (in_array($account->accountType->type, $liabilityTypes, true)) { if (in_array($account->accountType->type, $liabilityTypes, true)) {
$role = 'l_' . $account->accountType->type; // @codeCoverageIgnore $role = sprintf('l_%s', $account->accountType->type);
} }
if (null === $currency) {
$currency = $defaultCurrency;
}
$key = (string)trans('firefly.opt_group_' . $role); $key = (string)trans('firefly.opt_group_' . $role);
$grouped[$key][$account->id] = $account->name . ' (' . app('amount')->formatAnything($currency, $balance, false) . ')'; $formatted = app('amount')->formatAnything($currency, $balance, false);
$name = sprintf('%s (%s)', $account->name, $formatted);
$grouped[$key][$account->id] = $name;
} }
return $this->select($name, $grouped, $value, $options); return $this->select($name, $grouped, $value, $options);
} }
/** /**
* TODO cleanup.
* Grouped dropdown list of all accounts that are valid as the destination of a withdrawal. * Grouped dropdown list of all accounts that are valid as the destination of a withdrawal.
* *
* @param string $name * @param string $name
@ -207,24 +175,13 @@ class AccountForm
*/ */
public function activeDepositDestinations(string $name, $value = null, array $options = null): string public function activeDepositDestinations(string $name, $value = null, array $options = null): string
{ {
// make repositories $types = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN, AccountType::REVENUE,];
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
$accountList = $repository->getActiveAccountsByType(
[
AccountType::MORTGAGE,
AccountType::DEBT,
AccountType::CREDITCARD,
AccountType::LOAN,
AccountType::REVENUE,
]
);
$liabilityTypes = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN]; $liabilityTypes = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN];
$repository = $this->getAccountRepository();
$accountList = $repository->getActiveAccountsByType($types);
$defaultCurrency = app('amount')->getDefaultCurrency(); $defaultCurrency = app('amount')->getDefaultCurrency();
$grouped = []; $grouped = [];
$date = $this->getDate();
// add cash account first:
$cash = $repository->getCashAccount(); $cash = $repository->getCashAccount();
$key = (string)trans('firefly.cash_account_type'); $key = (string)trans('firefly.cash_account_type');
$grouped[$key][$cash->id] = sprintf('(%s)', (string)trans('firefly.cash')); $grouped[$key][$cash->id] = sprintf('(%s)', (string)trans('firefly.cash'));
@ -232,23 +189,22 @@ class AccountForm
// group accounts: // group accounts:
/** @var Account $account */ /** @var Account $account */
foreach ($accountList as $account) { foreach ($accountList as $account) {
$balance = app('steam')->balance($account, new Carbon); $balance = app('steam')->balance($account, $date);
$currency = $repository->getAccountCurrency($account) ?? $defaultCurrency; $currency = $repository->getAccountCurrency($account) ?? $defaultCurrency;
$role = (string)$repository->getMetaValue($account, 'account_role'); $role = (string)$repository->getMetaValue($account, 'account_role');
if ('' === $role && !in_array($account->accountType->type, $liabilityTypes, true)) { if ('' === $role && !in_array($account->accountType->type, $liabilityTypes, true)) {
$role = 'no_account_type'; // @codeCoverageIgnore $role = 'no_account_type';
} }
if ('no_account_type' === $role && AccountType::REVENUE === $account->accountType->type) { if ('no_account_type' === $role && AccountType::REVENUE === $account->accountType->type) {
$role = 'revenue_account'; // @codeCoverageIgnore $role = 'revenue_account';
} }
if (in_array($account->accountType->type, $liabilityTypes, true)) { if (in_array($account->accountType->type, $liabilityTypes, true)) {
$role = 'l_' . $account->accountType->type; // @codeCoverageIgnore $role = sprintf('l_%s', $account->accountType->type); // @codeCoverageIgnore
} }
$key = (string)trans(sprintf('firefly.opt_group_%s', $role));
$key = (string)trans('firefly.opt_group_' . $role); $formatted = app('amount')->formatAnything($currency, $balance, false);
$grouped[$key][$account->id] = $account->name . ' (' . app('amount')->formatAnything($currency, $balance, false) . ')'; $name = sprintf('%s (%s)', $account->name, $formatted);
$grouped[$key][$account->id] = $name;
} }
return $this->select($name, $grouped, $value, $options); return $this->select($name, $grouped, $value, $options);
@ -256,7 +212,7 @@ class AccountForm
/** /**
* TODO describe and cleanup. * Check list of asset accounts.
* *
* @param string $name * @param string $name
* @param array $options * @param array $options
@ -273,18 +229,18 @@ class AccountForm
$selected = request()->old($name) ?? []; $selected = request()->old($name) ?? [];
// get all asset accounts: // get all asset accounts:
/** @var AccountRepositoryInterface $repository */ $repository = $this->getAccountRepository();
$repository = app(AccountRepositoryInterface::class); $types = [AccountType::ASSET, AccountType::DEFAULT];
$assetAccounts = $repository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT]); $assetAccounts = $repository->getAccountsByType($types);
$grouped = []; $grouped = [];
// group accounts: // group accounts:
/** @var Account $account */ /** @var Account $account */
foreach ($assetAccounts as $account) { foreach ($assetAccounts as $account) {
$role = $repository->getMetaValue($account, 'account_role'); $role = $repository->getMetaValue($account, 'account_role');
if (null === $role) { if (null === $role) {
$role = 'no_account_type'; // @codeCoverageIgnore $role = 'no_account_type';
} }
$key = (string)trans('firefly.opt_group_' . $role); $key = (string)trans(sprintf('firefly.opt_group_%s', $role));
$grouped[$key][$account->id] = $account->name; $grouped[$key][$account->id] = $account->name;
} }
@ -300,7 +256,7 @@ class AccountForm
} }
/** /**
* TODO describe and cleanup. * Basic list of asset accounts.
* *
* @param string $name * @param string $name
* @param mixed $value * @param mixed $value
@ -310,32 +266,25 @@ class AccountForm
*/ */
public function assetAccountList(string $name, $value = null, array $options = null): string public function assetAccountList(string $name, $value = null, array $options = null): string
{ {
// make repositories $repository = $this->getAccountRepository();
/** @var AccountRepositoryInterface $repository */ $types = [AccountType::ASSET, AccountType::DEFAULT];
$repository = app(AccountRepositoryInterface::class); $accountList = $repository->getAccountsByType($types);
/** @var CurrencyRepositoryInterface $currencyRepos */
$currencyRepos = app(CurrencyRepositoryInterface::class);
$accountList = $repository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT]);
$defaultCurrency = app('amount')->getDefaultCurrency(); $defaultCurrency = app('amount')->getDefaultCurrency();
$grouped = []; $grouped = [];
// group accounts: $date = $this->getDate();
/** @var Account $account */ /** @var Account $account */
foreach ($accountList as $account) { foreach ($accountList as $account) {
$balance = app('steam')->balance($account, new Carbon); $balance = app('steam')->balance($account, $date);
$currencyId = (int)$repository->getMetaValue($account, 'currency_id'); $currency = $repository->getAccountCurrency($account) ?? $defaultCurrency;
$currency = $currencyRepos->findNull($currencyId);
$role = (string)$repository->getMetaValue($account, 'account_role'); $role = (string)$repository->getMetaValue($account, 'account_role');
if ('' === $role) { if ('' === $role) {
$role = 'no_account_type'; // @codeCoverageIgnore $role = 'no_account_type';
} }
if (null === $currency) { $key = (string)trans(sprintf('firefly.opt_group_%s', $role));
$currency = $defaultCurrency; $formatted = app('amount')->formatAnything($currency, $balance, false);
} $grouped[$key][$account->id] = sprintf('%s (%s)', $account->name, $formatted);
$key = (string)trans('firefly.opt_group_' . $role);
$grouped[$key][$account->id] = $account->name . ' (' . app('amount')->formatAnything($currency, $balance, false) . ')';
} }
return $this->select($name, $grouped, $value, $options); return $this->select($name, $grouped, $value, $options);
@ -343,7 +292,8 @@ class AccountForm
/** /**
* TODO decribe and cleanup * Same list but all liabilities as well.
*
* @param string $name * @param string $name
* @param mixed $value * @param mixed $value
* @param array $options * @param array $options
@ -352,39 +302,27 @@ class AccountForm
*/ */
public function longAccountList(string $name, $value = null, array $options = null): string public function longAccountList(string $name, $value = null, array $options = null): string
{ {
// make repositories $types = [AccountType::ASSET, AccountType::DEFAULT, AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN,];
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
/** @var CurrencyRepositoryInterface $currencyRepos */
$currencyRepos = app(CurrencyRepositoryInterface::class);
$accountList = $repository->getAccountsByType(
[AccountType::ASSET, AccountType::DEFAULT, AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN,]
);
$liabilityTypes = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN]; $liabilityTypes = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN];
$repository = $this->getAccountRepository();
$accountList = $repository->getAccountsByType($types);
$defaultCurrency = app('amount')->getDefaultCurrency(); $defaultCurrency = app('amount')->getDefaultCurrency();
$grouped = []; $grouped = [];
// group accounts: $date = $this->getDate();
/** @var Account $account */ /** @var Account $account */
foreach ($accountList as $account) { foreach ($accountList as $account) {
$balance = app('steam')->balance($account, new Carbon); $balance = app('steam')->balance($account, $date);
$currencyId = (int)$repository->getMetaValue($account, 'currency_id'); $currency = $repository->getAccountCurrency($account) ?? $defaultCurrency;
$currency = $currencyRepos->findNull($currencyId); $role = (string)$repository->getMetaValue($account, 'account_role');
$role = (string)$repository->getMetaValue($account, 'account_role'); // TODO bad form for currency
if ('' === $role) { if ('' === $role) {
$role = 'no_account_type'; // @codeCoverageIgnore $role = 'no_account_type';
} }
if (in_array($account->accountType->type, $liabilityTypes, true)) { if (in_array($account->accountType->type, $liabilityTypes, true)) {
$role = 'l_' . $account->accountType->type; // @codeCoverageIgnore $role = sprintf('l_%s', $account->accountType->type);
} }
$key = (string)trans(sprintf('firefly.opt_group_%s', $role));
if (null === $currency) { $formatted = app('amount')->formatAnything($currency, $balance, false);
$currency = $defaultCurrency; $grouped[$key][$account->id] = sprintf('%s (%s)', $account->name, $formatted);
}
$key = (string)trans('firefly.opt_group_' . $role);
$grouped[$key][$account->id] = $account->name . ' (' . app('amount')->formatAnything($currency, $balance, false) . ')';
} }
return $this->select($name, $grouped, $value, $options); return $this->select($name, $grouped, $value, $options);

View File

@ -22,6 +22,8 @@
namespace FireflyIII\Support\Form; namespace FireflyIII\Support\Form;
use Carbon\Carbon; use Carbon\Carbon;
use Exception;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Illuminate\Support\MessageBag; use Illuminate\Support\MessageBag;
use Log; use Log;
use RuntimeException; use RuntimeException;
@ -32,6 +34,32 @@ use Throwable;
*/ */
trait FormSupport trait FormSupport
{ {
/**
* @return AccountRepositoryInterface
*/
protected function getAccountRepository(): AccountRepositoryInterface
{
return app(AccountRepositoryInterface::class);
}
/**
* @return Carbon
*/
protected function getDate(): Carbon
{
/** @var Carbon $date */
$date = null;
try {
$date = new Carbon;
} catch (Exception $e) {
$e->getMessage();
}
return $date;
}
/** /**
* @param string $name * @param string $name
* @param array $list * @param array $list

View File

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