From 308da6dc6e0dedfe1464616aec4a05a229073159 Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 10 Jul 2015 07:39:59 +0200 Subject: [PATCH] Clean up some lists [skip ci] --- app/Http/Controllers/AccountController.php | 19 ++++++-- app/Models/Account.php | 12 +++--- .../Account/AccountRepository.php | 6 +-- app/Support/Amount.php | 41 ++++++++++++++---- app/Support/Steam.php | 43 +++++++++++++++++++ resources/twig/list/accounts.twig | 2 +- 6 files changed, 100 insertions(+), 23 deletions(-) diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index 0a904f593c..682c344f5e 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -153,12 +153,23 @@ class AccountController extends Controller * HERE WE ARE */ $start = clone Session::get('start', Carbon::now()->startOfMonth()); + $end = clone Session::get('end', Carbon::now()->endOfMonth()); $start->subDay(); + + // start balances: + $ids = []; + foreach ($accounts as $account) { + $ids[] = $account->id; + } + + $startBalances = Steam::balancesById($ids, $start); + $endBalances = Steam::balancesById($ids, $end); + $accounts->each( - function (Account $account) use ($start, $repository) { - $account->lastActivityDate = $repository->getLastActivity($account); - $account->startBalance = Steam::balance($account, $start); - $account->endBalance = Steam::balance($account, clone Session::get('end', Carbon::now()->endOfMonth())); + function (Account $account) use ($startBalances, $endBalances) { + $account->lastActivityDate = null;//$repository->getLastActivity($account); + $account->startBalance = isset($startBalances[$account->id]) ? $startBalances[$account->id] : null; + $account->endBalance = isset($endBalances[$account->id]) ? $endBalances[$account->id] : null; } ); diff --git a/app/Models/Account.php b/app/Models/Account.php index e94e1c9037..71c60a5360 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -42,12 +42,12 @@ use Watson\Validating\ValidatingTrait; * @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Account accountTypeIn($types) * @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Account hasMetaValue($name, $value) * @property-read bool $joinedAccountTypes - * @property-read float $startBalance - * @property-read float $endBalance - * @property-read float $piggyBalance - * @property-read float $percentage - * @property-read float $difference - * @property-read \Carbon\Carbon $lastActivityDate + * @property float $startBalance + * @property float $endBalance + * @property float $piggyBalance + * @property float $percentage + * @property float $difference + * @property \Carbon\Carbon $lastActivityDate */ class Account extends Model { diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php index bb6b5dc430..bf13acfafd 100644 --- a/app/Repositories/Account/AccountRepository.php +++ b/app/Repositories/Account/AccountRepository.php @@ -68,7 +68,7 @@ class AccountRepository implements AccountRepositoryInterface ['accountmeta' => function (HasMany $query) { $query->where('name', 'accountRole'); }] - )->accountTypeIn($types)->orderBy('accounts.name', 'ASC')->get(['accounts.*']); + )->accountTypeIn($types)->get(['accounts.*']); $result = $result->sortBy( function (Account $account) { @@ -210,9 +210,9 @@ class AccountRepository implements AccountRepositoryInterface { $lastTransaction = $account->transactions()->leftJoin( 'transaction_journals', 'transactions.transaction_journal_id', '=', 'transaction_journals.id' - )->orderBy('transaction_journals.date', 'DESC')->first(['transactions.*', 'transaction_journals.date']); + )->orderBy('transaction_journals.date', 'DESC')->first(['transactions.account_id', 'transaction_journals.date']); if ($lastTransaction) { - return $lastTransaction->transactionjournal->date; + return $lastTransaction->date; } return null; diff --git a/app/Support/Amount.php b/app/Support/Amount.php index 9b4cb692fc..fb8d93db45 100644 --- a/app/Support/Amount.php +++ b/app/Support/Amount.php @@ -35,10 +35,18 @@ class Amount */ public function getCurrencySymbol() { - $currencyPreference = Prefs::get('currencyPreference', 'EUR'); - $currency = TransactionCurrency::whereCode($currencyPreference->data)->first(); + $cache = new CacheProperties; + $cache->addProperty('getCurrencySymbol'); + if ($cache->has()) { + return $cache->get(); + } else { + $currencyPreference = Prefs::get('currencyPreference', 'EUR'); + $currency = TransactionCurrency::whereCode($currencyPreference->data)->first(); - return $currency->symbol; + $cache->store($currency->symbol); + + return $currency->symbol; + } } /** @@ -142,15 +150,24 @@ class Amount public function getCurrencyCode() { - $currencyPreference = Prefs::get('currencyPreference', 'EUR'); + $cache = new CacheProperties; + $cache->addProperty('getCurrencyCode'); + if ($cache->has()) { + return $cache->get(); + } else { + $currencyPreference = Prefs::get('currencyPreference', 'EUR'); - $currency = TransactionCurrency::whereCode($currencyPreference->data)->first(); - if ($currency) { + $currency = TransactionCurrency::whereCode($currencyPreference->data)->first(); + if ($currency) { - return $currency->code; + $cache->store($currency->code); + + return $currency->code; + } + $cache->store('EUR'); + + return 'EUR'; // @codeCoverageIgnore } - - return 'EUR'; // @codeCoverageIgnore } /** @@ -158,8 +175,14 @@ class Amount */ public function getDefaultCurrency() { + $cache = new CacheProperties; + $cache->addProperty('getDefaultCurrency'); + if ($cache->has()) { + return $cache->get(); + } $currencyPreference = Prefs::get('currencyPreference', 'EUR'); $currency = TransactionCurrency::whereCode($currencyPreference->data)->first(); + $cache->store($currency); return $currency; } diff --git a/app/Support/Steam.php b/app/Support/Steam.php index 56e9c40e33..f5f8e3f61f 100644 --- a/app/Support/Steam.php +++ b/app/Support/Steam.php @@ -3,7 +3,9 @@ namespace FireflyIII\Support; use Carbon\Carbon; +use DB; use FireflyIII\Models\Account; +use FireflyIII\Models\Transaction; /** * Class Steam @@ -47,4 +49,45 @@ class Steam return round($balance, 2); } + /** + * + * @param array $ids + * @param \Carbon\Carbon $date + * + * @return float + */ + public function balancesById(array $ids, Carbon $date) + { + + // abuse chart properties: + $cache = new CacheProperties; + $cache->addProperty($ids); + $cache->addProperty('balances'); + $cache->addProperty($date); + if ($cache->has()) { + return $cache->get(); // @codeCoverageIgnore + } + + bcscale(2); + + $balances = Transaction:: + leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') + ->where('transaction_journals.date', '<=', $date->format('Y-m-d')) + ->groupBy('transactions.account_id') + ->whereIn('transactions.account_id', $ids) + ->get(['transactions.account_id', DB::Raw('sum(`transactions`.`amount`) as aggregate')]); + + $result = []; + foreach ($balances as $entry) { + $accountId = intval($entry->account_id); + $balance = round($entry->aggregate, 2); + $result[$accountId] = $balance; + } + + + $cache->store($result); + + return $result; + } + } diff --git a/resources/twig/list/accounts.twig b/resources/twig/list/accounts.twig index cf06a1a359..1cf3fd6bd7 100644 --- a/resources/twig/list/accounts.twig +++ b/resources/twig/list/accounts.twig @@ -33,7 +33,7 @@ {% endif %} {{ account.iban }} - {{ account|balance|formatAmount }} + {{ account.startBalance|formatAmount }} {% if account.active %}