Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Some work on the chart controller. [skip ci]
James Cole9054b207002f
diff --git a/app/assets/javascripts/firefly/accounts.js b/app/assets/javascripts/firefly/accounts.jsindex 67040dd4d5..63efd1db42 100644--- a/app/assets/javascripts/firefly/accounts.js+++ b/app/assets/javascripts/firefly/accounts.js@@ -10,9 +10,9 @@ if($('#chart').length == 1) {type: 'line'},- series: data,+ series: data.series,title: {- text: 'BETTER TITLE HERE'+ text: data.chart_title},yAxis: {formatter: function () {@@ -20,7 +20,7 @@ if($('#chart').length == 1) {}},subtitle: {- text: '<a href="#">View more</a>',+ text: data.subtitle,useHTML: true},diff --git a/app/assets/javascripts/firefly/index.js b/app/assets/javascripts/firefly/index.jsindex e077bc457c..b46c80c64a 100644--- a/app/assets/javascripts/firefly/index.js+++ b/app/assets/javascripts/firefly/index.js@@ -11,9 +11,9 @@ $(function () {type: 'line'},- series: data,+ series: data.series,title: {- text: 'All accounts'+ text: data.chart_title},yAxis: {formatter: function () {@@ -21,7 +21,7 @@ $(function () {}},subtitle: {- text: '<a href="#">View more</a>',+ text: data.subtitle,useHTML: true},diff --git a/app/controllers/ChartController.php b/app/controllers/ChartController.phpindex 04eaae3091..d6870547f9 100644--- a/app/controllers/ChartController.php+++ b/app/controllers/ChartController.php@@ -1,11 +1,6 @@<?php-use Firefly\Exception\FireflyException;-use Firefly\Helper\Preferences\PreferencesHelperInterface as PHI;-use Firefly\Helper\Toolkit\ToolkitInterface as tk;-use Firefly\Storage\Account\AccountRepositoryInterface as ARI;-use Firefly\Storage\Budget\BudgetRepositoryInterface as BRI;-use Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface as TJRI;+use Firefly\Helper\Controllers\ChartInterface;/*** Class ChartController@@ -13,80 +8,31 @@ use Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface asclass ChartController extends BaseController{- protected $_accounts;- protected $_journals;- protected $_tk;- protected $_preferences;- protected $_budgets;+ protected $_chart;/**- * @param ARI $accounts- * @param TJRI $journals- * @param PHI $preferences- * @param tk $toolkit- * @param BRI $budgets+ * @param ChartInterface $chart*/- public function __construct(ARI $accounts, TJRI $journals, PHI $preferences, tk $toolkit, BRI $budgets)+ public function __construct(ChartInterface $chart){- $this->_accounts = $accounts;- $this->_journals = $journals;- $this->_preferences = $preferences;- $this->_tk = $toolkit;- $this->_budgets = $budgets;+ $this->_chart = $chart;}/**- * @param null $accountId- *- * @return \Illuminate\Http\JsonResponse+ * @param Account $account+ * @return mixed*/- public function homeAccount($accountId = null)+ public function homeAccount(Account $account = null){- list($start, $end) = $this->_tk->getDateRangeDates();- $current = clone $start;- $return = [];-- $account = !is_null($accountId) ? $this->_accounts->find($accountId) : null;- $today = new Carbon\Carbon;- if (is_null($account)) {- $pref = $this->_preferences->get('frontpageAccounts', []);- if ($pref->data == []) {- $accounts = $this->_accounts->getActiveDefault();- } else {- $accounts = $this->_accounts->getByIds($pref->data);- }- foreach ($accounts as $account) {- $return[] = ['name' => $account->name, 'id' => 'acc-' . $account->id, 'data' => []];-- }- while ($current <= $end) {- // loop accounts:- foreach ($accounts as $index => $account) {- if ($current > $today) {- $return[$index]['data'][] = [$current->timestamp * 1000, $account->predict(clone $current)];- } else {- $return[$index]['data'][] = [$current->timestamp * 1000, $account->balance(clone $current)];- }- }- $current->addDay();- }+ if (!is_null($account)) {+ $data = $this->_chart->account($account);} else {- $return[0] = ['name' => $account->name, 'id' => $account->id, 'data' => []];- while ($current <= $end) {- if ($current > $today) {- $return[0]['data'][] = [$current->timestamp * 1000, $account->predict(clone $current)];- } else {- $return[0]['data'][] = [$current->timestamp * 1000, $account->balance(clone $current)];- }-- $current->addDay();- }+ $data = $this->_chart->accounts();}-- return Response::json($return);+ return Response::json($data);}/**diff --git a/app/lib/Firefly/Helper/Controllers/Chart.php b/app/lib/Firefly/Helper/Controllers/Chart.phpnew file mode 100644index 0000000000..afbaf7d271--- /dev/null+++ b/app/lib/Firefly/Helper/Controllers/Chart.php@@ -0,0 +1,71 @@+<?php+/**+ * Created by PhpStorm.+ * User: User+ * Date: 29-7-14+ * Time: 10:41+ */++namespace Firefly\Helper\Controllers;++use Carbon\Carbon;++class Chart implements ChartInterface+{++ public function account(\Account $account)+ {+ $data = [+ 'chart_title' => $account->name,+ 'subtitle' => '<a href="' . route('accounts.show', [$account->id]) . '">View more</a>',+ 'series' => [$this->_account($account)]+ ];+ return $data;+ }++ public function accounts()+ {+ $data = [+ 'chart_title' => 'All accounts',+ 'subtitle' => '<a href="' . route('accounts.index') . '">View more</a>',+ 'series' => []+ ];+ /** @var \Firefly\Helper\Preferences\PreferencesHelperInterface $prefs */+ $prefs = \App::make('Firefly\Helper\Preferences\PreferencesHelperInterface');+ $pref = $prefs->get('frontpageAccounts', []);++ /** @var \Firefly\Storage\Account\AccountRepositoryInterface $acct */+ $acct = \App::make('Firefly\Storage\Account\AccountRepositoryInterface');++ if ($pref->data == []) {+ $accounts = $acct->getActiveDefault();+ } else {+ $accounts = $acct->getByIds($pref->data);+ }+ foreach($accounts as $account) {+ $data['series'][] = $this->_account($account);+ }+ return $data;++ }++ protected function _account(\Account $account)+ {+ $start = \Session::get('start');+ $end = \Session::get('end');+ $current = clone $start;+ $today = new Carbon;+ $return = ['name' => $account->name, 'id' => $account->id, 'data' => []];+ while ($current <= $end) {+ if ($current > $today) {+ $return['data'][] = [$current->timestamp * 1000, $account->predict(clone $current)];+ } else {+ $return['data'][] = [$current->timestamp * 1000, $account->balance(clone $current)];+ }++ $current->addDay();+ }+ return $return;+ }++}\ No newline at end of filediff --git a/app/lib/Firefly/Helper/Controllers/ChartInterface.php b/app/lib/Firefly/Helper/Controllers/ChartInterface.phpnew file mode 100644index 0000000000..5ad025d894--- /dev/null+++ b/app/lib/Firefly/Helper/Controllers/ChartInterface.php@@ -0,0 +1,17 @@+<?php+/**+ * Created by PhpStorm.+ * User: User+ * Date: 29-7-14+ * Time: 10:42+ */++namespace Firefly\Helper\Controllers;+++interface ChartInterface+{++ public function account(\Account $account);+ public function accounts();+}\ No newline at end of filediff --git a/app/lib/Firefly/Helper/HelperServiceProvider.php b/app/lib/Firefly/Helper/HelperServiceProvider.phpindex 843db0d16c..c60f39553a 100644--- a/app/lib/Firefly/Helper/HelperServiceProvider.php+++ b/app/lib/Firefly/Helper/HelperServiceProvider.php@@ -22,6 +22,10 @@ class HelperServiceProvider extends ServiceProvider'Firefly\Helper\Controllers\AccountInterface','Firefly\Helper\Controllers\Account');+ $this->app->bind(+ 'Firefly\Helper\Controllers\ChartInterface',+ 'Firefly\Helper\Controllers\Chart'+ );$this->app->bind('Firefly\Helper\Controllers\BudgetInterface',diff --git a/app/routes.php b/app/routes.phpindex c65281d2d7..49d5962f03 100644--- a/app/routes.php+++ b/app/routes.php@@ -30,7 +30,7 @@ Route::group(['before' => 'auth'], function () {Route::get('/flush', ['uses' => 'HomeController@flush', 'as' => 'flush']);// chart controller- Route::get('/chart/home/account/{accountname?}', ['uses' => 'ChartController@homeAccount', 'as' => 'chart.home']);+ Route::get('/chart/home/account/{account?}', ['uses' => 'ChartController@homeAccount', 'as' => 'chart.home']);Route::get('/chart/home/categories', ['uses' => 'ChartController@homeCategories', 'as' => 'chart.categories']);Route::get('/chart/home/budgets', ['uses' => 'ChartController@homeBudgets', 'as' => 'chart.budgets']);Route::get('/chart/home/info/{accountname}/{day}/{month}/{year}', ['uses' => 'ChartController@homeAccountInfo', 'as' => 'chart.info']);