diff --git a/app/Generator/Chart/Account/ChartJsAccountChartGenerator.php b/app/Generator/Chart/Account/ChartJsAccountChartGenerator.php index a6ab113571..f9a70c8778 100644 --- a/app/Generator/Chart/Account/ChartJsAccountChartGenerator.php +++ b/app/Generator/Chart/Account/ChartJsAccountChartGenerator.php @@ -32,6 +32,51 @@ class ChartJsAccountChartGenerator implements AccountChartGenerator return $this->frontpage($accounts, $start, $end); } + /** + * @param Collection $accounts + * @param Carbon $start + * @param Carbon $end + * + * @return array + */ + public function expenseAccounts(Collection $accounts, Carbon $start, Carbon $end) + { + // language: + + $data = [ + 'count' => 1, + 'labels' => [], + 'datasets' => [ + [ + 'label' => trans('firefly.spent'), + 'data' => [] + ] + ], + ]; + $ids = []; + + foreach ($accounts as $account) { + $ids[] = $account->id; + } + + $startBalances = Steam::balancesById($ids, $start); + $endBalances = Steam::balancesById($ids, $end); + + foreach ($accounts as $account) { + $id = $account->id; + $start = isset($startBalances[$id]) ? $startBalances[$id] : 0; + $end = isset($endBalances[$id]) ? $endBalances[$id] : 0; + $diff = $end - $start; + if ($diff > 0) { + $data['labels'][] = $account->name; + $data['datasets'][0]['data'][] = $diff; + } + } + + return $data; + } + + /** * @param Collection $accounts * @param Carbon $start diff --git a/app/Http/Controllers/Chart/AccountController.php b/app/Http/Controllers/Chart/AccountController.php index e1447488e9..116e2c00aa 100644 --- a/app/Http/Controllers/Chart/AccountController.php +++ b/app/Http/Controllers/Chart/AccountController.php @@ -79,6 +79,36 @@ class AccountController extends Controller return Response::json($data); } + /** + * Shows the balances for all the user's expense accounts. + * + * @param AccountRepositoryInterface $repository + * + * @return \Symfony\Component\HttpFoundation\Response + */ + public function expenseAccounts(AccountRepositoryInterface $repository) + { + $start = clone Session::get('start', Carbon::now()->startOfMonth()); + $end = clone Session::get('end', Carbon::now()->endOfMonth()); + $accounts = $repository->getAccounts(['Expense account', 'Beneficiary account']); + + // chart properties for cache: + $cache = new CacheProperties(); + $cache->addProperty($start); + $cache->addProperty($end); + $cache->addProperty('expenseAccounts'); + $cache->addProperty('accounts'); + if ($cache->has()) { + return Response::json($cache->get()); // @codeCoverageIgnore + } + + $data = $this->generator->expenseAccounts($accounts, $start, $end); + $cache->store($data); + + return Response::json($data); + + } + /** * Shows the balances for all the user's frontpage accounts. * diff --git a/app/Http/Controllers/Chart/CategoryController.php b/app/Http/Controllers/Chart/CategoryController.php index 2a190b2f51..67553440be 100644 --- a/app/Http/Controllers/Chart/CategoryController.php +++ b/app/Http/Controllers/Chart/CategoryController.php @@ -186,15 +186,16 @@ class CategoryController extends Controller $shared = $shared == 'shared' ? true : false; $allCategories = $repository->getCategories(); $entries = new Collection; - $categories = new Collection; + $categories = $allCategories->filter( + function (Category $category) use ($repository, $start, $end, $shared) { + $spent = $repository->spentInPeriodCorrected($category, $start, $end, $shared); + if ($spent < 0) { + return $category; + } - // filter by checking the entire year first: - foreach ($allCategories as $category) { - $spent = $repository->spentInPeriodCorrected($category, $start, $end, $shared); - if ($spent < 0) { - $categories->push($category); + return null; } - } + ); while ($start < $end) { $month = clone $start; // month is the current end of the period @@ -204,18 +205,14 @@ class CategoryController extends Controller foreach ($categories as $category) { // each budget, fill the row $spent = $repository->spentInPeriodCorrected($category, $start, $month, $shared); if ($spent < 0) { - $spent = $spent * -1; - $row[] = $spent; + $row[] = $spent * -1; } else { $row[] = 0; } - } $entries->push($row); - $start->addMonth(); } - $data = $this->generator->spentInYear($categories, $entries); $cache->store($data); @@ -248,16 +245,16 @@ class CategoryController extends Controller $shared = $shared == 'shared' ? true : false; $allCategories = $repository->getCategories(); $allEntries = new Collection; - $categories = new Collection; + $categories = $allCategories->filter( + function (Category $category) use ($repository, $start, $end, $shared) { + $spent = $repository->spentInPeriodCorrected($category, $start, $end, $shared); + if ($spent > 0) { + return $category; + } - // filter by checking the entire year first: - foreach ($allCategories as $category) { - $spent = $repository->spentInPeriodCorrected($category, $start, $end, $shared); - if ($spent > 0) { - $categories->push($category); + return null; } - } - + ); while ($start < $end) { $month = clone $start; // month is the current end of the period @@ -271,14 +268,10 @@ class CategoryController extends Controller } else { $row[] = 0; } - } $allEntries->push($row); - $start->addMonth(); } - - $data = $this->generator->earnedInYear($categories, $allEntries); $cache->store($data); diff --git a/app/Http/routes.php b/app/Http/routes.php index fcdfae5800..0471e559b9 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -162,7 +162,7 @@ Route::get('/cron/sendgrid', ['uses' => 'CronController@sendgrid']); Route::controllers( [ - 'auth' => 'Auth\AuthController', + 'auth' => 'Auth\AuthController', 'password' => 'Auth\PasswordController', ] ); @@ -282,6 +282,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/month/{year}/{month}/{shared?}', ['uses' => 'Chart\AccountController@all'])->where( ['year' => '[0-9]{4}', 'month' => '[0-9]{1,2}', 'shared' => 'shared'] ); @@ -300,8 +301,12 @@ Route::group( // categories: Route::get('/chart/category/frontpage', ['uses' => 'Chart\CategoryController@frontpage']); - Route::get('/chart/category/spent-in-year/{year}/{shared?}', ['uses' => 'Chart\CategoryController@spentInYear'])->where(['year' => '[0-9]{4}', 'shared' => 'shared']); - Route::get('/chart/category/earned-in-year/{year}/{shared?}', ['uses' => 'Chart\CategoryController@earnedInYear'])->where(['year' => '[0-9]{4}', 'shared' => 'shared']); + Route::get('/chart/category/spent-in-year/{year}/{shared?}', ['uses' => 'Chart\CategoryController@spentInYear'])->where( + ['year' => '[0-9]{4}', 'shared' => 'shared'] + ); + Route::get('/chart/category/earned-in-year/{year}/{shared?}', ['uses' => 'Chart\CategoryController@earnedInYear'])->where( + ['year' => '[0-9]{4}', 'shared' => 'shared'] + ); Route::get('/chart/category/{category}/month', ['uses' => 'Chart\CategoryController@month']); // should be period. Route::get('/chart/category/{category}/all', ['uses' => 'Chart\CategoryController@all']); diff --git a/public/js/index.js b/public/js/index.js index a7542a40ca..fa3962a85f 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -41,6 +41,7 @@ function drawChart() { pieChart('chart/bill/frontpage', 'bills-chart'); stackedColumnChart('chart/budget/frontpage', 'budgets-chart'); columnChart('chart/category/frontpage', 'categories-chart'); + columnChart('chart/account/expense', 'expense-accounts-chart'); getBoxAmounts(); diff --git a/resources/twig/index.twig b/resources/twig/index.twig index c0808e413f..3637bbb0e0 100644 --- a/resources/twig/index.twig +++ b/resources/twig/index.twig @@ -68,6 +68,25 @@ + +