More code for #452

This commit is contained in:
James Cole
2016-12-11 16:38:21 +01:00
parent 8a7297e131
commit 553e9270e5

View File

@@ -67,7 +67,7 @@ class AccountController extends Controller
$cache->addProperty('account-all-chart'); $cache->addProperty('account-all-chart');
$cache->addProperty($account->id); $cache->addProperty($account->id);
if ($cache->has()) { if ($cache->has()) {
return Response::json($cache->get()); return Response::json($cache->get());
} }
/** @var AccountRepositoryInterface $repository */ /** @var AccountRepositoryInterface $repository */
@@ -115,7 +115,7 @@ class AccountController extends Controller
$cache->addProperty('expenseAccounts'); $cache->addProperty('expenseAccounts');
$cache->addProperty('accounts'); $cache->addProperty('accounts');
if ($cache->has()) { if ($cache->has()) {
return Response::json($cache->get()); return Response::json($cache->get());
} }
$accounts = $repository->getAccountsByType([AccountType::EXPENSE, AccountType::BENEFICIARY]); $accounts = $repository->getAccountsByType([AccountType::EXPENSE, AccountType::BENEFICIARY]);
@@ -123,25 +123,22 @@ class AccountController extends Controller
$ids = $accounts->pluck('id')->toArray(); $ids = $accounts->pluck('id')->toArray();
$startBalances = Steam::balancesById($ids, $start); $startBalances = Steam::balancesById($ids, $start);
$endBalances = Steam::balancesById($ids, $end); $endBalances = Steam::balancesById($ids, $end);
$chartData = [];
$accounts->each( foreach ($accounts as $account) {
function (Account $account) use ($startBalances, $endBalances) { $id = $account->id;
$id = $account->id; $startBalance = $startBalances[$id] ?? '0';
$startBalance = $startBalances[$id] ?? '0'; $endBalance = $endBalances[$id] ?? '0';
$endBalance = $endBalances[$id] ?? '0'; $diff = bcsub($endBalance, $startBalance);
$diff = bcsub($endBalance, $startBalance); if (bccomp($diff, '0') !== 0) {
$account->difference = round($diff, 2); $chartData[$account->name] = round($diff, 2);
$account->difference = round($diff, 2);
} }
); }
arsort($chartData);
/** @var GeneratorInterface $generator */
$accounts = $accounts->sortByDesc( $generator = app(GeneratorInterface::class);
function (Account $account) { $data = $generator->singleSet(trans('firefly.spent'), $chartData);
return $account->difference;
}
);
$data = $this->generator->expenseAccounts($accounts, $start, $end);
$cache->store($data); $cache->store($data);
return Response::json($data); return Response::json($data);
@@ -360,15 +357,16 @@ class AccountController extends Controller
*/ */
public function revenueAccounts(AccountRepositoryInterface $repository) public function revenueAccounts(AccountRepositoryInterface $repository)
{ {
$start = clone session('start', Carbon::now()->startOfMonth()); $start = clone session('start', Carbon::now()->startOfMonth());
$end = clone session('end', Carbon::now()->endOfMonth()); $end = clone session('end', Carbon::now()->endOfMonth());
$cache = new CacheProperties; $chartData = [];
$cache = new CacheProperties;
$cache->addProperty($start); $cache->addProperty($start);
$cache->addProperty($end); $cache->addProperty($end);
$cache->addProperty('revenueAccounts'); $cache->addProperty('revenueAccounts');
$cache->addProperty('accounts'); $cache->addProperty('accounts');
if ($cache->has()) { if ($cache->has()) {
return Response::json($cache->get()); return Response::json($cache->get());
} }
$accounts = $repository->getAccountsByType([AccountType::REVENUE]); $accounts = $repository->getAccountsByType([AccountType::REVENUE]);
@@ -377,23 +375,24 @@ class AccountController extends Controller
$startBalances = Steam::balancesById($ids, $start); $startBalances = Steam::balancesById($ids, $start);
$endBalances = Steam::balancesById($ids, $end); $endBalances = Steam::balancesById($ids, $end);
$accounts->each( foreach ($accounts as $account) {
function (Account $account) use ($startBalances, $endBalances) { $id = $account->id;
$id = $account->id; $startBalance = $startBalances[$id] ?? '0';
$startBalance = $startBalances[$id] ?? '0'; $endBalance = $endBalances[$id] ?? '0';
$endBalance = $endBalances[$id] ?? '0'; $diff = bcsub($endBalance, $startBalance);
$diff = bcsub($endBalance, $startBalance); $diff = bcmul($diff, '-1');
$diff = bcmul($diff, '-1'); $account->difference = round($diff, 2);
$account->difference = round($diff, 2); if (bccomp($diff, '0') !== 0) {
$chartData[$account->name] = round($diff, 2);
$account->difference = round($diff, 2);
} }
); }
asort($chartData);
$accounts = $accounts->sortByDesc( /** @var GeneratorInterface $generator */
function (Account $account) { $generator = app(GeneratorInterface::class);
return $account->difference; $data = $generator->singleSet(trans('firefly.spent'), $chartData);
} $cache->store($data);
);
$data = $this->generator->revenueAccounts($accounts, $start, $end); $data = $this->generator->revenueAccounts($accounts, $start, $end);
$cache->store($data); $cache->store($data);