Skip to content

Firefly III: a personal finances manager

Piggy banks and #452

James Colecdd988b4de66
diff --git a/app/Generator/Chart/PiggyBank/ChartJsPiggyBankChartGenerator.php b/app/Generator/Chart/PiggyBank/ChartJsPiggyBankChartGenerator.php
deleted file mode 100644
index 2908fa0661..0000000000
--- a/app/Generator/Chart/PiggyBank/ChartJsPiggyBankChartGenerator.php
+++ /dev/null
@@ -1,58 +0,0 @@
-<?php
-/**
- * ChartJsPiggyBankChartGenerator.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\PiggyBank;
-
-use Carbon\Carbon;
-use Illuminate\Support\Collection;
-
-
-/**
- * Class ChartJsPiggyBankChartGenerator
- *
- * @package FireflyIII\Generator\Chart\PiggyBank
- */
-class ChartJsPiggyBankChartGenerator implements PiggyBankChartGeneratorInterface
-{
-
- /**
- * @param Collection $set
- *
- * @return array
- */
- public function history(Collection $set): array
- {
-
- // language:
- $format = (string)trans('config.month_and_day');
-
- $data = [
- 'count' => 1,
- 'labels' => [],
- 'datasets' => [
- [
- 'label' => 'Diff',
- 'data' => [],
- ],
- ],
- ];
- $sum = '0';
- foreach ($set as $key => $value) {
- $date = new Carbon($key);
- $sum = bcadd($sum, $value);
- $data['labels'][] = $date->formatLocalized($format);
- $data['datasets'][0]['data'][] = round($sum, 2);
- }
-
- return $data;
- }
-}
diff --git a/app/Generator/Chart/PiggyBank/PiggyBankChartGeneratorInterface.php b/app/Generator/Chart/PiggyBank/PiggyBankChartGeneratorInterface.php
deleted file mode 100644
index 2841cd928e..0000000000
--- a/app/Generator/Chart/PiggyBank/PiggyBankChartGeneratorInterface.php
+++ /dev/null
@@ -1,31 +0,0 @@
-<?php
-/**
- * PiggyBankChartGeneratorInterface.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\PiggyBank;
-
-use Illuminate\Support\Collection;
-
-/**
- * Interface PiggyBankChartGeneratorInterface
- *
- * @package FireflyIII\Generator\Chart\PiggyBank
- */
-interface PiggyBankChartGeneratorInterface
-{
- /**
- * @param Collection $set
- *
- * @return array
- */
- public function history(Collection $set): array;
-}
diff --git a/app/Http/Controllers/Chart/PiggyBankController.php b/app/Http/Controllers/Chart/PiggyBankController.php
index bdcf1408f5..aa11d8bbf0 100644
--- a/app/Http/Controllers/Chart/PiggyBankController.php
+++ b/app/Http/Controllers/Chart/PiggyBankController.php
@@ -13,13 +13,12 @@ declare(strict_types = 1);
namespace FireflyIII\Http\Controllers\Chart;
-use FireflyIII\Generator\Chart\PiggyBank\PiggyBankChartGeneratorInterface;
+use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\PiggyBankEvent;
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
use FireflyIII\Support\CacheProperties;
-use Illuminate\Support\Collection;
use Response;
@@ -31,7 +30,7 @@ use Response;
class PiggyBankController extends Controller
{
- /** @var PiggyBankChartGeneratorInterface */
+ /** @var GeneratorInterface */
protected $generator;
/**
@@ -41,7 +40,7 @@ class PiggyBankController extends Controller
{
parent::__construct();
// create chart generator:
- $this->generator = app(PiggyBankChartGeneratorInterface::class);
+ $this->generator = app(GeneratorInterface::class);
}
/**
@@ -56,26 +55,24 @@ class PiggyBankController extends Controller
{
// chart properties for cache:
$cache = new CacheProperties;
- $cache->addProperty('piggy-history');
+ $cache->addProperty('chart.piggy-bank.history');
$cache->addProperty($piggyBank->id);
if ($cache->has()) {
return Response::json($cache->get());
}
- $set = $repository->getEvents($piggyBank);
- $set = $set->reverse();
- $collection = [];
+ $set = $repository->getEvents($piggyBank);
+ $set = $set->reverse();
+ $chartData = [];
+ $sum = '0';
/** @var PiggyBankEvent $entry */
foreach ($set as $entry) {
- $date = $entry->date->format('Y-m-d');
- $amount = $entry->amount;
- if (isset($collection[$date])) {
- $amount = bcadd($amount, $collection[$date]);
- }
- $collection[$date] = $amount;
+ $label = $entry->date->formatLocalized(strval(trans('config.month_and_day')));
+ $sum = bcadd($sum, $entry->amount);
+ $chartData[$label] = $sum;
}
- $data = $this->generator->history(new Collection($collection));
+ $data = $this->generator->singleSet($piggyBank->name, $chartData);
$cache->store($data);
return Response::json($data);