From 51d97cdca505ad205c694d11761ad5a64ee09705 Mon Sep 17 00:00:00 2001 From: James Cole Date: Mon, 25 Apr 2016 09:57:39 +0200 Subject: [PATCH] Reinstated a chart. --- .../Budget/BudgetChartGeneratorInterface.php | 3 +- .../Budget/ChartJsBudgetChartGenerator.php | 5 +- .../Controllers/Chart/BudgetController.php | 49 +++++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/app/Generator/Chart/Budget/BudgetChartGeneratorInterface.php b/app/Generator/Chart/Budget/BudgetChartGeneratorInterface.php index 0d4e056673..fa418d3fd6 100644 --- a/app/Generator/Chart/Budget/BudgetChartGeneratorInterface.php +++ b/app/Generator/Chart/Budget/BudgetChartGeneratorInterface.php @@ -22,10 +22,11 @@ interface BudgetChartGeneratorInterface /** * @param Collection $entries + * @param string $dateFormat * * @return array */ - public function budgetLimit(Collection $entries): array; + public function budgetLimit(Collection $entries, string $dateFormat): array; /** * @param Collection $entries diff --git a/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php b/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php index 7da18e57ac..9d4adf812d 100644 --- a/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php +++ b/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php @@ -18,13 +18,14 @@ class ChartJsBudgetChartGenerator implements BudgetChartGeneratorInterface /** * * @param Collection $entries + * @param string $dateFormat * * @return array */ - public function budgetLimit(Collection $entries): array + public function budgetLimit(Collection $entries, string $dateFormat = 'monthAndDay'): array { $language = Preferences::get('language', env('DEFAULT_LANGUAGE', 'en_US'))->data; - $format = Config::get('firefly.monthAndDay.' . $language); + $format = Config::get('firefly.' . $dateFormat . '.' . $language); $data = [ 'labels' => [], diff --git a/app/Http/Controllers/Chart/BudgetController.php b/app/Http/Controllers/Chart/BudgetController.php index e4fca45a10..dbabcbae5c 100644 --- a/app/Http/Controllers/Chart/BudgetController.php +++ b/app/Http/Controllers/Chart/BudgetController.php @@ -37,6 +37,55 @@ class BudgetController extends Controller $this->generator = app('FireflyIII\Generator\Chart\Budget\BudgetChartGeneratorInterface'); } + /** + * @param BudgetRepositoryInterface $repository + * @param Budget $budget + * + * @return \Symfony\Component\HttpFoundation\Response + */ + public function budget(BudgetRepositoryInterface $repository, Budget $budget) + { + + // dates and times + $first = $repository->getFirstBudgetLimitDate($budget); + $range = Preferences::get('viewRange', '1M')->data; + $last = session('end', new Carbon); + + // chart properties for cache: + $cache = new CacheProperties(); + $cache->addProperty($first); + $cache->addProperty($last); + $cache->addProperty('budget'); + if ($cache->has()) { + + return Response::json($cache->get()); + } + + $final = clone $last; + $final->addYears(2); + $last = Navigation::endOfX($last, $range, $final); + $entries = new Collection; + // get all expenses: + $spentArray = $repository->spentPerDay($budget, $first, $last); + + while ($first < $last) { + + // periodspecific dates: + $currentStart = Navigation::startOfPeriod($first, $range); + $currentEnd = Navigation::endOfPeriod($first, $range); + $spent = $this->getSumOfRange($currentStart, $currentEnd, $spentArray); + $entry = [$first, ($spent * -1)]; + + $entries->push($entry); + $first = Navigation::addPeriod($first, $range, 0); + } + + $data = $this->generator->budgetLimit($entries, 'month'); + $cache->store($data); + + return Response::json($data); + } + /** * Shows the amount left in a specific budget limit. *