From 553e9270e5e42007c56bd8f811af9ca63f3ef958 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 11 Dec 2016 16:38:21 +0100 Subject: [PATCH] More code for #452 --- .../Controllers/Chart/AccountController.php | 75 +++++++++---------- 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/app/Http/Controllers/Chart/AccountController.php b/app/Http/Controllers/Chart/AccountController.php index 5955f333a6..379951d3b0 100644 --- a/app/Http/Controllers/Chart/AccountController.php +++ b/app/Http/Controllers/Chart/AccountController.php @@ -67,7 +67,7 @@ class AccountController extends Controller $cache->addProperty('account-all-chart'); $cache->addProperty($account->id); if ($cache->has()) { - return Response::json($cache->get()); + return Response::json($cache->get()); } /** @var AccountRepositoryInterface $repository */ @@ -115,7 +115,7 @@ class AccountController extends Controller $cache->addProperty('expenseAccounts'); $cache->addProperty('accounts'); if ($cache->has()) { - return Response::json($cache->get()); + return Response::json($cache->get()); } $accounts = $repository->getAccountsByType([AccountType::EXPENSE, AccountType::BENEFICIARY]); @@ -123,25 +123,22 @@ class AccountController extends Controller $ids = $accounts->pluck('id')->toArray(); $startBalances = Steam::balancesById($ids, $start); $endBalances = Steam::balancesById($ids, $end); + $chartData = []; - $accounts->each( - function (Account $account) use ($startBalances, $endBalances) { - $id = $account->id; - $startBalance = $startBalances[$id] ?? '0'; - $endBalance = $endBalances[$id] ?? '0'; - $diff = bcsub($endBalance, $startBalance); - $account->difference = round($diff, 2); + foreach ($accounts as $account) { + $id = $account->id; + $startBalance = $startBalances[$id] ?? '0'; + $endBalance = $endBalances[$id] ?? '0'; + $diff = bcsub($endBalance, $startBalance); + if (bccomp($diff, '0') !== 0) { + $chartData[$account->name] = round($diff, 2); + $account->difference = round($diff, 2); } - ); - - - $accounts = $accounts->sortByDesc( - function (Account $account) { - return $account->difference; - } - ); - - $data = $this->generator->expenseAccounts($accounts, $start, $end); + } + arsort($chartData); + /** @var GeneratorInterface $generator */ + $generator = app(GeneratorInterface::class); + $data = $generator->singleSet(trans('firefly.spent'), $chartData); $cache->store($data); return Response::json($data); @@ -360,15 +357,16 @@ class AccountController extends Controller */ public function revenueAccounts(AccountRepositoryInterface $repository) { - $start = clone session('start', Carbon::now()->startOfMonth()); - $end = clone session('end', Carbon::now()->endOfMonth()); - $cache = new CacheProperties; + $start = clone session('start', Carbon::now()->startOfMonth()); + $end = clone session('end', Carbon::now()->endOfMonth()); + $chartData = []; + $cache = new CacheProperties; $cache->addProperty($start); $cache->addProperty($end); $cache->addProperty('revenueAccounts'); $cache->addProperty('accounts'); if ($cache->has()) { - return Response::json($cache->get()); + return Response::json($cache->get()); } $accounts = $repository->getAccountsByType([AccountType::REVENUE]); @@ -377,23 +375,24 @@ class AccountController extends Controller $startBalances = Steam::balancesById($ids, $start); $endBalances = Steam::balancesById($ids, $end); - $accounts->each( - function (Account $account) use ($startBalances, $endBalances) { - $id = $account->id; - $startBalance = $startBalances[$id] ?? '0'; - $endBalance = $endBalances[$id] ?? '0'; - $diff = bcsub($endBalance, $startBalance); - $diff = bcmul($diff, '-1'); - $account->difference = round($diff, 2); + foreach ($accounts as $account) { + $id = $account->id; + $startBalance = $startBalances[$id] ?? '0'; + $endBalance = $endBalances[$id] ?? '0'; + $diff = bcsub($endBalance, $startBalance); + $diff = bcmul($diff, '-1'); + $account->difference = round($diff, 2); + if (bccomp($diff, '0') !== 0) { + $chartData[$account->name] = round($diff, 2); + $account->difference = round($diff, 2); } - ); + } - - $accounts = $accounts->sortByDesc( - function (Account $account) { - return $account->difference; - } - ); + asort($chartData); + /** @var GeneratorInterface $generator */ + $generator = app(GeneratorInterface::class); + $data = $generator->singleSet(trans('firefly.spent'), $chartData); + $cache->store($data); $data = $this->generator->revenueAccounts($accounts, $start, $end); $cache->store($data);