Some work on the chart controller. [skip ci]

This commit is contained in:
James Cole 2014-07-29 12:55:41 +02:00
parent 225cfc590c
commit 9054b20700
7 changed files with 111 additions and 73 deletions

View File

@ -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
},

View File

@ -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
},

View File

@ -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 as
class 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);
}
/**

View File

@ -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;
}
}

View File

@ -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();
}

View File

@ -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',

View File

@ -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']);