Code to implement #1168 and #1197.

This commit is contained in:
James Cole
2018-03-07 10:18:22 +01:00
parent 6c63583e49
commit a5fd821e0c
16 changed files with 190 additions and 180 deletions

View File

@@ -25,6 +25,10 @@ namespace FireflyIII\Support;
use Amount as Amt;
use Carbon\Carbon;
use Eloquent;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use Illuminate\Support\Collection;
use Illuminate\Support\MessageBag;
use RuntimeException;
@@ -61,6 +65,58 @@ class ExpandedForm
return $this->currencyField($name, 'amount-small', $value, $options);
}
/**
* @param string $name
* @param null $value
* @param array $options
*
* @return string
* @throws \Throwable
*/
public function assetAccountList(string $name, $value = null, array $options = []): string
{
// properties for cache
$cache = new CacheProperties;
$cache->addProperty('exp-form-asset-list');
$cache->addProperty($name);
$cache->addProperty($value);
$cache->addProperty($options);
if ($cache->has()) {
return $cache->get();
}
// make repositories
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
/** @var CurrencyRepositoryInterface $currencyRepos */
$currencyRepos = app(CurrencyRepositoryInterface::class);
$assetAccounts = $repository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT]);
$defaultCurrency = app('amount')->getDefaultCurrency();
$grouped = [];
// group accounts:
/** @var Account $account */
foreach ($assetAccounts as $account) {
$balance = app('steam')->balance($account, new Carbon);
$currencyId = intval($account->getMeta('currency_id'));
$currency = $currencyRepos->findNull($currencyId);
$role = $account->getMeta('accountRole');
if (0 === strlen($role)) {
$role = 'no_account_type'; // @codeCoverageIgnore
}
if (is_null($currency)) {
$currency = $defaultCurrency;
}
$key = strval(trans('firefly.opt_group_' . $role));
$grouped[$key][$account->id] = $account->name . ' (' . app('amount')->formatAnything($currency, $balance, false) . ')';
}
$res = $this->select($name, $grouped, $value, $options);
$cache->store($res);
return $res;
}
/**
* @param $name
* @param null $value