Skip to content

Firefly III: a personal finances manager

Budget charts #452

James Coleaea31b5e287f
diff --git a/app/Generator/Chart/Budget/BudgetChartGeneratorInterface.php b/app/Generator/Chart/Budget/BudgetChartGeneratorInterface.php
deleted file mode 100644
index 1abaa89746..0000000000
--- a/app/Generator/Chart/Budget/BudgetChartGeneratorInterface.php
+++ /dev/null
@@ -1,54 +0,0 @@
-<?php
-/**
- * BudgetChartGeneratorInterface.php
- * Copyright (C) 2016 [email protected]
- *
- * This software may be modified and distributed under the terms of the
- * Creative Commons Attribution-ShareAlike 4.0 International License.
- *
- * See the LICENSE file for details.
- */
-
-declare(strict_types = 1);
-
-namespace FireflyIII\Generator\Chart\Budget;
-
-use Illuminate\Support\Collection;
-
-/**
- * Interface BudgetChartGeneratorInterface
- *
- * @package FireflyIII\Generator\Chart\Budget
- */
-interface BudgetChartGeneratorInterface
-{
-
- /**
- * @param Collection $entries
- * @param string $dateFormat
- *
- * @return array
- */
- public function budgetLimit(Collection $entries, string $dateFormat): array;
-
- /**
- * @param Collection $entries
- *
- * @return array
- */
- public function frontpage(Collection $entries): array;
-
- /**
- * @param array $entries
- *
- * @return array
- */
- public function period(array $entries): array;
-
- /**
- * @param array $entries
- *
- * @return array
- */
- public function periodNoBudget(array $entries): array;
-}
diff --git a/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php b/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php
deleted file mode 100644
index bec7872b28..0000000000
--- a/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php
+++ /dev/null
@@ -1,164 +0,0 @@
-<?php
-/**
- * ChartJsBudgetChartGenerator.php
- * Copyright (C) 2016 [email protected]
- *
- * This software may be modified and distributed under the terms of the
- * Creative Commons Attribution-ShareAlike 4.0 International License.
- *
- * See the LICENSE file for details.
- */
-
-declare(strict_types = 1);
-namespace FireflyIII\Generator\Chart\Budget;
-
-
-use Illuminate\Support\Collection;
-
-/**
- * Class ChartJsBudgetChartGenerator
- *
- * @package FireflyIII\Generator\Chart\Budget
- */
-class ChartJsBudgetChartGenerator implements BudgetChartGeneratorInterface
-{
- /**
- *
- * @param Collection $entries
- * @param string $dateFormat
- *
- * @return array
- */
- public function budgetLimit(Collection $entries, string $dateFormat = 'month_and_day'): array
- {
- $format = strval(trans('config.' . $dateFormat));
- $data = [
- 'labels' => [],
- 'datasets' => [
- [
- 'label' => 'Amount',
- 'data' => [],
- ],
- ],
- ];
-
- /** @var array $entry */
- foreach ($entries as $entry) {
- $data['labels'][] = $entry[0]->formatLocalized($format);
- $data['datasets'][0]['data'][] = $entry[1];
-
- }
-
- $data['count'] = count($data['datasets']);
-
- return $data;
- }
-
- /**
- * @param Collection $entries
- *
- * @return array
- */
- public function frontpage(Collection $entries): array
- {
- $data = [
- 'count' => 0,
- 'labels' => [],
- 'datasets' => [],
- ];
- $left = [];
- $spent = [];
- $overspent = [];
- $filtered = $entries->filter(
- function ($entry) {
- return ($entry[1] != 0 || $entry[2] != 0 || $entry[3] != 0);
- }
- );
- foreach ($filtered as $entry) {
- $data['labels'][] = $entry[0];
- $left[] = round($entry[1], 2);
- $spent[] = round(bcmul($entry[2], '-1'), 2); // spent is coming in negative, must be positive
- $overspent[] = round(bcmul($entry[3], '-1'), 2); // same
- }
-
- $data['datasets'][] = [
- 'label' => trans('firefly.overspent'),
- 'data' => $overspent,
- ];
- $data['datasets'][] = [
- 'label' => trans('firefly.left'),
- 'data' => $left,
- ];
- $data['datasets'][] = [
- 'label' => trans('firefly.spent'),
- 'data' => $spent,
- ];
-
- $data['count'] = 3;
-
- return $data;
- }
-
- /**
- * @param array $entries
- *
- * @return array
- */
- public function period(array $entries): array
- {
-
- $data = [
- 'labels' => array_keys($entries),
- 'datasets' => [
- 0 => [
- 'label' => trans('firefly.budgeted'),
- 'data' => [],
- ],
- 1 => [
- 'label' => trans('firefly.spent'),
- 'data' => [],
- ],
- ],
- 'count' => 2,
- ];
-
- foreach ($entries as $label => $entry) {
- // data set 0 is budgeted
- // data set 1 is spent:
- $data['datasets'][0]['data'][] = $entry['budgeted'];
- $data['datasets'][1]['data'][] = round(($entry['spent'] * -1), 2);
-
- }
-
- return $data;
-
- }
-
- /**
- * @param array $entries
- *
- * @return array
- */
- public function periodNoBudget(array $entries): array
- {
- $data = [
- 'labels' => array_keys($entries),
- 'datasets' => [
- 0 => [
- 'label' => trans('firefly.spent'),
- 'data' => [],
- ],
- ],
- 'count' => 1,
- ];
-
- foreach ($entries as $label => $entry) {
- // data set 0 is budgeted
- // data set 1 is spent:
- $data['datasets'][0]['data'][] = round(($entry['spent'] * -1), 2);
-
- }
-
- return $data;
- }
-}
diff --git a/app/Http/Controllers/Chart/BudgetController.php b/app/Http/Controllers/Chart/BudgetController.php
index 3dc6b43620..b17bac12fe 100644
--- a/app/Http/Controllers/Chart/BudgetController.php
+++ b/app/Http/Controllers/Chart/BudgetController.php
@@ -15,7 +15,6 @@ namespace FireflyIII\Http\Controllers\Chart;
use Carbon\Carbon;
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
-use FireflyIII\Generator\Chart\Budget\BudgetChartGeneratorInterface;
use FireflyIII\Helpers\Collector\JournalCollector;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Budget;
@@ -37,7 +36,7 @@ use Response;
class BudgetController extends Controller
{
- /** @var BudgetChartGeneratorInterface */
+ /** @var GeneratorInterface */
protected $generator;
/**
@@ -46,8 +45,7 @@ class BudgetController extends Controller
public function __construct()
{
parent::__construct();
- // create chart generator:
- $this->generator = app(BudgetChartGeneratorInterface::class);
+ $this->generator = app(GeneratorInterface::class);
}
/**
@@ -92,9 +90,7 @@ class BudgetController extends Controller
$first = Navigation::addPeriod($first, $range, 0);
}
- /** @var GeneratorInterface $generator */
- $generator = app(GeneratorInterface::class);
- $data = $generator->singleSet(strval(trans('firefly.spent')), $entries);
+ $data = $this->generator->singleSet(strval(trans('firefly.spent')), $entries);
$cache->store($data);
@@ -135,9 +131,7 @@ class BudgetController extends Controller
$start->addDay();
}
- /** @var GeneratorInterface $generator */
- $generator = app(GeneratorInterface::class);
- $data = $generator->singleSet(strval(trans('firefly.left')), $entries);
+ $data = $this->generator->singleSet(strval(trans('firefly.left')), $entries);
$cache->store($data);
return Response::json($data);
@@ -216,9 +210,7 @@ class BudgetController extends Controller
$chartData[2]['entries'][$row['name']] = bcmul($row['repetition_overspent'], '-1');
}
- /** @var GeneratorInterface $generator */
- $generator = app(GeneratorInterface::class);
- $data = $generator->multiSet($chartData);
+ $data = $this->generator->multiSet($chartData);
$cache->store($data);
return Response::json($data);
@@ -244,7 +236,7 @@ class BudgetController extends Controller
$cache->addProperty($budget->id);
$cache->addProperty('chart.budget.period');
if ($cache->has()) {
- //return Response::json($cache->get());
+ return Response::json($cache->get());
}
// get the expenses
@@ -297,9 +289,7 @@ class BudgetController extends Controller
$chartData[1]['entries'][$label] = $limit;
}
- /** @var GeneratorInterface $generator */
- $generator = app(GeneratorInterface::class);
- $data = $generator->multiSet($chartData);
+ $data = $this->generator->multiSet($chartData);
$cache->store($data);
return Response::json($data);
@@ -322,7 +312,7 @@ class BudgetController extends Controller
$cache->addProperty($accounts);
$cache->addProperty('chart.budget.no-budget');
if ($cache->has()) {
- // return Response::json($cache->get());
+ return Response::json($cache->get());
}
// the expenses:
@@ -336,9 +326,7 @@ class BudgetController extends Controller
$spent = isset($entries['entries'][$period]) ? $entries['entries'][$period] : '0';
$chartData[$label] = bcmul($spent, '-1');
}
- /** @var GeneratorInterface $generator */
- $generator = app(GeneratorInterface::class);
- $data = $generator->singleSet(strval(trans('firefly.spent')), $chartData);
+ $data = $this->generator->singleSet(strval(trans('firefly.spent')), $chartData);
$cache->store($data);
return Response::json($data);