diff --git a/app/Generator/Chart/Account/AccountChartGeneratorInterface.php b/app/Generator/Chart/Account/AccountChartGeneratorInterface.php index c27376b837..9b90dc6668 100644 --- a/app/Generator/Chart/Account/AccountChartGeneratorInterface.php +++ b/app/Generator/Chart/Account/AccountChartGeneratorInterface.php @@ -24,6 +24,14 @@ use Illuminate\Support\Collection; */ interface AccountChartGeneratorInterface { + /** + * @param Collection $accounts + * @param Carbon $start + * @param Carbon $end + * + * @return array + */ + public function revenueAccounts(Collection $accounts, Carbon $start, Carbon $end): array; /** * @param Collection $accounts diff --git a/app/Generator/Chart/Account/ChartJsAccountChartGenerator.php b/app/Generator/Chart/Account/ChartJsAccountChartGenerator.php index 215a9969b0..a8a222e16b 100644 --- a/app/Generator/Chart/Account/ChartJsAccountChartGenerator.php +++ b/app/Generator/Chart/Account/ChartJsAccountChartGenerator.php @@ -83,6 +83,30 @@ class ChartJsAccountChartGenerator implements AccountChartGeneratorInterface return $data; } + /** + * @param Collection $accounts + * @param Carbon $start + * @param Carbon $end + * + * @return array + */ + public function revenueAccounts(Collection $accounts, Carbon $start, Carbon $end): array + { + $data = [ + 'count' => 1, + 'labels' => [], 'datasets' => [[ + 'label' => trans('firefly.earned'), + 'data' => []]]]; + foreach ($accounts as $account) { + if ($account->difference > 0) { + $data['labels'][] = $account->name; + $data['datasets'][0]['data'][] = $account->difference; + } + } + + return $data; + } + /** * @param Account $account * @param array $labels @@ -105,5 +129,4 @@ class ChartJsAccountChartGenerator implements AccountChartGeneratorInterface return $data; } - } diff --git a/app/Http/Controllers/Chart/AccountController.php b/app/Http/Controllers/Chart/AccountController.php index e1a18afd09..aaeb252cc7 100644 --- a/app/Http/Controllers/Chart/AccountController.php +++ b/app/Http/Controllers/Chart/AccountController.php @@ -190,6 +190,56 @@ class AccountController extends Controller return Response::json($data); } + /** + * Shows the balances for all the user's revenue accounts. + * + * @param AccountRepositoryInterface $repository + * + * @return \Illuminate\Http\JsonResponse + */ + public function revenueAccounts(AccountRepositoryInterface $repository) + { + $start = clone session('start', Carbon::now()->startOfMonth()); + $end = clone session('end', Carbon::now()->endOfMonth()); + $cache = new CacheProperties; + $cache->addProperty($start); + $cache->addProperty($end); + $cache->addProperty('revenueAccounts'); + $cache->addProperty('accounts'); + if ($cache->has()) { + return Response::json($cache->get()); + } + $accounts = $repository->getAccountsByType([AccountType::REVENUE]); + + $start->subDay(); + $ids = $accounts->pluck('id')->toArray(); + $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); + } + ); + + + $accounts = $accounts->sortBy( + function (Account $account) { + return $account->difference; + } + ); + + $data = $this->generator->revenueAccounts($accounts, $start, $end); + $cache->store($data); + + return Response::json($data); + } + /** * Shows an account's balance for a single month. * diff --git a/routes/web.php b/routes/web.php index 91187a8dc0..7006a4fef9 100755 --- a/routes/web.php +++ b/routes/web.php @@ -183,6 +183,7 @@ Route::group( // accounts: Route::get('/chart/account/frontpage', ['uses' => 'Chart\AccountController@frontpage']); Route::get('/chart/account/expense', ['uses' => 'Chart\AccountController@expenseAccounts']); + Route::get('/chart/account/revenue', ['uses' => 'Chart\AccountController@revenueAccounts']); Route::get('/chart/account/report/default/{start_date}/{end_date}/{accountList}', ['uses' => 'Chart\AccountController@report']); Route::get('/chart/account/{account}', ['uses' => 'Chart\AccountController@single']); Route::get('/chart/account/{account}/{date}', ['uses' => 'Chart\AccountController@specificPeriod']);