From 5c0bb34d773886a44d63c758fd00a6a2190902e2 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 15 Oct 2017 14:05:31 +0200 Subject: [PATCH] Code for #916 --- .../Controllers/Chart/BudgetController.php | 24 +++++++++---------- app/Support/Navigation.php | 5 ++-- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/app/Http/Controllers/Chart/BudgetController.php b/app/Http/Controllers/Chart/BudgetController.php index 04fd3dbd2b..aac0c2b726 100644 --- a/app/Http/Controllers/Chart/BudgetController.php +++ b/app/Http/Controllers/Chart/BudgetController.php @@ -74,17 +74,17 @@ class BudgetController extends Controller */ public function budget(Budget $budget) { - $first = $this->repository->firstUseDate($budget); - $range = Preferences::get('viewRange', '1M')->data; - $last = session('end', new Carbon); - - $cache = new CacheProperties(); + $first = $this->repository->firstUseDate($budget); + $range = Preferences::get('viewRange', '1M')->data; + $currentStart = Navigation::startOfPeriod($first, $range); + $last = session('end', new Carbon); + $cache = new CacheProperties(); $cache->addProperty($first); $cache->addProperty($last); $cache->addProperty('chart.budget.budget'); if ($cache->has()) { - return Response::json($cache->get()); // @codeCoverageIgnore + return Response::json($cache->get()); // @codeCoverageIgnore } $final = clone $last; @@ -92,17 +92,16 @@ class BudgetController extends Controller $budgetCollection = new Collection([$budget]); $last = Navigation::endOfX($last, $range, $final); // not to overshoot. $entries = []; - while ($first < $last) { - + while ($currentStart < $last) { // periodspecific dates: - $currentStart = Navigation::startOfPeriod($first, $range); - $currentEnd = Navigation::endOfPeriod($first, $range); + $currentEnd = Navigation::endOfPeriod($currentStart, $range); // sub another day because reasons. $currentEnd->subDay(); $spent = $this->repository->spentInPeriod($budgetCollection, new Collection, $currentStart, $currentEnd); - $format = Navigation::periodShow($first, $range); + $format = Navigation::periodShow($currentStart, $range); $entries[$format] = bcmul($spent, '-1'); - $first = Navigation::addPeriod($first, $range, 0); + $currentStart = clone $currentEnd; + $currentStart->addDays(2); } $data = $this->generator->singleSet(strval(trans('firefly.spent')), $entries); @@ -244,6 +243,7 @@ class BudgetController extends Controller $data = $this->generator->pieChart($chartData); $cache->store($data); + return Response::json($data); } diff --git a/app/Support/Navigation.php b/app/Support/Navigation.php index 15a3ade60d..af068c59f5 100644 --- a/app/Support/Navigation.php +++ b/app/Support/Navigation.php @@ -215,14 +215,15 @@ class Navigation } /** - * @param \Carbon\Carbon $date + * @param \Carbon\Carbon $theDate * @param $repeatFrequency * * @return string * @throws FireflyException */ - public function periodShow(Carbon $date, string $repeatFrequency): string + public function periodShow(Carbon $theDate, string $repeatFrequency): string { + $date = clone $theDate; $formatMap = [ '1D' => trans('config.specific_day'), 'daily' => trans('config.specific_day'),