From f231263085c1f8989a8a937bb903b8223e9b8369 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 24 Jan 2015 08:43:35 +0100 Subject: [PATCH] Updated chart, closed magic number issue. --- app/controllers/GoogleChartController.php | 31 ++++++++++++------- .../FireflyIII/Database/Account/Account.php | 2 +- app/routes.php | 5 +-- public/assets/javascript/firefly/budgets.js | 2 +- .../functional/GoogleChartControllerCest.php | 14 +-------- 5 files changed, 25 insertions(+), 29 deletions(-) diff --git a/app/controllers/GoogleChartController.php b/app/controllers/GoogleChartController.php index af1400bfb1..7179fdf914 100644 --- a/app/controllers/GoogleChartController.php +++ b/app/controllers/GoogleChartController.php @@ -277,19 +277,12 @@ class GoogleChartController extends BaseController /** * - * @param Budget $budget - * @param $year + * @param Budget $budget * * @return \Illuminate\Http\JsonResponse */ - public function budgetsAndSpending(Budget $budget, $year) + public function budgetsAndSpending(Budget $budget) { - try { - new Carbon('01-01-' . $year); - } catch (Exception $e) { - return View::make('error')->with('message', 'Invalid year.'); - } - /** @var \FireflyIII\Database\Budget\Budget $budgetRepository */ $budgetRepository = App::make('FireflyIII\Database\Budget\Budget'); @@ -297,9 +290,23 @@ class GoogleChartController extends BaseController $this->_chart->addColumn('Budgeted', 'number'); $this->_chart->addColumn('Spent', 'number'); - $start = new Carbon('01-01-' . $year); - $end = clone $start; - $end->endOfYear(); + // grab the first budgetlimit ever: + $firstLimit = $budget->budgetlimits()->orderBy('startdate', 'ASC')->first(); + if ($firstLimit) { + $start = new Carbon($firstLimit->startdate); + } else { + $start = Carbon::now()->startOfYear(); + } + + // grab the last budget limit ever: + $lastLimit = $budget->budgetlimits()->orderBy('startdate', 'DESC')->first(); + if ($lastLimit) { + $end = new Carbon($lastLimit->startdate); + } else { + $end = Carbon::now()->endOfYear(); + } + + while ($start <= $end) { $spent = $budgetRepository->spentInMonth($budget, $start); $repetition = $budgetRepository->repetitionOnStartingOnDate($budget, $start); diff --git a/app/lib/FireflyIII/Database/Account/Account.php b/app/lib/FireflyIII/Database/Account/Account.php index 4edfed9116..6753e2eb87 100644 --- a/app/lib/FireflyIII/Database/Account/Account.php +++ b/app/lib/FireflyIII/Database/Account/Account.php @@ -170,7 +170,7 @@ class Account implements CUDInterface, CommonDatabaseCallsInterface, AccountInte ->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id') ->where( function (QueryBuilder $q) use ($model) { - $q->where('id', $model->id); + $q->where('accounts.id', $model->id); $q->orWhere( function (QueryBuilder $q) use ($model) { $q->where('accounts.name', 'LIKE', '%' . $model->name . '%'); diff --git a/app/routes.php b/app/routes.php index 2bc973242b..24b2f3a926 100644 --- a/app/routes.php +++ b/app/routes.php @@ -222,11 +222,12 @@ Route::group( Route::get('/chart/reports/income-expenses/{year}', ['uses' => 'GoogleChartController@yearInExp']); Route::get('/chart/reports/income-expenses-sum/{year}', ['uses' => 'GoogleChartController@yearInExpSum']); Route::get('/chart/bills/{bill}', ['uses' => 'GoogleChartController@billOverview']); - Route::get('/chart/budget/{budget}/{limitrepetition}', ['uses' => 'GoogleChartController@budgetLimitSpending']); + Route::get('/chart/piggy_history/{piggyBank}', ['uses' => 'GoogleChartController@piggyBankHistory']); // google chart for components (categories + budgets combined) - Route::get('/chart/budget/{budget}/spending/{year}', ['uses' => 'GoogleChartController@budgetsAndSpending']); + Route::get('/chart/budget/{budget}/spending', ['uses' => 'GoogleChartController@budgetsAndSpending']); + Route::get('/chart/budget/{budget}/{limitrepetition}', ['uses' => 'GoogleChartController@budgetLimitSpending']); Route::get('/chart/category/{category}/spending/{year}', ['uses' => 'GoogleChartController@categoriesAndSpending']); // help controller diff --git a/public/assets/javascript/firefly/budgets.js b/public/assets/javascript/firefly/budgets.js index 1481c51598..e8a856cf4a 100644 --- a/public/assets/javascript/firefly/budgets.js +++ b/public/assets/javascript/firefly/budgets.js @@ -8,7 +8,7 @@ $(function () { if (typeof budgetID != 'undefined' && typeof repetitionID == 'undefined') { - googleColumnChart('chart/budget/' + budgetID + '/spending/' + year, 'budgetOverview'); + googleColumnChart('chart/budget/' + budgetID + '/spending', 'budgetOverview'); } if (typeof budgetID != 'undefined' && typeof repetitionID != 'undefined') { googleLineChart('chart/budget/' + budgetID + '/' + repetitionID, 'budgetOverview'); diff --git a/tests/functional/GoogleChartControllerCest.php b/tests/functional/GoogleChartControllerCest.php index 578204b153..a570d9214d 100644 --- a/tests/functional/GoogleChartControllerCest.php +++ b/tests/functional/GoogleChartControllerCest.php @@ -108,23 +108,11 @@ class GoogleChartControllerCest */ public function budgetsAndSpending(FunctionalTester $I) { - $year = date('Y'); $I->wantTo('see the chart for a budget in a specific year'); - $I->amOnPage('/chart/budget/1/spending/'.$year); + $I->amOnPage('/chart/budget/1/spending'); $I->seeResponseCodeIs(200); } - /** - * @param FunctionalTester $I - */ - public function budgetsAndSpendingInvalidYear(FunctionalTester $I) - { - $I->wantTo('see the chart for a budget in an invalid year'); - $I->amOnPage('/chart/budget/1/spending/XXXX'); - $I->seeResponseCodeIs(200); - $I->see('Invalid year'); - } - /** * @param FunctionalTester $I */