From c3dc193f3e14f46d2738c34c29f44208831c8af3 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 25 Jul 2015 19:22:41 +0200 Subject: [PATCH] First attempt at new last activity thing. --- app/Http/Controllers/AccountController.php | 5 +++-- .../Account/AccountRepository.php | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index 44026114aa..0d8ced19f0 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -167,10 +167,11 @@ class AccountController extends Controller $startBalances = Steam::balancesById($ids, $start); $endBalances = Steam::balancesById($ids, $end); + $activities = $repository->getLastActivities($ids); $accounts->each( - function (Account $account) use ($startBalances, $endBalances) { - $account->lastActivityDate = null;//$repository->getLastActivity($account); + function (Account $account) use ($activities, $startBalances, $endBalances) { + $account->lastActivityDate = isset($activities[$account->id]) ? $activities[$account->id] : null; $account->startBalance = isset($startBalances[$account->id]) ? $startBalances[$account->id] : null; $account->endBalance = isset($endBalances[$account->id]) ? $endBalances[$account->id] : null; } diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php index 3dd955bcfc..b4c466da49 100644 --- a/app/Repositories/Account/AccountRepository.php +++ b/app/Repositories/Account/AccountRepository.php @@ -207,6 +207,27 @@ class AccountRepository implements AccountRepositoryInterface } + /** + * @param array $accounts + * + * @return array + */ + public function getLastActivities(array $accounts) + { + $list = []; + + $set = Auth::user()->transactions() + ->whereIn('account_id', $accounts) + ->groupBy('account_id') + ->get(['transactions.account_id', DB::Raw('MAX(`transaction_journals`.`date`) as `max_date`')]); + + foreach ($set as $entry) { + $list[intval($entry->account_id)] = new Carbon($entry->max_date); + } + + return $list; + } + /** * @param Account $account *