Skip to content

Firefly III: a personal finances manager

Outsourced a chart to a specialised Google chart thing.

James Cole63ff01e78d9c
diff --git a/app/Generator/Chart/Account/AccountChartGenerator.php b/app/Generator/Chart/Account/AccountChartGenerator.php
new file mode 100644
index 0000000000..6d72af2407
--- /dev/null
+++ b/app/Generator/Chart/Account/AccountChartGenerator.php
@@ -0,0 +1,23 @@
+<?php
+
+namespace FireflyIII\Generator\Chart\Account;
+use Illuminate\Support\Collection;
+use Carbon\Carbon;
+
+/**
+ * Interface AccountChartGenerator
+ *
+ * @package FireflyIII\Generator\Chart\Account
+ */
+interface AccountChartGenerator
+{
+
+ /**
+ * @param Collection $accounts
+ * @param Carbon $start
+ * @param Carbon $end
+ *
+ * @return array
+ */
+ public function all(Collection $accounts, Carbon $start, Carbon $end);
+}
\ No newline at end of file
diff --git a/app/Generator/Chart/Account/GoogleAccountChartGenerator.php b/app/Generator/Chart/Account/GoogleAccountChartGenerator.php
new file mode 100644
index 0000000000..c9561fbef1
--- /dev/null
+++ b/app/Generator/Chart/Account/GoogleAccountChartGenerator.php
@@ -0,0 +1,56 @@
+<?php
+
+namespace FireflyIII\Generator\Chart\Account;
+
+use Carbon\Carbon;
+use Grumpydictator\Gchart\GChart;
+use Illuminate\Support\Collection;
+use Steam;
+
+
+/**
+ * Class GoogleAccountChartGenerator
+ *
+ * @package FireflyIII\Generator\Chart\Account
+ */
+class GoogleAccountChartGenerator implements AccountChartGenerator
+{
+
+
+ /**
+ * @param Collection $accounts
+ * @param Carbon $start
+ * @param Carbon $end
+ *
+ * @return array
+ */
+ public function all(Collection $accounts, Carbon $start, Carbon $end)
+ {
+ // make chart (original code):
+ $chart = new GChart;
+ $chart->addColumn(trans('firefly.dayOfMonth'), 'date');
+ $index = 1;
+ /** @var Account $account */
+ foreach ($accounts as $account) {
+ $chart->addColumn(trans('firefly.balanceFor', ['name' => $account->name]), 'number');
+ $chart->addCertainty($index);
+ $index++;
+ }
+ $current = clone $start;
+ $current->subDay();
+ $today = Carbon::now();
+ while ($end >= $current) {
+ $row = [clone $current];
+ $certain = $current < $today;
+ foreach ($accounts as $account) {
+ $row[] = Steam::balance($account, $current);
+ $row[] = $certain;
+ }
+ $chart->addRowArray($row);
+ $current->addDay();
+ }
+ $chart->generate();
+
+ return $chart->getData();
+ }
+}
\ No newline at end of file
diff --git a/app/Http/Controllers/Chart/AccountController.php b/app/Http/Controllers/Chart/AccountController.php
index 2c5d4f7a6e..2fec3f2619 100644
--- a/app/Http/Controllers/Chart/AccountController.php
+++ b/app/Http/Controllers/Chart/AccountController.php
@@ -2,6 +2,7 @@
namespace FireflyIII\Http\Controllers\Chart;
+use App;
use Carbon\Carbon;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Account;
@@ -21,10 +22,24 @@ use Steam;
*/
class AccountController extends Controller
{
+
+ /** @var \FireflyIII\Generator\Chart\Account\AccountChartGenerator */
+ protected $generator;
+
+ /**
+ *
+ */
+ public function __construct()
+ {
+ parent::__construct();
+ // create chart generator:
+ $generator = App::make('FireflyIII\Generator\Chart\Account\AccountChartGenerator');
+ }
+
+
/**
* Shows the balances for all the user's accounts.
*
- * @param GChart $chart
* @param AccountRepositoryInterface $repository
*
* @param $year
@@ -33,7 +48,7 @@ class AccountController extends Controller
*
* @return \Symfony\Component\HttpFoundation\Response
*/
- public function all(GChart $chart, AccountRepositoryInterface $repository, $year, $month, $shared = false)
+ public function all(AccountRepositoryInterface $repository, $year, $month, $shared = false)
{
$start = new Carbon($year . '-' . $month . '-01');
$end = clone $start;
@@ -49,9 +64,6 @@ class AccountController extends Controller
return Response::json($cache->get()); // @codeCoverageIgnore
}
-
- $chart->addColumn(trans('firefly.dayOfMonth'), 'date');
-
/** @var Collection $accounts */
$accounts = $repository->getAccounts(['Default account', 'Asset account']);
if ($shared === false) {
@@ -63,31 +75,8 @@ class AccountController extends Controller
}
}
- //
-
- $index = 1;
- /** @var Account $account */
- foreach ($accounts as $account) {
- $chart->addColumn(trans('firefly.balanceFor', ['name' => $account->name]), 'number');
- $chart->addCertainty($index);
- $index++;
- }
- $current = clone $start;
- $current->subDay();
- $today = Carbon::now();
- while ($end >= $current) {
- $row = [clone $current];
- $certain = $current < $today;
- foreach ($accounts as $account) {
- $row[] = Steam::balance($account, $current);
- $row[] = $certain;
- }
- $chart->addRowArray($row);
- $current->addDay();
- }
- $chart->generate();
-
- $data = $chart->getData();
+ // make chart:
+ $data = $this->generator->all($accounts, $start, $end);
$cache->store($data);
return Response::json($data);
diff --git a/app/Providers/FireflyServiceProvider.php b/app/Providers/FireflyServiceProvider.php
index 7429939d67..1f8c4d0a3a 100644
--- a/app/Providers/FireflyServiceProvider.php
+++ b/app/Providers/FireflyServiceProvider.php
@@ -91,6 +91,9 @@ class FireflyServiceProvider extends ServiceProvider
$this->app->bind('FireflyIII\Repositories\Tag\TagRepositoryInterface', 'FireflyIII\Repositories\Tag\TagRepository');
$this->app->bind('FireflyIII\Support\Search\SearchInterface', 'FireflyIII\Support\Search\Search');
+ // make Google charts:
+ $this->app->bind('FireflyIII\Generator\Chart\Account\AccountChartGenerator', 'FireflyIII\Generator\Chart\Account\GoogleAccountChartGenerator');
+
$this->app->bind('FireflyIII\Helpers\Help\HelpInterface', 'FireflyIII\Helpers\Help\Help');
$this->app->bind('FireflyIII\Helpers\Report\ReportHelperInterface', 'FireflyIII\Helpers\Report\ReportHelper');